blob: fd340cbeb921d23b33aa3cd484daf8f35c07894a [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;
20import android.widget.TextView;
Fred Quintanac4989b12009-10-13 14:02:10 -070021import android.widget.LinearLayout;
22import android.widget.ImageView;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070023import android.view.View;
24import android.view.LayoutInflater;
Fred Quintanac4989b12009-10-13 14:02:10 -070025import android.view.Window;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070026import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
Fred Quintanac4989b12009-10-13 14:02:10 -070029import android.text.TextUtils;
30import android.graphics.drawable.Drawable;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070031import com.android.internal.R;
32
33/**
34 * @hide
35 */
36public 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 Quintanac4989b12009-10-13 14:02:10 -070048 protected LayoutInflater mInflater;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070049
50 protected void onCreate(Bundle savedInstanceState) {
Fred Quintanac4989b12009-10-13 14:02:10 -070051 requestWindowFeature(Window.FEATURE_NO_TITLE);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070052 super.onCreate(savedInstanceState);
Fred Quintanac4989b12009-10-13 14:02:10 -070053 setContentView(R.layout.grant_credentials_permission);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070054
Fred Quintanac4989b12009-10-13 14:02:10 -070055 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070056
Fred Quintanac4989b12009-10-13 14:02:10 -070057 final Bundle extras = getIntent().getExtras();
58 mAccount = extras.getParcelable(EXTRAS_ACCOUNT);
59 mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE);
Fred Quintana382601f2010-03-25 12:25:10 -070060
61 if (mAccount == null || mAuthTokenType == null) {
62 // we were somehow started with bad parameters. abort the activity.
63 setResult(Activity.RESULT_CANCELED);
64 finish();
65 return;
66 }
67
Fred Quintanac4989b12009-10-13 14:02:10 -070068 mUid = extras.getInt(EXTRAS_REQUESTING_UID);
69 final String accountTypeLabel = extras.getString(EXTRAS_ACCOUNT_TYPE_LABEL);
70 final String[] packages = extras.getStringArray(EXTRAS_PACKAGES);
71 final String authTokenLabel = extras.getString(EXTRAS_AUTH_TOKEN_LABEL);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070072
Fred Quintanac4989b12009-10-13 14:02:10 -070073 findViewById(R.id.allow_button).setOnClickListener(this);
74 findViewById(R.id.deny_button).setOnClickListener(this);
75
76 LinearLayout packagesListView = (LinearLayout) findViewById(R.id.packages_list);
77
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070078 final PackageManager pm = getPackageManager();
Fred Quintanac4989b12009-10-13 14:02:10 -070079 for (String pkg : packages) {
80 String packageLabel;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070081 try {
Fred Quintanac4989b12009-10-13 14:02:10 -070082 packageLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString();
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070083 } catch (PackageManager.NameNotFoundException e) {
Fred Quintanac4989b12009-10-13 14:02:10 -070084 packageLabel = pkg;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070085 }
Fred Quintanac4989b12009-10-13 14:02:10 -070086 packagesListView.addView(newPackageView(packageLabel));
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070087 }
Fred Quintanac4989b12009-10-13 14:02:10 -070088
89 ((TextView) findViewById(R.id.account_name)).setText(mAccount.name);
90 ((TextView) findViewById(R.id.account_type)).setText(accountTypeLabel);
91 TextView authTokenTypeView = (TextView) findViewById(R.id.authtoken_type);
92 if (TextUtils.isEmpty(authTokenLabel)) {
93 authTokenTypeView.setVisibility(View.GONE);
94 } else {
95 authTokenTypeView.setText(authTokenLabel);
96 }
97 }
98
99 private View newPackageView(String packageLabel) {
100 View view = mInflater.inflate(R.layout.permissions_package_list_item, null);
101 ((TextView) view.findViewById(R.id.package_label)).setText(packageLabel);
102 return view;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700103 }
104
105 public void onClick(View v) {
Fred Quintanac4989b12009-10-13 14:02:10 -0700106 final AccountManagerService accountManagerService = AccountManagerService.getSingleton();
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700107 switch (v.getId()) {
Fred Quintanac4989b12009-10-13 14:02:10 -0700108 case R.id.allow_button:
109 accountManagerService.grantAppPermission(mAccount, mAuthTokenType, mUid);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700110 Intent result = new Intent();
111 result.putExtra("retry", true);
112 setResult(RESULT_OK, result);
113 setAccountAuthenticatorResult(result.getExtras());
114 break;
115
Fred Quintanac4989b12009-10-13 14:02:10 -0700116 case R.id.deny_button:
117 accountManagerService.revokeAppPermission(mAccount, mAuthTokenType, mUid);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700118 setResult(RESULT_CANCELED);
119 break;
120 }
121 finish();
122 }
123
124 public final void setAccountAuthenticatorResult(Bundle result) {
125 mResultBundle = result;
126 }
127
128 /**
Fred Quintanac4989b12009-10-13 14:02:10 -0700129 * Sends the result or a {@link AccountManager#ERROR_CODE_CANCELED} error if a
130 * result isn't present.
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700131 */
132 public void finish() {
133 Intent intent = getIntent();
Fred Quintanac4989b12009-10-13 14:02:10 -0700134 AccountAuthenticatorResponse response = intent.getParcelableExtra(EXTRAS_RESPONSE);
135 if (response != null) {
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700136 // send the result bundle back if set, otherwise send an error.
137 if (mResultBundle != null) {
Fred Quintanac4989b12009-10-13 14:02:10 -0700138 response.onResult(mResultBundle);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700139 } else {
Fred Quintanac4989b12009-10-13 14:02:10 -0700140 response.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700141 }
142 }
143 super.finish();
144 }
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700145}