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.content.pm.RegisteredServicesCache;
     20 import android.content.res.Resources;
     21 import android.os.Bundle;
     22 import android.widget.TextView;
     23 import android.widget.LinearLayout;
     24 import android.view.View;
     25 import android.view.LayoutInflater;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.content.pm.PackageManager;
     29 import android.text.TextUtils;
     30 import com.android.internal.R;
     31 
     32 import java.io.IOException;
     33 import java.net.Authenticator;
     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 
     52     protected void onCreate(Bundle savedInstanceState) {
     53         super.onCreate(savedInstanceState);
     54         setContentView(R.layout.grant_credentials_permission);
     55         setTitle(R.string.grant_permissions_header_text);
     56 
     57         mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     58 
     59         final Bundle extras = getIntent().getExtras();
     60         if (extras == null) {
     61             // we were somehow started with bad parameters. abort the activity.
     62             setResult(Activity.RESULT_CANCELED);
     63             finish();
     64             return;
     65         }
     66 
     67         // Grant 'account'/'type' to mUID
     68         mAccount = extras.getParcelable(EXTRAS_ACCOUNT);
     69         mAuthTokenType = extras.getString(EXTRAS_AUTH_TOKEN_TYPE);
     70         mUid = extras.getInt(EXTRAS_REQUESTING_UID);
     71         final PackageManager pm = getPackageManager();
     72         final String[] packages = pm.getPackagesForUid(mUid);
     73 
     74         if (mAccount == null || mAuthTokenType == null || packages == null) {
     75             // we were somehow started with bad parameters. abort the activity.
     76             setResult(Activity.RESULT_CANCELED);
     77             finish();
     78             return;
     79         }
     80 
     81         String accountTypeLabel;
     82         try {
     83             accountTypeLabel = getAccountLabel(mAccount);
     84         } catch (IllegalArgumentException e) {
     85             // label or resource was missing. abort the activity.
     86             setResult(Activity.RESULT_CANCELED);
     87             finish();
     88             return;
     89         }
     90 
     91         final TextView authTokenTypeView = (TextView) findViewById(R.id.authtoken_type);
     92         authTokenTypeView.setVisibility(View.GONE);
     93 
     94         final AccountManagerCallback<String> callback = new AccountManagerCallback<String>() {
     95             public void run(AccountManagerFuture<String> future) {
     96                 try {
     97                     final String authTokenLabel = future.getResult();
     98                     if (!TextUtils.isEmpty(authTokenLabel)) {
     99                         runOnUiThread(new Runnable() {
    100                             public void run() {
    101                                 if (!isFinishing()) {
    102                                     authTokenTypeView.setText(authTokenLabel);
    103                                     authTokenTypeView.setVisibility(View.VISIBLE);
    104                                 }
    105                             }
    106                         });
    107                     }
    108                 } catch (OperationCanceledException e) {
    109                 } catch (IOException e) {
    110                 } catch (AuthenticatorException e) {
    111                 }
    112             }
    113         };
    114         AccountManager.get(this).getAuthTokenLabel(mAccount.type, mAuthTokenType, callback, null);
    115 
    116         findViewById(R.id.allow_button).setOnClickListener(this);
    117         findViewById(R.id.deny_button).setOnClickListener(this);
    118 
    119         LinearLayout packagesListView = (LinearLayout) findViewById(R.id.packages_list);
    120 
    121         for (String pkg : packages) {
    122             String packageLabel;
    123             try {
    124                 packageLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString();
    125             } catch (PackageManager.NameNotFoundException e) {
    126                 packageLabel = pkg;
    127             }
    128             packagesListView.addView(newPackageView(packageLabel));
    129         }
    130 
    131         ((TextView) findViewById(R.id.account_name)).setText(mAccount.name);
    132         ((TextView) findViewById(R.id.account_type)).setText(accountTypeLabel);
    133     }
    134 
    135     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 
    153     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;
    157     }
    158 
    159     public void onClick(View v) {
    160         switch (v.getId()) {
    161             case R.id.allow_button:
    162                 AccountManager.get(this).updateAppPermission(mAccount, mAuthTokenType, mUid, true);
    163                 Intent result = new Intent();
    164                 result.putExtra("retry", true);
    165                 setResult(RESULT_OK, result);
    166                 setAccountAuthenticatorResult(result.getExtras());
    167                 break;
    168 
    169             case R.id.deny_button:
    170                 AccountManager.get(this).updateAppPermission(mAccount, mAuthTokenType, mUid, false);
    171                 setResult(RESULT_CANCELED);
    172                 break;
    173         }
    174         finish();
    175     }
    176 
    177     public final void setAccountAuthenticatorResult(Bundle result) {
    178         mResultBundle = result;
    179     }
    180 
    181     /**
    182      * Sends the result or a {@link AccountManager#ERROR_CODE_CANCELED} error if a
    183      * result isn't present.
    184      */
    185     public void finish() {
    186         Intent intent = getIntent();
    187         AccountAuthenticatorResponse response = intent.getParcelableExtra(EXTRAS_RESPONSE);
    188         if (response != null) {
    189             // send the result bundle back if set, otherwise send an error.
    190             if (mResultBundle != null) {
    191                 response.onResult(mResultBundle);
    192             } else {
    193                 response.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");
    194             }
    195         }
    196         super.finish();
    197     }
    198 }
    199