Home | History | Annotate | Download | only in accounts
      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  */
     16 package android.accounts;
     17 
     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;
     25 
     26 /**
     27  * @hide
     28  */
     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;
     34 
     35     @Override
     36     public void onCreate(Bundle savedInstanceState) {
     37         super.onCreate(savedInstanceState);
     38 
     39         mAccounts = getIntent().getParcelableArrayExtra(AccountManager.KEY_ACCOUNTS);
     40         mAccountManagerResponse =
     41                 getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE);
     42 
     43         // KEY_ACCOUNTS is a required parameter
     44         if (mAccounts == null) {
     45             setResult(RESULT_CANCELED);
     46             finish();
     47             return;
     48         }
     49 
     50         String[] mAccountNames = new String[mAccounts.length];
     51         for (int i = 0; i < mAccounts.length; i++) {
     52             mAccountNames[i] = ((Account) mAccounts[i]).name;
     53         }
     54 
     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);
     60     }
     61 
     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);
     68         mResult = bundle;
     69         finish();
     70     }
     71 
     72     public void finish() {
     73         if (mAccountManagerResponse != null) {
     74             if (mResult != null) {
     75                 mAccountManagerResponse.onResult(mResult);
     76             } else {
     77                 mAccountManagerResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");
     78             }
     79         }
     80         super.finish();
     81     }
     82 }
     83