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; |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 19 | import android.content.res.Resources; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
| 21 | import android.widget.TextView; |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 22 | import android.widget.LinearLayout; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 23 | import android.view.View; |
| 24 | import android.view.LayoutInflater; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.pm.PackageManager; |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 29 | import com.android.internal.R; |
| 30 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 31 | import java.io.IOException; |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 32 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 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) { |
| 51 | super.onCreate(savedInstanceState); |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 52 | setContentView(R.layout.grant_credentials_permission); |
Adam Powell | c91466f | 2011-01-22 14:34:13 -0800 | [diff] [blame] | 53 | setTitle(R.string.grant_permissions_header_text); |
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(); |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 58 | if (extras == null) { |
| 59 | // we were somehow started with bad parameters. abort the activity. |
| 60 | setResult(Activity.RESULT_CANCELED); |
| 61 | finish(); |
| 62 | return; |
| 63 | } |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 64 | |
| 65 | // Grant 'account'/'type' to mUID |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 66 | mAccount = extras.getParcelable(EXTRAS_ACCOUNT); |
| 67 | mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 68 | mUid = extras.getInt(EXTRAS_REQUESTING_UID); |
| 69 | final PackageManager pm = getPackageManager(); |
| 70 | final String[] packages = pm.getPackagesForUid(mUid); |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 71 | |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 72 | if (mAccount == null || mAuthTokenType == null || packages == null) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 73 | // we were somehow started with bad parameters. abort the activity. |
| 74 | setResult(Activity.RESULT_CANCELED); |
| 75 | finish(); |
| 76 | return; |
| 77 | } |
| 78 | |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 79 | String accountTypeLabel; |
| 80 | try { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 81 | accountTypeLabel = getAccountLabel(mAccount); |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 82 | } catch (IllegalArgumentException e) { |
| 83 | // label or resource was missing. abort the activity. |
| 84 | setResult(Activity.RESULT_CANCELED); |
| 85 | finish(); |
| 86 | return; |
| 87 | } |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 88 | |
| 89 | final TextView authTokenTypeView = (TextView) findViewById(R.id.authtoken_type); |
| 90 | authTokenTypeView.setVisibility(View.GONE); |
| 91 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 92 | final AccountManagerCallback<String> callback = new AccountManagerCallback<String>() { |
| 93 | public void run(AccountManagerFuture<String> future) { |
| 94 | try { |
| 95 | final String authTokenLabel = future.getResult(); |
| 96 | if (!TextUtils.isEmpty(authTokenLabel)) { |
| 97 | runOnUiThread(new Runnable() { |
| 98 | public void run() { |
| 99 | if (!isFinishing()) { |
| 100 | authTokenTypeView.setText(authTokenLabel); |
| 101 | authTokenTypeView.setVisibility(View.VISIBLE); |
| 102 | } |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 103 | } |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 104 | }); |
| 105 | } |
| 106 | } catch (OperationCanceledException e) { |
| 107 | } catch (IOException e) { |
| 108 | } catch (AuthenticatorException e) { |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 111 | }; |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 112 | AccountManager.get(this).getAuthTokenLabel(mAccount.type, mAuthTokenType, callback, null); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 113 | |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 114 | findViewById(R.id.allow_button).setOnClickListener(this); |
| 115 | findViewById(R.id.deny_button).setOnClickListener(this); |
| 116 | |
| 117 | LinearLayout packagesListView = (LinearLayout) findViewById(R.id.packages_list); |
| 118 | |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 119 | for (String pkg : packages) { |
| 120 | String packageLabel; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 121 | try { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 122 | packageLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 123 | } catch (PackageManager.NameNotFoundException e) { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 124 | packageLabel = pkg; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 125 | } |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 126 | packagesListView.addView(newPackageView(packageLabel)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 127 | } |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 128 | |
| 129 | ((TextView) findViewById(R.id.account_name)).setText(mAccount.name); |
| 130 | ((TextView) findViewById(R.id.account_type)).setText(accountTypeLabel); |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 133 | private String getAccountLabel(Account account) { |
| 134 | final AuthenticatorDescription[] authenticatorTypes = |
| 135 | AccountManager.get(this).getAuthenticatorTypes(); |
| 136 | for (int i = 0, N = authenticatorTypes.length; i < N; i++) { |
| 137 | final AuthenticatorDescription desc = authenticatorTypes[i]; |
| 138 | if (desc.type.equals(account.type)) { |
| 139 | try { |
| 140 | return createPackageContext(desc.packageName, 0).getString(desc.labelId); |
| 141 | } catch (PackageManager.NameNotFoundException e) { |
| 142 | return account.type; |
| 143 | } catch (Resources.NotFoundException e) { |
| 144 | return account.type; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | return account.type; |
| 149 | } |
| 150 | |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 151 | private View newPackageView(String packageLabel) { |
| 152 | View view = mInflater.inflate(R.layout.permissions_package_list_item, null); |
| 153 | ((TextView) view.findViewById(R.id.package_label)).setText(packageLabel); |
| 154 | return view; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | public void onClick(View v) { |
| 158 | switch (v.getId()) { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 159 | case R.id.allow_button: |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 160 | AccountManager.get(this).updateAppPermission(mAccount, mAuthTokenType, mUid, true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 161 | Intent result = new Intent(); |
| 162 | result.putExtra("retry", true); |
| 163 | setResult(RESULT_OK, result); |
| 164 | setAccountAuthenticatorResult(result.getExtras()); |
| 165 | break; |
| 166 | |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 167 | case R.id.deny_button: |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 168 | AccountManager.get(this).updateAppPermission(mAccount, mAuthTokenType, mUid, false); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 169 | setResult(RESULT_CANCELED); |
| 170 | break; |
| 171 | } |
| 172 | finish(); |
| 173 | } |
| 174 | |
| 175 | public final void setAccountAuthenticatorResult(Bundle result) { |
| 176 | mResultBundle = result; |
| 177 | } |
| 178 | |
| 179 | /** |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 180 | * Sends the result or a {@link AccountManager#ERROR_CODE_CANCELED} error if a |
| 181 | * result isn't present. |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 182 | */ |
| 183 | public void finish() { |
| 184 | Intent intent = getIntent(); |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 185 | AccountAuthenticatorResponse response = intent.getParcelableExtra(EXTRAS_RESPONSE); |
| 186 | if (response != null) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 187 | // send the result bundle back if set, otherwise send an error. |
| 188 | if (mResultBundle != null) { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 189 | response.onResult(mResultBundle); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 190 | } else { |
Fred Quintana | c4989b1 | 2009-10-13 14:02:10 -0700 | [diff] [blame] | 191 | response.onError(AccountManager.ERROR_CODE_CANCELED, "canceled"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | super.finish(); |
| 195 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 196 | } |