Home | History | Annotate | Download | only in accounts
      1 /*
      2  * Copyright (C) 2008 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 
     17 package com.android.settings.accounts;
     18 
     19 import android.accounts.AccountManager;
     20 import android.accounts.AccountManagerCallback;
     21 import android.accounts.AccountManagerFuture;
     22 import android.accounts.AuthenticatorException;
     23 import android.accounts.OperationCanceledException;
     24 import android.app.Activity;
     25 import android.app.PendingIntent;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.os.Bundle;
     29 import android.os.UserManager;
     30 import android.util.Log;
     31 import android.widget.Toast;
     32 
     33 import com.android.settings.R;
     34 import com.android.settings.Utils;
     35 
     36 import java.io.IOException;
     37 
     38 /**
     39  * Entry point Actiivty for account setup. Works as follows
     40  *
     41  * 1) When the other Activities launch this Activity, it launches {@link ChooseAccountActivity}
     42  *    without showing anything.
     43  * 2) After receiving an account type from ChooseAccountActivity, this Activity launches the
     44  *    account setup specified by AccountManager.
     45  * 3) After the account setup, this Activity finishes without showing anything.
     46  *
     47  * Note:
     48  * Previously this Activity did what {@link ChooseAccountActivity} does right now, but we
     49  * currently delegate the work to the other Activity. When we let this Activity do that work, users
     50  * would see the list of account types when leaving this Activity, since the UI is already ready
     51  * when returning from each account setup, which doesn't look good.
     52  */
     53 public class AddAccountSettings extends Activity {
     54     /**
     55      *
     56      */
     57     private static final String KEY_ADD_CALLED = "AddAccountCalled";
     58 
     59     /**
     60      * Extra parameter to identify the caller. Applications may display a
     61      * different UI if the calls is made from Settings or from a specific
     62      * application.
     63      */
     64     private static final String KEY_CALLER_IDENTITY = "pendingIntent";
     65 
     66     private static final String TAG = "AccountSettings";
     67 
     68     /* package */ static final String EXTRA_SELECTED_ACCOUNT = "selected_account";
     69 
     70     // show additional info regarding the use of a device with multiple users
     71     static final String EXTRA_HAS_MULTIPLE_USERS = "hasMultipleUsers";
     72 
     73     private static final int CHOOSE_ACCOUNT_REQUEST = 1;
     74     private static final int ADD_ACCOUNT_REQUEST = 2;
     75 
     76     private PendingIntent mPendingIntent;
     77 
     78     private final AccountManagerCallback<Bundle> mCallback = new AccountManagerCallback<Bundle>() {
     79         @Override
     80         public void run(AccountManagerFuture<Bundle> future) {
     81             boolean done = true;
     82             try {
     83                 Bundle bundle = future.getResult();
     84                 //bundle.keySet();
     85                 Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
     86                 if (intent != null) {
     87                     done = false;
     88                     Bundle addAccountOptions = new Bundle();
     89                     addAccountOptions.putParcelable(KEY_CALLER_IDENTITY, mPendingIntent);
     90                     addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS,
     91                             Utils.hasMultipleUsers(AddAccountSettings.this));
     92                     intent.putExtras(addAccountOptions);
     93                     startActivityForResult(intent, ADD_ACCOUNT_REQUEST);
     94                 } else {
     95                     setResult(RESULT_OK);
     96                     if (mPendingIntent != null) {
     97                         mPendingIntent.cancel();
     98                         mPendingIntent = null;
     99                     }
    100                 }
    101 
    102                 if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "account added: " + bundle);
    103             } catch (OperationCanceledException e) {
    104                 if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "addAccount was canceled");
    105             } catch (IOException e) {
    106                 if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "addAccount failed: " + e);
    107             } catch (AuthenticatorException e) {
    108                 if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "addAccount failed: " + e);
    109             } finally {
    110                 if (done) {
    111                     finish();
    112                 }
    113             }
    114         }
    115     };
    116 
    117     private boolean mAddAccountCalled = false;
    118 
    119     @Override
    120     public void onCreate(Bundle savedInstanceState) {
    121         super.onCreate(savedInstanceState);
    122 
    123         if (savedInstanceState != null) {
    124             mAddAccountCalled = savedInstanceState.getBoolean(KEY_ADD_CALLED);
    125             if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "restored");
    126         }
    127 
    128         final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
    129         if (um.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
    130             // We aren't allowed to add an account.
    131             Toast.makeText(this, R.string.user_cannot_add_accounts_message, Toast.LENGTH_LONG)
    132                     .show();
    133             finish();
    134             return;
    135         }
    136         if (mAddAccountCalled) {
    137             // We already called add account - maybe the callback was lost.
    138             finish();
    139             return;
    140         }
    141         final String[] authorities =
    142                 getIntent().getStringArrayExtra(AccountPreferenceBase.AUTHORITIES_FILTER_KEY);
    143         final String[] accountTypes =
    144                 getIntent().getStringArrayExtra(AccountPreferenceBase.ACCOUNT_TYPES_FILTER_KEY);
    145         final Intent intent = new Intent(this, ChooseAccountActivity.class);
    146         if (authorities != null) {
    147             intent.putExtra(AccountPreferenceBase.AUTHORITIES_FILTER_KEY, authorities);
    148         }
    149         if (accountTypes != null) {
    150             intent.putExtra(AccountPreferenceBase.ACCOUNT_TYPES_FILTER_KEY, accountTypes);
    151         }
    152         startActivityForResult(intent, CHOOSE_ACCOUNT_REQUEST);
    153     }
    154 
    155     @Override
    156     public void onActivityResult(int requestCode, int resultCode, Intent data) {
    157         switch (requestCode) {
    158         case CHOOSE_ACCOUNT_REQUEST:
    159             if (resultCode == RESULT_CANCELED) {
    160                 setResult(resultCode);
    161                 finish();
    162                 return;
    163             }
    164             // Go to account setup screen. finish() is called inside mCallback.
    165             addAccount(data.getStringExtra(EXTRA_SELECTED_ACCOUNT));
    166             break;
    167         case ADD_ACCOUNT_REQUEST:
    168             setResult(resultCode);
    169             if (mPendingIntent != null) {
    170                 mPendingIntent.cancel();
    171                 mPendingIntent = null;
    172             }
    173             finish();
    174             break;
    175         }
    176     }
    177 
    178     @Override
    179     protected void onSaveInstanceState(Bundle outState) {
    180         super.onSaveInstanceState(outState);
    181         outState.putBoolean(KEY_ADD_CALLED, mAddAccountCalled);
    182         if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "saved");
    183     }
    184 
    185     private void addAccount(String accountType) {
    186         Bundle addAccountOptions = new Bundle();
    187         mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(), 0);
    188         addAccountOptions.putParcelable(KEY_CALLER_IDENTITY, mPendingIntent);
    189         addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS, Utils.hasMultipleUsers(this));
    190         AccountManager.get(this).addAccount(
    191                 accountType,
    192                 null, /* authTokenType */
    193                 null, /* requiredFeatures */
    194                 addAccountOptions,
    195                 null,
    196                 mCallback,
    197                 null /* handler */);
    198         mAddAccountCalled  = true;
    199     }
    200 }
    201