Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 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 | */ |
| 16 | package android.accounts; |
| 17 | |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 18 | import com.google.android.collect.Sets; |
| 19 | |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 20 | import android.app.Activity; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 21 | import android.app.ActivityManagerNative; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 23 | import android.os.Bundle; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 24 | import android.os.IBinder; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 25 | import android.os.Parcelable; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 26 | import android.os.RemoteException; |
| 27 | import android.os.UserHandle; |
| 28 | import android.os.UserManager; |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 29 | import android.text.TextUtils; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 31 | import android.view.View; |
Amith Yamasani | 5a3915b | 2013-05-24 09:53:47 -0700 | [diff] [blame] | 32 | import android.view.Window; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 33 | import android.widget.AdapterView; |
| 34 | import android.widget.ArrayAdapter; |
| 35 | import android.widget.Button; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 36 | import android.widget.ListView; |
| 37 | import android.widget.TextView; |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 38 | |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 39 | import com.android.internal.R; |
| 40 | |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 41 | import java.io.IOException; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 42 | import java.util.ArrayList; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 43 | import java.util.HashSet; |
| 44 | import java.util.Set; |
| 45 | |
| 46 | /** |
| 47 | * @hide |
| 48 | */ |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 49 | public class ChooseTypeAndAccountActivity extends Activity |
| 50 | implements AccountManagerCallback<Bundle> { |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 51 | private static final String TAG = "AccountChooser"; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * A Parcelable ArrayList of Account objects that limits the choosable accounts to those |
| 55 | * in this list, if this parameter is supplied. |
| 56 | */ |
| 57 | public static final String EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST = "allowableAccounts"; |
| 58 | |
| 59 | /** |
| 60 | * A Parcelable ArrayList of String objects that limits the accounts to choose to those |
| 61 | * that match the types in this list, if this parameter is supplied. This list is also |
| 62 | * used to filter the allowable account types if add account is selected. |
| 63 | */ |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 64 | public static final String EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY = "allowableAccountTypes"; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 67 | * This is passed as the addAccountOptions parameter in AccountManager.addAccount() |
| 68 | * if it is called. |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 69 | */ |
| 70 | public static final String EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE = "addAccountOptions"; |
| 71 | |
| 72 | /** |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 73 | * This is passed as the requiredFeatures parameter in AccountManager.addAccount() |
| 74 | * if it is called. |
| 75 | */ |
Fred Quintana | 01df6a8 | 2011-10-17 21:04:47 -0700 | [diff] [blame] | 76 | public static final String EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY = |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 77 | "addAccountRequiredFeatures"; |
| 78 | |
| 79 | /** |
| 80 | * This is passed as the authTokenType string in AccountManager.addAccount() |
| 81 | * if it is called. |
| 82 | */ |
| 83 | public static final String EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING = "authTokenType"; |
| 84 | |
| 85 | /** |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 86 | * If set then the specified account is already "selected". |
| 87 | */ |
| 88 | public static final String EXTRA_SELECTED_ACCOUNT = "selectedAccount"; |
| 89 | |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 90 | /** |
| 91 | * If true then display the account selection list even if there is just |
| 92 | * one account to choose from. boolean. |
| 93 | */ |
| 94 | public static final String EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT = |
| 95 | "alwaysPromptForAccount"; |
| 96 | |
| 97 | /** |
| 98 | * If set then this string willb e used as the description rather than |
| 99 | * the default. |
| 100 | */ |
| 101 | public static final String EXTRA_DESCRIPTION_TEXT_OVERRIDE = |
| 102 | "descriptionTextOverride"; |
| 103 | |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 104 | public static final int REQUEST_NULL = 0; |
| 105 | public static final int REQUEST_CHOOSE_TYPE = 1; |
| 106 | public static final int REQUEST_ADD_ACCOUNT = 2; |
| 107 | |
| 108 | private static final String KEY_INSTANCE_STATE_PENDING_REQUEST = "pendingRequest"; |
| 109 | private static final String KEY_INSTANCE_STATE_EXISTING_ACCOUNTS = "existingAccounts"; |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 110 | private static final String KEY_INSTANCE_STATE_SELECTED_ACCOUNT_NAME = "selectedAccountName"; |
| 111 | private static final String KEY_INSTANCE_STATE_SELECTED_ADD_ACCOUNT = "selectedAddAccount"; |
Carlos Valdivia | 1b64c9d | 2013-04-19 01:07:12 -0700 | [diff] [blame] | 112 | private static final String KEY_INSTANCE_STATE_ACCOUNT_LIST = "accountList"; |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 113 | |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 114 | private static final int SELECTED_ITEM_NONE = -1; |
| 115 | |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 116 | private Set<Account> mSetOfAllowableAccounts; |
| 117 | private Set<String> mSetOfRelevantAccountTypes; |
| 118 | private String mSelectedAccountName = null; |
| 119 | private boolean mSelectedAddNewAccount = false; |
| 120 | private boolean mAlwaysPromptForAccount = false; |
| 121 | private String mDescriptionOverride; |
| 122 | |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 123 | private ArrayList<Account> mAccounts; |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 124 | private int mPendingRequest = REQUEST_NULL; |
| 125 | private Parcelable[] mExistingAccounts = null; |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 126 | private int mSelectedItemIndex; |
| 127 | private Button mOkButton; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 128 | private int mCallingUid; |
| 129 | private String mCallingPackage; |
| 130 | private boolean mDisallowAddAccounts; |
Amith Yamasani | 5a3915b | 2013-05-24 09:53:47 -0700 | [diff] [blame] | 131 | private boolean mDontShowPicker; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 132 | |
| 133 | @Override |
| 134 | public void onCreate(Bundle savedInstanceState) { |
| 135 | super.onCreate(savedInstanceState); |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 136 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 137 | Log.v(TAG, "ChooseTypeAndAccountActivity.onCreate(savedInstanceState=" |
| 138 | + savedInstanceState + ")"); |
| 139 | } |
| 140 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 141 | String message = null; |
| 142 | |
| 143 | try { |
| 144 | IBinder activityToken = getActivityToken(); |
| 145 | mCallingUid = ActivityManagerNative.getDefault().getLaunchedFromUid(activityToken); |
| 146 | mCallingPackage = ActivityManagerNative.getDefault().getLaunchedFromPackage( |
| 147 | activityToken); |
| 148 | if (mCallingUid != 0 && mCallingPackage != null) { |
| 149 | Bundle restrictions = UserManager.get(this) |
| 150 | .getUserRestrictions(new UserHandle(UserHandle.getUserId(mCallingUid))); |
| 151 | mDisallowAddAccounts = |
| 152 | restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false); |
| 153 | } |
| 154 | } catch (RemoteException re) { |
| 155 | // Couldn't figure out caller details |
| 156 | Log.w(getClass().getSimpleName(), "Unable to get caller identity \n" + re); |
| 157 | } |
| 158 | |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 159 | // save some items we use frequently |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 160 | final Intent intent = getIntent(); |
| 161 | |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 162 | if (savedInstanceState != null) { |
| 163 | mPendingRequest = savedInstanceState.getInt(KEY_INSTANCE_STATE_PENDING_REQUEST); |
| 164 | mExistingAccounts = |
| 165 | savedInstanceState.getParcelableArray(KEY_INSTANCE_STATE_EXISTING_ACCOUNTS); |
| 166 | |
| 167 | // Makes sure that any user selection is preserved across orientation changes. |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 168 | mSelectedAccountName = savedInstanceState.getString( |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 169 | KEY_INSTANCE_STATE_SELECTED_ACCOUNT_NAME); |
| 170 | |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 171 | mSelectedAddNewAccount = savedInstanceState.getBoolean( |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 172 | KEY_INSTANCE_STATE_SELECTED_ADD_ACCOUNT, false); |
Carlos Valdivia | 1b64c9d | 2013-04-19 01:07:12 -0700 | [diff] [blame] | 173 | mAccounts = savedInstanceState.getParcelableArrayList(KEY_INSTANCE_STATE_ACCOUNT_LIST); |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 174 | } else { |
| 175 | mPendingRequest = REQUEST_NULL; |
| 176 | mExistingAccounts = null; |
| 177 | // If the selected account as specified in the intent matches one in the list we will |
| 178 | // show is as pre-selected. |
| 179 | Account selectedAccount = (Account) intent.getParcelableExtra(EXTRA_SELECTED_ACCOUNT); |
| 180 | if (selectedAccount != null) { |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 181 | mSelectedAccountName = selectedAccount.name; |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 182 | } |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 185 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 186 | Log.v(TAG, "selected account name is " + mSelectedAccountName); |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 187 | } |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 188 | |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 189 | |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 190 | mSetOfAllowableAccounts = getAllowableAccountSet(intent); |
| 191 | mSetOfRelevantAccountTypes = getReleventAccountTypes(intent); |
| 192 | mAlwaysPromptForAccount = intent.getBooleanExtra(EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT, false); |
| 193 | mDescriptionOverride = intent.getStringExtra(EXTRA_DESCRIPTION_TEXT_OVERRIDE); |
Amith Yamasani | 5a3915b | 2013-05-24 09:53:47 -0700 | [diff] [blame] | 194 | |
| 195 | // Need to do this once here to request the window feature. Can't do it in onResume |
| 196 | mAccounts = getAcceptableAccountChoices(AccountManager.get(this)); |
| 197 | if (mAccounts.isEmpty() |
| 198 | && mDisallowAddAccounts) { |
| 199 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 200 | setContentView(R.layout.app_not_authorized); |
| 201 | mDontShowPicker = true; |
| 202 | } |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 203 | } |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 204 | |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 205 | @Override |
| 206 | protected void onResume() { |
| 207 | super.onResume(); |
Amith Yamasani | 5a3915b | 2013-05-24 09:53:47 -0700 | [diff] [blame] | 208 | |
| 209 | if (mDontShowPicker) return; |
| 210 | |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 211 | final AccountManager accountManager = AccountManager.get(this); |
Carlos Valdivia | cf0a881 | 2012-05-16 16:32:06 -0700 | [diff] [blame] | 212 | |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 213 | mAccounts = getAcceptableAccountChoices(accountManager); |
Carlos Valdivia | cf0a881 | 2012-05-16 16:32:06 -0700 | [diff] [blame] | 214 | |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 215 | // In cases where the activity does not need to show an account picker, cut the chase |
| 216 | // and return the result directly. Eg: |
| 217 | // Single account -> select it directly |
| 218 | // No account -> launch add account activity directly |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 219 | if (mPendingRequest == REQUEST_NULL) { |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 220 | // If there are no relevant accounts and only one relevant account type go directly to |
Carlos Valdivia | cf0a881 | 2012-05-16 16:32:06 -0700 | [diff] [blame] | 221 | // add account. Otherwise let the user choose. |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 222 | if (mAccounts.isEmpty()) { |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 223 | if (mSetOfRelevantAccountTypes.size() == 1) { |
| 224 | runAddAccountForAuthenticator(mSetOfRelevantAccountTypes.iterator().next()); |
Carlos Valdivia | cf0a881 | 2012-05-16 16:32:06 -0700 | [diff] [blame] | 225 | } else { |
| 226 | startChooseAccountTypeActivity(); |
| 227 | } |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
| 231 | // if there is only one allowable account return it |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 232 | if (!mAlwaysPromptForAccount && mAccounts.size() == 1) { |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 233 | Account account = mAccounts.get(0); |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 234 | setResultAndFinish(account.name, account.type); |
| 235 | return; |
| 236 | } |
| 237 | } |
Carlos Valdivia | cf0a881 | 2012-05-16 16:32:06 -0700 | [diff] [blame] | 238 | |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 239 | String[] listItems = getListOfDisplayableOptions(mAccounts); |
| 240 | mSelectedItemIndex = getItemIndexToSelect( |
| 241 | mAccounts, mSelectedAccountName, mSelectedAddNewAccount); |
| 242 | |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 243 | // Cannot set content view until we know that mPendingRequest is not null, otherwise |
| 244 | // would cause screen flicker. |
Carlos Valdivia | cf0a881 | 2012-05-16 16:32:06 -0700 | [diff] [blame] | 245 | setContentView(R.layout.choose_type_and_account); |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 246 | overrideDescriptionIfSupplied(mDescriptionOverride); |
| 247 | populateUIAccountList(listItems); |
Alice Yang | 6cab5e8 | 2012-05-31 15:48:51 -0700 | [diff] [blame] | 248 | |
| 249 | // Only enable "OK" button if something has been selected. |
| 250 | mOkButton = (Button) findViewById(android.R.id.button2); |
| 251 | mOkButton.setEnabled(mSelectedItemIndex != SELECTED_ITEM_NONE); |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
| 255 | protected void onDestroy() { |
| 256 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 257 | Log.v(TAG, "ChooseTypeAndAccountActivity.onDestroy()"); |
| 258 | } |
| 259 | super.onDestroy(); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | protected void onSaveInstanceState(final Bundle outState) { |
| 264 | super.onSaveInstanceState(outState); |
| 265 | outState.putInt(KEY_INSTANCE_STATE_PENDING_REQUEST, mPendingRequest); |
Fred Quintana | 01df6a8 | 2011-10-17 21:04:47 -0700 | [diff] [blame] | 266 | if (mPendingRequest == REQUEST_ADD_ACCOUNT) { |
| 267 | outState.putParcelableArray(KEY_INSTANCE_STATE_EXISTING_ACCOUNTS, mExistingAccounts); |
| 268 | } |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 269 | if (mSelectedItemIndex != SELECTED_ITEM_NONE) { |
| 270 | if (mSelectedItemIndex == mAccounts.size()) { |
| 271 | outState.putBoolean(KEY_INSTANCE_STATE_SELECTED_ADD_ACCOUNT, true); |
| 272 | } else { |
| 273 | outState.putBoolean(KEY_INSTANCE_STATE_SELECTED_ADD_ACCOUNT, false); |
| 274 | outState.putString(KEY_INSTANCE_STATE_SELECTED_ACCOUNT_NAME, |
| 275 | mAccounts.get(mSelectedItemIndex).name); |
| 276 | } |
| 277 | } |
Carlos Valdivia | 1b64c9d | 2013-04-19 01:07:12 -0700 | [diff] [blame] | 278 | outState.putParcelableArrayList(KEY_INSTANCE_STATE_ACCOUNT_LIST, mAccounts); |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | public void onCancelButtonClicked(View view) { |
| 282 | onBackPressed(); |
| 283 | } |
| 284 | |
| 285 | public void onOkButtonClicked(View view) { |
| 286 | if (mSelectedItemIndex == mAccounts.size()) { |
| 287 | // Selected "Add New Account" option |
| 288 | startChooseAccountTypeActivity(); |
| 289 | } else if (mSelectedItemIndex != SELECTED_ITEM_NONE) { |
| 290 | onAccountSelected(mAccounts.get(mSelectedItemIndex)); |
| 291 | } |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Called when the choose account type activity (for adding an account) returns. |
| 295 | // If it was a success read the account and set it in the result. In all cases |
| 296 | // return the result and finish this activity. |
| 297 | @Override |
| 298 | protected void onActivityResult(final int requestCode, final int resultCode, |
| 299 | final Intent data) { |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 300 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 301 | if (data != null && data.getExtras() != null) data.getExtras().keySet(); |
| 302 | Bundle extras = data != null ? data.getExtras() : null; |
| 303 | Log.v(TAG, "ChooseTypeAndAccountActivity.onActivityResult(reqCode=" + requestCode |
| 304 | + ", resCode=" + resultCode + ", extras=" + extras + ")"); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 305 | } |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 306 | |
| 307 | // we got our result, so clear the fact that we had a pending request |
| 308 | mPendingRequest = REQUEST_NULL; |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 309 | |
| 310 | if (resultCode == RESULT_CANCELED) { |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 311 | // if canceling out of addAccount and the original state caused us to skip this, |
Fred Quintana | 2becf93 | 2011-11-15 17:33:08 -0800 | [diff] [blame] | 312 | // finish this activity |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 313 | if (mAccounts.isEmpty()) { |
Fred Quintana | 2becf93 | 2011-11-15 17:33:08 -0800 | [diff] [blame] | 314 | setResult(Activity.RESULT_CANCELED); |
| 315 | finish(); |
| 316 | } |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 317 | return; |
| 318 | } |
| 319 | |
| 320 | if (resultCode == RESULT_OK) { |
| 321 | if (requestCode == REQUEST_CHOOSE_TYPE) { |
| 322 | if (data != null) { |
| 323 | String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE); |
| 324 | if (accountType != null) { |
| 325 | runAddAccountForAuthenticator(accountType); |
| 326 | return; |
| 327 | } |
| 328 | } |
| 329 | Log.d(TAG, "ChooseTypeAndAccountActivity.onActivityResult: unable to find account " |
| 330 | + "type, pretending the request was canceled"); |
| 331 | } else if (requestCode == REQUEST_ADD_ACCOUNT) { |
| 332 | String accountName = null; |
| 333 | String accountType = null; |
| 334 | |
| 335 | if (data != null) { |
| 336 | accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); |
| 337 | accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE); |
| 338 | } |
| 339 | |
| 340 | if (accountName == null || accountType == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 341 | Account[] currentAccounts = AccountManager.get(this).getAccountsForPackage( |
| 342 | mCallingPackage, mCallingUid); |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 343 | Set<Account> preExistingAccounts = new HashSet<Account>(); |
Fred Quintana | 01df6a8 | 2011-10-17 21:04:47 -0700 | [diff] [blame] | 344 | for (Parcelable accountParcel : mExistingAccounts) { |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 345 | preExistingAccounts.add((Account) accountParcel); |
| 346 | } |
| 347 | for (Account account : currentAccounts) { |
| 348 | if (!preExistingAccounts.contains(account)) { |
| 349 | accountName = account.name; |
| 350 | accountType = account.type; |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if (accountName != null || accountType != null) { |
| 357 | setResultAndFinish(accountName, accountType); |
| 358 | return; |
| 359 | } |
| 360 | } |
| 361 | Log.d(TAG, "ChooseTypeAndAccountActivity.onActivityResult: unable to find added " |
| 362 | + "account, pretending the request was canceled"); |
| 363 | } |
| 364 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 365 | Log.v(TAG, "ChooseTypeAndAccountActivity.onActivityResult: canceled"); |
| 366 | } |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 367 | setResult(Activity.RESULT_CANCELED); |
| 368 | finish(); |
| 369 | } |
| 370 | |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 371 | protected void runAddAccountForAuthenticator(String type) { |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 372 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 373 | Log.v(TAG, "runAddAccountForAuthenticator: " + type); |
| 374 | } |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 375 | final Bundle options = getIntent().getBundleExtra( |
| 376 | ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE); |
| 377 | final String[] requiredFeatures = getIntent().getStringArrayExtra( |
| 378 | ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY); |
| 379 | final String authTokenType = getIntent().getStringExtra( |
| 380 | ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING); |
| 381 | AccountManager.get(this).addAccount(type, authTokenType, requiredFeatures, |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 382 | options, null /* activity */, this /* callback */, null /* Handler */); |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 385 | @Override |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 386 | public void run(final AccountManagerFuture<Bundle> accountManagerFuture) { |
| 387 | try { |
| 388 | final Bundle accountManagerResult = accountManagerFuture.getResult(); |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 389 | final Intent intent = (Intent)accountManagerResult.getParcelable( |
| 390 | AccountManager.KEY_INTENT); |
| 391 | if (intent != null) { |
| 392 | mPendingRequest = REQUEST_ADD_ACCOUNT; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 393 | mExistingAccounts = AccountManager.get(this).getAccountsForPackage(mCallingPackage, |
| 394 | mCallingUid); |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 395 | intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_NEW_TASK); |
| 396 | startActivityForResult(intent, REQUEST_ADD_ACCOUNT); |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 397 | return; |
| 398 | } |
| 399 | } catch (OperationCanceledException e) { |
| 400 | setResult(Activity.RESULT_CANCELED); |
| 401 | finish(); |
| 402 | return; |
| 403 | } catch (IOException e) { |
| 404 | } catch (AuthenticatorException e) { |
| 405 | } |
| 406 | Bundle bundle = new Bundle(); |
| 407 | bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "error communicating with server"); |
| 408 | setResult(Activity.RESULT_OK, new Intent().putExtras(bundle)); |
| 409 | finish(); |
| 410 | } |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 411 | |
Alice Yang | 727c599 | 2012-05-29 13:31:04 -0700 | [diff] [blame] | 412 | private void onAccountSelected(Account account) { |
| 413 | Log.d(TAG, "selected account " + account); |
| 414 | setResultAndFinish(account.name, account.type); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | private void setResultAndFinish(final String accountName, final String accountType) { |
| 418 | Bundle bundle = new Bundle(); |
| 419 | bundle.putString(AccountManager.KEY_ACCOUNT_NAME, accountName); |
| 420 | bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType); |
| 421 | setResult(Activity.RESULT_OK, new Intent().putExtras(bundle)); |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 422 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 423 | Log.v(TAG, "ChooseTypeAndAccountActivity.setResultAndFinish: " |
| 424 | + "selected account " + accountName + ", " + accountType); |
| 425 | } |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 426 | finish(); |
| 427 | } |
| 428 | |
| 429 | private void startChooseAccountTypeActivity() { |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 430 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 431 | Log.v(TAG, "ChooseAccountTypeActivity.startChooseAccountTypeActivity()"); |
| 432 | } |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 433 | final Intent intent = new Intent(this, ChooseAccountTypeActivity.class); |
Fred Quintana | 9bbdd0b | 2011-09-27 17:24:32 -0700 | [diff] [blame] | 434 | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
Fred Quintana | a77253a | 2011-09-19 15:28:18 -0700 | [diff] [blame] | 435 | intent.putExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY, |
| 436 | getIntent().getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY)); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 437 | intent.putExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE, |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 438 | getIntent().getBundleExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE)); |
| 439 | intent.putExtra(EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY, |
| 440 | getIntent().getStringArrayExtra(EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY)); |
| 441 | intent.putExtra(EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING, |
Fred Quintana | a77253a | 2011-09-19 15:28:18 -0700 | [diff] [blame] | 442 | getIntent().getStringExtra(EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING)); |
Fred Quintana | e9095bd | 2011-10-11 17:47:58 -0700 | [diff] [blame] | 443 | startActivityForResult(intent, REQUEST_CHOOSE_TYPE); |
| 444 | mPendingRequest = REQUEST_CHOOSE_TYPE; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 445 | } |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 446 | |
| 447 | /** |
| 448 | * @return a value between 0 (inclusive) and accounts.size() (inclusive) or SELECTED_ITEM_NONE. |
| 449 | * An index value of accounts.size() indicates 'Add account' option. |
| 450 | */ |
| 451 | private int getItemIndexToSelect(ArrayList<Account> accounts, String selectedAccountName, |
| 452 | boolean selectedAddNewAccount) { |
| 453 | // If "Add account" option was previously selected by user, preserve it across |
| 454 | // orientation changes. |
| 455 | if (selectedAddNewAccount) { |
| 456 | return accounts.size(); |
| 457 | } |
| 458 | // search for the selected account name if present |
| 459 | for (int i = 0; i < accounts.size(); i++) { |
| 460 | if (accounts.get(i).name.equals(selectedAccountName)) { |
| 461 | return i; |
| 462 | } |
| 463 | } |
| 464 | // no account selected. |
| 465 | return SELECTED_ITEM_NONE; |
| 466 | } |
| 467 | |
| 468 | private String[] getListOfDisplayableOptions(ArrayList<Account> accounts) { |
| 469 | // List of options includes all accounts found together with "Add new account" as the |
| 470 | // last item in the list. |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 471 | String[] listItems = new String[accounts.size() + (mDisallowAddAccounts ? 0 : 1)]; |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 472 | for (int i = 0; i < accounts.size(); i++) { |
| 473 | listItems[i] = accounts.get(i).name; |
| 474 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 475 | if (!mDisallowAddAccounts) { |
| 476 | listItems[accounts.size()] = getResources().getString( |
| 477 | R.string.add_account_button_label); |
| 478 | } |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 479 | return listItems; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Create a list of Account objects for each account that is acceptable. Filter out |
| 484 | * accounts that don't match the allowable types, if provided, or that don't match the |
| 485 | * allowable accounts, if provided. |
| 486 | */ |
| 487 | private ArrayList<Account> getAcceptableAccountChoices(AccountManager accountManager) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 488 | final Account[] accounts = accountManager.getAccountsForPackage(mCallingPackage, |
| 489 | mCallingUid); |
Jatin Lodhia | 8d16778 | 2012-09-12 13:59:33 -0700 | [diff] [blame] | 490 | ArrayList<Account> accountsToPopulate = new ArrayList<Account>(accounts.length); |
| 491 | for (Account account : accounts) { |
| 492 | if (mSetOfAllowableAccounts != null |
| 493 | && !mSetOfAllowableAccounts.contains(account)) { |
| 494 | continue; |
| 495 | } |
| 496 | if (mSetOfRelevantAccountTypes != null |
| 497 | && !mSetOfRelevantAccountTypes.contains(account.type)) { |
| 498 | continue; |
| 499 | } |
| 500 | accountsToPopulate.add(account); |
| 501 | } |
| 502 | return accountsToPopulate; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Return a set of account types speficied by the intent as well as supported by the |
| 507 | * AccountManager. |
| 508 | */ |
| 509 | private Set<String> getReleventAccountTypes(final Intent intent) { |
| 510 | // An account type is relevant iff it is allowed by the caller and supported by the account |
| 511 | // manager. |
| 512 | Set<String> setOfRelevantAccountTypes = null; |
| 513 | final String[] allowedAccountTypes = |
| 514 | intent.getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY); |
| 515 | if (allowedAccountTypes != null) { |
| 516 | setOfRelevantAccountTypes = Sets.newHashSet(allowedAccountTypes); |
| 517 | AuthenticatorDescription[] descs = AccountManager.get(this).getAuthenticatorTypes(); |
| 518 | Set<String> supportedAccountTypes = new HashSet<String>(descs.length); |
| 519 | for (AuthenticatorDescription desc : descs) { |
| 520 | supportedAccountTypes.add(desc.type); |
| 521 | } |
| 522 | setOfRelevantAccountTypes.retainAll(supportedAccountTypes); |
| 523 | } |
| 524 | return setOfRelevantAccountTypes; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Returns a set of whitelisted accounts given by the intent or null if none specified by the |
| 529 | * intent. |
| 530 | */ |
| 531 | private Set<Account> getAllowableAccountSet(final Intent intent) { |
| 532 | Set<Account> setOfAllowableAccounts = null; |
| 533 | final ArrayList<Parcelable> validAccounts = |
| 534 | intent.getParcelableArrayListExtra(EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST); |
| 535 | if (validAccounts != null) { |
| 536 | setOfAllowableAccounts = new HashSet<Account>(validAccounts.size()); |
| 537 | for (Parcelable parcelable : validAccounts) { |
| 538 | setOfAllowableAccounts.add((Account)parcelable); |
| 539 | } |
| 540 | } |
| 541 | return setOfAllowableAccounts; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Overrides the description text view for the picker activity if specified by the intent. |
| 546 | * If not specified then makes the description invisible. |
| 547 | */ |
| 548 | private void overrideDescriptionIfSupplied(String descriptionOverride) { |
| 549 | TextView descriptionView = (TextView) findViewById(R.id.description); |
| 550 | if (!TextUtils.isEmpty(descriptionOverride)) { |
| 551 | descriptionView.setText(descriptionOverride); |
| 552 | } else { |
| 553 | descriptionView.setVisibility(View.GONE); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Populates the UI ListView with the given list of items and selects an item |
| 559 | * based on {@code mSelectedItemIndex} member variable. |
| 560 | */ |
| 561 | private final void populateUIAccountList(String[] listItems) { |
| 562 | ListView list = (ListView) findViewById(android.R.id.list); |
| 563 | list.setAdapter(new ArrayAdapter<String>(this, |
| 564 | android.R.layout.simple_list_item_single_choice, listItems)); |
| 565 | list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); |
| 566 | list.setItemsCanFocus(false); |
| 567 | list.setOnItemClickListener( |
| 568 | new AdapterView.OnItemClickListener() { |
| 569 | @Override |
| 570 | public void onItemClick(AdapterView<?> parent, View v, int position, long id) { |
| 571 | mSelectedItemIndex = position; |
| 572 | mOkButton.setEnabled(true); |
| 573 | } |
| 574 | }); |
| 575 | if (mSelectedItemIndex != SELECTED_ITEM_NONE) { |
| 576 | list.setItemChecked(mSelectedItemIndex, true); |
| 577 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 578 | Log.v(TAG, "List item " + mSelectedItemIndex + " should be selected"); |
| 579 | } |
| 580 | } |
| 581 | } |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 582 | } |