2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package android.accounts;
18 import android.app.ListActivity;
19 import android.os.Bundle;
20 import android.os.Parcelable;
21 import android.widget.ArrayAdapter;
22 import android.widget.ListView;
23 import android.view.View;
24 import android.util.Log;
29 public 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;
36 public void onCreate(Bundle savedInstanceState) {
37 super.onCreate(savedInstanceState);
39 mAccounts = getIntent().getParcelableArrayExtra(AccountManager.KEY_ACCOUNTS);
40 mAccountManagerResponse =
41 getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE);
43 // KEY_ACCOUNTS is a required parameter
44 if (mAccounts == null) {
45 setResult(RESULT_CANCELED);
50 String[] mAccountNames = new String[mAccounts.length];
51 for (int i = 0; i < mAccounts.length; i++) {
52 mAccountNames[i] = ((Account) mAccounts[i]).name;
55 // Use an existing ListAdapter that will map an array
56 // of strings to TextViews
57 setListAdapter(new ArrayAdapter<String>(this,
58 android.R.layout.simple_list_item_1, mAccountNames));
59 getListView().setTextFilterEnabled(true);
62 protected void onListItemClick(ListView l, View v, int position, long id) {
63 Account account = (Account) mAccounts[position];
64 Log.d(TAG, "selected account " + account);
65 Bundle bundle = new Bundle();
66 bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
67 bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
72 public void finish() {
73 if (mAccountManagerResponse != null) {
74 if (mResult != null) {
75 mAccountManagerResponse.onResult(mResult);
77 mAccountManagerResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");