• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.tv.settings.accounts.AccountsUtil.ACCOUNTS_FRAGMENT_DEFAULT;
20 
21 import android.accounts.AccountManager;
22 import android.accounts.AccountManagerCallback;
23 import android.accounts.AccountManagerFuture;
24 import android.accounts.AuthenticatorException;
25 import android.accounts.OperationCanceledException;
26 import android.app.Activity;
27 import android.content.Intent;
28 import android.os.Bundle;
29 import android.provider.Settings;
30 import android.util.Log;
31 
32 import androidx.fragment.app.FragmentActivity;
33 
34 import com.android.tv.settings.overlay.FlavorUtils;
35 
36 import java.io.IOException;
37 
38 
39 public class AddAccountWithTypeActivity extends FragmentActivity {
40 
41     // Must match com.google.android.gms.common.AccountPicker.
42     public static final String EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY = "allowableAccountTypes";
43 
44     private static final String TAG = "AddAccountWithType";
45 
46     private static final int REQUEST_CHOOSE_ACCOUNT_TYPE = 0;
47     private static final int REQUEST_ADD_ACCOUNT = 1;
48     private static final String CHOOSE_ACCOUNT_TYPE_ACTION =
49             "com.google.android.gms.common.account.CHOOSE_ACCOUNT_TYPE";
50 
51     private final AccountManagerCallback<Bundle> mCallback = new AccountManagerCallback<Bundle>() {
52         @Override
53         public void run(AccountManagerFuture<Bundle> future) {
54             try {
55                 Intent addAccountIntent = future.getResult()
56                         .getParcelable(AccountManager.KEY_INTENT);
57                 if (addAccountIntent == null) {
58                     Log.e(TAG, "Failed to retrieve add account intent from authenticator");
59                     setResultAndFinish(Activity.RESULT_CANCELED);
60                 } else {
61                     startActivityForResult(new Intent(addAccountIntent), REQUEST_ADD_ACCOUNT);
62                 }
63             } catch (IOException|AuthenticatorException|OperationCanceledException e) {
64                 Log.e(TAG, "Failed to get add account intent: ", e);
65                 setResultAndFinish(Activity.RESULT_CANCELED);
66             }
67         }
68     };
69 
70     @Override
onCreate(Bundle savedInstanceState)71     protected void onCreate(Bundle savedInstanceState) {
72         super.onCreate(savedInstanceState);
73         // Show AccountsFragment if DO is active and this activity was started through
74         // ADD_ACCOUNT_SETTINGS intent. This ensures that organization info. is displayed and
75         // restrictions are enforced, if present.
76         if (FlavorUtils.getFeatureFactory(this).getEnterprisePrivacyFeatureProvider(
77                 this).hasDeviceOwner() && Settings.ACTION_ADD_ACCOUNT.equals(
78                 getIntent().getAction())) {
79             startAccountsActivity();
80             setResultAndFinish(Activity.RESULT_CANCELED);
81         } else {
82             String accountType = getIntent().getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
83             if (accountType != null) {
84                 startAddAccount(accountType);
85             } else {
86                 String[] allowedTypes = getIntent().getStringArrayExtra(
87                         EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY);
88                 if (allowedTypes == null || allowedTypes.length == 0) {
89                     allowedTypes = getIntent().getStringArrayExtra(Settings.EXTRA_ACCOUNT_TYPES);
90                 }
91                 startAccountTypePicker(allowedTypes);
92             }
93         }
94     }
95 
96     @SuppressWarnings("MissingSuperCall") // TODO: fix me
97     @Override
onActivityResult(int requestCode, int resultCode, Intent data)98     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
99         // User selected an account type, so kick off the add account flow for that.
100         if (requestCode == REQUEST_CHOOSE_ACCOUNT_TYPE && resultCode == Activity.RESULT_OK) {
101             String accountType = data.getExtras().getString(AccountManager.KEY_ACCOUNT_TYPE);
102             startAddAccount(accountType);
103         } else {
104             setResultAndFinish(resultCode);
105         }
106     }
107 
startAccountTypePicker(String[] allowedTypes)108     private void startAccountTypePicker(String[] allowedTypes) {
109         Intent i = new Intent(CHOOSE_ACCOUNT_TYPE_ACTION);
110         i.putExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY, allowedTypes);
111         startActivityForResult(i, REQUEST_CHOOSE_ACCOUNT_TYPE);
112     }
113 
startAddAccount(String accountType)114     private void startAddAccount(String accountType) {
115         AccountManager.get(this).addAccount(
116                 accountType,
117                 null, /* authTokenType */
118                 null, /* requiredFeatures */
119                 null, /* accountOptions */
120                 null, mCallback, null);
121     }
122 
setResultAndFinish(int resultCode)123     private void setResultAndFinish(int resultCode) {
124         setResult(resultCode);
125         finish();
126     }
127 
startAccountsActivity()128     private void startAccountsActivity() {
129         Intent intent = new Intent(this, AccountsActivity.class);
130         intent.putExtra(AccountsActivity.EXTRA_FRAGMENT_TYPE, ACCOUNTS_FRAGMENT_DEFAULT);
131         startActivity(intent);
132     }
133 }
134