blob: 89eee6d4d36a952c79febf2e53d7aded7fc7724a [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;
19import android.os.Bundle;
Costin Manolache5f383ad92010-12-02 16:44:46 -080020import android.os.RemoteException;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070021import android.widget.TextView;
Fred Quintanac4989b12009-10-13 14:02:10 -070022import android.widget.LinearLayout;
23import android.widget.ImageView;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070024import android.view.View;
25import android.view.LayoutInflater;
Fred Quintanac4989b12009-10-13 14:02:10 -070026import android.view.Window;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070027import android.content.Context;
28import android.content.Intent;
29import android.content.pm.PackageManager;
Costin Manolache5f383ad92010-12-02 16:44:46 -080030import android.content.pm.RegisteredServicesCache;
Fred Quintanac4989b12009-10-13 14:02:10 -070031import android.text.TextUtils;
32import android.graphics.drawable.Drawable;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070033import com.android.internal.R;
34
35/**
36 * @hide
37 */
38public 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 Quintanac4989b12009-10-13 14:02:10 -070050 protected LayoutInflater mInflater;
Costin Manolache5f383ad92010-12-02 16:44:46 -080051 private final AccountManagerService accountManagerService = AccountManagerService.getSingleton();
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070052
53 protected void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
Fred Quintanac4989b12009-10-13 14:02:10 -070055 setContentView(R.layout.grant_credentials_permission);
Adam Powellc91466f2011-01-22 14:34:13 -080056 setTitle(R.string.grant_permissions_header_text);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070057
Fred Quintanac4989b12009-10-13 14:02:10 -070058 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070059
Fred Quintanac4989b12009-10-13 14:02:10 -070060 final Bundle extras = getIntent().getExtras();
Costin Manolache5f383ad92010-12-02 16:44:46 -080061
62 // Grant 'account'/'type' to mUID
Fred Quintanac4989b12009-10-13 14:02:10 -070063 mAccount = extras.getParcelable(EXTRAS_ACCOUNT);
64 mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE);
Costin Manolache5f383ad92010-12-02 16:44:46 -080065 mUid = extras.getInt(EXTRAS_REQUESTING_UID);
66 final PackageManager pm = getPackageManager();
67 final String[] packages = pm.getPackagesForUid(mUid);
Fred Quintana382601f2010-03-25 12:25:10 -070068
Costin Manolache5f383ad92010-12-02 16:44:46 -080069 if (mAccount == null || mAuthTokenType == null || packages == null) {
Fred Quintana382601f2010-03-25 12:25:10 -070070 // we were somehow started with bad parameters. abort the activity.
71 setResult(Activity.RESULT_CANCELED);
72 finish();
73 return;
74 }
75
Costin Manolache5f383ad92010-12-02 16:44:46 -080076 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 Quintanad4a1d2e2009-07-16 16:36:38 -0700105
Fred Quintanac4989b12009-10-13 14:02:10 -0700106 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 Quintanac4989b12009-10-13 14:02:10 -0700111 for (String pkg : packages) {
112 String packageLabel;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700113 try {
Fred Quintanac4989b12009-10-13 14:02:10 -0700114 packageLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString();
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700115 } catch (PackageManager.NameNotFoundException e) {
Fred Quintanac4989b12009-10-13 14:02:10 -0700116 packageLabel = pkg;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700117 }
Fred Quintanac4989b12009-10-13 14:02:10 -0700118 packagesListView.addView(newPackageView(packageLabel));
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700119 }
Fred Quintanac4989b12009-10-13 14:02:10 -0700120
121 ((TextView) findViewById(R.id.account_name)).setText(mAccount.name);
122 ((TextView) findViewById(R.id.account_type)).setText(accountTypeLabel);
Fred Quintanac4989b12009-10-13 14:02:10 -0700123 }
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 Quintanad4a1d2e2009-07-16 16:36:38 -0700129 }
130
131 public void onClick(View v) {
132 switch (v.getId()) {
Fred Quintanac4989b12009-10-13 14:02:10 -0700133 case R.id.allow_button:
134 accountManagerService.grantAppPermission(mAccount, mAuthTokenType, mUid);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700135 Intent result = new Intent();
136 result.putExtra("retry", true);
137 setResult(RESULT_OK, result);
138 setAccountAuthenticatorResult(result.getExtras());
139 break;
140
Fred Quintanac4989b12009-10-13 14:02:10 -0700141 case R.id.deny_button:
142 accountManagerService.revokeAppPermission(mAccount, mAuthTokenType, mUid);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700143 setResult(RESULT_CANCELED);
144 break;
145 }
146 finish();
147 }
148
149 public final void setAccountAuthenticatorResult(Bundle result) {
150 mResultBundle = result;
151 }
152
153 /**
Fred Quintanac4989b12009-10-13 14:02:10 -0700154 * Sends the result or a {@link AccountManager#ERROR_CODE_CANCELED} error if a
155 * result isn't present.
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700156 */
157 public void finish() {
158 Intent intent = getIntent();
Fred Quintanac4989b12009-10-13 14:02:10 -0700159 AccountAuthenticatorResponse response = intent.getParcelableExtra(EXTRAS_RESPONSE);
160 if (response != null) {
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700161 // send the result bundle back if set, otherwise send an error.
162 if (mResultBundle != null) {
Fred Quintanac4989b12009-10-13 14:02:10 -0700163 response.onResult(mResultBundle);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700164 } else {
Fred Quintanac4989b12009-10-13 14:02:10 -0700165 response.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700166 }
167 }
168 super.finish();
169 }
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700170}