Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 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 | |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 18 | import android.app.Activity; |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.pm.PackageManager; |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 24 | import android.os.Bundle; |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 25 | import android.os.IBinder; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 26 | import android.os.Parcelable; |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 27 | import android.os.RemoteException; |
| 28 | import android.os.Process; |
| 29 | import android.os.UserHandle; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 31 | import android.view.LayoutInflater; |
| 32 | import android.view.View; |
| 33 | import android.view.ViewGroup; |
| 34 | import android.widget.AdapterView; |
| 35 | import android.widget.ArrayAdapter; |
| 36 | import android.widget.ImageView; |
| 37 | import android.widget.ListView; |
| 38 | import android.widget.TextView; |
| 39 | import com.android.internal.R; |
| 40 | |
| 41 | import java.util.HashMap; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 42 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 43 | /** |
| 44 | * @hide |
| 45 | */ |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 46 | public class ChooseAccountActivity extends Activity { |
| 47 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 48 | private static final String TAG = "AccountManager"; |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 49 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 50 | private Parcelable[] mAccounts = null; |
| 51 | private AccountManagerResponse mAccountManagerResponse = null; |
| 52 | private Bundle mResult; |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 53 | private int mCallingUid; |
| 54 | private String mCallingPackage; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 55 | |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 56 | private HashMap<String, AuthenticatorDescription> mTypeToAuthDescription |
| 57 | = new HashMap<String, AuthenticatorDescription>(); |
| 58 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 59 | @Override |
| 60 | public void onCreate(Bundle savedInstanceState) { |
| 61 | super.onCreate(savedInstanceState); |
Fred Quintana | 2622261 | 2010-03-01 13:45:22 -0800 | [diff] [blame] | 62 | 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 Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 71 | } |
| 72 | |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 73 | 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 Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 95 | getAuthDescriptions(); |
| 96 | |
| 97 | AccountInfo[] mAccountInfos = new AccountInfo[mAccounts.length]; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 98 | for (int i = 0; i < mAccounts.length; i++) { |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 99 | mAccountInfos[i] = new AccountInfo(((Account) mAccounts[i]).name, |
| 100 | getDrawableForType(((Account) mAccounts[i]).type)); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 103 | setContentView(R.layout.choose_account); |
| 104 | |
| 105 | // Setup the list |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 106 | ListView list = findViewById(android.R.id.list); |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 107 | // 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 Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 131 | icon = authContext.getDrawable(desc.iconId); |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 132 | } catch (PackageManager.NameNotFoundException e) { |
| 133 | // Nothing we can do much here, just log |
| 134 | if (Log.isLoggable(TAG, Log.WARN)) { |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 135 | 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 Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
| 144 | return icon; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | protected void onListItemClick(ListView l, View v, int position, long id) { |
| 148 | Account account = (Account) mAccounts[position]; |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 149 | // 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 Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 157 | Log.d(TAG, "selected account " + account); |
| 158 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 159 | bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 160 | bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 161 | 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 Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 170 | mAccountManagerResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | super.finish(); |
| 174 | } |
Fabrice Di Meglio | 795f135 | 2011-01-20 16:12:36 -0800 | [diff] [blame] | 175 | |
| 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 Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 222 | } |