blob: 1eb63e0d44ad51e6505e2e3c679ade1c32d6005b [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
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -070021import android.annotation.NonNull;
Tor Norbye80b530a2015-04-23 16:36:09 -070022import android.annotation.RequiresPermission;
23import android.annotation.Size;
Sandra Kwana578d112015-12-16 16:01:43 -080024import android.annotation.SystemApi;
Fred Quintana60307342009-03-24 22:48:12 -070025import android.app.Activity;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080026import android.content.BroadcastReceiver;
Amith Yamasani12b8e132013-03-14 10:48:07 -070027import android.content.ComponentName;
Fred Quintana60307342009-03-24 22:48:12 -070028import android.content.Context;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080029import android.content.Intent;
Fred Quintanad9d2f112009-04-23 13:36:27 -070030import android.content.IntentFilter;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070031import android.content.IntentSender;
Amith Yamasani12b8e132013-03-14 10:48:07 -070032import android.content.res.Resources;
Costin Manolacheb6437242009-09-10 16:14:12 -070033import android.database.SQLException;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080034import android.os.Build;
Fred Quintanaa698f422009-04-08 19:14:54 -070035import android.os.Bundle;
36import android.os.Handler;
37import android.os.Looper;
Fred Quintana33269202009-04-20 16:05:10 -070038import android.os.Parcelable;
Amith Yamasani2c7bc262012-11-05 16:46:02 -080039import android.os.Process;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080040import android.os.RemoteException;
Amith Yamasani2c7bc262012-11-05 16:46:02 -080041import android.os.UserHandle;
Fred Quintanaf0fd8432010-03-08 12:48:05 -080042import android.text.TextUtils;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080043import android.util.Log;
44
45import com.android.internal.R;
46import com.google.android.collect.Maps;
Fred Quintana60307342009-03-24 22:48:12 -070047
Fred Quintanaa698f422009-04-08 19:14:54 -070048import java.io.IOException;
Fred Quintana1121bb52011-09-14 23:19:35 -070049import java.util.ArrayList;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080050import java.util.HashMap;
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -070051import java.util.List;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080052import java.util.Map;
Fred Quintanaa698f422009-04-08 19:14:54 -070053import java.util.concurrent.Callable;
54import java.util.concurrent.CancellationException;
55import java.util.concurrent.ExecutionException;
56import java.util.concurrent.FutureTask;
Fred Quintanaa698f422009-04-08 19:14:54 -070057import java.util.concurrent.TimeUnit;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080058import java.util.concurrent.TimeoutException;
Fred Quintana60307342009-03-24 22:48:12 -070059
60/**
Dan Egnor661f0132010-02-19 11:23:00 -080061 * This class provides access to a centralized registry of the user's
Dan Egnor8e4378b2010-08-02 18:22:09 -070062 * online accounts. The user enters credentials (username and password) once
63 * per account, granting applications access to online resources with
64 * "one-click" approval.
Fred Quintana60307342009-03-24 22:48:12 -070065 *
Dan Egnor661f0132010-02-19 11:23:00 -080066 * <p>Different online services have different ways of handling accounts and
67 * authentication, so the account manager uses pluggable <em>authenticator</em>
Dan Egnor8e4378b2010-08-02 18:22:09 -070068 * modules for different <em>account types</em>. Authenticators (which may be
69 * written by third parties) handle the actual details of validating account
70 * credentials and storing account information. For example, Google, Facebook,
71 * and Microsoft Exchange each have their own authenticator.
Dan Egnor661f0132010-02-19 11:23:00 -080072 *
73 * <p>Many servers support some notion of an <em>authentication token</em>,
74 * which can be used to authenticate a request to the server without sending
75 * the user's actual password. (Auth tokens are normally created with a
76 * separate request which does include the user's credentials.) AccountManager
Dan Egnor8e4378b2010-08-02 18:22:09 -070077 * can generate auth tokens for applications, so the application doesn't need to
78 * handle passwords directly. Auth tokens are normally reusable and cached by
79 * AccountManager, but must be refreshed periodically. It's the responsibility
80 * of applications to <em>invalidate</em> auth tokens when they stop working so
81 * the AccountManager knows it needs to regenerate them.
Dan Egnor661f0132010-02-19 11:23:00 -080082 *
83 * <p>Applications accessing a server normally go through these steps:
84 *
85 * <ul>
86 * <li>Get an instance of AccountManager using {@link #get(Context)}.
87 *
88 * <li>List the available accounts using {@link #getAccountsByType} or
89 * {@link #getAccountsByTypeAndFeatures}. Normally applications will only
90 * be interested in accounts with one particular <em>type</em>, which
91 * identifies the authenticator. Account <em>features</em> are used to
92 * identify particular account subtypes and capabilities. Both the account
93 * type and features are authenticator-specific strings, and must be known by
94 * the application in coordination with its preferred authenticators.
95 *
96 * <li>Select one or more of the available accounts, possibly by asking the
97 * user for their preference. If no suitable accounts are available,
98 * {@link #addAccount} may be called to prompt the user to create an
99 * account of the appropriate type.
100 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700101 * <li><b>Important:</b> If the application is using a previously remembered
102 * account selection, it must make sure the account is still in the list
103 * of accounts returned by {@link #getAccountsByType}. Requesting an auth token
104 * for an account no longer on the device results in an undefined failure.
105 *
Dan Egnor661f0132010-02-19 11:23:00 -0800106 * <li>Request an auth token for the selected account(s) using one of the
107 * {@link #getAuthToken} methods or related helpers. Refer to the description
108 * of each method for exact usage and error handling details.
109 *
110 * <li>Make the request using the auth token. The form of the auth token,
111 * the format of the request, and the protocol used are all specific to the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700112 * service you are accessing. The application may use whatever network and
113 * protocol libraries are useful.
Dan Egnor661f0132010-02-19 11:23:00 -0800114 *
115 * <li><b>Important:</b> If the request fails with an authentication error,
116 * it could be that a cached auth token is stale and no longer honored by
117 * the server. The application must call {@link #invalidateAuthToken} to remove
118 * the token from the cache, otherwise requests will continue failing! After
119 * invalidating the auth token, immediately go back to the "Request an auth
120 * token" step above. If the process fails the second time, then it can be
121 * treated as a "genuine" authentication failure and the user notified or other
122 * appropriate actions taken.
123 * </ul>
124 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700125 * <p>Some AccountManager methods may need to interact with the user to
Dan Egnor661f0132010-02-19 11:23:00 -0800126 * prompt for credentials, present options, or ask the user to add an account.
127 * The caller may choose whether to allow AccountManager to directly launch the
128 * necessary user interface and wait for the user, or to return an Intent which
129 * the caller may use to launch the interface, or (in some cases) to install a
130 * notification which the user can select at any time to launch the interface.
131 * To have AccountManager launch the interface directly, the caller must supply
132 * the current foreground {@link Activity} context.
133 *
134 * <p>Many AccountManager methods take {@link AccountManagerCallback} and
Dan Egnor8e4378b2010-08-02 18:22:09 -0700135 * {@link Handler} as parameters. These methods return immediately and
Dan Egnor661f0132010-02-19 11:23:00 -0800136 * run asynchronously. If a callback is provided then
137 * {@link AccountManagerCallback#run} will be invoked on the Handler's
138 * thread when the request completes, successfully or not.
Dan Egnor8e4378b2010-08-02 18:22:09 -0700139 * The result is retrieved by calling {@link AccountManagerFuture#getResult()}
140 * on the {@link AccountManagerFuture} returned by the method (and also passed
141 * to the callback). This method waits for the operation to complete (if
142 * necessary) and either returns the result or throws an exception if an error
143 * occurred during the operation. To make the request synchronously, call
Dan Egnor661f0132010-02-19 11:23:00 -0800144 * {@link AccountManagerFuture#getResult()} immediately on receiving the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700145 * future from the method; no callback need be supplied.
Dan Egnor661f0132010-02-19 11:23:00 -0800146 *
147 * <p>Requests which may block, including
148 * {@link AccountManagerFuture#getResult()}, must never be called on
149 * the application's main event thread. These operations throw
150 * {@link IllegalStateException} if they are used on the main thread.
Fred Quintana60307342009-03-24 22:48:12 -0700151 */
152public class AccountManager {
153 private static final String TAG = "AccountManager";
154
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700155 public static final int ERROR_CODE_REMOTE_EXCEPTION = 1;
156 public static final int ERROR_CODE_NETWORK_ERROR = 3;
157 public static final int ERROR_CODE_CANCELED = 4;
158 public static final int ERROR_CODE_INVALID_RESPONSE = 5;
159 public static final int ERROR_CODE_UNSUPPORTED_OPERATION = 6;
160 public static final int ERROR_CODE_BAD_ARGUMENTS = 7;
161 public static final int ERROR_CODE_BAD_REQUEST = 8;
Jatin Lodhia3df7d692013-03-27 10:57:23 -0700162 public static final int ERROR_CODE_BAD_AUTHENTICATION = 9;
Fred Quintana756b7352009-10-21 13:43:10 -0700163
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800164 /** @hide */
165 public static final int ERROR_CODE_USER_RESTRICTED = 100;
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100166 /** @hide */
167 public static final int ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE = 101;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800168
Dan Egnor661f0132010-02-19 11:23:00 -0800169 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700170 * Bundle key used for the {@link String} account name in results
Dan Egnor661f0132010-02-19 11:23:00 -0800171 * from methods which return information about a particular account.
172 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700173 public static final String KEY_ACCOUNT_NAME = "authAccount";
Dan Egnor661f0132010-02-19 11:23:00 -0800174
175 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700176 * Bundle key used for the {@link String} account type in results
Dan Egnor661f0132010-02-19 11:23:00 -0800177 * from methods which return information about a particular account.
178 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700179 public static final String KEY_ACCOUNT_TYPE = "accountType";
Dan Egnor661f0132010-02-19 11:23:00 -0800180
181 /**
Svet Ganovf6d424f12016-09-20 20:18:53 -0700182 * Bundle key used for the {@link IAccountAccessTracker} account access tracker
183 * used for noting the account was accessed when unmarshalled from a parcel.
184 *
185 * @hide
186 */
187 public static final String KEY_ACCOUNT_ACCESS_TRACKER = "accountAccessTracker";
188
189 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700190 * Bundle key used for the auth token value in results
Dan Egnor661f0132010-02-19 11:23:00 -0800191 * from {@link #getAuthToken} and friends.
192 */
193 public static final String KEY_AUTHTOKEN = "authtoken";
194
195 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700196 * Bundle key used for an {@link Intent} in results from methods that
Dan Egnor661f0132010-02-19 11:23:00 -0800197 * may require the caller to interact with the user. The Intent can
198 * be used to start the corresponding user interface activity.
199 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700200 public static final String KEY_INTENT = "intent";
Dan Egnor661f0132010-02-19 11:23:00 -0800201
202 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700203 * Bundle key used to supply the password directly in options to
Dan Egnor661f0132010-02-19 11:23:00 -0800204 * {@link #confirmCredentials}, rather than prompting the user with
205 * the standard password prompt.
206 */
207 public static final String KEY_PASSWORD = "password";
208
209 public static final String KEY_ACCOUNTS = "accounts";
Brian Carlstrom46703b02011-04-06 15:41:29 -0700210
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700211 public static final String KEY_ACCOUNT_AUTHENTICATOR_RESPONSE = "accountAuthenticatorResponse";
212 public static final String KEY_ACCOUNT_MANAGER_RESPONSE = "accountManagerResponse";
Dan Egnor661f0132010-02-19 11:23:00 -0800213 public static final String KEY_AUTHENTICATOR_TYPES = "authenticator_types";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700214 public static final String KEY_AUTH_FAILED_MESSAGE = "authFailedMessage";
215 public static final String KEY_AUTH_TOKEN_LABEL = "authTokenLabelKey";
Dan Egnor661f0132010-02-19 11:23:00 -0800216 public static final String KEY_BOOLEAN_RESULT = "booleanResult";
217 public static final String KEY_ERROR_CODE = "errorCode";
218 public static final String KEY_ERROR_MESSAGE = "errorMessage";
219 public static final String KEY_USERDATA = "userdata";
Costin Manolacheb61e8fb2011-09-08 11:26:09 -0700220
Costin Manolachea40c6302010-12-13 14:50:45 -0800221 /**
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800222 * Bundle key used to supply the last time the credentials of the account
223 * were authenticated successfully. Time is specified in milliseconds since
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700224 * epoch. Associated time is updated on successful authentication of account
225 * on adding account, confirming credentials, or updating credentials.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800226 */
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -0700227 public static final String KEY_LAST_AUTHENTICATED_TIME = "lastAuthenticatedTime";
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800228
229 /**
Costin Manolachea40c6302010-12-13 14:50:45 -0800230 * Authenticators using 'customTokens' option will also get the UID of the
231 * caller
232 */
233 public static final String KEY_CALLER_UID = "callerUid";
234 public static final String KEY_CALLER_PID = "callerPid";
Dan Egnor661f0132010-02-19 11:23:00 -0800235
Costin Manolached6060452011-01-24 16:11:36 -0800236 /**
Fred Quintanaad93a322011-09-08 13:21:01 -0700237 * The Android package of the caller will be set in the options bundle by the
238 * {@link AccountManager} and will be passed to the AccountManagerService and
239 * to the AccountAuthenticators. The uid of the caller will be known by the
240 * AccountManagerService as well as the AccountAuthenticators so they will be able to
241 * verify that the package is consistent with the uid (a uid might be shared by many
242 * packages).
243 */
244 public static final String KEY_ANDROID_PACKAGE_NAME = "androidPackageName";
245
246 /**
Costin Manolached6060452011-01-24 16:11:36 -0800247 * Boolean, if set and 'customTokens' the authenticator is responsible for
248 * notifications.
249 * @hide
250 */
251 public static final String KEY_NOTIFY_ON_FAILURE = "notifyOnAuthFailure";
252
Sandra Kwan78812282015-11-04 11:19:47 -0800253 /**
254 * Bundle key used for a {@link Bundle} in result from
255 * {@link #startAddAccountSession} and friends which returns session data
256 * for installing an account later.
257 */
258 public static final String KEY_ACCOUNT_SESSION_BUNDLE = "accountSessionBundle";
259
260 /**
261 * Bundle key used for the {@link String} account status token in result
262 * from {@link #startAddAccountSession} and friends which returns
263 * information about a particular account.
264 */
265 public static final String KEY_ACCOUNT_STATUS_TOKEN = "accountStatusToken";
266
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700267 public static final String ACTION_AUTHENTICATOR_INTENT =
268 "android.accounts.AccountAuthenticator";
269 public static final String AUTHENTICATOR_META_DATA_NAME =
Dan Egnor661f0132010-02-19 11:23:00 -0800270 "android.accounts.AccountAuthenticator";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700271 public static final String AUTHENTICATOR_ATTRIBUTES_NAME = "account-authenticator";
272
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700273 /**
Svet Ganovf6d424f12016-09-20 20:18:53 -0700274 * Token type for the special case where a UID has access only to an account
275 * but no authenticator specific auth token types.
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700276 *
277 * @hide
278 */
Svet Ganovf6d424f12016-09-20 20:18:53 -0700279 public static final String ACCOUNT_ACCESS_TOKEN_TYPE =
280 "com.android.AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE";
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700281
Fred Quintana60307342009-03-24 22:48:12 -0700282 private final Context mContext;
283 private final IAccountManager mService;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700284 private final Handler mMainHandler;
Dan Egnor661f0132010-02-19 11:23:00 -0800285
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700286 /**
287 * Action sent as a broadcast Intent by the AccountsService
Dan Egnor661f0132010-02-19 11:23:00 -0800288 * when accounts are added, accounts are removed, or an
289 * account's credentials (saved password, etc) are changed.
290 *
291 * @see #addOnAccountsUpdatedListener
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700292 */
293 public static final String LOGIN_ACCOUNTS_CHANGED_ACTION =
294 "android.accounts.LOGIN_ACCOUNTS_CHANGED";
Fred Quintana60307342009-03-24 22:48:12 -0700295
Fred Quintana33269202009-04-20 16:05:10 -0700296 /**
297 * @hide
298 */
Fred Quintana60307342009-03-24 22:48:12 -0700299 public AccountManager(Context context, IAccountManager service) {
300 mContext = context;
301 mService = service;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700302 mMainHandler = new Handler(mContext.getMainLooper());
Fred Quintana60307342009-03-24 22:48:12 -0700303 }
304
Fred Quintana0eabf022009-04-27 15:08:17 -0700305 /**
306 * @hide used for testing only
307 */
308 public AccountManager(Context context, IAccountManager service, Handler handler) {
309 mContext = context;
310 mService = service;
311 mMainHandler = handler;
312 }
313
Fred Quintana756b7352009-10-21 13:43:10 -0700314 /**
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800315 * @hide for internal use only
316 */
317 public static Bundle sanitizeResult(Bundle result) {
Fred Quintana382601f2010-03-25 12:25:10 -0700318 if (result != null) {
319 if (result.containsKey(KEY_AUTHTOKEN)
320 && !TextUtils.isEmpty(result.getString(KEY_AUTHTOKEN))) {
321 final Bundle newResult = new Bundle(result);
322 newResult.putString(KEY_AUTHTOKEN, "<omitted for logging purposes>");
323 return newResult;
324 }
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800325 }
326 return result;
327 }
328
329 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800330 * Gets an AccountManager instance associated with a Context.
331 * The {@link Context} will be used as long as the AccountManager is
332 * active, so make sure to use a {@link Context} whose lifetime is
333 * commensurate with any listeners registered to
334 * {@link #addOnAccountsUpdatedListener} or similar methods.
335 *
336 * <p>It is safe to call this method from the main thread.
337 *
338 * <p>No permission is required to call this method.
339 *
Fred Quintana756b7352009-10-21 13:43:10 -0700340 * @param context The {@link Context} to use when necessary
Dan Egnor661f0132010-02-19 11:23:00 -0800341 * @return An {@link AccountManager} instance
Fred Quintana756b7352009-10-21 13:43:10 -0700342 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700343 public static AccountManager get(Context context) {
Fred Quintana382601f2010-03-25 12:25:10 -0700344 if (context == null) throw new IllegalArgumentException("context is null");
Fred Quintanaa698f422009-04-08 19:14:54 -0700345 return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
346 }
347
Fred Quintana756b7352009-10-21 13:43:10 -0700348 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800349 * Gets the saved password associated with the account.
350 * This is intended for authenticators and related code; applications
351 * should get an auth token instead.
352 *
353 * <p>It is safe to call this method from the main thread.
354 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700355 * <p>This method requires the caller to have a signature match with the
356 * authenticator that owns the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800357 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700358 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
359 * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs for
360 * this function in API level 22.
361 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700362 * @param account The account to query for a password. Must not be {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -0800363 * @return The account's password, null if none or if the account doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700364 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700365 public String getPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -0700366 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintana60307342009-03-24 22:48:12 -0700367 try {
368 return mService.getPassword(account);
369 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700370 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700371 }
372 }
373
Fred Quintana756b7352009-10-21 13:43:10 -0700374 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800375 * Gets the user data named by "key" associated with the account.
376 * This is intended for authenticators and related code to store
377 * arbitrary metadata along with accounts. The meaning of the keys
378 * and values is up to the authenticator for the account.
379 *
380 * <p>It is safe to call this method from the main thread.
381 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700382 * <p>This method requires the caller to have a signature match with the
383 * authenticator that owns the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800384 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700385 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
386 * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs
387 * for this function in API level 22.
388 *
Dan Egnor661f0132010-02-19 11:23:00 -0800389 * @param account The account to query for user data
390 * @return The user data, null if the account or key doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700391 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700392 public String getUserData(final Account account, final String key) {
Fred Quintana382601f2010-03-25 12:25:10 -0700393 if (account == null) throw new IllegalArgumentException("account is null");
394 if (key == null) throw new IllegalArgumentException("key is null");
Fred Quintana60307342009-03-24 22:48:12 -0700395 try {
396 return mService.getUserData(account, key);
397 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700398 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700399 }
400 }
401
Fred Quintana756b7352009-10-21 13:43:10 -0700402 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800403 * Lists the currently registered authenticators.
404 *
405 * <p>It is safe to call this method from the main thread.
406 *
407 * <p>No permission is required to call this method.
408 *
409 * @return An array of {@link AuthenticatorDescription} for every
410 * authenticator known to the AccountManager service. Empty (never
411 * null) if no authenticators are known.
Fred Quintana756b7352009-10-21 13:43:10 -0700412 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700413 public AuthenticatorDescription[] getAuthenticatorTypes() {
Fred Quintanaa698f422009-04-08 19:14:54 -0700414 try {
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100415 return mService.getAuthenticatorTypes(UserHandle.getCallingUserId());
416 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700417 throw e.rethrowFromSystemServer();
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100418 }
419 }
420
421 /**
422 * @hide
423 * Lists the currently registered authenticators for a given user id.
424 *
425 * <p>It is safe to call this method from the main thread.
426 *
427 * <p>The caller has to be in the same user or have the permission
428 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
429 *
430 * @return An array of {@link AuthenticatorDescription} for every
431 * authenticator known to the AccountManager service. Empty (never
432 * null) if no authenticators are known.
433 */
434 public AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) {
435 try {
436 return mService.getAuthenticatorTypes(userId);
Fred Quintanaa698f422009-04-08 19:14:54 -0700437 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700438 throw e.rethrowFromSystemServer();
Fred Quintanaa698f422009-04-08 19:14:54 -0700439 }
440 }
441
Fred Quintana756b7352009-10-21 13:43:10 -0700442 /**
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000443 * Lists all accounts of any type registered on the device.
444 * Equivalent to getAccountsByType(null).
Dan Egnor661f0132010-02-19 11:23:00 -0800445 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000446 * <p>It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800447 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000448 * <p>Clients of this method that have not been granted the
449 * {@link android.Manifest.permission#GET_ACCOUNTS} permission,
450 * will only see those accounts managed by AbstractAccountAuthenticators whose
451 * signature matches the client.
Dan Egnor661f0132010-02-19 11:23:00 -0800452 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000453 * @return An array of {@link Account}, one for each account. Empty
454 * (never null) if no accounts have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700455 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700456 @NonNull
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000457 @RequiresPermission(GET_ACCOUNTS)
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700458 public Account[] getAccounts() {
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000459 try {
460 return mService.getAccounts(null, mContext.getOpPackageName());
461 } catch (RemoteException e) {
462 throw e.rethrowFromSystemServer();
463 }
Fred Quintana60307342009-03-24 22:48:12 -0700464 }
465
Fred Quintana756b7352009-10-21 13:43:10 -0700466 /**
Amith Yamasani27db4682013-03-30 17:07:47 -0700467 * @hide
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000468 * Lists all accounts of any type registered on the device for a given
469 * user id. Equivalent to getAccountsByType(null).
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100470 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000471 * <p>It is safe to call this method from the main thread.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100472 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000473 * <p>Clients of this method that have not been granted the
474 * {@link android.Manifest.permission#GET_ACCOUNTS} permission,
475 * will only see those accounts managed by AbstractAccountAuthenticators whose
476 * signature matches the client.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100477 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000478 * @return An array of {@link Account}, one for each account. Empty
479 * (never null) if no accounts have been added.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100480 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700481 @NonNull
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000482 @RequiresPermission(GET_ACCOUNTS)
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100483 public Account[] getAccountsAsUser(int userId) {
484 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700485 return mService.getAccountsAsUser(null, userId, mContext.getOpPackageName());
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100486 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700487 throw e.rethrowFromSystemServer();
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100488 }
489 }
490
491 /**
492 * @hide
Amith Yamasani27db4682013-03-30 17:07:47 -0700493 * For use by internal activities. Returns the list of accounts that the calling package
494 * is authorized to use, particularly for shared accounts.
495 * @param packageName package name of the calling app.
496 * @param uid the uid of the calling app.
497 * @return the accounts that are available to this package and user.
498 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700499 @NonNull
Amith Yamasani27db4682013-03-30 17:07:47 -0700500 public Account[] getAccountsForPackage(String packageName, int uid) {
501 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700502 return mService.getAccountsForPackage(packageName, uid, mContext.getOpPackageName());
Amith Yamasani27db4682013-03-30 17:07:47 -0700503 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700504 throw re.rethrowFromSystemServer();
Amith Yamasani27db4682013-03-30 17:07:47 -0700505 }
506 }
507
508 /**
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700509 * Returns the accounts visible to the specified package, in an environment where some apps
510 * are not authorized to view all accounts. This method can only be called by system apps.
511 * @param type The type of accounts to return, null to retrieve all accounts
512 * @param packageName The package name of the app for which the accounts are to be returned
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000513 * @return An array of {@link Account}, one per matching account. Empty
514 * (never null) if no accounts of the specified type have been added.
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700515 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700516 @NonNull
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700517 public Account[] getAccountsByTypeForPackage(String type, String packageName) {
518 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700519 return mService.getAccountsByTypeForPackage(type, packageName,
520 mContext.getOpPackageName());
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700521 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700522 throw re.rethrowFromSystemServer();
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700523 }
524 }
525
526 /**
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000527 * Lists all accounts of a particular type. The account type is a
528 * string token corresponding to the authenticator and useful domain
529 * of the account. For example, there are types corresponding to Google
530 * and Facebook. The exact string token to use will be published somewhere
531 * associated with the authenticator in question.
Dan Egnor661f0132010-02-19 11:23:00 -0800532 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000533 * <p>It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800534 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000535 * <p>Clients of this method that have not been granted the
536 * {@link android.Manifest.permission#GET_ACCOUNTS} permission,
537 * will only see those accounts managed by AbstractAccountAuthenticators whose
538 * signature matches the client.
Dan Egnor661f0132010-02-19 11:23:00 -0800539 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000540 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
541 * GET_ACCOUNTS permission is needed for those platforms, irrespective of uid
542 * or signature match. See docs for this function in API level 22.
543 *
544 * @param type The type of accounts to return, null to retrieve all accounts
545 * @return An array of {@link Account}, one per matching account. Empty
546 * (never null) if no accounts of the specified type have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700547 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700548 @NonNull
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000549 @RequiresPermission(GET_ACCOUNTS)
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700550 public Account[] getAccountsByType(String type) {
Amith Yamasani2c7bc262012-11-05 16:46:02 -0800551 return getAccountsByTypeAsUser(type, Process.myUserHandle());
552 }
553
554 /** @hide Same as {@link #getAccountsByType(String)} but for a specific user. */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700555 @NonNull
Amith Yamasani2c7bc262012-11-05 16:46:02 -0800556 public Account[] getAccountsByTypeAsUser(String type, UserHandle userHandle) {
Fred Quintana60307342009-03-24 22:48:12 -0700557 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700558 return mService.getAccountsAsUser(type, userHandle.getIdentifier(),
559 mContext.getOpPackageName());
Fred Quintana60307342009-03-24 22:48:12 -0700560 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700561 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700562 }
563 }
564
Fred Quintana756b7352009-10-21 13:43:10 -0700565 /**
Fred Quintanad9640ec2012-05-23 12:37:00 -0700566 * Change whether or not an app (identified by its uid) is allowed to retrieve an authToken
567 * for an account.
568 * <p>
569 * This is only meant to be used by system activities and is not in the SDK.
570 * @param account The account whose permissions are being modified
571 * @param authTokenType The type of token whose permissions are being modified
572 * @param uid The uid that identifies the app which is being granted or revoked permission.
573 * @param value true is permission is being granted, false for revoked
574 * @hide
575 */
576 public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) {
577 try {
578 mService.updateAppPermission(account, authTokenType, uid, value);
579 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700580 throw e.rethrowFromSystemServer();
Fred Quintanad9640ec2012-05-23 12:37:00 -0700581 }
582 }
583
584 /**
585 * Get the user-friendly label associated with an authenticator's auth token.
586 * @param accountType the type of the authenticator. must not be null.
587 * @param authTokenType the token type. must not be null.
588 * @param callback callback to invoke when the result is available. may be null.
589 * @param handler the handler on which to invoke the callback, or null for the main thread
590 * @return a future containing the label string
591 * @hide
592 */
593 public AccountManagerFuture<String> getAuthTokenLabel(
594 final String accountType, final String authTokenType,
595 AccountManagerCallback<String> callback, Handler handler) {
596 if (accountType == null) throw new IllegalArgumentException("accountType is null");
597 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
598 return new Future2Task<String>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800599 @Override
Fred Quintanad9640ec2012-05-23 12:37:00 -0700600 public void doWork() throws RemoteException {
601 mService.getAuthTokenLabel(mResponse, accountType, authTokenType);
602 }
603
604 @Override
605 public String bundleToResult(Bundle bundle) throws AuthenticatorException {
606 if (!bundle.containsKey(KEY_AUTH_TOKEN_LABEL)) {
607 throw new AuthenticatorException("no result in response");
608 }
609 return bundle.getString(KEY_AUTH_TOKEN_LABEL);
610 }
611 }.start();
612 }
613
614 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800615 * Finds out whether a particular account has all the specified features.
616 * Account features are authenticator-specific string tokens identifying
617 * boolean account properties. For example, features are used to tell
618 * whether Google accounts have a particular service (such as Google
619 * Calendar or Google Talk) enabled. The feature names and their meanings
620 * are published somewhere associated with the authenticator in question.
621 *
622 * <p>This method may be called from any thread, but the returned
623 * {@link AccountManagerFuture} must not be used on the main thread.
624 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000625 * <p>This method requires the caller to hold the permission
626 * {@link android.Manifest.permission#GET_ACCOUNTS} or be a signature
627 * match with the AbstractAccountAuthenticator that manages the account.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800628 *
629 * @param account The {@link Account} to test
Dan Egnor661f0132010-02-19 11:23:00 -0800630 * @param features An array of the account features to check
631 * @param callback Callback to invoke when the request completes,
632 * null for no callback
633 * @param handler {@link Handler} identifying the callback thread,
634 * null for the main thread
635 * @return An {@link AccountManagerFuture} which resolves to a Boolean,
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000636 * true if the account exists and has all of the specified features.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800637 */
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000638 @RequiresPermission(GET_ACCOUNTS)
Fred Quintana3084a6f2010-01-14 18:02:03 -0800639 public AccountManagerFuture<Boolean> hasFeatures(final Account account,
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800640 final String[] features,
641 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700642 if (account == null) throw new IllegalArgumentException("account is null");
643 if (features == null) throw new IllegalArgumentException("features is null");
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800644 return new Future2Task<Boolean>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800645 @Override
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800646 public void doWork() throws RemoteException {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700647 mService.hasFeatures(mResponse, account, features, mContext.getOpPackageName());
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800648 }
Sandra Kwan0b84b452016-01-20 15:25:42 -0800649 @Override
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800650 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
651 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
652 throw new AuthenticatorException("no result in response");
653 }
654 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
655 }
656 }.start();
657 }
658
659 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800660 * Lists all accounts of a type which have certain features. The account
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000661 * type identifies the authenticator (see {@link #getAccountsByType}).
662 * Account features are authenticator-specific string tokens identifying
663 * boolean account properties (see {@link #hasFeatures}).
Fred Quintana756b7352009-10-21 13:43:10 -0700664 *
Dan Egnor661f0132010-02-19 11:23:00 -0800665 * <p>Unlike {@link #getAccountsByType}, this method calls the authenticator,
666 * which may contact the server or do other work to check account features,
667 * so the method returns an {@link AccountManagerFuture}.
Fred Quintanaa698f422009-04-08 19:14:54 -0700668 *
Dan Egnor661f0132010-02-19 11:23:00 -0800669 * <p>This method may be called from any thread, but the returned
670 * {@link AccountManagerFuture} must not be used on the main thread.
Fred Quintana756b7352009-10-21 13:43:10 -0700671 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000672 * <p>Clients of this method that have not been granted the
673 * {@link android.Manifest.permission#GET_ACCOUNTS} permission,
674 * will only see those accounts managed by AbstractAccountAuthenticators whose
675 * signature matches the client.
Fred Quintana756b7352009-10-21 13:43:10 -0700676 *
Dan Egnor661f0132010-02-19 11:23:00 -0800677 * @param type The type of accounts to return, must not be null
678 * @param features An array of the account features to require,
679 * may be null or empty
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000680 *
681 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
682 * GET_ACCOUNTS permission is needed for those platforms, irrespective of uid
683 * or signature match. See docs for this function in API level 22.
684 *
Dan Egnor661f0132010-02-19 11:23:00 -0800685 * @param callback Callback to invoke when the request completes,
686 * null for no callback
687 * @param handler {@link Handler} identifying the callback thread,
688 * null for the main thread
689 * @return An {@link AccountManagerFuture} which resolves to an array of
690 * {@link Account}, one per account of the specified type which
691 * matches the requested features.
Fred Quintana8570f742010-02-18 10:32:54 -0800692 */
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000693 @RequiresPermission(GET_ACCOUNTS)
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700694 public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures(
695 final String type, final String[] features,
696 AccountManagerCallback<Account[]> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700697 if (type == null) throw new IllegalArgumentException("type is null");
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700698 return new Future2Task<Account[]>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800699 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700700 public void doWork() throws RemoteException {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700701 mService.getAccountsByFeatures(mResponse, type, features,
702 mContext.getOpPackageName());
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700703 }
Sandra Kwan0b84b452016-01-20 15:25:42 -0800704 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700705 public Account[] bundleToResult(Bundle bundle) throws AuthenticatorException {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700706 if (!bundle.containsKey(KEY_ACCOUNTS)) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700707 throw new AuthenticatorException("no result in response");
708 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700709 final Parcelable[] parcelables = bundle.getParcelableArray(KEY_ACCOUNTS);
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700710 Account[] descs = new Account[parcelables.length];
711 for (int i = 0; i < parcelables.length; i++) {
712 descs[i] = (Account) parcelables[i];
713 }
714 return descs;
715 }
716 }.start();
717 }
718
Fred Quintana756b7352009-10-21 13:43:10 -0700719 /**
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700720 * Adds an account directly to the AccountManager. Normally used by sign-up
Dan Egnor661f0132010-02-19 11:23:00 -0800721 * wizards associated with authenticators, not directly by applications.
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700722 * <p>Calling this method does not update the last authenticated timestamp,
723 * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
724 * {@link #notifyAccountAuthenticated(Account)} after getting success.
725 * However, if this method is called when it is triggered by addAccount() or
726 * addAccountAsUser() or similar functions, then there is no need to update
727 * timestamp manually as it is updated automatically by framework on
728 * successful completion of the mentioned functions.
Dan Egnor661f0132010-02-19 11:23:00 -0800729 * <p>It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700730 * <p>This method requires the caller to have a signature match with the
731 * authenticator that owns the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800732 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700733 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
734 * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs
735 * for this function in API level 22.
736 *
Dan Egnor661f0132010-02-19 11:23:00 -0800737 * @param account The {@link Account} to add
738 * @param password The password to associate with the account, null for none
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700739 * @param userdata String values to use for the account's userdata, null for
740 * none
Dan Egnor8e4378b2010-08-02 18:22:09 -0700741 * @return True if the account was successfully added, false if the account
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700742 * already exists, the account is null, or another error occurs.
Dan Egnor661f0132010-02-19 11:23:00 -0800743 */
744 public boolean addAccountExplicitly(Account account, String password, Bundle userdata) {
Fred Quintana382601f2010-03-25 12:25:10 -0700745 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800746 try {
Amith Yamasani27db4682013-03-30 17:07:47 -0700747 return mService.addAccountExplicitly(account, password, userdata);
Dan Egnor661f0132010-02-19 11:23:00 -0800748 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700749 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -0800750 }
751 }
752
753 /**
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700754 * Adds an account directly to the AccountManager. Additionally this
755 * makes the Account visible to desired UIDs of applications on the device,
756 * and sends directed broadcasts to these individual applications.
757 * <p>Normally used by sign-up wizards associated with authenticators, not
758 * directly by applications.
759 * <p>Calling this method does not update the last authenticated timestamp,
760 * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
761 * {@link #notifyAccountAuthenticated(Account)} after getting success.
762 * <p>It is safe to call this method from the main thread.
763 * <p>This method requires the caller to have a signature match with the
764 * authenticator that owns the specified account.
765 *
766 * @param account The {@link Account} to add
767 * @param password The password to associate with the account, null for none
768 * @param extras String values to use for the account's userdata, null for
769 * none
770 * @param selectedUids Array of uids whose associated applications can access
771 * this account without any additional user approval.
772 *
773 * @return True if the account was successfully added, false if the account
774 * already exists, the account is null, or another error occurs.
775 */
776 public boolean addAccountExplicitly(Account account, String password, Bundle extras,
777 int[] selectedUids) {
778 if (account == null) throw new IllegalArgumentException("account is null");
779 try {
780 return mService.addAccountExplicitlyWithUid(account, password, extras, selectedUids);
781 } catch (RemoteException e) {
782 throw e.rethrowFromSystemServer();
783 }
784 }
785
786 /**
787 * Returns all UIDs for applications that requested the account type.
788 * <p>This method requires the caller to have a signature match with the authenticator
789 * that owns the specified account.
790 *
791 * @param accountType The account type to be authenticated.
792 *
793 * @return array of all UIDs that support accounts of this
794 * account type that seek approval (to be used to know which accounts for
795 * the authenticator to include in addAccountExplicitly). Null if none.
796 */
797 public int[] getRequestingUidsForType(String accountType) {
798 try {
799 return mService.getRequestingUidsForType(accountType);
800 } catch (RemoteException re) {
801 throw re.rethrowFromSystemServer();
802 }
803 }
804
805 /**
806 * Gives a certain UID, represented a application, access to an account
807 * <p>This method requires the caller to have a signature match with the authenticator
808 * that owns the specified account.
809 *
810 * @param account Account to make visible.
811 * @param uid The UID of the application to add account access.
812 *
813 * @return True if account made visible to application and was not previously visible.
814 */
815 public boolean makeAccountVisible(Account account, int uid) {
816 try {
817 return mService.makeAccountVisible(account, uid);
818 } catch (RemoteException re) {
819 throw re.rethrowFromSystemServer();
820 }
821 }
822
823 /**
824 * Removes visibility of certain account of a process identified
825 * by a given UID to an application.
826 * This is called by the Authenticator.
827 * <p>This method requires the caller to have a signature match with the authenticator
828 * that owns the specified account.
829 *
830 * @param account Remove visibility of this account..
831 * @param uid The UID of the application to remove account access.
832 *
833 * @return True if application access to account removed and was previously visible.
834 */
835 public boolean removeAccountVisibility(Account account, int uid) {
836 try {
837 return mService.removeAccountVisibility(account, uid);
838 } catch (RemoteException re) {
839 throw re.rethrowFromSystemServer();
840 }
841 }
842
843 /**
844 * Checks visibility of certain account of a process identified
845 * by a given UID. This is called by the Authenticator.
846 * <p>This method requires the caller to have a signature match with the authenticator
847 * that owns the specified account.
848 *
849 * @param account Account to check visibility.
850 * @param uid The UID of the application to check account access.
851 *
852 * @return True if application has access to the account
853 */
854 public boolean isAccountVisible(Account account, int uid) {
855 try {
856 return mService.isAccountVisible(account, uid);
857 } catch (RemoteException re) {
858 throw re.rethrowFromSystemServer();
859 }
860 }
861
862 /**
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -0700863 * Notifies the system that the account has just been authenticated. This
864 * information may be used by other applications to verify the account. This
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800865 * should be called only when the user has entered correct credentials for
866 * the account.
867 * <p>
868 * It is not safe to call this method from the main thread. As such, call it
869 * from another thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700870 * <p>This method requires the caller to have a signature match with the
871 * authenticator that owns the specified account.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800872 *
873 * @param account The {@link Account} to be updated.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700874 * @return boolean {@code true} if the authentication of the account has been successfully
875 * acknowledged. Otherwise {@code false}.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800876 */
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -0700877 public boolean notifyAccountAuthenticated(Account account) {
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800878 if (account == null)
879 throw new IllegalArgumentException("account is null");
880 try {
881 return mService.accountAuthenticated(account);
882 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700883 throw e.rethrowFromSystemServer();
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800884 }
885 }
886
887 /**
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -0700888 * Rename the specified {@link Account}. This is equivalent to removing
889 * the existing account and adding a new renamed account with the old
890 * account's user data.
891 *
892 * <p>It is safe to call this method from the main thread.
893 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700894 * <p>This method requires the caller to have a signature match with the
895 * authenticator that manages the specified account.
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -0700896 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700897 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
898 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
899 * is needed for those platforms. See docs for this function in API level 22.
900 *
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -0700901 * @param account The {@link Account} to rename
902 * @param newName String name to be associated with the account.
903 * @param callback Callback to invoke when the request completes, null for
904 * no callback
905 * @param handler {@link Handler} identifying the callback thread, null for
906 * the main thread
907 * @return An {@link AccountManagerFuture} which resolves to the Account
908 * after the name change. If successful the account's name will be the
909 * specified new name.
910 */
911 public AccountManagerFuture<Account> renameAccount(
912 final Account account,
Tor Norbye80b530a2015-04-23 16:36:09 -0700913 @Size(min = 1) final String newName,
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -0700914 AccountManagerCallback<Account> callback,
915 Handler handler) {
916 if (account == null) throw new IllegalArgumentException("account is null.");
917 if (TextUtils.isEmpty(newName)) {
918 throw new IllegalArgumentException("newName is empty or null.");
919 }
920 return new Future2Task<Account>(handler, callback) {
921 @Override
922 public void doWork() throws RemoteException {
923 mService.renameAccount(mResponse, account, newName);
924 }
925 @Override
926 public Account bundleToResult(Bundle bundle) throws AuthenticatorException {
927 String name = bundle.getString(KEY_ACCOUNT_NAME);
928 String type = bundle.getString(KEY_ACCOUNT_TYPE);
Svet Ganovf6d424f12016-09-20 20:18:53 -0700929 IAccountAccessTracker tracker = IAccountAccessTracker.Stub.asInterface(
930 bundle.getBinder(KEY_ACCOUNT_ACCESS_TRACKER));
931 return new Account(name, type, tracker);
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -0700932 }
933 }.start();
934 }
935
936 /**
937 * Gets the previous name associated with the account or {@code null}, if
938 * none. This is intended so that clients of {@link
939 * #LOGIN_ACCOUNTS_CHANGED_ACTION} broadcasts can determine if an
940 * authenticator has renamed an account.
941 *
942 * <p>It is safe to call this method from the main thread.
943 *
944 * @param account The account to query for a previous name.
945 * @return The account's previous name, null if the account has never been
946 * renamed.
947 */
948 public String getPreviousName(final Account account) {
949 if (account == null) throw new IllegalArgumentException("account is null");
950 try {
951 return mService.getPreviousName(account);
952 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700953 throw e.rethrowFromSystemServer();
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -0700954 }
955 }
956
957 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800958 * Removes an account from the AccountManager. Does nothing if the account
959 * does not exist. Does not delete the account from the server.
960 * The authenticator may have its own policies preventing account
961 * deletion, in which case the account will not be deleted.
962 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700963 * <p>This method requires the caller to have a signature match with the
964 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800965 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700966 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
967 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
968 * this function in API level 22.
969 *
Dan Egnor661f0132010-02-19 11:23:00 -0800970 * @param account The {@link Account} to remove
971 * @param callback Callback to invoke when the request completes,
972 * null for no callback
973 * @param handler {@link Handler} identifying the callback thread,
974 * null for the main thread
975 * @return An {@link AccountManagerFuture} which resolves to a Boolean,
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100976 * true if the account has been successfully removed
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800977 * @deprecated use
978 * {@link #removeAccount(Account, Activity, AccountManagerCallback, Handler)}
979 * instead
Dan Egnor661f0132010-02-19 11:23:00 -0800980 */
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800981 @Deprecated
Dan Egnor661f0132010-02-19 11:23:00 -0800982 public AccountManagerFuture<Boolean> removeAccount(final Account account,
983 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700984 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800985 return new Future2Task<Boolean>(handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700986 @Override
Dan Egnor661f0132010-02-19 11:23:00 -0800987 public void doWork() throws RemoteException {
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800988 mService.removeAccount(mResponse, account, false);
Dan Egnor661f0132010-02-19 11:23:00 -0800989 }
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700990 @Override
Dan Egnor661f0132010-02-19 11:23:00 -0800991 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
992 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
993 throw new AuthenticatorException("no result in response");
994 }
995 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
996 }
997 }.start();
998 }
999
1000 /**
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001001 * Removes an account from the AccountManager. Does nothing if the account
1002 * does not exist. Does not delete the account from the server.
1003 * The authenticator may have its own policies preventing account
1004 * deletion, in which case the account will not be deleted.
1005 *
1006 * <p>This method may be called from any thread, but the returned
1007 * {@link AccountManagerFuture} must not be used on the main thread.
1008 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001009 * <p>This method requires the caller to have a signature match with the
1010 * authenticator that manages the specified account.
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001011 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001012 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1013 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1014 * this function in API level 22.
1015 *
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001016 * @param account The {@link Account} to remove
1017 * @param activity The {@link Activity} context to use for launching a new
1018 * authenticator-defined sub-Activity to prompt the user to delete an
1019 * account; used only to call startActivity(); if null, the prompt
1020 * will not be launched directly, but the {@link Intent} may be
1021 * returned to the caller instead
1022 * @param callback Callback to invoke when the request completes,
1023 * null for no callback
1024 * @param handler {@link Handler} identifying the callback thread,
1025 * null for the main thread
1026 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1027 * {@link #KEY_BOOLEAN_RESULT} if activity was specified and an account
1028 * was removed or if active. If no activity was specified, the returned
1029 * Bundle contains only {@link #KEY_INTENT} with the {@link Intent}
1030 * needed to launch the actual account removal process, if authenticator
1031 * needs the activity launch. If an error occurred,
1032 * {@link AccountManagerFuture#getResult()} throws:
1033 * <ul>
1034 * <li> {@link AuthenticatorException} if no authenticator was registered for
1035 * this account type or the authenticator failed to respond
1036 * <li> {@link OperationCanceledException} if the operation was canceled for
1037 * any reason, including the user canceling the creation process or
1038 * adding accounts (of this type) has been disabled by policy
1039 * </ul>
1040 */
1041 public AccountManagerFuture<Bundle> removeAccount(final Account account,
1042 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
1043 if (account == null) throw new IllegalArgumentException("account is null");
1044 return new AmsTask(activity, handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001045 @Override
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001046 public void doWork() throws RemoteException {
1047 mService.removeAccount(mResponse, account, activity != null);
1048 }
1049 }.start();
1050 }
1051
1052 /**
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001053 * @see #removeAccount(Account, AccountManagerCallback, Handler)
1054 * @hide
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001055 * @deprecated use
1056 * {@link #removeAccountAsUser(Account, Activity, AccountManagerCallback, Handler)}
1057 * instead
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001058 */
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001059 @Deprecated
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001060 public AccountManagerFuture<Boolean> removeAccountAsUser(final Account account,
1061 AccountManagerCallback<Boolean> callback, Handler handler,
1062 final UserHandle userHandle) {
1063 if (account == null) throw new IllegalArgumentException("account is null");
1064 if (userHandle == null) throw new IllegalArgumentException("userHandle is null");
1065 return new Future2Task<Boolean>(handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001066 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001067 public void doWork() throws RemoteException {
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001068 mService.removeAccountAsUser(mResponse, account, false, userHandle.getIdentifier());
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001069 }
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001070 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001071 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1072 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1073 throw new AuthenticatorException("no result in response");
1074 }
1075 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1076 }
1077 }.start();
1078 }
1079
1080 /**
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001081 * @see #removeAccount(Account, Activity, AccountManagerCallback, Handler)
1082 * @hide
1083 */
1084 public AccountManagerFuture<Bundle> removeAccountAsUser(final Account account,
1085 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler,
1086 final UserHandle userHandle) {
1087 if (account == null)
1088 throw new IllegalArgumentException("account is null");
1089 if (userHandle == null)
1090 throw new IllegalArgumentException("userHandle is null");
1091 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001092 @Override
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001093 public void doWork() throws RemoteException {
1094 mService.removeAccountAsUser(mResponse, account, activity != null,
1095 userHandle.getIdentifier());
1096 }
1097 }.start();
1098 }
1099
1100 /**
1101 * Removes an account directly. Normally used by authenticators, not
1102 * directly by applications. Does not delete the account from the server.
1103 * The authenticator may have its own policies preventing account deletion,
1104 * in which case the account will not be deleted.
1105 * <p>
1106 * It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001107 * <p>This method requires the caller to have a signature match with the
1108 * authenticator that manages the specified account.
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001109 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001110 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1111 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1112 * is needed for those platforms. See docs for this function in API level 22.
1113 *
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001114 * @param account The {@link Account} to delete.
1115 * @return True if the account was successfully deleted, false if the
1116 * account did not exist, the account is null, or another error
1117 * occurs.
1118 */
1119 public boolean removeAccountExplicitly(Account account) {
1120 if (account == null) throw new IllegalArgumentException("account is null");
1121 try {
1122 return mService.removeAccountExplicitly(account);
1123 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001124 throw e.rethrowFromSystemServer();
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001125 }
1126 }
1127
1128 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001129 * Removes an auth token from the AccountManager's cache. Does nothing if
1130 * the auth token is not currently in the cache. Applications must call this
1131 * method when the auth token is found to have expired or otherwise become
1132 * invalid for authenticating requests. The AccountManager does not validate
1133 * or expire cached auth tokens otherwise.
1134 *
1135 * <p>It is safe to call this method from the main thread.
1136 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001137 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1138 * MANAGE_ACCOUNTS or USE_CREDENTIALS permission is needed for those
1139 * platforms. See docs for this function in API level 22.
1140 *
Fred Quintanaf35b68f2010-04-01 11:36:00 -07001141 * @param accountType The account type of the auth token to invalidate, must not be null
1142 * @param authToken The auth token to invalidate, may be null
Dan Egnor661f0132010-02-19 11:23:00 -08001143 */
1144 public void invalidateAuthToken(final String accountType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -07001145 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001146 try {
Fred Quintanaf35b68f2010-04-01 11:36:00 -07001147 if (authToken != null) {
1148 mService.invalidateAuthToken(accountType, authToken);
1149 }
Dan Egnor661f0132010-02-19 11:23:00 -08001150 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001151 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001152 }
1153 }
1154
1155 /**
1156 * Gets an auth token from the AccountManager's cache. If no auth
1157 * token is cached for this account, null will be returned -- a new
1158 * auth token will not be generated, and the server will not be contacted.
1159 * Intended for use by the authenticator, not directly by applications.
1160 *
1161 * <p>It is safe to call this method from the main thread.
1162 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001163 * <p>This method requires the caller to have a signature match with the
1164 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001165 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001166 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1167 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1168 * is needed for those platforms. See docs for this function in API level 22.
1169 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001170 * @param account The account for which an auth token is to be fetched. Cannot be {@code null}.
Sandra Kwan0b84b452016-01-20 15:25:42 -08001171 * @param authTokenType The type of auth token to fetch. Cannot be {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -08001172 * @return The cached auth token for this account and type, or null if
1173 * no auth token is cached or the account does not exist.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001174 * @see #getAuthToken
Dan Egnor661f0132010-02-19 11:23:00 -08001175 */
1176 public String peekAuthToken(final Account account, final String authTokenType) {
Fred Quintana382601f2010-03-25 12:25:10 -07001177 if (account == null) throw new IllegalArgumentException("account is null");
1178 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001179 try {
1180 return mService.peekAuthToken(account, authTokenType);
1181 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001182 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001183 }
1184 }
1185
1186 /**
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -07001187 * Sets or forgets a saved password. This modifies the local copy of the
1188 * password used to automatically authenticate the user; it does not change
1189 * the user's account password on the server. Intended for use by the
1190 * authenticator, not directly by applications.
1191 * <p>Calling this method does not update the last authenticated timestamp,
1192 * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
1193 * {@link #notifyAccountAuthenticated(Account)} after getting success.
Dan Egnor661f0132010-02-19 11:23:00 -08001194 * <p>It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001195 * <p>This method requires the caller to have a signature match with the
1196 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001197 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001198 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1199 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1200 * is needed for those platforms. See docs for this function in API level 22.
1201 *
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -07001202 * @param account The account whose password is to be set. Cannot be
1203 * {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -08001204 * @param password The password to set, null to clear the password
1205 */
1206 public void setPassword(final Account account, final String password) {
Fred Quintana382601f2010-03-25 12:25:10 -07001207 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001208 try {
1209 mService.setPassword(account, password);
1210 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001211 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001212 }
1213 }
1214
1215 /**
1216 * Forgets a saved password. This erases the local copy of the password;
1217 * it does not change the user's account password on the server.
1218 * Has the same effect as setPassword(account, null) but requires fewer
1219 * permissions, and may be used by applications or management interfaces
1220 * to "sign out" from an account.
1221 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001222 * <p>This method only successfully clear the account's password when the
1223 * caller has the same signature as the authenticator that owns the
1224 * specified account. Otherwise, this method will silently fail.
Dan Egnor661f0132010-02-19 11:23:00 -08001225 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001226 * <p>It is safe to call this method from the main thread.
Carlos Valdiviadcddc472015-06-11 20:04:04 +00001227 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001228 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1229 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1230 * this function in API level 22.
1231 *
Dan Egnor661f0132010-02-19 11:23:00 -08001232 * @param account The account whose password to clear
1233 */
1234 public void clearPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -07001235 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001236 try {
1237 mService.clearPassword(account);
1238 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001239 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001240 }
1241 }
1242
1243 /**
1244 * Sets one userdata key for an account. Intended by use for the
1245 * authenticator to stash state for itself, not directly by applications.
1246 * The meaning of the keys and values is up to the authenticator.
1247 *
1248 * <p>It is safe to call this method from the main thread.
1249 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001250 * <p>This method requires the caller to have a signature match with the
1251 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001252 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001253 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1254 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1255 * is needed for those platforms. See docs for this function in API level 22.
1256 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001257 * @param account Account whose user data is to be set. Must not be {@code null}.
1258 * @param key String user data key to set. Must not be null
1259 * @param value String value to set, {@code null} to clear this user data key
Dan Egnor661f0132010-02-19 11:23:00 -08001260 */
1261 public void setUserData(final Account account, final String key, final String value) {
Fred Quintana382601f2010-03-25 12:25:10 -07001262 if (account == null) throw new IllegalArgumentException("account is null");
1263 if (key == null) throw new IllegalArgumentException("key is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001264 try {
1265 mService.setUserData(account, key, value);
1266 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001267 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001268 }
1269 }
1270
1271 /**
1272 * Adds an auth token to the AccountManager cache for an account.
1273 * If the account does not exist then this call has no effect.
1274 * Replaces any previous auth token for this account and auth token type.
1275 * Intended for use by the authenticator, not directly by applications.
1276 *
1277 * <p>It is safe to call this method from the main thread.
1278 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001279 * <p>This method requires the caller to have a signature match with the
1280 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001281 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001282 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1283 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1284 * is needed for those platforms. See docs for this function in API level 22.
1285 *
Dan Egnor661f0132010-02-19 11:23:00 -08001286 * @param account The account to set an auth token for
1287 * @param authTokenType The type of the auth token, see {#getAuthToken}
1288 * @param authToken The auth token to add to the cache
1289 */
1290 public void setAuthToken(Account account, final String authTokenType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -07001291 if (account == null) throw new IllegalArgumentException("account is null");
1292 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001293 try {
1294 mService.setAuthToken(account, authTokenType, authToken);
1295 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001296 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001297 }
1298 }
1299
1300 /**
1301 * This convenience helper synchronously gets an auth token with
1302 * {@link #getAuthToken(Account, String, boolean, AccountManagerCallback, Handler)}.
1303 *
1304 * <p>This method may block while a network request completes, and must
1305 * never be made from the main thread.
1306 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001307 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1308 * USE_CREDENTIALS permission is needed for those platforms. See docs for
1309 * this function in API level 22.
1310 *
Dan Egnor661f0132010-02-19 11:23:00 -08001311 * @param account The account to fetch an auth token for
Joe Malinb6a35262013-06-03 09:49:04 -07001312 * @param authTokenType The auth token type, see {@link #getAuthToken getAuthToken()}
Dan Egnor661f0132010-02-19 11:23:00 -08001313 * @param notifyAuthFailure If true, display a notification and return null
1314 * if authentication fails; if false, prompt and wait for the user to
1315 * re-enter correct credentials before returning
1316 * @return An auth token of the specified type for this account, or null
1317 * if authentication fails or none can be fetched.
1318 * @throws AuthenticatorException if the authenticator failed to respond
1319 * @throws OperationCanceledException if the request was canceled for any
1320 * reason, including the user canceling a credential request
1321 * @throws java.io.IOException if the authenticator experienced an I/O problem
1322 * creating a new auth token, usually because of network trouble
1323 */
1324 public String blockingGetAuthToken(Account account, String authTokenType,
1325 boolean notifyAuthFailure)
1326 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana382601f2010-03-25 12:25:10 -07001327 if (account == null) throw new IllegalArgumentException("account is null");
1328 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001329 Bundle bundle = getAuthToken(account, authTokenType, notifyAuthFailure, null /* callback */,
1330 null /* handler */).getResult();
Fred Quintana96580e02010-03-04 13:42:42 -08001331 if (bundle == null) {
1332 // This should never happen, but it does, occasionally. If it does return null to
1333 // signify that we were not able to get the authtoken.
1334 // TODO: remove this when the bug is found that sometimes causes a null bundle to be
1335 // returned
1336 Log.e(TAG, "blockingGetAuthToken: null was returned from getResult() for "
1337 + account + ", authTokenType " + authTokenType);
1338 return null;
1339 }
Dan Egnor661f0132010-02-19 11:23:00 -08001340 return bundle.getString(KEY_AUTHTOKEN);
1341 }
1342
1343 /**
1344 * Gets an auth token of the specified type for a particular account,
1345 * prompting the user for credentials if necessary. This method is
1346 * intended for applications running in the foreground where it makes
1347 * sense to ask the user directly for a password.
1348 *
1349 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -07001350 * type, then it is returned. Otherwise, if a saved password is
1351 * available, it is sent to the server to generate a new auth token.
1352 * Otherwise, the user is prompted to enter a password.
Dan Egnor661f0132010-02-19 11:23:00 -08001353 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001354 * <p>Some authenticators have auth token <em>types</em>, whose value
1355 * is authenticator-dependent. Some services use different token types to
1356 * access different functionality -- for example, Google uses different auth
1357 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -08001358 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001359 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
Sandra Kwan0b84b452016-01-20 15:25:42 -08001360 * USE_CREDENTIALS permission is needed for those platforms. See docs for
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001361 * this function in API level 22.
1362 *
Dan Egnor661f0132010-02-19 11:23:00 -08001363 * <p>This method may be called from any thread, but the returned
1364 * {@link AccountManagerFuture} must not be used on the main thread.
1365 *
Dan Egnor661f0132010-02-19 11:23:00 -08001366 * @param account The account to fetch an auth token for
1367 * @param authTokenType The auth token type, an authenticator-dependent
1368 * string token, must not be null
1369 * @param options Authenticator-specific options for the request,
1370 * may be null or empty
1371 * @param activity The {@link Activity} context to use for launching a new
1372 * authenticator-defined sub-Activity to prompt the user for a password
1373 * if necessary; used only to call startActivity(); must not be null.
1374 * @param callback Callback to invoke when the request completes,
1375 * null for no callback
1376 * @param handler {@link Handler} identifying the callback thread,
1377 * null for the main thread
1378 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1379 * at least the following fields:
Fred Quintana756b7352009-10-21 13:43:10 -07001380 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001381 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1382 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1383 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
Fred Quintana756b7352009-10-21 13:43:10 -07001384 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001385 *
1386 * (Other authenticator-specific values may be returned.) If an auth token
1387 * could not be fetched, {@link AccountManagerFuture#getResult()} throws:
1388 * <ul>
1389 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1390 * <li> {@link OperationCanceledException} if the operation is canceled for
1391 * any reason, incluidng the user canceling a credential request
1392 * <li> {@link IOException} if the authenticator experienced an I/O problem
1393 * creating a new auth token, usually because of network trouble
1394 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -07001395 * If the account is no longer present on the device, the return value is
1396 * authenticator-dependent. The caller should verify the validity of the
1397 * account before requesting an auth token.
Dan Egnor661f0132010-02-19 11:23:00 -08001398 */
1399 public AccountManagerFuture<Bundle> getAuthToken(
1400 final Account account, final String authTokenType, final Bundle options,
1401 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001402 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001403 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001404 final Bundle optionsIn = new Bundle();
1405 if (options != null) {
1406 optionsIn.putAll(options);
1407 }
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001408 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Dan Egnor661f0132010-02-19 11:23:00 -08001409 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001410 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001411 public void doWork() throws RemoteException {
1412 mService.getAuthToken(mResponse, account, authTokenType,
1413 false /* notifyOnAuthFailure */, true /* expectActivityLaunch */,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001414 optionsIn);
Dan Egnor661f0132010-02-19 11:23:00 -08001415 }
1416 }.start();
1417 }
1418
1419 /**
1420 * Gets an auth token of the specified type for a particular account,
1421 * optionally raising a notification if the user must enter credentials.
1422 * This method is intended for background tasks and services where the
1423 * user should not be immediately interrupted with a password prompt.
1424 *
1425 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -07001426 * type, then it is returned. Otherwise, if a saved password is
1427 * available, it is sent to the server to generate a new auth token.
1428 * Otherwise, an {@link Intent} is returned which, when started, will
1429 * prompt the user for a password. If the notifyAuthFailure parameter is
1430 * set, a status bar notification is also created with the same Intent,
Dan Egnor661f0132010-02-19 11:23:00 -08001431 * alerting the user that they need to enter a password at some point.
1432 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001433 * <p>In that case, you may need to wait until the user responds, which
1434 * could take hours or days or forever. When the user does respond and
1435 * supply a new password, the account manager will broadcast the
1436 * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent, which applications can
1437 * use to try again.
Dan Egnor661f0132010-02-19 11:23:00 -08001438 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001439 * <p>If notifyAuthFailure is not set, it is the application's
1440 * responsibility to launch the returned Intent at some point.
1441 * Either way, the result from this call will not wait for user action.
Dan Egnor661f0132010-02-19 11:23:00 -08001442 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001443 * <p>Some authenticators have auth token <em>types</em>, whose value
1444 * is authenticator-dependent. Some services use different token types to
1445 * access different functionality -- for example, Google uses different auth
1446 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -08001447 *
1448 * <p>This method may be called from any thread, but the returned
1449 * {@link AccountManagerFuture} must not be used on the main thread.
1450 *
Dan Egnor661f0132010-02-19 11:23:00 -08001451 * @param account The account to fetch an auth token for
1452 * @param authTokenType The auth token type, an authenticator-dependent
1453 * string token, must not be null
Dan Egnor661f0132010-02-19 11:23:00 -08001454 * @param notifyAuthFailure True to add a notification to prompt the
1455 * user for a password if necessary, false to leave that to the caller
1456 * @param callback Callback to invoke when the request completes,
1457 * null for no callback
1458 * @param handler {@link Handler} identifying the callback thread,
1459 * null for the main thread
1460 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1461 * at least the following fields on success:
1462 * <ul>
1463 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1464 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1465 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
1466 * </ul>
1467 *
1468 * (Other authenticator-specific values may be returned.) If the user
1469 * must enter credentials, the returned Bundle contains only
1470 * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt.
1471 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001472 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001473 * <ul>
1474 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1475 * <li> {@link OperationCanceledException} if the operation is canceled for
1476 * any reason, incluidng the user canceling a credential request
1477 * <li> {@link IOException} if the authenticator experienced an I/O problem
1478 * creating a new auth token, usually because of network trouble
1479 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -07001480 * If the account is no longer present on the device, the return value is
1481 * authenticator-dependent. The caller should verify the validity of the
1482 * account before requesting an auth token.
Fred Quintanaad93a322011-09-08 13:21:01 -07001483 * @deprecated use {@link #getAuthToken(Account, String, android.os.Bundle,
1484 * boolean, AccountManagerCallback, android.os.Handler)} instead
Dan Egnor661f0132010-02-19 11:23:00 -08001485 */
Fred Quintanaad93a322011-09-08 13:21:01 -07001486 @Deprecated
Dan Egnor661f0132010-02-19 11:23:00 -08001487 public AccountManagerFuture<Bundle> getAuthToken(
Jatin Lodhia3df7d692013-03-27 10:57:23 -07001488 final Account account, final String authTokenType,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001489 final boolean notifyAuthFailure,
Dan Egnor661f0132010-02-19 11:23:00 -08001490 AccountManagerCallback<Bundle> callback, Handler handler) {
Jatin Lodhia3df7d692013-03-27 10:57:23 -07001491 return getAuthToken(account, authTokenType, null, notifyAuthFailure, callback,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001492 handler);
Dan Egnor661f0132010-02-19 11:23:00 -08001493 }
1494
1495 /**
Fred Quintanaad93a322011-09-08 13:21:01 -07001496 * Gets an auth token of the specified type for a particular account,
1497 * optionally raising a notification if the user must enter credentials.
1498 * This method is intended for background tasks and services where the
1499 * user should not be immediately interrupted with a password prompt.
1500 *
1501 * <p>If a previously generated auth token is cached for this account and
1502 * type, then it is returned. Otherwise, if a saved password is
1503 * available, it is sent to the server to generate a new auth token.
1504 * Otherwise, an {@link Intent} is returned which, when started, will
1505 * prompt the user for a password. If the notifyAuthFailure parameter is
1506 * set, a status bar notification is also created with the same Intent,
1507 * alerting the user that they need to enter a password at some point.
1508 *
1509 * <p>In that case, you may need to wait until the user responds, which
1510 * could take hours or days or forever. When the user does respond and
1511 * supply a new password, the account manager will broadcast the
1512 * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent, which applications can
1513 * use to try again.
1514 *
1515 * <p>If notifyAuthFailure is not set, it is the application's
1516 * responsibility to launch the returned Intent at some point.
1517 * Either way, the result from this call will not wait for user action.
1518 *
1519 * <p>Some authenticators have auth token <em>types</em>, whose value
1520 * is authenticator-dependent. Some services use different token types to
1521 * access different functionality -- for example, Google uses different auth
1522 * tokens to access Gmail and Google Calendar for the same account.
1523 *
1524 * <p>This method may be called from any thread, but the returned
1525 * {@link AccountManagerFuture} must not be used on the main thread.
1526 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001527 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
Sandra Kwan0b84b452016-01-20 15:25:42 -08001528 * USE_CREDENTIALS permission is needed for those platforms. See docs for
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001529 * this function in API level 22.
1530 *
Fred Quintanaad93a322011-09-08 13:21:01 -07001531 * @param account The account to fetch an auth token for
1532 * @param authTokenType The auth token type, an authenticator-dependent
1533 * string token, must not be null
1534 * @param options Authenticator-specific options for the request,
1535 * may be null or empty
1536 * @param notifyAuthFailure True to add a notification to prompt the
1537 * user for a password if necessary, false to leave that to the caller
1538 * @param callback Callback to invoke when the request completes,
1539 * null for no callback
1540 * @param handler {@link Handler} identifying the callback thread,
1541 * null for the main thread
1542 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1543 * at least the following fields on success:
1544 * <ul>
1545 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1546 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1547 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
1548 * </ul>
1549 *
1550 * (Other authenticator-specific values may be returned.) If the user
1551 * must enter credentials, the returned Bundle contains only
1552 * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt.
1553 *
1554 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
1555 * <ul>
1556 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1557 * <li> {@link OperationCanceledException} if the operation is canceled for
1558 * any reason, incluidng the user canceling a credential request
1559 * <li> {@link IOException} if the authenticator experienced an I/O problem
1560 * creating a new auth token, usually because of network trouble
1561 * </ul>
1562 * If the account is no longer present on the device, the return value is
1563 * authenticator-dependent. The caller should verify the validity of the
1564 * account before requesting an auth token.
1565 */
1566 public AccountManagerFuture<Bundle> getAuthToken(
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001567 final Account account, final String authTokenType, final Bundle options,
1568 final boolean notifyAuthFailure,
Fred Quintanaad93a322011-09-08 13:21:01 -07001569 AccountManagerCallback<Bundle> callback, Handler handler) {
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001570
Fred Quintanaad93a322011-09-08 13:21:01 -07001571 if (account == null) throw new IllegalArgumentException("account is null");
1572 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001573 final Bundle optionsIn = new Bundle();
1574 if (options != null) {
1575 optionsIn.putAll(options);
1576 }
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001577 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Fred Quintanaad93a322011-09-08 13:21:01 -07001578 return new AmsTask(null, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001579 @Override
Fred Quintanaad93a322011-09-08 13:21:01 -07001580 public void doWork() throws RemoteException {
1581 mService.getAuthToken(mResponse, account, authTokenType,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001582 notifyAuthFailure, false /* expectActivityLaunch */, optionsIn);
Fred Quintanaad93a322011-09-08 13:21:01 -07001583 }
1584 }.start();
1585 }
1586
1587 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001588 * Asks the user to add an account of a specified type. The authenticator
1589 * for this account type processes this request with the appropriate user
1590 * interface. If the user does elect to create a new account, the account
1591 * name is returned.
1592 *
1593 * <p>This method may be called from any thread, but the returned
1594 * {@link AccountManagerFuture} must not be used on the main thread.
Sandra Kwan0b84b452016-01-20 15:25:42 -08001595 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001596 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1597 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1598 * this function in API level 22.
Dan Egnor661f0132010-02-19 11:23:00 -08001599 *
Dan Egnor661f0132010-02-19 11:23:00 -08001600 * @param accountType The type of account to add; must not be null
1601 * @param authTokenType The type of auth token (see {@link #getAuthToken})
1602 * this account will need to be able to generate, null for none
1603 * @param requiredFeatures The features (see {@link #hasFeatures}) this
1604 * account must have, null for none
1605 * @param addAccountOptions Authenticator-specific options for the request,
1606 * may be null or empty
1607 * @param activity The {@link Activity} context to use for launching a new
1608 * authenticator-defined sub-Activity to prompt the user to create an
1609 * account; used only to call startActivity(); if null, the prompt
1610 * will not be launched directly, but the necessary {@link Intent}
1611 * will be returned to the caller instead
1612 * @param callback Callback to invoke when the request completes,
1613 * null for no callback
1614 * @param handler {@link Handler} identifying the callback thread,
1615 * null for the main thread
Doug Zongkerff592dc2010-02-23 12:26:33 -08001616 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
Dan Egnor661f0132010-02-19 11:23:00 -08001617 * these fields if activity was specified and an account was created:
1618 * <ul>
1619 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created
1620 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1621 * </ul>
1622 *
1623 * If no activity was specified, the returned Bundle contains only
1624 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Dan Egnor8e4378b2010-08-02 18:22:09 -07001625 * actual account creation process. If an error occurred,
1626 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001627 * <ul>
1628 * <li> {@link AuthenticatorException} if no authenticator was registered for
1629 * this account type or the authenticator failed to respond
1630 * <li> {@link OperationCanceledException} if the operation was canceled for
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001631 * any reason, including the user canceling the creation process or adding accounts
1632 * (of this type) has been disabled by policy
Dan Egnor661f0132010-02-19 11:23:00 -08001633 * <li> {@link IOException} if the authenticator experienced an I/O problem
1634 * creating a new account, usually because of network trouble
1635 * </ul>
1636 */
1637 public AccountManagerFuture<Bundle> addAccount(final String accountType,
1638 final String authTokenType, final String[] requiredFeatures,
1639 final Bundle addAccountOptions,
1640 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001641 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001642 final Bundle optionsIn = new Bundle();
1643 if (addAccountOptions != null) {
1644 optionsIn.putAll(addAccountOptions);
1645 }
1646 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001647
Dan Egnor661f0132010-02-19 11:23:00 -08001648 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001649 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001650 public void doWork() throws RemoteException {
Amith Yamasani27db4682013-03-30 17:07:47 -07001651 mService.addAccount(mResponse, accountType, authTokenType,
Costin Manolachee5847ad2011-09-14 12:52:19 -07001652 requiredFeatures, activity != null, optionsIn);
Dan Egnor661f0132010-02-19 11:23:00 -08001653 }
1654 }.start();
1655 }
1656
1657 /**
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001658 * @see #addAccount(String, String, String[], Bundle, Activity, AccountManagerCallback, Handler)
1659 * @hide
1660 */
1661 public AccountManagerFuture<Bundle> addAccountAsUser(final String accountType,
1662 final String authTokenType, final String[] requiredFeatures,
1663 final Bundle addAccountOptions, final Activity activity,
1664 AccountManagerCallback<Bundle> callback, Handler handler, final UserHandle userHandle) {
1665 if (accountType == null) throw new IllegalArgumentException("accountType is null");
1666 if (userHandle == null) throw new IllegalArgumentException("userHandle is null");
1667 final Bundle optionsIn = new Bundle();
1668 if (addAccountOptions != null) {
1669 optionsIn.putAll(addAccountOptions);
1670 }
1671 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
1672
1673 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001674 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001675 public void doWork() throws RemoteException {
1676 mService.addAccountAsUser(mResponse, accountType, authTokenType,
1677 requiredFeatures, activity != null, optionsIn, userHandle.getIdentifier());
1678 }
1679 }.start();
1680 }
1681
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001682
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001683 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001684 * Adds shared accounts from a parent user to a secondary user. Adding the shared account
Amith Yamasani67df64b2012-12-14 12:09:36 -08001685 * doesn't take effect immediately. When the target user starts up, any pending shared accounts
1686 * are attempted to be copied to the target user from the primary via calls to the
1687 * authenticator.
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001688 * @param parentUser parent user
1689 * @param user target user
Amith Yamasani67df64b2012-12-14 12:09:36 -08001690 * @hide
1691 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001692 public void addSharedAccountsFromParentUser(UserHandle parentUser, UserHandle user) {
Amith Yamasani67df64b2012-12-14 12:09:36 -08001693 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001694 mService.addSharedAccountsFromParentUser(parentUser.getIdentifier(),
1695 user.getIdentifier());
Amith Yamasani67df64b2012-12-14 12:09:36 -08001696 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001697 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001698 }
1699 }
1700
1701 /**
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001702 * Copies an account from one user to another user.
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001703 * @param account the account to copy
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001704 * @param fromUser the user to copy the account from
1705 * @param toUser the target user
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001706 * @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 Boolean indicated wether it
1711 * succeeded.
1712 * @hide
1713 */
1714 public AccountManagerFuture<Boolean> copyAccountToUser(
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001715 final Account account, final UserHandle fromUser, final UserHandle toUser,
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001716 AccountManagerCallback<Boolean> callback, Handler handler) {
1717 if (account == null) throw new IllegalArgumentException("account is null");
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001718 if (toUser == null || fromUser == null) {
1719 throw new IllegalArgumentException("fromUser and toUser cannot be null");
1720 }
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001721
1722 return new Future2Task<Boolean>(handler, callback) {
1723 @Override
1724 public void doWork() throws RemoteException {
1725 mService.copyAccountToUser(
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001726 mResponse, account, fromUser.getIdentifier(), toUser.getIdentifier());
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001727 }
1728 @Override
1729 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1730 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1731 throw new AuthenticatorException("no result in response");
1732 }
1733 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1734 }
1735 }.start();
1736 }
1737
1738 /**
Amith Yamasani67df64b2012-12-14 12:09:36 -08001739 * @hide
1740 * Removes the shared account.
1741 * @param account the account to remove
1742 * @param user the user to remove the account from
1743 * @return
1744 */
1745 public boolean removeSharedAccount(final Account account, UserHandle user) {
1746 try {
1747 boolean val = mService.removeSharedAccountAsUser(account, user.getIdentifier());
1748 return val;
1749 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001750 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001751 }
1752 }
1753
1754 /**
1755 * @hide
1756 * @param user
1757 * @return
1758 */
1759 public Account[] getSharedAccounts(UserHandle user) {
1760 try {
1761 return mService.getSharedAccountsAsUser(user.getIdentifier());
1762 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001763 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001764 }
1765 }
1766
1767 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001768 * Confirms that the user knows the password for an account to make extra
1769 * sure they are the owner of the account. The user-entered password can
1770 * be supplied directly, otherwise the authenticator for this account type
1771 * prompts the user with the appropriate interface. This method is
1772 * intended for applications which want extra assurance; for example, the
1773 * phone lock screen uses this to let the user unlock the phone with an
1774 * account password if they forget the lock pattern.
1775 *
1776 * <p>If the user-entered password matches a saved password for this
1777 * account, the request is considered valid; otherwise the authenticator
1778 * verifies the password (usually by contacting the server).
1779 *
1780 * <p>This method may be called from any thread, but the returned
1781 * {@link AccountManagerFuture} must not be used on the main thread.
1782 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001783 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1784 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs
1785 * for this function in API level 22.
1786 *
Dan Egnor661f0132010-02-19 11:23:00 -08001787 * @param account The account to confirm password knowledge for
1788 * @param options Authenticator-specific options for the request;
1789 * if the {@link #KEY_PASSWORD} string field is present, the
1790 * authenticator may use it directly rather than prompting the user;
1791 * may be null or empty
1792 * @param activity The {@link Activity} context to use for launching a new
1793 * authenticator-defined sub-Activity to prompt the user to enter a
1794 * password; used only to call startActivity(); if null, the prompt
1795 * will not be launched directly, but the necessary {@link Intent}
1796 * will be returned to the caller instead
1797 * @param callback Callback to invoke when the request completes,
1798 * null for no callback
1799 * @param handler {@link Handler} identifying the callback thread,
1800 * null for the main thread
1801 * @return An {@link AccountManagerFuture} which resolves to a Bundle
1802 * with these fields if activity or password was supplied and
1803 * the account was successfully verified:
1804 * <ul>
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001805 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account verified
Dan Egnor661f0132010-02-19 11:23:00 -08001806 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1807 * <li> {@link #KEY_BOOLEAN_RESULT} - true to indicate success
1808 * </ul>
1809 *
1810 * If no activity or password was specified, the returned Bundle contains
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001811 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
1812 * password prompt.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001813 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001814 * <p>Also the returning Bundle may contain {@link
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -07001815 * #KEY_LAST_AUTHENTICATED_TIME} indicating the last time the
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001816 * credential was validated/created.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001817 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001818 * If an error occurred,{@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001819 * <ul>
1820 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1821 * <li> {@link OperationCanceledException} if the operation was canceled for
1822 * any reason, including the user canceling the password prompt
1823 * <li> {@link IOException} if the authenticator experienced an I/O problem
1824 * verifying the password, usually because of network trouble
1825 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001826 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001827 public AccountManagerFuture<Bundle> confirmCredentials(final Account account,
1828 final Bundle options,
1829 final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001830 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07001831 final Handler handler) {
Amith Yamasani2c7bc262012-11-05 16:46:02 -08001832 return confirmCredentialsAsUser(account, options, activity, callback, handler,
1833 Process.myUserHandle());
1834 }
1835
1836 /**
1837 * @hide
1838 * Same as {@link #confirmCredentials(Account, Bundle, Activity, AccountManagerCallback, Handler)}
1839 * but for the specified user.
1840 */
1841 public AccountManagerFuture<Bundle> confirmCredentialsAsUser(final Account account,
1842 final Bundle options,
1843 final Activity activity,
1844 final AccountManagerCallback<Bundle> callback,
1845 final Handler handler, UserHandle userHandle) {
Fred Quintana382601f2010-03-25 12:25:10 -07001846 if (account == null) throw new IllegalArgumentException("account is null");
Amith Yamasani2c7bc262012-11-05 16:46:02 -08001847 final int userId = userHandle.getIdentifier();
Fred Quintanaa698f422009-04-08 19:14:54 -07001848 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001849 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07001850 public void doWork() throws RemoteException {
Amith Yamasani2c7bc262012-11-05 16:46:02 -08001851 mService.confirmCredentialsAsUser(mResponse, account, options, activity != null,
1852 userId);
Fred Quintanaa698f422009-04-08 19:14:54 -07001853 }
Fred Quintana33269202009-04-20 16:05:10 -07001854 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07001855 }
1856
Fred Quintana756b7352009-10-21 13:43:10 -07001857 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001858 * Asks the user to enter a new password for an account, updating the
1859 * saved credentials for the account. Normally this happens automatically
1860 * when the server rejects credentials during an auth token fetch, but this
1861 * can be invoked directly to ensure we have the correct credentials stored.
Fred Quintana756b7352009-10-21 13:43:10 -07001862 *
Dan Egnor661f0132010-02-19 11:23:00 -08001863 * <p>This method may be called from any thread, but the returned
1864 * {@link AccountManagerFuture} must not be used on the main thread.
1865 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001866 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1867 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1868 * this function in API level 22.
1869 *
Dan Egnor661f0132010-02-19 11:23:00 -08001870 * @param account The account to update credentials for
1871 * @param authTokenType The credentials entered must allow an auth token
1872 * of this type to be created (but no actual auth token is returned);
1873 * may be null
1874 * @param options Authenticator-specific options for the request;
1875 * may be null or empty
1876 * @param activity The {@link Activity} context to use for launching a new
1877 * authenticator-defined sub-Activity to prompt the user to enter a
1878 * password; used only to call startActivity(); if null, the prompt
1879 * will not be launched directly, but the necessary {@link Intent}
1880 * will be returned to the caller instead
1881 * @param callback Callback to invoke when the request completes,
1882 * null for no callback
1883 * @param handler {@link Handler} identifying the callback thread,
1884 * null for the main thread
1885 * @return An {@link AccountManagerFuture} which resolves to a Bundle
1886 * with these fields if an activity was supplied and the account
1887 * credentials were successfully updated:
Fred Quintana756b7352009-10-21 13:43:10 -07001888 * <ul>
Ian Pedowitz6cc066d2015-08-05 14:23:43 +00001889 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account
Dan Egnor661f0132010-02-19 11:23:00 -08001890 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
Fred Quintana756b7352009-10-21 13:43:10 -07001891 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001892 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001893 * If no activity was specified, the returned Bundle contains
Dan Egnor661f0132010-02-19 11:23:00 -08001894 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001895 * password prompt. If an error occurred,
Dan Egnor8e4378b2010-08-02 18:22:09 -07001896 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001897 * <ul>
1898 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1899 * <li> {@link OperationCanceledException} if the operation was canceled for
1900 * any reason, including the user canceling the password prompt
1901 * <li> {@link IOException} if the authenticator experienced an I/O problem
1902 * verifying the password, usually because of network trouble
1903 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001904 */
1905 public AccountManagerFuture<Bundle> updateCredentials(final Account account,
1906 final String authTokenType,
Fred Quintana31957f12009-10-21 13:43:10 -07001907 final Bundle options, final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001908 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07001909 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001910 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07001911 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001912 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07001913 public void doWork() throws RemoteException {
1914 mService.updateCredentials(mResponse, account, authTokenType, activity != null,
Fred Quintana31957f12009-10-21 13:43:10 -07001915 options);
Fred Quintanaa698f422009-04-08 19:14:54 -07001916 }
Fred Quintana33269202009-04-20 16:05:10 -07001917 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07001918 }
1919
Fred Quintana756b7352009-10-21 13:43:10 -07001920 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001921 * Offers the user an opportunity to change an authenticator's settings.
1922 * These properties are for the authenticator in general, not a particular
1923 * account. Not all authenticators support this method.
Fred Quintana756b7352009-10-21 13:43:10 -07001924 *
Dan Egnor661f0132010-02-19 11:23:00 -08001925 * <p>This method may be called from any thread, but the returned
1926 * {@link AccountManagerFuture} must not be used on the main thread.
1927 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001928 * <p>This method requires the caller to have the same signature as the
1929 * authenticator associated with the specified account type.
Dan Egnor661f0132010-02-19 11:23:00 -08001930 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001931 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1932 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs
1933 * for this function in API level 22.
1934 *
Dan Egnor661f0132010-02-19 11:23:00 -08001935 * @param accountType The account type associated with the authenticator
1936 * to adjust
1937 * @param activity The {@link Activity} context to use for launching a new
1938 * authenticator-defined sub-Activity to adjust authenticator settings;
1939 * used only to call startActivity(); if null, the settings dialog will
1940 * not be launched directly, but the necessary {@link Intent} will be
1941 * returned to the caller instead
1942 * @param callback Callback to invoke when the request completes,
1943 * null for no callback
1944 * @param handler {@link Handler} identifying the callback thread,
1945 * null for the main thread
1946 * @return An {@link AccountManagerFuture} which resolves to a Bundle
1947 * which is empty if properties were edited successfully, or
1948 * if no activity was specified, contains only {@link #KEY_INTENT}
1949 * needed to launch the authenticator's settings dialog.
Dan Egnor8e4378b2010-08-02 18:22:09 -07001950 * If an error occurred, {@link AccountManagerFuture#getResult()}
1951 * throws:
Fred Quintana756b7352009-10-21 13:43:10 -07001952 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001953 * <li> {@link AuthenticatorException} if no authenticator was registered for
1954 * this account type or the authenticator failed to respond
1955 * <li> {@link OperationCanceledException} if the operation was canceled for
1956 * any reason, including the user canceling the settings dialog
1957 * <li> {@link IOException} if the authenticator experienced an I/O problem
1958 * updating settings, usually because of network trouble
Fred Quintana756b7352009-10-21 13:43:10 -07001959 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001960 */
1961 public AccountManagerFuture<Bundle> editProperties(final String accountType,
1962 final Activity activity, final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07001963 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001964 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07001965 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001966 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07001967 public void doWork() throws RemoteException {
1968 mService.editProperties(mResponse, accountType, activity != null);
1969 }
Fred Quintana33269202009-04-20 16:05:10 -07001970 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07001971 }
1972
Amith Yamasani12747872015-12-07 14:19:49 -08001973 /**
1974 * @hide
1975 * Checks if the given account exists on any of the users on the device.
1976 * Only the system process can call this method.
1977 *
1978 * @param account The account to check for existence.
1979 * @return whether any user has this account
1980 */
1981 public boolean someUserHasAccount(@NonNull final Account account) {
1982 try {
1983 return mService.someUserHasAccount(account);
1984 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001985 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001986 }
1987 }
1988
Fred Quintanaa698f422009-04-08 19:14:54 -07001989 private void ensureNotOnMainThread() {
1990 final Looper looper = Looper.myLooper();
1991 if (looper != null && looper == mContext.getMainLooper()) {
Fred Quintana53bd2522010-02-05 15:28:12 -08001992 final IllegalStateException exception = new IllegalStateException(
1993 "calling this from your main thread can lead to deadlock");
1994 Log.e(TAG, "calling this from your main thread can lead to deadlock and/or ANRs",
1995 exception);
Fred Quintana751fdc02010-02-09 14:13:18 -08001996 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.FROYO) {
1997 throw exception;
1998 }
Fred Quintana60307342009-03-24 22:48:12 -07001999 }
2000 }
2001
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002002 private void postToHandler(Handler handler, final AccountManagerCallback<Bundle> callback,
2003 final AccountManagerFuture<Bundle> future) {
Fred Quintanad9d2f112009-04-23 13:36:27 -07002004 handler = handler == null ? mMainHandler : handler;
2005 handler.post(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002006 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002007 public void run() {
2008 callback.run(future);
2009 }
2010 });
2011 }
Fred Quintana60307342009-03-24 22:48:12 -07002012
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002013 private void postToHandler(Handler handler, final OnAccountsUpdateListener listener,
Fred Quintanad9d2f112009-04-23 13:36:27 -07002014 final Account[] accounts) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002015 final Account[] accountsCopy = new Account[accounts.length];
2016 // send a copy to make sure that one doesn't
2017 // change what another sees
2018 System.arraycopy(accounts, 0, accountsCopy, 0, accountsCopy.length);
2019 handler = (handler == null) ? mMainHandler : handler;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002020 handler.post(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002021 @Override
Fred Quintanad9d2f112009-04-23 13:36:27 -07002022 public void run() {
Hiroaki Kuriyama8d3e1592015-10-09 16:30:03 +02002023 synchronized (mAccountsUpdatedListeners) {
2024 try {
2025 if (mAccountsUpdatedListeners.containsKey(listener)) {
2026 listener.onAccountsUpdated(accountsCopy);
2027 }
2028 } catch (SQLException e) {
2029 // Better luck next time. If the problem was disk-full,
2030 // the STORAGE_OK intent will re-trigger the update.
2031 Log.e(TAG, "Can't update accounts", e);
2032 }
Costin Manolacheb6437242009-09-10 16:14:12 -07002033 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002034 }
2035 });
2036 }
2037
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002038 private abstract class AmsTask extends FutureTask<Bundle> implements AccountManagerFuture<Bundle> {
Fred Quintanaa698f422009-04-08 19:14:54 -07002039 final IAccountManagerResponse mResponse;
2040 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002041 final AccountManagerCallback<Bundle> mCallback;
Fred Quintanaa698f422009-04-08 19:14:54 -07002042 final Activity mActivity;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002043 public AmsTask(Activity activity, Handler handler, AccountManagerCallback<Bundle> callback) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002044 super(new Callable<Bundle>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002045 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002046 public Bundle call() throws Exception {
2047 throw new IllegalStateException("this should never be called");
2048 }
2049 });
2050
2051 mHandler = handler;
2052 mCallback = callback;
2053 mActivity = activity;
2054 mResponse = new Response();
Fred Quintana33269202009-04-20 16:05:10 -07002055 }
2056
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002057 public final AccountManagerFuture<Bundle> start() {
2058 try {
2059 doWork();
2060 } catch (RemoteException e) {
2061 setException(e);
2062 }
Fred Quintana33269202009-04-20 16:05:10 -07002063 return this;
Fred Quintana60307342009-03-24 22:48:12 -07002064 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002065
Sandra Kwan0b84b452016-01-20 15:25:42 -08002066 @Override
Fred Quintana96580e02010-03-04 13:42:42 -08002067 protected void set(Bundle bundle) {
2068 // TODO: somehow a null is being set as the result of the Future. Log this
2069 // case to help debug where this is occurring. When this bug is fixed this
2070 // condition statement should be removed.
2071 if (bundle == null) {
2072 Log.e(TAG, "the bundle must not be null", new Exception());
2073 }
2074 super.set(bundle);
2075 }
2076
Fred Quintanaa698f422009-04-08 19:14:54 -07002077 public abstract void doWork() throws RemoteException;
2078
2079 private Bundle internalGetResult(Long timeout, TimeUnit unit)
2080 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08002081 if (!isDone()) {
2082 ensureNotOnMainThread();
2083 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002084 try {
2085 if (timeout == null) {
2086 return get();
2087 } else {
2088 return get(timeout, unit);
2089 }
2090 } catch (CancellationException e) {
2091 throw new OperationCanceledException();
2092 } catch (TimeoutException e) {
2093 // fall through and cancel
2094 } catch (InterruptedException e) {
2095 // fall through and cancel
2096 } catch (ExecutionException e) {
2097 final Throwable cause = e.getCause();
2098 if (cause instanceof IOException) {
2099 throw (IOException) cause;
2100 } else if (cause instanceof UnsupportedOperationException) {
2101 throw new AuthenticatorException(cause);
2102 } else if (cause instanceof AuthenticatorException) {
2103 throw (AuthenticatorException) cause;
2104 } else if (cause instanceof RuntimeException) {
2105 throw (RuntimeException) cause;
2106 } else if (cause instanceof Error) {
2107 throw (Error) cause;
2108 } else {
2109 throw new IllegalStateException(cause);
2110 }
2111 } finally {
2112 cancel(true /* interrupt if running */);
2113 }
2114 throw new OperationCanceledException();
2115 }
2116
Sandra Kwan0b84b452016-01-20 15:25:42 -08002117 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002118 public Bundle getResult()
2119 throws OperationCanceledException, IOException, AuthenticatorException {
2120 return internalGetResult(null, null);
2121 }
2122
Sandra Kwan0b84b452016-01-20 15:25:42 -08002123 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002124 public Bundle getResult(long timeout, TimeUnit unit)
2125 throws OperationCanceledException, IOException, AuthenticatorException {
2126 return internalGetResult(timeout, unit);
2127 }
2128
Sandra Kwan0b84b452016-01-20 15:25:42 -08002129 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002130 protected void done() {
2131 if (mCallback != null) {
2132 postToHandler(mHandler, mCallback, this);
2133 }
2134 }
2135
2136 /** Handles the responses from the AccountManager */
2137 private class Response extends IAccountManagerResponse.Stub {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002138 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002139 public void onResult(Bundle bundle) {
Brian Carlstrom46703b02011-04-06 15:41:29 -07002140 Intent intent = bundle.getParcelable(KEY_INTENT);
Fred Quintanaa698f422009-04-08 19:14:54 -07002141 if (intent != null && mActivity != null) {
2142 // since the user provided an Activity we will silently start intents
2143 // that we see
2144 mActivity.startActivity(intent);
2145 // leave the Future running to wait for the real response to this request
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07002146 } else if (bundle.getBoolean("retry")) {
2147 try {
2148 doWork();
2149 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002150 throw e.rethrowFromSystemServer();
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07002151 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002152 } else {
2153 set(bundle);
2154 }
2155 }
2156
Sandra Kwan0b84b452016-01-20 15:25:42 -08002157 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002158 public void onError(int code, String message) {
Alexandra Gherghina999d3942014-07-03 11:40:08 +01002159 if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED
2160 || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) {
2161 // the authenticator indicated that this request was canceled or we were
2162 // forbidden to fulfill; cancel now
Fred Quintanaa698f422009-04-08 19:14:54 -07002163 cancel(true /* mayInterruptIfRunning */);
2164 return;
2165 }
2166 setException(convertErrorToException(code, message));
2167 }
2168 }
2169
Fred Quintana60307342009-03-24 22:48:12 -07002170 }
2171
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002172 private abstract class BaseFutureTask<T> extends FutureTask<T> {
2173 final public IAccountManagerResponse mResponse;
Fred Quintanaa698f422009-04-08 19:14:54 -07002174 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002175
2176 public BaseFutureTask(Handler handler) {
2177 super(new Callable<T>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002178 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002179 public T call() throws Exception {
Fred Quintanaa698f422009-04-08 19:14:54 -07002180 throw new IllegalStateException("this should never be called");
2181 }
2182 });
Fred Quintanaa698f422009-04-08 19:14:54 -07002183 mHandler = handler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002184 mResponse = new Response();
Fred Quintana60307342009-03-24 22:48:12 -07002185 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002186
2187 public abstract void doWork() throws RemoteException;
2188
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002189 public abstract T bundleToResult(Bundle bundle) throws AuthenticatorException;
Fred Quintanaa698f422009-04-08 19:14:54 -07002190
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002191 protected void postRunnableToHandler(Runnable runnable) {
2192 Handler handler = (mHandler == null) ? mMainHandler : mHandler;
2193 handler.post(runnable);
Fred Quintanaa698f422009-04-08 19:14:54 -07002194 }
2195
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002196 protected void startTask() {
Fred Quintanaa698f422009-04-08 19:14:54 -07002197 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002198 doWork();
2199 } catch (RemoteException e) {
2200 setException(e);
Fred Quintanaa698f422009-04-08 19:14:54 -07002201 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002202 }
2203
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002204 protected class Response extends IAccountManagerResponse.Stub {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002205 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002206 public void onResult(Bundle bundle) {
2207 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002208 T result = bundleToResult(bundle);
2209 if (result == null) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002210 return;
2211 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002212 set(result);
2213 return;
Fred Quintanaa698f422009-04-08 19:14:54 -07002214 } catch (ClassCastException e) {
2215 // we will set the exception below
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002216 } catch (AuthenticatorException e) {
2217 // we will set the exception below
Fred Quintanaa698f422009-04-08 19:14:54 -07002218 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002219 onError(ERROR_CODE_INVALID_RESPONSE, "no result in response");
Fred Quintanaa698f422009-04-08 19:14:54 -07002220 }
2221
Sandra Kwan0b84b452016-01-20 15:25:42 -08002222 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002223 public void onError(int code, String message) {
Alexandra Gherghina999d3942014-07-03 11:40:08 +01002224 if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED
2225 || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) {
2226 // the authenticator indicated that this request was canceled or we were
2227 // forbidden to fulfill; cancel now
Fred Quintanaa698f422009-04-08 19:14:54 -07002228 cancel(true /* mayInterruptIfRunning */);
2229 return;
2230 }
2231 setException(convertErrorToException(code, message));
2232 }
2233 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002234 }
2235
2236 private abstract class Future2Task<T>
2237 extends BaseFutureTask<T> implements AccountManagerFuture<T> {
2238 final AccountManagerCallback<T> mCallback;
2239 public Future2Task(Handler handler, AccountManagerCallback<T> callback) {
2240 super(handler);
2241 mCallback = callback;
2242 }
2243
Sandra Kwan0b84b452016-01-20 15:25:42 -08002244 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002245 protected void done() {
2246 if (mCallback != null) {
2247 postRunnableToHandler(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002248 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002249 public void run() {
2250 mCallback.run(Future2Task.this);
2251 }
2252 });
2253 }
2254 }
2255
2256 public Future2Task<T> start() {
2257 startTask();
2258 return this;
2259 }
2260
2261 private T internalGetResult(Long timeout, TimeUnit unit)
2262 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08002263 if (!isDone()) {
2264 ensureNotOnMainThread();
2265 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002266 try {
2267 if (timeout == null) {
2268 return get();
2269 } else {
2270 return get(timeout, unit);
2271 }
2272 } catch (InterruptedException e) {
2273 // fall through and cancel
2274 } catch (TimeoutException e) {
2275 // fall through and cancel
2276 } catch (CancellationException e) {
2277 // fall through and cancel
2278 } catch (ExecutionException e) {
2279 final Throwable cause = e.getCause();
2280 if (cause instanceof IOException) {
2281 throw (IOException) cause;
2282 } else if (cause instanceof UnsupportedOperationException) {
2283 throw new AuthenticatorException(cause);
2284 } else if (cause instanceof AuthenticatorException) {
2285 throw (AuthenticatorException) cause;
2286 } else if (cause instanceof RuntimeException) {
2287 throw (RuntimeException) cause;
2288 } else if (cause instanceof Error) {
2289 throw (Error) cause;
2290 } else {
2291 throw new IllegalStateException(cause);
2292 }
2293 } finally {
2294 cancel(true /* interrupt if running */);
2295 }
2296 throw new OperationCanceledException();
2297 }
2298
Sandra Kwan0b84b452016-01-20 15:25:42 -08002299 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002300 public T getResult()
2301 throws OperationCanceledException, IOException, AuthenticatorException {
2302 return internalGetResult(null, null);
2303 }
2304
Sandra Kwan0b84b452016-01-20 15:25:42 -08002305 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002306 public T getResult(long timeout, TimeUnit unit)
2307 throws OperationCanceledException, IOException, AuthenticatorException {
2308 return internalGetResult(timeout, unit);
2309 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002310
Fred Quintana60307342009-03-24 22:48:12 -07002311 }
2312
Fred Quintanaa698f422009-04-08 19:14:54 -07002313 private Exception convertErrorToException(int code, String message) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002314 if (code == ERROR_CODE_NETWORK_ERROR) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002315 return new IOException(message);
Fred Quintana60307342009-03-24 22:48:12 -07002316 }
Fred Quintana60307342009-03-24 22:48:12 -07002317
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002318 if (code == ERROR_CODE_UNSUPPORTED_OPERATION) {
Fred Quintana33269202009-04-20 16:05:10 -07002319 return new UnsupportedOperationException(message);
Fred Quintana60307342009-03-24 22:48:12 -07002320 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002321
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002322 if (code == ERROR_CODE_INVALID_RESPONSE) {
Fred Quintana33269202009-04-20 16:05:10 -07002323 return new AuthenticatorException(message);
Fred Quintanaa698f422009-04-08 19:14:54 -07002324 }
2325
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002326 if (code == ERROR_CODE_BAD_ARGUMENTS) {
Fred Quintana33269202009-04-20 16:05:10 -07002327 return new IllegalArgumentException(message);
2328 }
2329
2330 return new AuthenticatorException(message);
2331 }
2332
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002333 private class GetAuthTokenByTypeAndFeaturesTask
2334 extends AmsTask implements AccountManagerCallback<Bundle> {
Fred Quintana33269202009-04-20 16:05:10 -07002335 GetAuthTokenByTypeAndFeaturesTask(final String accountType, final String authTokenType,
2336 final String[] features, Activity activityForPrompting,
2337 final Bundle addAccountOptions, final Bundle loginOptions,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002338 AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07002339 super(activityForPrompting, handler, callback);
2340 if (accountType == null) throw new IllegalArgumentException("account type is null");
2341 mAccountType = accountType;
2342 mAuthTokenType = authTokenType;
2343 mFeatures = features;
2344 mAddAccountOptions = addAccountOptions;
2345 mLoginOptions = loginOptions;
2346 mMyCallback = this;
2347 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002348 volatile AccountManagerFuture<Bundle> mFuture = null;
Fred Quintana33269202009-04-20 16:05:10 -07002349 final String mAccountType;
2350 final String mAuthTokenType;
2351 final String[] mFeatures;
2352 final Bundle mAddAccountOptions;
2353 final Bundle mLoginOptions;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002354 final AccountManagerCallback<Bundle> mMyCallback;
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002355 private volatile int mNumAccounts = 0;
Fred Quintana33269202009-04-20 16:05:10 -07002356
Sandra Kwan0b84b452016-01-20 15:25:42 -08002357 @Override
Fred Quintana33269202009-04-20 16:05:10 -07002358 public void doWork() throws RemoteException {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002359 getAccountsByTypeAndFeatures(mAccountType, mFeatures,
2360 new AccountManagerCallback<Account[]>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002361 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002362 public void run(AccountManagerFuture<Account[]> future) {
2363 Account[] accounts;
Fred Quintana33269202009-04-20 16:05:10 -07002364 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002365 accounts = future.getResult();
2366 } catch (OperationCanceledException e) {
2367 setException(e);
2368 return;
2369 } catch (IOException e) {
2370 setException(e);
2371 return;
2372 } catch (AuthenticatorException e) {
2373 setException(e);
2374 return;
Fred Quintana33269202009-04-20 16:05:10 -07002375 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002376
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002377 mNumAccounts = accounts.length;
2378
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002379 if (accounts.length == 0) {
2380 if (mActivity != null) {
2381 // no accounts, add one now. pretend that the user directly
2382 // made this request
2383 mFuture = addAccount(mAccountType, mAuthTokenType, mFeatures,
2384 mAddAccountOptions, mActivity, mMyCallback, mHandler);
2385 } else {
2386 // send result since we can't prompt to add an account
2387 Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002388 result.putString(KEY_ACCOUNT_NAME, null);
2389 result.putString(KEY_ACCOUNT_TYPE, null);
2390 result.putString(KEY_AUTHTOKEN, null);
Svet Ganovf6d424f12016-09-20 20:18:53 -07002391 result.putBinder(KEY_ACCOUNT_ACCESS_TRACKER, null);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002392 try {
2393 mResponse.onResult(result);
2394 } catch (RemoteException e) {
2395 // this will never happen
2396 }
2397 // we are done
2398 }
2399 } else if (accounts.length == 1) {
2400 // have a single account, return an authtoken for it
2401 if (mActivity == null) {
2402 mFuture = getAuthToken(accounts[0], mAuthTokenType,
2403 false /* notifyAuthFailure */, mMyCallback, mHandler);
2404 } else {
2405 mFuture = getAuthToken(accounts[0],
2406 mAuthTokenType, mLoginOptions,
Fred Quintana33269202009-04-20 16:05:10 -07002407 mActivity, mMyCallback, mHandler);
2408 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002409 } else {
2410 if (mActivity != null) {
2411 IAccountManagerResponse chooseResponse =
2412 new IAccountManagerResponse.Stub() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002413 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002414 public void onResult(Bundle value) throws RemoteException {
2415 Account account = new Account(
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002416 value.getString(KEY_ACCOUNT_NAME),
Svet Ganovf6d424f12016-09-20 20:18:53 -07002417 value.getString(KEY_ACCOUNT_TYPE),
2418 IAccountAccessTracker.Stub.asInterface(
2419 value.getBinder(
2420 KEY_ACCOUNT_ACCESS_TRACKER)));
2421 mFuture = getAuthToken(account, mAuthTokenType,
2422 mLoginOptions, mActivity, mMyCallback,
2423 mHandler);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002424 }
Fred Quintana33269202009-04-20 16:05:10 -07002425
Sandra Kwan0b84b452016-01-20 15:25:42 -08002426 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002427 public void onError(int errorCode, String errorMessage)
2428 throws RemoteException {
2429 mResponse.onError(errorCode, errorMessage);
2430 }
2431 };
2432 // have many accounts, launch the chooser
2433 Intent intent = new Intent();
Amith Yamasani12b8e132013-03-14 10:48:07 -07002434 ComponentName componentName = ComponentName.unflattenFromString(
2435 Resources.getSystem().getString(
2436 R.string.config_chooseAccountActivity));
2437 intent.setClassName(componentName.getPackageName(),
2438 componentName.getClassName());
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002439 intent.putExtra(KEY_ACCOUNTS, accounts);
2440 intent.putExtra(KEY_ACCOUNT_MANAGER_RESPONSE,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002441 new AccountManagerResponse(chooseResponse));
2442 mActivity.startActivity(intent);
2443 // the result will arrive via the IAccountManagerResponse
2444 } else {
2445 // send result since we can't prompt to select an account
2446 Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002447 result.putString(KEY_ACCOUNTS, null);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002448 try {
2449 mResponse.onResult(result);
2450 } catch (RemoteException e) {
2451 // this will never happen
2452 }
2453 // we are done
Fred Quintana33269202009-04-20 16:05:10 -07002454 }
Fred Quintana33269202009-04-20 16:05:10 -07002455 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002456 }}, mHandler);
Fred Quintana33269202009-04-20 16:05:10 -07002457 }
2458
Sandra Kwan0b84b452016-01-20 15:25:42 -08002459 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002460 public void run(AccountManagerFuture<Bundle> future) {
Fred Quintana33269202009-04-20 16:05:10 -07002461 try {
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002462 final Bundle result = future.getResult();
2463 if (mNumAccounts == 0) {
2464 final String accountName = result.getString(KEY_ACCOUNT_NAME);
2465 final String accountType = result.getString(KEY_ACCOUNT_TYPE);
2466 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
2467 setException(new AuthenticatorException("account not in result"));
2468 return;
2469 }
Svet Ganovf6d424f12016-09-20 20:18:53 -07002470 final IAccountAccessTracker tracker = IAccountAccessTracker.Stub.asInterface(
2471 result.getBinder(KEY_ACCOUNT_ACCESS_TRACKER));
2472 final Account account = new Account(accountName, accountType, tracker);
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002473 mNumAccounts = 1;
2474 getAuthToken(account, mAuthTokenType, null /* options */, mActivity,
2475 mMyCallback, mHandler);
2476 return;
2477 }
2478 set(result);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002479 } catch (OperationCanceledException e) {
2480 cancel(true /* mayInterruptIfRUnning */);
2481 } catch (IOException e) {
2482 setException(e);
2483 } catch (AuthenticatorException e) {
2484 setException(e);
Fred Quintana33269202009-04-20 16:05:10 -07002485 }
2486 }
2487 }
2488
Fred Quintana756b7352009-10-21 13:43:10 -07002489 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002490 * This convenience helper combines the functionality of
2491 * {@link #getAccountsByTypeAndFeatures}, {@link #getAuthToken}, and
2492 * {@link #addAccount}.
Fred Quintana756b7352009-10-21 13:43:10 -07002493 *
Dan Egnor661f0132010-02-19 11:23:00 -08002494 * <p>This method gets a list of the accounts matching the
2495 * specified type and feature set; if there is exactly one, it is
2496 * used; if there are more than one, the user is prompted to pick one;
2497 * if there are none, the user is prompted to add one. Finally,
2498 * an auth token is acquired for the chosen account.
2499 *
2500 * <p>This method may be called from any thread, but the returned
2501 * {@link AccountManagerFuture} must not be used on the main thread.
2502 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002503 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
2504 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
2505 * this function in API level 22.
2506 *
Dan Egnor661f0132010-02-19 11:23:00 -08002507 * @param accountType The account type required
Doug Zongkerff592dc2010-02-23 12:26:33 -08002508 * (see {@link #getAccountsByType}), must not be null
Dan Egnor661f0132010-02-19 11:23:00 -08002509 * @param authTokenType The desired auth token type
2510 * (see {@link #getAuthToken}), must not be null
2511 * @param features Required features for the account
2512 * (see {@link #getAccountsByTypeAndFeatures}), may be null or empty
2513 * @param activity The {@link Activity} context to use for launching new
2514 * sub-Activities to prompt to add an account, select an account,
2515 * and/or enter a password, as necessary; used only to call
2516 * startActivity(); should not be null
2517 * @param addAccountOptions Authenticator-specific options to use for
2518 * adding new accounts; may be null or empty
2519 * @param getAuthTokenOptions Authenticator-specific options to use for
2520 * getting auth tokens; may be null or empty
2521 * @param callback Callback to invoke when the request completes,
2522 * null for no callback
2523 * @param handler {@link Handler} identifying the callback thread,
2524 * null for the main thread
2525 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
2526 * at least the following fields:
Fred Quintana756b7352009-10-21 13:43:10 -07002527 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002528 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account
2529 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
2530 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
Fred Quintana756b7352009-10-21 13:43:10 -07002531 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002532 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07002533 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08002534 * <ul>
2535 * <li> {@link AuthenticatorException} if no authenticator was registered for
2536 * this account type or the authenticator failed to respond
2537 * <li> {@link OperationCanceledException} if the operation was canceled for
2538 * any reason, including the user canceling any operation
2539 * <li> {@link IOException} if the authenticator experienced an I/O problem
2540 * updating settings, usually because of network trouble
2541 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002542 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002543 public AccountManagerFuture<Bundle> getAuthTokenByFeatures(
Fred Quintana33269202009-04-20 16:05:10 -07002544 final String accountType, final String authTokenType, final String[] features,
Dan Egnor661f0132010-02-19 11:23:00 -08002545 final Activity activity, final Bundle addAccountOptions,
Fred Quintana31957f12009-10-21 13:43:10 -07002546 final Bundle getAuthTokenOptions,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002547 final AccountManagerCallback<Bundle> callback, final Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07002548 if (accountType == null) throw new IllegalArgumentException("account type is null");
2549 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002550 final GetAuthTokenByTypeAndFeaturesTask task =
2551 new GetAuthTokenByTypeAndFeaturesTask(accountType, authTokenType, features,
Dan Egnor661f0132010-02-19 11:23:00 -08002552 activity, addAccountOptions, getAuthTokenOptions, callback, handler);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002553 task.start();
2554 return task;
Fred Quintana60307342009-03-24 22:48:12 -07002555 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002556
Fred Quintana1121bb52011-09-14 23:19:35 -07002557 /**
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002558 * Deprecated in favor of {@link #newChooseAccountIntent(Account, List, String[], String,
2559 * String, String[], Bundle)}.
2560 *
Fred Quintana1121bb52011-09-14 23:19:35 -07002561 * Returns an intent to an {@link Activity} that prompts the user to choose from a list of
2562 * accounts.
2563 * The caller will then typically start the activity by calling
Mark Fickettab249e02012-09-05 09:45:47 -04002564 * <code>startActivityForResult(intent, ...);</code>.
Fred Quintana1121bb52011-09-14 23:19:35 -07002565 * <p>
2566 * On success the activity returns a Bundle with the account name and type specified using
2567 * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}.
2568 * <p>
2569 * The most common case is to call this with one account type, e.g.:
2570 * <p>
kmccormickf783ce52013-03-29 14:31:54 -07002571 * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, false, null,
Fred Quintanad88324d2011-09-19 11:43:05 -07002572 * null, null, null);</pre>
Fred Quintana1121bb52011-09-14 23:19:35 -07002573 * @param selectedAccount if specified, indicates that the {@link Account} is the currently
2574 * selected one, according to the caller's definition of selected.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002575 * @param allowableAccounts an optional {@link List} of accounts that are allowed to be
Fred Quintana1121bb52011-09-14 23:19:35 -07002576 * shown. If not specified then this field will not limit the displayed accounts.
2577 * @param allowableAccountTypes an optional string array of account types. These are used
2578 * 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 -07002579 * when adding an account. If not specified then this field will not limit the displayed
2580 * account types when adding an account.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002581 * @param alwaysPromptForAccount boolean that is ignored.
Fred Quintanad88324d2011-09-19 11:43:05 -07002582 * @param descriptionOverrideText if non-null this string is used as the description in the
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002583 * accounts chooser screen rather than the default
Fred Quintanad88324d2011-09-19 11:43:05 -07002584 * @param addAccountAuthTokenType this string is passed as the {@link #addAccount}
2585 * authTokenType parameter
2586 * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount}
2587 * requiredFeatures parameter
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002588 * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options
Fred Quintanad88324d2011-09-19 11:43:05 -07002589 * parameter
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002590 * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow.
Fred Quintana1121bb52011-09-14 23:19:35 -07002591 */
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002592 @Deprecated
2593 static public Intent newChooseAccountIntent(
2594 Account selectedAccount,
Baligh Uddinf2d248d2015-07-10 03:01:47 +00002595 ArrayList<Account> allowableAccounts,
Fred Quintana1121bb52011-09-14 23:19:35 -07002596 String[] allowableAccountTypes,
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002597 boolean alwaysPromptForAccount,
2598 String descriptionOverrideText,
2599 String addAccountAuthTokenType,
2600 String[] addAccountRequiredFeatures,
Fred Quintana1121bb52011-09-14 23:19:35 -07002601 Bundle addAccountOptions) {
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002602 return newChooseAccountIntent(
2603 selectedAccount,
2604 allowableAccounts,
2605 allowableAccountTypes,
2606 descriptionOverrideText,
2607 addAccountAuthTokenType,
2608 addAccountRequiredFeatures,
2609 addAccountOptions);
2610 }
2611
2612 /**
2613 * Returns an intent to an {@link Activity} that prompts the user to choose from a list of
2614 * accounts.
2615 * The caller will then typically start the activity by calling
2616 * <code>startActivityForResult(intent, ...);</code>.
2617 * <p>
2618 * On success the activity returns a Bundle with the account name and type specified using
2619 * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}.
2620 * <p>
2621 * The most common case is to call this with one account type, e.g.:
2622 * <p>
2623 * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, null, null, null,
2624 * null);</pre>
2625 * @param selectedAccount if specified, indicates that the {@link Account} is the currently
2626 * selected one, according to the caller's definition of selected.
2627 * @param allowableAccounts an optional {@link List} of accounts that are allowed to be
2628 * shown. If not specified then this field will not limit the displayed accounts.
2629 * @param allowableAccountTypes an optional string array of account types. These are used
2630 * both to filter the shown accounts and to filter the list of account types that are shown
2631 * when adding an account. If not specified then this field will not limit the displayed
2632 * account types when adding an account.
2633 * @param descriptionOverrideText if non-null this string is used as the description in the
2634 * accounts chooser screen rather than the default
2635 * @param addAccountAuthTokenType this string is passed as the {@link #addAccount}
2636 * authTokenType parameter
2637 * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount}
2638 * requiredFeatures parameter
2639 * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options
2640 * parameter
2641 * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow.
2642 */
2643 static public Intent newChooseAccountIntent(
2644 Account selectedAccount,
2645 List<Account> allowableAccounts,
2646 String[] allowableAccountTypes,
2647 String descriptionOverrideText,
2648 String addAccountAuthTokenType,
2649 String[] addAccountRequiredFeatures,
2650 Bundle addAccountOptions) {
Fred Quintana1121bb52011-09-14 23:19:35 -07002651 Intent intent = new Intent();
Amith Yamasani12b8e132013-03-14 10:48:07 -07002652 ComponentName componentName = ComponentName.unflattenFromString(
2653 Resources.getSystem().getString(R.string.config_chooseTypeAndAccountActivity));
2654 intent.setClassName(componentName.getPackageName(),
2655 componentName.getClassName());
Fred Quintana1121bb52011-09-14 23:19:35 -07002656 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST,
Craig Lafayette3c9c71d2015-07-14 10:48:46 -04002657 allowableAccounts == null ? null : new ArrayList<Account>(allowableAccounts));
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002658 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
2659 allowableAccountTypes);
Fred Quintana1121bb52011-09-14 23:19:35 -07002660 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,
2661 addAccountOptions);
2662 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_SELECTED_ACCOUNT, selectedAccount);
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002663 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_DESCRIPTION_TEXT_OVERRIDE,
2664 descriptionOverrideText);
2665 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING,
2666 addAccountAuthTokenType);
2667 intent.putExtra(
2668 ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY,
2669 addAccountRequiredFeatures);
Fred Quintana1121bb52011-09-14 23:19:35 -07002670 return intent;
2671 }
2672
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002673 private final HashMap<OnAccountsUpdateListener, Handler> mAccountsUpdatedListeners =
Fred Quintanad9d2f112009-04-23 13:36:27 -07002674 Maps.newHashMap();
2675
Fred Quintanad9d2f112009-04-23 13:36:27 -07002676 /**
2677 * BroadcastReceiver that listens for the LOGIN_ACCOUNTS_CHANGED_ACTION intent
2678 * so that it can read the updated list of accounts and send them to the listener
2679 * in mAccountsUpdatedListeners.
2680 */
2681 private final BroadcastReceiver mAccountsChangedBroadcastReceiver = new BroadcastReceiver() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002682 @Override
Fred Quintanad9d2f112009-04-23 13:36:27 -07002683 public void onReceive(final Context context, final Intent intent) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002684 final Account[] accounts = getAccounts();
2685 // send the result to the listeners
2686 synchronized (mAccountsUpdatedListeners) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002687 for (Map.Entry<OnAccountsUpdateListener, Handler> entry :
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002688 mAccountsUpdatedListeners.entrySet()) {
2689 postToHandler(entry.getValue(), entry.getKey(), accounts);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002690 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002691 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002692 }
2693 };
2694
2695 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002696 * Adds an {@link OnAccountsUpdateListener} to this instance of the
2697 * {@link AccountManager}. This listener will be notified whenever the
2698 * list of accounts on the device changes.
2699 *
2700 * <p>As long as this listener is present, the AccountManager instance
2701 * will not be garbage-collected, and neither will the {@link Context}
2702 * used to retrieve it, which may be a large Activity instance. To avoid
2703 * memory leaks, you must remove this listener before then. Normally
2704 * listeners are added in an Activity or Service's {@link Activity#onCreate}
2705 * and removed in {@link Activity#onDestroy}.
2706 *
Ian Pedowitz6cc066d2015-08-05 14:23:43 +00002707 * <p>The listener will only be informed of accounts that would be returned
2708 * to the caller via {@link #getAccounts()}. Typically this means that to
2709 * get any accounts, the caller will need to be grated the GET_ACCOUNTS
2710 * permission.
Dan Egnor661f0132010-02-19 11:23:00 -08002711 *
Ian Pedowitz6cc066d2015-08-05 14:23:43 +00002712 * <p>It is safe to call this method from the main thread.
Ian Pedowitz845d14d2015-08-04 07:47:37 -07002713 *
Dan Egnor661f0132010-02-19 11:23:00 -08002714 * @param listener The listener to send notifications to
2715 * @param handler {@link Handler} identifying the thread to use
2716 * for notifications, null for the main thread
2717 * @param updateImmediately If true, the listener will be invoked
2718 * (on the handler thread) right away with the current account list
Fred Quintanad9d2f112009-04-23 13:36:27 -07002719 * @throws IllegalArgumentException if listener is null
2720 * @throws IllegalStateException if listener was already added
2721 */
Tor Norbye80b530a2015-04-23 16:36:09 -07002722 @RequiresPermission(GET_ACCOUNTS)
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002723 public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener,
Fred Quintanad9d2f112009-04-23 13:36:27 -07002724 Handler handler, boolean updateImmediately) {
2725 if (listener == null) {
2726 throw new IllegalArgumentException("the listener is null");
2727 }
2728 synchronized (mAccountsUpdatedListeners) {
2729 if (mAccountsUpdatedListeners.containsKey(listener)) {
2730 throw new IllegalStateException("this listener is already added");
2731 }
2732 final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty();
2733
2734 mAccountsUpdatedListeners.put(listener, handler);
2735
2736 if (wasEmpty) {
2737 // Register a broadcast receiver to monitor account changes
2738 IntentFilter intentFilter = new IntentFilter();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002739 intentFilter.addAction(LOGIN_ACCOUNTS_CHANGED_ACTION);
Costin Manolacheb6437242009-09-10 16:14:12 -07002740 // To recover from disk-full.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002741 intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002742 mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter);
2743 }
2744 }
2745
2746 if (updateImmediately) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002747 postToHandler(handler, listener, getAccounts());
Fred Quintanad9d2f112009-04-23 13:36:27 -07002748 }
2749 }
2750
2751 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002752 * Removes an {@link OnAccountsUpdateListener} previously registered with
2753 * {@link #addOnAccountsUpdatedListener}. The listener will no longer
2754 * receive notifications of account changes.
2755 *
2756 * <p>It is safe to call this method from the main thread.
2757 *
2758 * <p>No permission is required to call this method.
2759 *
2760 * @param listener The previously added listener to remove
Fred Quintanad9d2f112009-04-23 13:36:27 -07002761 * @throws IllegalArgumentException if listener is null
2762 * @throws IllegalStateException if listener was not already added
2763 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002764 public void removeOnAccountsUpdatedListener(OnAccountsUpdateListener listener) {
Fred Quintana382601f2010-03-25 12:25:10 -07002765 if (listener == null) throw new IllegalArgumentException("listener is null");
Fred Quintanad9d2f112009-04-23 13:36:27 -07002766 synchronized (mAccountsUpdatedListeners) {
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01002767 if (!mAccountsUpdatedListeners.containsKey(listener)) {
Costin Manolache88a211b2009-10-29 11:30:11 -07002768 Log.e(TAG, "Listener was not previously added");
2769 return;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002770 }
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01002771 mAccountsUpdatedListeners.remove(listener);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002772 if (mAccountsUpdatedListeners.isEmpty()) {
2773 mContext.unregisterReceiver(mAccountsChangedBroadcastReceiver);
2774 }
2775 }
2776 }
Sandra Kwan78812282015-11-04 11:19:47 -08002777
2778 /**
2779 * Asks the user to authenticate with an account of a specified type. The
2780 * authenticator for this account type processes this request with the
2781 * appropriate user interface. If the user does elect to authenticate with a
2782 * new account, a bundle of session data for installing the account later is
2783 * returned with optional account password and account status token.
2784 * <p>
2785 * This method may be called from any thread, but the returned
2786 * {@link AccountManagerFuture} must not be used on the main thread.
2787 * <p>
2788 * <p>
2789 * <b>NOTE:</b> The account will not be installed to the device by calling
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002790 * this api alone. #finishSession should be called after this to install the
2791 * account on device.
Sandra Kwan78812282015-11-04 11:19:47 -08002792 *
2793 * @param accountType The type of account to add; must not be null
2794 * @param authTokenType The type of auth token (see {@link #getAuthToken})
2795 * this account will need to be able to generate, null for none
2796 * @param requiredFeatures The features (see {@link #hasFeatures}) this
2797 * account must have, null for none
2798 * @param options Authenticator-specific options for the request, may be
2799 * null or empty
2800 * @param activity The {@link Activity} context to use for launching a new
2801 * authenticator-defined sub-Activity to prompt the user to
2802 * create an account; used only to call startActivity(); if null,
2803 * the prompt will not be launched directly, but the necessary
2804 * {@link Intent} will be returned to the caller instead
2805 * @param callback Callback to invoke when the request completes, null for
2806 * no callback
2807 * @param handler {@link Handler} identifying the callback thread, null for
2808 * the main thread
2809 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
2810 * these fields if activity was specified and user was authenticated
2811 * with an account:
2812 * <ul>
2813 * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for
2814 * adding the the to the device later.
Sandra Kwan78812282015-11-04 11:19:47 -08002815 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
2816 * status of the account
2817 * </ul>
2818 * If no activity was specified, the returned Bundle contains only
2819 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
2820 * actual account creation process. If authenticator doesn't support
2821 * this method, the returned Bundle contains only
2822 * {@link #KEY_ACCOUNT_SESSION_BUNDLE} with encrypted
2823 * {@code options} needed to add account later. If an error
2824 * occurred, {@link AccountManagerFuture#getResult()} throws:
2825 * <ul>
2826 * <li>{@link AuthenticatorException} if no authenticator was
2827 * registered for this account type or the authenticator failed to
2828 * respond
2829 * <li>{@link OperationCanceledException} if the operation was
2830 * canceled for any reason, including the user canceling the
2831 * creation process or adding accounts (of this type) has been
2832 * disabled by policy
2833 * <li>{@link IOException} if the authenticator experienced an I/O
2834 * problem creating a new account, usually because of network
2835 * trouble
2836 * </ul>
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002837 * @see #finishSession
Sandra Kwan78812282015-11-04 11:19:47 -08002838 */
Sandra Kwane68c37e2015-11-12 17:11:49 -08002839 public AccountManagerFuture<Bundle> startAddAccountSession(
2840 final String accountType,
2841 final String authTokenType,
2842 final String[] requiredFeatures,
2843 final Bundle options,
2844 final Activity activity,
2845 AccountManagerCallback<Bundle> callback,
2846 Handler handler) {
Sandra Kwan78812282015-11-04 11:19:47 -08002847 if (accountType == null) throw new IllegalArgumentException("accountType is null");
2848 final Bundle optionsIn = new Bundle();
2849 if (options != null) {
2850 optionsIn.putAll(options);
2851 }
2852 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
2853
2854 return new AmsTask(activity, handler, callback) {
2855 @Override
2856 public void doWork() throws RemoteException {
Sandra Kwane68c37e2015-11-12 17:11:49 -08002857 mService.startAddAccountSession(
2858 mResponse,
2859 accountType,
2860 authTokenType,
2861 requiredFeatures,
2862 activity != null,
2863 optionsIn);
2864 }
2865 }.start();
2866 }
2867
2868 /**
2869 * Asks the user to enter a new password for an account but not updating the
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002870 * saved credentials for the account until {@link #finishSession} is called.
Sandra Kwane68c37e2015-11-12 17:11:49 -08002871 * <p>
2872 * This method may be called from any thread, but the returned
2873 * {@link AccountManagerFuture} must not be used on the main thread.
2874 * <p>
2875 * <b>NOTE:</b> The saved credentials for the account alone will not be
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002876 * updated by calling this API alone. #finishSession should be called after
2877 * this to update local credentials
Sandra Kwane68c37e2015-11-12 17:11:49 -08002878 *
2879 * @param account The account to update credentials for
2880 * @param authTokenType The credentials entered must allow an auth token of
2881 * this type to be created (but no actual auth token is
2882 * returned); may be null
2883 * @param options Authenticator-specific options for the request; may be
2884 * null or empty
2885 * @param activity The {@link Activity} context to use for launching a new
2886 * authenticator-defined sub-Activity to prompt the user to enter
2887 * a password; used only to call startActivity(); if null, the
2888 * prompt will not be launched directly, but the necessary
2889 * {@link Intent} will be returned to the caller instead
2890 * @param callback Callback to invoke when the request completes, null for
2891 * no callback
2892 * @param handler {@link Handler} identifying the callback thread, null for
2893 * the main thread
2894 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
2895 * these fields if an activity was supplied and user was
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002896 * successfully re-authenticated to the account:
Sandra Kwane68c37e2015-11-12 17:11:49 -08002897 * <ul>
2898 * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for
2899 * updating the local credentials on device later.
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002900 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
2901 * status of the account
Sandra Kwane68c37e2015-11-12 17:11:49 -08002902 * </ul>
2903 * If no activity was specified, the returned Bundle contains
2904 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
2905 * password prompt. If an error occurred,
2906 * {@link AccountManagerFuture#getResult()} throws:
2907 * <ul>
2908 * <li>{@link AuthenticatorException} if the authenticator failed to
2909 * respond
2910 * <li>{@link OperationCanceledException} if the operation was
2911 * canceled for any reason, including the user canceling the
2912 * password prompt
2913 * <li>{@link IOException} if the authenticator experienced an I/O
2914 * problem verifying the password, usually because of network
2915 * trouble
2916 * </ul>
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002917 * @see #finishSession
Sandra Kwane68c37e2015-11-12 17:11:49 -08002918 */
2919 public AccountManagerFuture<Bundle> startUpdateCredentialsSession(
2920 final Account account,
2921 final String authTokenType,
2922 final Bundle options,
2923 final Activity activity,
2924 final AccountManagerCallback<Bundle> callback,
2925 final Handler handler) {
2926 if (account == null) {
2927 throw new IllegalArgumentException("account is null");
2928 }
Carlos Valdivia51b651a2016-03-30 13:44:28 -07002929
2930 // Always include the calling package name. This just makes life easier
2931 // down stream.
2932 final Bundle optionsIn = new Bundle();
2933 if (options != null) {
2934 optionsIn.putAll(options);
2935 }
2936 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
2937
Sandra Kwane68c37e2015-11-12 17:11:49 -08002938 return new AmsTask(activity, handler, callback) {
2939 @Override
2940 public void doWork() throws RemoteException {
2941 mService.startUpdateCredentialsSession(
2942 mResponse,
2943 account,
2944 authTokenType,
2945 activity != null,
Carlos Valdivia51b651a2016-03-30 13:44:28 -07002946 optionsIn);
Sandra Kwan78812282015-11-04 11:19:47 -08002947 }
2948 }.start();
2949 }
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002950
2951 /**
2952 * Finishes the session started by {@link #startAddAccountSession} or
2953 * {@link #startUpdateCredentialsSession}. This will either add the account
2954 * to AccountManager or update the local credentials stored.
2955 * <p>
2956 * This method may be called from any thread, but the returned
2957 * {@link AccountManagerFuture} must not be used on the main thread.
2958 *
2959 * @param sessionBundle a {@link Bundle} created by {@link #startAddAccountSession} or
2960 * {@link #startUpdateCredentialsSession}
2961 * @param activity The {@link Activity} context to use for launching a new
2962 * authenticator-defined sub-Activity to prompt the user to
2963 * create an account or reauthenticate existing account; used
2964 * only to call startActivity(); if null, the prompt will not
2965 * be launched directly, but the necessary {@link Intent} will
2966 * be returned to the caller instead
2967 * @param callback Callback to invoke when the request completes, null for
2968 * no callback
2969 * @param handler {@link Handler} identifying the callback thread, null for
2970 * the main thread
2971 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
2972 * these fields if an activity was supplied and an account was added
2973 * to device or local credentials were updated::
2974 * <ul>
2975 * <li>{@link #KEY_ACCOUNT_NAME} - the name of the account created
2976 * <li>{@link #KEY_ACCOUNT_TYPE} - the type of the account
Hongming Jin8b442752016-06-26 10:36:21 -07002977 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
2978 * status of the account
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002979 * </ul>
2980 * If no activity was specified and additional information is needed
2981 * from user, the returned Bundle may contains only
2982 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
2983 * actual account creation process. If an error occurred,
2984 * {@link AccountManagerFuture#getResult()} throws:
2985 * <ul>
2986 * <li>{@link AuthenticatorException} if no authenticator was
2987 * registered for this account type or the authenticator failed to
2988 * respond
2989 * <li>{@link OperationCanceledException} if the operation was
2990 * canceled for any reason, including the user canceling the
2991 * creation process or adding accounts (of this type) has been
2992 * disabled by policy
2993 * <li>{@link IOException} if the authenticator experienced an I/O
2994 * problem creating a new account, usually because of network
2995 * trouble
2996 * </ul>
2997 * @see #startAddAccountSession and #startUpdateCredentialsSession
2998 */
2999 public AccountManagerFuture<Bundle> finishSession(
3000 final Bundle sessionBundle,
3001 final Activity activity,
3002 AccountManagerCallback<Bundle> callback,
3003 Handler handler) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003004 return finishSessionAsUser(
3005 sessionBundle,
3006 activity,
3007 Process.myUserHandle(),
3008 callback,
3009 handler);
3010 }
3011
3012 /**
3013 * @see #finishSession
3014 * @hide
3015 */
3016 @SystemApi
3017 public AccountManagerFuture<Bundle> finishSessionAsUser(
3018 final Bundle sessionBundle,
3019 final Activity activity,
3020 final UserHandle userHandle,
3021 AccountManagerCallback<Bundle> callback,
3022 Handler handler) {
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003023 if (sessionBundle == null) {
3024 throw new IllegalArgumentException("sessionBundle is null");
3025 }
3026
3027 /* Add information required by add account flow */
3028 final Bundle appInfo = new Bundle();
3029 appInfo.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
3030
3031 return new AmsTask(activity, handler, callback) {
3032 @Override
3033 public void doWork() throws RemoteException {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003034 mService.finishSessionAsUser(
3035 mResponse,
3036 sessionBundle,
3037 activity != null,
3038 appInfo,
3039 userHandle.getIdentifier());
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003040 }
3041 }.start();
3042 }
Sandra Kwan390c9d22016-01-12 14:13:37 -08003043
3044 /**
3045 * Checks whether {@link #updateCredentials} or {@link #startUpdateCredentialsSession} should be
3046 * called with respect to the specified account.
3047 * <p>
3048 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
3049 * not be used on the main thread.
3050 *
3051 * @param account The {@link Account} to be checked whether {@link #updateCredentials} or
3052 * {@link #startUpdateCredentialsSession} should be called
3053 * @param statusToken a String of token to check account staus
3054 * @param callback Callback to invoke when the request completes, null for no callback
3055 * @param handler {@link Handler} identifying the callback thread, null for the main thread
3056 * @return An {@link AccountManagerFuture} which resolves to a Boolean, true if the credentials
3057 * of the account should be updated.
Sandra Kwan390c9d22016-01-12 14:13:37 -08003058 */
Sandra Kwan390c9d22016-01-12 14:13:37 -08003059 public AccountManagerFuture<Boolean> isCredentialsUpdateSuggested(
3060 final Account account,
3061 final String statusToken,
3062 AccountManagerCallback<Boolean> callback,
3063 Handler handler) {
3064 if (account == null) {
3065 throw new IllegalArgumentException("account is null");
3066 }
3067
3068 if (TextUtils.isEmpty(statusToken)) {
3069 throw new IllegalArgumentException("status token is empty");
3070 }
3071
3072 return new Future2Task<Boolean>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003073 @Override
Sandra Kwan390c9d22016-01-12 14:13:37 -08003074 public void doWork() throws RemoteException {
3075 mService.isCredentialsUpdateSuggested(
3076 mResponse,
3077 account,
3078 statusToken);
3079 }
Sandra Kwan0b84b452016-01-20 15:25:42 -08003080 @Override
Sandra Kwan390c9d22016-01-12 14:13:37 -08003081 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
3082 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
3083 throw new AuthenticatorException("no result in response");
3084 }
3085 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
3086 }
3087 }.start();
3088 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003089
3090 /**
3091 * Gets whether a given package under a user has access to an account.
3092 * Can be called only from the system UID.
3093 *
3094 * @param account The account for which to check.
3095 * @param packageName The package for which to check.
3096 * @param userHandle The user for which to check.
3097 * @return True if the package can access the account.
3098 *
3099 * @hide
3100 */
3101 public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName,
3102 @NonNull UserHandle userHandle) {
3103 try {
3104 return mService.hasAccountAccess(account, packageName, userHandle);
3105 } catch (RemoteException e) {
3106 throw e.rethrowFromSystemServer();
3107 }
3108 }
3109
3110 /**
3111 * Creates an intent to request access to a given account for a UID.
3112 * The returned intent should be stated for a result where {@link
3113 * Activity#RESULT_OK} result means access was granted whereas {@link
3114 * Activity#RESULT_CANCELED} result means access wasn't granted. Can
3115 * be called only from the system UID.
3116 *
3117 * @param account The account for which to request.
3118 * @param packageName The package name which to request.
3119 * @param userHandle The user for which to request.
3120 * @return The intent to request account access or null if the package
3121 * doesn't exist.
3122 *
3123 * @hide
3124 */
3125 public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account,
3126 @NonNull String packageName, @NonNull UserHandle userHandle) {
3127 try {
3128 return mService.createRequestAccountAccessIntentSenderAsUser(account, packageName,
3129 userHandle);
3130 } catch (RemoteException e) {
3131 throw e.rethrowFromSystemServer();
3132 }
3133 }
Fred Quintana60307342009-03-24 22:48:12 -07003134}