John Reck | 5a77537 | 2011-06-17 14:27:24 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.browser; |
| 17 | |
| 18 | import android.accounts.Account; |
| 19 | import android.accounts.AccountManager; |
| 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.ContentResolver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.database.Cursor; |
| 25 | import android.net.Uri; |
| 26 | import android.provider.BrowserContract; |
| 27 | import android.provider.BrowserContract.Accounts; |
| 28 | import android.provider.BrowserContract.Bookmarks; |
| 29 | import android.text.TextUtils; |
| 30 | |
| 31 | public 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 Murdoch | 914c559 | 2011-08-01 13:58:47 +0100 | [diff] [blame] | 49 | mContext = context.getApplicationContext(); |
John Reck | 5a77537 | 2011-06-17 14:27:24 -0700 | [diff] [blame] | 50 | } |
| 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 Monsen | 2b00b82 | 2011-07-11 17:49:46 +0100 | [diff] [blame] | 66 | c.close(); |
John Reck | 5a77537 | 2011-06-17 14:27:24 -0700 | [diff] [blame] | 67 | } |
| 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 | } |