ChooseTypeAndAccountActivity.java revision cf0a881f1c27718f686a307e6c94213815ee9dc1
1/* 2 * Copyright (C) 2011 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 */ 16package android.accounts; 17 18import android.app.Activity; 19import android.content.Context; 20import android.content.Intent; 21import android.content.pm.PackageManager; 22import android.content.res.Resources; 23import android.graphics.drawable.Drawable; 24import android.os.Bundle; 25import android.os.Parcelable; 26import android.text.TextUtils; 27import android.util.Log; 28import android.view.LayoutInflater; 29import android.view.View; 30import android.view.ViewGroup; 31import android.widget.AdapterView; 32import android.widget.ArrayAdapter; 33import android.widget.Button; 34import android.widget.ImageView; 35import android.widget.ListView; 36import android.widget.TextView; 37import com.android.internal.R; 38 39import java.io.IOException; 40import java.util.ArrayList; 41import java.util.HashMap; 42import java.util.HashSet; 43import java.util.Set; 44 45/** 46 * @hide 47 */ 48public class ChooseTypeAndAccountActivity extends Activity 49 implements AccountManagerCallback<Bundle> { 50 private static final String TAG = "AccountChooser"; 51 52 /** 53 * A Parcelable ArrayList of Account objects that limits the choosable accounts to those 54 * in this list, if this parameter is supplied. 55 */ 56 public static final String EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST = "allowableAccounts"; 57 58 /** 59 * A Parcelable ArrayList of String objects that limits the accounts to choose to those 60 * that match the types in this list, if this parameter is supplied. This list is also 61 * used to filter the allowable account types if add account is selected. 62 */ 63 public static final String EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY = "allowableAccountTypes"; 64 65 /** 66 * This is passed as the addAccountOptions parameter in AccountManager.addAccount() 67 * if it is called. 68 */ 69 public static final String EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE = "addAccountOptions"; 70 71 /** 72 * This is passed as the requiredFeatures parameter in AccountManager.addAccount() 73 * if it is called. 74 */ 75 public static final String EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY = 76 "addAccountRequiredFeatures"; 77 78 /** 79 * This is passed as the authTokenType string in AccountManager.addAccount() 80 * if it is called. 81 */ 82 public static final String EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING = "authTokenType"; 83 84 /** 85 * If set then the specified account is already "selected". 86 */ 87 public static final String EXTRA_SELECTED_ACCOUNT = "selectedAccount"; 88 89 /** 90 * If true then display the account selection list even if there is just 91 * one account to choose from. boolean. 92 */ 93 public static final String EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT = 94 "alwaysPromptForAccount"; 95 96 /** 97 * If set then this string willb e used as the description rather than 98 * the default. 99 */ 100 public static final String EXTRA_DESCRIPTION_TEXT_OVERRIDE = 101 "descriptionTextOverride"; 102 103 public static final int REQUEST_NULL = 0; 104 public static final int REQUEST_CHOOSE_TYPE = 1; 105 public static final int REQUEST_ADD_ACCOUNT = 2; 106 107 private static final String KEY_INSTANCE_STATE_PENDING_REQUEST = "pendingRequest"; 108 private static final String KEY_INSTANCE_STATE_EXISTING_ACCOUNTS = "existingAccounts"; 109 110 private ArrayList<AccountInfo> mAccountInfos; 111 private int mPendingRequest = REQUEST_NULL; 112 private Parcelable[] mExistingAccounts = null; 113 114 @Override 115 public void onCreate(Bundle savedInstanceState) { 116 super.onCreate(savedInstanceState); 117 if (Log.isLoggable(TAG, Log.VERBOSE)) { 118 Log.v(TAG, "ChooseTypeAndAccountActivity.onCreate(savedInstanceState=" 119 + savedInstanceState + ")"); 120 } 121 122 if (savedInstanceState != null) { 123 mPendingRequest = savedInstanceState.getInt(KEY_INSTANCE_STATE_PENDING_REQUEST); 124 mExistingAccounts = 125 savedInstanceState.getParcelableArray(KEY_INSTANCE_STATE_EXISTING_ACCOUNTS); 126 } else { 127 mPendingRequest = REQUEST_NULL; 128 mExistingAccounts = null; 129 } 130 131 // save some items we use frequently 132 final AccountManager accountManager = AccountManager.get(this); 133 final Intent intent = getIntent(); 134 135 // override the description text if supplied 136 final String descriptionOverride = 137 intent.getStringExtra(EXTRA_DESCRIPTION_TEXT_OVERRIDE); 138 if (!TextUtils.isEmpty(descriptionOverride)) { 139 ((TextView)findViewById(R.id.description)).setText(descriptionOverride); 140 } 141 142 // If the selected account matches one in the list we will place a 143 // checkmark next to it. 144 final Account selectedAccount = 145 (Account)intent.getParcelableExtra(EXTRA_SELECTED_ACCOUNT); 146 147 // build an efficiently queryable map of account types to authenticator descriptions 148 final HashMap<String, AuthenticatorDescription> typeToAuthDescription = 149 new HashMap<String, AuthenticatorDescription>(); 150 for(AuthenticatorDescription desc : accountManager.getAuthenticatorTypes()) { 151 typeToAuthDescription.put(desc.type, desc); 152 } 153 154 // Read the validAccounts, if present, and add them to the setOfAllowableAccounts 155 Set<Account> setOfAllowableAccounts = null; 156 final ArrayList<Parcelable> validAccounts = 157 intent.getParcelableArrayListExtra(EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST); 158 if (validAccounts != null) { 159 setOfAllowableAccounts = new HashSet<Account>(validAccounts.size()); 160 for (Parcelable parcelable : validAccounts) { 161 setOfAllowableAccounts.add((Account)parcelable); 162 } 163 } 164 165 // An account type is relevant iff it is allowed by the caller and supported by the account 166 // manager. 167 Set<String> setOfRelevantAccountTypes = null; 168 final String[] allowedAccountTypes = 169 intent.getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY); 170 if (allowedAccountTypes != null) { 171 172 setOfRelevantAccountTypes = new HashSet<String>(allowedAccountTypes.length); 173 Set<String> setOfAllowedAccountTypes = new HashSet<String>(allowedAccountTypes.length); 174 for (String type : allowedAccountTypes) { 175 setOfAllowedAccountTypes.add(type); 176 } 177 178 AuthenticatorDescription[] descs = AccountManager.get(this).getAuthenticatorTypes(); 179 Set<String> supportedAccountTypes = new HashSet<String>(descs.length); 180 for (AuthenticatorDescription desc : descs) { 181 supportedAccountTypes.add(desc.type); 182 } 183 184 for (String acctType : setOfAllowedAccountTypes) { 185 if (supportedAccountTypes.contains(acctType)) { 186 setOfRelevantAccountTypes.add(acctType); 187 } 188 } 189 } 190 191 // Create a list of AccountInfo objects for each account that is allowable. Filter out 192 // accounts that don't match the allowable types, if provided, or that don't match the 193 // allowable accounts, if provided. 194 final Account[] accounts = accountManager.getAccounts(); 195 mAccountInfos = new ArrayList<AccountInfo>(accounts.length); 196 for (Account account : accounts) { 197 if (setOfAllowableAccounts != null 198 && !setOfAllowableAccounts.contains(account)) { 199 continue; 200 } 201 if (setOfRelevantAccountTypes != null 202 && !setOfRelevantAccountTypes.contains(account.type)) { 203 continue; 204 } 205 mAccountInfos.add(new AccountInfo(account, 206 getDrawableForType(typeToAuthDescription, account.type), 207 account.equals(selectedAccount))); 208 } 209 210 if (mPendingRequest == REQUEST_NULL) { 211 // If there are no relevant accounts and only one relevant account typoe go directly to 212 // add account. Otherwise let the user choose. 213 if (mAccountInfos.isEmpty()) { 214 if (setOfRelevantAccountTypes.size() == 1) { 215 runAddAccountForAuthenticator(setOfRelevantAccountTypes.iterator().next()); 216 } else { 217 startChooseAccountTypeActivity(); 218 } 219 return; 220 } 221 222 // if there is only one allowable account return it 223 if (!intent.getBooleanExtra(EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT, false) 224 && mAccountInfos.size() == 1) { 225 Account account = mAccountInfos.get(0).account; 226 setResultAndFinish(account.name, account.type); 227 return; 228 } 229 } 230 231 setContentView(R.layout.choose_type_and_account); 232 233 // there is more than one allowable account. initialize the list adapter to allow 234 // the user to select an account. 235 ListView list = (ListView) findViewById(android.R.id.list); 236 list.setAdapter(new AccountArrayAdapter(this, 237 android.R.layout.simple_list_item_1, mAccountInfos)); 238 list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 239 list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 240 @Override 241 public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 242 onListItemClick((ListView)parent, v, position, id); 243 } 244 }); 245 246 // set the listener for the addAccount button 247 Button addAccountButton = (Button) findViewById(R.id.addAccount); 248 addAccountButton.setOnClickListener(new View.OnClickListener() { 249 @Override 250 public void onClick(final View v) { 251 startChooseAccountTypeActivity(); 252 } 253 }); 254 } 255 256 @Override 257 protected void onDestroy() { 258 if (Log.isLoggable(TAG, Log.VERBOSE)) { 259 Log.v(TAG, "ChooseTypeAndAccountActivity.onDestroy()"); 260 } 261 super.onDestroy(); 262 } 263 264 @Override 265 protected void onSaveInstanceState(final Bundle outState) { 266 super.onSaveInstanceState(outState); 267 outState.putInt(KEY_INSTANCE_STATE_PENDING_REQUEST, mPendingRequest); 268 if (mPendingRequest == REQUEST_ADD_ACCOUNT) { 269 outState.putParcelableArray(KEY_INSTANCE_STATE_EXISTING_ACCOUNTS, mExistingAccounts); 270 } 271 } 272 273 // Called when the choose account type activity (for adding an account) returns. 274 // If it was a success read the account and set it in the result. In all cases 275 // return the result and finish this activity. 276 @Override 277 protected void onActivityResult(final int requestCode, final int resultCode, 278 final Intent data) { 279 if (Log.isLoggable(TAG, Log.VERBOSE)) { 280 if (data != null && data.getExtras() != null) data.getExtras().keySet(); 281 Bundle extras = data != null ? data.getExtras() : null; 282 Log.v(TAG, "ChooseTypeAndAccountActivity.onActivityResult(reqCode=" + requestCode 283 + ", resCode=" + resultCode + ", extras=" + extras + ")"); 284 } 285 286 // we got our result, so clear the fact that we had a pending request 287 mPendingRequest = REQUEST_NULL; 288 289 if (resultCode == RESULT_CANCELED) { 290 // if cancelling out of addAccount and the original state caused us to skip this, 291 // finish this activity 292 if (mAccountInfos.isEmpty()) { 293 setResult(Activity.RESULT_CANCELED); 294 finish(); 295 } 296 return; 297 } 298 299 if (resultCode == RESULT_OK) { 300 if (requestCode == REQUEST_CHOOSE_TYPE) { 301 if (data != null) { 302 String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE); 303 if (accountType != null) { 304 runAddAccountForAuthenticator(accountType); 305 return; 306 } 307 } 308 Log.d(TAG, "ChooseTypeAndAccountActivity.onActivityResult: unable to find account " 309 + "type, pretending the request was canceled"); 310 } else if (requestCode == REQUEST_ADD_ACCOUNT) { 311 String accountName = null; 312 String accountType = null; 313 314 if (data != null) { 315 accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); 316 accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE); 317 } 318 319 if (accountName == null || accountType == null) { 320 Account[] currentAccounts = AccountManager.get(this).getAccounts(); 321 Set<Account> preExistingAccounts = new HashSet<Account>(); 322 for (Parcelable accountParcel : mExistingAccounts) { 323 preExistingAccounts.add((Account) accountParcel); 324 } 325 for (Account account : currentAccounts) { 326 if (!preExistingAccounts.contains(account)) { 327 accountName = account.name; 328 accountType = account.type; 329 break; 330 } 331 } 332 } 333 334 if (accountName != null || accountType != null) { 335 setResultAndFinish(accountName, accountType); 336 return; 337 } 338 } 339 Log.d(TAG, "ChooseTypeAndAccountActivity.onActivityResult: unable to find added " 340 + "account, pretending the request was canceled"); 341 } 342 if (Log.isLoggable(TAG, Log.VERBOSE)) { 343 Log.v(TAG, "ChooseTypeAndAccountActivity.onActivityResult: canceled"); 344 } 345 setResult(Activity.RESULT_CANCELED); 346 finish(); 347 } 348 349 protected void runAddAccountForAuthenticator(String type) { 350 if (Log.isLoggable(TAG, Log.VERBOSE)) { 351 Log.v(TAG, "runAddAccountForAuthenticator: " + type); 352 } 353 final Bundle options = getIntent().getBundleExtra( 354 ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE); 355 final String[] requiredFeatures = getIntent().getStringArrayExtra( 356 ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY); 357 final String authTokenType = getIntent().getStringExtra( 358 ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING); 359 AccountManager.get(this).addAccount(type, authTokenType, requiredFeatures, 360 options, null /* activity */, this /* callback */, null /* Handler */); 361 } 362 363 public void run(final AccountManagerFuture<Bundle> accountManagerFuture) { 364 try { 365 final Bundle accountManagerResult = accountManagerFuture.getResult(); 366 final Intent intent = (Intent)accountManagerResult.getParcelable( 367 AccountManager.KEY_INTENT); 368 if (intent != null) { 369 mPendingRequest = REQUEST_ADD_ACCOUNT; 370 mExistingAccounts = AccountManager.get(this).getAccounts(); 371 intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_NEW_TASK); 372 startActivityForResult(intent, REQUEST_ADD_ACCOUNT); 373 return; 374 } 375 } catch (OperationCanceledException e) { 376 setResult(Activity.RESULT_CANCELED); 377 finish(); 378 return; 379 } catch (IOException e) { 380 } catch (AuthenticatorException e) { 381 } 382 Bundle bundle = new Bundle(); 383 bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "error communicating with server"); 384 setResult(Activity.RESULT_OK, new Intent().putExtras(bundle)); 385 finish(); 386 } 387 388 private Drawable getDrawableForType( 389 final HashMap<String, AuthenticatorDescription> typeToAuthDescription, 390 String accountType) { 391 Drawable icon = null; 392 if (typeToAuthDescription.containsKey(accountType)) { 393 try { 394 AuthenticatorDescription desc = typeToAuthDescription.get(accountType); 395 Context authContext = createPackageContext(desc.packageName, 0); 396 icon = authContext.getResources().getDrawable(desc.iconId); 397 } catch (PackageManager.NameNotFoundException e) { 398 // Nothing we can do much here, just log 399 if (Log.isLoggable(TAG, Log.WARN)) { 400 Log.w(TAG, "No icon name for account type " + accountType); 401 } 402 } catch (Resources.NotFoundException e) { 403 // Nothing we can do much here, just log 404 if (Log.isLoggable(TAG, Log.WARN)) { 405 Log.w(TAG, "No icon resource for account type " + accountType); 406 } 407 } 408 } 409 return icon; 410 } 411 412 protected void onListItemClick(ListView l, View v, int position, long id) { 413 AccountInfo accountInfo = mAccountInfos.get(position); 414 Log.d(TAG, "selected account " + accountInfo.account); 415 setResultAndFinish(accountInfo.account.name, accountInfo.account.type); 416 } 417 418 private void setResultAndFinish(final String accountName, final String accountType) { 419 Bundle bundle = new Bundle(); 420 bundle.putString(AccountManager.KEY_ACCOUNT_NAME, accountName); 421 bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType); 422 setResult(Activity.RESULT_OK, new Intent().putExtras(bundle)); 423 if (Log.isLoggable(TAG, Log.VERBOSE)) { 424 Log.v(TAG, "ChooseTypeAndAccountActivity.setResultAndFinish: " 425 + "selected account " + accountName + ", " + accountType); 426 } 427 finish(); 428 } 429 430 private void startChooseAccountTypeActivity() { 431 if (Log.isLoggable(TAG, Log.VERBOSE)) { 432 Log.v(TAG, "ChooseAccountTypeActivity.startChooseAccountTypeActivity()"); 433 } 434 final Intent intent = new Intent(this, ChooseAccountTypeActivity.class); 435 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 436 intent.putExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY, 437 getIntent().getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY)); 438 intent.putExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE, 439 getIntent().getBundleExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE)); 440 intent.putExtra(EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY, 441 getIntent().getStringArrayExtra(EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY)); 442 intent.putExtra(EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING, 443 getIntent().getStringExtra(EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING)); 444 startActivityForResult(intent, REQUEST_CHOOSE_TYPE); 445 mPendingRequest = REQUEST_CHOOSE_TYPE; 446 } 447 448 private static class AccountInfo { 449 final Account account; 450 final Drawable drawable; 451 private final boolean checked; 452 453 AccountInfo(Account account, Drawable drawable, boolean checked) { 454 this.account = account; 455 this.drawable = drawable; 456 this.checked = checked; 457 } 458 } 459 460 private static class ViewHolder { 461 ImageView icon; 462 TextView text; 463 ImageView checkmark; 464 } 465 466 private static class AccountArrayAdapter extends ArrayAdapter<AccountInfo> { 467 private LayoutInflater mLayoutInflater; 468 private ArrayList<AccountInfo> mInfos; 469 470 public AccountArrayAdapter(Context context, int textViewResourceId, 471 ArrayList<AccountInfo> infos) { 472 super(context, textViewResourceId, infos); 473 mInfos = infos; 474 mLayoutInflater = (LayoutInflater) context.getSystemService( 475 Context.LAYOUT_INFLATER_SERVICE); 476 } 477 478 @Override 479 public View getView(int position, View convertView, ViewGroup parent) { 480 ViewHolder holder; 481 482 if (convertView == null) { 483 convertView = mLayoutInflater.inflate(R.layout.choose_selected_account_row, null); 484 holder = new ViewHolder(); 485 holder.text = (TextView) convertView.findViewById(R.id.account_row_text); 486 holder.icon = (ImageView) convertView.findViewById(R.id.account_row_icon); 487 holder.checkmark = (ImageView) convertView.findViewById(R.id.account_row_checkmark); 488 convertView.setTag(holder); 489 } else { 490 holder = (ViewHolder) convertView.getTag(); 491 } 492 493 holder.text.setText(mInfos.get(position).account.name); 494 holder.icon.setImageDrawable(mInfos.get(position).drawable); 495 final int displayCheckmark = 496 mInfos.get(position).checked ? View.VISIBLE : View.INVISIBLE; 497 holder.checkmark.setVisibility(displayCheckmark); 498 return convertView; 499 } 500 } 501} 502