blob: cea884397348397415435e22c6171c9988633a94 [file] [log] [blame]
Doris Ling20d4b042016-11-22 16:37:06 -08001/*
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
17package com.android.settings.accounts;
18
19import static android.content.Intent.EXTRA_USER;
20
21import android.accounts.Account;
Fan Zhang31b21002019-01-16 13:49:47 -080022import android.app.settings.SettingsEnums;
Doris Ling771848d2017-06-28 13:18:56 -070023import android.content.ContentResolver;
Doris Ling20d4b042016-11-22 16:37:06 -080024import android.content.Context;
Doris Ling771848d2017-06-28 13:18:56 -070025import android.content.SyncAdapterType;
Doris Ling20d4b042016-11-22 16:37:06 -080026import android.os.Bundle;
27import android.os.UserHandle;
Doris Ling20d4b042016-11-22 16:37:06 -080028
Fan Zhang23f8d592018-08-28 15:11:40 -070029import androidx.annotation.VisibleForTesting;
30import androidx.preference.Preference;
31import androidx.preference.PreferenceScreen;
32
Fan Zhang044dc592017-04-21 13:24:34 -070033import com.android.settings.R;
Tony Mantler1d583e12017-06-13 13:09:25 -070034import com.android.settings.core.PreferenceControllerMixin;
Fan Zhang47854c22018-02-19 13:54:09 -080035import com.android.settings.core.SubSettingLauncher;
Doris Ling771848d2017-06-28 13:18:56 -070036import com.android.settingslib.accounts.AuthenticatorHelper;
Tony Mantler1d583e12017-06-13 13:09:25 -070037import com.android.settingslib.core.AbstractPreferenceController;
Doris Ling20d4b042016-11-22 16:37:06 -080038
Tony Mantler1d583e12017-06-13 13:09:25 -070039public class AccountSyncPreferenceController extends AbstractPreferenceController
Doris Ling771848d2017-06-28 13:18:56 -070040 implements PreferenceControllerMixin, AuthenticatorHelper.OnAccountsUpdateListener {
Doris Ling20d4b042016-11-22 16:37:06 -080041
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;
Doris Ling771848d2017-06-28 13:18:56 -070047 private Preference mPreference;
Doris Ling20d4b042016-11-22 16:37:06 -080048
49 public AccountSyncPreferenceController(Context context) {
50 super(context);
51 }
52
53 @Override
54 public boolean isAvailable() {
55 return true;
56 }
57
58 @Override
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);
Fan Zhang47854c22018-02-19 13:54:09 -080066 new SubSettingLauncher(mContext)
67 .setDestination(AccountSyncSettings.class.getName())
68 .setArguments(args)
Fan Zhang31b21002019-01-16 13:49:47 -080069 .setSourceMetricsCategory( SettingsEnums.ACCOUNT)
hjchangliaoe86eec02018-05-02 13:01:07 +080070 .setTitleRes( R.string.account_sync_title)
Fan Zhang47854c22018-02-19 13:54:09 -080071 .launch();
Fan Zhang044dc592017-04-21 13:24:34 -070072
Doris Ling20d4b042016-11-22 16:37:06 -080073 return true;
74 }
75
76 @Override
77 public String getPreferenceKey() {
78 return KEY_ACCOUNT_SYNC;
79 }
80
Doris Ling771848d2017-06-28 13:18:56 -070081 @Override
82 public void displayPreference(PreferenceScreen screen) {
83 super.displayPreference(screen);
84 mPreference = screen.findPreference(getPreferenceKey());
85 }
86
87 @Override
88 public void updateState(Preference preference) {
89 updateSummary(preference);
90 }
91
92 @Override
93 public void onAccountsUpdate(UserHandle userHandle) {
94 updateSummary(mPreference);
95 }
96
Doris Ling20d4b042016-11-22 16:37:06 -080097 public void init(Account account, UserHandle userHandle) {
98 mAccount = account;
99 mUserHandle = userHandle;
Doris Ling771848d2017-06-28 13:18:56 -0700100 }
101
102 @VisibleForTesting
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 }
Doris Ling20d4b042016-11-22 16:37:06 -0800139 }
140}