• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.user.CarUserManagerHelper;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.os.Bundle;
32 import android.os.UserHandle;
33 import android.os.UserManager;
34 import android.widget.Toast;
35 
36 import com.android.car.settings.R;
37 import com.android.car.settings.common.Logger;
38 
39 import java.io.IOException;
40 /**
41  *
42  * Entry point Activity for account setup. Works as follows
43  *
44  * <ol>
45  *   <li> After receiving an account type from ChooseAccountFragment, this Activity launches the
46  *        account setup specified by AccountManager.
47  *   <li> After the account setup, this Activity finishes without showing anything.
48  * </ol>
49  *
50  */
51 public class AddAccountActivity extends Activity {
52     /**
53      * A boolean to keep the state of whether add account has already been called.
54      */
55     private static final String KEY_ADD_CALLED = "AddAccountCalled";
56     /**
57      *
58      * Extra parameter to identify the caller. Applications may display a
59      * different UI if the call is made from Settings or from a specific
60      * application.
61      *
62      */
63     private static final String KEY_CALLER_IDENTITY = "pendingIntent";
64     private static final String SHOULD_NOT_RESOLVE = "SHOULDN'T RESOLVE!";
65 
66     private static final Logger LOG = new Logger(AddAccountActivity.class);
67     private static final String ALLOW_SKIP = "allowSkip";
68 
69     /* package */ static final String EXTRA_SELECTED_ACCOUNT = "selected_account";
70 
71     // Show additional info regarding the use of a device with multiple users
72     static final String EXTRA_HAS_MULTIPLE_USERS = "hasMultipleUsers";
73 
74     // Need a specific request code for add account activity.
75     public static final int ADD_ACCOUNT_REQUEST = 2001;
76 
77     private CarUserManagerHelper mCarUserManagerHelper;
78     private UserHandle mUserHandle;
79     private PendingIntent mPendingIntent;
80     private boolean mAddAccountCalled;
81 
hasMultipleUsers(Context context)82     public boolean hasMultipleUsers(Context context) {
83         return ((UserManager) context.getSystemService(Context.USER_SERVICE))
84                 .getUsers().size() > 1;
85     }
86 
87     private final AccountManagerCallback<Bundle> mCallback = new AccountManagerCallback<Bundle>() {
88         @Override
89         public void run(AccountManagerFuture<Bundle> future) {
90             if (!future.isDone()) {
91                 LOG.v("Account manager future is not done.");
92                 finish();
93             }
94             try {
95                 Bundle result = future.getResult();
96 
97                 Intent intent = (Intent) result.getParcelable(AccountManager.KEY_INTENT);
98                 Bundle addAccountOptions = new Bundle();
99                 addAccountOptions.putBoolean(EXTRA_HAS_MULTIPLE_USERS,
100                         hasMultipleUsers(AddAccountActivity.this));
101                 addAccountOptions.putParcelable(EXTRA_USER, mUserHandle);
102                 intent.putExtras(addAccountOptions);
103                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
104                 startActivityForResultAsUser(
105                         intent, ADD_ACCOUNT_REQUEST, mUserHandle);
106                 LOG.v("account added: " + result);
107             } catch (OperationCanceledException | IOException | AuthenticatorException e) {
108                 LOG.v("addAccount error: " + e);
109             } finally {
110                 finish();
111             }
112         }
113     };
114 
115     @Override
onSaveInstanceState(Bundle outState)116     protected void onSaveInstanceState(Bundle outState) {
117         super.onSaveInstanceState(outState);
118         outState.putBoolean(KEY_ADD_CALLED, mAddAccountCalled);
119         LOG.v("saved");
120     }
121 
122     @Override
onCreate(Bundle savedInstanceState)123     public void onCreate(Bundle savedInstanceState) {
124         super.onCreate(savedInstanceState);
125         if (savedInstanceState != null) {
126             mAddAccountCalled = savedInstanceState.getBoolean(KEY_ADD_CALLED);
127             LOG.v("Restored from previous add account call: " + mAddAccountCalled);
128         }
129 
130         mCarUserManagerHelper = new CarUserManagerHelper(this);
131 
132         if (mAddAccountCalled) {
133             // We already called add account - maybe the callback was lost.
134             finish();
135             return;
136         }
137 
138         mUserHandle = mCarUserManagerHelper.getCurrentProcessUserInfo().getUserHandle();
139         if (mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
140                 UserManager.DISALLOW_MODIFY_ACCOUNTS)) {
141             // We aren't allowed to add an account.
142             Toast.makeText(
143                     this, R.string.user_cannot_add_accounts_message, Toast.LENGTH_LONG)
144                     .show();
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 }
191