2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package android.accounts;
18 import android.app.Activity;
19 import android.os.Bundle;
20 import android.os.RemoteException;
21 import android.widget.TextView;
22 import android.widget.LinearLayout;
23 import android.widget.ImageView;
24 import android.view.View;
25 import android.view.LayoutInflater;
26 import android.view.Window;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.pm.PackageManager;
30 import android.content.pm.RegisteredServicesCache;
31 import android.text.TextUtils;
32 import android.graphics.drawable.Drawable;
33 import com.android.internal.R;
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;
49 private Bundle mResultBundle = null;
50 protected LayoutInflater mInflater;
51 private final AccountManagerService accountManagerService = AccountManagerService.getSingleton();
53 protected void onCreate(Bundle savedInstanceState) {
54 requestWindowFeature(Window.FEATURE_NO_TITLE);
55 super.onCreate(savedInstanceState);
56 setContentView(R.layout.grant_credentials_permission);
58 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
60 final Bundle extras = getIntent().getExtras();
62 // Grant 'account'/'type' to mUID
63 mAccount = extras.getParcelable(EXTRAS_ACCOUNT);
64 mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE);
65 mUid = extras.getInt(EXTRAS_REQUESTING_UID);
66 final PackageManager pm = getPackageManager();
67 final String[] packages = pm.getPackagesForUid(mUid);
69 if (mAccount == null || mAuthTokenType == null || packages == null) {
70 // we were somehow started with bad parameters. abort the activity.
71 setResult(Activity.RESULT_CANCELED);
76 final String accountTypeLabel = accountManagerService.getAccountLabel(mAccount.type);
79 final TextView authTokenTypeView = (TextView) findViewById(R.id.authtoken_type);
80 authTokenTypeView.setVisibility(View.GONE);
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() {
91 authTokenTypeView.setText(authTokenLabel);
92 authTokenTypeView.setVisibility(View.VISIBLE);
99 public void onError(int code, String message) {
103 accountManagerService.getAuthTokenLabel(
104 response, mAccount, mAuthTokenType);
106 findViewById(R.id.allow_button).setOnClickListener(this);
107 findViewById(R.id.deny_button).setOnClickListener(this);
109 LinearLayout packagesListView = (LinearLayout) findViewById(R.id.packages_list);
111 for (String pkg : packages) {
114 packageLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString();
115 } catch (PackageManager.NameNotFoundException e) {
118 packagesListView.addView(newPackageView(packageLabel));
121 ((TextView) findViewById(R.id.account_name)).setText(mAccount.name);
122 ((TextView) findViewById(R.id.account_type)).setText(accountTypeLabel);
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);
131 public void onClick(View v) {
133 case R.id.allow_button:
134 accountManagerService.grantAppPermission(mAccount, mAuthTokenType, mUid);
135 Intent result = new Intent();
136 result.putExtra("retry", true);
137 setResult(RESULT_OK, result);
138 setAccountAuthenticatorResult(result.getExtras());
141 case R.id.deny_button:
142 accountManagerService.revokeAppPermission(mAccount, mAuthTokenType, mUid);
143 setResult(RESULT_CANCELED);
149 public final void setAccountAuthenticatorResult(Bundle result) {
150 mResultBundle = result;
154 * Sends the result or a {@link AccountManager#ERROR_CODE_CANCELED} error if a
155 * result isn't present.
157 public void finish() {
158 Intent intent = getIntent();
159 AccountAuthenticatorResponse response = intent.getParcelableExtra(EXTRAS_RESPONSE);
160 if (response != null) {
161 // send the result bundle back if set, otherwise send an error.
162 if (mResultBundle != null) {
163 response.onResult(mResultBundle);
165 response.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");