Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -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 | |
| 18 | import android.app.Activity; |
| 19 | import android.os.Bundle; |
| 20 | import android.widget.TextView; |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 21 | import android.widget.LinearLayout; |
| 22 | import android.widget.ImageView; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 23 | import android.view.View; |
| 24 | import android.view.LayoutInflater; |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 25 | import android.view.Window; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.pm.PackageManager; |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 29 | import android.text.TextUtils; |
| 30 | import android.graphics.drawable.Drawable; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 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; |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 48 | protected LayoutInflater mInflater; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 49 | |
| 50 | protected void onCreate(Bundle savedInstanceState) { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 51 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 52 | super.onCreate(savedInstanceState); |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 53 | setContentView(R.layout.grant_credentials_permission); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 54 | |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 55 | mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 56 | |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 57 | final Bundle extras = getIntent().getExtras(); |
| 58 | mAccount = extras.getParcelable(EXTRAS_ACCOUNT); |
| 59 | mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE); |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame^] | 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 | |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 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); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 72 | |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 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 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 78 | final PackageManager pm = getPackageManager(); |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 79 | for (String pkg : packages) { |
| 80 | String packageLabel; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 81 | try { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 82 | packageLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 83 | } catch (PackageManager.NameNotFoundException e) { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 84 | packageLabel = pkg; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 85 | } |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 86 | packagesListView.addView(newPackageView(packageLabel)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 87 | } |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 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; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | public void onClick(View v) { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 106 | final AccountManagerService accountManagerService = AccountManagerService.getSingleton(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 107 | switch (v.getId()) { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 108 | case R.id.allow_button: |
| 109 | accountManagerService.grantAppPermission(mAccount, mAuthTokenType, mUid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 110 | Intent result = new Intent(); |
| 111 | result.putExtra("retry", true); |
| 112 | setResult(RESULT_OK, result); |
| 113 | setAccountAuthenticatorResult(result.getExtras()); |
| 114 | break; |
| 115 | |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 116 | case R.id.deny_button: |
| 117 | accountManagerService.revokeAppPermission(mAccount, mAuthTokenType, mUid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 118 | setResult(RESULT_CANCELED); |
| 119 | break; |
| 120 | } |
| 121 | finish(); |
| 122 | } |
| 123 | |
| 124 | public final void setAccountAuthenticatorResult(Bundle result) { |
| 125 | mResultBundle = result; |
| 126 | } |
| 127 | |
| 128 | /** |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 129 | * Sends the result or a {@link AccountManager#ERROR_CODE_CANCELED} error if a |
| 130 | * result isn't present. |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 131 | */ |
| 132 | public void finish() { |
| 133 | Intent intent = getIntent(); |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 134 | AccountAuthenticatorResponse response = intent.getParcelableExtra(EXTRAS_RESPONSE); |
| 135 | if (response != null) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 136 | // send the result bundle back if set, otherwise send an error. |
| 137 | if (mResultBundle != null) { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 138 | response.onResult(mResultBundle); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 139 | } else { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 140 | response.onError(AccountManager.ERROR_CODE_CANCELED, "canceled"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | super.finish(); |
| 144 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 145 | } |