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