• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.settings.accounts;
18 
19 import static android.content.Intent.EXTRA_USER;
20 
21 import android.accounts.Account;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.SyncAdapterType;
25 import android.os.Bundle;
26 import android.os.UserHandle;
27 import android.support.annotation.VisibleForTesting;
28 import android.support.v7.preference.Preference;
29 import android.support.v7.preference.PreferenceScreen;
30 
31 import com.android.internal.logging.nano.MetricsProto;
32 import com.android.settings.R;
33 import com.android.settings.Utils;
34 import com.android.settings.core.PreferenceControllerMixin;
35 import com.android.settingslib.accounts.AuthenticatorHelper;
36 import com.android.settingslib.core.AbstractPreferenceController;
37 
38 public class AccountSyncPreferenceController extends AbstractPreferenceController
39         implements PreferenceControllerMixin, AuthenticatorHelper.OnAccountsUpdateListener {
40 
41     private static final String TAG = "AccountSyncController";
42     private static final String KEY_ACCOUNT_SYNC = "account_sync";
43 
44     private Account mAccount;
45     private UserHandle mUserHandle;
46     private AuthenticatorHelper mAuthenticatorHelper;
47     private Preference mPreference;
48 
AccountSyncPreferenceController(Context context)49     public AccountSyncPreferenceController(Context context) {
50         super(context);
51     }
52 
53     @Override
isAvailable()54     public boolean isAvailable() {
55         return true;
56     }
57 
58     @Override
handlePreferenceTreeClick(Preference preference)59     public boolean handlePreferenceTreeClick(Preference preference) {
60         if (!KEY_ACCOUNT_SYNC.equals(preference.getKey())) {
61             return false;
62         }
63         final Bundle args = new Bundle();
64         args.putParcelable(AccountSyncSettings.ACCOUNT_KEY, mAccount);
65         args.putParcelable(EXTRA_USER, mUserHandle);
66         Utils.startWithFragment(mContext, AccountSyncSettings.class.getName(), args, null, 0,
67                 R.string.account_sync_title, null, MetricsProto.MetricsEvent.ACCOUNT);
68 
69         return true;
70     }
71 
72     @Override
getPreferenceKey()73     public String getPreferenceKey() {
74         return KEY_ACCOUNT_SYNC;
75     }
76 
77     @Override
displayPreference(PreferenceScreen screen)78     public void displayPreference(PreferenceScreen screen) {
79         super.displayPreference(screen);
80         mPreference = screen.findPreference(getPreferenceKey());
81     }
82 
83     @Override
updateState(Preference preference)84     public void updateState(Preference preference) {
85         updateSummary(preference);
86     }
87 
88     @Override
onAccountsUpdate(UserHandle userHandle)89     public void onAccountsUpdate(UserHandle userHandle) {
90         updateSummary(mPreference);
91     }
92 
init(Account account, UserHandle userHandle)93     public void init(Account account, UserHandle userHandle) {
94         mAccount = account;
95         mUserHandle = userHandle;
96         mAuthenticatorHelper = new AuthenticatorHelper(mContext, mUserHandle, this);
97     }
98 
99     @VisibleForTesting
updateSummary(Preference preference)100     void updateSummary(Preference preference) {
101         if (mAccount == null) {
102             return;
103         }
104         final int userId = mUserHandle.getIdentifier();
105         final SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(userId);
106         int total = 0;
107         int enabled = 0;
108         if (syncAdapters != null) {
109             for (int i = 0, n = syncAdapters.length; i < n; i++) {
110                 final SyncAdapterType sa = syncAdapters[i];
111                 if (!sa.accountType.equals(mAccount.type) || !sa.isUserVisible()) {
112                     continue;
113                 }
114                 final int syncState =
115                         ContentResolver.getIsSyncableAsUser(mAccount, sa.authority, userId);
116                 if (syncState > 0) {
117                     total++;
118                     final boolean syncEnabled = ContentResolver.getSyncAutomaticallyAsUser(
119                             mAccount, sa.authority, userId);
120                     final boolean oneTimeSyncMode =
121                             !ContentResolver.getMasterSyncAutomaticallyAsUser(userId);
122                     if (oneTimeSyncMode || syncEnabled) {
123                         enabled++;
124                     }
125                 }
126             }
127         }
128         if (enabled == 0) {
129             preference.setSummary(R.string.account_sync_summary_all_off);
130         } else if (enabled == total) {
131             preference.setSummary(R.string.account_sync_summary_all_on);
132         } else {
133             preference.setSummary(
134                     mContext.getString(R.string.account_sync_summary_some_on, enabled, total));
135         }
136     }
137 }
138