blob: a4d10d759d190d09dce8e300a2c1c8547bb5be54 [file] [log] [blame]
John Reck5a775372011-06-17 14:27:24 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Bijan Amirzada41242f22014-03-21 12:12:18 -070016package com.android.browser;
John Reck5a775372011-06-17 14:27:24 -070017
18import android.accounts.Account;
19import android.accounts.AccountManager;
20import android.content.BroadcastReceiver;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.database.Cursor;
25import android.net.Uri;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080026
Bijan Amirzada41242f22014-03-21 12:12:18 -070027import com.android.browser.platformsupport.BrowserContract;
28import com.android.browser.platformsupport.BrowserContract.Accounts;
29import com.android.browser.platformsupport.BrowserContract.Bookmarks;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080030
John Reck5a775372011-06-17 14:27:24 -070031import android.text.TextUtils;
32
33public class AccountsChangedReceiver extends BroadcastReceiver {
34
35 private static final String[] PROJECTION = new String[] {
36 Accounts.ACCOUNT_NAME,
37 Accounts.ACCOUNT_TYPE,
38 };
39 private static final String SELECTION = Accounts.ACCOUNT_NAME + " IS NOT NULL";
40 private static final String DELETE_SELECTION = Accounts.ACCOUNT_NAME + "=? AND "
41 + Accounts.ACCOUNT_TYPE + "=?";
42
43 @Override
44 public void onReceive(Context context, Intent intent) {
45 new DeleteRemovedAccounts(context).start();
46 }
47
48 static class DeleteRemovedAccounts extends Thread {
49 Context mContext;
50 public DeleteRemovedAccounts(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +010051 mContext = context.getApplicationContext();
John Reck5a775372011-06-17 14:27:24 -070052 }
53
54 @Override
55 public void run() {
56 Account[] accounts = AccountManager.get(mContext).getAccounts();
57 ContentResolver cr = mContext.getContentResolver();
58 Cursor c = cr.query(Accounts.CONTENT_URI, PROJECTION,
59 SELECTION, null, null);
60 while (c.moveToNext()) {
61 String name = c.getString(0);
62 String type = c.getString(1);
63 if (!contains(accounts, name, type)) {
64 delete(cr, name, type);
65 }
66 }
67 cr.update(Accounts.CONTENT_URI, null, null, null);
Kristian Monsen2b00b822011-07-11 17:49:46 +010068 c.close();
John Reck5a775372011-06-17 14:27:24 -070069 }
70
71 void delete(ContentResolver cr, String name, String type) {
72 // Pretend to be a sync adapter to delete the data and not mark
73 // it for deletion. Without this, the bookmarks will be marked to
74 // be deleted, which will propagate to the server if the account
75 // is added back.
76 Uri uri = Bookmarks.CONTENT_URI.buildUpon()
77 .appendQueryParameter(BrowserContract.CALLER_IS_SYNCADAPTER, "true")
78 .build();
79 cr.delete(uri, DELETE_SELECTION, new String[] { name, type });
80 }
81
82 boolean contains(Account[] accounts, String name, String type) {
83 for (Account a : accounts) {
84 if (TextUtils.equals(a.name, name)
85 && TextUtils.equals(a.type, type)) {
86 return true;
87 }
88 }
89 return false;
90 }
91 }
92}