blob: aff78ca44563151937b97e4ee39d3bdf971bcc06 [file] [log] [blame]
John Reck267b6af2011-01-19 10:15:47 -08001/*
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
17package com.android.browser;
18
John Reck99c6d332011-02-01 12:57:06 -080019import com.android.browser.widget.BookmarkThumbnailWidgetProvider;
20
John Reck267b6af2011-01-19 10:15:47 -080021import android.accounts.Account;
22import android.accounts.AccountManager;
23import android.content.BroadcastReceiver;
24import android.content.ContentResolver;
25import android.content.Context;
26import android.content.Intent;
27import android.content.SharedPreferences;
28import android.preference.PreferenceManager;
29import android.provider.BrowserContract;
30
31public 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 Reck267b6af2011-01-19 10:15:47 -080060 }
61
62}