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