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