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 | */ |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 16 | package com.android.browser; |
John Reck | 5a77537 | 2011-06-17 14:27:24 -0700 | [diff] [blame] | 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; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 26 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 27 | import com.android.browser.platformsupport.BrowserContract; |
| 28 | import com.android.browser.platformsupport.BrowserContract.Accounts; |
| 29 | import com.android.browser.platformsupport.BrowserContract.Bookmarks; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 30 | |
John Reck | 5a77537 | 2011-06-17 14:27:24 -0700 | [diff] [blame] | 31 | import android.text.TextUtils; |
| 32 | |
| 33 | public 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 Murdoch | 914c559 | 2011-08-01 13:58:47 +0100 | [diff] [blame] | 51 | mContext = context.getApplicationContext(); |
John Reck | 5a77537 | 2011-06-17 14:27:24 -0700 | [diff] [blame] | 52 | } |
| 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 Monsen | 2b00b82 | 2011-07-11 17:49:46 +0100 | [diff] [blame] | 68 | c.close(); |
John Reck | 5a77537 | 2011-06-17 14:27:24 -0700 | [diff] [blame] | 69 | } |
| 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 | } |