1 /* 2 * Copyright (C) 2017 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 com.android.car.settings.accounts; 17 18 import static android.content.Intent.EXTRA_USER; 19 20 import android.accounts.AccountManager; 21 import android.accounts.AccountManagerCallback; 22 import android.accounts.AccountManagerFuture; 23 import android.accounts.AuthenticatorException; 24 import android.accounts.OperationCanceledException; 25 import android.app.Activity; 26 import android.app.PendingIntent; 27 import android.content.ComponentName; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.os.Bundle; 31 import android.os.UserHandle; 32 import android.os.UserManager; 33 import android.widget.Toast; 34 35 import com.android.car.settings.R; 36 import com.android.car.settings.common.Logger; 37 38 import java.io.IOException; 39 40 /** 41 * Entry point Activity for account setup. Works as follows 42 * 43 * <ol> 44 * <li> After receiving an account type from ChooseAccountFragment, this Activity launches the 45 * account setup specified by AccountManager. 46 * <li> After the account setup, this Activity finishes without showing anything. 47 * </ol> 48 */ 49 public class AddAccountActivity extends Activity { 50 /** 51 * A boolean to keep the state of whether add account has already been called. 52 */ 53 private static final String KEY_ADD_CALLED = "AddAccountCalled"; 54 /** 55 * Extra parameter to identify the caller. Applications may display a 56 * different UI if the call is made from Settings or from a specific 57 * application. 58 */ 59 private static final String KEY_CALLER_IDENTITY = "pendingIntent"; 60 private static final String SHOULD_NOT_RESOLVE = "SHOULDN'T RESOLVE!"; 61 62 private static final Logger LOG = new Logger(AddAccountActivity.class); 63 private static final String ALLOW_SKIP = "allowSkip"; 64 65 /* package */ static final String EXTRA_SELECTED_ACCOUNT = "selected_account"; 66 67 // Show additional info regarding the use of a device with multiple users 68 static final String EXTRA_HAS_MULTIPLE_USERS = "hasMultipleUsers"; 69 70 // Need a specific request code for add account activity. 71 private static final int ADD_ACCOUNT_REQUEST = 2001; 72 73 private UserManager mUserManager; 74 private UserHandle mUserHandle; 75 private PendingIntent mPendingIntent; 76 private boolean mAddAccountCalled; 77 78 private final AccountManagerCallback<Bundle> mCallback = new AccountManagerCallback<Bundle>() { 79 @Override 80 public void run(AccountManagerFuture<Bundle> future) { 81 if (!future.isDone()) { 82 LOG.v("Account manager future is not done."); 83 finish(); 84 } 85 boolean done = true; 86 try { 87 Bundle result = future.getResult(); 88 Intent intent = result.getParcelable(AccountManager.KEY_INTENT); 89 if (intent != null) { 90 done = false; 91 Bundle addAccountOptions = new Bundle(); 92 addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS, 93 hasMultipleUsers(AddAccountActivity.this)); 94 addAccountOptions.putParcelable(EXTRA_USER, mUserHandle); 95 intent.putExtras(addAccountOptions); 96 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 97 startActivityForResultAsUser( 98 intent, ADD_ACCOUNT_REQUEST, mUserHandle); 99 } else { 100 setResult(RESULT_OK); 101 if (mPendingIntent != null) { 102 mPendingIntent.cancel(); 103 mPendingIntent = null; 104 } 105 } 106 LOG.v("account added: " + result); 107 } catch (OperationCanceledException | IOException | AuthenticatorException e) { 108 LOG.v("addAccount error: " + e); 109 } finally { 110 if (done) { 111 finish(); 112 } 113 } 114 } 115 }; 116 117 /** 118 * Creates an intent to start the {@link AddAccountActivity} to add an account of the given 119 * account type. 120 */ createAddAccountActivityIntent(Context context, String accountType)121 public static Intent createAddAccountActivityIntent(Context context, String accountType) { 122 Intent intent = new Intent(context, AddAccountActivity.class); 123 intent.putExtra(EXTRA_SELECTED_ACCOUNT, accountType); 124 return intent; 125 } 126 127 @Override onSaveInstanceState(Bundle outState)128 protected void onSaveInstanceState(Bundle outState) { 129 super.onSaveInstanceState(outState); 130 outState.putBoolean(KEY_ADD_CALLED, mAddAccountCalled); 131 LOG.v("saved"); 132 } 133 134 @Override onCreate(Bundle savedInstanceState)135 public void onCreate(Bundle savedInstanceState) { 136 super.onCreate(savedInstanceState); 137 if (savedInstanceState != null) { 138 mAddAccountCalled = savedInstanceState.getBoolean(KEY_ADD_CALLED); 139 LOG.v("Restored from previous add account call: " + mAddAccountCalled); 140 } 141 142 mUserManager = UserManager.get(getApplicationContext()); 143 mUserHandle = UserHandle.of(UserHandle.myUserId()); 144 145 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { 146 // We aren't allowed to add an account. 147 Toast.makeText( 148 this, R.string.user_cannot_add_accounts_message, Toast.LENGTH_LONG) 149 .show(); 150 finish(); 151 return; 152 } 153 154 if (mAddAccountCalled) { 155 // We already called add account - maybe the callback was lost. 156 finish(); 157 return; 158 } 159 addAccount(getIntent().getStringExtra(EXTRA_SELECTED_ACCOUNT)); 160 } 161 162 @Override onActivityResult(int requestCode, int resultCode, Intent data)163 public void onActivityResult(int requestCode, int resultCode, Intent data) { 164 setResult(resultCode); 165 if (mPendingIntent != null) { 166 mPendingIntent.cancel(); 167 mPendingIntent = null; 168 } 169 finish(); 170 } 171 addAccount(String accountType)172 private void addAccount(String accountType) { 173 Bundle addAccountOptions = new Bundle(); 174 addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS, hasMultipleUsers(this)); 175 addAccountOptions.putBoolean(ALLOW_SKIP, true); 176 177 /* 178 * The identityIntent is for the purposes of establishing the identity 179 * of the caller and isn't intended for launching activities, services 180 * or broadcasts. 181 */ 182 Intent identityIntent = new Intent(); 183 identityIntent.setComponent(new ComponentName(SHOULD_NOT_RESOLVE, SHOULD_NOT_RESOLVE)); 184 identityIntent.setAction(SHOULD_NOT_RESOLVE); 185 identityIntent.addCategory(SHOULD_NOT_RESOLVE); 186 187 mPendingIntent = 188 PendingIntent.getBroadcast(this, 0, identityIntent, PendingIntent.FLAG_IMMUTABLE); 189 addAccountOptions.putParcelable(KEY_CALLER_IDENTITY, mPendingIntent); 190 191 AccountManager.get(this).addAccountAsUser( 192 accountType, 193 /* authTokenType= */ null, 194 /* requiredFeatures= */ null, 195 addAccountOptions, 196 null, 197 mCallback, 198 /* handler= */ null, 199 mUserHandle); 200 mAddAccountCalled = true; 201 } 202 hasMultipleUsers(Context context)203 private boolean hasMultipleUsers(Context context) { 204 return ((UserManager) context.getSystemService(Context.USER_SERVICE)) 205 .getUsers().size() > 1; 206 } 207 } 208