blob: 9f2c39bbc4ceb5c38c5c8df64c6969bd64269725 [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;
Brian Carlstrom46703b02011-04-06 15:41:29 -070021import android.content.res.Resources;
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080022import android.graphics.drawable.Drawable;
Fred Quintana33269202009-04-20 16:05:10 -070023import android.os.Bundle;
24import android.os.Parcelable;
Fred Quintana33269202009-04-20 16:05:10 -070025import android.util.Log;
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080026import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.AdapterView;
30import android.widget.ArrayAdapter;
31import android.widget.ImageView;
32import android.widget.ListView;
33import android.widget.TextView;
34import com.android.internal.R;
35
36import java.util.HashMap;
Fred Quintana33269202009-04-20 16:05:10 -070037
Fred Quintanaf7ae77c2009-10-02 17:19:31 -070038/**
39 * @hide
40 */
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080041public class ChooseAccountActivity extends Activity {
42
Fred Quintana33269202009-04-20 16:05:10 -070043 private static final String TAG = "AccountManager";
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080044
Fred Quintana33269202009-04-20 16:05:10 -070045 private Parcelable[] mAccounts = null;
46 private AccountManagerResponse mAccountManagerResponse = null;
47 private Bundle mResult;
48
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080049 private HashMap<String, AuthenticatorDescription> mTypeToAuthDescription
50 = new HashMap<String, AuthenticatorDescription>();
51
Fred Quintana33269202009-04-20 16:05:10 -070052 @Override
53 public void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
Dmitry Dementyev01985ff2017-01-19 16:03:39 -080055 // TODO This activity is only used by getAuthTokenByFeatures and can not see
56 // VISIBILITY_USER_MANAGED_NOT_VISIBLE accounts. It should be moved to account managed
57 // service.
Fred Quintana26222612010-03-01 13:45:22 -080058 mAccounts = getIntent().getParcelableArrayExtra(AccountManager.KEY_ACCOUNTS);
59 mAccountManagerResponse =
60 getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE);
61
62 // KEY_ACCOUNTS is a required parameter
63 if (mAccounts == null) {
64 setResult(RESULT_CANCELED);
65 finish();
66 return;
Fred Quintana33269202009-04-20 16:05:10 -070067 }
68
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080069 getAuthDescriptions();
70
71 AccountInfo[] mAccountInfos = new AccountInfo[mAccounts.length];
Fred Quintana33269202009-04-20 16:05:10 -070072 for (int i = 0; i < mAccounts.length; i++) {
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080073 mAccountInfos[i] = new AccountInfo(((Account) mAccounts[i]).name,
74 getDrawableForType(((Account) mAccounts[i]).type));
Fred Quintana33269202009-04-20 16:05:10 -070075 }
76
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080077 setContentView(R.layout.choose_account);
78
79 // Setup the list
Alan Viverette51efddb2017-04-05 10:00:01 -040080 ListView list = findViewById(android.R.id.list);
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080081 // Use an existing ListAdapter that will map an array of strings to TextViews
82 list.setAdapter(new AccountArrayAdapter(this,
83 android.R.layout.simple_list_item_1, mAccountInfos));
84 list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
85 list.setTextFilterEnabled(true);
86 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
87 public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
88 onListItemClick((ListView)parent, v, position, id);
89 }
90 });
91 }
92
93 private void getAuthDescriptions() {
94 for(AuthenticatorDescription desc : AccountManager.get(this).getAuthenticatorTypes()) {
95 mTypeToAuthDescription.put(desc.type, desc);
96 }
97 }
98
99 private Drawable getDrawableForType(String accountType) {
100 Drawable icon = null;
101 if(mTypeToAuthDescription.containsKey(accountType)) {
102 try {
103 AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
104 Context authContext = createPackageContext(desc.packageName, 0);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800105 icon = authContext.getDrawable(desc.iconId);
Fabrice Di Meglio795f1352011-01-20 16:12:36 -0800106 } catch (PackageManager.NameNotFoundException e) {
107 // Nothing we can do much here, just log
108 if (Log.isLoggable(TAG, Log.WARN)) {
Brian Carlstrom46703b02011-04-06 15:41:29 -0700109 Log.w(TAG, "No icon name for account type " + accountType);
110 }
111 } catch (Resources.NotFoundException e) {
112 // Nothing we can do much here, just log
113 if (Log.isLoggable(TAG, Log.WARN)) {
114 Log.w(TAG, "No icon resource for account type " + accountType);
Fabrice Di Meglio795f1352011-01-20 16:12:36 -0800115 }
116 }
117 }
118 return icon;
Fred Quintana33269202009-04-20 16:05:10 -0700119 }
120
121 protected void onListItemClick(ListView l, View v, int position, long id) {
122 Account account = (Account) mAccounts[position];
123 Log.d(TAG, "selected account " + account);
124 Bundle bundle = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700125 bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
126 bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
Fred Quintana33269202009-04-20 16:05:10 -0700127 mResult = bundle;
128 finish();
129 }
130
131 public void finish() {
132 if (mAccountManagerResponse != null) {
133 if (mResult != null) {
134 mAccountManagerResponse.onResult(mResult);
135 } else {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700136 mAccountManagerResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");
Fred Quintana33269202009-04-20 16:05:10 -0700137 }
138 }
139 super.finish();
140 }
Fabrice Di Meglio795f1352011-01-20 16:12:36 -0800141
142 private static class AccountInfo {
143 final String name;
144 final Drawable drawable;
145
146 AccountInfo(String name, Drawable drawable) {
147 this.name = name;
148 this.drawable = drawable;
149 }
150 }
151
152 private static class ViewHolder {
153 ImageView icon;
154 TextView text;
155 }
156
157 private static class AccountArrayAdapter extends ArrayAdapter<AccountInfo> {
158 private LayoutInflater mLayoutInflater;
159 private AccountInfo[] mInfos;
160
161 public AccountArrayAdapter(Context context, int textViewResourceId, AccountInfo[] infos) {
162 super(context, textViewResourceId, infos);
163 mInfos = infos;
164 mLayoutInflater = (LayoutInflater) context.getSystemService(
165 Context.LAYOUT_INFLATER_SERVICE);
166 }
167
168 @Override
169 public View getView(int position, View convertView, ViewGroup parent) {
170 ViewHolder holder;
171
172 if (convertView == null) {
173 convertView = mLayoutInflater.inflate(R.layout.choose_account_row, null);
174 holder = new ViewHolder();
175 holder.text = (TextView) convertView.findViewById(R.id.account_row_text);
176 holder.icon = (ImageView) convertView.findViewById(R.id.account_row_icon);
177 convertView.setTag(holder);
178 } else {
179 holder = (ViewHolder) convertView.getTag();
180 }
181
182 holder.text.setText(mInfos[position].name);
183 holder.icon.setImageDrawable(mInfos[position].drawable);
184
185 return convertView;
186 }
187 }
Fred Quintana33269202009-04-20 16:05:10 -0700188}