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.Activity; 19 import android.app.ActivityManager; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.content.res.Resources; 23 import android.graphics.drawable.Drawable; 24 import android.os.Bundle; 25 import android.os.IBinder; 26 import android.os.Parcelable; 27 import android.os.RemoteException; 28 import android.os.Process; 29 import android.os.UserHandle; 30 import android.util.Log; 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; 42 43 /** 44 * @hide 45 */ 46 public class ChooseAccountActivity extends Activity { 47 48 private static final String TAG = "AccountManager"; 49 50 private Parcelable[] mAccounts = null; 51 private AccountManagerResponse mAccountManagerResponse = null; 52 private Bundle mResult; 53 private int mCallingUid; 54 private String mCallingPackage; 55 56 private HashMap<String, AuthenticatorDescription> mTypeToAuthDescription 57 = new HashMap<String, AuthenticatorDescription>(); 58 59 @Override 60 public void onCreate(Bundle savedInstanceState) { 61 super.onCreate(savedInstanceState); 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; 71 } 72 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 95 getAuthDescriptions(); 96 97 AccountInfo[] mAccountInfos = new AccountInfo[mAccounts.length]; 98 for (int i = 0; i < mAccounts.length; i++) { 99 mAccountInfos[i] = new AccountInfo(((Account) mAccounts[i]).name, 100 getDrawableForType(((Account) mAccounts[i]).type)); 101 } 102 103 setContentView(R.layout.choose_account); 104 105 // Setup the list 106 ListView list = findViewById(android.R.id.list); 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); 131 icon = authContext.getDrawable(desc.iconId); 132 } catch (PackageManager.NameNotFoundException e) { 133 // Nothing we can do much here, just log 134 if (Log.isLoggable(TAG, Log.WARN)) { 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); 141 } 142 } 143 } 144 return icon; 145 } 146 147 protected void onListItemClick(ListView l, View v, int position, long id) { 148 Account account = (Account) mAccounts[position]; 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 } 157 Log.d(TAG, "selected account " + account); 158 Bundle bundle = new Bundle(); 159 bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); 160 bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); 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 { 170 mAccountManagerResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled"); 171 } 172 } 173 super.finish(); 174 } 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 } 222 } 223