blob: 6a436869a0ec964782b19e083976a6ce48f73752 [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;
sunjianbdabd402017-06-06 17:54:07 -070019import android.app.ActivityManager;
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080020import android.content.Context;
21import android.content.pm.PackageManager;
Brian Carlstrom46703b02011-04-06 15:41:29 -070022import android.content.res.Resources;
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080023import android.graphics.drawable.Drawable;
Fred Quintana33269202009-04-20 16:05:10 -070024import android.os.Bundle;
sunjianbdabd402017-06-06 17:54:07 -070025import android.os.IBinder;
Fred Quintana33269202009-04-20 16:05:10 -070026import android.os.Parcelable;
sunjianbdabd402017-06-06 17:54:07 -070027import android.os.RemoteException;
28import android.os.Process;
29import android.os.UserHandle;
Fred Quintana33269202009-04-20 16:05:10 -070030import android.util.Log;
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080031import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
34import android.widget.AdapterView;
35import android.widget.ArrayAdapter;
36import android.widget.ImageView;
37import android.widget.ListView;
38import android.widget.TextView;
39import com.android.internal.R;
40
41import java.util.HashMap;
Fred Quintana33269202009-04-20 16:05:10 -070042
Fred Quintanaf7ae77c2009-10-02 17:19:31 -070043/**
44 * @hide
45 */
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080046public class ChooseAccountActivity extends Activity {
47
Fred Quintana33269202009-04-20 16:05:10 -070048 private static final String TAG = "AccountManager";
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080049
Fred Quintana33269202009-04-20 16:05:10 -070050 private Parcelable[] mAccounts = null;
51 private AccountManagerResponse mAccountManagerResponse = null;
52 private Bundle mResult;
sunjianbdabd402017-06-06 17:54:07 -070053 private int mCallingUid;
54 private String mCallingPackage;
Fred Quintana33269202009-04-20 16:05:10 -070055
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080056 private HashMap<String, AuthenticatorDescription> mTypeToAuthDescription
57 = new HashMap<String, AuthenticatorDescription>();
58
Fred Quintana33269202009-04-20 16:05:10 -070059 @Override
60 public void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
Fred Quintana26222612010-03-01 13:45:22 -080062 mAccounts = getIntent().getParcelableArrayExtra(AccountManager.KEY_ACCOUNTS);
63 mAccountManagerResponse =
64 getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE);
65
66 // KEY_ACCOUNTS is a required parameter
67 if (mAccounts == null) {
68 setResult(RESULT_CANCELED);
69 finish();
70 return;
Fred Quintana33269202009-04-20 16:05:10 -070071 }
72
sunjianbdabd402017-06-06 17:54:07 -070073 try {
74 IBinder activityToken = getActivityToken();
75 mCallingUid = ActivityManager.getService().getLaunchedFromUid(activityToken);
76 mCallingPackage = ActivityManager.getService().getLaunchedFromPackage(
77 activityToken);
78 } catch (RemoteException re) {
79 // Couldn't figure out caller details
80 Log.w(getClass().getSimpleName(), "Unable to get caller identity \n" + re);
81 }
82
83 if (UserHandle.isSameApp(mCallingUid, Process.SYSTEM_UID) &&
84 getIntent().getStringExtra(AccountManager.KEY_ANDROID_PACKAGE_NAME) != null) {
85 mCallingPackage = getIntent().getStringExtra(
86 AccountManager.KEY_ANDROID_PACKAGE_NAME);
87 }
88
89 if (!UserHandle.isSameApp(mCallingUid, Process.SYSTEM_UID) &&
90 getIntent().getStringExtra(AccountManager.KEY_ANDROID_PACKAGE_NAME) != null) {
91 Log.w(getClass().getSimpleName(),
92 "Non-system Uid: " + mCallingUid + " tried to override packageName \n");
93 }
94
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080095 getAuthDescriptions();
96
97 AccountInfo[] mAccountInfos = new AccountInfo[mAccounts.length];
Fred Quintana33269202009-04-20 16:05:10 -070098 for (int i = 0; i < mAccounts.length; i++) {
Fabrice Di Meglio795f1352011-01-20 16:12:36 -080099 mAccountInfos[i] = new AccountInfo(((Account) mAccounts[i]).name,
100 getDrawableForType(((Account) mAccounts[i]).type));
Fred Quintana33269202009-04-20 16:05:10 -0700101 }
102
Fabrice Di Meglio795f1352011-01-20 16:12:36 -0800103 setContentView(R.layout.choose_account);
104
105 // Setup the list
Alan Viverette51efddb2017-04-05 10:00:01 -0400106 ListView list = findViewById(android.R.id.list);
Fabrice Di Meglio795f1352011-01-20 16:12:36 -0800107 // Use an existing ListAdapter that will map an array of strings to TextViews
108 list.setAdapter(new AccountArrayAdapter(this,
109 android.R.layout.simple_list_item_1, mAccountInfos));
110 list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
111 list.setTextFilterEnabled(true);
112 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
113 public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
114 onListItemClick((ListView)parent, v, position, id);
115 }
116 });
117 }
118
119 private void getAuthDescriptions() {
120 for(AuthenticatorDescription desc : AccountManager.get(this).getAuthenticatorTypes()) {
121 mTypeToAuthDescription.put(desc.type, desc);
122 }
123 }
124
125 private Drawable getDrawableForType(String accountType) {
126 Drawable icon = null;
127 if(mTypeToAuthDescription.containsKey(accountType)) {
128 try {
129 AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
130 Context authContext = createPackageContext(desc.packageName, 0);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800131 icon = authContext.getDrawable(desc.iconId);
Fabrice Di Meglio795f1352011-01-20 16:12:36 -0800132 } catch (PackageManager.NameNotFoundException e) {
133 // Nothing we can do much here, just log
134 if (Log.isLoggable(TAG, Log.WARN)) {
Brian Carlstrom46703b02011-04-06 15:41:29 -0700135 Log.w(TAG, "No icon name for account type " + accountType);
136 }
137 } catch (Resources.NotFoundException e) {
138 // Nothing we can do much here, just log
139 if (Log.isLoggable(TAG, Log.WARN)) {
140 Log.w(TAG, "No icon resource for account type " + accountType);
Fabrice Di Meglio795f1352011-01-20 16:12:36 -0800141 }
142 }
143 }
144 return icon;
Fred Quintana33269202009-04-20 16:05:10 -0700145 }
146
147 protected void onListItemClick(ListView l, View v, int position, long id) {
148 Account account = (Account) mAccounts[position];
sunjianbdabd402017-06-06 17:54:07 -0700149 // Mark account as visible since user chose it.
150 AccountManager am = AccountManager.get(this);
151 Integer oldVisibility = am.getAccountVisibility(account, mCallingPackage);
152 if (oldVisibility != null
153 && oldVisibility == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE) {
154 am.setAccountVisibility(account, mCallingPackage,
155 AccountManager.VISIBILITY_USER_MANAGED_VISIBLE);
156 }
Fred Quintana33269202009-04-20 16:05:10 -0700157 Log.d(TAG, "selected account " + account);
158 Bundle bundle = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700159 bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
160 bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
Fred Quintana33269202009-04-20 16:05:10 -0700161 mResult = bundle;
162 finish();
163 }
164
165 public void finish() {
166 if (mAccountManagerResponse != null) {
167 if (mResult != null) {
168 mAccountManagerResponse.onResult(mResult);
169 } else {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700170 mAccountManagerResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");
Fred Quintana33269202009-04-20 16:05:10 -0700171 }
172 }
173 super.finish();
174 }
Fabrice Di Meglio795f1352011-01-20 16:12:36 -0800175
176 private static class AccountInfo {
177 final String name;
178 final Drawable drawable;
179
180 AccountInfo(String name, Drawable drawable) {
181 this.name = name;
182 this.drawable = drawable;
183 }
184 }
185
186 private static class ViewHolder {
187 ImageView icon;
188 TextView text;
189 }
190
191 private static class AccountArrayAdapter extends ArrayAdapter<AccountInfo> {
192 private LayoutInflater mLayoutInflater;
193 private AccountInfo[] mInfos;
194
195 public AccountArrayAdapter(Context context, int textViewResourceId, AccountInfo[] infos) {
196 super(context, textViewResourceId, infos);
197 mInfos = infos;
198 mLayoutInflater = (LayoutInflater) context.getSystemService(
199 Context.LAYOUT_INFLATER_SERVICE);
200 }
201
202 @Override
203 public View getView(int position, View convertView, ViewGroup parent) {
204 ViewHolder holder;
205
206 if (convertView == null) {
207 convertView = mLayoutInflater.inflate(R.layout.choose_account_row, null);
208 holder = new ViewHolder();
209 holder.text = (TextView) convertView.findViewById(R.id.account_row_text);
210 holder.icon = (ImageView) convertView.findViewById(R.id.account_row_icon);
211 convertView.setTag(holder);
212 } else {
213 holder = (ViewHolder) convertView.getTag();
214 }
215
216 holder.text.setText(mInfos[position].name);
217 holder.icon.setImageDrawable(mInfos[position].drawable);
218
219 return convertView;
220 }
221 }
Fred Quintana33269202009-04-20 16:05:10 -0700222}