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.ActivityTaskManager; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.content.res.Resources; 24 import android.os.Bundle; 25 import android.os.IBinder; 26 import android.os.Process; 27 import android.os.RemoteException; 28 import android.os.UserHandle; 29 import android.text.TextUtils; 30 import android.util.Log; 31 import android.view.LayoutInflater; 32 import android.view.View; 33 import android.widget.LinearLayout; 34 import android.widget.TextView; 35 36 import com.android.internal.R; 37 38 import java.io.IOException; 39 40 /** 41 * @hide 42 */ 43 public class GrantCredentialsPermissionActivity extends Activity implements View.OnClickListener { 44 public static final String EXTRAS_ACCOUNT = "account"; 45 public static final String EXTRAS_AUTH_TOKEN_TYPE = "authTokenType"; 46 public static final String EXTRAS_RESPONSE = "response"; 47 public static final String EXTRAS_REQUESTING_UID = "uid"; 48 49 private Account mAccount; 50 private String mAuthTokenType; 51 private int mUid; 52 private int mCallingUid; 53 private Bundle mResultBundle = null; 54 protected LayoutInflater mInflater; 55 onCreate(Bundle savedInstanceState)56 protected void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 getWindow().addSystemFlags( 59 android.view.WindowManager.LayoutParams 60 .SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 61 setContentView(R.layout.grant_credentials_permission); 62 setTitle(R.string.grant_permissions_header_text); 63 64 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 65 66 final Bundle extras = getIntent().getExtras(); 67 if (extras == null) { 68 // we were somehow started with bad parameters. abort the activity. 69 setResult(Activity.RESULT_CANCELED); 70 finish(); 71 return; 72 } 73 74 // Grant 'account'/'type' to mUID 75 mAccount = extras.getParcelable(EXTRAS_ACCOUNT); 76 mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE); 77 mUid = extras.getInt(EXTRAS_REQUESTING_UID); 78 final PackageManager pm = getPackageManager(); 79 final String[] packages = pm.getPackagesForUid(mUid); 80 81 if (mAccount == null || mAuthTokenType == null || packages == null) { 82 // we were somehow started with bad parameters. abort the activity. 83 setResult(Activity.RESULT_CANCELED); 84 finish(); 85 return; 86 } 87 88 try { 89 IBinder activityToken = getActivityToken(); 90 mCallingUid = ActivityTaskManager.getService().getLaunchedFromUid(activityToken); 91 } catch (RemoteException re) { 92 // Couldn't figure out caller details 93 Log.w(getClass().getSimpleName(), "Unable to get caller identity \n" + re); 94 } 95 96 if (!UserHandle.isSameApp(mCallingUid, Process.SYSTEM_UID) && mCallingUid != mUid) { 97 setResult(Activity.RESULT_CANCELED); 98 finish(); 99 return; 100 } 101 102 String accountTypeLabel; 103 try { 104 accountTypeLabel = getAccountLabel(mAccount); 105 } catch (IllegalArgumentException e) { 106 // label or resource was missing. abort the activity. 107 setResult(Activity.RESULT_CANCELED); 108 finish(); 109 return; 110 } 111 112 final TextView authTokenTypeView = findViewById(R.id.authtoken_type); 113 authTokenTypeView.setVisibility(View.GONE); 114 115 final AccountManagerCallback<String> callback = new AccountManagerCallback<String>() { 116 public void run(AccountManagerFuture<String> future) { 117 try { 118 final String authTokenLabel = future.getResult(); 119 if (!TextUtils.isEmpty(authTokenLabel)) { 120 runOnUiThread(new Runnable() { 121 public void run() { 122 if (!isFinishing()) { 123 authTokenTypeView.setText(authTokenLabel); 124 authTokenTypeView.setVisibility(View.VISIBLE); 125 } 126 } 127 }); 128 } 129 } catch (OperationCanceledException e) { 130 } catch (IOException e) { 131 } catch (AuthenticatorException e) { 132 } 133 } 134 }; 135 136 if (!AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(mAuthTokenType)) { 137 AccountManager.get(this).getAuthTokenLabel(mAccount.type, 138 mAuthTokenType, callback, null); 139 } 140 141 findViewById(R.id.allow_button).setOnClickListener(this); 142 findViewById(R.id.deny_button).setOnClickListener(this); 143 144 LinearLayout packagesListView = findViewById(R.id.packages_list); 145 146 for (String pkg : packages) { 147 String packageLabel; 148 try { 149 packageLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString(); 150 } catch (PackageManager.NameNotFoundException e) { 151 packageLabel = pkg; 152 } 153 packagesListView.addView(newPackageView(packageLabel)); 154 } 155 156 ((TextView) findViewById(R.id.account_name)).setText(mAccount.name); 157 ((TextView) findViewById(R.id.account_type)).setText(accountTypeLabel); 158 } 159 getAccountLabel(Account account)160 private String getAccountLabel(Account account) { 161 final AuthenticatorDescription[] authenticatorTypes = 162 AccountManager.get(this).getAuthenticatorTypes(); 163 for (int i = 0, N = authenticatorTypes.length; i < N; i++) { 164 final AuthenticatorDescription desc = authenticatorTypes[i]; 165 if (desc.type.equals(account.type)) { 166 try { 167 return createPackageContext(desc.packageName, 0).getString(desc.labelId); 168 } catch (PackageManager.NameNotFoundException e) { 169 return account.type; 170 } catch (Resources.NotFoundException e) { 171 return account.type; 172 } 173 } 174 } 175 return account.type; 176 } 177 newPackageView(String packageLabel)178 private View newPackageView(String packageLabel) { 179 View view = mInflater.inflate(R.layout.permissions_package_list_item, null); 180 ((TextView) view.findViewById(R.id.package_label)).setText(packageLabel); 181 return view; 182 } 183 onClick(View v)184 public void onClick(View v) { 185 switch (v.getId()) { 186 case R.id.allow_button: 187 AccountManager.get(this).updateAppPermission(mAccount, mAuthTokenType, mUid, true); 188 Intent result = new Intent(); 189 result.putExtra("retry", true); 190 setResult(RESULT_OK, result); 191 setAccountAuthenticatorResult(result.getExtras()); 192 break; 193 194 case R.id.deny_button: 195 AccountManager.get(this).updateAppPermission(mAccount, mAuthTokenType, mUid, false); 196 setResult(RESULT_CANCELED); 197 break; 198 } 199 finish(); 200 } 201 setAccountAuthenticatorResult(Bundle result)202 public final void setAccountAuthenticatorResult(Bundle result) { 203 mResultBundle = result; 204 } 205 206 /** 207 * Sends the result or a {@link AccountManager#ERROR_CODE_CANCELED} error if a 208 * result isn't present. 209 */ finish()210 public void finish() { 211 Intent intent = getIntent(); 212 AccountAuthenticatorResponse response = intent.getParcelableExtra(EXTRAS_RESPONSE); 213 if (response != null) { 214 // send the result bundle back if set, otherwise send an error. 215 if (mResultBundle != null) { 216 response.onResult(mResultBundle); 217 } else { 218 response.onError(AccountManager.ERROR_CODE_CANCELED, "canceled"); 219 } 220 } 221 super.finish(); 222 } 223 } 224