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