blob: d22e268dad5bcec3862e5d051fafdd9324a4f899 [file] [log] [blame]
Fred Quintana60307342009-03-24 22:48:12 -07001/*
2 * Copyright (C) 2009 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
17package android.accounts;
18
Sandra Kwan390c9d22016-01-12 14:13:37 -080019import static android.Manifest.permission.GET_ACCOUNTS;
20
Dmitry Dementyev52745472016-12-02 10:27:45 -080021import android.annotation.IntDef;
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -070022import android.annotation.NonNull;
Tor Norbye80b530a2015-04-23 16:36:09 -070023import android.annotation.RequiresPermission;
Jeff Sharkey32ee8ee2017-03-08 20:17:51 -070024import android.annotation.SdkConstant;
Tor Norbye80b530a2015-04-23 16:36:09 -070025import android.annotation.Size;
Sandra Kwana578d112015-12-16 16:01:43 -080026import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060027import android.annotation.SystemService;
Jeff Sharkey32ee8ee2017-03-08 20:17:51 -070028import android.annotation.SdkConstant.SdkConstantType;
29import android.annotation.BroadcastBehavior;
Fred Quintana60307342009-03-24 22:48:12 -070030import android.app.Activity;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080031import android.content.BroadcastReceiver;
Amith Yamasani12b8e132013-03-14 10:48:07 -070032import android.content.ComponentName;
Fred Quintana60307342009-03-24 22:48:12 -070033import android.content.Context;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080034import android.content.Intent;
Fred Quintanad9d2f112009-04-23 13:36:27 -070035import android.content.IntentFilter;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070036import android.content.IntentSender;
Amith Yamasani12b8e132013-03-14 10:48:07 -070037import android.content.res.Resources;
Dmitry Dementyev52745472016-12-02 10:27:45 -080038import android.content.pm.ApplicationInfo;
39import android.content.pm.PackageManager;
Costin Manolacheb6437242009-09-10 16:14:12 -070040import android.database.SQLException;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080041import android.os.Build;
Fred Quintanaa698f422009-04-08 19:14:54 -070042import android.os.Bundle;
43import android.os.Handler;
44import android.os.Looper;
Fred Quintana33269202009-04-20 16:05:10 -070045import android.os.Parcelable;
Amith Yamasani2c7bc262012-11-05 16:46:02 -080046import android.os.Process;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080047import android.os.RemoteException;
Amith Yamasani2c7bc262012-11-05 16:46:02 -080048import android.os.UserHandle;
Fred Quintanaf0fd8432010-03-08 12:48:05 -080049import android.text.TextUtils;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080050import android.util.Log;
51
52import com.android.internal.R;
53import com.google.android.collect.Maps;
Fred Quintana60307342009-03-24 22:48:12 -070054
Fred Quintanaa698f422009-04-08 19:14:54 -070055import java.io.IOException;
Dmitry Dementyev52745472016-12-02 10:27:45 -080056import java.lang.annotation.Retention;
57import java.lang.annotation.RetentionPolicy;
58import java.lang.SuppressWarnings;
Fred Quintana1121bb52011-09-14 23:19:35 -070059import java.util.ArrayList;
Dmitry Dementyev01985ff2017-01-19 16:03:39 -080060import java.util.Arrays;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080061import java.util.HashMap;
Dmitry Dementyev01985ff2017-01-19 16:03:39 -080062import java.util.HashSet;
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -070063import java.util.List;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080064import java.util.Map;
Dmitry Dementyev01985ff2017-01-19 16:03:39 -080065import java.util.Set;
Fred Quintanaa698f422009-04-08 19:14:54 -070066import java.util.concurrent.Callable;
67import java.util.concurrent.CancellationException;
68import java.util.concurrent.ExecutionException;
69import java.util.concurrent.FutureTask;
Fred Quintanaa698f422009-04-08 19:14:54 -070070import java.util.concurrent.TimeUnit;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080071import java.util.concurrent.TimeoutException;
Fred Quintana60307342009-03-24 22:48:12 -070072
73/**
Dan Egnor661f0132010-02-19 11:23:00 -080074 * This class provides access to a centralized registry of the user's
Dan Egnor8e4378b2010-08-02 18:22:09 -070075 * online accounts. The user enters credentials (username and password) once
76 * per account, granting applications access to online resources with
77 * "one-click" approval.
Fred Quintana60307342009-03-24 22:48:12 -070078 *
Dan Egnor661f0132010-02-19 11:23:00 -080079 * <p>Different online services have different ways of handling accounts and
80 * authentication, so the account manager uses pluggable <em>authenticator</em>
Dan Egnor8e4378b2010-08-02 18:22:09 -070081 * modules for different <em>account types</em>. Authenticators (which may be
82 * written by third parties) handle the actual details of validating account
83 * credentials and storing account information. For example, Google, Facebook,
84 * and Microsoft Exchange each have their own authenticator.
Dan Egnor661f0132010-02-19 11:23:00 -080085 *
86 * <p>Many servers support some notion of an <em>authentication token</em>,
87 * which can be used to authenticate a request to the server without sending
88 * the user's actual password. (Auth tokens are normally created with a
89 * separate request which does include the user's credentials.) AccountManager
Dan Egnor8e4378b2010-08-02 18:22:09 -070090 * can generate auth tokens for applications, so the application doesn't need to
91 * handle passwords directly. Auth tokens are normally reusable and cached by
92 * AccountManager, but must be refreshed periodically. It's the responsibility
93 * of applications to <em>invalidate</em> auth tokens when they stop working so
94 * the AccountManager knows it needs to regenerate them.
Dan Egnor661f0132010-02-19 11:23:00 -080095 *
96 * <p>Applications accessing a server normally go through these steps:
97 *
98 * <ul>
99 * <li>Get an instance of AccountManager using {@link #get(Context)}.
100 *
101 * <li>List the available accounts using {@link #getAccountsByType} or
102 * {@link #getAccountsByTypeAndFeatures}. Normally applications will only
103 * be interested in accounts with one particular <em>type</em>, which
104 * identifies the authenticator. Account <em>features</em> are used to
105 * identify particular account subtypes and capabilities. Both the account
106 * type and features are authenticator-specific strings, and must be known by
107 * the application in coordination with its preferred authenticators.
108 *
109 * <li>Select one or more of the available accounts, possibly by asking the
110 * user for their preference. If no suitable accounts are available,
111 * {@link #addAccount} may be called to prompt the user to create an
112 * account of the appropriate type.
113 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700114 * <li><b>Important:</b> If the application is using a previously remembered
115 * account selection, it must make sure the account is still in the list
116 * of accounts returned by {@link #getAccountsByType}. Requesting an auth token
117 * for an account no longer on the device results in an undefined failure.
118 *
Dan Egnor661f0132010-02-19 11:23:00 -0800119 * <li>Request an auth token for the selected account(s) using one of the
120 * {@link #getAuthToken} methods or related helpers. Refer to the description
121 * of each method for exact usage and error handling details.
122 *
123 * <li>Make the request using the auth token. The form of the auth token,
124 * the format of the request, and the protocol used are all specific to the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700125 * service you are accessing. The application may use whatever network and
126 * protocol libraries are useful.
Dan Egnor661f0132010-02-19 11:23:00 -0800127 *
128 * <li><b>Important:</b> If the request fails with an authentication error,
129 * it could be that a cached auth token is stale and no longer honored by
130 * the server. The application must call {@link #invalidateAuthToken} to remove
131 * the token from the cache, otherwise requests will continue failing! After
132 * invalidating the auth token, immediately go back to the "Request an auth
133 * token" step above. If the process fails the second time, then it can be
134 * treated as a "genuine" authentication failure and the user notified or other
135 * appropriate actions taken.
136 * </ul>
137 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700138 * <p>Some AccountManager methods may need to interact with the user to
Dan Egnor661f0132010-02-19 11:23:00 -0800139 * prompt for credentials, present options, or ask the user to add an account.
140 * The caller may choose whether to allow AccountManager to directly launch the
141 * necessary user interface and wait for the user, or to return an Intent which
142 * the caller may use to launch the interface, or (in some cases) to install a
143 * notification which the user can select at any time to launch the interface.
144 * To have AccountManager launch the interface directly, the caller must supply
145 * the current foreground {@link Activity} context.
146 *
147 * <p>Many AccountManager methods take {@link AccountManagerCallback} and
Dan Egnor8e4378b2010-08-02 18:22:09 -0700148 * {@link Handler} as parameters. These methods return immediately and
Dan Egnor661f0132010-02-19 11:23:00 -0800149 * run asynchronously. If a callback is provided then
150 * {@link AccountManagerCallback#run} will be invoked on the Handler's
151 * thread when the request completes, successfully or not.
Dan Egnor8e4378b2010-08-02 18:22:09 -0700152 * The result is retrieved by calling {@link AccountManagerFuture#getResult()}
153 * on the {@link AccountManagerFuture} returned by the method (and also passed
154 * to the callback). This method waits for the operation to complete (if
155 * necessary) and either returns the result or throws an exception if an error
156 * occurred during the operation. To make the request synchronously, call
Dan Egnor661f0132010-02-19 11:23:00 -0800157 * {@link AccountManagerFuture#getResult()} immediately on receiving the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700158 * future from the method; no callback need be supplied.
Dan Egnor661f0132010-02-19 11:23:00 -0800159 *
160 * <p>Requests which may block, including
161 * {@link AccountManagerFuture#getResult()}, must never be called on
162 * the application's main event thread. These operations throw
163 * {@link IllegalStateException} if they are used on the main thread.
Fred Quintana60307342009-03-24 22:48:12 -0700164 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600165@SystemService(Context.ACCOUNT_SERVICE)
Fred Quintana60307342009-03-24 22:48:12 -0700166public class AccountManager {
Dmitry Dementyev52745472016-12-02 10:27:45 -0800167
Fred Quintana60307342009-03-24 22:48:12 -0700168 private static final String TAG = "AccountManager";
169
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700170 public static final int ERROR_CODE_REMOTE_EXCEPTION = 1;
171 public static final int ERROR_CODE_NETWORK_ERROR = 3;
172 public static final int ERROR_CODE_CANCELED = 4;
173 public static final int ERROR_CODE_INVALID_RESPONSE = 5;
174 public static final int ERROR_CODE_UNSUPPORTED_OPERATION = 6;
175 public static final int ERROR_CODE_BAD_ARGUMENTS = 7;
176 public static final int ERROR_CODE_BAD_REQUEST = 8;
Jatin Lodhia3df7d692013-03-27 10:57:23 -0700177 public static final int ERROR_CODE_BAD_AUTHENTICATION = 9;
Fred Quintana756b7352009-10-21 13:43:10 -0700178
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800179 /** @hide */
180 public static final int ERROR_CODE_USER_RESTRICTED = 100;
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100181 /** @hide */
182 public static final int ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE = 101;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800183
Dan Egnor661f0132010-02-19 11:23:00 -0800184 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700185 * Bundle key used for the {@link String} account name in results
Dan Egnor661f0132010-02-19 11:23:00 -0800186 * from methods which return information about a particular account.
187 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700188 public static final String KEY_ACCOUNT_NAME = "authAccount";
Dan Egnor661f0132010-02-19 11:23:00 -0800189
190 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700191 * Bundle key used for the {@link String} account type in results
Dan Egnor661f0132010-02-19 11:23:00 -0800192 * from methods which return information about a particular account.
193 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700194 public static final String KEY_ACCOUNT_TYPE = "accountType";
Dan Egnor661f0132010-02-19 11:23:00 -0800195
196 /**
Svet Ganovd4696122016-09-24 13:57:45 -0700197 * Bundle key used for the account access id used for noting the
198 * account was accessed when unmarshaled from a parcel.
Svet Ganovf6d424f12016-09-20 20:18:53 -0700199 *
200 * @hide
201 */
Svet Ganovd4696122016-09-24 13:57:45 -0700202 public static final String KEY_ACCOUNT_ACCESS_ID = "accountAccessId";
Svet Ganovf6d424f12016-09-20 20:18:53 -0700203
204 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700205 * Bundle key used for the auth token value in results
Dan Egnor661f0132010-02-19 11:23:00 -0800206 * from {@link #getAuthToken} and friends.
207 */
208 public static final String KEY_AUTHTOKEN = "authtoken";
209
210 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700211 * Bundle key used for an {@link Intent} in results from methods that
Dan Egnor661f0132010-02-19 11:23:00 -0800212 * may require the caller to interact with the user. The Intent can
213 * be used to start the corresponding user interface activity.
214 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700215 public static final String KEY_INTENT = "intent";
Dan Egnor661f0132010-02-19 11:23:00 -0800216
217 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700218 * Bundle key used to supply the password directly in options to
Dan Egnor661f0132010-02-19 11:23:00 -0800219 * {@link #confirmCredentials}, rather than prompting the user with
220 * the standard password prompt.
221 */
222 public static final String KEY_PASSWORD = "password";
223
224 public static final String KEY_ACCOUNTS = "accounts";
Brian Carlstrom46703b02011-04-06 15:41:29 -0700225
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700226 public static final String KEY_ACCOUNT_AUTHENTICATOR_RESPONSE = "accountAuthenticatorResponse";
227 public static final String KEY_ACCOUNT_MANAGER_RESPONSE = "accountManagerResponse";
Dan Egnor661f0132010-02-19 11:23:00 -0800228 public static final String KEY_AUTHENTICATOR_TYPES = "authenticator_types";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700229 public static final String KEY_AUTH_FAILED_MESSAGE = "authFailedMessage";
230 public static final String KEY_AUTH_TOKEN_LABEL = "authTokenLabelKey";
Dan Egnor661f0132010-02-19 11:23:00 -0800231 public static final String KEY_BOOLEAN_RESULT = "booleanResult";
232 public static final String KEY_ERROR_CODE = "errorCode";
233 public static final String KEY_ERROR_MESSAGE = "errorMessage";
234 public static final String KEY_USERDATA = "userdata";
Costin Manolacheb61e8fb2011-09-08 11:26:09 -0700235
Costin Manolachea40c6302010-12-13 14:50:45 -0800236 /**
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800237 * Bundle key used to supply the last time the credentials of the account
238 * were authenticated successfully. Time is specified in milliseconds since
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700239 * epoch. Associated time is updated on successful authentication of account
240 * on adding account, confirming credentials, or updating credentials.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800241 */
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -0700242 public static final String KEY_LAST_AUTHENTICATED_TIME = "lastAuthenticatedTime";
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800243
244 /**
Costin Manolachea40c6302010-12-13 14:50:45 -0800245 * Authenticators using 'customTokens' option will also get the UID of the
246 * caller
247 */
248 public static final String KEY_CALLER_UID = "callerUid";
249 public static final String KEY_CALLER_PID = "callerPid";
Dan Egnor661f0132010-02-19 11:23:00 -0800250
Costin Manolached6060452011-01-24 16:11:36 -0800251 /**
Fred Quintanaad93a322011-09-08 13:21:01 -0700252 * The Android package of the caller will be set in the options bundle by the
253 * {@link AccountManager} and will be passed to the AccountManagerService and
254 * to the AccountAuthenticators. The uid of the caller will be known by the
255 * AccountManagerService as well as the AccountAuthenticators so they will be able to
256 * verify that the package is consistent with the uid (a uid might be shared by many
257 * packages).
258 */
259 public static final String KEY_ANDROID_PACKAGE_NAME = "androidPackageName";
260
261 /**
Costin Manolached6060452011-01-24 16:11:36 -0800262 * Boolean, if set and 'customTokens' the authenticator is responsible for
263 * notifications.
264 * @hide
265 */
266 public static final String KEY_NOTIFY_ON_FAILURE = "notifyOnAuthFailure";
267
Sandra Kwan78812282015-11-04 11:19:47 -0800268 /**
269 * Bundle key used for a {@link Bundle} in result from
270 * {@link #startAddAccountSession} and friends which returns session data
271 * for installing an account later.
272 */
273 public static final String KEY_ACCOUNT_SESSION_BUNDLE = "accountSessionBundle";
274
275 /**
276 * Bundle key used for the {@link String} account status token in result
277 * from {@link #startAddAccountSession} and friends which returns
278 * information about a particular account.
279 */
280 public static final String KEY_ACCOUNT_STATUS_TOKEN = "accountStatusToken";
281
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700282 public static final String ACTION_AUTHENTICATOR_INTENT =
283 "android.accounts.AccountAuthenticator";
284 public static final String AUTHENTICATOR_META_DATA_NAME =
Dan Egnor661f0132010-02-19 11:23:00 -0800285 "android.accounts.AccountAuthenticator";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700286 public static final String AUTHENTICATOR_ATTRIBUTES_NAME = "account-authenticator";
287
Dmitry Dementyev52745472016-12-02 10:27:45 -0800288 /** @hide */
289 @Retention(RetentionPolicy.SOURCE)
290 @IntDef({VISIBILITY_UNDEFINED, VISIBILITY_VISIBLE, VISIBILITY_USER_MANAGED_VISIBLE,
291 VISIBILITY_NOT_VISIBLE, VISIBILITY_USER_MANAGED_NOT_VISIBLE})
292 public @interface AccountVisibility {
293 }
294
295 /**
Dmitry Dementyev06f32e02017-02-16 17:47:48 -0800296 * Account visibility was not set. Default visibility value will be used.
297 * See {@link #PACKAGE_NAME_KEY_LEGACY_VISIBLE}, {@link #PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE}
Dmitry Dementyev52745472016-12-02 10:27:45 -0800298 */
299 public static final int VISIBILITY_UNDEFINED = 0;
300
301 /**
302 * Account is always visible to given application and only authenticator can revoke visibility.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800303 */
304 public static final int VISIBILITY_VISIBLE = 1;
305
306 /**
307 * Account is visible to given application, but user can revoke visibility.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800308 */
309 public static final int VISIBILITY_USER_MANAGED_VISIBLE = 2;
310
311 /**
312 * Account is not visible to given application and only authenticator can grant visibility.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800313 */
314 public static final int VISIBILITY_NOT_VISIBLE = 3;
315
316 /**
317 * Account is not visible to given application, but user can reveal it, for example, using
318 * {@link #newChooseAccountIntent(Account, List, String[], String, String, String[], Bundle)}
Dmitry Dementyev52745472016-12-02 10:27:45 -0800319 */
320 public static final int VISIBILITY_USER_MANAGED_NOT_VISIBLE = 4;
321
322 /**
Svet Ganovf6d424f12016-09-20 20:18:53 -0700323 * Token type for the special case where a UID has access only to an account
324 * but no authenticator specific auth token types.
Dmitry Dementyev2e22cfb2017-01-09 18:42:14 +0000325 *
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700326 * @hide
327 */
Svet Ganovf6d424f12016-09-20 20:18:53 -0700328 public static final String ACCOUNT_ACCESS_TOKEN_TYPE =
329 "com.android.AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE";
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700330
Fred Quintana60307342009-03-24 22:48:12 -0700331 private final Context mContext;
332 private final IAccountManager mService;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700333 private final Handler mMainHandler;
Dan Egnor661f0132010-02-19 11:23:00 -0800334
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700335 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800336 * Action sent as a broadcast Intent by the AccountsService when accounts are added, accounts
337 * are removed, or an account's credentials (saved password, etc) are changed.
Dan Egnor661f0132010-02-19 11:23:00 -0800338 *
339 * @see #addOnAccountsUpdatedListener
Dmitry Dementyeva461e302017-04-12 11:00:48 -0700340 * @see #ACTION_ACCOUNT_REMOVED
Dmitry Dementyev52745472016-12-02 10:27:45 -0800341 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700342 * @deprecated use {@link #addOnAccountsUpdatedListener} to get account updates in runtime.
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700343 */
Jeff Sharkey32ee8ee2017-03-08 20:17:51 -0700344 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
345 @BroadcastBehavior(includeBackground = true)
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700346 public static final String LOGIN_ACCOUNTS_CHANGED_ACTION =
347 "android.accounts.LOGIN_ACCOUNTS_CHANGED";
Fred Quintana60307342009-03-24 22:48:12 -0700348
Fred Quintana33269202009-04-20 16:05:10 -0700349 /**
Dmitry Dementyevb6a7dc02017-04-18 13:43:31 -0700350 * Action sent as a broadcast Intent by the AccountsService when any account is removed
351 * or renamed. Only applications which were able to see the account will receive the intent.
352 * Intent extra will include the following fields:
353 * <ul>
354 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the removed account
355 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
356 * </ul>
Dmitry Dementyeva461e302017-04-12 11:00:48 -0700357 */
358 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
359 @BroadcastBehavior(includeBackground = true)
360 public static final String ACTION_ACCOUNT_REMOVED =
361 "android.accounts.action.ACCOUNT_REMOVED";
362
363 /**
Dmitry Dementyev8882d882017-03-14 17:25:46 -0700364 * Action sent as a broadcast Intent to specific package by the AccountsService
365 * when account visibility or account's credentials (saved password, etc) are changed.
366 *
367 * @see #addOnAccountsUpdatedListener
368 *
369 * @hide
370 */
371 public static final String ACTION_VISIBLE_ACCOUNTS_CHANGED =
372 "android.accounts.action.VISIBLE_ACCOUNTS_CHANGED";
373
374 /**
Dmitry Dementyevd6f06722017-04-05 12:43:26 -0700375 * Key to set visibility for applications which satisfy one of the following conditions:
376 * <ul>
377 * <li>Target API level below {@link android.os.Build.VERSION_CODES#O} and have
378 * deprecated {@link android.Manifest.permission#GET_ACCOUNTS} permission.
379 * </li>
380 * <li> Have {@link android.Manifest.permission#GET_ACCOUNTS_PRIVILEGED} permission. </li>
381 * <li> Have the same signature as authenticator. </li>
382 * <li> Have {@link android.Manifest.permission#READ_CONTACTS} permission and
383 * account type may be associated with contacts data - (verified by
384 * {@link android.Manifest.permission#WRITE_CONTACTS} permission check for the authenticator).
385 * </li>
386 * </ul>
387 * See {@link #getAccountVisibility}. If the value was not set by authenticator
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800388 * {@link #VISIBILITY_USER_MANAGED_VISIBLE} is used.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800389 */
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700390 public static final String PACKAGE_NAME_KEY_LEGACY_VISIBLE =
391 "android:accounts:key_legacy_visible";
392
393 /**
394 * Key to set default visibility for applications which don't satisfy conditions in
395 * {@link PACKAGE_NAME_KEY_LEGACY_VISIBLE}. If the value was not set by authenticator
396 * {@link #VISIBILITY_USER_MANAGED_NOT_VISIBLE} is used.
397 */
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800398 public static final String PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE =
Dmitry Dementyevcbe1bd12017-04-25 17:02:47 -0700399 "android:accounts:key_legacy_not_visible";
Dmitry Dementyev52745472016-12-02 10:27:45 -0800400
401 /**
Fred Quintana33269202009-04-20 16:05:10 -0700402 * @hide
403 */
Fred Quintana60307342009-03-24 22:48:12 -0700404 public AccountManager(Context context, IAccountManager service) {
405 mContext = context;
406 mService = service;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700407 mMainHandler = new Handler(mContext.getMainLooper());
Fred Quintana60307342009-03-24 22:48:12 -0700408 }
409
Fred Quintana0eabf022009-04-27 15:08:17 -0700410 /**
411 * @hide used for testing only
412 */
413 public AccountManager(Context context, IAccountManager service, Handler handler) {
414 mContext = context;
415 mService = service;
416 mMainHandler = handler;
417 }
418
Fred Quintana756b7352009-10-21 13:43:10 -0700419 /**
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800420 * @hide for internal use only
421 */
422 public static Bundle sanitizeResult(Bundle result) {
Fred Quintana382601f2010-03-25 12:25:10 -0700423 if (result != null) {
424 if (result.containsKey(KEY_AUTHTOKEN)
425 && !TextUtils.isEmpty(result.getString(KEY_AUTHTOKEN))) {
426 final Bundle newResult = new Bundle(result);
427 newResult.putString(KEY_AUTHTOKEN, "<omitted for logging purposes>");
428 return newResult;
429 }
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800430 }
431 return result;
432 }
433
434 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800435 * Gets an AccountManager instance associated with a Context.
436 * The {@link Context} will be used as long as the AccountManager is
437 * active, so make sure to use a {@link Context} whose lifetime is
438 * commensurate with any listeners registered to
439 * {@link #addOnAccountsUpdatedListener} or similar methods.
440 *
441 * <p>It is safe to call this method from the main thread.
442 *
443 * <p>No permission is required to call this method.
444 *
Fred Quintana756b7352009-10-21 13:43:10 -0700445 * @param context The {@link Context} to use when necessary
Dan Egnor661f0132010-02-19 11:23:00 -0800446 * @return An {@link AccountManager} instance
Fred Quintana756b7352009-10-21 13:43:10 -0700447 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700448 public static AccountManager get(Context context) {
Fred Quintana382601f2010-03-25 12:25:10 -0700449 if (context == null) throw new IllegalArgumentException("context is null");
Fred Quintanaa698f422009-04-08 19:14:54 -0700450 return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
451 }
452
Fred Quintana756b7352009-10-21 13:43:10 -0700453 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800454 * Gets the saved password associated with the account. This is intended for authenticators and
455 * related code; applications should get an auth token instead.
Dan Egnor661f0132010-02-19 11:23:00 -0800456 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800457 * <p>
458 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800459 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800460 * <p>
461 * This method requires the caller to have a signature match with the authenticator that owns
462 * the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800463 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800464 * <p>
465 * <b>NOTE:</b> If targeting your app to work on API level
466 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before, AUTHENTICATE_ACCOUNTS
467 * permission is needed for those platforms. See docs for this function in API level
468 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700469 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700470 * @param account The account to query for a password. Must not be {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -0800471 * @return The account's password, null if none or if the account doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700472 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700473 public String getPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -0700474 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintana60307342009-03-24 22:48:12 -0700475 try {
476 return mService.getPassword(account);
477 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700478 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700479 }
480 }
481
Fred Quintana756b7352009-10-21 13:43:10 -0700482 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800483 * Gets the user data named by "key" associated with the account. This is intended for
484 * authenticators and related code to store arbitrary metadata along with accounts. The meaning
485 * of the keys and values is up to the authenticator for the account.
Dan Egnor661f0132010-02-19 11:23:00 -0800486 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800487 * <p>
488 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800489 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800490 * <p>
491 * This method requires the caller to have a signature match with the authenticator that owns
492 * the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800493 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800494 * <p>
495 * <b>NOTE:</b> If targeting your app to work on API level
496 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before, AUTHENTICATE_ACCOUNTS
497 * permission is needed for those platforms. See docs for this function in API level
498 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700499 *
Dan Egnor661f0132010-02-19 11:23:00 -0800500 * @param account The account to query for user data
501 * @return The user data, null if the account or key doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700502 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700503 public String getUserData(final Account account, final String key) {
Fred Quintana382601f2010-03-25 12:25:10 -0700504 if (account == null) throw new IllegalArgumentException("account is null");
505 if (key == null) throw new IllegalArgumentException("key is null");
Fred Quintana60307342009-03-24 22:48:12 -0700506 try {
507 return mService.getUserData(account, key);
508 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700509 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700510 }
511 }
512
Fred Quintana756b7352009-10-21 13:43:10 -0700513 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800514 * Lists the currently registered authenticators.
515 *
516 * <p>It is safe to call this method from the main thread.
517 *
518 * <p>No permission is required to call this method.
519 *
520 * @return An array of {@link AuthenticatorDescription} for every
521 * authenticator known to the AccountManager service. Empty (never
522 * null) if no authenticators are known.
Fred Quintana756b7352009-10-21 13:43:10 -0700523 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700524 public AuthenticatorDescription[] getAuthenticatorTypes() {
Fred Quintanaa698f422009-04-08 19:14:54 -0700525 try {
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100526 return mService.getAuthenticatorTypes(UserHandle.getCallingUserId());
527 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700528 throw e.rethrowFromSystemServer();
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100529 }
530 }
531
532 /**
533 * @hide
534 * Lists the currently registered authenticators for a given user id.
535 *
536 * <p>It is safe to call this method from the main thread.
537 *
538 * <p>The caller has to be in the same user or have the permission
539 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
540 *
541 * @return An array of {@link AuthenticatorDescription} for every
542 * authenticator known to the AccountManager service. Empty (never
543 * null) if no authenticators are known.
544 */
545 public AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) {
546 try {
547 return mService.getAuthenticatorTypes(userId);
Fred Quintanaa698f422009-04-08 19:14:54 -0700548 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700549 throw e.rethrowFromSystemServer();
Fred Quintanaa698f422009-04-08 19:14:54 -0700550 }
551 }
552
Fred Quintana756b7352009-10-21 13:43:10 -0700553 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800554 * Lists all accounts visible to the caller regardless of type. Equivalent to
555 * getAccountsByType(null). These accounts may be visible because the user granted access to the
556 * account, or the AbstractAcccountAuthenticator managing the account did so or because the
557 * client shares a signature with the managing AbstractAccountAuthenticator.
Dan Egnor661f0132010-02-19 11:23:00 -0800558 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800559 * <p>
560 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800561 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800562 * @return An array of {@link Account}, one for each account. Empty (never null) if no accounts
563 * have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700564 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700565 @NonNull
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700566 public Account[] getAccounts() {
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000567 try {
568 return mService.getAccounts(null, mContext.getOpPackageName());
569 } catch (RemoteException e) {
570 throw e.rethrowFromSystemServer();
571 }
Fred Quintana60307342009-03-24 22:48:12 -0700572 }
573
Fred Quintana756b7352009-10-21 13:43:10 -0700574 /**
Amith Yamasani27db4682013-03-30 17:07:47 -0700575 * @hide
Dmitry Dementyev52745472016-12-02 10:27:45 -0800576 * Lists all accounts visible to caller regardless of type for a given user id. Equivalent to
577 * getAccountsByType(null).
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100578 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800579 * <p>
580 * It is safe to call this method from the main thread.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100581 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800582 * @return An array of {@link Account}, one for each account. Empty (never null) if no accounts
583 * have been added.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100584 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700585 @NonNull
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100586 public Account[] getAccountsAsUser(int userId) {
587 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700588 return mService.getAccountsAsUser(null, userId, mContext.getOpPackageName());
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100589 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700590 throw e.rethrowFromSystemServer();
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100591 }
592 }
593
594 /**
595 * @hide
Amith Yamasani27db4682013-03-30 17:07:47 -0700596 * For use by internal activities. Returns the list of accounts that the calling package
597 * is authorized to use, particularly for shared accounts.
598 * @param packageName package name of the calling app.
599 * @param uid the uid of the calling app.
600 * @return the accounts that are available to this package and user.
601 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700602 @NonNull
Amith Yamasani27db4682013-03-30 17:07:47 -0700603 public Account[] getAccountsForPackage(String packageName, int uid) {
604 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700605 return mService.getAccountsForPackage(packageName, uid, mContext.getOpPackageName());
Amith Yamasani27db4682013-03-30 17:07:47 -0700606 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700607 throw re.rethrowFromSystemServer();
Amith Yamasani27db4682013-03-30 17:07:47 -0700608 }
609 }
610
611 /**
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700612 * Returns the accounts visible to the specified package in an environment where some apps are
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800613 * not authorized to view all accounts. This method can only be called by system apps and
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700614 * authenticators managing the type.
615 * Beginning API level {@link android.os.Build.VERSION_CODES#O} it also return accounts
616 * which user can make visible to the application (see {@link VISIBILITY_USER_MANAGED_VISIBLE}).
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800617 *
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700618 * @param type The type of accounts to return, null to retrieve all accounts
619 * @param packageName The package name of the app for which the accounts are to be returned
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800620 * @return An array of {@link Account}, one per matching account. Empty (never null) if no
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700621 * accounts of the specified type can be accessed by the package.
622 *
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700623 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700624 @NonNull
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700625 public Account[] getAccountsByTypeForPackage(String type, String packageName) {
626 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700627 return mService.getAccountsByTypeForPackage(type, packageName,
628 mContext.getOpPackageName());
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700629 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700630 throw re.rethrowFromSystemServer();
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700631 }
632 }
633
634 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800635 * Lists all accounts of particular type visible to the caller. These accounts may be visible
636 * because the user granted access to the account, or the AbstractAcccountAuthenticator managing
637 * the account did so or because the client shares a signature with the managing
638 * AbstractAccountAuthenticator.
Dan Egnor661f0132010-02-19 11:23:00 -0800639 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800640 * <p>
641 * The account type is a string token corresponding to the authenticator and useful domain of
642 * the account. For example, there are types corresponding to Google and Facebook. The exact
643 * string token to use will be published somewhere associated with the authenticator in
644 * question.
Dan Egnor661f0132010-02-19 11:23:00 -0800645 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800646 * <p>
647 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800648 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800649 * <p>
650 * Caller targeting API level {@link android.os.Build.VERSION_CODES#O} and above, will get list
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700651 * of accounts made visible to it by user
652 * (see {@link #newChooseAccountIntent(Account, List, String[], String,
653 * String, String[], Bundle)}) or AbstractAcccountAuthenticator
654 * using {@link setAccountVisibility}.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800655 * {@link android.Manifest.permission#GET_ACCOUNTS} permission is not used.
656 *
657 * <p>
658 * Caller targeting API level below {@link android.os.Build.VERSION_CODES#O} that have not been
659 * granted the {@link android.Manifest.permission#GET_ACCOUNTS} permission, will only see those
660 * accounts managed by AbstractAccountAuthenticators whose signature matches the client.
661 *
662 * <p>
663 * <b>NOTE:</b> If targeting your app to work on API level
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800664 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before,
665 * {@link android.Manifest.permission#GET_ACCOUNTS} permission is
Dmitry Dementyev52745472016-12-02 10:27:45 -0800666 * needed for those platforms, irrespective of uid or signature match. See docs for this
667 * function in API level {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000668 *
669 * @param type The type of accounts to return, null to retrieve all accounts
Dmitry Dementyev52745472016-12-02 10:27:45 -0800670 * @return An array of {@link Account}, one per matching account. Empty (never null) if no
671 * accounts of the specified type have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700672 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700673 @NonNull
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700674 public Account[] getAccountsByType(String type) {
Amith Yamasani2c7bc262012-11-05 16:46:02 -0800675 return getAccountsByTypeAsUser(type, Process.myUserHandle());
676 }
677
678 /** @hide Same as {@link #getAccountsByType(String)} but for a specific user. */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700679 @NonNull
Amith Yamasani2c7bc262012-11-05 16:46:02 -0800680 public Account[] getAccountsByTypeAsUser(String type, UserHandle userHandle) {
Fred Quintana60307342009-03-24 22:48:12 -0700681 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700682 return mService.getAccountsAsUser(type, userHandle.getIdentifier(),
683 mContext.getOpPackageName());
Fred Quintana60307342009-03-24 22:48:12 -0700684 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700685 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700686 }
687 }
688
Fred Quintana756b7352009-10-21 13:43:10 -0700689 /**
Fred Quintanad9640ec2012-05-23 12:37:00 -0700690 * Change whether or not an app (identified by its uid) is allowed to retrieve an authToken
691 * for an account.
692 * <p>
693 * This is only meant to be used by system activities and is not in the SDK.
694 * @param account The account whose permissions are being modified
695 * @param authTokenType The type of token whose permissions are being modified
696 * @param uid The uid that identifies the app which is being granted or revoked permission.
697 * @param value true is permission is being granted, false for revoked
698 * @hide
699 */
700 public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) {
701 try {
702 mService.updateAppPermission(account, authTokenType, uid, value);
703 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700704 throw e.rethrowFromSystemServer();
Fred Quintanad9640ec2012-05-23 12:37:00 -0700705 }
706 }
707
708 /**
709 * Get the user-friendly label associated with an authenticator's auth token.
710 * @param accountType the type of the authenticator. must not be null.
711 * @param authTokenType the token type. must not be null.
712 * @param callback callback to invoke when the result is available. may be null.
713 * @param handler the handler on which to invoke the callback, or null for the main thread
714 * @return a future containing the label string
715 * @hide
716 */
717 public AccountManagerFuture<String> getAuthTokenLabel(
718 final String accountType, final String authTokenType,
719 AccountManagerCallback<String> callback, Handler handler) {
720 if (accountType == null) throw new IllegalArgumentException("accountType is null");
721 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
722 return new Future2Task<String>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800723 @Override
Fred Quintanad9640ec2012-05-23 12:37:00 -0700724 public void doWork() throws RemoteException {
725 mService.getAuthTokenLabel(mResponse, accountType, authTokenType);
726 }
727
728 @Override
729 public String bundleToResult(Bundle bundle) throws AuthenticatorException {
730 if (!bundle.containsKey(KEY_AUTH_TOKEN_LABEL)) {
731 throw new AuthenticatorException("no result in response");
732 }
733 return bundle.getString(KEY_AUTH_TOKEN_LABEL);
734 }
735 }.start();
736 }
737
738 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800739 * Finds out whether a particular account has all the specified features. Account features are
740 * authenticator-specific string tokens identifying boolean account properties. For example,
741 * features are used to tell whether Google accounts have a particular service (such as Google
742 * Calendar or Google Talk) enabled. The feature names and their meanings are published
743 * somewhere associated with the authenticator in question.
Dan Egnor661f0132010-02-19 11:23:00 -0800744 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800745 * <p>
746 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
747 * not be used on the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800748 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800749 * <p>
750 * If caller target API level is below {@link android.os.Build.VERSION_CODES#O}, it is
751 * required to hold the permission {@link android.Manifest.permission#GET_ACCOUNTS} or have a
752 * signature match with the AbstractAccountAuthenticator that manages the account.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800753 *
754 * @param account The {@link Account} to test
Dan Egnor661f0132010-02-19 11:23:00 -0800755 * @param features An array of the account features to check
Dmitry Dementyev52745472016-12-02 10:27:45 -0800756 * @param callback Callback to invoke when the request completes, null for no callback
757 * @param handler {@link Handler} identifying the callback thread, null for the main thread
758 * @return An {@link AccountManagerFuture} which resolves to a Boolean, true if the account
759 * exists and has all of the specified features.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800760 */
Fred Quintana3084a6f2010-01-14 18:02:03 -0800761 public AccountManagerFuture<Boolean> hasFeatures(final Account account,
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800762 final String[] features,
763 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700764 if (account == null) throw new IllegalArgumentException("account is null");
765 if (features == null) throw new IllegalArgumentException("features is null");
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800766 return new Future2Task<Boolean>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800767 @Override
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800768 public void doWork() throws RemoteException {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700769 mService.hasFeatures(mResponse, account, features, mContext.getOpPackageName());
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800770 }
Sandra Kwan0b84b452016-01-20 15:25:42 -0800771 @Override
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800772 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
773 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
774 throw new AuthenticatorException("no result in response");
775 }
776 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
777 }
778 }.start();
779 }
780
781 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800782 * Lists all accounts of a type which have certain features. The account type identifies the
783 * authenticator (see {@link #getAccountsByType}). Account features are authenticator-specific
784 * string tokens identifying boolean account properties (see {@link #hasFeatures}).
Fred Quintana756b7352009-10-21 13:43:10 -0700785 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800786 * <p>
787 * Unlike {@link #getAccountsByType}, this method calls the authenticator, which may contact the
788 * server or do other work to check account features, so the method returns an
789 * {@link AccountManagerFuture}.
Fred Quintanaa698f422009-04-08 19:14:54 -0700790 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800791 * <p>
792 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
793 * not be used on the main thread.
Fred Quintana756b7352009-10-21 13:43:10 -0700794 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800795 * <p>
796 * Caller targeting API level {@link android.os.Build.VERSION_CODES#O} and above, will get list
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700797 * of accounts made visible to it by user
798 * (see {@link #newChooseAccountIntent(Account, List, String[], String,
799 * String, String[], Bundle)}) or AbstractAcccountAuthenticator
800 * using {@link setAccountVisibility}.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800801 * {@link android.Manifest.permission#GET_ACCOUNTS} permission is not used.
802 *
803 * <p>
804 * Caller targeting API level below {@link android.os.Build.VERSION_CODES#O} that have not been
805 * granted the {@link android.Manifest.permission#GET_ACCOUNTS} permission, will only see those
806 * accounts managed by AbstractAccountAuthenticators whose signature matches the client.
807 * <p>
808 * <b>NOTE:</b> If targeting your app to work on API level
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800809 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before,
810 * {@link android.Manifest.permission#GET_ACCOUNTS} permission is
Dmitry Dementyev52745472016-12-02 10:27:45 -0800811 * needed for those platforms, irrespective of uid or signature match. See docs for this
812 * function in API level {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
813 *
Fred Quintana756b7352009-10-21 13:43:10 -0700814 *
Dan Egnor661f0132010-02-19 11:23:00 -0800815 * @param type The type of accounts to return, must not be null
Dmitry Dementyev52745472016-12-02 10:27:45 -0800816 * @param features An array of the account features to require, may be null or empty *
817 * @param callback Callback to invoke when the request completes, null for no callback
818 * @param handler {@link Handler} identifying the callback thread, null for the main thread
819 * @return An {@link AccountManagerFuture} which resolves to an array of {@link Account}, one
820 * per account of the specified type which matches the requested features.
Fred Quintana8570f742010-02-18 10:32:54 -0800821 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700822 public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures(
823 final String type, final String[] features,
824 AccountManagerCallback<Account[]> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700825 if (type == null) throw new IllegalArgumentException("type is null");
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700826 return new Future2Task<Account[]>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800827 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700828 public void doWork() throws RemoteException {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700829 mService.getAccountsByFeatures(mResponse, type, features,
830 mContext.getOpPackageName());
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700831 }
Sandra Kwan0b84b452016-01-20 15:25:42 -0800832 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700833 public Account[] bundleToResult(Bundle bundle) throws AuthenticatorException {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700834 if (!bundle.containsKey(KEY_ACCOUNTS)) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700835 throw new AuthenticatorException("no result in response");
836 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700837 final Parcelable[] parcelables = bundle.getParcelableArray(KEY_ACCOUNTS);
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700838 Account[] descs = new Account[parcelables.length];
839 for (int i = 0; i < parcelables.length; i++) {
840 descs[i] = (Account) parcelables[i];
841 }
842 return descs;
843 }
844 }.start();
845 }
846
Fred Quintana756b7352009-10-21 13:43:10 -0700847 /**
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700848 * Adds an account directly to the AccountManager. Normally used by sign-up
Dan Egnor661f0132010-02-19 11:23:00 -0800849 * wizards associated with authenticators, not directly by applications.
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700850 * <p>Calling this method does not update the last authenticated timestamp,
851 * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
852 * {@link #notifyAccountAuthenticated(Account)} after getting success.
853 * However, if this method is called when it is triggered by addAccount() or
854 * addAccountAsUser() or similar functions, then there is no need to update
855 * timestamp manually as it is updated automatically by framework on
856 * successful completion of the mentioned functions.
Dan Egnor661f0132010-02-19 11:23:00 -0800857 * <p>It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700858 * <p>This method requires the caller to have a signature match with the
859 * authenticator that owns the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800860 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700861 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
862 * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs
863 * for this function in API level 22.
864 *
Dan Egnor661f0132010-02-19 11:23:00 -0800865 * @param account The {@link Account} to add
866 * @param password The password to associate with the account, null for none
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700867 * @param userdata String values to use for the account's userdata, null for
868 * none
Dan Egnor8e4378b2010-08-02 18:22:09 -0700869 * @return True if the account was successfully added, false if the account
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700870 * already exists, the account is null, or another error occurs.
Dan Egnor661f0132010-02-19 11:23:00 -0800871 */
872 public boolean addAccountExplicitly(Account account, String password, Bundle userdata) {
Fred Quintana382601f2010-03-25 12:25:10 -0700873 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800874 try {
Amith Yamasani27db4682013-03-30 17:07:47 -0700875 return mService.addAccountExplicitly(account, password, userdata);
Dan Egnor661f0132010-02-19 11:23:00 -0800876 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700877 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -0800878 }
879 }
880
881 /**
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700882 * Adds an account directly to the AccountManager. Additionally it specifies Account visibility
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800883 * for given list of packages.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800884 * <p>
885 * Normally used by sign-up wizards associated with authenticators, not directly by
886 * applications.
887 * <p>
888 * Calling this method does not update the last authenticated timestamp, referred by
889 * {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700890 * {@link #notifyAccountAuthenticated(Account)} after getting success.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800891 * <p>
892 * It is safe to call this method from the main thread.
893 * <p>
894 * This method requires the caller to have a signature match with the authenticator that owns
895 * the specified account.
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700896 *
897 * @param account The {@link Account} to add
898 * @param password The password to associate with the account, null for none
Dmitry Dementyev52745472016-12-02 10:27:45 -0800899 * @param extras String values to use for the account's userdata, null for none
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800900 * @param visibility Map from packageName to visibility values which will be set before account
901 * is added. See {@link #getAccountVisibility} for possible values.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800902 *
903 * @return True if the account was successfully added, false if the account already exists, the
904 * account is null, or another error occurs.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800905 */
906 public boolean addAccountExplicitly(Account account, String password, Bundle extras,
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800907 Map<String, Integer> visibility) {
Dmitry Dementyev52745472016-12-02 10:27:45 -0800908 if (account == null)
909 throw new IllegalArgumentException("account is null");
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700910 try {
Dmitry Dementyev52745472016-12-02 10:27:45 -0800911 return mService.addAccountExplicitlyWithVisibility(account, password, extras,
912 visibility);
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700913 } catch (RemoteException e) {
914 throw e.rethrowFromSystemServer();
915 }
916 }
917
918 /**
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800919 * Returns package names and visibility which were explicitly set for given account.
Dmitry Dementyev2ae64092017-01-09 12:48:18 -0800920 * <p>
921 * This method requires the caller to have a signature match with the authenticator that owns
922 * the specified account.
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700923 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700924 * @param account The account for which visibility data should be returned
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700925 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700926 * @return Map from package names to visibility for given account
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700927 */
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800928 public Map<String, Integer> getPackagesAndVisibilityForAccount(Account account) {
Dmitry Dementyev01985ff2017-01-19 16:03:39 -0800929 try {
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800930 if (account == null)
931 throw new IllegalArgumentException("account is null");
Dmitry Dementyev01985ff2017-01-19 16:03:39 -0800932 @SuppressWarnings("unchecked")
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800933 Map<String, Integer> result = (Map<String, Integer>) mService
934 .getPackagesAndVisibilityForAccount(account);
Dmitry Dementyev01985ff2017-01-19 16:03:39 -0800935 return result;
936 } catch (RemoteException re) {
937 throw re.rethrowFromSystemServer();
938 }
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700939 }
940
941 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800942 * Gets all accounts of given type and their visibility for specific package. This method
943 * requires the caller to have a signature match with the authenticator that manages
944 * accountType. It is a helper method which combines calls to {@link #getAccountsByType} by
945 * authenticator and {@link #getAccountVisibility} for every returned account.
946 *
947 * <p>
948 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700949 * @param packageName Package name
950 * @param accountType {@link Account} type
Dmitry Dementyev52745472016-12-02 10:27:45 -0800951 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700952 * @return Map with visibility for all accounts of given type
953 * See {@link #getAccountVisibility} for possible values
Dmitry Dementyev52745472016-12-02 10:27:45 -0800954 */
955 public Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName,
956 String accountType) {
957 try {
958 @SuppressWarnings("unchecked")
959 Map<Account, Integer> result = (Map<Account, Integer>) mService
960 .getAccountsAndVisibilityForPackage(packageName, accountType);
961 return result;
962 } catch (RemoteException re) {
963 throw re.rethrowFromSystemServer();
964 }
965 }
966
967 /**
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700968 * Set visibility value of given account to certain package.
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800969 * Package name must match installed application, or be equal to
970 * {@link #PACKAGE_NAME_KEY_LEGACY_VISIBLE} or {@link #PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE}.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800971 * <p>
Dmitry Dementyev06f32e02017-02-16 17:47:48 -0800972 * Possible visibility values:
973 * <ul>
974 * <li>{@link #VISIBILITY_UNDEFINED}</li>
975 * <li>{@link #VISIBILITY_VISIBLE}</li>
976 * <li>{@link #VISIBILITY_USER_MANAGED_VISIBLE}</li>
977 * <li>{@link #VISIBILITY_NOT_VISIBLE}
978 * <li>{@link #VISIBILITY_USER_MANAGED_NOT_VISIBLE}</li>
979 * </ul>
Dmitry Dementyev52745472016-12-02 10:27:45 -0800980 * <p>
981 * This method requires the caller to have a signature match with the authenticator that owns
982 * the specified account.
983 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700984 * @param account {@link Account} to update visibility
985 * @param packageName Package name of the application to modify account visibility
986 * @param visibility New visibility value
Dmitry Dementyev52745472016-12-02 10:27:45 -0800987 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700988 * @return True, if visibility value was successfully updated.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800989 */
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800990 public boolean setAccountVisibility(Account account, String packageName,
Dmitry Dementyev52745472016-12-02 10:27:45 -0800991 @AccountVisibility int visibility) {
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800992 if (account == null)
993 throw new IllegalArgumentException("account is null");
Dmitry Dementyev52745472016-12-02 10:27:45 -0800994 try {
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800995 return mService.setAccountVisibility(account, packageName, visibility);
Dmitry Dementyev52745472016-12-02 10:27:45 -0800996 } catch (RemoteException re) {
997 throw re.rethrowFromSystemServer();
998 }
999 }
1000
1001 /**
Dmitry Dementyeve366f822017-01-31 10:25:10 -08001002 * Get visibility of certain account for given application. Possible returned values are:
Dmitry Dementyev52745472016-12-02 10:27:45 -08001003 * <ul>
1004 * <li>{@link #VISIBILITY_VISIBLE}</li>
1005 * <li>{@link #VISIBILITY_USER_MANAGED_VISIBLE}</li>
1006 * <li>{@link #VISIBILITY_NOT_VISIBLE}
1007 * <li>{@link #VISIBILITY_USER_MANAGED_NOT_VISIBLE}</li>
1008 * </ul>
1009 *
1010 * <p>
1011 * This method requires the caller to have a signature match with the authenticator that owns
1012 * the specified account.
1013 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -07001014 * @param account {@link Account} to get visibility
Dmitry Dementyeve366f822017-01-31 10:25:10 -08001015 * @param packageName Package name of the application to get account visibility
Dmitry Dementyev52745472016-12-02 10:27:45 -08001016 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -07001017 * @return int Visibility of given account.
Dmitry Dementyev52745472016-12-02 10:27:45 -08001018 */
Dmitry Dementyeve366f822017-01-31 10:25:10 -08001019 public @AccountVisibility int getAccountVisibility(Account account, String packageName) {
1020 if (account == null)
1021 throw new IllegalArgumentException("account is null");
Dmitry Dementyev52745472016-12-02 10:27:45 -08001022 try {
Dmitry Dementyeve366f822017-01-31 10:25:10 -08001023 return mService.getAccountVisibility(account, packageName);
Tejas Khorana5edff3b2016-06-28 20:59:52 -07001024 } catch (RemoteException re) {
1025 throw re.rethrowFromSystemServer();
1026 }
1027 }
1028
1029 /**
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -07001030 * Notifies the system that the account has just been authenticated. This
1031 * information may be used by other applications to verify the account. This
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001032 * should be called only when the user has entered correct credentials for
1033 * the account.
1034 * <p>
1035 * It is not safe to call this method from the main thread. As such, call it
1036 * from another thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001037 * <p>This method requires the caller to have a signature match with the
1038 * authenticator that owns the specified account.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001039 *
1040 * @param account The {@link Account} to be updated.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001041 * @return boolean {@code true} if the authentication of the account has been successfully
1042 * acknowledged. Otherwise {@code false}.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001043 */
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -07001044 public boolean notifyAccountAuthenticated(Account account) {
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001045 if (account == null)
1046 throw new IllegalArgumentException("account is null");
1047 try {
1048 return mService.accountAuthenticated(account);
1049 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001050 throw e.rethrowFromSystemServer();
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001051 }
1052 }
1053
1054 /**
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001055 * Rename the specified {@link Account}. This is equivalent to removing
1056 * the existing account and adding a new renamed account with the old
1057 * account's user data.
1058 *
1059 * <p>It is safe to call this method from the main thread.
1060 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001061 * <p>This method requires the caller to have a signature match with the
1062 * authenticator that manages the specified account.
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001063 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001064 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1065 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1066 * is needed for those platforms. See docs for this function in API level 22.
1067 *
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001068 * @param account The {@link Account} to rename
1069 * @param newName String name to be associated with the account.
1070 * @param callback Callback to invoke when the request completes, null for
1071 * no callback
1072 * @param handler {@link Handler} identifying the callback thread, null for
1073 * the main thread
1074 * @return An {@link AccountManagerFuture} which resolves to the Account
1075 * after the name change. If successful the account's name will be the
1076 * specified new name.
1077 */
1078 public AccountManagerFuture<Account> renameAccount(
1079 final Account account,
Tor Norbye80b530a2015-04-23 16:36:09 -07001080 @Size(min = 1) final String newName,
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001081 AccountManagerCallback<Account> callback,
1082 Handler handler) {
1083 if (account == null) throw new IllegalArgumentException("account is null.");
1084 if (TextUtils.isEmpty(newName)) {
1085 throw new IllegalArgumentException("newName is empty or null.");
1086 }
1087 return new Future2Task<Account>(handler, callback) {
1088 @Override
1089 public void doWork() throws RemoteException {
1090 mService.renameAccount(mResponse, account, newName);
1091 }
1092 @Override
1093 public Account bundleToResult(Bundle bundle) throws AuthenticatorException {
1094 String name = bundle.getString(KEY_ACCOUNT_NAME);
1095 String type = bundle.getString(KEY_ACCOUNT_TYPE);
Svet Ganovc1c0d1c2016-09-23 19:15:47 -07001096 String accessId = bundle.getString(KEY_ACCOUNT_ACCESS_ID);
1097 return new Account(name, type, accessId);
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001098 }
1099 }.start();
1100 }
1101
1102 /**
1103 * Gets the previous name associated with the account or {@code null}, if
Dmitry Dementyev8882d882017-03-14 17:25:46 -07001104 * none. This is intended so that clients of
1105 * {@link OnAccountsUpdateListener} can determine if an
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001106 * authenticator has renamed an account.
1107 *
1108 * <p>It is safe to call this method from the main thread.
1109 *
1110 * @param account The account to query for a previous name.
1111 * @return The account's previous name, null if the account has never been
1112 * renamed.
1113 */
1114 public String getPreviousName(final Account account) {
1115 if (account == null) throw new IllegalArgumentException("account is null");
1116 try {
1117 return mService.getPreviousName(account);
1118 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001119 throw e.rethrowFromSystemServer();
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001120 }
1121 }
1122
1123 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001124 * Removes an account from the AccountManager. Does nothing if the account
1125 * does not exist. Does not delete the account from the server.
1126 * The authenticator may have its own policies preventing account
1127 * deletion, in which case the account will not be deleted.
1128 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001129 * <p>This method requires the caller to have a signature match with the
1130 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001131 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001132 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1133 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1134 * this function in API level 22.
1135 *
Dan Egnor661f0132010-02-19 11:23:00 -08001136 * @param account The {@link Account} to remove
1137 * @param callback Callback to invoke when the request completes,
1138 * null for no callback
1139 * @param handler {@link Handler} identifying the callback thread,
1140 * null for the main thread
1141 * @return An {@link AccountManagerFuture} which resolves to a Boolean,
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001142 * true if the account has been successfully removed
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001143 * @deprecated use
1144 * {@link #removeAccount(Account, Activity, AccountManagerCallback, Handler)}
1145 * instead
Dan Egnor661f0132010-02-19 11:23:00 -08001146 */
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001147 @Deprecated
Dan Egnor661f0132010-02-19 11:23:00 -08001148 public AccountManagerFuture<Boolean> removeAccount(final Account account,
1149 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001150 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001151 return new Future2Task<Boolean>(handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001152 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001153 public void doWork() throws RemoteException {
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001154 mService.removeAccount(mResponse, account, false);
Dan Egnor661f0132010-02-19 11:23:00 -08001155 }
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001156 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001157 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1158 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1159 throw new AuthenticatorException("no result in response");
1160 }
1161 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1162 }
1163 }.start();
1164 }
1165
1166 /**
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001167 * Removes an account from the AccountManager. Does nothing if the account
1168 * does not exist. Does not delete the account from the server.
1169 * The authenticator may have its own policies preventing account
1170 * deletion, in which case the account will not be deleted.
1171 *
1172 * <p>This method may be called from any thread, but the returned
1173 * {@link AccountManagerFuture} must not be used on the main thread.
1174 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001175 * <p>This method requires the caller to have a signature match with the
1176 * authenticator that manages the specified account.
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001177 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001178 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1179 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1180 * this function in API level 22.
1181 *
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001182 * @param account The {@link Account} to remove
1183 * @param activity The {@link Activity} context to use for launching a new
1184 * authenticator-defined sub-Activity to prompt the user to delete an
1185 * account; used only to call startActivity(); if null, the prompt
1186 * will not be launched directly, but the {@link Intent} may be
1187 * returned to the caller instead
1188 * @param callback Callback to invoke when the request completes,
1189 * null for no callback
1190 * @param handler {@link Handler} identifying the callback thread,
1191 * null for the main thread
1192 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1193 * {@link #KEY_BOOLEAN_RESULT} if activity was specified and an account
1194 * was removed or if active. If no activity was specified, the returned
1195 * Bundle contains only {@link #KEY_INTENT} with the {@link Intent}
1196 * needed to launch the actual account removal process, if authenticator
1197 * needs the activity launch. If an error occurred,
1198 * {@link AccountManagerFuture#getResult()} throws:
1199 * <ul>
1200 * <li> {@link AuthenticatorException} if no authenticator was registered for
1201 * this account type or the authenticator failed to respond
1202 * <li> {@link OperationCanceledException} if the operation was canceled for
1203 * any reason, including the user canceling the creation process or
1204 * adding accounts (of this type) has been disabled by policy
1205 * </ul>
1206 */
1207 public AccountManagerFuture<Bundle> removeAccount(final Account account,
1208 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
1209 if (account == null) throw new IllegalArgumentException("account is null");
1210 return new AmsTask(activity, handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001211 @Override
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001212 public void doWork() throws RemoteException {
1213 mService.removeAccount(mResponse, account, activity != null);
1214 }
1215 }.start();
1216 }
1217
1218 /**
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001219 * @see #removeAccount(Account, AccountManagerCallback, Handler)
1220 * @hide
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001221 * @deprecated use
1222 * {@link #removeAccountAsUser(Account, Activity, AccountManagerCallback, Handler)}
1223 * instead
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001224 */
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001225 @Deprecated
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001226 public AccountManagerFuture<Boolean> removeAccountAsUser(final Account account,
1227 AccountManagerCallback<Boolean> callback, Handler handler,
1228 final UserHandle userHandle) {
1229 if (account == null) throw new IllegalArgumentException("account is null");
1230 if (userHandle == null) throw new IllegalArgumentException("userHandle is null");
1231 return new Future2Task<Boolean>(handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001232 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001233 public void doWork() throws RemoteException {
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001234 mService.removeAccountAsUser(mResponse, account, false, userHandle.getIdentifier());
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001235 }
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001236 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001237 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1238 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1239 throw new AuthenticatorException("no result in response");
1240 }
1241 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1242 }
1243 }.start();
1244 }
1245
1246 /**
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001247 * @see #removeAccount(Account, Activity, AccountManagerCallback, Handler)
1248 * @hide
1249 */
1250 public AccountManagerFuture<Bundle> removeAccountAsUser(final Account account,
1251 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler,
1252 final UserHandle userHandle) {
1253 if (account == null)
1254 throw new IllegalArgumentException("account is null");
1255 if (userHandle == null)
1256 throw new IllegalArgumentException("userHandle is null");
1257 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001258 @Override
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001259 public void doWork() throws RemoteException {
1260 mService.removeAccountAsUser(mResponse, account, activity != null,
1261 userHandle.getIdentifier());
1262 }
1263 }.start();
1264 }
1265
1266 /**
1267 * Removes an account directly. Normally used by authenticators, not
1268 * directly by applications. Does not delete the account from the server.
1269 * The authenticator may have its own policies preventing account deletion,
1270 * in which case the account will not be deleted.
1271 * <p>
1272 * It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001273 * <p>This method requires the caller to have a signature match with the
1274 * authenticator that manages the specified account.
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001275 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001276 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1277 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1278 * is needed for those platforms. See docs for this function in API level 22.
1279 *
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001280 * @param account The {@link Account} to delete.
1281 * @return True if the account was successfully deleted, false if the
1282 * account did not exist, the account is null, or another error
1283 * occurs.
1284 */
1285 public boolean removeAccountExplicitly(Account account) {
1286 if (account == null) throw new IllegalArgumentException("account is null");
1287 try {
1288 return mService.removeAccountExplicitly(account);
1289 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001290 throw e.rethrowFromSystemServer();
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001291 }
1292 }
1293
1294 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001295 * Removes an auth token from the AccountManager's cache. Does nothing if
1296 * the auth token is not currently in the cache. Applications must call this
1297 * method when the auth token is found to have expired or otherwise become
1298 * invalid for authenticating requests. The AccountManager does not validate
1299 * or expire cached auth tokens otherwise.
1300 *
1301 * <p>It is safe to call this method from the main thread.
1302 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001303 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1304 * MANAGE_ACCOUNTS or USE_CREDENTIALS permission is needed for those
1305 * platforms. See docs for this function in API level 22.
1306 *
Fred Quintanaf35b68f2010-04-01 11:36:00 -07001307 * @param accountType The account type of the auth token to invalidate, must not be null
1308 * @param authToken The auth token to invalidate, may be null
Dan Egnor661f0132010-02-19 11:23:00 -08001309 */
1310 public void invalidateAuthToken(final String accountType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -07001311 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001312 try {
Fred Quintanaf35b68f2010-04-01 11:36:00 -07001313 if (authToken != null) {
1314 mService.invalidateAuthToken(accountType, authToken);
1315 }
Dan Egnor661f0132010-02-19 11:23:00 -08001316 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001317 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001318 }
1319 }
1320
1321 /**
1322 * Gets an auth token from the AccountManager's cache. If no auth
1323 * token is cached for this account, null will be returned -- a new
1324 * auth token will not be generated, and the server will not be contacted.
1325 * Intended for use by the authenticator, not directly by applications.
1326 *
1327 * <p>It is safe to call this method from the main thread.
1328 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001329 * <p>This method requires the caller to have a signature match with the
1330 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001331 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001332 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1333 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1334 * is needed for those platforms. See docs for this function in API level 22.
1335 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001336 * @param account The account for which an auth token is to be fetched. Cannot be {@code null}.
Sandra Kwan0b84b452016-01-20 15:25:42 -08001337 * @param authTokenType The type of auth token to fetch. Cannot be {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -08001338 * @return The cached auth token for this account and type, or null if
1339 * no auth token is cached or the account does not exist.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001340 * @see #getAuthToken
Dan Egnor661f0132010-02-19 11:23:00 -08001341 */
1342 public String peekAuthToken(final Account account, final String authTokenType) {
Fred Quintana382601f2010-03-25 12:25:10 -07001343 if (account == null) throw new IllegalArgumentException("account is null");
1344 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001345 try {
1346 return mService.peekAuthToken(account, authTokenType);
1347 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001348 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001349 }
1350 }
1351
1352 /**
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -07001353 * Sets or forgets a saved password. This modifies the local copy of the
1354 * password used to automatically authenticate the user; it does not change
1355 * the user's account password on the server. Intended for use by the
1356 * authenticator, not directly by applications.
1357 * <p>Calling this method does not update the last authenticated timestamp,
1358 * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
1359 * {@link #notifyAccountAuthenticated(Account)} after getting success.
Dan Egnor661f0132010-02-19 11:23:00 -08001360 * <p>It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001361 * <p>This method requires the caller to have a signature match with the
1362 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001363 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001364 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1365 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1366 * is needed for those platforms. See docs for this function in API level 22.
1367 *
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -07001368 * @param account The account whose password is to be set. Cannot be
1369 * {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -08001370 * @param password The password to set, null to clear the password
1371 */
1372 public void setPassword(final Account account, final String password) {
Fred Quintana382601f2010-03-25 12:25:10 -07001373 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001374 try {
1375 mService.setPassword(account, password);
1376 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001377 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001378 }
1379 }
1380
1381 /**
1382 * Forgets a saved password. This erases the local copy of the password;
1383 * it does not change the user's account password on the server.
1384 * Has the same effect as setPassword(account, null) but requires fewer
1385 * permissions, and may be used by applications or management interfaces
1386 * to "sign out" from an account.
1387 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001388 * <p>This method only successfully clear the account's password when the
1389 * caller has the same signature as the authenticator that owns the
1390 * specified account. Otherwise, this method will silently fail.
Dan Egnor661f0132010-02-19 11:23:00 -08001391 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001392 * <p>It is safe to call this method from the main thread.
Carlos Valdiviadcddc472015-06-11 20:04:04 +00001393 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001394 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1395 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1396 * this function in API level 22.
1397 *
Dan Egnor661f0132010-02-19 11:23:00 -08001398 * @param account The account whose password to clear
1399 */
1400 public void clearPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -07001401 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001402 try {
1403 mService.clearPassword(account);
1404 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001405 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001406 }
1407 }
1408
1409 /**
1410 * Sets one userdata key for an account. Intended by use for the
1411 * authenticator to stash state for itself, not directly by applications.
1412 * The meaning of the keys and values is up to the authenticator.
1413 *
1414 * <p>It is safe to call this method from the main thread.
1415 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001416 * <p>This method requires the caller to have a signature match with the
1417 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001418 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001419 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1420 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1421 * is needed for those platforms. See docs for this function in API level 22.
1422 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001423 * @param account Account whose user data is to be set. Must not be {@code null}.
1424 * @param key String user data key to set. Must not be null
1425 * @param value String value to set, {@code null} to clear this user data key
Dan Egnor661f0132010-02-19 11:23:00 -08001426 */
1427 public void setUserData(final Account account, final String key, final String value) {
Fred Quintana382601f2010-03-25 12:25:10 -07001428 if (account == null) throw new IllegalArgumentException("account is null");
1429 if (key == null) throw new IllegalArgumentException("key is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001430 try {
1431 mService.setUserData(account, key, value);
1432 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001433 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001434 }
1435 }
1436
1437 /**
1438 * Adds an auth token to the AccountManager cache for an account.
1439 * If the account does not exist then this call has no effect.
1440 * Replaces any previous auth token for this account and auth token type.
1441 * Intended for use by the authenticator, not directly by applications.
1442 *
1443 * <p>It is safe to call this method from the main thread.
1444 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001445 * <p>This method requires the caller to have a signature match with the
1446 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001447 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001448 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1449 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1450 * is needed for those platforms. See docs for this function in API level 22.
1451 *
Dan Egnor661f0132010-02-19 11:23:00 -08001452 * @param account The account to set an auth token for
1453 * @param authTokenType The type of the auth token, see {#getAuthToken}
1454 * @param authToken The auth token to add to the cache
1455 */
1456 public void setAuthToken(Account account, final String authTokenType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -07001457 if (account == null) throw new IllegalArgumentException("account is null");
1458 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001459 try {
1460 mService.setAuthToken(account, authTokenType, authToken);
1461 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001462 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001463 }
1464 }
1465
1466 /**
1467 * This convenience helper synchronously gets an auth token with
1468 * {@link #getAuthToken(Account, String, boolean, AccountManagerCallback, Handler)}.
1469 *
1470 * <p>This method may block while a network request completes, and must
1471 * never be made from the main thread.
1472 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001473 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1474 * USE_CREDENTIALS permission is needed for those platforms. See docs for
1475 * this function in API level 22.
1476 *
Dan Egnor661f0132010-02-19 11:23:00 -08001477 * @param account The account to fetch an auth token for
Joe Malinb6a35262013-06-03 09:49:04 -07001478 * @param authTokenType The auth token type, see {@link #getAuthToken getAuthToken()}
Dan Egnor661f0132010-02-19 11:23:00 -08001479 * @param notifyAuthFailure If true, display a notification and return null
1480 * if authentication fails; if false, prompt and wait for the user to
1481 * re-enter correct credentials before returning
1482 * @return An auth token of the specified type for this account, or null
1483 * if authentication fails or none can be fetched.
1484 * @throws AuthenticatorException if the authenticator failed to respond
1485 * @throws OperationCanceledException if the request was canceled for any
1486 * reason, including the user canceling a credential request
1487 * @throws java.io.IOException if the authenticator experienced an I/O problem
1488 * creating a new auth token, usually because of network trouble
1489 */
1490 public String blockingGetAuthToken(Account account, String authTokenType,
1491 boolean notifyAuthFailure)
1492 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana382601f2010-03-25 12:25:10 -07001493 if (account == null) throw new IllegalArgumentException("account is null");
1494 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001495 Bundle bundle = getAuthToken(account, authTokenType, notifyAuthFailure, null /* callback */,
1496 null /* handler */).getResult();
Fred Quintana96580e02010-03-04 13:42:42 -08001497 if (bundle == null) {
1498 // This should never happen, but it does, occasionally. If it does return null to
1499 // signify that we were not able to get the authtoken.
1500 // TODO: remove this when the bug is found that sometimes causes a null bundle to be
1501 // returned
1502 Log.e(TAG, "blockingGetAuthToken: null was returned from getResult() for "
1503 + account + ", authTokenType " + authTokenType);
1504 return null;
1505 }
Dan Egnor661f0132010-02-19 11:23:00 -08001506 return bundle.getString(KEY_AUTHTOKEN);
1507 }
1508
1509 /**
1510 * Gets an auth token of the specified type for a particular account,
1511 * prompting the user for credentials if necessary. This method is
1512 * intended for applications running in the foreground where it makes
1513 * sense to ask the user directly for a password.
1514 *
1515 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -07001516 * type, then it is returned. Otherwise, if a saved password is
1517 * available, it is sent to the server to generate a new auth token.
1518 * Otherwise, the user is prompted to enter a password.
Dan Egnor661f0132010-02-19 11:23:00 -08001519 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001520 * <p>Some authenticators have auth token <em>types</em>, whose value
1521 * is authenticator-dependent. Some services use different token types to
1522 * access different functionality -- for example, Google uses different auth
1523 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -08001524 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001525 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
Sandra Kwan0b84b452016-01-20 15:25:42 -08001526 * USE_CREDENTIALS permission is needed for those platforms. See docs for
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001527 * this function in API level 22.
1528 *
Dan Egnor661f0132010-02-19 11:23:00 -08001529 * <p>This method may be called from any thread, but the returned
1530 * {@link AccountManagerFuture} must not be used on the main thread.
1531 *
Dan Egnor661f0132010-02-19 11:23:00 -08001532 * @param account The account to fetch an auth token for
1533 * @param authTokenType The auth token type, an authenticator-dependent
1534 * string token, must not be null
1535 * @param options Authenticator-specific options for the request,
1536 * may be null or empty
1537 * @param activity The {@link Activity} context to use for launching a new
1538 * authenticator-defined sub-Activity to prompt the user for a password
1539 * if necessary; used only to call startActivity(); must not be null.
1540 * @param callback Callback to invoke when the request completes,
1541 * null for no callback
1542 * @param handler {@link Handler} identifying the callback thread,
1543 * null for the main thread
1544 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1545 * at least the following fields:
Fred Quintana756b7352009-10-21 13:43:10 -07001546 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001547 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1548 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1549 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
Fred Quintana756b7352009-10-21 13:43:10 -07001550 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001551 *
1552 * (Other authenticator-specific values may be returned.) If an auth token
1553 * could not be fetched, {@link AccountManagerFuture#getResult()} throws:
1554 * <ul>
1555 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1556 * <li> {@link OperationCanceledException} if the operation is canceled for
1557 * any reason, incluidng the user canceling a credential request
1558 * <li> {@link IOException} if the authenticator experienced an I/O problem
1559 * creating a new auth token, usually because of network trouble
1560 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -07001561 * If the account is no longer present on the device, the return value is
1562 * authenticator-dependent. The caller should verify the validity of the
1563 * account before requesting an auth token.
Dan Egnor661f0132010-02-19 11:23:00 -08001564 */
1565 public AccountManagerFuture<Bundle> getAuthToken(
1566 final Account account, final String authTokenType, final Bundle options,
1567 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001568 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001569 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001570 final Bundle optionsIn = new Bundle();
1571 if (options != null) {
1572 optionsIn.putAll(options);
1573 }
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001574 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Dan Egnor661f0132010-02-19 11:23:00 -08001575 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001576 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001577 public void doWork() throws RemoteException {
1578 mService.getAuthToken(mResponse, account, authTokenType,
1579 false /* notifyOnAuthFailure */, true /* expectActivityLaunch */,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001580 optionsIn);
Dan Egnor661f0132010-02-19 11:23:00 -08001581 }
1582 }.start();
1583 }
1584
1585 /**
1586 * Gets an auth token of the specified type for a particular account,
1587 * optionally raising a notification if the user must enter credentials.
1588 * This method is intended for background tasks and services where the
1589 * user should not be immediately interrupted with a password prompt.
1590 *
1591 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -07001592 * type, then it is returned. Otherwise, if a saved password is
1593 * available, it is sent to the server to generate a new auth token.
1594 * Otherwise, an {@link Intent} is returned which, when started, will
1595 * prompt the user for a password. If the notifyAuthFailure parameter is
1596 * set, a status bar notification is also created with the same Intent,
Dan Egnor661f0132010-02-19 11:23:00 -08001597 * alerting the user that they need to enter a password at some point.
1598 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001599 * <p>In that case, you may need to wait until the user responds, which
1600 * could take hours or days or forever. When the user does respond and
1601 * supply a new password, the account manager will broadcast the
Dmitry Dementyev8882d882017-03-14 17:25:46 -07001602 * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent and
1603 * notify {@link OnAccountsUpdateListener} which applications can
Dan Egnor8e4378b2010-08-02 18:22:09 -07001604 * use to try again.
Dan Egnor661f0132010-02-19 11:23:00 -08001605 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001606 * <p>If notifyAuthFailure is not set, it is the application's
1607 * responsibility to launch the returned Intent at some point.
1608 * Either way, the result from this call will not wait for user action.
Dan Egnor661f0132010-02-19 11:23:00 -08001609 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001610 * <p>Some authenticators have auth token <em>types</em>, whose value
1611 * is authenticator-dependent. Some services use different token types to
1612 * access different functionality -- for example, Google uses different auth
1613 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -08001614 *
1615 * <p>This method may be called from any thread, but the returned
1616 * {@link AccountManagerFuture} must not be used on the main thread.
1617 *
Dan Egnor661f0132010-02-19 11:23:00 -08001618 * @param account The account to fetch an auth token for
1619 * @param authTokenType The auth token type, an authenticator-dependent
1620 * string token, must not be null
Dan Egnor661f0132010-02-19 11:23:00 -08001621 * @param notifyAuthFailure True to add a notification to prompt the
1622 * user for a password if necessary, false to leave that to the caller
1623 * @param callback Callback to invoke when the request completes,
1624 * null for no callback
1625 * @param handler {@link Handler} identifying the callback thread,
1626 * null for the main thread
1627 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1628 * at least the following fields on success:
1629 * <ul>
1630 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1631 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1632 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
1633 * </ul>
1634 *
1635 * (Other authenticator-specific values may be returned.) If the user
1636 * must enter credentials, the returned Bundle contains only
1637 * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt.
1638 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001639 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001640 * <ul>
1641 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1642 * <li> {@link OperationCanceledException} if the operation is canceled for
1643 * any reason, incluidng the user canceling a credential request
1644 * <li> {@link IOException} if the authenticator experienced an I/O problem
1645 * creating a new auth token, usually because of network trouble
1646 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -07001647 * If the account is no longer present on the device, the return value is
1648 * authenticator-dependent. The caller should verify the validity of the
1649 * account before requesting an auth token.
Fred Quintanaad93a322011-09-08 13:21:01 -07001650 * @deprecated use {@link #getAuthToken(Account, String, android.os.Bundle,
1651 * boolean, AccountManagerCallback, android.os.Handler)} instead
Dan Egnor661f0132010-02-19 11:23:00 -08001652 */
Fred Quintanaad93a322011-09-08 13:21:01 -07001653 @Deprecated
Dan Egnor661f0132010-02-19 11:23:00 -08001654 public AccountManagerFuture<Bundle> getAuthToken(
Jatin Lodhia3df7d692013-03-27 10:57:23 -07001655 final Account account, final String authTokenType,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001656 final boolean notifyAuthFailure,
Dan Egnor661f0132010-02-19 11:23:00 -08001657 AccountManagerCallback<Bundle> callback, Handler handler) {
Jatin Lodhia3df7d692013-03-27 10:57:23 -07001658 return getAuthToken(account, authTokenType, null, notifyAuthFailure, callback,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001659 handler);
Dan Egnor661f0132010-02-19 11:23:00 -08001660 }
1661
1662 /**
Fred Quintanaad93a322011-09-08 13:21:01 -07001663 * Gets an auth token of the specified type for a particular account,
1664 * optionally raising a notification if the user must enter credentials.
1665 * This method is intended for background tasks and services where the
1666 * user should not be immediately interrupted with a password prompt.
1667 *
1668 * <p>If a previously generated auth token is cached for this account and
1669 * type, then it is returned. Otherwise, if a saved password is
1670 * available, it is sent to the server to generate a new auth token.
1671 * Otherwise, an {@link Intent} is returned which, when started, will
1672 * prompt the user for a password. If the notifyAuthFailure parameter is
1673 * set, a status bar notification is also created with the same Intent,
1674 * alerting the user that they need to enter a password at some point.
1675 *
1676 * <p>In that case, you may need to wait until the user responds, which
1677 * could take hours or days or forever. When the user does respond and
1678 * supply a new password, the account manager will broadcast the
Dmitry Dementyev8882d882017-03-14 17:25:46 -07001679 * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent and
1680 * notify {@link OnAccountsUpdateListener} which applications can
Fred Quintanaad93a322011-09-08 13:21:01 -07001681 * use to try again.
1682 *
1683 * <p>If notifyAuthFailure is not set, it is the application's
1684 * responsibility to launch the returned Intent at some point.
1685 * Either way, the result from this call will not wait for user action.
1686 *
1687 * <p>Some authenticators have auth token <em>types</em>, whose value
1688 * is authenticator-dependent. Some services use different token types to
1689 * access different functionality -- for example, Google uses different auth
1690 * tokens to access Gmail and Google Calendar for the same account.
1691 *
1692 * <p>This method may be called from any thread, but the returned
1693 * {@link AccountManagerFuture} must not be used on the main thread.
1694 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001695 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
Sandra Kwan0b84b452016-01-20 15:25:42 -08001696 * USE_CREDENTIALS permission is needed for those platforms. See docs for
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001697 * this function in API level 22.
1698 *
Fred Quintanaad93a322011-09-08 13:21:01 -07001699 * @param account The account to fetch an auth token for
1700 * @param authTokenType The auth token type, an authenticator-dependent
1701 * string token, must not be null
1702 * @param options Authenticator-specific options for the request,
1703 * may be null or empty
1704 * @param notifyAuthFailure True to add a notification to prompt the
1705 * user for a password if necessary, false to leave that to the caller
1706 * @param callback Callback to invoke when the request completes,
1707 * null for no callback
1708 * @param handler {@link Handler} identifying the callback thread,
1709 * null for the main thread
1710 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1711 * at least the following fields on success:
1712 * <ul>
1713 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1714 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1715 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
1716 * </ul>
1717 *
1718 * (Other authenticator-specific values may be returned.) If the user
1719 * must enter credentials, the returned Bundle contains only
1720 * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt.
1721 *
1722 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
1723 * <ul>
1724 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1725 * <li> {@link OperationCanceledException} if the operation is canceled for
1726 * any reason, incluidng the user canceling a credential request
1727 * <li> {@link IOException} if the authenticator experienced an I/O problem
1728 * creating a new auth token, usually because of network trouble
1729 * </ul>
1730 * If the account is no longer present on the device, the return value is
1731 * authenticator-dependent. The caller should verify the validity of the
1732 * account before requesting an auth token.
1733 */
1734 public AccountManagerFuture<Bundle> getAuthToken(
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001735 final Account account, final String authTokenType, final Bundle options,
1736 final boolean notifyAuthFailure,
Fred Quintanaad93a322011-09-08 13:21:01 -07001737 AccountManagerCallback<Bundle> callback, Handler handler) {
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001738
Fred Quintanaad93a322011-09-08 13:21:01 -07001739 if (account == null) throw new IllegalArgumentException("account is null");
1740 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001741 final Bundle optionsIn = new Bundle();
1742 if (options != null) {
1743 optionsIn.putAll(options);
1744 }
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001745 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Fred Quintanaad93a322011-09-08 13:21:01 -07001746 return new AmsTask(null, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001747 @Override
Fred Quintanaad93a322011-09-08 13:21:01 -07001748 public void doWork() throws RemoteException {
1749 mService.getAuthToken(mResponse, account, authTokenType,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001750 notifyAuthFailure, false /* expectActivityLaunch */, optionsIn);
Fred Quintanaad93a322011-09-08 13:21:01 -07001751 }
1752 }.start();
1753 }
1754
1755 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001756 * Asks the user to add an account of a specified type. The authenticator
1757 * for this account type processes this request with the appropriate user
1758 * interface. If the user does elect to create a new account, the account
1759 * name is returned.
1760 *
1761 * <p>This method may be called from any thread, but the returned
1762 * {@link AccountManagerFuture} must not be used on the main thread.
Sandra Kwan0b84b452016-01-20 15:25:42 -08001763 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001764 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1765 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1766 * this function in API level 22.
Dan Egnor661f0132010-02-19 11:23:00 -08001767 *
Dan Egnor661f0132010-02-19 11:23:00 -08001768 * @param accountType The type of account to add; must not be null
1769 * @param authTokenType The type of auth token (see {@link #getAuthToken})
1770 * this account will need to be able to generate, null for none
1771 * @param requiredFeatures The features (see {@link #hasFeatures}) this
1772 * account must have, null for none
1773 * @param addAccountOptions Authenticator-specific options for the request,
1774 * may be null or empty
1775 * @param activity The {@link Activity} context to use for launching a new
1776 * authenticator-defined sub-Activity to prompt the user to create an
1777 * account; used only to call startActivity(); if null, the prompt
1778 * will not be launched directly, but the necessary {@link Intent}
1779 * will be returned to the caller instead
1780 * @param callback Callback to invoke when the request completes,
1781 * null for no callback
1782 * @param handler {@link Handler} identifying the callback thread,
1783 * null for the main thread
Doug Zongkerff592dc2010-02-23 12:26:33 -08001784 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
Dan Egnor661f0132010-02-19 11:23:00 -08001785 * these fields if activity was specified and an account was created:
1786 * <ul>
1787 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created
1788 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1789 * </ul>
1790 *
1791 * If no activity was specified, the returned Bundle contains only
1792 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Dan Egnor8e4378b2010-08-02 18:22:09 -07001793 * actual account creation process. If an error occurred,
1794 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001795 * <ul>
1796 * <li> {@link AuthenticatorException} if no authenticator was registered for
1797 * this account type or the authenticator failed to respond
1798 * <li> {@link OperationCanceledException} if the operation was canceled for
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001799 * any reason, including the user canceling the creation process or adding accounts
1800 * (of this type) has been disabled by policy
Dan Egnor661f0132010-02-19 11:23:00 -08001801 * <li> {@link IOException} if the authenticator experienced an I/O problem
1802 * creating a new account, usually because of network trouble
1803 * </ul>
1804 */
1805 public AccountManagerFuture<Bundle> addAccount(final String accountType,
1806 final String authTokenType, final String[] requiredFeatures,
1807 final Bundle addAccountOptions,
1808 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001809 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001810 final Bundle optionsIn = new Bundle();
1811 if (addAccountOptions != null) {
1812 optionsIn.putAll(addAccountOptions);
1813 }
1814 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001815
Dan Egnor661f0132010-02-19 11:23:00 -08001816 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001817 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001818 public void doWork() throws RemoteException {
Amith Yamasani27db4682013-03-30 17:07:47 -07001819 mService.addAccount(mResponse, accountType, authTokenType,
Costin Manolachee5847ad2011-09-14 12:52:19 -07001820 requiredFeatures, activity != null, optionsIn);
Dan Egnor661f0132010-02-19 11:23:00 -08001821 }
1822 }.start();
1823 }
1824
1825 /**
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001826 * @see #addAccount(String, String, String[], Bundle, Activity, AccountManagerCallback, Handler)
1827 * @hide
1828 */
1829 public AccountManagerFuture<Bundle> addAccountAsUser(final String accountType,
1830 final String authTokenType, final String[] requiredFeatures,
1831 final Bundle addAccountOptions, final Activity activity,
1832 AccountManagerCallback<Bundle> callback, Handler handler, final UserHandle userHandle) {
1833 if (accountType == null) throw new IllegalArgumentException("accountType is null");
1834 if (userHandle == null) throw new IllegalArgumentException("userHandle is null");
1835 final Bundle optionsIn = new Bundle();
1836 if (addAccountOptions != null) {
1837 optionsIn.putAll(addAccountOptions);
1838 }
1839 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
1840
1841 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001842 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001843 public void doWork() throws RemoteException {
1844 mService.addAccountAsUser(mResponse, accountType, authTokenType,
1845 requiredFeatures, activity != null, optionsIn, userHandle.getIdentifier());
1846 }
1847 }.start();
1848 }
1849
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001850
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001851 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001852 * Adds shared accounts from a parent user to a secondary user. Adding the shared account
Amith Yamasani67df64b2012-12-14 12:09:36 -08001853 * doesn't take effect immediately. When the target user starts up, any pending shared accounts
1854 * are attempted to be copied to the target user from the primary via calls to the
1855 * authenticator.
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001856 * @param parentUser parent user
1857 * @param user target user
Amith Yamasani67df64b2012-12-14 12:09:36 -08001858 * @hide
1859 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001860 public void addSharedAccountsFromParentUser(UserHandle parentUser, UserHandle user) {
Amith Yamasani67df64b2012-12-14 12:09:36 -08001861 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001862 mService.addSharedAccountsFromParentUser(parentUser.getIdentifier(),
Sudheer Shankaf88ebeb2017-02-14 18:30:40 -08001863 user.getIdentifier(), mContext.getOpPackageName());
Amith Yamasani67df64b2012-12-14 12:09:36 -08001864 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001865 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001866 }
1867 }
1868
1869 /**
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001870 * Copies an account from one user to another user.
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001871 * @param account the account to copy
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001872 * @param fromUser the user to copy the account from
1873 * @param toUser the target user
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001874 * @param callback Callback to invoke when the request completes,
1875 * null for no callback
1876 * @param handler {@link Handler} identifying the callback thread,
1877 * null for the main thread
1878 * @return An {@link AccountManagerFuture} which resolves to a Boolean indicated wether it
1879 * succeeded.
1880 * @hide
1881 */
1882 public AccountManagerFuture<Boolean> copyAccountToUser(
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001883 final Account account, final UserHandle fromUser, final UserHandle toUser,
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001884 AccountManagerCallback<Boolean> callback, Handler handler) {
1885 if (account == null) throw new IllegalArgumentException("account is null");
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001886 if (toUser == null || fromUser == null) {
1887 throw new IllegalArgumentException("fromUser and toUser cannot be null");
1888 }
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001889
1890 return new Future2Task<Boolean>(handler, callback) {
1891 @Override
1892 public void doWork() throws RemoteException {
1893 mService.copyAccountToUser(
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001894 mResponse, account, fromUser.getIdentifier(), toUser.getIdentifier());
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001895 }
1896 @Override
1897 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1898 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1899 throw new AuthenticatorException("no result in response");
1900 }
1901 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1902 }
1903 }.start();
1904 }
1905
1906 /**
Amith Yamasani67df64b2012-12-14 12:09:36 -08001907 * @hide
1908 * Removes the shared account.
1909 * @param account the account to remove
1910 * @param user the user to remove the account from
1911 * @return
1912 */
1913 public boolean removeSharedAccount(final Account account, UserHandle user) {
1914 try {
1915 boolean val = mService.removeSharedAccountAsUser(account, user.getIdentifier());
1916 return val;
1917 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001918 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001919 }
1920 }
1921
1922 /**
1923 * @hide
1924 * @param user
1925 * @return
1926 */
1927 public Account[] getSharedAccounts(UserHandle user) {
1928 try {
1929 return mService.getSharedAccountsAsUser(user.getIdentifier());
1930 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001931 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001932 }
1933 }
1934
1935 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001936 * Confirms that the user knows the password for an account to make extra
1937 * sure they are the owner of the account. The user-entered password can
1938 * be supplied directly, otherwise the authenticator for this account type
1939 * prompts the user with the appropriate interface. This method is
1940 * intended for applications which want extra assurance; for example, the
1941 * phone lock screen uses this to let the user unlock the phone with an
1942 * account password if they forget the lock pattern.
1943 *
1944 * <p>If the user-entered password matches a saved password for this
1945 * account, the request is considered valid; otherwise the authenticator
1946 * verifies the password (usually by contacting the server).
1947 *
1948 * <p>This method may be called from any thread, but the returned
1949 * {@link AccountManagerFuture} must not be used on the main thread.
1950 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001951 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1952 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs
1953 * for this function in API level 22.
1954 *
Dan Egnor661f0132010-02-19 11:23:00 -08001955 * @param account The account to confirm password knowledge for
1956 * @param options Authenticator-specific options for the request;
1957 * if the {@link #KEY_PASSWORD} string field is present, the
1958 * authenticator may use it directly rather than prompting the user;
1959 * may be null or empty
1960 * @param activity The {@link Activity} context to use for launching a new
1961 * authenticator-defined sub-Activity to prompt the user to enter a
1962 * password; used only to call startActivity(); if null, the prompt
1963 * will not be launched directly, but the necessary {@link Intent}
1964 * will be returned to the caller instead
1965 * @param callback Callback to invoke when the request completes,
1966 * null for no callback
1967 * @param handler {@link Handler} identifying the callback thread,
1968 * null for the main thread
1969 * @return An {@link AccountManagerFuture} which resolves to a Bundle
1970 * with these fields if activity or password was supplied and
1971 * the account was successfully verified:
1972 * <ul>
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001973 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account verified
Dan Egnor661f0132010-02-19 11:23:00 -08001974 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1975 * <li> {@link #KEY_BOOLEAN_RESULT} - true to indicate success
1976 * </ul>
1977 *
1978 * If no activity or password was specified, the returned Bundle contains
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001979 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
1980 * password prompt.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001981 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001982 * <p>Also the returning Bundle may contain {@link
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -07001983 * #KEY_LAST_AUTHENTICATED_TIME} indicating the last time the
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001984 * credential was validated/created.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001985 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001986 * If an error occurred,{@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001987 * <ul>
1988 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1989 * <li> {@link OperationCanceledException} if the operation was canceled for
1990 * any reason, including the user canceling the password prompt
1991 * <li> {@link IOException} if the authenticator experienced an I/O problem
1992 * verifying the password, usually because of network trouble
1993 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001994 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001995 public AccountManagerFuture<Bundle> confirmCredentials(final Account account,
1996 final Bundle options,
1997 final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001998 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07001999 final Handler handler) {
Amith Yamasani2c7bc262012-11-05 16:46:02 -08002000 return confirmCredentialsAsUser(account, options, activity, callback, handler,
2001 Process.myUserHandle());
2002 }
2003
2004 /**
2005 * @hide
2006 * Same as {@link #confirmCredentials(Account, Bundle, Activity, AccountManagerCallback, Handler)}
2007 * but for the specified user.
2008 */
2009 public AccountManagerFuture<Bundle> confirmCredentialsAsUser(final Account account,
2010 final Bundle options,
2011 final Activity activity,
2012 final AccountManagerCallback<Bundle> callback,
2013 final Handler handler, UserHandle userHandle) {
Fred Quintana382601f2010-03-25 12:25:10 -07002014 if (account == null) throw new IllegalArgumentException("account is null");
Amith Yamasani2c7bc262012-11-05 16:46:02 -08002015 final int userId = userHandle.getIdentifier();
Fred Quintanaa698f422009-04-08 19:14:54 -07002016 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002017 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002018 public void doWork() throws RemoteException {
Amith Yamasani2c7bc262012-11-05 16:46:02 -08002019 mService.confirmCredentialsAsUser(mResponse, account, options, activity != null,
2020 userId);
Fred Quintanaa698f422009-04-08 19:14:54 -07002021 }
Fred Quintana33269202009-04-20 16:05:10 -07002022 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07002023 }
2024
Fred Quintana756b7352009-10-21 13:43:10 -07002025 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002026 * Asks the user to enter a new password for an account, updating the
2027 * saved credentials for the account. Normally this happens automatically
2028 * when the server rejects credentials during an auth token fetch, but this
2029 * can be invoked directly to ensure we have the correct credentials stored.
Fred Quintana756b7352009-10-21 13:43:10 -07002030 *
Dan Egnor661f0132010-02-19 11:23:00 -08002031 * <p>This method may be called from any thread, but the returned
2032 * {@link AccountManagerFuture} must not be used on the main thread.
2033 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002034 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
2035 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
2036 * this function in API level 22.
2037 *
Dan Egnor661f0132010-02-19 11:23:00 -08002038 * @param account The account to update credentials for
2039 * @param authTokenType The credentials entered must allow an auth token
2040 * of this type to be created (but no actual auth token is returned);
2041 * may be null
2042 * @param options Authenticator-specific options for the request;
2043 * may be null or empty
2044 * @param activity The {@link Activity} context to use for launching a new
2045 * authenticator-defined sub-Activity to prompt the user to enter a
2046 * password; used only to call startActivity(); if null, the prompt
2047 * will not be launched directly, but the necessary {@link Intent}
2048 * will be returned to the caller instead
2049 * @param callback Callback to invoke when the request completes,
2050 * null for no callback
2051 * @param handler {@link Handler} identifying the callback thread,
2052 * null for the main thread
2053 * @return An {@link AccountManagerFuture} which resolves to a Bundle
2054 * with these fields if an activity was supplied and the account
2055 * credentials were successfully updated:
Fred Quintana756b7352009-10-21 13:43:10 -07002056 * <ul>
Ian Pedowitz6cc066d2015-08-05 14:23:43 +00002057 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account
Dan Egnor661f0132010-02-19 11:23:00 -08002058 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
Fred Quintana756b7352009-10-21 13:43:10 -07002059 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002060 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08002061 * If no activity was specified, the returned Bundle contains
Dan Egnor661f0132010-02-19 11:23:00 -08002062 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08002063 * password prompt. If an error occurred,
Dan Egnor8e4378b2010-08-02 18:22:09 -07002064 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08002065 * <ul>
2066 * <li> {@link AuthenticatorException} if the authenticator failed to respond
2067 * <li> {@link OperationCanceledException} if the operation was canceled for
2068 * any reason, including the user canceling the password prompt
2069 * <li> {@link IOException} if the authenticator experienced an I/O problem
2070 * verifying the password, usually because of network trouble
2071 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002072 */
2073 public AccountManagerFuture<Bundle> updateCredentials(final Account account,
2074 final String authTokenType,
Fred Quintana31957f12009-10-21 13:43:10 -07002075 final Bundle options, final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002076 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07002077 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07002078 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07002079 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002080 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002081 public void doWork() throws RemoteException {
2082 mService.updateCredentials(mResponse, account, authTokenType, activity != null,
Fred Quintana31957f12009-10-21 13:43:10 -07002083 options);
Fred Quintanaa698f422009-04-08 19:14:54 -07002084 }
Fred Quintana33269202009-04-20 16:05:10 -07002085 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07002086 }
2087
Fred Quintana756b7352009-10-21 13:43:10 -07002088 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002089 * Offers the user an opportunity to change an authenticator's settings.
2090 * These properties are for the authenticator in general, not a particular
2091 * account. Not all authenticators support this method.
Fred Quintana756b7352009-10-21 13:43:10 -07002092 *
Dan Egnor661f0132010-02-19 11:23:00 -08002093 * <p>This method may be called from any thread, but the returned
2094 * {@link AccountManagerFuture} must not be used on the main thread.
2095 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07002096 * <p>This method requires the caller to have the same signature as the
2097 * authenticator associated with the specified account type.
Dan Egnor661f0132010-02-19 11:23:00 -08002098 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002099 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
2100 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs
2101 * for this function in API level 22.
2102 *
Dan Egnor661f0132010-02-19 11:23:00 -08002103 * @param accountType The account type associated with the authenticator
2104 * to adjust
2105 * @param activity The {@link Activity} context to use for launching a new
2106 * authenticator-defined sub-Activity to adjust authenticator settings;
2107 * used only to call startActivity(); if null, the settings dialog will
2108 * not be launched directly, but the necessary {@link Intent} will be
2109 * returned to the caller instead
2110 * @param callback Callback to invoke when the request completes,
2111 * null for no callback
2112 * @param handler {@link Handler} identifying the callback thread,
2113 * null for the main thread
2114 * @return An {@link AccountManagerFuture} which resolves to a Bundle
2115 * which is empty if properties were edited successfully, or
2116 * if no activity was specified, contains only {@link #KEY_INTENT}
2117 * needed to launch the authenticator's settings dialog.
Dan Egnor8e4378b2010-08-02 18:22:09 -07002118 * If an error occurred, {@link AccountManagerFuture#getResult()}
2119 * throws:
Fred Quintana756b7352009-10-21 13:43:10 -07002120 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002121 * <li> {@link AuthenticatorException} if no authenticator was registered for
2122 * this account type or the authenticator failed to respond
2123 * <li> {@link OperationCanceledException} if the operation was canceled for
2124 * any reason, including the user canceling the settings dialog
2125 * <li> {@link IOException} if the authenticator experienced an I/O problem
2126 * updating settings, usually because of network trouble
Fred Quintana756b7352009-10-21 13:43:10 -07002127 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002128 */
2129 public AccountManagerFuture<Bundle> editProperties(final String accountType,
2130 final Activity activity, final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07002131 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07002132 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07002133 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002134 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002135 public void doWork() throws RemoteException {
2136 mService.editProperties(mResponse, accountType, activity != null);
2137 }
Fred Quintana33269202009-04-20 16:05:10 -07002138 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07002139 }
2140
Amith Yamasani12747872015-12-07 14:19:49 -08002141 /**
2142 * @hide
2143 * Checks if the given account exists on any of the users on the device.
2144 * Only the system process can call this method.
2145 *
2146 * @param account The account to check for existence.
2147 * @return whether any user has this account
2148 */
2149 public boolean someUserHasAccount(@NonNull final Account account) {
2150 try {
2151 return mService.someUserHasAccount(account);
2152 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002153 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002154 }
2155 }
2156
Fred Quintanaa698f422009-04-08 19:14:54 -07002157 private void ensureNotOnMainThread() {
2158 final Looper looper = Looper.myLooper();
2159 if (looper != null && looper == mContext.getMainLooper()) {
Fred Quintana53bd2522010-02-05 15:28:12 -08002160 final IllegalStateException exception = new IllegalStateException(
2161 "calling this from your main thread can lead to deadlock");
2162 Log.e(TAG, "calling this from your main thread can lead to deadlock and/or ANRs",
2163 exception);
Fred Quintana751fdc02010-02-09 14:13:18 -08002164 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.FROYO) {
2165 throw exception;
2166 }
Fred Quintana60307342009-03-24 22:48:12 -07002167 }
2168 }
2169
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002170 private void postToHandler(Handler handler, final AccountManagerCallback<Bundle> callback,
2171 final AccountManagerFuture<Bundle> future) {
Fred Quintanad9d2f112009-04-23 13:36:27 -07002172 handler = handler == null ? mMainHandler : handler;
2173 handler.post(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002174 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002175 public void run() {
2176 callback.run(future);
2177 }
2178 });
2179 }
Fred Quintana60307342009-03-24 22:48:12 -07002180
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002181 private void postToHandler(Handler handler, final OnAccountsUpdateListener listener,
Fred Quintanad9d2f112009-04-23 13:36:27 -07002182 final Account[] accounts) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002183 final Account[] accountsCopy = new Account[accounts.length];
2184 // send a copy to make sure that one doesn't
2185 // change what another sees
2186 System.arraycopy(accounts, 0, accountsCopy, 0, accountsCopy.length);
2187 handler = (handler == null) ? mMainHandler : handler;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002188 handler.post(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002189 @Override
Fred Quintanad9d2f112009-04-23 13:36:27 -07002190 public void run() {
Hiroaki Kuriyama8d3e1592015-10-09 16:30:03 +02002191 synchronized (mAccountsUpdatedListeners) {
2192 try {
2193 if (mAccountsUpdatedListeners.containsKey(listener)) {
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002194 Set<String> types = mAccountsUpdatedListenersTypes.get(listener);
2195 if (types != null) {
2196 // filter by account type;
2197 ArrayList<Account> filtered = new ArrayList<>();
2198 for (Account account : accountsCopy) {
2199 if (types.contains(account.type)) {
2200 filtered.add(account);
2201 }
2202 }
2203 listener.onAccountsUpdated(
2204 filtered.toArray(new Account[filtered.size()]));
2205 } else {
2206 listener.onAccountsUpdated(accountsCopy);
2207 }
Hiroaki Kuriyama8d3e1592015-10-09 16:30:03 +02002208 }
2209 } catch (SQLException e) {
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002210 // Better luck next time. If the problem was disk-full,
Hiroaki Kuriyama8d3e1592015-10-09 16:30:03 +02002211 // the STORAGE_OK intent will re-trigger the update.
2212 Log.e(TAG, "Can't update accounts", e);
2213 }
Costin Manolacheb6437242009-09-10 16:14:12 -07002214 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002215 }
2216 });
2217 }
2218
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002219 private abstract class AmsTask extends FutureTask<Bundle> implements AccountManagerFuture<Bundle> {
Fred Quintanaa698f422009-04-08 19:14:54 -07002220 final IAccountManagerResponse mResponse;
2221 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002222 final AccountManagerCallback<Bundle> mCallback;
Fred Quintanaa698f422009-04-08 19:14:54 -07002223 final Activity mActivity;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002224 public AmsTask(Activity activity, Handler handler, AccountManagerCallback<Bundle> callback) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002225 super(new Callable<Bundle>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002226 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002227 public Bundle call() throws Exception {
2228 throw new IllegalStateException("this should never be called");
2229 }
2230 });
2231
2232 mHandler = handler;
2233 mCallback = callback;
2234 mActivity = activity;
2235 mResponse = new Response();
Fred Quintana33269202009-04-20 16:05:10 -07002236 }
2237
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002238 public final AccountManagerFuture<Bundle> start() {
2239 try {
2240 doWork();
2241 } catch (RemoteException e) {
2242 setException(e);
2243 }
Fred Quintana33269202009-04-20 16:05:10 -07002244 return this;
Fred Quintana60307342009-03-24 22:48:12 -07002245 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002246
Sandra Kwan0b84b452016-01-20 15:25:42 -08002247 @Override
Fred Quintana96580e02010-03-04 13:42:42 -08002248 protected void set(Bundle bundle) {
2249 // TODO: somehow a null is being set as the result of the Future. Log this
2250 // case to help debug where this is occurring. When this bug is fixed this
2251 // condition statement should be removed.
2252 if (bundle == null) {
2253 Log.e(TAG, "the bundle must not be null", new Exception());
2254 }
2255 super.set(bundle);
2256 }
2257
Fred Quintanaa698f422009-04-08 19:14:54 -07002258 public abstract void doWork() throws RemoteException;
2259
2260 private Bundle internalGetResult(Long timeout, TimeUnit unit)
2261 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08002262 if (!isDone()) {
2263 ensureNotOnMainThread();
2264 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002265 try {
2266 if (timeout == null) {
2267 return get();
2268 } else {
2269 return get(timeout, unit);
2270 }
2271 } catch (CancellationException e) {
2272 throw new OperationCanceledException();
2273 } catch (TimeoutException e) {
2274 // fall through and cancel
2275 } catch (InterruptedException e) {
2276 // fall through and cancel
2277 } catch (ExecutionException e) {
2278 final Throwable cause = e.getCause();
2279 if (cause instanceof IOException) {
2280 throw (IOException) cause;
2281 } else if (cause instanceof UnsupportedOperationException) {
2282 throw new AuthenticatorException(cause);
2283 } else if (cause instanceof AuthenticatorException) {
2284 throw (AuthenticatorException) cause;
2285 } else if (cause instanceof RuntimeException) {
2286 throw (RuntimeException) cause;
2287 } else if (cause instanceof Error) {
2288 throw (Error) cause;
2289 } else {
2290 throw new IllegalStateException(cause);
2291 }
2292 } finally {
2293 cancel(true /* interrupt if running */);
2294 }
2295 throw new OperationCanceledException();
2296 }
2297
Sandra Kwan0b84b452016-01-20 15:25:42 -08002298 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002299 public Bundle getResult()
2300 throws OperationCanceledException, IOException, AuthenticatorException {
2301 return internalGetResult(null, null);
2302 }
2303
Sandra Kwan0b84b452016-01-20 15:25:42 -08002304 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002305 public Bundle getResult(long timeout, TimeUnit unit)
2306 throws OperationCanceledException, IOException, AuthenticatorException {
2307 return internalGetResult(timeout, unit);
2308 }
2309
Sandra Kwan0b84b452016-01-20 15:25:42 -08002310 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002311 protected void done() {
2312 if (mCallback != null) {
2313 postToHandler(mHandler, mCallback, this);
2314 }
2315 }
2316
2317 /** Handles the responses from the AccountManager */
2318 private class Response extends IAccountManagerResponse.Stub {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002319 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002320 public void onResult(Bundle bundle) {
tiansimingf58793c2017-09-03 16:48:34 +08002321 if (bundle == null) {
2322 onError(ERROR_CODE_INVALID_RESPONSE, "null bundle returned");
2323 return;
2324 }
Brian Carlstrom46703b02011-04-06 15:41:29 -07002325 Intent intent = bundle.getParcelable(KEY_INTENT);
Fred Quintanaa698f422009-04-08 19:14:54 -07002326 if (intent != null && mActivity != null) {
2327 // since the user provided an Activity we will silently start intents
2328 // that we see
2329 mActivity.startActivity(intent);
2330 // leave the Future running to wait for the real response to this request
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07002331 } else if (bundle.getBoolean("retry")) {
2332 try {
2333 doWork();
2334 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002335 throw e.rethrowFromSystemServer();
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07002336 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002337 } else {
2338 set(bundle);
2339 }
2340 }
2341
Sandra Kwan0b84b452016-01-20 15:25:42 -08002342 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002343 public void onError(int code, String message) {
Alexandra Gherghina999d3942014-07-03 11:40:08 +01002344 if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED
2345 || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) {
2346 // the authenticator indicated that this request was canceled or we were
2347 // forbidden to fulfill; cancel now
Fred Quintanaa698f422009-04-08 19:14:54 -07002348 cancel(true /* mayInterruptIfRunning */);
2349 return;
2350 }
2351 setException(convertErrorToException(code, message));
2352 }
2353 }
2354
Fred Quintana60307342009-03-24 22:48:12 -07002355 }
2356
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002357 private abstract class BaseFutureTask<T> extends FutureTask<T> {
2358 final public IAccountManagerResponse mResponse;
Fred Quintanaa698f422009-04-08 19:14:54 -07002359 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002360
2361 public BaseFutureTask(Handler handler) {
2362 super(new Callable<T>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002363 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002364 public T call() throws Exception {
Fred Quintanaa698f422009-04-08 19:14:54 -07002365 throw new IllegalStateException("this should never be called");
2366 }
2367 });
Fred Quintanaa698f422009-04-08 19:14:54 -07002368 mHandler = handler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002369 mResponse = new Response();
Fred Quintana60307342009-03-24 22:48:12 -07002370 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002371
2372 public abstract void doWork() throws RemoteException;
2373
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002374 public abstract T bundleToResult(Bundle bundle) throws AuthenticatorException;
Fred Quintanaa698f422009-04-08 19:14:54 -07002375
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002376 protected void postRunnableToHandler(Runnable runnable) {
2377 Handler handler = (mHandler == null) ? mMainHandler : mHandler;
2378 handler.post(runnable);
Fred Quintanaa698f422009-04-08 19:14:54 -07002379 }
2380
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002381 protected void startTask() {
Fred Quintanaa698f422009-04-08 19:14:54 -07002382 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002383 doWork();
2384 } catch (RemoteException e) {
2385 setException(e);
Fred Quintanaa698f422009-04-08 19:14:54 -07002386 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002387 }
2388
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002389 protected class Response extends IAccountManagerResponse.Stub {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002390 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002391 public void onResult(Bundle bundle) {
2392 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002393 T result = bundleToResult(bundle);
2394 if (result == null) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002395 return;
2396 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002397 set(result);
2398 return;
Fred Quintanaa698f422009-04-08 19:14:54 -07002399 } catch (ClassCastException e) {
2400 // we will set the exception below
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002401 } catch (AuthenticatorException e) {
2402 // we will set the exception below
Fred Quintanaa698f422009-04-08 19:14:54 -07002403 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002404 onError(ERROR_CODE_INVALID_RESPONSE, "no result in response");
Fred Quintanaa698f422009-04-08 19:14:54 -07002405 }
2406
Sandra Kwan0b84b452016-01-20 15:25:42 -08002407 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002408 public void onError(int code, String message) {
Alexandra Gherghina999d3942014-07-03 11:40:08 +01002409 if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED
2410 || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) {
2411 // the authenticator indicated that this request was canceled or we were
2412 // forbidden to fulfill; cancel now
Fred Quintanaa698f422009-04-08 19:14:54 -07002413 cancel(true /* mayInterruptIfRunning */);
2414 return;
2415 }
2416 setException(convertErrorToException(code, message));
2417 }
2418 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002419 }
2420
2421 private abstract class Future2Task<T>
2422 extends BaseFutureTask<T> implements AccountManagerFuture<T> {
2423 final AccountManagerCallback<T> mCallback;
2424 public Future2Task(Handler handler, AccountManagerCallback<T> callback) {
2425 super(handler);
2426 mCallback = callback;
2427 }
2428
Sandra Kwan0b84b452016-01-20 15:25:42 -08002429 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002430 protected void done() {
2431 if (mCallback != null) {
2432 postRunnableToHandler(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002433 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002434 public void run() {
2435 mCallback.run(Future2Task.this);
2436 }
2437 });
2438 }
2439 }
2440
2441 public Future2Task<T> start() {
2442 startTask();
2443 return this;
2444 }
2445
2446 private T internalGetResult(Long timeout, TimeUnit unit)
2447 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08002448 if (!isDone()) {
2449 ensureNotOnMainThread();
2450 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002451 try {
2452 if (timeout == null) {
2453 return get();
2454 } else {
2455 return get(timeout, unit);
2456 }
2457 } catch (InterruptedException e) {
2458 // fall through and cancel
2459 } catch (TimeoutException e) {
2460 // fall through and cancel
2461 } catch (CancellationException e) {
2462 // fall through and cancel
2463 } catch (ExecutionException e) {
2464 final Throwable cause = e.getCause();
2465 if (cause instanceof IOException) {
2466 throw (IOException) cause;
2467 } else if (cause instanceof UnsupportedOperationException) {
2468 throw new AuthenticatorException(cause);
2469 } else if (cause instanceof AuthenticatorException) {
2470 throw (AuthenticatorException) cause;
2471 } else if (cause instanceof RuntimeException) {
2472 throw (RuntimeException) cause;
2473 } else if (cause instanceof Error) {
2474 throw (Error) cause;
2475 } else {
2476 throw new IllegalStateException(cause);
2477 }
2478 } finally {
2479 cancel(true /* interrupt if running */);
2480 }
2481 throw new OperationCanceledException();
2482 }
2483
Sandra Kwan0b84b452016-01-20 15:25:42 -08002484 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002485 public T getResult()
2486 throws OperationCanceledException, IOException, AuthenticatorException {
2487 return internalGetResult(null, null);
2488 }
2489
Sandra Kwan0b84b452016-01-20 15:25:42 -08002490 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002491 public T getResult(long timeout, TimeUnit unit)
2492 throws OperationCanceledException, IOException, AuthenticatorException {
2493 return internalGetResult(timeout, unit);
2494 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002495
Fred Quintana60307342009-03-24 22:48:12 -07002496 }
2497
Fred Quintanaa698f422009-04-08 19:14:54 -07002498 private Exception convertErrorToException(int code, String message) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002499 if (code == ERROR_CODE_NETWORK_ERROR) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002500 return new IOException(message);
Fred Quintana60307342009-03-24 22:48:12 -07002501 }
Fred Quintana60307342009-03-24 22:48:12 -07002502
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002503 if (code == ERROR_CODE_UNSUPPORTED_OPERATION) {
Fred Quintana33269202009-04-20 16:05:10 -07002504 return new UnsupportedOperationException(message);
Fred Quintana60307342009-03-24 22:48:12 -07002505 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002506
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002507 if (code == ERROR_CODE_INVALID_RESPONSE) {
Fred Quintana33269202009-04-20 16:05:10 -07002508 return new AuthenticatorException(message);
Fred Quintanaa698f422009-04-08 19:14:54 -07002509 }
2510
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002511 if (code == ERROR_CODE_BAD_ARGUMENTS) {
Fred Quintana33269202009-04-20 16:05:10 -07002512 return new IllegalArgumentException(message);
2513 }
2514
2515 return new AuthenticatorException(message);
2516 }
2517
sunjianf29d5492017-05-11 15:42:31 -07002518 private void getAccountByTypeAndFeatures(String accountType, String[] features,
2519 AccountManagerCallback<Bundle> callback, Handler handler) {
2520 (new AmsTask(null, handler, callback) {
2521 @Override
2522 public void doWork() throws RemoteException {
2523 mService.getAccountByTypeAndFeatures(mResponse, accountType, features,
2524 mContext.getOpPackageName());
2525 }
2526
2527 }).start();
2528 }
2529
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002530 private class GetAuthTokenByTypeAndFeaturesTask
2531 extends AmsTask implements AccountManagerCallback<Bundle> {
Fred Quintana33269202009-04-20 16:05:10 -07002532 GetAuthTokenByTypeAndFeaturesTask(final String accountType, final String authTokenType,
2533 final String[] features, Activity activityForPrompting,
2534 final Bundle addAccountOptions, final Bundle loginOptions,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002535 AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07002536 super(activityForPrompting, handler, callback);
2537 if (accountType == null) throw new IllegalArgumentException("account type is null");
2538 mAccountType = accountType;
2539 mAuthTokenType = authTokenType;
2540 mFeatures = features;
2541 mAddAccountOptions = addAccountOptions;
2542 mLoginOptions = loginOptions;
2543 mMyCallback = this;
2544 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002545 volatile AccountManagerFuture<Bundle> mFuture = null;
Fred Quintana33269202009-04-20 16:05:10 -07002546 final String mAccountType;
2547 final String mAuthTokenType;
2548 final String[] mFeatures;
2549 final Bundle mAddAccountOptions;
2550 final Bundle mLoginOptions;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002551 final AccountManagerCallback<Bundle> mMyCallback;
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002552 private volatile int mNumAccounts = 0;
Fred Quintana33269202009-04-20 16:05:10 -07002553
Sandra Kwan0b84b452016-01-20 15:25:42 -08002554 @Override
Fred Quintana33269202009-04-20 16:05:10 -07002555 public void doWork() throws RemoteException {
sunjianf29d5492017-05-11 15:42:31 -07002556 getAccountByTypeAndFeatures(mAccountType, mFeatures,
2557 new AccountManagerCallback<Bundle>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002558 @Override
sunjianf29d5492017-05-11 15:42:31 -07002559 public void run(AccountManagerFuture<Bundle> future) {
2560 String accountName = null;
2561 String accountType = null;
Fred Quintana33269202009-04-20 16:05:10 -07002562 try {
sunjianf29d5492017-05-11 15:42:31 -07002563 Bundle result = future.getResult();
2564 accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME);
2565 accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002566 } catch (OperationCanceledException e) {
2567 setException(e);
2568 return;
2569 } catch (IOException e) {
2570 setException(e);
2571 return;
2572 } catch (AuthenticatorException e) {
2573 setException(e);
2574 return;
Fred Quintana33269202009-04-20 16:05:10 -07002575 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002576
sunjianf29d5492017-05-11 15:42:31 -07002577 if (accountName == null) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002578 if (mActivity != null) {
2579 // no accounts, add one now. pretend that the user directly
2580 // made this request
2581 mFuture = addAccount(mAccountType, mAuthTokenType, mFeatures,
2582 mAddAccountOptions, mActivity, mMyCallback, mHandler);
2583 } else {
2584 // send result since we can't prompt to add an account
2585 Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002586 result.putString(KEY_ACCOUNT_NAME, null);
2587 result.putString(KEY_ACCOUNT_TYPE, null);
2588 result.putString(KEY_AUTHTOKEN, null);
Svet Ganovc1c0d1c2016-09-23 19:15:47 -07002589 result.putBinder(KEY_ACCOUNT_ACCESS_ID, null);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002590 try {
2591 mResponse.onResult(result);
2592 } catch (RemoteException e) {
2593 // this will never happen
2594 }
2595 // we are done
2596 }
sunjianf29d5492017-05-11 15:42:31 -07002597 } else {
2598 mNumAccounts = 1;
2599 Account account = new Account(accountName, accountType);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002600 // have a single account, return an authtoken for it
2601 if (mActivity == null) {
sunjianf29d5492017-05-11 15:42:31 -07002602 mFuture = getAuthToken(account, mAuthTokenType,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002603 false /* notifyAuthFailure */, mMyCallback, mHandler);
2604 } else {
sunjianf29d5492017-05-11 15:42:31 -07002605 mFuture = getAuthToken(account, mAuthTokenType, mLoginOptions,
Fred Quintana33269202009-04-20 16:05:10 -07002606 mActivity, mMyCallback, mHandler);
2607 }
Fred Quintana33269202009-04-20 16:05:10 -07002608 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002609 }}, mHandler);
Fred Quintana33269202009-04-20 16:05:10 -07002610 }
2611
Sandra Kwan0b84b452016-01-20 15:25:42 -08002612 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002613 public void run(AccountManagerFuture<Bundle> future) {
Fred Quintana33269202009-04-20 16:05:10 -07002614 try {
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002615 final Bundle result = future.getResult();
2616 if (mNumAccounts == 0) {
2617 final String accountName = result.getString(KEY_ACCOUNT_NAME);
2618 final String accountType = result.getString(KEY_ACCOUNT_TYPE);
2619 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
2620 setException(new AuthenticatorException("account not in result"));
2621 return;
2622 }
Svet Ganovc1c0d1c2016-09-23 19:15:47 -07002623 final String accessId = result.getString(KEY_ACCOUNT_ACCESS_ID);
2624 final Account account = new Account(accountName, accountType, accessId);
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002625 mNumAccounts = 1;
2626 getAuthToken(account, mAuthTokenType, null /* options */, mActivity,
2627 mMyCallback, mHandler);
2628 return;
2629 }
2630 set(result);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002631 } catch (OperationCanceledException e) {
2632 cancel(true /* mayInterruptIfRUnning */);
2633 } catch (IOException e) {
2634 setException(e);
2635 } catch (AuthenticatorException e) {
2636 setException(e);
Fred Quintana33269202009-04-20 16:05:10 -07002637 }
2638 }
2639 }
2640
Fred Quintana756b7352009-10-21 13:43:10 -07002641 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -08002642 * This convenience helper combines the functionality of {@link #getAccountsByTypeAndFeatures},
2643 * {@link #getAuthToken}, and {@link #addAccount}.
Fred Quintana756b7352009-10-21 13:43:10 -07002644 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002645 * <p>
2646 * This method gets a list of the accounts matching specific type and feature set which are
Dmitry Dementyev85d8a342017-05-02 17:33:17 -07002647 * visible to the caller (see {@link #getAccountsByType} for details);
2648 * if there is exactly one already visible account, it is used; if there are some
Dmitry Dementyev52745472016-12-02 10:27:45 -08002649 * accounts for which user grant visibility, the user is prompted to pick one; if there are
2650 * none, the user is prompted to add one. Finally, an auth token is acquired for the chosen
2651 * account.
Dan Egnor661f0132010-02-19 11:23:00 -08002652 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002653 * <p>
2654 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
2655 * not be used on the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -08002656 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002657 * <p>
2658 * <b>NOTE:</b> If targeting your app to work on API level 22 and before, MANAGE_ACCOUNTS
2659 * permission is needed for those platforms. See docs for this function in API level 22.
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002660 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002661 * @param accountType The account type required (see {@link #getAccountsByType}), must not be
2662 * null
2663 * @param authTokenType The desired auth token type (see {@link #getAuthToken}), must not be
2664 * null
2665 * @param features Required features for the account (see
2666 * {@link #getAccountsByTypeAndFeatures}), may be null or empty
2667 * @param activity The {@link Activity} context to use for launching new sub-Activities to
2668 * prompt to add an account, select an account, and/or enter a password, as necessary;
2669 * used only to call startActivity(); should not be null
2670 * @param addAccountOptions Authenticator-specific options to use for adding new accounts; may
2671 * be null or empty
2672 * @param getAuthTokenOptions Authenticator-specific options to use for getting auth tokens; may
2673 * be null or empty
2674 * @param callback Callback to invoke when the request completes, null for no callback
2675 * @param handler {@link Handler} identifying the callback thread, null for the main thread
2676 * @return An {@link AccountManagerFuture} which resolves to a Bundle with at least the
2677 * following fields:
2678 * <ul>
2679 * <li>{@link #KEY_ACCOUNT_NAME} - the name of the account
2680 * <li>{@link #KEY_ACCOUNT_TYPE} - the type of the account
2681 * <li>{@link #KEY_AUTHTOKEN} - the auth token you wanted
2682 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002683 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002684 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
2685 * <ul>
2686 * <li>{@link AuthenticatorException} if no authenticator was registered for this
2687 * account type or the authenticator failed to respond
2688 * <li>{@link OperationCanceledException} if the operation was canceled for any reason,
2689 * including the user canceling any operation
2690 * <li>{@link IOException} if the authenticator experienced an I/O problem updating
2691 * settings, usually because of network trouble
2692 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002693 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002694 public AccountManagerFuture<Bundle> getAuthTokenByFeatures(
Fred Quintana33269202009-04-20 16:05:10 -07002695 final String accountType, final String authTokenType, final String[] features,
Dan Egnor661f0132010-02-19 11:23:00 -08002696 final Activity activity, final Bundle addAccountOptions,
sunjianf29d5492017-05-11 15:42:31 -07002697 final Bundle getAuthTokenOptions, final AccountManagerCallback<Bundle> callback,
2698 final Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07002699 if (accountType == null) throw new IllegalArgumentException("account type is null");
2700 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002701 final GetAuthTokenByTypeAndFeaturesTask task =
2702 new GetAuthTokenByTypeAndFeaturesTask(accountType, authTokenType, features,
Dan Egnor661f0132010-02-19 11:23:00 -08002703 activity, addAccountOptions, getAuthTokenOptions, callback, handler);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002704 task.start();
2705 return task;
Fred Quintana60307342009-03-24 22:48:12 -07002706 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002707
Fred Quintana1121bb52011-09-14 23:19:35 -07002708 /**
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002709 * Deprecated in favor of {@link #newChooseAccountIntent(Account, List, String[], String,
2710 * String, String[], Bundle)}.
2711 *
Fred Quintana1121bb52011-09-14 23:19:35 -07002712 * Returns an intent to an {@link Activity} that prompts the user to choose from a list of
2713 * accounts.
2714 * The caller will then typically start the activity by calling
Mark Fickettab249e02012-09-05 09:45:47 -04002715 * <code>startActivityForResult(intent, ...);</code>.
Fred Quintana1121bb52011-09-14 23:19:35 -07002716 * <p>
2717 * On success the activity returns a Bundle with the account name and type specified using
2718 * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}.
Dmitry Dementyev85d8a342017-05-02 17:33:17 -07002719 * Chosen account is marked as {@link #VISIBILITY_USER_MANAGED_VISIBLE} to the caller
2720 * (see {@link setAccountVisibility}) and will be returned to it in consequent
2721 * {@link #getAccountsByType}) calls.
Fred Quintana1121bb52011-09-14 23:19:35 -07002722 * <p>
2723 * The most common case is to call this with one account type, e.g.:
2724 * <p>
kmccormickf783ce52013-03-29 14:31:54 -07002725 * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, false, null,
Fred Quintanad88324d2011-09-19 11:43:05 -07002726 * null, null, null);</pre>
Fred Quintana1121bb52011-09-14 23:19:35 -07002727 * @param selectedAccount if specified, indicates that the {@link Account} is the currently
2728 * selected one, according to the caller's definition of selected.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002729 * @param allowableAccounts an optional {@link List} of accounts that are allowed to be
Fred Quintana1121bb52011-09-14 23:19:35 -07002730 * shown. If not specified then this field will not limit the displayed accounts.
2731 * @param allowableAccountTypes an optional string array of account types. These are used
2732 * both to filter the shown accounts and to filter the list of account types that are shown
Simranjit Singh Kohli734f8fb2015-05-22 14:00:32 -07002733 * when adding an account. If not specified then this field will not limit the displayed
2734 * account types when adding an account.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002735 * @param alwaysPromptForAccount boolean that is ignored.
Fred Quintanad88324d2011-09-19 11:43:05 -07002736 * @param descriptionOverrideText if non-null this string is used as the description in the
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002737 * accounts chooser screen rather than the default
Fred Quintanad88324d2011-09-19 11:43:05 -07002738 * @param addAccountAuthTokenType this string is passed as the {@link #addAccount}
2739 * authTokenType parameter
2740 * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount}
2741 * requiredFeatures parameter
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002742 * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options
Fred Quintanad88324d2011-09-19 11:43:05 -07002743 * parameter
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002744 * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow.
Fred Quintana1121bb52011-09-14 23:19:35 -07002745 */
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002746 @Deprecated
2747 static public Intent newChooseAccountIntent(
2748 Account selectedAccount,
Baligh Uddinf2d248d2015-07-10 03:01:47 +00002749 ArrayList<Account> allowableAccounts,
Fred Quintana1121bb52011-09-14 23:19:35 -07002750 String[] allowableAccountTypes,
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002751 boolean alwaysPromptForAccount,
2752 String descriptionOverrideText,
2753 String addAccountAuthTokenType,
2754 String[] addAccountRequiredFeatures,
Fred Quintana1121bb52011-09-14 23:19:35 -07002755 Bundle addAccountOptions) {
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002756 return newChooseAccountIntent(
2757 selectedAccount,
2758 allowableAccounts,
2759 allowableAccountTypes,
2760 descriptionOverrideText,
2761 addAccountAuthTokenType,
2762 addAccountRequiredFeatures,
2763 addAccountOptions);
2764 }
2765
2766 /**
2767 * Returns an intent to an {@link Activity} that prompts the user to choose from a list of
2768 * accounts.
2769 * The caller will then typically start the activity by calling
2770 * <code>startActivityForResult(intent, ...);</code>.
2771 * <p>
2772 * On success the activity returns a Bundle with the account name and type specified using
2773 * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}.
Dmitry Dementyev85d8a342017-05-02 17:33:17 -07002774 * Chosen account is marked as {@link #VISIBILITY_USER_MANAGED_VISIBLE} to the caller
2775 * (see {@link setAccountVisibility}) and will be returned to it in consequent
2776 * {@link #getAccountsByType}) calls.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002777 * <p>
2778 * The most common case is to call this with one account type, e.g.:
2779 * <p>
2780 * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, null, null, null,
2781 * null);</pre>
2782 * @param selectedAccount if specified, indicates that the {@link Account} is the currently
2783 * selected one, according to the caller's definition of selected.
2784 * @param allowableAccounts an optional {@link List} of accounts that are allowed to be
2785 * shown. If not specified then this field will not limit the displayed accounts.
2786 * @param allowableAccountTypes an optional string array of account types. These are used
2787 * both to filter the shown accounts and to filter the list of account types that are shown
2788 * when adding an account. If not specified then this field will not limit the displayed
2789 * account types when adding an account.
2790 * @param descriptionOverrideText if non-null this string is used as the description in the
2791 * accounts chooser screen rather than the default
2792 * @param addAccountAuthTokenType this string is passed as the {@link #addAccount}
2793 * authTokenType parameter
2794 * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount}
2795 * requiredFeatures parameter
2796 * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options
2797 * parameter
2798 * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow.
2799 */
2800 static public Intent newChooseAccountIntent(
2801 Account selectedAccount,
2802 List<Account> allowableAccounts,
2803 String[] allowableAccountTypes,
2804 String descriptionOverrideText,
2805 String addAccountAuthTokenType,
2806 String[] addAccountRequiredFeatures,
2807 Bundle addAccountOptions) {
Fred Quintana1121bb52011-09-14 23:19:35 -07002808 Intent intent = new Intent();
Amith Yamasani12b8e132013-03-14 10:48:07 -07002809 ComponentName componentName = ComponentName.unflattenFromString(
2810 Resources.getSystem().getString(R.string.config_chooseTypeAndAccountActivity));
2811 intent.setClassName(componentName.getPackageName(),
2812 componentName.getClassName());
Fred Quintana1121bb52011-09-14 23:19:35 -07002813 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST,
Craig Lafayette3c9c71d2015-07-14 10:48:46 -04002814 allowableAccounts == null ? null : new ArrayList<Account>(allowableAccounts));
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002815 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
2816 allowableAccountTypes);
Fred Quintana1121bb52011-09-14 23:19:35 -07002817 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,
2818 addAccountOptions);
2819 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_SELECTED_ACCOUNT, selectedAccount);
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002820 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_DESCRIPTION_TEXT_OVERRIDE,
2821 descriptionOverrideText);
2822 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING,
2823 addAccountAuthTokenType);
2824 intent.putExtra(
2825 ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY,
2826 addAccountRequiredFeatures);
Fred Quintana1121bb52011-09-14 23:19:35 -07002827 return intent;
2828 }
2829
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002830 private final HashMap<OnAccountsUpdateListener, Handler> mAccountsUpdatedListeners =
Fred Quintanad9d2f112009-04-23 13:36:27 -07002831 Maps.newHashMap();
2832
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002833 private final HashMap<OnAccountsUpdateListener, Set<String> > mAccountsUpdatedListenersTypes =
2834 Maps.newHashMap();
2835
Fred Quintanad9d2f112009-04-23 13:36:27 -07002836 /**
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002837 * BroadcastReceiver that listens for the ACTION_VISIBLE_ACCOUNTS_CHANGED intent
Fred Quintanad9d2f112009-04-23 13:36:27 -07002838 * so that it can read the updated list of accounts and send them to the listener
2839 * in mAccountsUpdatedListeners.
2840 */
2841 private final BroadcastReceiver mAccountsChangedBroadcastReceiver = new BroadcastReceiver() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002842 @Override
Fred Quintanad9d2f112009-04-23 13:36:27 -07002843 public void onReceive(final Context context, final Intent intent) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002844 final Account[] accounts = getAccounts();
2845 // send the result to the listeners
2846 synchronized (mAccountsUpdatedListeners) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002847 for (Map.Entry<OnAccountsUpdateListener, Handler> entry :
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002848 mAccountsUpdatedListeners.entrySet()) {
2849 postToHandler(entry.getValue(), entry.getKey(), accounts);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002850 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002851 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002852 }
2853 };
2854
2855 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -08002856 * Adds an {@link OnAccountsUpdateListener} to this instance of the {@link AccountManager}. This
2857 * listener will be notified whenever user or AbstractAcccountAuthenticator made changes to
Dmitry Dementyev2ae64092017-01-09 12:48:18 -08002858 * accounts of any type related to the caller. This method is equivalent to
2859 * addOnAccountsUpdatedListener(listener, handler, updateImmediately, null)
Dan Egnor661f0132010-02-19 11:23:00 -08002860 *
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002861 * @see #addOnAccountsUpdatedListener(OnAccountsUpdateListener, Handler, boolean,
Dmitry Dementyev2ae64092017-01-09 12:48:18 -08002862 * String[])
2863 */
2864 public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener,
2865 Handler handler, boolean updateImmediately) {
2866 addOnAccountsUpdatedListener(listener, handler,updateImmediately, null);
2867 }
2868
2869 /**
2870 * Adds an {@link OnAccountsUpdateListener} to this instance of the {@link AccountManager}. This
2871 * listener will be notified whenever user or AbstractAcccountAuthenticator made changes to
2872 * accounts of given types related to the caller -
2873 * either list of accounts returned by {@link #getAccounts()}
2874 * was changed, or new account was added for which user can grant access to the caller.
Dmitry Dementyev52745472016-12-02 10:27:45 -08002875 * <p>
2876 * As long as this listener is present, the AccountManager instance will not be
2877 * garbage-collected, and neither will the {@link Context} used to retrieve it, which may be a
2878 * large Activity instance. To avoid memory leaks, you must remove this listener before then.
2879 * Normally listeners are added in an Activity or Service's {@link Activity#onCreate} and
2880 * removed in {@link Activity#onDestroy}.
Dmitry Dementyev52745472016-12-02 10:27:45 -08002881 * <p>
2882 * It is safe to call this method from the main thread.
Ian Pedowitz845d14d2015-08-04 07:47:37 -07002883 *
Dan Egnor661f0132010-02-19 11:23:00 -08002884 * @param listener The listener to send notifications to
Dmitry Dementyev52745472016-12-02 10:27:45 -08002885 * @param handler {@link Handler} identifying the thread to use for notifications, null for the
2886 * main thread
2887 * @param updateImmediately If true, the listener will be invoked (on the handler thread) right
2888 * away with the current account list
Dmitry Dementyev2ae64092017-01-09 12:48:18 -08002889 * @param accountTypes If set, only changes to accounts of given types will be reported.
Fred Quintanad9d2f112009-04-23 13:36:27 -07002890 * @throws IllegalArgumentException if listener is null
2891 * @throws IllegalStateException if listener was already added
2892 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002893 public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener,
Dmitry Dementyev2ae64092017-01-09 12:48:18 -08002894 Handler handler, boolean updateImmediately, String[] accountTypes) {
Fred Quintanad9d2f112009-04-23 13:36:27 -07002895 if (listener == null) {
2896 throw new IllegalArgumentException("the listener is null");
2897 }
2898 synchronized (mAccountsUpdatedListeners) {
2899 if (mAccountsUpdatedListeners.containsKey(listener)) {
2900 throw new IllegalStateException("this listener is already added");
2901 }
2902 final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty();
2903
2904 mAccountsUpdatedListeners.put(listener, handler);
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002905 if (accountTypes != null) {
2906 mAccountsUpdatedListenersTypes.put(listener,
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002907 new HashSet<String>(Arrays.asList(accountTypes)));
2908 } else {
2909 mAccountsUpdatedListenersTypes.put(listener, null);
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002910 }
Dmitry Dementyev52745472016-12-02 10:27:45 -08002911
Fred Quintanad9d2f112009-04-23 13:36:27 -07002912 if (wasEmpty) {
2913 // Register a broadcast receiver to monitor account changes
2914 IntentFilter intentFilter = new IntentFilter();
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002915 intentFilter.addAction(ACTION_VISIBLE_ACCOUNTS_CHANGED);
Costin Manolacheb6437242009-09-10 16:14:12 -07002916 // To recover from disk-full.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002917 intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002918 mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter);
2919 }
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002920
2921 try {
2922 // Notify AccountManagedService about new receiver.
2923 // The receiver must be unregistered later exactly one time
2924 mService.registerAccountListener(accountTypes, mContext.getOpPackageName());
2925 } catch (RemoteException e) {
2926 throw e.rethrowFromSystemServer();
2927 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002928 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002929 if (updateImmediately) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002930 postToHandler(handler, listener, getAccounts());
Fred Quintanad9d2f112009-04-23 13:36:27 -07002931 }
2932 }
2933
2934 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002935 * Removes an {@link OnAccountsUpdateListener} previously registered with
2936 * {@link #addOnAccountsUpdatedListener}. The listener will no longer
2937 * receive notifications of account changes.
2938 *
2939 * <p>It is safe to call this method from the main thread.
2940 *
2941 * <p>No permission is required to call this method.
2942 *
2943 * @param listener The previously added listener to remove
Fred Quintanad9d2f112009-04-23 13:36:27 -07002944 * @throws IllegalArgumentException if listener is null
2945 * @throws IllegalStateException if listener was not already added
2946 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002947 public void removeOnAccountsUpdatedListener(OnAccountsUpdateListener listener) {
Fred Quintana382601f2010-03-25 12:25:10 -07002948 if (listener == null) throw new IllegalArgumentException("listener is null");
Fred Quintanad9d2f112009-04-23 13:36:27 -07002949 synchronized (mAccountsUpdatedListeners) {
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01002950 if (!mAccountsUpdatedListeners.containsKey(listener)) {
Costin Manolache88a211b2009-10-29 11:30:11 -07002951 Log.e(TAG, "Listener was not previously added");
2952 return;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002953 }
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002954 Set<String> accountTypes = mAccountsUpdatedListenersTypes.get(listener);
2955 String[] accountsArray;
2956 if (accountTypes != null) {
2957 accountsArray = accountTypes.toArray(new String[accountTypes.size()]);
2958 } else {
2959 accountsArray = null;
2960 }
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01002961 mAccountsUpdatedListeners.remove(listener);
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002962 mAccountsUpdatedListenersTypes.remove(listener);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002963 if (mAccountsUpdatedListeners.isEmpty()) {
2964 mContext.unregisterReceiver(mAccountsChangedBroadcastReceiver);
2965 }
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002966 try {
2967 mService.unregisterAccountListener(accountsArray, mContext.getOpPackageName());
2968 } catch (RemoteException e) {
2969 throw e.rethrowFromSystemServer();
2970 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002971 }
2972 }
Sandra Kwan78812282015-11-04 11:19:47 -08002973
2974 /**
2975 * Asks the user to authenticate with an account of a specified type. The
2976 * authenticator for this account type processes this request with the
2977 * appropriate user interface. If the user does elect to authenticate with a
2978 * new account, a bundle of session data for installing the account later is
2979 * returned with optional account password and account status token.
2980 * <p>
2981 * This method may be called from any thread, but the returned
2982 * {@link AccountManagerFuture} must not be used on the main thread.
2983 * <p>
2984 * <p>
2985 * <b>NOTE:</b> The account will not be installed to the device by calling
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002986 * this api alone. #finishSession should be called after this to install the
2987 * account on device.
Sandra Kwan78812282015-11-04 11:19:47 -08002988 *
2989 * @param accountType The type of account to add; must not be null
2990 * @param authTokenType The type of auth token (see {@link #getAuthToken})
2991 * this account will need to be able to generate, null for none
2992 * @param requiredFeatures The features (see {@link #hasFeatures}) this
2993 * account must have, null for none
2994 * @param options Authenticator-specific options for the request, may be
2995 * null or empty
2996 * @param activity The {@link Activity} context to use for launching a new
2997 * authenticator-defined sub-Activity to prompt the user to
2998 * create an account; used only to call startActivity(); if null,
2999 * the prompt will not be launched directly, but the necessary
3000 * {@link Intent} will be returned to the caller instead
3001 * @param callback Callback to invoke when the request completes, null for
3002 * no callback
3003 * @param handler {@link Handler} identifying the callback thread, null for
3004 * the main thread
3005 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
3006 * these fields if activity was specified and user was authenticated
3007 * with an account:
3008 * <ul>
3009 * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for
3010 * adding the the to the device later.
Sandra Kwan78812282015-11-04 11:19:47 -08003011 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
3012 * status of the account
3013 * </ul>
3014 * If no activity was specified, the returned Bundle contains only
3015 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
3016 * actual account creation process. If authenticator doesn't support
3017 * this method, the returned Bundle contains only
3018 * {@link #KEY_ACCOUNT_SESSION_BUNDLE} with encrypted
3019 * {@code options} needed to add account later. If an error
3020 * occurred, {@link AccountManagerFuture#getResult()} throws:
3021 * <ul>
3022 * <li>{@link AuthenticatorException} if no authenticator was
3023 * registered for this account type or the authenticator failed to
3024 * respond
3025 * <li>{@link OperationCanceledException} if the operation was
3026 * canceled for any reason, including the user canceling the
3027 * creation process or adding accounts (of this type) has been
3028 * disabled by policy
3029 * <li>{@link IOException} if the authenticator experienced an I/O
3030 * problem creating a new account, usually because of network
3031 * trouble
3032 * </ul>
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003033 * @see #finishSession
Sandra Kwan78812282015-11-04 11:19:47 -08003034 */
Sandra Kwane68c37e2015-11-12 17:11:49 -08003035 public AccountManagerFuture<Bundle> startAddAccountSession(
3036 final String accountType,
3037 final String authTokenType,
3038 final String[] requiredFeatures,
3039 final Bundle options,
3040 final Activity activity,
3041 AccountManagerCallback<Bundle> callback,
3042 Handler handler) {
Sandra Kwan78812282015-11-04 11:19:47 -08003043 if (accountType == null) throw new IllegalArgumentException("accountType is null");
3044 final Bundle optionsIn = new Bundle();
3045 if (options != null) {
3046 optionsIn.putAll(options);
3047 }
3048 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
3049
3050 return new AmsTask(activity, handler, callback) {
3051 @Override
3052 public void doWork() throws RemoteException {
Sandra Kwane68c37e2015-11-12 17:11:49 -08003053 mService.startAddAccountSession(
3054 mResponse,
3055 accountType,
3056 authTokenType,
3057 requiredFeatures,
3058 activity != null,
3059 optionsIn);
3060 }
3061 }.start();
3062 }
3063
3064 /**
3065 * Asks the user to enter a new password for an account but not updating the
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003066 * saved credentials for the account until {@link #finishSession} is called.
Sandra Kwane68c37e2015-11-12 17:11:49 -08003067 * <p>
3068 * This method may be called from any thread, but the returned
3069 * {@link AccountManagerFuture} must not be used on the main thread.
3070 * <p>
3071 * <b>NOTE:</b> The saved credentials for the account alone will not be
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003072 * updated by calling this API alone. #finishSession should be called after
3073 * this to update local credentials
Sandra Kwane68c37e2015-11-12 17:11:49 -08003074 *
3075 * @param account The account to update credentials for
3076 * @param authTokenType The credentials entered must allow an auth token of
3077 * this type to be created (but no actual auth token is
3078 * returned); may be null
3079 * @param options Authenticator-specific options for the request; may be
3080 * null or empty
3081 * @param activity The {@link Activity} context to use for launching a new
3082 * authenticator-defined sub-Activity to prompt the user to enter
3083 * a password; used only to call startActivity(); if null, the
3084 * prompt will not be launched directly, but the necessary
3085 * {@link Intent} will be returned to the caller instead
3086 * @param callback Callback to invoke when the request completes, null for
3087 * no callback
3088 * @param handler {@link Handler} identifying the callback thread, null for
3089 * the main thread
3090 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
3091 * these fields if an activity was supplied and user was
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003092 * successfully re-authenticated to the account:
Sandra Kwane68c37e2015-11-12 17:11:49 -08003093 * <ul>
3094 * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for
3095 * updating the local credentials on device later.
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003096 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
3097 * status of the account
Sandra Kwane68c37e2015-11-12 17:11:49 -08003098 * </ul>
3099 * If no activity was specified, the returned Bundle contains
3100 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
3101 * password prompt. If an error occurred,
3102 * {@link AccountManagerFuture#getResult()} throws:
3103 * <ul>
3104 * <li>{@link AuthenticatorException} if the authenticator failed to
3105 * respond
3106 * <li>{@link OperationCanceledException} if the operation was
3107 * canceled for any reason, including the user canceling the
3108 * password prompt
3109 * <li>{@link IOException} if the authenticator experienced an I/O
3110 * problem verifying the password, usually because of network
3111 * trouble
3112 * </ul>
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003113 * @see #finishSession
Sandra Kwane68c37e2015-11-12 17:11:49 -08003114 */
3115 public AccountManagerFuture<Bundle> startUpdateCredentialsSession(
3116 final Account account,
3117 final String authTokenType,
3118 final Bundle options,
3119 final Activity activity,
3120 final AccountManagerCallback<Bundle> callback,
3121 final Handler handler) {
3122 if (account == null) {
3123 throw new IllegalArgumentException("account is null");
3124 }
Carlos Valdivia51b651a2016-03-30 13:44:28 -07003125
3126 // Always include the calling package name. This just makes life easier
3127 // down stream.
3128 final Bundle optionsIn = new Bundle();
3129 if (options != null) {
3130 optionsIn.putAll(options);
3131 }
3132 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
3133
Sandra Kwane68c37e2015-11-12 17:11:49 -08003134 return new AmsTask(activity, handler, callback) {
3135 @Override
3136 public void doWork() throws RemoteException {
3137 mService.startUpdateCredentialsSession(
3138 mResponse,
3139 account,
3140 authTokenType,
3141 activity != null,
Carlos Valdivia51b651a2016-03-30 13:44:28 -07003142 optionsIn);
Sandra Kwan78812282015-11-04 11:19:47 -08003143 }
3144 }.start();
3145 }
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003146
3147 /**
3148 * Finishes the session started by {@link #startAddAccountSession} or
3149 * {@link #startUpdateCredentialsSession}. This will either add the account
3150 * to AccountManager or update the local credentials stored.
3151 * <p>
3152 * This method may be called from any thread, but the returned
3153 * {@link AccountManagerFuture} must not be used on the main thread.
3154 *
3155 * @param sessionBundle a {@link Bundle} created by {@link #startAddAccountSession} or
3156 * {@link #startUpdateCredentialsSession}
3157 * @param activity The {@link Activity} context to use for launching a new
3158 * authenticator-defined sub-Activity to prompt the user to
3159 * create an account or reauthenticate existing account; used
3160 * only to call startActivity(); if null, the prompt will not
3161 * be launched directly, but the necessary {@link Intent} will
3162 * be returned to the caller instead
3163 * @param callback Callback to invoke when the request completes, null for
3164 * no callback
3165 * @param handler {@link Handler} identifying the callback thread, null for
3166 * the main thread
3167 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
3168 * these fields if an activity was supplied and an account was added
3169 * to device or local credentials were updated::
3170 * <ul>
3171 * <li>{@link #KEY_ACCOUNT_NAME} - the name of the account created
3172 * <li>{@link #KEY_ACCOUNT_TYPE} - the type of the account
Hongming Jin8b442752016-06-26 10:36:21 -07003173 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
3174 * status of the account
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003175 * </ul>
3176 * If no activity was specified and additional information is needed
3177 * from user, the returned Bundle may contains only
3178 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
3179 * actual account creation process. If an error occurred,
3180 * {@link AccountManagerFuture#getResult()} throws:
3181 * <ul>
3182 * <li>{@link AuthenticatorException} if no authenticator was
3183 * registered for this account type or the authenticator failed to
3184 * respond
3185 * <li>{@link OperationCanceledException} if the operation was
3186 * canceled for any reason, including the user canceling the
3187 * creation process or adding accounts (of this type) has been
3188 * disabled by policy
3189 * <li>{@link IOException} if the authenticator experienced an I/O
3190 * problem creating a new account, usually because of network
3191 * trouble
3192 * </ul>
3193 * @see #startAddAccountSession and #startUpdateCredentialsSession
3194 */
3195 public AccountManagerFuture<Bundle> finishSession(
3196 final Bundle sessionBundle,
3197 final Activity activity,
3198 AccountManagerCallback<Bundle> callback,
3199 Handler handler) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003200 return finishSessionAsUser(
3201 sessionBundle,
3202 activity,
3203 Process.myUserHandle(),
3204 callback,
3205 handler);
3206 }
3207
3208 /**
3209 * @see #finishSession
3210 * @hide
3211 */
3212 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06003213 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
Sandra Kwan0b84b452016-01-20 15:25:42 -08003214 public AccountManagerFuture<Bundle> finishSessionAsUser(
3215 final Bundle sessionBundle,
3216 final Activity activity,
3217 final UserHandle userHandle,
3218 AccountManagerCallback<Bundle> callback,
3219 Handler handler) {
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003220 if (sessionBundle == null) {
3221 throw new IllegalArgumentException("sessionBundle is null");
3222 }
3223
3224 /* Add information required by add account flow */
3225 final Bundle appInfo = new Bundle();
3226 appInfo.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
3227
3228 return new AmsTask(activity, handler, callback) {
3229 @Override
3230 public void doWork() throws RemoteException {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003231 mService.finishSessionAsUser(
3232 mResponse,
3233 sessionBundle,
3234 activity != null,
3235 appInfo,
3236 userHandle.getIdentifier());
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003237 }
3238 }.start();
3239 }
Sandra Kwan390c9d22016-01-12 14:13:37 -08003240
3241 /**
3242 * Checks whether {@link #updateCredentials} or {@link #startUpdateCredentialsSession} should be
3243 * called with respect to the specified account.
3244 * <p>
3245 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
3246 * not be used on the main thread.
3247 *
3248 * @param account The {@link Account} to be checked whether {@link #updateCredentials} or
3249 * {@link #startUpdateCredentialsSession} should be called
3250 * @param statusToken a String of token to check account staus
3251 * @param callback Callback to invoke when the request completes, null for no callback
3252 * @param handler {@link Handler} identifying the callback thread, null for the main thread
3253 * @return An {@link AccountManagerFuture} which resolves to a Boolean, true if the credentials
3254 * of the account should be updated.
Sandra Kwan390c9d22016-01-12 14:13:37 -08003255 */
Sandra Kwan390c9d22016-01-12 14:13:37 -08003256 public AccountManagerFuture<Boolean> isCredentialsUpdateSuggested(
3257 final Account account,
3258 final String statusToken,
3259 AccountManagerCallback<Boolean> callback,
3260 Handler handler) {
3261 if (account == null) {
3262 throw new IllegalArgumentException("account is null");
3263 }
3264
3265 if (TextUtils.isEmpty(statusToken)) {
3266 throw new IllegalArgumentException("status token is empty");
3267 }
3268
3269 return new Future2Task<Boolean>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003270 @Override
Sandra Kwan390c9d22016-01-12 14:13:37 -08003271 public void doWork() throws RemoteException {
3272 mService.isCredentialsUpdateSuggested(
3273 mResponse,
3274 account,
3275 statusToken);
3276 }
Sandra Kwan0b84b452016-01-20 15:25:42 -08003277 @Override
Sandra Kwan390c9d22016-01-12 14:13:37 -08003278 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
3279 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
3280 throw new AuthenticatorException("no result in response");
3281 }
3282 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
3283 }
3284 }.start();
3285 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003286
3287 /**
3288 * Gets whether a given package under a user has access to an account.
3289 * Can be called only from the system UID.
3290 *
3291 * @param account The account for which to check.
3292 * @param packageName The package for which to check.
3293 * @param userHandle The user for which to check.
3294 * @return True if the package can access the account.
3295 *
3296 * @hide
3297 */
3298 public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName,
3299 @NonNull UserHandle userHandle) {
3300 try {
3301 return mService.hasAccountAccess(account, packageName, userHandle);
3302 } catch (RemoteException e) {
3303 throw e.rethrowFromSystemServer();
3304 }
3305 }
3306
3307 /**
3308 * Creates an intent to request access to a given account for a UID.
3309 * The returned intent should be stated for a result where {@link
3310 * Activity#RESULT_OK} result means access was granted whereas {@link
3311 * Activity#RESULT_CANCELED} result means access wasn't granted. Can
3312 * be called only from the system UID.
3313 *
3314 * @param account The account for which to request.
3315 * @param packageName The package name which to request.
3316 * @param userHandle The user for which to request.
3317 * @return The intent to request account access or null if the package
3318 * doesn't exist.
3319 *
3320 * @hide
3321 */
3322 public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account,
3323 @NonNull String packageName, @NonNull UserHandle userHandle) {
3324 try {
3325 return mService.createRequestAccountAccessIntentSenderAsUser(account, packageName,
3326 userHandle);
3327 } catch (RemoteException e) {
3328 throw e.rethrowFromSystemServer();
3329 }
3330 }
Fred Quintana60307342009-03-24 22:48:12 -07003331}