Handle managing accounts for a managed profile
Bug: 15466880 Change-Id: Icf0e06c954269426b500f0e5fbcdf3346c59eb9d
This commit is contained in:
@@ -138,7 +138,8 @@ class AccountPreferenceBase extends SettingsPreferenceFragment
|
|||||||
public ArrayList<String> getAuthoritiesForAccountType(String type) {
|
public ArrayList<String> getAuthoritiesForAccountType(String type) {
|
||||||
if (mAccountTypeToAuthorities == null) {
|
if (mAccountTypeToAuthorities == null) {
|
||||||
mAccountTypeToAuthorities = Maps.newHashMap();
|
mAccountTypeToAuthorities = Maps.newHashMap();
|
||||||
SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes();
|
SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(
|
||||||
|
mUserHandle.getIdentifier());
|
||||||
for (int i = 0, n = syncAdapters.length; i < n; i++) {
|
for (int i = 0, n = syncAdapters.length; i < n; i++) {
|
||||||
final SyncAdapterType sa = syncAdapters[i];
|
final SyncAdapterType sa = syncAdapters[i];
|
||||||
ArrayList<String> authorities = mAccountTypeToAuthorities.get(sa.accountType);
|
ArrayList<String> authorities = mAccountTypeToAuthorities.get(sa.accountType);
|
||||||
|
@@ -18,7 +18,6 @@ package com.android.settings.accounts;
|
|||||||
|
|
||||||
import android.accounts.Account;
|
import android.accounts.Account;
|
||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
import android.accounts.OnAccountsUpdateListener;
|
|
||||||
import android.app.ActionBar;
|
import android.app.ActionBar;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
@@ -30,7 +29,9 @@ import android.content.pm.PackageManager;
|
|||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.UserHandle;
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
|
import android.preference.Preference.OnPreferenceClickListener;
|
||||||
import android.preference.PreferenceScreen;
|
import android.preference.PreferenceScreen;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@@ -51,10 +52,13 @@ import com.android.settings.location.LocationSettings;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static android.content.Intent.EXTRA_USER;
|
||||||
|
|
||||||
/** Manages settings for Google Account. */
|
/** Manages settings for Google Account. */
|
||||||
public class ManageAccountsSettings extends AccountPreferenceBase
|
public class ManageAccountsSettings extends AccountPreferenceBase
|
||||||
implements OnAccountsUpdateListener {
|
implements AuthenticatorHelper.OnAccountsUpdateListener {
|
||||||
private static final String ACCOUNT_KEY = "account"; // to pass to auth settings
|
private static final String ACCOUNT_KEY = "account"; // to pass to auth settings
|
||||||
public static final String KEY_ACCOUNT_TYPE = "account_type";
|
public static final String KEY_ACCOUNT_TYPE = "account_type";
|
||||||
public static final String KEY_ACCOUNT_LABEL = "account_label";
|
public static final String KEY_ACCOUNT_LABEL = "account_label";
|
||||||
@@ -92,8 +96,7 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
|||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
Activity activity = getActivity();
|
mAuthenticatorHelper.listenToAccountUpdates();
|
||||||
AccountManager.get(activity).addOnAccountsUpdatedListener(this, null, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -122,13 +125,14 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
|||||||
getActivity().setTitle(args.getString(KEY_ACCOUNT_LABEL));
|
getActivity().setTitle(args.getString(KEY_ACCOUNT_LABEL));
|
||||||
}
|
}
|
||||||
updateAuthDescriptions();
|
updateAuthDescriptions();
|
||||||
|
showAccountsIfNeeded();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStop() {
|
public void onStop() {
|
||||||
super.onStop();
|
super.onStop();
|
||||||
final Activity activity = getActivity();
|
final Activity activity = getActivity();
|
||||||
AccountManager.get(activity).removeOnAccountsUpdatedListener(this);
|
mAuthenticatorHelper.stopListeningToAccountUpdates();
|
||||||
activity.getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);
|
activity.getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);
|
||||||
activity.getActionBar().setCustomView(null);
|
activity.getActionBar().setCustomView(null);
|
||||||
}
|
}
|
||||||
@@ -146,6 +150,7 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
|||||||
private void startAccountSettings(AccountPreference acctPref) {
|
private void startAccountSettings(AccountPreference acctPref) {
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
args.putParcelable(AccountSyncSettings.ACCOUNT_KEY, acctPref.getAccount());
|
args.putParcelable(AccountSyncSettings.ACCOUNT_KEY, acctPref.getAccount());
|
||||||
|
args.putParcelable(EXTRA_USER, mUserHandle);
|
||||||
((SettingsActivity) getActivity()).startPreferencePanel(
|
((SettingsActivity) getActivity()).startPreferencePanel(
|
||||||
AccountSyncSettings.class.getCanonicalName(), args,
|
AccountSyncSettings.class.getCanonicalName(), args,
|
||||||
R.string.account_sync_settings_title, acctPref.getAccount().name,
|
R.string.account_sync_settings_title, acctPref.getAccount().name,
|
||||||
@@ -166,7 +171,8 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
|||||||
@Override
|
@Override
|
||||||
public void onPrepareOptionsMenu(Menu menu) {
|
public void onPrepareOptionsMenu(Menu menu) {
|
||||||
super.onPrepareOptionsMenu(menu);
|
super.onPrepareOptionsMenu(menu);
|
||||||
boolean syncActive = ContentResolver.getCurrentSync() != null;
|
boolean syncActive = ContentResolver.getCurrentSyncsAsUser(
|
||||||
|
mUserHandle.getIdentifier()).isEmpty();
|
||||||
menu.findItem(MENU_SYNC_NOW_ID).setVisible(!syncActive && mFirstAccount != null);
|
menu.findItem(MENU_SYNC_NOW_ID).setVisible(!syncActive && mFirstAccount != null);
|
||||||
menu.findItem(MENU_SYNC_CANCEL_ID).setVisible(syncActive && mFirstAccount != null);
|
menu.findItem(MENU_SYNC_CANCEL_ID).setVisible(syncActive && mFirstAccount != null);
|
||||||
}
|
}
|
||||||
@@ -185,7 +191,8 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void requestOrCancelSyncForAccounts(boolean sync) {
|
private void requestOrCancelSyncForAccounts(boolean sync) {
|
||||||
SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes();
|
final int userId = mUserHandle.getIdentifier();
|
||||||
|
SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(userId);
|
||||||
Bundle extras = new Bundle();
|
Bundle extras = new Bundle();
|
||||||
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
|
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
|
||||||
int count = getPreferenceScreen().getPreferenceCount();
|
int count = getPreferenceScreen().getPreferenceCount();
|
||||||
@@ -198,11 +205,13 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
|||||||
for (int j = 0; j < syncAdapters.length; j++) {
|
for (int j = 0; j < syncAdapters.length; j++) {
|
||||||
SyncAdapterType sa = syncAdapters[j];
|
SyncAdapterType sa = syncAdapters[j];
|
||||||
if (syncAdapters[j].accountType.equals(mAccountType)
|
if (syncAdapters[j].accountType.equals(mAccountType)
|
||||||
&& ContentResolver.getSyncAutomatically(account, sa.authority)) {
|
&& ContentResolver.getSyncAutomaticallyAsUser(account, sa.authority,
|
||||||
|
userId)) {
|
||||||
if (sync) {
|
if (sync) {
|
||||||
ContentResolver.requestSync(account, sa.authority, extras);
|
ContentResolver.requestSyncAsUser(account, sa.authority, userId,
|
||||||
|
extras);
|
||||||
} else {
|
} else {
|
||||||
ContentResolver.cancelSync(account, sa.authority);
|
ContentResolver.cancelSyncAsUser(account, sa.authority, userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -212,17 +221,23 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onSyncStateUpdated() {
|
protected void onSyncStateUpdated() {
|
||||||
|
showSyncState();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showSyncState() {
|
||||||
// Catch any delayed delivery of update messages
|
// Catch any delayed delivery of update messages
|
||||||
if (getActivity() == null) return;
|
if (getActivity() == null) return;
|
||||||
|
|
||||||
|
final int userId = mUserHandle.getIdentifier();
|
||||||
|
|
||||||
// iterate over all the preferences, setting the state properly for each
|
// iterate over all the preferences, setting the state properly for each
|
||||||
SyncInfo currentSync = ContentResolver.getCurrentSync();
|
List<SyncInfo> currentSyncs = ContentResolver.getCurrentSyncsAsUser(userId);
|
||||||
|
|
||||||
boolean anySyncFailed = false; // true if sync on any account failed
|
boolean anySyncFailed = false; // true if sync on any account failed
|
||||||
Date date = new Date();
|
Date date = new Date();
|
||||||
|
|
||||||
// only track userfacing sync adapters when deciding if account is synced or not
|
// only track userfacing sync adapters when deciding if account is synced or not
|
||||||
final SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes();
|
final SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(userId);
|
||||||
HashSet<String> userFacing = new HashSet<String>();
|
HashSet<String> userFacing = new HashSet<String>();
|
||||||
for (int k = 0, n = syncAdapters.length; k < n; k++) {
|
for (int k = 0, n = syncAdapters.length; k < n; k++) {
|
||||||
final SyncAdapterType sa = syncAdapters[k];
|
final SyncAdapterType sa = syncAdapters[k];
|
||||||
@@ -245,15 +260,11 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
|||||||
boolean syncingNow = false;
|
boolean syncingNow = false;
|
||||||
if (authorities != null) {
|
if (authorities != null) {
|
||||||
for (String authority : authorities) {
|
for (String authority : authorities) {
|
||||||
SyncStatusInfo status = ContentResolver.getSyncStatus(account, authority);
|
SyncStatusInfo status = ContentResolver.getSyncStatusAsUser(account, authority,
|
||||||
boolean syncEnabled = ContentResolver.getSyncAutomatically(account, authority)
|
userId);
|
||||||
&& ContentResolver.getMasterSyncAutomatically()
|
boolean syncEnabled = isSyncEnabled(userId, account, authority);
|
||||||
&& (ContentResolver.getIsSyncable(account, authority) > 0);
|
|
||||||
boolean authorityIsPending = ContentResolver.isSyncPending(account, authority);
|
boolean authorityIsPending = ContentResolver.isSyncPending(account, authority);
|
||||||
boolean activelySyncing = currentSync != null
|
boolean activelySyncing = isSyncing(currentSyncs, account, authority);
|
||||||
&& currentSync.authority.equals(authority)
|
|
||||||
&& new Account(currentSync.account.name, currentSync.account.type)
|
|
||||||
.equals(account);
|
|
||||||
boolean lastSyncFailed = status != null
|
boolean lastSyncFailed = status != null
|
||||||
&& syncEnabled
|
&& syncEnabled
|
||||||
&& status.lastFailureTime != 0
|
&& status.lastFailureTime != 0
|
||||||
@@ -299,9 +310,34 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
|||||||
mErrorInfoView.setVisibility(anySyncFailed ? View.VISIBLE : View.GONE);
|
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++) {
|
||||||
|
SyncInfo syncInfo = currentSyncs.get(i);
|
||||||
|
if (syncInfo.account.equals(account) && syncInfo.authority.equals(authority)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSyncEnabled(int userId, Account account, String authority) {
|
||||||
|
return ContentResolver.getSyncAutomaticallyAsUser(account, authority, userId)
|
||||||
|
&& ContentResolver.getMasterSyncAutomaticallyAsUser(userId)
|
||||||
|
&& (ContentResolver.getIsSyncableAsUser(account, authority, userId) > 0);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAccountsUpdated(Account[] accounts) {
|
public void onAccountsUpdate(UserHandle userHandle) {
|
||||||
|
showAccountsIfNeeded();
|
||||||
|
onSyncStateUpdated();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showAccountsIfNeeded() {
|
||||||
if (getActivity() == null) return;
|
if (getActivity() == null) return;
|
||||||
|
Account[] accounts = AccountManager.get(getActivity()).getAccountsAsUser(
|
||||||
|
mUserHandle.getIdentifier());
|
||||||
getPreferenceScreen().removeAll();
|
getPreferenceScreen().removeAll();
|
||||||
mFirstAccount = null;
|
mFirstAccount = null;
|
||||||
addPreferencesFromResource(R.xml.manage_accounts_settings);
|
addPreferencesFromResource(R.xml.manage_accounts_settings);
|
||||||
@@ -341,7 +377,6 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
|||||||
settingsTop.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
settingsTop.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
getActivity().startActivity(settingsTop);
|
getActivity().startActivity(settingsTop);
|
||||||
}
|
}
|
||||||
onSyncStateUpdated();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addAuthenticatorSettings() {
|
private void addAuthenticatorSettings() {
|
||||||
@@ -415,13 +450,22 @@ public class ManageAccountsSettings extends AccountPreferenceBase
|
|||||||
LocationSettings.class.getName(),
|
LocationSettings.class.getName(),
|
||||||
R.string.location_settings_title));
|
R.string.location_settings_title));
|
||||||
} else {
|
} else {
|
||||||
ResolveInfo ri = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
|
ResolveInfo ri = pm.resolveActivityAsUser(intent,
|
||||||
|
PackageManager.MATCH_DEFAULT_ONLY, mUserHandle.getIdentifier());
|
||||||
if (ri == null) {
|
if (ri == null) {
|
||||||
prefs.removePreference(pref);
|
prefs.removePreference(pref);
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
intent.putExtra(ACCOUNT_KEY, mFirstAccount);
|
intent.putExtra(ACCOUNT_KEY, mFirstAccount);
|
||||||
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
|
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onPreferenceClick(Preference preference) {
|
||||||
|
getActivity().startActivityAsUser(preference.getIntent(),
|
||||||
|
mUserHandle);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user