Merge "Improve UI pref when sync/cancel account syncs."
This commit is contained in:
154
src/com/android/settings/accounts/AccountPreference.java
Normal file
154
src/com/android/settings/accounts/AccountPreference.java
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.accounts;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceViewHolder;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* AccountPreference is used to display a username, status and provider icon for an account on
|
||||
* the device.
|
||||
*/
|
||||
public class AccountPreference extends Preference {
|
||||
private static final String TAG = "AccountPreference";
|
||||
public static final int SYNC_ENABLED = 0; // all know sync adapters are enabled and OK
|
||||
public static final int SYNC_DISABLED = 1; // no sync adapters are enabled
|
||||
public static final int SYNC_ERROR = 2; // one or more sync adapters have a problem
|
||||
public static final int SYNC_IN_PROGRESS = 3; // currently syncing
|
||||
private int mStatus;
|
||||
private Account mAccount;
|
||||
private ArrayList<String> mAuthorities;
|
||||
private ImageView mSyncStatusIcon;
|
||||
private boolean mShowTypeIcon;
|
||||
|
||||
public AccountPreference(Context context, Account account, Drawable icon,
|
||||
ArrayList<String> authorities, boolean showTypeIcon) {
|
||||
super(context);
|
||||
mAccount = account;
|
||||
mAuthorities = authorities;
|
||||
mShowTypeIcon = showTypeIcon;
|
||||
if (showTypeIcon) {
|
||||
setIcon(icon);
|
||||
} else {
|
||||
setIcon(getSyncStatusIcon(SYNC_DISABLED));
|
||||
}
|
||||
setTitle(mAccount.name);
|
||||
setSummary("");
|
||||
setPersistent(false);
|
||||
setSyncStatus(SYNC_DISABLED, false);
|
||||
}
|
||||
|
||||
public Account getAccount() {
|
||||
return mAccount;
|
||||
}
|
||||
|
||||
public ArrayList<String> getAuthorities() {
|
||||
return mAuthorities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder view) {
|
||||
super.onBindViewHolder(view);
|
||||
if (!mShowTypeIcon) {
|
||||
mSyncStatusIcon = (ImageView) view.findViewById(android.R.id.icon);
|
||||
mSyncStatusIcon.setImageResource(getSyncStatusIcon(mStatus));
|
||||
mSyncStatusIcon.setContentDescription(getSyncContentDescription(mStatus));
|
||||
}
|
||||
}
|
||||
|
||||
public void setSyncStatus(int status, boolean updateSummary) {
|
||||
if (mStatus == status) {
|
||||
Log.d(TAG, "Status is the same, not changing anything");
|
||||
return;
|
||||
}
|
||||
mStatus = status;
|
||||
if (!mShowTypeIcon && mSyncStatusIcon != null) {
|
||||
mSyncStatusIcon.setImageResource(getSyncStatusIcon(status));
|
||||
mSyncStatusIcon.setContentDescription(getSyncContentDescription(mStatus));
|
||||
}
|
||||
if (updateSummary) {
|
||||
setSummary(getSyncStatusMessage(status));
|
||||
}
|
||||
}
|
||||
|
||||
private int getSyncStatusMessage(int status) {
|
||||
int res;
|
||||
switch (status) {
|
||||
case SYNC_ENABLED:
|
||||
res = R.string.sync_enabled;
|
||||
break;
|
||||
case SYNC_DISABLED:
|
||||
res = R.string.sync_disabled;
|
||||
break;
|
||||
case SYNC_ERROR:
|
||||
res = R.string.sync_error;
|
||||
break;
|
||||
case SYNC_IN_PROGRESS:
|
||||
res = R.string.sync_in_progress;
|
||||
break;
|
||||
default:
|
||||
res = R.string.sync_error;
|
||||
Log.e(TAG, "Unknown sync status: " + status);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private int getSyncStatusIcon(int status) {
|
||||
int res;
|
||||
switch (status) {
|
||||
case SYNC_ENABLED:
|
||||
case SYNC_IN_PROGRESS:
|
||||
res = R.drawable.ic_settings_sync;
|
||||
break;
|
||||
case SYNC_DISABLED:
|
||||
res = R.drawable.ic_sync_grey_holo;
|
||||
break;
|
||||
case SYNC_ERROR:
|
||||
res = R.drawable.ic_sync_red_holo;
|
||||
break;
|
||||
default:
|
||||
res = R.drawable.ic_sync_red_holo;
|
||||
Log.e(TAG, "Unknown sync status: " + status);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private String getSyncContentDescription(int status) {
|
||||
switch (status) {
|
||||
case SYNC_ENABLED:
|
||||
return getContext().getString(R.string.accessibility_sync_enabled);
|
||||
case SYNC_DISABLED:
|
||||
return getContext().getString(R.string.accessibility_sync_disabled);
|
||||
case SYNC_ERROR:
|
||||
return getContext().getString(R.string.accessibility_sync_error);
|
||||
case SYNC_IN_PROGRESS:
|
||||
return getContext().getString(R.string.accessibility_sync_in_progress);
|
||||
default:
|
||||
Log.e(TAG, "Unknown sync status: " + status);
|
||||
return getContext().getString(R.string.accessibility_sync_error);
|
||||
}
|
||||
}
|
||||
}
|
@@ -32,6 +32,7 @@ import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.text.format.DateFormat;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settings.SettingsPreferenceFragment;
|
||||
@@ -46,6 +47,7 @@ abstract class AccountPreferenceBase extends SettingsPreferenceFragment
|
||||
implements AuthenticatorHelper.OnAccountsUpdateListener {
|
||||
|
||||
protected static final String TAG = "AccountSettings";
|
||||
protected static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
|
||||
|
||||
public static final String AUTHORITIES_FILTER_KEY = "authorities";
|
||||
public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types";
|
||||
|
@@ -34,9 +34,11 @@ import android.content.pm.ResolveInfo;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.Preference.OnPreferenceClickListener;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.util.ArraySet;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
@@ -47,7 +49,6 @@ import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.AccountPreference;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.Utils;
|
||||
@@ -56,8 +57,8 @@ import com.android.settingslib.accounts.AuthenticatorHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static android.content.Intent.EXTRA_USER;
|
||||
|
||||
@@ -74,7 +75,7 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
"com.android.settings.accounts.LAUNCHING_LOCATION_SETTINGS";
|
||||
|
||||
private static final int MENU_SYNC_NOW_ID = Menu.FIRST;
|
||||
private static final int MENU_SYNC_CANCEL_ID = Menu.FIRST + 1;
|
||||
private static final int MENU_SYNC_CANCEL_ID = Menu.FIRST + 1;
|
||||
|
||||
private static final int REQUEST_SHOW_SYNC_SETTINGS = 1;
|
||||
|
||||
@@ -87,6 +88,8 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
// mFirstAccount is used for the injected preferences
|
||||
private Account mFirstAccount;
|
||||
|
||||
protected Set<String> mUserFacingSyncAuthorities;
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.ACCOUNTS_MANAGE_ACCOUNTS;
|
||||
@@ -131,7 +134,7 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
final Activity activity = getActivity();
|
||||
final View view = getView();
|
||||
|
||||
mErrorInfoView = (TextView)view.findViewById(R.id.sync_settings_error_info);
|
||||
mErrorInfoView = (TextView) view.findViewById(R.id.sync_settings_error_info);
|
||||
mErrorInfoView.setVisibility(View.GONE);
|
||||
|
||||
mAuthorities = activity.getIntent().getStringArrayExtra(AUTHORITIES_FILTER_KEY);
|
||||
@@ -188,8 +191,7 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
@Override
|
||||
public void onPrepareOptionsMenu(Menu menu) {
|
||||
super.onPrepareOptionsMenu(menu);
|
||||
boolean syncActive = !ContentResolver.getCurrentSyncsAsUser(
|
||||
mUserHandle.getIdentifier()).isEmpty();
|
||||
boolean syncActive = !getCurrentSyncs(mUserHandle.getIdentifier()).isEmpty();
|
||||
menu.findItem(MENU_SYNC_NOW_ID).setVisible(!syncActive);
|
||||
menu.findItem(MENU_SYNC_CANCEL_ID).setVisible(syncActive);
|
||||
}
|
||||
@@ -197,12 +199,12 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case MENU_SYNC_NOW_ID:
|
||||
requestOrCancelSyncForAccounts(true);
|
||||
return true;
|
||||
case MENU_SYNC_CANCEL_ID:
|
||||
requestOrCancelSyncForAccounts(false);
|
||||
return true;
|
||||
case MENU_SYNC_NOW_ID:
|
||||
requestOrCancelSyncForAccounts(true);
|
||||
return true;
|
||||
case MENU_SYNC_CANCEL_ID:
|
||||
requestOrCancelSyncForAccounts(false);
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
@@ -223,7 +225,7 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
SyncAdapterType sa = syncAdapters[j];
|
||||
if (syncAdapters[j].accountType.equals(mAccountType)
|
||||
&& ContentResolver.getSyncAutomaticallyAsUser(account, sa.authority,
|
||||
userId)) {
|
||||
userId)) {
|
||||
if (sync) {
|
||||
ContentResolver.requestSyncAsUser(account, sa.authority, userId,
|
||||
extras);
|
||||
@@ -238,47 +240,58 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
|
||||
@Override
|
||||
protected void onSyncStateUpdated() {
|
||||
showSyncState();
|
||||
// Catch any delayed delivery of update messages
|
||||
final Activity activity = getActivity();
|
||||
if (activity != null) {
|
||||
activity.invalidateOptionsMenu();
|
||||
// Catch any delayed delivery of update messages
|
||||
if (activity == null || activity.isFinishing()) {
|
||||
return;
|
||||
}
|
||||
showSyncState();
|
||||
activity.invalidateOptionsMenu();
|
||||
}
|
||||
|
||||
private void tryInitUserFacingSyncAuthorities(int userId) {
|
||||
if (mUserFacingSyncAuthorities != null) {
|
||||
return;
|
||||
}
|
||||
mUserFacingSyncAuthorities = new ArraySet<>();
|
||||
|
||||
// only track userfacing sync adapters when deciding if account is synced or not
|
||||
final SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(userId);
|
||||
for (int k = 0, n = syncAdapters.length; k < n; k++) {
|
||||
final SyncAdapterType sa = syncAdapters[k];
|
||||
if (sa.isUserVisible()) {
|
||||
mUserFacingSyncAuthorities.add(sa.authority);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the sync state of the accounts. Note: it must be called after the accounts have been
|
||||
* loaded, @see #showAccountsIfNeeded().
|
||||
* loaded.
|
||||
*
|
||||
* @see {@link #showAccountsIfNeeded()}.
|
||||
*/
|
||||
private void showSyncState() {
|
||||
// Catch any delayed delivery of update messages
|
||||
if (getActivity() == null || getActivity().isFinishing()) return;
|
||||
|
||||
@VisibleForTesting
|
||||
void showSyncState() {
|
||||
final int userId = mUserHandle.getIdentifier();
|
||||
tryInitUserFacingSyncAuthorities(userId);
|
||||
|
||||
// iterate over all the preferences, setting the state properly for each
|
||||
List<SyncInfo> currentSyncs = ContentResolver.getCurrentSyncsAsUser(userId);
|
||||
final List<SyncInfo> currentSyncs = getCurrentSyncs(userId);
|
||||
|
||||
boolean anySyncFailed = false; // true if sync on any account failed
|
||||
Date date = new Date();
|
||||
|
||||
// only track userfacing sync adapters when deciding if account is synced or not
|
||||
final SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(userId);
|
||||
HashSet<String> userFacing = new HashSet<String>();
|
||||
for (int k = 0, n = syncAdapters.length; k < n; k++) {
|
||||
final SyncAdapterType sa = syncAdapters[k];
|
||||
if (sa.isUserVisible()) {
|
||||
userFacing.add(sa.authority);
|
||||
}
|
||||
}
|
||||
for (int i = 0, count = getPreferenceScreen().getPreferenceCount(); i < count; i++) {
|
||||
Preference pref = getPreferenceScreen().getPreference(i);
|
||||
if (! (pref instanceof AccountPreference)) {
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
final int prefCount = screen.getPreferenceCount();
|
||||
for (int i = 0; i < prefCount; i++) {
|
||||
Preference pref = screen.getPreference(i);
|
||||
if (!(pref instanceof AccountPreference)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
AccountPreference accountPref = (AccountPreference) pref;
|
||||
Account account = accountPref.getAccount();
|
||||
final AccountPreference accountPref = (AccountPreference) pref;
|
||||
final Account account = accountPref.getAccount();
|
||||
int syncCount = 0;
|
||||
long lastSuccessTime = 0;
|
||||
boolean syncIsFailing = false;
|
||||
@@ -286,28 +299,33 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
boolean syncingNow = false;
|
||||
if (authorities != null) {
|
||||
for (String authority : authorities) {
|
||||
SyncStatusInfo status = ContentResolver.getSyncStatusAsUser(account, authority,
|
||||
userId);
|
||||
SyncStatusInfo status = getSyncStatusInfo(account, authority, userId);
|
||||
boolean syncEnabled = isSyncEnabled(userId, account, authority);
|
||||
boolean authorityIsPending = ContentResolver.isSyncPending(account, authority);
|
||||
boolean activelySyncing = isSyncing(currentSyncs, account, authority);
|
||||
boolean lastSyncFailed = status != null
|
||||
&& syncEnabled
|
||||
&& status.lastFailureTime != 0
|
||||
&& status.getLastFailureMesgAsInt(0)
|
||||
!= ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS;
|
||||
if (lastSyncFailed && !activelySyncing && !authorityIsPending) {
|
||||
!= ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS;
|
||||
if (lastSyncFailed && !activelySyncing
|
||||
&& !ContentResolver.isSyncPending(account, authority)) {
|
||||
syncIsFailing = true;
|
||||
anySyncFailed = true;
|
||||
break;
|
||||
}
|
||||
syncingNow |= activelySyncing;
|
||||
|
||||
if (status != null && lastSuccessTime < status.lastSuccessTime) {
|
||||
lastSuccessTime = status.lastSuccessTime;
|
||||
}
|
||||
syncCount += syncEnabled && userFacing.contains(authority) ? 1 : 0;
|
||||
syncCount += syncEnabled && mUserFacingSyncAuthorities.contains(authority)
|
||||
? 1 : 0;
|
||||
syncingNow |= activelySyncing;
|
||||
if (syncingNow) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (Log.isLoggable(TAG, Log.VERBOSE)) {
|
||||
if (VERBOSE) {
|
||||
Log.v(TAG, "no syncadapters found for " + account);
|
||||
}
|
||||
}
|
||||
@@ -332,14 +350,14 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
accountPref.setSyncStatus(AccountPreference.SYNC_DISABLED, true);
|
||||
}
|
||||
}
|
||||
|
||||
mErrorInfoView.setVisibility(anySyncFailed ? View.VISIBLE : View.GONE);
|
||||
if (mErrorInfoView != null) {
|
||||
mErrorInfoView.setVisibility(anySyncFailed ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean isSyncing(List<SyncInfo> currentSyncs, Account account, String authority) {
|
||||
final int count = currentSyncs.size();
|
||||
for (int i = 0; i < count; i++) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
SyncInfo syncInfo = currentSyncs.get(i);
|
||||
if (syncInfo.account.equals(account) && syncInfo.authority.equals(authority)) {
|
||||
return true;
|
||||
@@ -348,7 +366,8 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isSyncEnabled(int userId, Account account, String authority) {
|
||||
@VisibleForTesting
|
||||
protected boolean isSyncEnabled(int userId, Account account, String authority) {
|
||||
return ContentResolver.getSyncAutomaticallyAsUser(account, authority, userId)
|
||||
&& ContentResolver.getMasterSyncAutomaticallyAsUser(userId)
|
||||
&& (ContentResolver.getIsSyncableAsUser(account, authority, userId) > 0);
|
||||
@@ -436,7 +455,7 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
*/
|
||||
private void updatePreferenceIntents(PreferenceScreen prefs) {
|
||||
final PackageManager pm = getActivity().getPackageManager();
|
||||
for (int i = 0; i < prefs.getPreferenceCount();) {
|
||||
for (int i = 0; i < prefs.getPreferenceCount(); ) {
|
||||
Preference pref = prefs.getPreference(i);
|
||||
Intent intent = pref.getIntent();
|
||||
if (intent != null) {
|
||||
@@ -486,8 +505,8 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
} else {
|
||||
Log.e(TAG,
|
||||
"Refusing to launch authenticator intent because"
|
||||
+ "it exploits Settings permissions: "
|
||||
+ prefIntent);
|
||||
+ "it exploits Settings permissions: "
|
||||
+ prefIntent);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -536,4 +555,14 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected List<SyncInfo> getCurrentSyncs(int userId) {
|
||||
return ContentResolver.getCurrentSyncsAsUser(userId);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected SyncStatusInfo getSyncStatusInfo(Account account, String authority, int userId) {
|
||||
return ContentResolver.getSyncStatusAsUser(account, authority, userId);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user