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