• 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.app.settings.SettingsEnums;
23 import android.content.ContentResolver;
24 import android.content.Context;
25 import android.content.SyncAdapterType;
26 import android.os.Bundle;
27 import android.os.UserHandle;
28 
29 import androidx.annotation.VisibleForTesting;
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceScreen;
32 
33 import com.android.settings.R;
34 import com.android.settings.core.PreferenceControllerMixin;
35 import com.android.settings.core.SubSettingLauncher;
36 import com.android.settingslib.accounts.AuthenticatorHelper;
37 import com.android.settingslib.core.AbstractPreferenceController;
38 
39 public class AccountSyncPreferenceController extends AbstractPreferenceController
40         implements PreferenceControllerMixin, AuthenticatorHelper.OnAccountsUpdateListener {
41 
42     private static final String TAG = "AccountSyncController";
43     private static final String KEY_ACCOUNT_SYNC = "account_sync";
44 
45     private Account mAccount;
46     private UserHandle mUserHandle;
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         new SubSettingLauncher(mContext)
67                 .setDestination(AccountSyncSettings.class.getName())
68                 .setArguments(args)
69                 .setSourceMetricsCategory( SettingsEnums.ACCOUNT)
70                 .setTitleRes( R.string.account_sync_title)
71                 .launch();
72 
73         return true;
74     }
75 
76     @Override
getPreferenceKey()77     public String getPreferenceKey() {
78         return KEY_ACCOUNT_SYNC;
79     }
80 
81     @Override
displayPreference(PreferenceScreen screen)82     public void displayPreference(PreferenceScreen screen) {
83         super.displayPreference(screen);
84         mPreference = screen.findPreference(getPreferenceKey());
85     }
86 
87     @Override
updateState(Preference preference)88     public void updateState(Preference preference) {
89         updateSummary(preference);
90     }
91 
92     @Override
onAccountsUpdate(UserHandle userHandle)93     public void onAccountsUpdate(UserHandle userHandle) {
94         updateSummary(mPreference);
95     }
96 
init(Account account, UserHandle userHandle)97     public void init(Account account, UserHandle userHandle) {
98         mAccount = account;
99         mUserHandle = userHandle;
100     }
101 
102     @VisibleForTesting
updateSummary(Preference preference)103     void updateSummary(Preference preference) {
104         if (mAccount == null) {
105             return;
106         }
107         final int userId = mUserHandle.getIdentifier();
108         final SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(userId);
109         int total = 0;
110         int enabled = 0;
111         if (syncAdapters != null) {
112             for (int i = 0, n = syncAdapters.length; i < n; i++) {
113                 final SyncAdapterType sa = syncAdapters[i];
114                 if (!sa.accountType.equals(mAccount.type) || !sa.isUserVisible()) {
115                     continue;
116                 }
117                 final int syncState =
118                         ContentResolver.getIsSyncableAsUser(mAccount, sa.authority, userId);
119                 if (syncState > 0) {
120                     total++;
121                     final boolean syncEnabled = ContentResolver.getSyncAutomaticallyAsUser(
122                             mAccount, sa.authority, userId);
123                     final boolean oneTimeSyncMode =
124                             !ContentResolver.getMasterSyncAutomaticallyAsUser(userId);
125                     if (oneTimeSyncMode || syncEnabled) {
126                         enabled++;
127                     }
128                 }
129             }
130         }
131         if (enabled == 0) {
132             preference.setSummary(R.string.account_sync_summary_all_off);
133         } else if (enabled == total) {
134             preference.setSummary(R.string.account_sync_summary_all_on);
135         } else {
136             preference.setSummary(
137                     mContext.getString(R.string.account_sync_summary_some_on, enabled, total));
138         }
139     }
140 }
141