Home | History | Annotate | Download | only in accounts
      1 /*
      2  * Copyright (C) 2008 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 android.accounts.Account;
     20 import android.accounts.AccountManager;
     21 import android.accounts.AuthenticatorDescription;
     22 import android.app.ActionBar;
     23 import android.app.Activity;
     24 import android.content.ContentResolver;
     25 import android.content.Intent;
     26 import android.content.SyncAdapterType;
     27 import android.content.SyncInfo;
     28 import android.content.SyncStatusInfo;
     29 import android.content.pm.ActivityInfo;
     30 import android.content.pm.ApplicationInfo;
     31 import android.content.pm.PackageInfo;
     32 import android.content.pm.PackageManager;
     33 import android.content.pm.PackageManager.NameNotFoundException;
     34 import android.content.pm.ResolveInfo;
     35 import android.graphics.drawable.Drawable;
     36 import android.os.Bundle;
     37 import android.os.UserHandle;
     38 import android.preference.Preference;
     39 import android.preference.Preference.OnPreferenceClickListener;
     40 import android.preference.PreferenceScreen;
     41 import android.util.Log;
     42 import android.view.LayoutInflater;
     43 import android.view.Menu;
     44 import android.view.MenuInflater;
     45 import android.view.MenuItem;
     46 import android.view.View;
     47 import android.view.ViewGroup;
     48 import android.widget.ListView;
     49 import android.widget.TextView;
     50 
     51 import com.android.settings.AccountPreference;
     52 import com.android.settings.R;
     53 import com.android.settings.SettingsActivity;
     54 import com.android.settings.Utils;
     55 import com.android.settings.location.LocationSettings;
     56 
     57 import java.util.ArrayList;
     58 import java.util.Date;
     59 import java.util.HashSet;
     60 import java.util.List;
     61 
     62 import static android.content.Intent.EXTRA_USER;
     63 
     64 /** Manages settings for Google Account. */
     65 public class ManageAccountsSettings extends AccountPreferenceBase
     66         implements AuthenticatorHelper.OnAccountsUpdateListener {
     67     private static final String ACCOUNT_KEY = "account"; // to pass to auth settings
     68     public static final String KEY_ACCOUNT_TYPE = "account_type";
     69     public static final String KEY_ACCOUNT_LABEL = "account_label";
     70 
     71     // Action name for the broadcast intent when the Google account preferences page is launching
     72     // the location settings.
     73     private static final String LAUNCHING_LOCATION_SETTINGS =
     74             "com.android.settings.accounts.LAUNCHING_LOCATION_SETTINGS";
     75 
     76     private static final int MENU_SYNC_NOW_ID = Menu.FIRST;
     77     private static final int MENU_SYNC_CANCEL_ID    = Menu.FIRST + 1;
     78 
     79     private static final int REQUEST_SHOW_SYNC_SETTINGS = 1;
     80 
     81     private String[] mAuthorities;
     82     private TextView mErrorInfoView;
     83 
     84     // If an account type is set, then show only accounts of that type
     85     private String mAccountType;
     86     // Temporary hack, to deal with backward compatibility
     87     private Account mFirstAccount;
     88 
     89     @Override
     90     public void onCreate(Bundle icicle) {
     91         super.onCreate(icicle);
     92 
     93         Bundle args = getArguments();
     94         if (args != null && args.containsKey(KEY_ACCOUNT_TYPE)) {
     95             mAccountType = args.getString(KEY_ACCOUNT_TYPE);
     96         }
     97         addPreferencesFromResource(R.xml.manage_accounts_settings);
     98         setHasOptionsMenu(true);
     99     }
    100 
    101     @Override
    102     public void onStart() {
    103         super.onStart();
    104         mAuthenticatorHelper.listenToAccountUpdates();
    105         updateAuthDescriptions();
    106         showAccountsIfNeeded();
    107     }
    108 
    109     @Override
    110     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    111             Bundle savedInstanceState) {
    112         final View view = inflater.inflate(R.layout.manage_accounts_screen, container, false);
    113         final ListView list = (ListView) view.findViewById(android.R.id.list);
    114         Utils.prepareCustomPreferencesList(container, view, list, false);
    115         return view;
    116     }
    117 
    118     @Override
    119     public void onActivityCreated(Bundle savedInstanceState) {
    120         super.onActivityCreated(savedInstanceState);
    121 
    122         final Activity activity = getActivity();
    123         final View view = getView();
    124 
    125         mErrorInfoView = (TextView)view.findViewById(R.id.sync_settings_error_info);
    126         mErrorInfoView.setVisibility(View.GONE);
    127 
    128         mAuthorities = activity.getIntent().getStringArrayExtra(AUTHORITIES_FILTER_KEY);
    129 
    130         Bundle args = getArguments();
    131         if (args != null && args.containsKey(KEY_ACCOUNT_LABEL)) {
    132             getActivity().setTitle(args.getString(KEY_ACCOUNT_LABEL));
    133         }
    134     }
    135 
    136     @Override
    137     public void onStop() {
    138         super.onStop();
    139         final Activity activity = getActivity();
    140         mAuthenticatorHelper.stopListeningToAccountUpdates();
    141         activity.getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);
    142         activity.getActionBar().setCustomView(null);
    143     }
    144 
    145     @Override
    146     public boolean onPreferenceTreeClick(PreferenceScreen preferences, Preference preference) {
    147         if (preference instanceof AccountPreference) {
    148             startAccountSettings((AccountPreference) preference);
    149         } else {
    150             return false;
    151         }
    152         return true;
    153     }
    154 
    155     private void startAccountSettings(AccountPreference acctPref) {
    156         Bundle args = new Bundle();
    157         args.putParcelable(AccountSyncSettings.ACCOUNT_KEY, acctPref.getAccount());
    158         args.putParcelable(EXTRA_USER, mUserHandle);
    159         ((SettingsActivity) getActivity()).startPreferencePanel(
    160                 AccountSyncSettings.class.getCanonicalName(), args,
    161                 R.string.account_sync_settings_title, acctPref.getAccount().name,
    162                 this, REQUEST_SHOW_SYNC_SETTINGS);
    163     }
    164 
    165     @Override
    166     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    167         MenuItem syncNow = menu.add(0, MENU_SYNC_NOW_ID, 0,
    168                 getString(R.string.sync_menu_sync_now))
    169                 .setIcon(R.drawable.ic_menu_refresh_holo_dark);
    170         MenuItem syncCancel = menu.add(0, MENU_SYNC_CANCEL_ID, 0,
    171                 getString(R.string.sync_menu_sync_cancel))
    172                 .setIcon(com.android.internal.R.drawable.ic_menu_close_clear_cancel);
    173         super.onCreateOptionsMenu(menu, inflater);
    174     }
    175 
    176     @Override
    177     public void onPrepareOptionsMenu(Menu menu) {
    178         super.onPrepareOptionsMenu(menu);
    179         boolean syncActive = ContentResolver.getCurrentSyncsAsUser(
    180                 mUserHandle.getIdentifier()).isEmpty();
    181         menu.findItem(MENU_SYNC_NOW_ID).setVisible(!syncActive && mFirstAccount != null);
    182         menu.findItem(MENU_SYNC_CANCEL_ID).setVisible(syncActive && mFirstAccount != null);
    183     }
    184 
    185     @Override
    186     public boolean onOptionsItemSelected(MenuItem item) {
    187         switch (item.getItemId()) {
    188         case MENU_SYNC_NOW_ID:
    189             requestOrCancelSyncForAccounts(true);
    190             return true;
    191         case MENU_SYNC_CANCEL_ID:
    192             requestOrCancelSyncForAccounts(false);
    193             return true;
    194         }
    195         return super.onOptionsItemSelected(item);
    196     }
    197 
    198     private void requestOrCancelSyncForAccounts(boolean sync) {
    199         final int userId = mUserHandle.getIdentifier();
    200         SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(userId);
    201         Bundle extras = new Bundle();
    202         extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    203         int count = getPreferenceScreen().getPreferenceCount();
    204         // For each account
    205         for (int i = 0; i < count; i++) {
    206             Preference pref = getPreferenceScreen().getPreference(i);
    207             if (pref instanceof AccountPreference) {
    208                 Account account = ((AccountPreference) pref).getAccount();
    209                 // For all available sync authorities, sync those that are enabled for the account
    210                 for (int j = 0; j < syncAdapters.length; j++) {
    211                     SyncAdapterType sa = syncAdapters[j];
    212                     if (syncAdapters[j].accountType.equals(mAccountType)
    213                             && ContentResolver.getSyncAutomaticallyAsUser(account, sa.authority,
    214                                     userId)) {
    215                         if (sync) {
    216                             ContentResolver.requestSyncAsUser(account, sa.authority, userId,
    217                                     extras);
    218                         } else {
    219                             ContentResolver.cancelSyncAsUser(account, sa.authority, userId);
    220                         }
    221                     }
    222                 }
    223             }
    224         }
    225     }
    226 
    227     @Override
    228     protected void onSyncStateUpdated() {
    229         showSyncState();
    230     }
    231 
    232     private void showSyncState() {
    233         // Catch any delayed delivery of update messages
    234         if (getActivity() == null) return;
    235 
    236         final int userId = mUserHandle.getIdentifier();
    237 
    238         // iterate over all the preferences, setting the state properly for each
    239         List<SyncInfo> currentSyncs = ContentResolver.getCurrentSyncsAsUser(userId);
    240 
    241         boolean anySyncFailed = false; // true if sync on any account failed
    242         Date date = new Date();
    243 
    244         // only track userfacing sync adapters when deciding if account is synced or not
    245         final SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(userId);
    246         HashSet<String> userFacing = new HashSet<String>();
    247         for (int k = 0, n = syncAdapters.length; k < n; k++) {
    248             final SyncAdapterType sa = syncAdapters[k];
    249             if (sa.isUserVisible()) {
    250                 userFacing.add(sa.authority);
    251             }
    252         }
    253         for (int i = 0, count = getPreferenceScreen().getPreferenceCount(); i < count; i++) {
    254             Preference pref = getPreferenceScreen().getPreference(i);
    255             if (! (pref instanceof AccountPreference)) {
    256                 continue;
    257             }
    258 
    259             AccountPreference accountPref = (AccountPreference) pref;
    260             Account account = accountPref.getAccount();
    261             int syncCount = 0;
    262             long lastSuccessTime = 0;
    263             boolean syncIsFailing = false;
    264             final ArrayList<String> authorities = accountPref.getAuthorities();
    265             boolean syncingNow = false;
    266             if (authorities != null) {
    267                 for (String authority : authorities) {
    268                     SyncStatusInfo status = ContentResolver.getSyncStatusAsUser(account, authority,
    269                             userId);
    270                     boolean syncEnabled = isSyncEnabled(userId, account, authority);
    271                     boolean authorityIsPending = ContentResolver.isSyncPending(account, authority);
    272                     boolean activelySyncing = isSyncing(currentSyncs, account, authority);
    273                     boolean lastSyncFailed = status != null
    274                             && syncEnabled
    275                             && status.lastFailureTime != 0
    276                             && status.getLastFailureMesgAsInt(0)
    277                                != ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS;
    278                     if (lastSyncFailed && !activelySyncing && !authorityIsPending) {
    279                         syncIsFailing = true;
    280                         anySyncFailed = true;
    281                     }
    282                     syncingNow |= activelySyncing;
    283                     if (status != null && lastSuccessTime < status.lastSuccessTime) {
    284                         lastSuccessTime = status.lastSuccessTime;
    285                     }
    286                     syncCount += syncEnabled && userFacing.contains(authority) ? 1 : 0;
    287                 }
    288             } else {
    289                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
    290                     Log.v(TAG, "no syncadapters found for " + account);
    291                 }
    292             }
    293             if (syncIsFailing) {
    294                 accountPref.setSyncStatus(AccountPreference.SYNC_ERROR, true);
    295             } else if (syncCount == 0) {
    296                 accountPref.setSyncStatus(AccountPreference.SYNC_DISABLED, true);
    297             } else if (syncCount > 0) {
    298                 if (syncingNow) {
    299                     accountPref.setSyncStatus(AccountPreference.SYNC_IN_PROGRESS, true);
    300                 } else {
    301                     accountPref.setSyncStatus(AccountPreference.SYNC_ENABLED, true);
    302                     if (lastSuccessTime > 0) {
    303                         accountPref.setSyncStatus(AccountPreference.SYNC_ENABLED, false);
    304                         date.setTime(lastSuccessTime);
    305                         final String timeString = formatSyncDate(date);
    306                         accountPref.setSummary(getResources().getString(
    307                                 R.string.last_synced, timeString));
    308                     }
    309                 }
    310             } else {
    311                 accountPref.setSyncStatus(AccountPreference.SYNC_DISABLED, true);
    312             }
    313         }
    314 
    315         mErrorInfoView.setVisibility(anySyncFailed ? View.VISIBLE : View.GONE);
    316     }
    317 
    318 
    319     private boolean isSyncing(List<SyncInfo> currentSyncs, Account account, String authority) {
    320         final int count = currentSyncs.size();
    321         for (int i = 0; i < count;  i++) {
    322             SyncInfo syncInfo = currentSyncs.get(i);
    323             if (syncInfo.account.equals(account) && syncInfo.authority.equals(authority)) {
    324                 return true;
    325             }
    326         }
    327         return false;
    328     }
    329 
    330     private boolean isSyncEnabled(int userId, Account account, String authority) {
    331         return ContentResolver.getSyncAutomaticallyAsUser(account, authority, userId)
    332                 && ContentResolver.getMasterSyncAutomaticallyAsUser(userId)
    333                 && (ContentResolver.getIsSyncableAsUser(account, authority, userId) > 0);
    334     }
    335 
    336     @Override
    337     public void onAccountsUpdate(UserHandle userHandle) {
    338         showAccountsIfNeeded();
    339         onSyncStateUpdated();
    340     }
    341 
    342     private void showAccountsIfNeeded() {
    343         if (getActivity() == null) return;
    344         Account[] accounts = AccountManager.get(getActivity()).getAccountsAsUser(
    345                 mUserHandle.getIdentifier());
    346         getPreferenceScreen().removeAll();
    347         mFirstAccount = null;
    348         addPreferencesFromResource(R.xml.manage_accounts_settings);
    349         for (int i = 0, n = accounts.length; i < n; i++) {
    350             final Account account = accounts[i];
    351             // If an account type is specified for this screen, skip other types
    352             if (mAccountType != null && !account.type.equals(mAccountType)) continue;
    353             final ArrayList<String> auths = getAuthoritiesForAccountType(account.type);
    354 
    355             boolean showAccount = true;
    356             if (mAuthorities != null && auths != null) {
    357                 showAccount = false;
    358                 for (String requestedAuthority : mAuthorities) {
    359                     if (auths.contains(requestedAuthority)) {
    360                         showAccount = true;
    361                         break;
    362                     }
    363                 }
    364             }
    365 
    366             if (showAccount) {
    367                 final Drawable icon = getDrawableForType(account.type);
    368                 final AccountPreference preference =
    369                         new AccountPreference(getActivity(), account, icon, auths, false);
    370                 getPreferenceScreen().addPreference(preference);
    371                 if (mFirstAccount == null) {
    372                     mFirstAccount = account;
    373                     getActivity().invalidateOptionsMenu();
    374                 }
    375             }
    376         }
    377         if (mAccountType != null && mFirstAccount != null) {
    378             addAuthenticatorSettings();
    379         } else {
    380             // There's no account, reset to top-level of settings
    381             Intent settingsTop = new Intent(android.provider.Settings.ACTION_SETTINGS);
    382             settingsTop.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    383             getActivity().startActivity(settingsTop);
    384         }
    385     }
    386 
    387     private void addAuthenticatorSettings() {
    388         PreferenceScreen prefs = addPreferencesForType(mAccountType, getPreferenceScreen());
    389         if (prefs != null) {
    390             updatePreferenceIntents(prefs);
    391         }
    392     }
    393 
    394     /** Listens to a preference click event and starts a fragment */
    395     private class FragmentStarter
    396             implements Preference.OnPreferenceClickListener {
    397         private final String mClass;
    398         private final int mTitleRes;
    399 
    400         /**
    401          * @param className the class name of the fragment to be started.
    402          * @param title the title resource id of the started preference panel.
    403          */
    404         public FragmentStarter(String className, int title) {
    405             mClass = className;
    406             mTitleRes = title;
    407         }
    408 
    409         @Override
    410         public boolean onPreferenceClick(Preference preference) {
    411             ((SettingsActivity) getActivity()).startPreferencePanel(
    412                     mClass, null, mTitleRes, null, null, 0);
    413             // Hack: announce that the Google account preferences page is launching the location
    414             // settings
    415             if (mClass.equals(LocationSettings.class.getName())) {
    416                 Intent intent = new Intent(LAUNCHING_LOCATION_SETTINGS);
    417                 getActivity().sendBroadcast(
    418                         intent, android.Manifest.permission.WRITE_SECURE_SETTINGS);
    419             }
    420             return true;
    421         }
    422     }
    423 
    424     /**
    425      * Filters through the preference list provided by GoogleLoginService.
    426      *
    427      * This method removes all the invalid intent from the list, adds account name as extra into the
    428      * intent, and hack the location settings to start it as a fragment.
    429      */
    430     private void updatePreferenceIntents(PreferenceScreen prefs) {
    431         final PackageManager pm = getActivity().getPackageManager();
    432         for (int i = 0; i < prefs.getPreferenceCount();) {
    433             Preference pref = prefs.getPreference(i);
    434             Intent intent = pref.getIntent();
    435             if (intent != null) {
    436                 // Hack. Launch "Location" as fragment instead of as activity.
    437                 //
    438                 // When "Location" is launched as activity via Intent, there's no "Up" button at the
    439                 // top left, and if there's another running instance of "Location" activity, the
    440                 // back stack would usually point to some other place so the user won't be able to
    441                 // go back to the previous page by "back" key. Using fragment is a much easier
    442                 // solution to those problems.
    443                 //
    444                 // If we set Intent to null and assign a fragment to the PreferenceScreen item here,
    445                 // in order to make it work as expected, we still need to modify the container
    446                 // PreferenceActivity, override onPreferenceStartFragment() and call
    447                 // startPreferencePanel() there. In order to inject the title string there, more
    448                 // dirty further hack is still needed. It's much easier and cleaner to listen to
    449                 // preference click event here directly.
    450                 if (intent.getAction().equals(
    451                         android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)) {
    452                     // The OnPreferenceClickListener overrides the click event completely. No intent
    453                     // will get fired.
    454                     pref.setOnPreferenceClickListener(new FragmentStarter(
    455                             LocationSettings.class.getName(),
    456                             R.string.location_settings_title));
    457                 } else {
    458                     ResolveInfo ri = pm.resolveActivityAsUser(intent,
    459                             PackageManager.MATCH_DEFAULT_ONLY, mUserHandle.getIdentifier());
    460                     if (ri == null) {
    461                         prefs.removePreference(pref);
    462                         continue;
    463                     } else {
    464                         intent.putExtra(ACCOUNT_KEY, mFirstAccount);
    465                         intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
    466                         pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
    467                             @Override
    468                             public boolean onPreferenceClick(Preference preference) {
    469                                 Intent prefIntent = preference.getIntent();
    470                                 /*
    471                                  * Check the intent to see if it resolves to a exported=false
    472                                  * activity that doesn't share a uid with the authenticator.
    473                                  *
    474                                  * Otherwise the intent is considered unsafe in that it will be
    475                                  * exploiting the fact that settings has system privileges.
    476                                  */
    477                                 if (isSafeIntent(pm, prefIntent)) {
    478                                     getActivity().startActivityAsUser(prefIntent, mUserHandle);
    479                                 } else {
    480                                     Log.e(TAG,
    481                                             "Refusing to launch authenticator intent because"
    482                                             + "it exploits Settings permissions: "
    483                                             + prefIntent);
    484                                 }
    485                                 return true;
    486                             }
    487                         });
    488                     }
    489                 }
    490             }
    491             i++;
    492         }
    493     }
    494 
    495     /**
    496      * Determines if the supplied Intent is safe. A safe intent is one that is
    497      * will launch a exported=true activity or owned by the same uid as the
    498      * authenticator supplying the intent.
    499      */
    500     private boolean isSafeIntent(PackageManager pm, Intent intent) {
    501         AuthenticatorDescription authDesc =
    502                 mAuthenticatorHelper.getAccountTypeDescription(mAccountType);
    503         ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
    504         if (resolveInfo == null) {
    505             return false;
    506         }
    507         ActivityInfo resolvedActivityInfo = resolveInfo.activityInfo;
    508         ApplicationInfo resolvedAppInfo = resolvedActivityInfo.applicationInfo;
    509         try {
    510             ApplicationInfo authenticatorAppInf = pm.getApplicationInfo(authDesc.packageName, 0);
    511             return resolvedActivityInfo.exported
    512                     || resolvedAppInfo.uid == authenticatorAppInf.uid;
    513         } catch (NameNotFoundException e) {
    514             Log.e(TAG,
    515                     "Intent considered unsafe due to exception.",
    516                     e);
    517             return false;
    518         }
    519     }
    520 
    521     @Override
    522     protected void onAuthDescriptionsUpdated() {
    523         // Update account icons for all account preference items
    524         for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
    525             Preference pref = getPreferenceScreen().getPreference(i);
    526             if (pref instanceof AccountPreference) {
    527                 AccountPreference accPref = (AccountPreference) pref;
    528                 accPref.setSummary(getLabelForType(accPref.getAccount().type));
    529             }
    530         }
    531     }
    532 }
    533