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             try {
86                 Bundle result = future.getResult();
87 
88                 Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
89                 Bundle addAccountOptions = new Bundle();
90                 addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS,
91                         hasMultipleUsers(AddAccountActivity.this));
92                 addAccountOptions.putParcelable(EXTRA_USER, mUserHandle);
93                 intent.putExtras(addAccountOptions);
94                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
95                 startActivityForResultAsUser(
96                         intent, ADD_ACCOUNT_REQUEST, mUserHandle);
97                 LOG.v("account added: " + result);
98             } catch (OperationCanceledException | IOException | AuthenticatorException e) {
99                 LOG.v("addAccount error: " + e);
100             } finally {
101                 finish();
102             }
103         }
104     };
105 
106     /**
107      * Creates an intent to start the {@link AddAccountActivity} to add an account of the given
108      * account type.
109      */
createAddAccountActivityIntent(Context context, String accountType)110     public static Intent createAddAccountActivityIntent(Context context, String accountType) {
111         Intent intent = new Intent(context, AddAccountActivity.class);
112         intent.putExtra(EXTRA_SELECTED_ACCOUNT, accountType);
113         return intent;
114     }
115 
116     @Override
onSaveInstanceState(Bundle outState)117     protected void onSaveInstanceState(Bundle outState) {
118         super.onSaveInstanceState(outState);
119         outState.putBoolean(KEY_ADD_CALLED, mAddAccountCalled);
120         LOG.v("saved");
121     }
122 
123     @Override
onCreate(Bundle savedInstanceState)124     public void onCreate(Bundle savedInstanceState) {
125         super.onCreate(savedInstanceState);
126         if (savedInstanceState != null) {
127             mAddAccountCalled = savedInstanceState.getBoolean(KEY_ADD_CALLED);
128             LOG.v("Restored from previous add account call: " + mAddAccountCalled);
129         }
130 
131         mUserManager = UserManager.get(getApplicationContext());
132         mUserHandle = UserHandle.of(UserHandle.myUserId());
133 
134         if (mUserManager.hasUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
135             // We aren't allowed to add an account.
136             Toast.makeText(
137                     this, R.string.user_cannot_add_accounts_message, Toast.LENGTH_LONG)
138                     .show();
139             finish();
140             return;
141         }
142 
143         if (mAddAccountCalled) {
144             // We already called add account - maybe the callback was lost.
145             finish();
146             return;
147         }
148         addAccount(getIntent().getStringExtra(EXTRA_SELECTED_ACCOUNT));
149     }
150 
151     @Override
onActivityResult(int requestCode, int resultCode, Intent data)152     public void onActivityResult(int requestCode, int resultCode, Intent data) {
153         setResult(resultCode);
154         if (mPendingIntent != null) {
155             mPendingIntent.cancel();
156             mPendingIntent = null;
157         }
158         finish();
159     }
160 
addAccount(String accountType)161     private void addAccount(String accountType) {
162         Bundle addAccountOptions = new Bundle();
163         addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS, hasMultipleUsers(this));
164         addAccountOptions.putBoolean(ALLOW_SKIP, true);
165 
166         /*
167          * The identityIntent is for the purposes of establishing the identity
168          * of the caller and isn't intended for launching activities, services
169          * or broadcasts.
170          */
171         Intent identityIntent = new Intent();
172         identityIntent.setComponent(new ComponentName(SHOULD_NOT_RESOLVE, SHOULD_NOT_RESOLVE));
173         identityIntent.setAction(SHOULD_NOT_RESOLVE);
174         identityIntent.addCategory(SHOULD_NOT_RESOLVE);
175 
176         mPendingIntent = PendingIntent.getBroadcast(this, 0, identityIntent, 0);
177         addAccountOptions.putParcelable(KEY_CALLER_IDENTITY, mPendingIntent);
178 
179         AccountManager.get(this).addAccountAsUser(
180                 accountType,
181                 /* authTokenType= */ null,
182                 /* requiredFeatures= */ null,
183                 addAccountOptions,
184                 null,
185                 mCallback,
186                 /* handler= */ null,
187                 mUserHandle);
188         mAddAccountCalled = true;
189     }
190 
hasMultipleUsers(Context context)191     private boolean hasMultipleUsers(Context context) {
192         return ((UserManager) context.getSystemService(Context.USER_SERVICE))
193                 .getUsers().size() > 1;
194     }
195 }
196