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