Home | History | Annotate | Download | only in accounts
      1 /*
      2  * Copyright (C) 2014 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.tv.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.content.Intent;
     26 import android.os.Bundle;
     27 import android.provider.Settings;
     28 import android.util.Log;
     29 
     30 import java.io.IOException;
     31 
     32 
     33 public class AddAccountWithTypeActivity extends Activity {
     34 
     35     // Must match com.google.android.gms.common.AccountPicker.
     36     public static final String EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY = "allowableAccountTypes";
     37 
     38     private static final String TAG = "AddAccountWithType";
     39 
     40     private static final int REQUEST_CHOOSE_ACCOUNT_TYPE = 0;
     41     private static final int REQUEST_ADD_ACCOUNT = 1;
     42     private static final String CHOOSE_ACCOUNT_TYPE_ACTION =
     43             "com.google.android.gms.common.account.CHOOSE_ACCOUNT_TYPE";
     44 
     45     private final AccountManagerCallback<Bundle> mCallback = new AccountManagerCallback<Bundle>() {
     46         @Override
     47         public void run(AccountManagerFuture<Bundle> future) {
     48             try {
     49                 Intent addAccountIntent = future.getResult()
     50                         .getParcelable(AccountManager.KEY_INTENT);
     51                 if (addAccountIntent == null) {
     52                     Log.e(TAG, "Failed to retrieve add account intent from authenticator");
     53                     setResultAndFinish(Activity.RESULT_CANCELED);
     54                 } else {
     55                     startActivityForResult(addAccountIntent, REQUEST_ADD_ACCOUNT);
     56                 }
     57             } catch (IOException|AuthenticatorException|OperationCanceledException e) {
     58                 Log.e(TAG, "Failed to get add account intent: ", e);
     59                 setResultAndFinish(Activity.RESULT_CANCELED);
     60             }
     61         }
     62     };
     63 
     64     @Override
     65     protected void onCreate(Bundle savedInstanceState) {
     66         super.onCreate(savedInstanceState);
     67         String accountType = getIntent().getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
     68         if (accountType != null) {
     69             startAddAccount(accountType);
     70         } else {
     71             String[] allowedTypes = getIntent().getStringArrayExtra(
     72                     EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY);
     73             if (allowedTypes == null || allowedTypes.length == 0) {
     74                 allowedTypes = getIntent().getStringArrayExtra(Settings.EXTRA_ACCOUNT_TYPES);
     75             }
     76             startAccountTypePicker(allowedTypes);
     77         }
     78     }
     79 
     80     @Override
     81     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     82         // User selected an account type, so kick off the add account flow for that.
     83         if (requestCode == REQUEST_CHOOSE_ACCOUNT_TYPE && resultCode == Activity.RESULT_OK) {
     84             String accountType = data.getExtras().getString(AccountManager.KEY_ACCOUNT_TYPE);
     85             startAddAccount(accountType);
     86         } else {
     87             setResultAndFinish(resultCode);
     88         }
     89     }
     90 
     91     private void startAccountTypePicker(String[] allowedTypes) {
     92         Intent i = new Intent(CHOOSE_ACCOUNT_TYPE_ACTION);
     93         i.putExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY, allowedTypes);
     94         startActivityForResult(i, REQUEST_CHOOSE_ACCOUNT_TYPE);
     95     }
     96 
     97     private void startAddAccount(String accountType) {
     98         AccountManager.get(this).addAccount(
     99                 accountType,
    100                 null, /* authTokenType */
    101                 null, /* requiredFeatures */
    102                 null, /* accountOptions */
    103                 null, mCallback, null);
    104     }
    105 
    106     private void setResultAndFinish(int resultCode) {
    107         setResult(resultCode);
    108         finish();
    109     }
    110 }
    111