Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 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; |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 22 | import android.app.settings.SettingsEnums; |
Doris Ling | 771848d | 2017-06-28 13:18:56 -0700 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Doris Ling | 771848d | 2017-06-28 13:18:56 -0700 | [diff] [blame] | 25 | import android.content.SyncAdapterType; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 26 | import android.os.Bundle; |
| 27 | import android.os.UserHandle; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 28 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 29 | import androidx.annotation.VisibleForTesting; |
| 30 | import androidx.preference.Preference; |
| 31 | import androidx.preference.PreferenceScreen; |
| 32 | |
Fan Zhang | 044dc59 | 2017-04-21 13:24:34 -0700 | [diff] [blame] | 33 | import com.android.settings.R; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 34 | import com.android.settings.core.PreferenceControllerMixin; |
Fan Zhang | 47854c2 | 2018-02-19 13:54:09 -0800 | [diff] [blame] | 35 | import com.android.settings.core.SubSettingLauncher; |
Doris Ling | 771848d | 2017-06-28 13:18:56 -0700 | [diff] [blame] | 36 | import com.android.settingslib.accounts.AuthenticatorHelper; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 37 | import com.android.settingslib.core.AbstractPreferenceController; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 38 | |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 39 | public class AccountSyncPreferenceController extends AbstractPreferenceController |
Doris Ling | 771848d | 2017-06-28 13:18:56 -0700 | [diff] [blame] | 40 | implements PreferenceControllerMixin, AuthenticatorHelper.OnAccountsUpdateListener { |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 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; |
Doris Ling | 771848d | 2017-06-28 13:18:56 -0700 | [diff] [blame] | 47 | private Preference mPreference; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 48 | |
| 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 Zhang | 47854c2 | 2018-02-19 13:54:09 -0800 | [diff] [blame] | 66 | new SubSettingLauncher(mContext) |
| 67 | .setDestination(AccountSyncSettings.class.getName()) |
| 68 | .setArguments(args) |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 69 | .setSourceMetricsCategory( SettingsEnums.ACCOUNT) |
hjchangliao | e86eec0 | 2018-05-02 13:01:07 +0800 | [diff] [blame] | 70 | .setTitleRes( R.string.account_sync_title) |
Fan Zhang | 47854c2 | 2018-02-19 13:54:09 -0800 | [diff] [blame] | 71 | .launch(); |
Fan Zhang | 044dc59 | 2017-04-21 13:24:34 -0700 | [diff] [blame] | 72 | |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String getPreferenceKey() { |
| 78 | return KEY_ACCOUNT_SYNC; |
| 79 | } |
| 80 | |
Doris Ling | 771848d | 2017-06-28 13:18:56 -0700 | [diff] [blame] | 81 | @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 Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 97 | public void init(Account account, UserHandle userHandle) { |
| 98 | mAccount = account; |
| 99 | mUserHandle = userHandle; |
Doris Ling | 771848d | 2017-06-28 13:18:56 -0700 | [diff] [blame] | 100 | } |
| 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 Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 139 | } |
| 140 | } |