Home | History | Annotate | Download | only in accounts
      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;
     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;
     34 
     35 /**
     36  * @hide
     37  */
     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;
     48     private int mUid;
     49     private Bundle mResultBundle = null;
     50     protected LayoutInflater mInflater;
     51     private final AccountManagerService accountManagerService = AccountManagerService.getSingleton();
     52 
     53     protected void onCreate(Bundle savedInstanceState) {
     54         requestWindowFeature(Window.FEATURE_NO_TITLE);
     55         super.onCreate(savedInstanceState);
     56         setContentView(R.layout.grant_credentials_permission);
     57 
     58         mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     59 
     60         final Bundle extras = getIntent().getExtras();
     61 
     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);
     68 
     69         if (mAccount == null || mAuthTokenType == null || packages == null) {
     70             // we were somehow started with bad parameters. abort the activity.
     71             setResult(Activity.RESULT_CANCELED);
     72             finish();
     73             return;
     74         }
     75 
     76         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);
    105 
    106         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 
    111         for (String pkg : packages) {
    112             String packageLabel;
    113             try {
    114                 packageLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString();
    115             } catch (PackageManager.NameNotFoundException e) {
    116                 packageLabel = pkg;
    117             }
    118             packagesListView.addView(newPackageView(packageLabel));
    119         }
    120 
    121         ((TextView) findViewById(R.id.account_name)).setText(mAccount.name);
    122         ((TextView) findViewById(R.id.account_type)).setText(accountTypeLabel);
    123     }
    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;
    129     }
    130 
    131     public void onClick(View v) {
    132         switch (v.getId()) {
    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());
    139                 break;
    140 
    141             case R.id.deny_button:
    142                 accountManagerService.revokeAppPermission(mAccount, mAuthTokenType, mUid);
    143                 setResult(RESULT_CANCELED);
    144                 break;
    145         }
    146         finish();
    147     }
    148 
    149     public final void setAccountAuthenticatorResult(Bundle result) {
    150         mResultBundle = result;
    151     }
    152 
    153     /**
    154      * Sends the result or a {@link AccountManager#ERROR_CODE_CANCELED} error if a
    155      * result isn't present.
    156      */
    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);
    164             } else {
    165                 response.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");
    166             }
    167         }
    168         super.finish();
    169     }
    170 }
    171