blob: ba1b3cc9a86df61a320851d2f5b699f1360d12a7 [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 */
16package com.android.browser;
17
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;
26import android.provider.BrowserContract;
27import android.provider.BrowserContract.Accounts;
28import android.provider.BrowserContract.Bookmarks;
29import android.text.TextUtils;
30
31public class AccountsChangedReceiver extends BroadcastReceiver {
32
33 private static final String[] PROJECTION = new String[] {
34 Accounts.ACCOUNT_NAME,
35 Accounts.ACCOUNT_TYPE,
36 };
37 private static final String SELECTION = Accounts.ACCOUNT_NAME + " IS NOT NULL";
38 private static final String DELETE_SELECTION = Accounts.ACCOUNT_NAME + "=? AND "
39 + Accounts.ACCOUNT_TYPE + "=?";
40
41 @Override
42 public void onReceive(Context context, Intent intent) {
43 new DeleteRemovedAccounts(context).start();
44 }
45
46 static class DeleteRemovedAccounts extends Thread {
47 Context mContext;
48 public DeleteRemovedAccounts(Context context) {
Ben Murdoch914c5592011-08-01 13:58:47 +010049 mContext = context.getApplicationContext();
John Reck5a775372011-06-17 14:27:24 -070050 }
51
52 @Override
53 public void run() {
54 Account[] accounts = AccountManager.get(mContext).getAccounts();
55 ContentResolver cr = mContext.getContentResolver();
56 Cursor c = cr.query(Accounts.CONTENT_URI, PROJECTION,
57 SELECTION, null, null);
58 while (c.moveToNext()) {
59 String name = c.getString(0);
60 String type = c.getString(1);
61 if (!contains(accounts, name, type)) {
62 delete(cr, name, type);
63 }
64 }
65 cr.update(Accounts.CONTENT_URI, null, null, null);
Kristian Monsen2b00b822011-07-11 17:49:46 +010066 c.close();
John Reck5a775372011-06-17 14:27:24 -070067 }
68
69 void delete(ContentResolver cr, String name, String type) {
70 // Pretend to be a sync adapter to delete the data and not mark
71 // it for deletion. Without this, the bookmarks will be marked to
72 // be deleted, which will propagate to the server if the account
73 // is added back.
74 Uri uri = Bookmarks.CONTENT_URI.buildUpon()
75 .appendQueryParameter(BrowserContract.CALLER_IS_SYNCADAPTER, "true")
76 .build();
77 cr.delete(uri, DELETE_SELECTION, new String[] { name, type });
78 }
79
80 boolean contains(Account[] accounts, String name, String type) {
81 for (Account a : accounts) {
82 if (TextUtils.equals(a.name, name)
83 && TextUtils.equals(a.type, type)) {
84 return true;
85 }
86 }
87 return false;
88 }
89 }
90}