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