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.os.Bundle; 20 import android.os.RemoteException; 21 import android.widget.TextView; 22 import android.widget.LinearLayout; 23 import android.widget.ImageView; 24 import android.view.View; 25 import android.view.LayoutInflater; 26 import android.view.Window; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.content.pm.PackageManager; 30 import android.content.pm.RegisteredServicesCache; 31 import android.text.TextUtils; 32 import android.graphics.drawable.Drawable; 33 import com.android.internal.R; 34 35 /** 36 * @hide 37 */ 38 public class GrantCredentialsPermissionActivity extends Activity implements View.OnClickListener { 39 public static final String EXTRAS_ACCOUNT = "account"; 40 public static final String EXTRAS_AUTH_TOKEN_LABEL = "authTokenLabel"; 41 public static final String EXTRAS_AUTH_TOKEN_TYPE = "authTokenType"; 42 public static final String EXTRAS_RESPONSE = "response"; 43 public static final String EXTRAS_ACCOUNT_TYPE_LABEL = "accountTypeLabel"; 44 public static final String EXTRAS_PACKAGES = "application"; 45 public static final String EXTRAS_REQUESTING_UID = "uid"; 46 private Account mAccount; 47 private String mAuthTokenType; 48 private int mUid; 49 private Bundle mResultBundle = null; 50 protected LayoutInflater mInflater; 51 private final AccountManagerService accountManagerService = AccountManagerService.getSingleton(); 52 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 setContentView(R.layout.grant_credentials_permission); 56 setTitle(R.string.grant_permissions_header_text); 57 58 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 59 60 final Bundle extras = getIntent().getExtras(); 61 if (extras == null) { 62 // we were somehow started with bad parameters. abort the activity. 63 setResult(Activity.RESULT_CANCELED); 64 finish(); 65 return; 66 } 67 68 // Grant 'account'/'type' to mUID 69 mAccount = extras.getParcelable(EXTRAS_ACCOUNT); 70 mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE); 71 mUid = extras.getInt(EXTRAS_REQUESTING_UID); 72 final PackageManager pm = getPackageManager(); 73 final String[] packages = pm.getPackagesForUid(mUid); 74 75 if (mAccount == null || mAuthTokenType == null || packages == null) { 76 // we were somehow started with bad parameters. abort the activity. 77 setResult(Activity.RESULT_CANCELED); 78 finish(); 79 return; 80 } 81 82 String accountTypeLabel; 83 try { 84 accountTypeLabel = accountManagerService.getAccountLabel(mAccount.type); 85 } catch (IllegalArgumentException e) { 86 // label or resource was missing. abort the activity. 87 setResult(Activity.RESULT_CANCELED); 88 finish(); 89 return; 90 } 91 92 final TextView authTokenTypeView = (TextView) findViewById(R.id.authtoken_type); 93 authTokenTypeView.setVisibility(View.GONE); 94 95 /** Handles the responses from the AccountManager */ 96 IAccountManagerResponse response = new IAccountManagerResponse.Stub() { 97 public void onResult(Bundle bundle) { 98 final String authTokenLabel = 99 bundle.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); 100 if (!TextUtils.isEmpty(authTokenLabel)) { 101 runOnUiThread(new Runnable() { 102 public void run() { 103 if (!isFinishing()) { 104 authTokenTypeView.setText(authTokenLabel); 105 authTokenTypeView.setVisibility(View.VISIBLE); 106 } 107 } 108 }); 109 } 110 } 111 112 public void onError(int code, String message) { 113 } 114 }; 115 116 accountManagerService.getAuthTokenLabel( 117 response, mAccount, mAuthTokenType); 118 119 findViewById(R.id.allow_button).setOnClickListener(this); 120 findViewById(R.id.deny_button).setOnClickListener(this); 121 122 LinearLayout packagesListView = (LinearLayout) findViewById(R.id.packages_list); 123 124 for (String pkg : packages) { 125 String packageLabel; 126 try { 127 packageLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString(); 128 } catch (PackageManager.NameNotFoundException e) { 129 packageLabel = pkg; 130 } 131 packagesListView.addView(newPackageView(packageLabel)); 132 } 133 134 ((TextView) findViewById(R.id.account_name)).setText(mAccount.name); 135 ((TextView) findViewById(R.id.account_type)).setText(accountTypeLabel); 136 } 137 138 private View newPackageView(String packageLabel) { 139 View view = mInflater.inflate(R.layout.permissions_package_list_item, null); 140 ((TextView) view.findViewById(R.id.package_label)).setText(packageLabel); 141 return view; 142 } 143 144 public void onClick(View v) { 145 switch (v.getId()) { 146 case R.id.allow_button: 147 accountManagerService.grantAppPermission(mAccount, mAuthTokenType, mUid); 148 Intent result = new Intent(); 149 result.putExtra("retry", true); 150 setResult(RESULT_OK, result); 151 setAccountAuthenticatorResult(result.getExtras()); 152 break; 153 154 case R.id.deny_button: 155 accountManagerService.revokeAppPermission(mAccount, mAuthTokenType, mUid); 156 setResult(RESULT_CANCELED); 157 break; 158 } 159 finish(); 160 } 161 162 public final void setAccountAuthenticatorResult(Bundle result) { 163 mResultBundle = result; 164 } 165 166 /** 167 * Sends the result or a {@link AccountManager#ERROR_CODE_CANCELED} error if a 168 * result isn't present. 169 */ 170 public void finish() { 171 Intent intent = getIntent(); 172 AccountAuthenticatorResponse response = intent.getParcelableExtra(EXTRAS_RESPONSE); 173 if (response != null) { 174 // send the result bundle back if set, otherwise send an error. 175 if (mResultBundle != null) { 176 response.onResult(mResultBundle); 177 } else { 178 response.onError(AccountManager.ERROR_CODE_CANCELED, "canceled"); 179 } 180 } 181 super.finish(); 182 } 183 } 184