diff options
Diffstat (limited to 'src/com/android/settings/accounts/ManageAccountsSettings.java')
-rw-r--r-- | src/com/android/settings/accounts/ManageAccountsSettings.java | 145 |
1 files changed, 117 insertions, 28 deletions
diff --git a/src/com/android/settings/accounts/ManageAccountsSettings.java b/src/com/android/settings/accounts/ManageAccountsSettings.java index 184f680..074176b 100644 --- a/src/com/android/settings/accounts/ManageAccountsSettings.java +++ b/src/com/android/settings/accounts/ManageAccountsSettings.java @@ -18,7 +18,7 @@ package com.android.settings.accounts; import android.accounts.Account; import android.accounts.AccountManager; -import android.accounts.OnAccountsUpdateListener; +import android.accounts.AuthenticatorDescription; import android.app.ActionBar; import android.app.Activity; import android.content.ContentResolver; @@ -26,12 +26,17 @@ import android.content.Intent; import android.content.SyncAdapterType; import android.content.SyncInfo; import android.content.SyncStatusInfo; +import android.content.pm.ActivityInfo; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.os.UserHandle; import android.preference.Preference; -import android.preference.PreferenceActivity; +import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceScreen; import android.util.Log; import android.view.LayoutInflater; @@ -45,16 +50,20 @@ import android.widget.TextView; import com.android.settings.AccountPreference; import com.android.settings.R; +import com.android.settings.SettingsActivity; import com.android.settings.Utils; import com.android.settings.location.LocationSettings; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; +import java.util.List; + +import static android.content.Intent.EXTRA_USER; /** Manages settings for Google Account. */ public class ManageAccountsSettings extends AccountPreferenceBase - implements OnAccountsUpdateListener { + implements AuthenticatorHelper.OnAccountsUpdateListener { 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_LABEL = "account_label"; @@ -92,8 +101,9 @@ public class ManageAccountsSettings extends AccountPreferenceBase @Override public void onStart() { super.onStart(); - Activity activity = getActivity(); - AccountManager.get(activity).addOnAccountsUpdatedListener(this, null, true); + mAuthenticatorHelper.listenToAccountUpdates(); + updateAuthDescriptions(); + showAccountsIfNeeded(); } @Override @@ -121,14 +131,13 @@ public class ManageAccountsSettings extends AccountPreferenceBase if (args != null && args.containsKey(KEY_ACCOUNT_LABEL)) { getActivity().setTitle(args.getString(KEY_ACCOUNT_LABEL)); } - updateAuthDescriptions(); } @Override public void onStop() { super.onStop(); final Activity activity = getActivity(); - AccountManager.get(activity).removeOnAccountsUpdatedListener(this); + mAuthenticatorHelper.stopListeningToAccountUpdates(); activity.getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM); activity.getActionBar().setCustomView(null); } @@ -146,7 +155,8 @@ public class ManageAccountsSettings extends AccountPreferenceBase private void startAccountSettings(AccountPreference acctPref) { Bundle args = new Bundle(); args.putParcelable(AccountSyncSettings.ACCOUNT_KEY, acctPref.getAccount()); - ((PreferenceActivity) getActivity()).startPreferencePanel( + args.putParcelable(EXTRA_USER, mUserHandle); + ((SettingsActivity) getActivity()).startPreferencePanel( AccountSyncSettings.class.getCanonicalName(), args, R.string.account_sync_settings_title, acctPref.getAccount().name, this, REQUEST_SHOW_SYNC_SETTINGS); @@ -166,7 +176,8 @@ public class ManageAccountsSettings extends AccountPreferenceBase @Override public void onPrepareOptionsMenu(Menu 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_CANCEL_ID).setVisible(syncActive && mFirstAccount != null); } @@ -185,7 +196,8 @@ public class ManageAccountsSettings extends AccountPreferenceBase } private void requestOrCancelSyncForAccounts(boolean sync) { - SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes(); + final int userId = mUserHandle.getIdentifier(); + SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(userId); Bundle extras = new Bundle(); extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); int count = getPreferenceScreen().getPreferenceCount(); @@ -198,11 +210,13 @@ public class ManageAccountsSettings extends AccountPreferenceBase for (int j = 0; j < syncAdapters.length; j++) { SyncAdapterType sa = syncAdapters[j]; if (syncAdapters[j].accountType.equals(mAccountType) - && ContentResolver.getSyncAutomatically(account, sa.authority)) { + && ContentResolver.getSyncAutomaticallyAsUser(account, sa.authority, + userId)) { if (sync) { - ContentResolver.requestSync(account, sa.authority, extras); + ContentResolver.requestSyncAsUser(account, sa.authority, userId, + extras); } else { - ContentResolver.cancelSync(account, sa.authority); + ContentResolver.cancelSyncAsUser(account, sa.authority, userId); } } } @@ -212,17 +226,23 @@ public class ManageAccountsSettings extends AccountPreferenceBase @Override protected void onSyncStateUpdated() { + showSyncState(); + } + + private void showSyncState() { // Catch any delayed delivery of update messages if (getActivity() == null) return; + final int userId = mUserHandle.getIdentifier(); + // 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 Date date = new Date(); // 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>(); for (int k = 0, n = syncAdapters.length; k < n; k++) { final SyncAdapterType sa = syncAdapters[k]; @@ -245,15 +265,11 @@ public class ManageAccountsSettings extends AccountPreferenceBase boolean syncingNow = false; if (authorities != null) { for (String authority : authorities) { - SyncStatusInfo status = ContentResolver.getSyncStatus(account, authority); - boolean syncEnabled = ContentResolver.getSyncAutomatically(account, authority) - && ContentResolver.getMasterSyncAutomatically() - && (ContentResolver.getIsSyncable(account, authority) > 0); + SyncStatusInfo status = ContentResolver.getSyncStatusAsUser(account, authority, + userId); + boolean syncEnabled = isSyncEnabled(userId, account, authority); boolean authorityIsPending = ContentResolver.isSyncPending(account, authority); - boolean activelySyncing = currentSync != null - && currentSync.authority.equals(authority) - && new Account(currentSync.account.name, currentSync.account.type) - .equals(account); + boolean activelySyncing = isSyncing(currentSyncs, account, authority); boolean lastSyncFailed = status != null && syncEnabled && status.lastFailureTime != 0 @@ -299,9 +315,34 @@ public class ManageAccountsSettings extends AccountPreferenceBase 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 - public void onAccountsUpdated(Account[] accounts) { + public void onAccountsUpdate(UserHandle userHandle) { + showAccountsIfNeeded(); + onSyncStateUpdated(); + } + + private void showAccountsIfNeeded() { if (getActivity() == null) return; + Account[] accounts = AccountManager.get(getActivity()).getAccountsAsUser( + mUserHandle.getIdentifier()); getPreferenceScreen().removeAll(); mFirstAccount = null; addPreferencesFromResource(R.xml.manage_accounts_settings); @@ -341,7 +382,6 @@ public class ManageAccountsSettings extends AccountPreferenceBase settingsTop.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); getActivity().startActivity(settingsTop); } - onSyncStateUpdated(); } private void addAuthenticatorSettings() { @@ -368,7 +408,7 @@ public class ManageAccountsSettings extends AccountPreferenceBase @Override public boolean onPreferenceClick(Preference preference) { - ((PreferenceActivity) getActivity()).startPreferencePanel( + ((SettingsActivity) getActivity()).startPreferencePanel( mClass, null, mTitleRes, null, null, 0); // Hack: announce that the Google account preferences page is launching the location // settings @@ -388,7 +428,7 @@ public class ManageAccountsSettings extends AccountPreferenceBase * intent, and hack the location settings to start it as a fragment. */ private void updatePreferenceIntents(PreferenceScreen prefs) { - PackageManager pm = getActivity().getPackageManager(); + final PackageManager pm = getActivity().getPackageManager(); for (int i = 0; i < prefs.getPreferenceCount();) { Preference pref = prefs.getPreference(i); Intent intent = pref.getIntent(); @@ -415,13 +455,36 @@ public class ManageAccountsSettings extends AccountPreferenceBase LocationSettings.class.getName(), R.string.location_settings_title)); } else { - ResolveInfo ri = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); + ResolveInfo ri = pm.resolveActivityAsUser(intent, + PackageManager.MATCH_DEFAULT_ONLY, mUserHandle.getIdentifier()); if (ri == null) { prefs.removePreference(pref); continue; } else { intent.putExtra(ACCOUNT_KEY, mFirstAccount); intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); + pref.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + Intent prefIntent = preference.getIntent(); + /* + * Check the intent to see if it resolves to a exported=false + * activity that doesn't share a uid with the authenticator. + * + * Otherwise the intent is considered unsafe in that it will be + * exploiting the fact that settings has system privileges. + */ + if (isSafeIntent(pm, prefIntent)) { + getActivity().startActivityAsUser(prefIntent, mUserHandle); + } else { + Log.e(TAG, + "Refusing to launch authenticator intent because" + + "it exploits Settings permissions: " + + prefIntent); + } + return true; + } + }); } } } @@ -429,6 +492,32 @@ public class ManageAccountsSettings extends AccountPreferenceBase } } + /** + * Determines if the supplied Intent is safe. A safe intent is one that is + * will launch a exported=true activity or owned by the same uid as the + * authenticator supplying the intent. + */ + private boolean isSafeIntent(PackageManager pm, Intent intent) { + AuthenticatorDescription authDesc = + mAuthenticatorHelper.getAccountTypeDescription(mAccountType); + ResolveInfo resolveInfo = pm.resolveActivity(intent, 0); + if (resolveInfo == null) { + return false; + } + ActivityInfo resolvedActivityInfo = resolveInfo.activityInfo; + ApplicationInfo resolvedAppInfo = resolvedActivityInfo.applicationInfo; + try { + ApplicationInfo authenticatorAppInf = pm.getApplicationInfo(authDesc.packageName, 0); + return resolvedActivityInfo.exported + || resolvedAppInfo.uid == authenticatorAppInf.uid; + } catch (NameNotFoundException e) { + Log.e(TAG, + "Intent considered unsafe due to exception.", + e); + return false; + } + } + @Override protected void onAuthDescriptionsUpdated() { // Update account icons for all account preference items |