ChooseAccountActivity.java revision f7ae77cd67f1a3993b8e56c1af4720a7adf4e69d
1/*
2 * Copyright (C) 2009 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.ListActivity;
19import android.os.Bundle;
20import android.os.Parcelable;
21import android.widget.ArrayAdapter;
22import android.widget.ListView;
23import android.view.View;
24import android.util.Log;
25
26/**
27 * @hide
28 */
29public class ChooseAccountActivity extends ListActivity {
30    private static final String TAG = "AccountManager";
31    private Parcelable[] mAccounts = null;
32    private AccountManagerResponse mAccountManagerResponse = null;
33    private Bundle mResult;
34
35    @Override
36    public void onCreate(Bundle savedInstanceState) {
37        super.onCreate(savedInstanceState);
38
39        if (savedInstanceState == null) {
40            mAccounts = getIntent().getParcelableArrayExtra(AccountManager.KEY_ACCOUNTS);
41            mAccountManagerResponse =
42                    getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE);
43        } else {
44            mAccounts = savedInstanceState.getParcelableArray(AccountManager.KEY_ACCOUNTS);
45            mAccountManagerResponse =
46                    savedInstanceState.getParcelable(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE);
47        }
48
49        String[] mAccountNames = new String[mAccounts.length];
50        for (int i = 0; i < mAccounts.length; i++) {
51            mAccountNames[i] = ((Account) mAccounts[i]).name;
52        }
53
54        // Use an existing ListAdapter that will map an array
55        // of strings to TextViews
56        setListAdapter(new ArrayAdapter<String>(this,
57                android.R.layout.simple_list_item_1, mAccountNames));
58        getListView().setTextFilterEnabled(true);
59    }
60
61    protected void onListItemClick(ListView l, View v, int position, long id) {
62        Account account = (Account) mAccounts[position];
63        Log.d(TAG, "selected account " + account);
64        Bundle bundle = new Bundle();
65        bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
66        bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
67        mResult = bundle;
68        finish();
69    }
70
71    public void finish() {
72        if (mAccountManagerResponse != null) {
73            if (mResult != null) {
74                mAccountManagerResponse.onResult(mResult);
75            } else {
76                mAccountManagerResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");
77            }
78        }
79        super.finish();
80    }
81}
82