blob: 293df782e2958d771bb070be6caa60456e2a2088 [file] [log] [blame]
Fred Quintana33269202009-04-20 16:05:10 -07001/*
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
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080018import android.app.Activity;
19import android.content.Context;
20import android.content.pm.PackageManager;
21import android.graphics.drawable.Drawable;
Fred Quintana33269202009-04-20 16:05:10 -070022import android.os.Bundle;
23import android.os.Parcelable;
Fred Quintana33269202009-04-20 16:05:10 -070024import android.util.Log;
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080025import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.AdapterView;
29import android.widget.ArrayAdapter;
30import android.widget.ImageView;
31import android.widget.ListView;
32import android.widget.TextView;
33import com.android.internal.R;
34
35import java.util.HashMap;
Fred Quintana33269202009-04-20 16:05:10 -070036
Fred Quintanaf7ae77c2009-10-02 17:19:31 -070037/**
38 * @hide
39 */
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080040public class ChooseAccountActivity extends Activity {
41
Fred Quintana33269202009-04-20 16:05:10 -070042 private static final String TAG = "AccountManager";
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080043
Fred Quintana33269202009-04-20 16:05:10 -070044 private Parcelable[] mAccounts = null;
45 private AccountManagerResponse mAccountManagerResponse = null;
46 private Bundle mResult;
47
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080048 private HashMap<String, AuthenticatorDescription> mTypeToAuthDescription
49 = new HashMap<String, AuthenticatorDescription>();
50
Fred Quintana33269202009-04-20 16:05:10 -070051 @Override
52 public void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54
Fred Quintana26222612010-03-01 13:45:22 -080055 mAccounts = getIntent().getParcelableArrayExtra(AccountManager.KEY_ACCOUNTS);
56 mAccountManagerResponse =
57 getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE);
58
59 // KEY_ACCOUNTS is a required parameter
60 if (mAccounts == null) {
61 setResult(RESULT_CANCELED);
62 finish();
63 return;
Fred Quintana33269202009-04-20 16:05:10 -070064 }
65
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080066 getAuthDescriptions();
67
68 AccountInfo[] mAccountInfos = new AccountInfo[mAccounts.length];
Fred Quintana33269202009-04-20 16:05:10 -070069 for (int i = 0; i < mAccounts.length; i++) {
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080070 mAccountInfos[i] = new AccountInfo(((Account) mAccounts[i]).name,
71 getDrawableForType(((Account) mAccounts[i]).type));
Fred Quintana33269202009-04-20 16:05:10 -070072 }
73
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080074 setContentView(R.layout.choose_account);
75
76 // Setup the list
77 ListView list = (ListView) findViewById(android.R.id.list);
78 // Use an existing ListAdapter that will map an array of strings to TextViews
79 list.setAdapter(new AccountArrayAdapter(this,
80 android.R.layout.simple_list_item_1, mAccountInfos));
81 list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
82 list.setTextFilterEnabled(true);
83 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
84 public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
85 onListItemClick((ListView)parent, v, position, id);
86 }
87 });
88 }
89
90 private void getAuthDescriptions() {
91 for(AuthenticatorDescription desc : AccountManager.get(this).getAuthenticatorTypes()) {
92 mTypeToAuthDescription.put(desc.type, desc);
93 }
94 }
95
96 private Drawable getDrawableForType(String accountType) {
97 Drawable icon = null;
98 if(mTypeToAuthDescription.containsKey(accountType)) {
99 try {
100 AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
101 Context authContext = createPackageContext(desc.packageName, 0);
102 icon = authContext.getResources().getDrawable(desc.iconId);
103 } catch (PackageManager.NameNotFoundException e) {
104 // Nothing we can do much here, just log
105 if (Log.isLoggable(TAG, Log.WARN)) {
106 Log.w(TAG, "No icon for account type " + accountType);
107 }
108 }
109 }
110 return icon;
Fred Quintana33269202009-04-20 16:05:10 -0700111 }
112
113 protected void onListItemClick(ListView l, View v, int position, long id) {
114 Account account = (Account) mAccounts[position];
115 Log.d(TAG, "selected account " + account);
116 Bundle bundle = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700117 bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
118 bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
Fred Quintana33269202009-04-20 16:05:10 -0700119 mResult = bundle;
120 finish();
121 }
122
123 public void finish() {
124 if (mAccountManagerResponse != null) {
125 if (mResult != null) {
126 mAccountManagerResponse.onResult(mResult);
127 } else {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700128 mAccountManagerResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");
Fred Quintana33269202009-04-20 16:05:10 -0700129 }
130 }
131 super.finish();
132 }
Fabrice Di Meglio795f1352011-01-20 16:12:36 -0800133
134 private static class AccountInfo {
135 final String name;
136 final Drawable drawable;
137
138 AccountInfo(String name, Drawable drawable) {
139 this.name = name;
140 this.drawable = drawable;
141 }
142 }
143
144 private static class ViewHolder {
145 ImageView icon;
146 TextView text;
147 }
148
149 private static class AccountArrayAdapter extends ArrayAdapter<AccountInfo> {
150 private LayoutInflater mLayoutInflater;
151 private AccountInfo[] mInfos;
152
153 public AccountArrayAdapter(Context context, int textViewResourceId, AccountInfo[] infos) {
154 super(context, textViewResourceId, infos);
155 mInfos = infos;
156 mLayoutInflater = (LayoutInflater) context.getSystemService(
157 Context.LAYOUT_INFLATER_SERVICE);
158 }
159
160 @Override
161 public View getView(int position, View convertView, ViewGroup parent) {
162 ViewHolder holder;
163
164 if (convertView == null) {
165 convertView = mLayoutInflater.inflate(R.layout.choose_account_row, null);
166 holder = new ViewHolder();
167 holder.text = (TextView) convertView.findViewById(R.id.account_row_text);
168 holder.icon = (ImageView) convertView.findViewById(R.id.account_row_icon);
169 convertView.setTag(holder);
170 } else {
171 holder = (ViewHolder) convertView.getTag();
172 }
173
174 holder.text.setText(mInfos[position].name);
175 holder.icon.setImageDrawable(mInfos[position].drawable);
176
177 return convertView;
178 }
179 }
Fred Quintana33269202009-04-20 16:05:10 -0700180}