ChooseTypeAndAccountActivity.java revision 9bbdd0bf5006512a000b0d3e6bd6ee2998a2e48b
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 = "AccountManager";
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    private ArrayList<AccountInfo> mAccountInfos;
104
105    @Override
106    public void onCreate(Bundle savedInstanceState) {
107        super.onCreate(savedInstanceState);
108        setContentView(R.layout.choose_type_and_account);
109
110        // save some items we use frequently
111        final AccountManager accountManager = AccountManager.get(this);
112        final Intent intent = getIntent();
113
114        // override the description text if supplied
115        final String descriptionOverride =
116                intent.getStringExtra(EXTRA_DESCRIPTION_TEXT_OVERRIDE);
117        if (!TextUtils.isEmpty(descriptionOverride)) {
118            ((TextView)findViewById(R.id.description)).setText(descriptionOverride);
119        }
120
121        // If the selected account matches one in the list we will place a
122        // checkmark next to it.
123        final Account selectedAccount =
124                (Account)intent.getParcelableExtra(EXTRA_SELECTED_ACCOUNT);
125
126        // build an efficiently queryable map of account types to authenticator descriptions
127        final HashMap<String, AuthenticatorDescription> typeToAuthDescription =
128                new HashMap<String, AuthenticatorDescription>();
129        for(AuthenticatorDescription desc : accountManager.getAuthenticatorTypes()) {
130            typeToAuthDescription.put(desc.type, desc);
131        }
132
133        // Read the validAccounts, if present, and add them to the setOfAllowableAccounts
134        Set<Account> setOfAllowableAccounts = null;
135        final ArrayList<Parcelable> validAccounts =
136                intent.getParcelableArrayListExtra(EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST);
137        if (validAccounts != null) {
138            setOfAllowableAccounts = new HashSet<Account>(validAccounts.size());
139            for (Parcelable parcelable : validAccounts) {
140                setOfAllowableAccounts.add((Account)parcelable);
141            }
142        }
143
144        // Read the validAccountTypes, if present, and add them to the setOfAllowableAccountTypes
145        Set<String> setOfAllowableAccountTypes = null;
146        final String[] validAccountTypes =
147                intent.getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY);
148        if (validAccountTypes != null) {
149            setOfAllowableAccountTypes = new HashSet<String>(validAccountTypes.length);
150            for (String type : validAccountTypes) {
151                setOfAllowableAccountTypes.add(type);
152            }
153        }
154
155        // Create a list of AccountInfo objects for each account that is allowable. Filter out
156        // accounts that don't match the allowable types, if provided, or that don't match the
157        // allowable accounts, if provided.
158        final Account[] accounts = accountManager.getAccounts();
159        mAccountInfos = new ArrayList<AccountInfo>(accounts.length);
160        for (Account account : accounts) {
161            if (setOfAllowableAccounts != null
162                    && !setOfAllowableAccounts.contains(account)) {
163                continue;
164            }
165            if (setOfAllowableAccountTypes != null
166                    && !setOfAllowableAccountTypes.contains(account.type)) {
167                continue;
168            }
169            mAccountInfos.add(new AccountInfo(account,
170                    getDrawableForType(typeToAuthDescription, account.type),
171                    account.equals(selectedAccount)));
172        }
173
174        // If there are no allowable accounts go directly to add account
175        if (mAccountInfos.isEmpty()) {
176            startChooseAccountTypeActivity();
177            return;
178        }
179
180        // if there is only one allowable account return it
181        if (!intent.getBooleanExtra(EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT, false)
182                && mAccountInfos.size() == 1) {
183            Account account = mAccountInfos.get(0).account;
184            setResultAndFinish(account.name, account.type);
185            return;
186        }
187
188        // there is more than one allowable account. initialize the list adapter to allow
189        // the user to select an account.
190        ListView list = (ListView) findViewById(android.R.id.list);
191        list.setAdapter(new AccountArrayAdapter(this,
192                android.R.layout.simple_list_item_1, mAccountInfos));
193        list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
194        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
195            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
196                onListItemClick((ListView)parent, v, position, id);
197            }
198        });
199
200        // set the listener for the addAccount button
201        Button addAccountButton = (Button) findViewById(R.id.addAccount);
202        addAccountButton.setOnClickListener(new View.OnClickListener() {
203            public void onClick(final View v) {
204                startChooseAccountTypeActivity();
205            }
206        });
207    }
208
209    // Called when the choose account type activity (for adding an account) returns.
210    // If it was a success read the account and set it in the result. In all cases
211    // return the result and finish this activity.
212    @Override
213    protected void onActivityResult(final int requestCode, final int resultCode,
214            final Intent data) {
215        if (resultCode == RESULT_OK && data != null) {
216            String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
217            if (accountType != null) {
218                runAddAccountForAuthenticator(accountType);
219                return;
220            }
221        }
222        Log.d(TAG, "ChooseTypeAndAccountActivity.onActivityResult: canceled");
223        setResult(Activity.RESULT_CANCELED);
224        finish();
225    }
226
227    protected void runAddAccountForAuthenticator(String type) {
228        Log.d(TAG, "selected account type " + type);
229        final Bundle options = getIntent().getBundleExtra(
230                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE);
231        final String[] requiredFeatures = getIntent().getStringArrayExtra(
232                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY);
233        final String authTokenType = getIntent().getStringExtra(
234                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING);
235        AccountManager.get(this).addAccount(type, authTokenType, requiredFeatures,
236                options, this, this, null /* Handler */);
237    }
238
239    public void run(final AccountManagerFuture<Bundle> accountManagerFuture) {
240        try {
241            final Bundle accountManagerResult = accountManagerFuture.getResult();
242            final String name = accountManagerResult.getString(AccountManager.KEY_ACCOUNT_NAME);
243            final String type = accountManagerResult.getString(AccountManager.KEY_ACCOUNT_TYPE);
244            if (name != null && type != null) {
245                final Bundle bundle = new Bundle();
246                bundle.putString(AccountManager.KEY_ACCOUNT_NAME, name);
247                bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, type);
248                setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
249                finish();
250                return;
251            }
252        } catch (OperationCanceledException e) {
253            setResult(Activity.RESULT_CANCELED);
254            finish();
255            return;
256        } catch (IOException e) {
257        } catch (AuthenticatorException e) {
258        }
259        Bundle bundle = new Bundle();
260        bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "error communicating with server");
261        setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
262        finish();
263    }
264
265    private Drawable getDrawableForType(
266            final HashMap<String, AuthenticatorDescription> typeToAuthDescription,
267            String accountType) {
268        Drawable icon = null;
269        if (typeToAuthDescription.containsKey(accountType)) {
270            try {
271                AuthenticatorDescription desc = typeToAuthDescription.get(accountType);
272                Context authContext = createPackageContext(desc.packageName, 0);
273                icon = authContext.getResources().getDrawable(desc.iconId);
274            } catch (PackageManager.NameNotFoundException e) {
275                // Nothing we can do much here, just log
276                if (Log.isLoggable(TAG, Log.WARN)) {
277                    Log.w(TAG, "No icon name for account type " + accountType);
278                }
279            } catch (Resources.NotFoundException e) {
280                // Nothing we can do much here, just log
281                if (Log.isLoggable(TAG, Log.WARN)) {
282                    Log.w(TAG, "No icon resource for account type " + accountType);
283                }
284            }
285        }
286        return icon;
287    }
288
289    protected void onListItemClick(ListView l, View v, int position, long id) {
290        AccountInfo accountInfo = mAccountInfos.get(position);
291        Log.d(TAG, "selected account " + accountInfo.account);
292        setResultAndFinish(accountInfo.account.name, accountInfo.account.type);
293    }
294
295    private void setResultAndFinish(final String accountName, final String accountType) {
296        Bundle bundle = new Bundle();
297        bundle.putString(AccountManager.KEY_ACCOUNT_NAME, accountName);
298        bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
299        setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
300        Log.d(TAG, "ChooseTypeAndAccountActivity.setResultAndFinish: "
301                + "selected account " + accountName + ", " + accountType);
302        finish();
303    }
304
305    private void startChooseAccountTypeActivity() {
306        final Intent intent = new Intent(this, ChooseAccountTypeActivity.class);
307        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
308        intent.putExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
309                getIntent().getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY));
310        intent.putExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,
311                getIntent().getBundleExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE));
312        intent.putExtra(EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY,
313                getIntent().getStringArrayExtra(EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY));
314        intent.putExtra(EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING,
315                getIntent().getStringExtra(EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING));
316        startActivityForResult(intent, 0);
317    }
318
319    private static class AccountInfo {
320        final Account account;
321        final Drawable drawable;
322        private final boolean checked;
323
324        AccountInfo(Account account, Drawable drawable, boolean checked) {
325            this.account = account;
326            this.drawable = drawable;
327            this.checked = checked;
328        }
329    }
330
331    private static class ViewHolder {
332        ImageView icon;
333        TextView text;
334        ImageView checkmark;
335    }
336
337    private static class AccountArrayAdapter extends ArrayAdapter<AccountInfo> {
338        private LayoutInflater mLayoutInflater;
339        private ArrayList<AccountInfo> mInfos;
340
341        public AccountArrayAdapter(Context context, int textViewResourceId,
342                ArrayList<AccountInfo> infos) {
343            super(context, textViewResourceId, infos);
344            mInfos = infos;
345            mLayoutInflater = (LayoutInflater) context.getSystemService(
346                    Context.LAYOUT_INFLATER_SERVICE);
347        }
348
349        @Override
350        public View getView(int position, View convertView, ViewGroup parent) {
351            ViewHolder holder;
352
353            if (convertView == null) {
354                convertView = mLayoutInflater.inflate(R.layout.choose_selected_account_row, null);
355                holder = new ViewHolder();
356                holder.text = (TextView) convertView.findViewById(R.id.account_row_text);
357                holder.icon = (ImageView) convertView.findViewById(R.id.account_row_icon);
358                holder.checkmark = (ImageView) convertView.findViewById(R.id.account_row_checkmark);
359                convertView.setTag(holder);
360            } else {
361                holder = (ViewHolder) convertView.getTag();
362            }
363
364            holder.text.setText(mInfos.get(position).account.name);
365            holder.icon.setImageDrawable(mInfos.get(position).drawable);
366            final int displayCheckmark =
367                    mInfos.get(position).checked ? View.VISIBLE : View.INVISIBLE;
368            holder.checkmark.setVisibility(displayCheckmark);
369            return convertView;
370        }
371    }
372}
373