blob: af74b036a796d30171e4e57dd01c462ba76e7c8a [file] [log] [blame]
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07001/*
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 */
16package android.accounts;
17
18import android.app.Activity;
Fred Quintanad9640ec2012-05-23 12:37:00 -070019import android.content.res.Resources;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070020import android.os.Bundle;
21import android.widget.TextView;
Fred Quintanac4989b12009-10-13 14:02:10 -070022import android.widget.LinearLayout;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070023import android.view.View;
24import android.view.LayoutInflater;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070025import android.content.Context;
26import android.content.Intent;
27import android.content.pm.PackageManager;
Fred Quintanac4989b12009-10-13 14:02:10 -070028import android.text.TextUtils;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070029import com.android.internal.R;
30
Fred Quintanad9640ec2012-05-23 12:37:00 -070031import java.io.IOException;
Fred Quintanad9640ec2012-05-23 12:37:00 -070032
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070033/**
34 * @hide
35 */
36public class GrantCredentialsPermissionActivity extends Activity implements View.OnClickListener {
37 public static final String EXTRAS_ACCOUNT = "account";
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070038 public static final String EXTRAS_AUTH_TOKEN_TYPE = "authTokenType";
39 public static final String EXTRAS_RESPONSE = "response";
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070040 public static final String EXTRAS_REQUESTING_UID = "uid";
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070041
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070042 private Account mAccount;
43 private String mAuthTokenType;
44 private int mUid;
45 private Bundle mResultBundle = null;
Fred Quintanac4989b12009-10-13 14:02:10 -070046 protected LayoutInflater mInflater;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070047
48 protected void onCreate(Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
Fred Quintanac4989b12009-10-13 14:02:10 -070050 setContentView(R.layout.grant_credentials_permission);
Adam Powellc91466f2011-01-22 14:34:13 -080051 setTitle(R.string.grant_permissions_header_text);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070052
Fred Quintanac4989b12009-10-13 14:02:10 -070053 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070054
Fred Quintanac4989b12009-10-13 14:02:10 -070055 final Bundle extras = getIntent().getExtras();
Brian Carlstrom46703b02011-04-06 15:41:29 -070056 if (extras == null) {
57 // we were somehow started with bad parameters. abort the activity.
58 setResult(Activity.RESULT_CANCELED);
59 finish();
60 return;
61 }
Costin Manolache5f383ad92010-12-02 16:44:46 -080062
63 // Grant 'account'/'type' to mUID
Fred Quintanac4989b12009-10-13 14:02:10 -070064 mAccount = extras.getParcelable(EXTRAS_ACCOUNT);
65 mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE);
Costin Manolache5f383ad92010-12-02 16:44:46 -080066 mUid = extras.getInt(EXTRAS_REQUESTING_UID);
67 final PackageManager pm = getPackageManager();
68 final String[] packages = pm.getPackagesForUid(mUid);
Fred Quintana382601f2010-03-25 12:25:10 -070069
Costin Manolache5f383ad92010-12-02 16:44:46 -080070 if (mAccount == null || mAuthTokenType == null || packages == null) {
Fred Quintana382601f2010-03-25 12:25:10 -070071 // we were somehow started with bad parameters. abort the activity.
72 setResult(Activity.RESULT_CANCELED);
73 finish();
74 return;
75 }
76
Brian Carlstrom46703b02011-04-06 15:41:29 -070077 String accountTypeLabel;
78 try {
Fred Quintanad9640ec2012-05-23 12:37:00 -070079 accountTypeLabel = getAccountLabel(mAccount);
Brian Carlstrom46703b02011-04-06 15:41:29 -070080 } catch (IllegalArgumentException e) {
81 // label or resource was missing. abort the activity.
82 setResult(Activity.RESULT_CANCELED);
83 finish();
84 return;
85 }
Costin Manolache5f383ad92010-12-02 16:44:46 -080086
Alan Viverette51efddb2017-04-05 10:00:01 -040087 final TextView authTokenTypeView = findViewById(R.id.authtoken_type);
Costin Manolache5f383ad92010-12-02 16:44:46 -080088 authTokenTypeView.setVisibility(View.GONE);
89
Fred Quintanad9640ec2012-05-23 12:37:00 -070090 final AccountManagerCallback<String> callback = new AccountManagerCallback<String>() {
91 public void run(AccountManagerFuture<String> future) {
92 try {
93 final String authTokenLabel = future.getResult();
94 if (!TextUtils.isEmpty(authTokenLabel)) {
95 runOnUiThread(new Runnable() {
96 public void run() {
97 if (!isFinishing()) {
98 authTokenTypeView.setText(authTokenLabel);
99 authTokenTypeView.setVisibility(View.VISIBLE);
100 }
Costin Manolache5f383ad92010-12-02 16:44:46 -0800101 }
Fred Quintanad9640ec2012-05-23 12:37:00 -0700102 });
103 }
104 } catch (OperationCanceledException e) {
105 } catch (IOException e) {
106 } catch (AuthenticatorException e) {
Costin Manolache5f383ad92010-12-02 16:44:46 -0800107 }
108 }
Costin Manolache5f383ad92010-12-02 16:44:46 -0800109 };
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700110
Svet Ganovf6d424f12016-09-20 20:18:53 -0700111 if (!AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(mAuthTokenType)) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700112 AccountManager.get(this).getAuthTokenLabel(mAccount.type,
113 mAuthTokenType, callback, null);
114 }
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700115
Fred Quintanac4989b12009-10-13 14:02:10 -0700116 findViewById(R.id.allow_button).setOnClickListener(this);
117 findViewById(R.id.deny_button).setOnClickListener(this);
118
Alan Viverette51efddb2017-04-05 10:00:01 -0400119 LinearLayout packagesListView = findViewById(R.id.packages_list);
Fred Quintanac4989b12009-10-13 14:02:10 -0700120
Fred Quintanac4989b12009-10-13 14:02:10 -0700121 for (String pkg : packages) {
122 String packageLabel;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700123 try {
Fred Quintanac4989b12009-10-13 14:02:10 -0700124 packageLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString();
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700125 } catch (PackageManager.NameNotFoundException e) {
Fred Quintanac4989b12009-10-13 14:02:10 -0700126 packageLabel = pkg;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700127 }
Fred Quintanac4989b12009-10-13 14:02:10 -0700128 packagesListView.addView(newPackageView(packageLabel));
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700129 }
Fred Quintanac4989b12009-10-13 14:02:10 -0700130
131 ((TextView) findViewById(R.id.account_name)).setText(mAccount.name);
132 ((TextView) findViewById(R.id.account_type)).setText(accountTypeLabel);
Fred Quintanac4989b12009-10-13 14:02:10 -0700133 }
134
Fred Quintanad9640ec2012-05-23 12:37:00 -0700135 private String getAccountLabel(Account account) {
136 final AuthenticatorDescription[] authenticatorTypes =
137 AccountManager.get(this).getAuthenticatorTypes();
138 for (int i = 0, N = authenticatorTypes.length; i < N; i++) {
139 final AuthenticatorDescription desc = authenticatorTypes[i];
140 if (desc.type.equals(account.type)) {
141 try {
142 return createPackageContext(desc.packageName, 0).getString(desc.labelId);
143 } catch (PackageManager.NameNotFoundException e) {
144 return account.type;
145 } catch (Resources.NotFoundException e) {
146 return account.type;
147 }
148 }
149 }
150 return account.type;
151 }
152
Fred Quintanac4989b12009-10-13 14:02:10 -0700153 private View newPackageView(String packageLabel) {
154 View view = mInflater.inflate(R.layout.permissions_package_list_item, null);
155 ((TextView) view.findViewById(R.id.package_label)).setText(packageLabel);
156 return view;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700157 }
158
159 public void onClick(View v) {
160 switch (v.getId()) {
Fred Quintanac4989b12009-10-13 14:02:10 -0700161 case R.id.allow_button:
Fred Quintanad9640ec2012-05-23 12:37:00 -0700162 AccountManager.get(this).updateAppPermission(mAccount, mAuthTokenType, mUid, true);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700163 Intent result = new Intent();
164 result.putExtra("retry", true);
165 setResult(RESULT_OK, result);
166 setAccountAuthenticatorResult(result.getExtras());
167 break;
168
Fred Quintanac4989b12009-10-13 14:02:10 -0700169 case R.id.deny_button:
Fred Quintanad9640ec2012-05-23 12:37:00 -0700170 AccountManager.get(this).updateAppPermission(mAccount, mAuthTokenType, mUid, false);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700171 setResult(RESULT_CANCELED);
172 break;
173 }
174 finish();
175 }
176
177 public final void setAccountAuthenticatorResult(Bundle result) {
178 mResultBundle = result;
179 }
180
181 /**
Fred Quintanac4989b12009-10-13 14:02:10 -0700182 * Sends the result or a {@link AccountManager#ERROR_CODE_CANCELED} error if a
183 * result isn't present.
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700184 */
185 public void finish() {
186 Intent intent = getIntent();
Fred Quintanac4989b12009-10-13 14:02:10 -0700187 AccountAuthenticatorResponse response = intent.getParcelableExtra(EXTRAS_RESPONSE);
188 if (response != null) {
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700189 // send the result bundle back if set, otherwise send an error.
190 if (mResultBundle != null) {
Fred Quintanac4989b12009-10-13 14:02:10 -0700191 response.onResult(mResultBundle);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700192 } else {
Fred Quintanac4989b12009-10-13 14:02:10 -0700193 response.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700194 }
195 }
196 super.finish();
197 }
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700198}