John Reck | 267b6af | 2011-01-19 10:15:47 -0800 | [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 | |
| 17 | package com.android.browser; |
| 18 | |
John Reck | 99c6d33 | 2011-02-01 12:57:06 -0800 | [diff] [blame] | 19 | import com.android.browser.widget.BookmarkThumbnailWidgetProvider; |
| 20 | |
John Reck | 267b6af | 2011-01-19 10:15:47 -0800 | [diff] [blame] | 21 | import android.accounts.Account; |
| 22 | import android.accounts.AccountManager; |
| 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.ContentResolver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.SharedPreferences; |
| 28 | import android.preference.PreferenceManager; |
| 29 | import android.provider.BrowserContract; |
| 30 | |
| 31 | public class AccountsChangedReceiver extends BroadcastReceiver { |
| 32 | |
| 33 | @Override |
| 34 | public void onReceive(Context context, Intent intent) { |
| 35 | // Validate that the account we are syncing to still exists |
| 36 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); |
| 37 | String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null); |
| 38 | String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null); |
| 39 | if (accountType == null || accountName == null) { |
| 40 | // Not syncing, nothing to do |
| 41 | return; |
| 42 | } |
| 43 | Account[] accounts = AccountManager.get(context).getAccountsByType(accountType); |
| 44 | for (Account a : accounts) { |
| 45 | if (accountName.equals(a.name)) { |
| 46 | // Still have a valid account, sweet |
| 47 | return; |
| 48 | } |
| 49 | } |
| 50 | // Account deleted - disable sync |
| 51 | prefs.edit() |
| 52 | .remove(BrowserBookmarksPage.PREF_ACCOUNT_TYPE) |
| 53 | .remove(BrowserBookmarksPage.PREF_ACCOUNT_NAME) |
| 54 | .commit(); |
| 55 | BrowserContract.Settings.setSyncEnabled(context, false); |
| 56 | for (Account a : accounts) { |
| 57 | ContentResolver.setSyncAutomatically(a, BrowserContract.AUTHORITY, false); |
| 58 | ContentResolver.setIsSyncable(a, BrowserContract.AUTHORITY, 0); |
| 59 | } |
John Reck | 267b6af | 2011-01-19 10:15:47 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | } |