ChooseTypeAndAccountActivity.java revision 2becf93d275a1e678abedd16b88225d6cecf49e2
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        setContentView(R.layout.choose_type_and_account);
123
124        if (savedInstanceState != null) {
125            mPendingRequest = savedInstanceState.getInt(KEY_INSTANCE_STATE_PENDING_REQUEST);
126            mExistingAccounts =
127                    savedInstanceState.getParcelableArray(KEY_INSTANCE_STATE_EXISTING_ACCOUNTS);
128        } else {
129            mPendingRequest = REQUEST_NULL;
130            mExistingAccounts = null;
131        }
132
133        // save some items we use frequently
134        final AccountManager accountManager = AccountManager.get(this);
135        final Intent intent = getIntent();
136
137        // override the description text if supplied
138        final String descriptionOverride =
139                intent.getStringExtra(EXTRA_DESCRIPTION_TEXT_OVERRIDE);
140        if (!TextUtils.isEmpty(descriptionOverride)) {
141            ((TextView)findViewById(R.id.description)).setText(descriptionOverride);
142        }
143
144        // If the selected account matches one in the list we will place a
145        // checkmark next to it.
146        final Account selectedAccount =
147                (Account)intent.getParcelableExtra(EXTRA_SELECTED_ACCOUNT);
148
149        // build an efficiently queryable map of account types to authenticator descriptions
150        final HashMap<String, AuthenticatorDescription> typeToAuthDescription =
151                new HashMap<String, AuthenticatorDescription>();
152        for(AuthenticatorDescription desc : accountManager.getAuthenticatorTypes()) {
153            typeToAuthDescription.put(desc.type, desc);
154        }
155
156        // Read the validAccounts, if present, and add them to the setOfAllowableAccounts
157        Set<Account> setOfAllowableAccounts = null;
158        final ArrayList<Parcelable> validAccounts =
159                intent.getParcelableArrayListExtra(EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST);
160        if (validAccounts != null) {
161            setOfAllowableAccounts = new HashSet<Account>(validAccounts.size());
162            for (Parcelable parcelable : validAccounts) {
163                setOfAllowableAccounts.add((Account)parcelable);
164            }
165        }
166
167        // Read the validAccountTypes, if present, and add them to the setOfAllowableAccountTypes
168        Set<String> setOfAllowableAccountTypes = null;
169        final String[] validAccountTypes =
170                intent.getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY);
171        if (validAccountTypes != null) {
172            setOfAllowableAccountTypes = new HashSet<String>(validAccountTypes.length);
173            for (String type : validAccountTypes) {
174                setOfAllowableAccountTypes.add(type);
175            }
176        }
177
178        // Create a list of AccountInfo objects for each account that is allowable. Filter out
179        // accounts that don't match the allowable types, if provided, or that don't match the
180        // allowable accounts, if provided.
181        final Account[] accounts = accountManager.getAccounts();
182        mAccountInfos = new ArrayList<AccountInfo>(accounts.length);
183        for (Account account : accounts) {
184            if (setOfAllowableAccounts != null
185                    && !setOfAllowableAccounts.contains(account)) {
186                continue;
187            }
188            if (setOfAllowableAccountTypes != null
189                    && !setOfAllowableAccountTypes.contains(account.type)) {
190                continue;
191            }
192            mAccountInfos.add(new AccountInfo(account,
193                    getDrawableForType(typeToAuthDescription, account.type),
194                    account.equals(selectedAccount)));
195        }
196
197        // there is more than one allowable account. initialize the list adapter to allow
198        // the user to select an account.
199        ListView list = (ListView) findViewById(android.R.id.list);
200        list.setAdapter(new AccountArrayAdapter(this,
201                android.R.layout.simple_list_item_1, mAccountInfos));
202        list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
203        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
204            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
205                onListItemClick((ListView)parent, v, position, id);
206            }
207        });
208
209        // set the listener for the addAccount button
210        Button addAccountButton = (Button) findViewById(R.id.addAccount);
211        addAccountButton.setOnClickListener(new View.OnClickListener() {
212            public void onClick(final View v) {
213                startChooseAccountTypeActivity();
214            }
215        });
216
217        if (mPendingRequest == REQUEST_NULL) {
218            // If there are no allowable accounts go directly to add account
219            if (shouldSkipToChooseAccountTypeFlow()) {
220                startChooseAccountTypeActivity();
221                return;
222            }
223
224            // if there is only one allowable account return it
225            if (!intent.getBooleanExtra(EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT, false)
226                    && mAccountInfos.size() == 1) {
227                Account account = mAccountInfos.get(0).account;
228                setResultAndFinish(account.name, account.type);
229                return;
230            }
231        }
232    }
233
234    @Override
235    protected void onDestroy() {
236        if (Log.isLoggable(TAG, Log.VERBOSE)) {
237            Log.v(TAG, "ChooseTypeAndAccountActivity.onDestroy()");
238        }
239        super.onDestroy();
240    }
241
242    @Override
243    protected void onSaveInstanceState(final Bundle outState) {
244        super.onSaveInstanceState(outState);
245        outState.putInt(KEY_INSTANCE_STATE_PENDING_REQUEST, mPendingRequest);
246        if (mPendingRequest == REQUEST_ADD_ACCOUNT) {
247            outState.putParcelableArray(KEY_INSTANCE_STATE_EXISTING_ACCOUNTS, mExistingAccounts);
248        }
249    }
250
251    // Called when the choose account type activity (for adding an account) returns.
252    // If it was a success read the account and set it in the result. In all cases
253    // return the result and finish this activity.
254    @Override
255    protected void onActivityResult(final int requestCode, final int resultCode,
256            final Intent data) {
257        if (Log.isLoggable(TAG, Log.VERBOSE)) {
258            if (data != null && data.getExtras() != null) data.getExtras().keySet();
259            Bundle extras = data != null ? data.getExtras() : null;
260            Log.v(TAG, "ChooseTypeAndAccountActivity.onActivityResult(reqCode=" + requestCode
261                    + ", resCode=" + resultCode + ", extras=" + extras + ")");
262        }
263
264        // we got our result, so clear the fact that we had a pending request
265        mPendingRequest = REQUEST_NULL;
266
267        if (resultCode == RESULT_CANCELED) {
268            // if cancelling out of addAccount and the original state caused us to skip this,
269            // finish this activity
270            if (shouldSkipToChooseAccountTypeFlow()) {
271                setResult(Activity.RESULT_CANCELED);
272                finish();
273            }
274            return;
275        }
276
277        if (resultCode == RESULT_OK) {
278            if (requestCode == REQUEST_CHOOSE_TYPE) {
279                if (data != null) {
280                    String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
281                    if (accountType != null) {
282                        runAddAccountForAuthenticator(accountType);
283                        return;
284                    }
285                }
286                Log.d(TAG, "ChooseTypeAndAccountActivity.onActivityResult: unable to find account "
287                        + "type, pretending the request was canceled");
288            } else if (requestCode == REQUEST_ADD_ACCOUNT) {
289                String accountName = null;
290                String accountType = null;
291
292                if (data != null) {
293                    accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
294                    accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
295                }
296
297                if (accountName == null || accountType == null) {
298                    Account[] currentAccounts = AccountManager.get(this).getAccounts();
299                    Set<Account> preExistingAccounts = new HashSet<Account>();
300                    for (Parcelable accountParcel : mExistingAccounts) {
301                        preExistingAccounts.add((Account) accountParcel);
302                    }
303                    for (Account account : currentAccounts) {
304                        if (!preExistingAccounts.contains(account)) {
305                            accountName = account.name;
306                            accountType = account.type;
307                            break;
308                        }
309                    }
310                }
311
312                if (accountName != null || accountType != null) {
313                    setResultAndFinish(accountName, accountType);
314                    return;
315                }
316            }
317            Log.d(TAG, "ChooseTypeAndAccountActivity.onActivityResult: unable to find added "
318                    + "account, pretending the request was canceled");
319        }
320        if (Log.isLoggable(TAG, Log.VERBOSE)) {
321            Log.v(TAG, "ChooseTypeAndAccountActivity.onActivityResult: canceled");
322        }
323        setResult(Activity.RESULT_CANCELED);
324        finish();
325    }
326
327    /**
328     * convenience method to check if we should skip the accounts list display and immediately
329     * jump to the flow that asks the user to select from the account type list
330     */
331    private boolean shouldSkipToChooseAccountTypeFlow() {
332        return mAccountInfos.isEmpty();
333    }
334
335    protected void runAddAccountForAuthenticator(String type) {
336        if (Log.isLoggable(TAG, Log.VERBOSE)) {
337            Log.v(TAG, "runAddAccountForAuthenticator: " + type);
338        }
339        final Bundle options = getIntent().getBundleExtra(
340                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE);
341        final String[] requiredFeatures = getIntent().getStringArrayExtra(
342                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY);
343        final String authTokenType = getIntent().getStringExtra(
344                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING);
345        AccountManager.get(this).addAccount(type, authTokenType, requiredFeatures,
346                options, null /* activity */, this /* callback */, null /* Handler */);
347    }
348
349    public void run(final AccountManagerFuture<Bundle> accountManagerFuture) {
350        try {
351            final Bundle accountManagerResult = accountManagerFuture.getResult();
352            final Intent intent = (Intent)accountManagerResult.getParcelable(
353                    AccountManager.KEY_INTENT);
354            if (intent != null) {
355                mPendingRequest = REQUEST_ADD_ACCOUNT;
356                mExistingAccounts = AccountManager.get(this).getAccounts();
357                intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_NEW_TASK);
358                startActivityForResult(intent, REQUEST_ADD_ACCOUNT);
359                return;
360            }
361        } catch (OperationCanceledException e) {
362            setResult(Activity.RESULT_CANCELED);
363            finish();
364            return;
365        } catch (IOException e) {
366        } catch (AuthenticatorException e) {
367        }
368        Bundle bundle = new Bundle();
369        bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "error communicating with server");
370        setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
371        finish();
372    }
373
374    private Drawable getDrawableForType(
375            final HashMap<String, AuthenticatorDescription> typeToAuthDescription,
376            String accountType) {
377        Drawable icon = null;
378        if (typeToAuthDescription.containsKey(accountType)) {
379            try {
380                AuthenticatorDescription desc = typeToAuthDescription.get(accountType);
381                Context authContext = createPackageContext(desc.packageName, 0);
382                icon = authContext.getResources().getDrawable(desc.iconId);
383            } catch (PackageManager.NameNotFoundException e) {
384                // Nothing we can do much here, just log
385                if (Log.isLoggable(TAG, Log.WARN)) {
386                    Log.w(TAG, "No icon name for account type " + accountType);
387                }
388            } catch (Resources.NotFoundException e) {
389                // Nothing we can do much here, just log
390                if (Log.isLoggable(TAG, Log.WARN)) {
391                    Log.w(TAG, "No icon resource for account type " + accountType);
392                }
393            }
394        }
395        return icon;
396    }
397
398    protected void onListItemClick(ListView l, View v, int position, long id) {
399        AccountInfo accountInfo = mAccountInfos.get(position);
400        Log.d(TAG, "selected account " + accountInfo.account);
401        setResultAndFinish(accountInfo.account.name, accountInfo.account.type);
402    }
403
404    private void setResultAndFinish(final String accountName, final String accountType) {
405        Bundle bundle = new Bundle();
406        bundle.putString(AccountManager.KEY_ACCOUNT_NAME, accountName);
407        bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
408        setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
409        if (Log.isLoggable(TAG, Log.VERBOSE)) {
410            Log.v(TAG, "ChooseTypeAndAccountActivity.setResultAndFinish: "
411                    + "selected account " + accountName + ", " + accountType);
412        }
413        finish();
414    }
415
416    private void startChooseAccountTypeActivity() {
417        if (Log.isLoggable(TAG, Log.VERBOSE)) {
418            Log.v(TAG, "ChooseAccountTypeActivity.startChooseAccountTypeActivity()");
419        }
420        final Intent intent = new Intent(this, ChooseAccountTypeActivity.class);
421        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
422        intent.putExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
423                getIntent().getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY));
424        intent.putExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,
425                getIntent().getBundleExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE));
426        intent.putExtra(EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY,
427                getIntent().getStringArrayExtra(EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY));
428        intent.putExtra(EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING,
429                getIntent().getStringExtra(EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING));
430        startActivityForResult(intent, REQUEST_CHOOSE_TYPE);
431        mPendingRequest = REQUEST_CHOOSE_TYPE;
432    }
433
434    private static class AccountInfo {
435        final Account account;
436        final Drawable drawable;
437        private final boolean checked;
438
439        AccountInfo(Account account, Drawable drawable, boolean checked) {
440            this.account = account;
441            this.drawable = drawable;
442            this.checked = checked;
443        }
444    }
445
446    private static class ViewHolder {
447        ImageView icon;
448        TextView text;
449        ImageView checkmark;
450    }
451
452    private static class AccountArrayAdapter extends ArrayAdapter<AccountInfo> {
453        private LayoutInflater mLayoutInflater;
454        private ArrayList<AccountInfo> mInfos;
455
456        public AccountArrayAdapter(Context context, int textViewResourceId,
457                ArrayList<AccountInfo> infos) {
458            super(context, textViewResourceId, infos);
459            mInfos = infos;
460            mLayoutInflater = (LayoutInflater) context.getSystemService(
461                    Context.LAYOUT_INFLATER_SERVICE);
462        }
463
464        @Override
465        public View getView(int position, View convertView, ViewGroup parent) {
466            ViewHolder holder;
467
468            if (convertView == null) {
469                convertView = mLayoutInflater.inflate(R.layout.choose_selected_account_row, null);
470                holder = new ViewHolder();
471                holder.text = (TextView) convertView.findViewById(R.id.account_row_text);
472                holder.icon = (ImageView) convertView.findViewById(R.id.account_row_icon);
473                holder.checkmark = (ImageView) convertView.findViewById(R.id.account_row_checkmark);
474                convertView.setTag(holder);
475            } else {
476                holder = (ViewHolder) convertView.getTag();
477            }
478
479            holder.text.setText(mInfos.get(position).account.name);
480            holder.icon.setImageDrawable(mInfos.get(position).drawable);
481            final int displayCheckmark =
482                    mInfos.get(position).checked ? View.VISIBLE : View.INVISIBLE;
483            holder.checkmark.setVisibility(displayCheckmark);
484            return convertView;
485        }
486    }
487}
488