blob: 7465ed92e4692471e288ab8eef1a512f2e52c92f [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;
Amith Yamasani12b8e132013-03-14 10:48:07 -070031import android.content.res.Resources;
Costin Manolacheb6437242009-09-10 16:14:12 -070032import android.database.SQLException;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080033import android.os.Build;
Fred Quintanaa698f422009-04-08 19:14:54 -070034import android.os.Bundle;
35import android.os.Handler;
36import android.os.Looper;
Fred Quintana33269202009-04-20 16:05:10 -070037import android.os.Parcelable;
Amith Yamasani2c7bc262012-11-05 16:46:02 -080038import android.os.Process;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080039import android.os.RemoteException;
Amith Yamasani2c7bc262012-11-05 16:46:02 -080040import android.os.UserHandle;
Fred Quintanaf0fd8432010-03-08 12:48:05 -080041import android.text.TextUtils;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080042import android.util.Log;
43
44import com.android.internal.R;
45import com.google.android.collect.Maps;
Fred Quintana60307342009-03-24 22:48:12 -070046
Fred Quintanaa698f422009-04-08 19:14:54 -070047import java.io.IOException;
Fred Quintana1121bb52011-09-14 23:19:35 -070048import java.util.ArrayList;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080049import java.util.HashMap;
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -070050import java.util.List;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080051import java.util.Map;
Fred Quintanaa698f422009-04-08 19:14:54 -070052import java.util.concurrent.Callable;
53import java.util.concurrent.CancellationException;
54import java.util.concurrent.ExecutionException;
55import java.util.concurrent.FutureTask;
Fred Quintanaa698f422009-04-08 19:14:54 -070056import java.util.concurrent.TimeUnit;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080057import java.util.concurrent.TimeoutException;
Fred Quintana60307342009-03-24 22:48:12 -070058
59/**
Dan Egnor661f0132010-02-19 11:23:00 -080060 * This class provides access to a centralized registry of the user's
Dan Egnor8e4378b2010-08-02 18:22:09 -070061 * online accounts. The user enters credentials (username and password) once
62 * per account, granting applications access to online resources with
63 * "one-click" approval.
Fred Quintana60307342009-03-24 22:48:12 -070064 *
Dan Egnor661f0132010-02-19 11:23:00 -080065 * <p>Different online services have different ways of handling accounts and
66 * authentication, so the account manager uses pluggable <em>authenticator</em>
Dan Egnor8e4378b2010-08-02 18:22:09 -070067 * modules for different <em>account types</em>. Authenticators (which may be
68 * written by third parties) handle the actual details of validating account
69 * credentials and storing account information. For example, Google, Facebook,
70 * and Microsoft Exchange each have their own authenticator.
Dan Egnor661f0132010-02-19 11:23:00 -080071 *
72 * <p>Many servers support some notion of an <em>authentication token</em>,
73 * which can be used to authenticate a request to the server without sending
74 * the user's actual password. (Auth tokens are normally created with a
75 * separate request which does include the user's credentials.) AccountManager
Dan Egnor8e4378b2010-08-02 18:22:09 -070076 * can generate auth tokens for applications, so the application doesn't need to
77 * handle passwords directly. Auth tokens are normally reusable and cached by
78 * AccountManager, but must be refreshed periodically. It's the responsibility
79 * of applications to <em>invalidate</em> auth tokens when they stop working so
80 * the AccountManager knows it needs to regenerate them.
Dan Egnor661f0132010-02-19 11:23:00 -080081 *
82 * <p>Applications accessing a server normally go through these steps:
83 *
84 * <ul>
85 * <li>Get an instance of AccountManager using {@link #get(Context)}.
86 *
87 * <li>List the available accounts using {@link #getAccountsByType} or
88 * {@link #getAccountsByTypeAndFeatures}. Normally applications will only
89 * be interested in accounts with one particular <em>type</em>, which
90 * identifies the authenticator. Account <em>features</em> are used to
91 * identify particular account subtypes and capabilities. Both the account
92 * type and features are authenticator-specific strings, and must be known by
93 * the application in coordination with its preferred authenticators.
94 *
95 * <li>Select one or more of the available accounts, possibly by asking the
96 * user for their preference. If no suitable accounts are available,
97 * {@link #addAccount} may be called to prompt the user to create an
98 * account of the appropriate type.
99 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700100 * <li><b>Important:</b> If the application is using a previously remembered
101 * account selection, it must make sure the account is still in the list
102 * of accounts returned by {@link #getAccountsByType}. Requesting an auth token
103 * for an account no longer on the device results in an undefined failure.
104 *
Dan Egnor661f0132010-02-19 11:23:00 -0800105 * <li>Request an auth token for the selected account(s) using one of the
106 * {@link #getAuthToken} methods or related helpers. Refer to the description
107 * of each method for exact usage and error handling details.
108 *
109 * <li>Make the request using the auth token. The form of the auth token,
110 * the format of the request, and the protocol used are all specific to the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700111 * service you are accessing. The application may use whatever network and
112 * protocol libraries are useful.
Dan Egnor661f0132010-02-19 11:23:00 -0800113 *
114 * <li><b>Important:</b> If the request fails with an authentication error,
115 * it could be that a cached auth token is stale and no longer honored by
116 * the server. The application must call {@link #invalidateAuthToken} to remove
117 * the token from the cache, otherwise requests will continue failing! After
118 * invalidating the auth token, immediately go back to the "Request an auth
119 * token" step above. If the process fails the second time, then it can be
120 * treated as a "genuine" authentication failure and the user notified or other
121 * appropriate actions taken.
122 * </ul>
123 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700124 * <p>Some AccountManager methods may need to interact with the user to
Dan Egnor661f0132010-02-19 11:23:00 -0800125 * prompt for credentials, present options, or ask the user to add an account.
126 * The caller may choose whether to allow AccountManager to directly launch the
127 * necessary user interface and wait for the user, or to return an Intent which
128 * the caller may use to launch the interface, or (in some cases) to install a
129 * notification which the user can select at any time to launch the interface.
130 * To have AccountManager launch the interface directly, the caller must supply
131 * the current foreground {@link Activity} context.
132 *
133 * <p>Many AccountManager methods take {@link AccountManagerCallback} and
Dan Egnor8e4378b2010-08-02 18:22:09 -0700134 * {@link Handler} as parameters. These methods return immediately and
Dan Egnor661f0132010-02-19 11:23:00 -0800135 * run asynchronously. If a callback is provided then
136 * {@link AccountManagerCallback#run} will be invoked on the Handler's
137 * thread when the request completes, successfully or not.
Dan Egnor8e4378b2010-08-02 18:22:09 -0700138 * The result is retrieved by calling {@link AccountManagerFuture#getResult()}
139 * on the {@link AccountManagerFuture} returned by the method (and also passed
140 * to the callback). This method waits for the operation to complete (if
141 * necessary) and either returns the result or throws an exception if an error
142 * occurred during the operation. To make the request synchronously, call
Dan Egnor661f0132010-02-19 11:23:00 -0800143 * {@link AccountManagerFuture#getResult()} immediately on receiving the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700144 * future from the method; no callback need be supplied.
Dan Egnor661f0132010-02-19 11:23:00 -0800145 *
146 * <p>Requests which may block, including
147 * {@link AccountManagerFuture#getResult()}, must never be called on
148 * the application's main event thread. These operations throw
149 * {@link IllegalStateException} if they are used on the main thread.
Fred Quintana60307342009-03-24 22:48:12 -0700150 */
151public class AccountManager {
152 private static final String TAG = "AccountManager";
153
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700154 public static final int ERROR_CODE_REMOTE_EXCEPTION = 1;
155 public static final int ERROR_CODE_NETWORK_ERROR = 3;
156 public static final int ERROR_CODE_CANCELED = 4;
157 public static final int ERROR_CODE_INVALID_RESPONSE = 5;
158 public static final int ERROR_CODE_UNSUPPORTED_OPERATION = 6;
159 public static final int ERROR_CODE_BAD_ARGUMENTS = 7;
160 public static final int ERROR_CODE_BAD_REQUEST = 8;
Jatin Lodhia3df7d692013-03-27 10:57:23 -0700161 public static final int ERROR_CODE_BAD_AUTHENTICATION = 9;
Fred Quintana756b7352009-10-21 13:43:10 -0700162
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800163 /** @hide */
164 public static final int ERROR_CODE_USER_RESTRICTED = 100;
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100165 /** @hide */
166 public static final int ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE = 101;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800167
Dan Egnor661f0132010-02-19 11:23:00 -0800168 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700169 * Bundle key used for the {@link String} account name in results
Dan Egnor661f0132010-02-19 11:23:00 -0800170 * from methods which return information about a particular account.
171 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700172 public static final String KEY_ACCOUNT_NAME = "authAccount";
Dan Egnor661f0132010-02-19 11:23:00 -0800173
174 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700175 * Bundle key used for the {@link String} account type in results
Dan Egnor661f0132010-02-19 11:23:00 -0800176 * from methods which return information about a particular account.
177 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700178 public static final String KEY_ACCOUNT_TYPE = "accountType";
Dan Egnor661f0132010-02-19 11:23:00 -0800179
180 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700181 * Bundle key used for the auth token value in results
Dan Egnor661f0132010-02-19 11:23:00 -0800182 * from {@link #getAuthToken} and friends.
183 */
184 public static final String KEY_AUTHTOKEN = "authtoken";
185
186 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700187 * Bundle key used for an {@link Intent} in results from methods that
Dan Egnor661f0132010-02-19 11:23:00 -0800188 * may require the caller to interact with the user. The Intent can
189 * be used to start the corresponding user interface activity.
190 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700191 public static final String KEY_INTENT = "intent";
Dan Egnor661f0132010-02-19 11:23:00 -0800192
193 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700194 * Bundle key used to supply the password directly in options to
Dan Egnor661f0132010-02-19 11:23:00 -0800195 * {@link #confirmCredentials}, rather than prompting the user with
196 * the standard password prompt.
197 */
198 public static final String KEY_PASSWORD = "password";
199
200 public static final String KEY_ACCOUNTS = "accounts";
Brian Carlstrom46703b02011-04-06 15:41:29 -0700201
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700202 public static final String KEY_ACCOUNT_AUTHENTICATOR_RESPONSE = "accountAuthenticatorResponse";
203 public static final String KEY_ACCOUNT_MANAGER_RESPONSE = "accountManagerResponse";
Dan Egnor661f0132010-02-19 11:23:00 -0800204 public static final String KEY_AUTHENTICATOR_TYPES = "authenticator_types";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700205 public static final String KEY_AUTH_FAILED_MESSAGE = "authFailedMessage";
206 public static final String KEY_AUTH_TOKEN_LABEL = "authTokenLabelKey";
Dan Egnor661f0132010-02-19 11:23:00 -0800207 public static final String KEY_BOOLEAN_RESULT = "booleanResult";
208 public static final String KEY_ERROR_CODE = "errorCode";
209 public static final String KEY_ERROR_MESSAGE = "errorMessage";
210 public static final String KEY_USERDATA = "userdata";
Costin Manolacheb61e8fb2011-09-08 11:26:09 -0700211
Costin Manolachea40c6302010-12-13 14:50:45 -0800212 /**
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800213 * Bundle key used to supply the last time the credentials of the account
214 * were authenticated successfully. Time is specified in milliseconds since
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700215 * epoch. Associated time is updated on successful authentication of account
216 * on adding account, confirming credentials, or updating credentials.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800217 */
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -0700218 public static final String KEY_LAST_AUTHENTICATED_TIME = "lastAuthenticatedTime";
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800219
220 /**
Costin Manolachea40c6302010-12-13 14:50:45 -0800221 * Authenticators using 'customTokens' option will also get the UID of the
222 * caller
223 */
224 public static final String KEY_CALLER_UID = "callerUid";
225 public static final String KEY_CALLER_PID = "callerPid";
Dan Egnor661f0132010-02-19 11:23:00 -0800226
Costin Manolached6060452011-01-24 16:11:36 -0800227 /**
Fred Quintanaad93a322011-09-08 13:21:01 -0700228 * The Android package of the caller will be set in the options bundle by the
229 * {@link AccountManager} and will be passed to the AccountManagerService and
230 * to the AccountAuthenticators. The uid of the caller will be known by the
231 * AccountManagerService as well as the AccountAuthenticators so they will be able to
232 * verify that the package is consistent with the uid (a uid might be shared by many
233 * packages).
234 */
235 public static final String KEY_ANDROID_PACKAGE_NAME = "androidPackageName";
236
237 /**
Costin Manolached6060452011-01-24 16:11:36 -0800238 * Boolean, if set and 'customTokens' the authenticator is responsible for
239 * notifications.
240 * @hide
241 */
242 public static final String KEY_NOTIFY_ON_FAILURE = "notifyOnAuthFailure";
243
Sandra Kwan78812282015-11-04 11:19:47 -0800244 /**
245 * Bundle key used for a {@link Bundle} in result from
246 * {@link #startAddAccountSession} and friends which returns session data
247 * for installing an account later.
Sandra Kwana578d112015-12-16 16:01:43 -0800248 * @hide
Sandra Kwan78812282015-11-04 11:19:47 -0800249 */
Sandra Kwana578d112015-12-16 16:01:43 -0800250 @SystemApi
Sandra Kwan78812282015-11-04 11:19:47 -0800251 public static final String KEY_ACCOUNT_SESSION_BUNDLE = "accountSessionBundle";
252
253 /**
254 * Bundle key used for the {@link String} account status token in result
255 * from {@link #startAddAccountSession} and friends which returns
256 * information about a particular account.
Sandra Kwana578d112015-12-16 16:01:43 -0800257 * @hide
Sandra Kwan78812282015-11-04 11:19:47 -0800258 */
Sandra Kwana578d112015-12-16 16:01:43 -0800259 @SystemApi
Sandra Kwan78812282015-11-04 11:19:47 -0800260 public static final String KEY_ACCOUNT_STATUS_TOKEN = "accountStatusToken";
261
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700262 public static final String ACTION_AUTHENTICATOR_INTENT =
263 "android.accounts.AccountAuthenticator";
264 public static final String AUTHENTICATOR_META_DATA_NAME =
Dan Egnor661f0132010-02-19 11:23:00 -0800265 "android.accounts.AccountAuthenticator";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700266 public static final String AUTHENTICATOR_ATTRIBUTES_NAME = "account-authenticator";
267
Fred Quintana60307342009-03-24 22:48:12 -0700268 private final Context mContext;
269 private final IAccountManager mService;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700270 private final Handler mMainHandler;
Dan Egnor661f0132010-02-19 11:23:00 -0800271
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700272 /**
273 * Action sent as a broadcast Intent by the AccountsService
Dan Egnor661f0132010-02-19 11:23:00 -0800274 * when accounts are added, accounts are removed, or an
275 * account's credentials (saved password, etc) are changed.
276 *
277 * @see #addOnAccountsUpdatedListener
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700278 */
279 public static final String LOGIN_ACCOUNTS_CHANGED_ACTION =
280 "android.accounts.LOGIN_ACCOUNTS_CHANGED";
Fred Quintana60307342009-03-24 22:48:12 -0700281
Fred Quintana33269202009-04-20 16:05:10 -0700282 /**
283 * @hide
284 */
Fred Quintana60307342009-03-24 22:48:12 -0700285 public AccountManager(Context context, IAccountManager service) {
286 mContext = context;
287 mService = service;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700288 mMainHandler = new Handler(mContext.getMainLooper());
Fred Quintana60307342009-03-24 22:48:12 -0700289 }
290
Fred Quintana0eabf022009-04-27 15:08:17 -0700291 /**
292 * @hide used for testing only
293 */
294 public AccountManager(Context context, IAccountManager service, Handler handler) {
295 mContext = context;
296 mService = service;
297 mMainHandler = handler;
298 }
299
Fred Quintana756b7352009-10-21 13:43:10 -0700300 /**
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800301 * @hide for internal use only
302 */
303 public static Bundle sanitizeResult(Bundle result) {
Fred Quintana382601f2010-03-25 12:25:10 -0700304 if (result != null) {
305 if (result.containsKey(KEY_AUTHTOKEN)
306 && !TextUtils.isEmpty(result.getString(KEY_AUTHTOKEN))) {
307 final Bundle newResult = new Bundle(result);
308 newResult.putString(KEY_AUTHTOKEN, "<omitted for logging purposes>");
309 return newResult;
310 }
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800311 }
312 return result;
313 }
314
315 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800316 * Gets an AccountManager instance associated with a Context.
317 * The {@link Context} will be used as long as the AccountManager is
318 * active, so make sure to use a {@link Context} whose lifetime is
319 * commensurate with any listeners registered to
320 * {@link #addOnAccountsUpdatedListener} or similar methods.
321 *
322 * <p>It is safe to call this method from the main thread.
323 *
324 * <p>No permission is required to call this method.
325 *
Fred Quintana756b7352009-10-21 13:43:10 -0700326 * @param context The {@link Context} to use when necessary
Dan Egnor661f0132010-02-19 11:23:00 -0800327 * @return An {@link AccountManager} instance
Fred Quintana756b7352009-10-21 13:43:10 -0700328 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700329 public static AccountManager get(Context context) {
Fred Quintana382601f2010-03-25 12:25:10 -0700330 if (context == null) throw new IllegalArgumentException("context is null");
Fred Quintanaa698f422009-04-08 19:14:54 -0700331 return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
332 }
333
Fred Quintana756b7352009-10-21 13:43:10 -0700334 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800335 * Gets the saved password associated with the account.
336 * This is intended for authenticators and related code; applications
337 * should get an auth token instead.
338 *
339 * <p>It is safe to call this method from the main thread.
340 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700341 * <p>This method requires the caller to have a signature match with the
342 * authenticator that owns the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800343 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700344 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
345 * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs for
346 * this function in API level 22.
347 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700348 * @param account The account to query for a password. Must not be {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -0800349 * @return The account's password, null if none or if the account doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700350 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700351 public String getPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -0700352 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintana60307342009-03-24 22:48:12 -0700353 try {
354 return mService.getPassword(account);
355 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700356 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700357 }
358 }
359
Fred Quintana756b7352009-10-21 13:43:10 -0700360 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800361 * Gets the user data named by "key" associated with the account.
362 * This is intended for authenticators and related code to store
363 * arbitrary metadata along with accounts. The meaning of the keys
364 * and values is up to the authenticator for the account.
365 *
366 * <p>It is safe to call this method from the main thread.
367 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700368 * <p>This method requires the caller to have a signature match with the
369 * authenticator that owns the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800370 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700371 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
372 * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs
373 * for this function in API level 22.
374 *
Dan Egnor661f0132010-02-19 11:23:00 -0800375 * @param account The account to query for user data
376 * @return The user data, null if the account or key doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700377 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700378 public String getUserData(final Account account, final String key) {
Fred Quintana382601f2010-03-25 12:25:10 -0700379 if (account == null) throw new IllegalArgumentException("account is null");
380 if (key == null) throw new IllegalArgumentException("key is null");
Fred Quintana60307342009-03-24 22:48:12 -0700381 try {
382 return mService.getUserData(account, key);
383 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700384 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700385 }
386 }
387
Fred Quintana756b7352009-10-21 13:43:10 -0700388 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800389 * Lists the currently registered authenticators.
390 *
391 * <p>It is safe to call this method from the main thread.
392 *
393 * <p>No permission is required to call this method.
394 *
395 * @return An array of {@link AuthenticatorDescription} for every
396 * authenticator known to the AccountManager service. Empty (never
397 * null) if no authenticators are known.
Fred Quintana756b7352009-10-21 13:43:10 -0700398 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700399 public AuthenticatorDescription[] getAuthenticatorTypes() {
Fred Quintanaa698f422009-04-08 19:14:54 -0700400 try {
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100401 return mService.getAuthenticatorTypes(UserHandle.getCallingUserId());
402 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700403 throw e.rethrowFromSystemServer();
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100404 }
405 }
406
407 /**
408 * @hide
409 * Lists the currently registered authenticators for a given user id.
410 *
411 * <p>It is safe to call this method from the main thread.
412 *
413 * <p>The caller has to be in the same user or have the permission
414 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
415 *
416 * @return An array of {@link AuthenticatorDescription} for every
417 * authenticator known to the AccountManager service. Empty (never
418 * null) if no authenticators are known.
419 */
420 public AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) {
421 try {
422 return mService.getAuthenticatorTypes(userId);
Fred Quintanaa698f422009-04-08 19:14:54 -0700423 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700424 throw e.rethrowFromSystemServer();
Fred Quintanaa698f422009-04-08 19:14:54 -0700425 }
426 }
427
Fred Quintana756b7352009-10-21 13:43:10 -0700428 /**
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000429 * Lists all accounts of any type registered on the device.
430 * Equivalent to getAccountsByType(null).
Dan Egnor661f0132010-02-19 11:23:00 -0800431 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000432 * <p>It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800433 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000434 * <p>Clients of this method that have not been granted the
435 * {@link android.Manifest.permission#GET_ACCOUNTS} permission,
436 * will only see those accounts managed by AbstractAccountAuthenticators whose
437 * signature matches the client.
Dan Egnor661f0132010-02-19 11:23:00 -0800438 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000439 * @return An array of {@link Account}, one for each account. Empty
440 * (never null) if no accounts have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700441 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700442 @NonNull
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000443 @RequiresPermission(GET_ACCOUNTS)
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700444 public Account[] getAccounts() {
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000445 try {
446 return mService.getAccounts(null, mContext.getOpPackageName());
447 } catch (RemoteException e) {
448 throw e.rethrowFromSystemServer();
449 }
Fred Quintana60307342009-03-24 22:48:12 -0700450 }
451
Fred Quintana756b7352009-10-21 13:43:10 -0700452 /**
Amith Yamasani27db4682013-03-30 17:07:47 -0700453 * @hide
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000454 * Lists all accounts of any type registered on the device for a given
455 * user id. Equivalent to getAccountsByType(null).
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100456 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000457 * <p>It is safe to call this method from the main thread.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100458 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000459 * <p>Clients of this method that have not been granted the
460 * {@link android.Manifest.permission#GET_ACCOUNTS} permission,
461 * will only see those accounts managed by AbstractAccountAuthenticators whose
462 * signature matches the client.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100463 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000464 * @return An array of {@link Account}, one for each account. Empty
465 * (never null) if no accounts have been added.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100466 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700467 @NonNull
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000468 @RequiresPermission(GET_ACCOUNTS)
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100469 public Account[] getAccountsAsUser(int userId) {
470 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700471 return mService.getAccountsAsUser(null, userId, mContext.getOpPackageName());
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100472 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700473 throw e.rethrowFromSystemServer();
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100474 }
475 }
476
477 /**
478 * @hide
Amith Yamasani27db4682013-03-30 17:07:47 -0700479 * For use by internal activities. Returns the list of accounts that the calling package
480 * is authorized to use, particularly for shared accounts.
481 * @param packageName package name of the calling app.
482 * @param uid the uid of the calling app.
483 * @return the accounts that are available to this package and user.
484 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700485 @NonNull
Amith Yamasani27db4682013-03-30 17:07:47 -0700486 public Account[] getAccountsForPackage(String packageName, int uid) {
487 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700488 return mService.getAccountsForPackage(packageName, uid, mContext.getOpPackageName());
Amith Yamasani27db4682013-03-30 17:07:47 -0700489 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700490 throw re.rethrowFromSystemServer();
Amith Yamasani27db4682013-03-30 17:07:47 -0700491 }
492 }
493
494 /**
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700495 * Returns the accounts visible to the specified package, in an environment where some apps
496 * are not authorized to view all accounts. This method can only be called by system apps.
497 * @param type The type of accounts to return, null to retrieve all accounts
498 * @param packageName The package name of the app for which the accounts are to be returned
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000499 * @return An array of {@link Account}, one per matching account. Empty
500 * (never null) if no accounts of the specified type have been added.
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700501 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700502 @NonNull
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700503 public Account[] getAccountsByTypeForPackage(String type, String packageName) {
504 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700505 return mService.getAccountsByTypeForPackage(type, packageName,
506 mContext.getOpPackageName());
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700507 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700508 throw re.rethrowFromSystemServer();
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700509 }
510 }
511
512 /**
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000513 * Lists all accounts of a particular type. The account type is a
514 * string token corresponding to the authenticator and useful domain
515 * of the account. For example, there are types corresponding to Google
516 * and Facebook. The exact string token to use will be published somewhere
517 * associated with the authenticator in question.
Dan Egnor661f0132010-02-19 11:23:00 -0800518 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000519 * <p>It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800520 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000521 * <p>Clients of this method that have not been granted the
522 * {@link android.Manifest.permission#GET_ACCOUNTS} permission,
523 * will only see those accounts managed by AbstractAccountAuthenticators whose
524 * signature matches the client.
Dan Egnor661f0132010-02-19 11:23:00 -0800525 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000526 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
527 * GET_ACCOUNTS permission is needed for those platforms, irrespective of uid
528 * or signature match. See docs for this function in API level 22.
529 *
530 * @param type The type of accounts to return, null to retrieve all accounts
531 * @return An array of {@link Account}, one per matching account. Empty
532 * (never null) if no accounts of the specified type have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700533 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700534 @NonNull
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000535 @RequiresPermission(GET_ACCOUNTS)
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700536 public Account[] getAccountsByType(String type) {
Amith Yamasani2c7bc262012-11-05 16:46:02 -0800537 return getAccountsByTypeAsUser(type, Process.myUserHandle());
538 }
539
540 /** @hide Same as {@link #getAccountsByType(String)} but for a specific user. */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700541 @NonNull
Amith Yamasani2c7bc262012-11-05 16:46:02 -0800542 public Account[] getAccountsByTypeAsUser(String type, UserHandle userHandle) {
Fred Quintana60307342009-03-24 22:48:12 -0700543 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700544 return mService.getAccountsAsUser(type, userHandle.getIdentifier(),
545 mContext.getOpPackageName());
Fred Quintana60307342009-03-24 22:48:12 -0700546 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700547 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700548 }
549 }
550
Fred Quintana756b7352009-10-21 13:43:10 -0700551 /**
Fred Quintanad9640ec2012-05-23 12:37:00 -0700552 * Change whether or not an app (identified by its uid) is allowed to retrieve an authToken
553 * for an account.
554 * <p>
555 * This is only meant to be used by system activities and is not in the SDK.
556 * @param account The account whose permissions are being modified
557 * @param authTokenType The type of token whose permissions are being modified
558 * @param uid The uid that identifies the app which is being granted or revoked permission.
559 * @param value true is permission is being granted, false for revoked
560 * @hide
561 */
562 public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) {
563 try {
564 mService.updateAppPermission(account, authTokenType, uid, value);
565 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700566 throw e.rethrowFromSystemServer();
Fred Quintanad9640ec2012-05-23 12:37:00 -0700567 }
568 }
569
570 /**
571 * Get the user-friendly label associated with an authenticator's auth token.
572 * @param accountType the type of the authenticator. must not be null.
573 * @param authTokenType the token type. must not be null.
574 * @param callback callback to invoke when the result is available. may be null.
575 * @param handler the handler on which to invoke the callback, or null for the main thread
576 * @return a future containing the label string
577 * @hide
578 */
579 public AccountManagerFuture<String> getAuthTokenLabel(
580 final String accountType, final String authTokenType,
581 AccountManagerCallback<String> callback, Handler handler) {
582 if (accountType == null) throw new IllegalArgumentException("accountType is null");
583 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
584 return new Future2Task<String>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800585 @Override
Fred Quintanad9640ec2012-05-23 12:37:00 -0700586 public void doWork() throws RemoteException {
587 mService.getAuthTokenLabel(mResponse, accountType, authTokenType);
588 }
589
590 @Override
591 public String bundleToResult(Bundle bundle) throws AuthenticatorException {
592 if (!bundle.containsKey(KEY_AUTH_TOKEN_LABEL)) {
593 throw new AuthenticatorException("no result in response");
594 }
595 return bundle.getString(KEY_AUTH_TOKEN_LABEL);
596 }
597 }.start();
598 }
599
600 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800601 * Finds out whether a particular account has all the specified features.
602 * Account features are authenticator-specific string tokens identifying
603 * boolean account properties. For example, features are used to tell
604 * whether Google accounts have a particular service (such as Google
605 * Calendar or Google Talk) enabled. The feature names and their meanings
606 * are published somewhere associated with the authenticator in question.
607 *
608 * <p>This method may be called from any thread, but the returned
609 * {@link AccountManagerFuture} must not be used on the main thread.
610 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000611 * <p>This method requires the caller to hold the permission
612 * {@link android.Manifest.permission#GET_ACCOUNTS} or be a signature
613 * match with the AbstractAccountAuthenticator that manages the account.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800614 *
615 * @param account The {@link Account} to test
Dan Egnor661f0132010-02-19 11:23:00 -0800616 * @param features An array of the account features to check
617 * @param callback Callback to invoke when the request completes,
618 * null for no callback
619 * @param handler {@link Handler} identifying the callback thread,
620 * null for the main thread
621 * @return An {@link AccountManagerFuture} which resolves to a Boolean,
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000622 * true if the account exists and has all of the specified features.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800623 */
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000624 @RequiresPermission(GET_ACCOUNTS)
Fred Quintana3084a6f2010-01-14 18:02:03 -0800625 public AccountManagerFuture<Boolean> hasFeatures(final Account account,
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800626 final String[] features,
627 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700628 if (account == null) throw new IllegalArgumentException("account is null");
629 if (features == null) throw new IllegalArgumentException("features is null");
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800630 return new Future2Task<Boolean>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800631 @Override
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800632 public void doWork() throws RemoteException {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700633 mService.hasFeatures(mResponse, account, features, mContext.getOpPackageName());
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800634 }
Sandra Kwan0b84b452016-01-20 15:25:42 -0800635 @Override
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800636 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
637 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
638 throw new AuthenticatorException("no result in response");
639 }
640 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
641 }
642 }.start();
643 }
644
645 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800646 * Lists all accounts of a type which have certain features. The account
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000647 * type identifies the authenticator (see {@link #getAccountsByType}).
648 * Account features are authenticator-specific string tokens identifying
649 * boolean account properties (see {@link #hasFeatures}).
Fred Quintana756b7352009-10-21 13:43:10 -0700650 *
Dan Egnor661f0132010-02-19 11:23:00 -0800651 * <p>Unlike {@link #getAccountsByType}, this method calls the authenticator,
652 * which may contact the server or do other work to check account features,
653 * so the method returns an {@link AccountManagerFuture}.
Fred Quintanaa698f422009-04-08 19:14:54 -0700654 *
Dan Egnor661f0132010-02-19 11:23:00 -0800655 * <p>This method may be called from any thread, but the returned
656 * {@link AccountManagerFuture} must not be used on the main thread.
Fred Quintana756b7352009-10-21 13:43:10 -0700657 *
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000658 * <p>Clients of this method that have not been granted the
659 * {@link android.Manifest.permission#GET_ACCOUNTS} permission,
660 * will only see those accounts managed by AbstractAccountAuthenticators whose
661 * signature matches the client.
Fred Quintana756b7352009-10-21 13:43:10 -0700662 *
Dan Egnor661f0132010-02-19 11:23:00 -0800663 * @param type The type of accounts to return, must not be null
664 * @param features An array of the account features to require,
665 * may be null or empty
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000666 *
667 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
668 * GET_ACCOUNTS permission is needed for those platforms, irrespective of uid
669 * or signature match. See docs for this function in API level 22.
670 *
Dan Egnor661f0132010-02-19 11:23:00 -0800671 * @param callback Callback to invoke when the request completes,
672 * null for no callback
673 * @param handler {@link Handler} identifying the callback thread,
674 * null for the main thread
675 * @return An {@link AccountManagerFuture} which resolves to an array of
676 * {@link Account}, one per account of the specified type which
677 * matches the requested features.
Fred Quintana8570f742010-02-18 10:32:54 -0800678 */
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000679 @RequiresPermission(GET_ACCOUNTS)
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700680 public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures(
681 final String type, final String[] features,
682 AccountManagerCallback<Account[]> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700683 if (type == null) throw new IllegalArgumentException("type is null");
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700684 return new Future2Task<Account[]>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800685 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700686 public void doWork() throws RemoteException {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700687 mService.getAccountsByFeatures(mResponse, type, features,
688 mContext.getOpPackageName());
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700689 }
Sandra Kwan0b84b452016-01-20 15:25:42 -0800690 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700691 public Account[] bundleToResult(Bundle bundle) throws AuthenticatorException {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700692 if (!bundle.containsKey(KEY_ACCOUNTS)) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700693 throw new AuthenticatorException("no result in response");
694 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700695 final Parcelable[] parcelables = bundle.getParcelableArray(KEY_ACCOUNTS);
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700696 Account[] descs = new Account[parcelables.length];
697 for (int i = 0; i < parcelables.length; i++) {
698 descs[i] = (Account) parcelables[i];
699 }
700 return descs;
701 }
702 }.start();
703 }
704
Fred Quintana756b7352009-10-21 13:43:10 -0700705 /**
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700706 * Adds an account directly to the AccountManager. Normally used by sign-up
Dan Egnor661f0132010-02-19 11:23:00 -0800707 * wizards associated with authenticators, not directly by applications.
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700708 * <p>Calling this method does not update the last authenticated timestamp,
709 * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
710 * {@link #notifyAccountAuthenticated(Account)} after getting success.
711 * However, if this method is called when it is triggered by addAccount() or
712 * addAccountAsUser() or similar functions, then there is no need to update
713 * timestamp manually as it is updated automatically by framework on
714 * successful completion of the mentioned functions.
Dan Egnor661f0132010-02-19 11:23:00 -0800715 * <p>It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700716 * <p>This method requires the caller to have a signature match with the
717 * authenticator that owns the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800718 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700719 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
720 * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs
721 * for this function in API level 22.
722 *
Dan Egnor661f0132010-02-19 11:23:00 -0800723 * @param account The {@link Account} to add
724 * @param password The password to associate with the account, null for none
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700725 * @param userdata String values to use for the account's userdata, null for
726 * none
Dan Egnor8e4378b2010-08-02 18:22:09 -0700727 * @return True if the account was successfully added, false if the account
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700728 * already exists, the account is null, or another error occurs.
Dan Egnor661f0132010-02-19 11:23:00 -0800729 */
730 public boolean addAccountExplicitly(Account account, String password, Bundle userdata) {
Fred Quintana382601f2010-03-25 12:25:10 -0700731 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800732 try {
Amith Yamasani27db4682013-03-30 17:07:47 -0700733 return mService.addAccountExplicitly(account, password, userdata);
Dan Egnor661f0132010-02-19 11:23:00 -0800734 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700735 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -0800736 }
737 }
738
739 /**
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -0700740 * Notifies the system that the account has just been authenticated. This
741 * information may be used by other applications to verify the account. This
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800742 * should be called only when the user has entered correct credentials for
743 * the account.
744 * <p>
745 * It is not safe to call this method from the main thread. As such, call it
746 * from another thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700747 * <p>This method requires the caller to have a signature match with the
748 * authenticator that owns the specified account.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800749 *
750 * @param account The {@link Account} to be updated.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700751 * @return boolean {@code true} if the authentication of the account has been successfully
752 * acknowledged. Otherwise {@code false}.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800753 */
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -0700754 public boolean notifyAccountAuthenticated(Account account) {
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800755 if (account == null)
756 throw new IllegalArgumentException("account is null");
757 try {
758 return mService.accountAuthenticated(account);
759 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700760 throw e.rethrowFromSystemServer();
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800761 }
762 }
763
764 /**
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -0700765 * Rename the specified {@link Account}. This is equivalent to removing
766 * the existing account and adding a new renamed account with the old
767 * account's user data.
768 *
769 * <p>It is safe to call this method from the main thread.
770 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700771 * <p>This method requires the caller to have a signature match with the
772 * authenticator that manages the specified account.
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -0700773 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700774 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
775 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
776 * is needed for those platforms. See docs for this function in API level 22.
777 *
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -0700778 * @param account The {@link Account} to rename
779 * @param newName String name to be associated with the account.
780 * @param callback Callback to invoke when the request completes, null for
781 * no callback
782 * @param handler {@link Handler} identifying the callback thread, null for
783 * the main thread
784 * @return An {@link AccountManagerFuture} which resolves to the Account
785 * after the name change. If successful the account's name will be the
786 * specified new name.
787 */
788 public AccountManagerFuture<Account> renameAccount(
789 final Account account,
Tor Norbye80b530a2015-04-23 16:36:09 -0700790 @Size(min = 1) final String newName,
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -0700791 AccountManagerCallback<Account> callback,
792 Handler handler) {
793 if (account == null) throw new IllegalArgumentException("account is null.");
794 if (TextUtils.isEmpty(newName)) {
795 throw new IllegalArgumentException("newName is empty or null.");
796 }
797 return new Future2Task<Account>(handler, callback) {
798 @Override
799 public void doWork() throws RemoteException {
800 mService.renameAccount(mResponse, account, newName);
801 }
802 @Override
803 public Account bundleToResult(Bundle bundle) throws AuthenticatorException {
804 String name = bundle.getString(KEY_ACCOUNT_NAME);
805 String type = bundle.getString(KEY_ACCOUNT_TYPE);
806 return new Account(name, type);
807 }
808 }.start();
809 }
810
811 /**
812 * Gets the previous name associated with the account or {@code null}, if
813 * none. This is intended so that clients of {@link
814 * #LOGIN_ACCOUNTS_CHANGED_ACTION} broadcasts can determine if an
815 * authenticator has renamed an account.
816 *
817 * <p>It is safe to call this method from the main thread.
818 *
819 * @param account The account to query for a previous name.
820 * @return The account's previous name, null if the account has never been
821 * renamed.
822 */
823 public String getPreviousName(final Account account) {
824 if (account == null) throw new IllegalArgumentException("account is null");
825 try {
826 return mService.getPreviousName(account);
827 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700828 throw e.rethrowFromSystemServer();
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -0700829 }
830 }
831
832 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800833 * Removes an account from the AccountManager. Does nothing if the account
834 * does not exist. Does not delete the account from the server.
835 * The authenticator may have its own policies preventing account
836 * deletion, in which case the account will not be deleted.
837 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700838 * <p>This method requires the caller to have a signature match with the
839 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800840 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700841 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
842 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
843 * this function in API level 22.
844 *
Dan Egnor661f0132010-02-19 11:23:00 -0800845 * @param account The {@link Account} to remove
846 * @param callback Callback to invoke when the request completes,
847 * null for no callback
848 * @param handler {@link Handler} identifying the callback thread,
849 * null for the main thread
850 * @return An {@link AccountManagerFuture} which resolves to a Boolean,
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100851 * true if the account has been successfully removed
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800852 * @deprecated use
853 * {@link #removeAccount(Account, Activity, AccountManagerCallback, Handler)}
854 * instead
Dan Egnor661f0132010-02-19 11:23:00 -0800855 */
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800856 @Deprecated
Dan Egnor661f0132010-02-19 11:23:00 -0800857 public AccountManagerFuture<Boolean> removeAccount(final Account account,
858 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700859 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800860 return new Future2Task<Boolean>(handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700861 @Override
Dan Egnor661f0132010-02-19 11:23:00 -0800862 public void doWork() throws RemoteException {
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800863 mService.removeAccount(mResponse, account, false);
Dan Egnor661f0132010-02-19 11:23:00 -0800864 }
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700865 @Override
Dan Egnor661f0132010-02-19 11:23:00 -0800866 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
867 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
868 throw new AuthenticatorException("no result in response");
869 }
870 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
871 }
872 }.start();
873 }
874
875 /**
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800876 * Removes an account from the AccountManager. Does nothing if the account
877 * does not exist. Does not delete the account from the server.
878 * The authenticator may have its own policies preventing account
879 * deletion, in which case the account will not be deleted.
880 *
881 * <p>This method may be called from any thread, but the returned
882 * {@link AccountManagerFuture} must not be used on the main thread.
883 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700884 * <p>This method requires the caller to have a signature match with the
885 * authenticator that manages the specified account.
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800886 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700887 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
888 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
889 * this function in API level 22.
890 *
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800891 * @param account The {@link Account} to remove
892 * @param activity The {@link Activity} context to use for launching a new
893 * authenticator-defined sub-Activity to prompt the user to delete an
894 * account; used only to call startActivity(); if null, the prompt
895 * will not be launched directly, but the {@link Intent} may be
896 * returned to the caller instead
897 * @param callback Callback to invoke when the request completes,
898 * null for no callback
899 * @param handler {@link Handler} identifying the callback thread,
900 * null for the main thread
901 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
902 * {@link #KEY_BOOLEAN_RESULT} if activity was specified and an account
903 * was removed or if active. If no activity was specified, the returned
904 * Bundle contains only {@link #KEY_INTENT} with the {@link Intent}
905 * needed to launch the actual account removal process, if authenticator
906 * needs the activity launch. If an error occurred,
907 * {@link AccountManagerFuture#getResult()} throws:
908 * <ul>
909 * <li> {@link AuthenticatorException} if no authenticator was registered for
910 * this account type or the authenticator failed to respond
911 * <li> {@link OperationCanceledException} if the operation was canceled for
912 * any reason, including the user canceling the creation process or
913 * adding accounts (of this type) has been disabled by policy
914 * </ul>
915 */
916 public AccountManagerFuture<Bundle> removeAccount(final Account account,
917 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
918 if (account == null) throw new IllegalArgumentException("account is null");
919 return new AmsTask(activity, handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700920 @Override
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800921 public void doWork() throws RemoteException {
922 mService.removeAccount(mResponse, account, activity != null);
923 }
924 }.start();
925 }
926
927 /**
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100928 * @see #removeAccount(Account, AccountManagerCallback, Handler)
929 * @hide
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800930 * @deprecated use
931 * {@link #removeAccountAsUser(Account, Activity, AccountManagerCallback, Handler)}
932 * instead
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100933 */
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800934 @Deprecated
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100935 public AccountManagerFuture<Boolean> removeAccountAsUser(final Account account,
936 AccountManagerCallback<Boolean> callback, Handler handler,
937 final UserHandle userHandle) {
938 if (account == null) throw new IllegalArgumentException("account is null");
939 if (userHandle == null) throw new IllegalArgumentException("userHandle is null");
940 return new Future2Task<Boolean>(handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700941 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100942 public void doWork() throws RemoteException {
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800943 mService.removeAccountAsUser(mResponse, account, false, userHandle.getIdentifier());
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100944 }
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700945 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100946 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
947 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
948 throw new AuthenticatorException("no result in response");
949 }
950 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
951 }
952 }.start();
953 }
954
955 /**
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800956 * @see #removeAccount(Account, Activity, AccountManagerCallback, Handler)
957 * @hide
958 */
959 public AccountManagerFuture<Bundle> removeAccountAsUser(final Account account,
960 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler,
961 final UserHandle userHandle) {
962 if (account == null)
963 throw new IllegalArgumentException("account is null");
964 if (userHandle == null)
965 throw new IllegalArgumentException("userHandle is null");
966 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800967 @Override
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800968 public void doWork() throws RemoteException {
969 mService.removeAccountAsUser(mResponse, account, activity != null,
970 userHandle.getIdentifier());
971 }
972 }.start();
973 }
974
975 /**
976 * Removes an account directly. Normally used by authenticators, not
977 * directly by applications. Does not delete the account from the server.
978 * The authenticator may have its own policies preventing account deletion,
979 * in which case the account will not be deleted.
980 * <p>
981 * It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700982 * <p>This method requires the caller to have a signature match with the
983 * authenticator that manages the specified account.
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800984 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700985 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
986 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
987 * is needed for those platforms. See docs for this function in API level 22.
988 *
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -0800989 * @param account The {@link Account} to delete.
990 * @return True if the account was successfully deleted, false if the
991 * account did not exist, the account is null, or another error
992 * occurs.
993 */
994 public boolean removeAccountExplicitly(Account account) {
995 if (account == null) throw new IllegalArgumentException("account is null");
996 try {
997 return mService.removeAccountExplicitly(account);
998 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700999 throw e.rethrowFromSystemServer();
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001000 }
1001 }
1002
1003 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001004 * Removes an auth token from the AccountManager's cache. Does nothing if
1005 * the auth token is not currently in the cache. Applications must call this
1006 * method when the auth token is found to have expired or otherwise become
1007 * invalid for authenticating requests. The AccountManager does not validate
1008 * or expire cached auth tokens otherwise.
1009 *
1010 * <p>It is safe to call this method from the main thread.
1011 *
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 or USE_CREDENTIALS permission is needed for those
1014 * platforms. See docs for this function in API level 22.
1015 *
Fred Quintanaf35b68f2010-04-01 11:36:00 -07001016 * @param accountType The account type of the auth token to invalidate, must not be null
1017 * @param authToken The auth token to invalidate, may be null
Dan Egnor661f0132010-02-19 11:23:00 -08001018 */
1019 public void invalidateAuthToken(final String accountType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -07001020 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001021 try {
Fred Quintanaf35b68f2010-04-01 11:36:00 -07001022 if (authToken != null) {
1023 mService.invalidateAuthToken(accountType, authToken);
1024 }
Dan Egnor661f0132010-02-19 11:23:00 -08001025 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001026 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001027 }
1028 }
1029
1030 /**
1031 * Gets an auth token from the AccountManager's cache. If no auth
1032 * token is cached for this account, null will be returned -- a new
1033 * auth token will not be generated, and the server will not be contacted.
1034 * Intended for use by the authenticator, not directly by applications.
1035 *
1036 * <p>It is safe to call this method from the main thread.
1037 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001038 * <p>This method requires the caller to have a signature match with the
1039 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001040 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001041 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1042 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1043 * is needed for those platforms. See docs for this function in API level 22.
1044 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001045 * @param account The account for which an auth token is to be fetched. Cannot be {@code null}.
Sandra Kwan0b84b452016-01-20 15:25:42 -08001046 * @param authTokenType The type of auth token to fetch. Cannot be {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -08001047 * @return The cached auth token for this account and type, or null if
1048 * no auth token is cached or the account does not exist.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001049 * @see #getAuthToken
Dan Egnor661f0132010-02-19 11:23:00 -08001050 */
1051 public String peekAuthToken(final Account account, final String authTokenType) {
Fred Quintana382601f2010-03-25 12:25:10 -07001052 if (account == null) throw new IllegalArgumentException("account is null");
1053 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001054 try {
1055 return mService.peekAuthToken(account, authTokenType);
1056 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001057 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001058 }
1059 }
1060
1061 /**
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -07001062 * Sets or forgets a saved password. This modifies the local copy of the
1063 * password used to automatically authenticate the user; it does not change
1064 * the user's account password on the server. Intended for use by the
1065 * authenticator, not directly by applications.
1066 * <p>Calling this method does not update the last authenticated timestamp,
1067 * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
1068 * {@link #notifyAccountAuthenticated(Account)} after getting success.
Dan Egnor661f0132010-02-19 11:23:00 -08001069 * <p>It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001070 * <p>This method requires the caller to have a signature match with the
1071 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001072 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001073 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1074 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1075 * is needed for those platforms. See docs for this function in API level 22.
1076 *
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -07001077 * @param account The account whose password is to be set. Cannot be
1078 * {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -08001079 * @param password The password to set, null to clear the password
1080 */
1081 public void setPassword(final Account account, final String password) {
Fred Quintana382601f2010-03-25 12:25:10 -07001082 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001083 try {
1084 mService.setPassword(account, password);
1085 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001086 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001087 }
1088 }
1089
1090 /**
1091 * Forgets a saved password. This erases the local copy of the password;
1092 * it does not change the user's account password on the server.
1093 * Has the same effect as setPassword(account, null) but requires fewer
1094 * permissions, and may be used by applications or management interfaces
1095 * to "sign out" from an account.
1096 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001097 * <p>This method only successfully clear the account's password when the
1098 * caller has the same signature as the authenticator that owns the
1099 * specified account. Otherwise, this method will silently fail.
Dan Egnor661f0132010-02-19 11:23:00 -08001100 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001101 * <p>It is safe to call this method from the main thread.
Carlos Valdiviadcddc472015-06-11 20:04:04 +00001102 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001103 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1104 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1105 * this function in API level 22.
1106 *
Dan Egnor661f0132010-02-19 11:23:00 -08001107 * @param account The account whose password to clear
1108 */
1109 public void clearPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -07001110 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001111 try {
1112 mService.clearPassword(account);
1113 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001114 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001115 }
1116 }
1117
1118 /**
1119 * Sets one userdata key for an account. Intended by use for the
1120 * authenticator to stash state for itself, not directly by applications.
1121 * The meaning of the keys and values is up to the authenticator.
1122 *
1123 * <p>It is safe to call this method from the main thread.
1124 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001125 * <p>This method requires the caller to have a signature match with the
1126 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001127 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001128 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1129 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1130 * is needed for those platforms. See docs for this function in API level 22.
1131 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001132 * @param account Account whose user data is to be set. Must not be {@code null}.
1133 * @param key String user data key to set. Must not be null
1134 * @param value String value to set, {@code null} to clear this user data key
Dan Egnor661f0132010-02-19 11:23:00 -08001135 */
1136 public void setUserData(final Account account, final String key, final String value) {
Fred Quintana382601f2010-03-25 12:25:10 -07001137 if (account == null) throw new IllegalArgumentException("account is null");
1138 if (key == null) throw new IllegalArgumentException("key is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001139 try {
1140 mService.setUserData(account, key, value);
1141 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001142 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001143 }
1144 }
1145
1146 /**
1147 * Adds an auth token to the AccountManager cache for an account.
1148 * If the account does not exist then this call has no effect.
1149 * Replaces any previous auth token for this account and auth token type.
1150 * Intended for use by the authenticator, not directly by applications.
1151 *
1152 * <p>It is safe to call this method from the main thread.
1153 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001154 * <p>This method requires the caller to have a signature match with the
1155 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001156 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001157 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1158 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1159 * is needed for those platforms. See docs for this function in API level 22.
1160 *
Dan Egnor661f0132010-02-19 11:23:00 -08001161 * @param account The account to set an auth token for
1162 * @param authTokenType The type of the auth token, see {#getAuthToken}
1163 * @param authToken The auth token to add to the cache
1164 */
1165 public void setAuthToken(Account account, final String authTokenType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -07001166 if (account == null) throw new IllegalArgumentException("account is null");
1167 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001168 try {
1169 mService.setAuthToken(account, authTokenType, authToken);
1170 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001171 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001172 }
1173 }
1174
1175 /**
1176 * This convenience helper synchronously gets an auth token with
1177 * {@link #getAuthToken(Account, String, boolean, AccountManagerCallback, Handler)}.
1178 *
1179 * <p>This method may block while a network request completes, and must
1180 * never be made from the main thread.
1181 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001182 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1183 * USE_CREDENTIALS permission is needed for those platforms. See docs for
1184 * this function in API level 22.
1185 *
Dan Egnor661f0132010-02-19 11:23:00 -08001186 * @param account The account to fetch an auth token for
Joe Malinb6a35262013-06-03 09:49:04 -07001187 * @param authTokenType The auth token type, see {@link #getAuthToken getAuthToken()}
Dan Egnor661f0132010-02-19 11:23:00 -08001188 * @param notifyAuthFailure If true, display a notification and return null
1189 * if authentication fails; if false, prompt and wait for the user to
1190 * re-enter correct credentials before returning
1191 * @return An auth token of the specified type for this account, or null
1192 * if authentication fails or none can be fetched.
1193 * @throws AuthenticatorException if the authenticator failed to respond
1194 * @throws OperationCanceledException if the request was canceled for any
1195 * reason, including the user canceling a credential request
1196 * @throws java.io.IOException if the authenticator experienced an I/O problem
1197 * creating a new auth token, usually because of network trouble
1198 */
1199 public String blockingGetAuthToken(Account account, String authTokenType,
1200 boolean notifyAuthFailure)
1201 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana382601f2010-03-25 12:25:10 -07001202 if (account == null) throw new IllegalArgumentException("account is null");
1203 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001204 Bundle bundle = getAuthToken(account, authTokenType, notifyAuthFailure, null /* callback */,
1205 null /* handler */).getResult();
Fred Quintana96580e02010-03-04 13:42:42 -08001206 if (bundle == null) {
1207 // This should never happen, but it does, occasionally. If it does return null to
1208 // signify that we were not able to get the authtoken.
1209 // TODO: remove this when the bug is found that sometimes causes a null bundle to be
1210 // returned
1211 Log.e(TAG, "blockingGetAuthToken: null was returned from getResult() for "
1212 + account + ", authTokenType " + authTokenType);
1213 return null;
1214 }
Dan Egnor661f0132010-02-19 11:23:00 -08001215 return bundle.getString(KEY_AUTHTOKEN);
1216 }
1217
1218 /**
1219 * Gets an auth token of the specified type for a particular account,
1220 * prompting the user for credentials if necessary. This method is
1221 * intended for applications running in the foreground where it makes
1222 * sense to ask the user directly for a password.
1223 *
1224 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -07001225 * type, then it is returned. Otherwise, if a saved password is
1226 * available, it is sent to the server to generate a new auth token.
1227 * Otherwise, the user is prompted to enter a password.
Dan Egnor661f0132010-02-19 11:23:00 -08001228 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001229 * <p>Some authenticators have auth token <em>types</em>, whose value
1230 * is authenticator-dependent. Some services use different token types to
1231 * access different functionality -- for example, Google uses different auth
1232 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -08001233 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001234 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
Sandra Kwan0b84b452016-01-20 15:25:42 -08001235 * USE_CREDENTIALS permission is needed for those platforms. See docs for
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001236 * this function in API level 22.
1237 *
Dan Egnor661f0132010-02-19 11:23:00 -08001238 * <p>This method may be called from any thread, but the returned
1239 * {@link AccountManagerFuture} must not be used on the main thread.
1240 *
Dan Egnor661f0132010-02-19 11:23:00 -08001241 * @param account The account to fetch an auth token for
1242 * @param authTokenType The auth token type, an authenticator-dependent
1243 * string token, must not be null
1244 * @param options Authenticator-specific options for the request,
1245 * may be null or empty
1246 * @param activity The {@link Activity} context to use for launching a new
1247 * authenticator-defined sub-Activity to prompt the user for a password
1248 * if necessary; used only to call startActivity(); must not be null.
1249 * @param callback Callback to invoke when the request completes,
1250 * null for no callback
1251 * @param handler {@link Handler} identifying the callback thread,
1252 * null for the main thread
1253 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1254 * at least the following fields:
Fred Quintana756b7352009-10-21 13:43:10 -07001255 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001256 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1257 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1258 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
Fred Quintana756b7352009-10-21 13:43:10 -07001259 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001260 *
1261 * (Other authenticator-specific values may be returned.) If an auth token
1262 * could not be fetched, {@link AccountManagerFuture#getResult()} throws:
1263 * <ul>
1264 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1265 * <li> {@link OperationCanceledException} if the operation is canceled for
1266 * any reason, incluidng the user canceling a credential request
1267 * <li> {@link IOException} if the authenticator experienced an I/O problem
1268 * creating a new auth token, usually because of network trouble
1269 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -07001270 * If the account is no longer present on the device, the return value is
1271 * authenticator-dependent. The caller should verify the validity of the
1272 * account before requesting an auth token.
Dan Egnor661f0132010-02-19 11:23:00 -08001273 */
1274 public AccountManagerFuture<Bundle> getAuthToken(
1275 final Account account, final String authTokenType, final Bundle options,
1276 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001277 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001278 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001279 final Bundle optionsIn = new Bundle();
1280 if (options != null) {
1281 optionsIn.putAll(options);
1282 }
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001283 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Dan Egnor661f0132010-02-19 11:23:00 -08001284 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001285 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001286 public void doWork() throws RemoteException {
1287 mService.getAuthToken(mResponse, account, authTokenType,
1288 false /* notifyOnAuthFailure */, true /* expectActivityLaunch */,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001289 optionsIn);
Dan Egnor661f0132010-02-19 11:23:00 -08001290 }
1291 }.start();
1292 }
1293
1294 /**
1295 * Gets an auth token of the specified type for a particular account,
1296 * optionally raising a notification if the user must enter credentials.
1297 * This method is intended for background tasks and services where the
1298 * user should not be immediately interrupted with a password prompt.
1299 *
1300 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -07001301 * type, then it is returned. Otherwise, if a saved password is
1302 * available, it is sent to the server to generate a new auth token.
1303 * Otherwise, an {@link Intent} is returned which, when started, will
1304 * prompt the user for a password. If the notifyAuthFailure parameter is
1305 * set, a status bar notification is also created with the same Intent,
Dan Egnor661f0132010-02-19 11:23:00 -08001306 * alerting the user that they need to enter a password at some point.
1307 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001308 * <p>In that case, you may need to wait until the user responds, which
1309 * could take hours or days or forever. When the user does respond and
1310 * supply a new password, the account manager will broadcast the
1311 * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent, which applications can
1312 * use to try again.
Dan Egnor661f0132010-02-19 11:23:00 -08001313 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001314 * <p>If notifyAuthFailure is not set, it is the application's
1315 * responsibility to launch the returned Intent at some point.
1316 * Either way, the result from this call will not wait for user action.
Dan Egnor661f0132010-02-19 11:23:00 -08001317 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001318 * <p>Some authenticators have auth token <em>types</em>, whose value
1319 * is authenticator-dependent. Some services use different token types to
1320 * access different functionality -- for example, Google uses different auth
1321 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -08001322 *
1323 * <p>This method may be called from any thread, but the returned
1324 * {@link AccountManagerFuture} must not be used on the main thread.
1325 *
Dan Egnor661f0132010-02-19 11:23:00 -08001326 * @param account The account to fetch an auth token for
1327 * @param authTokenType The auth token type, an authenticator-dependent
1328 * string token, must not be null
Dan Egnor661f0132010-02-19 11:23:00 -08001329 * @param notifyAuthFailure True to add a notification to prompt the
1330 * user for a password if necessary, false to leave that to the caller
1331 * @param callback Callback to invoke when the request completes,
1332 * null for no callback
1333 * @param handler {@link Handler} identifying the callback thread,
1334 * null for the main thread
1335 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1336 * at least the following fields on success:
1337 * <ul>
1338 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1339 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1340 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
1341 * </ul>
1342 *
1343 * (Other authenticator-specific values may be returned.) If the user
1344 * must enter credentials, the returned Bundle contains only
1345 * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt.
1346 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001347 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001348 * <ul>
1349 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1350 * <li> {@link OperationCanceledException} if the operation is canceled for
1351 * any reason, incluidng the user canceling a credential request
1352 * <li> {@link IOException} if the authenticator experienced an I/O problem
1353 * creating a new auth token, usually because of network trouble
1354 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -07001355 * If the account is no longer present on the device, the return value is
1356 * authenticator-dependent. The caller should verify the validity of the
1357 * account before requesting an auth token.
Fred Quintanaad93a322011-09-08 13:21:01 -07001358 * @deprecated use {@link #getAuthToken(Account, String, android.os.Bundle,
1359 * boolean, AccountManagerCallback, android.os.Handler)} instead
Dan Egnor661f0132010-02-19 11:23:00 -08001360 */
Fred Quintanaad93a322011-09-08 13:21:01 -07001361 @Deprecated
Dan Egnor661f0132010-02-19 11:23:00 -08001362 public AccountManagerFuture<Bundle> getAuthToken(
Jatin Lodhia3df7d692013-03-27 10:57:23 -07001363 final Account account, final String authTokenType,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001364 final boolean notifyAuthFailure,
Dan Egnor661f0132010-02-19 11:23:00 -08001365 AccountManagerCallback<Bundle> callback, Handler handler) {
Jatin Lodhia3df7d692013-03-27 10:57:23 -07001366 return getAuthToken(account, authTokenType, null, notifyAuthFailure, callback,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001367 handler);
Dan Egnor661f0132010-02-19 11:23:00 -08001368 }
1369
1370 /**
Fred Quintanaad93a322011-09-08 13:21:01 -07001371 * Gets an auth token of the specified type for a particular account,
1372 * optionally raising a notification if the user must enter credentials.
1373 * This method is intended for background tasks and services where the
1374 * user should not be immediately interrupted with a password prompt.
1375 *
1376 * <p>If a previously generated auth token is cached for this account and
1377 * type, then it is returned. Otherwise, if a saved password is
1378 * available, it is sent to the server to generate a new auth token.
1379 * Otherwise, an {@link Intent} is returned which, when started, will
1380 * prompt the user for a password. If the notifyAuthFailure parameter is
1381 * set, a status bar notification is also created with the same Intent,
1382 * alerting the user that they need to enter a password at some point.
1383 *
1384 * <p>In that case, you may need to wait until the user responds, which
1385 * could take hours or days or forever. When the user does respond and
1386 * supply a new password, the account manager will broadcast the
1387 * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent, which applications can
1388 * use to try again.
1389 *
1390 * <p>If notifyAuthFailure is not set, it is the application's
1391 * responsibility to launch the returned Intent at some point.
1392 * Either way, the result from this call will not wait for user action.
1393 *
1394 * <p>Some authenticators have auth token <em>types</em>, whose value
1395 * is authenticator-dependent. Some services use different token types to
1396 * access different functionality -- for example, Google uses different auth
1397 * tokens to access Gmail and Google Calendar for the same account.
1398 *
1399 * <p>This method may be called from any thread, but the returned
1400 * {@link AccountManagerFuture} must not be used on the main thread.
1401 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001402 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
Sandra Kwan0b84b452016-01-20 15:25:42 -08001403 * USE_CREDENTIALS permission is needed for those platforms. See docs for
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001404 * this function in API level 22.
1405 *
Fred Quintanaad93a322011-09-08 13:21:01 -07001406 * @param account The account to fetch an auth token for
1407 * @param authTokenType The auth token type, an authenticator-dependent
1408 * string token, must not be null
1409 * @param options Authenticator-specific options for the request,
1410 * may be null or empty
1411 * @param notifyAuthFailure True to add a notification to prompt the
1412 * user for a password if necessary, false to leave that to the caller
1413 * @param callback Callback to invoke when the request completes,
1414 * null for no callback
1415 * @param handler {@link Handler} identifying the callback thread,
1416 * null for the main thread
1417 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1418 * at least the following fields on success:
1419 * <ul>
1420 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1421 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1422 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
1423 * </ul>
1424 *
1425 * (Other authenticator-specific values may be returned.) If the user
1426 * must enter credentials, the returned Bundle contains only
1427 * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt.
1428 *
1429 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
1430 * <ul>
1431 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1432 * <li> {@link OperationCanceledException} if the operation is canceled for
1433 * any reason, incluidng the user canceling a credential request
1434 * <li> {@link IOException} if the authenticator experienced an I/O problem
1435 * creating a new auth token, usually because of network trouble
1436 * </ul>
1437 * If the account is no longer present on the device, the return value is
1438 * authenticator-dependent. The caller should verify the validity of the
1439 * account before requesting an auth token.
1440 */
1441 public AccountManagerFuture<Bundle> getAuthToken(
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001442 final Account account, final String authTokenType, final Bundle options,
1443 final boolean notifyAuthFailure,
Fred Quintanaad93a322011-09-08 13:21:01 -07001444 AccountManagerCallback<Bundle> callback, Handler handler) {
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001445
Fred Quintanaad93a322011-09-08 13:21:01 -07001446 if (account == null) throw new IllegalArgumentException("account is null");
1447 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001448 final Bundle optionsIn = new Bundle();
1449 if (options != null) {
1450 optionsIn.putAll(options);
1451 }
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001452 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Fred Quintanaad93a322011-09-08 13:21:01 -07001453 return new AmsTask(null, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001454 @Override
Fred Quintanaad93a322011-09-08 13:21:01 -07001455 public void doWork() throws RemoteException {
1456 mService.getAuthToken(mResponse, account, authTokenType,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001457 notifyAuthFailure, false /* expectActivityLaunch */, optionsIn);
Fred Quintanaad93a322011-09-08 13:21:01 -07001458 }
1459 }.start();
1460 }
1461
1462 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001463 * Asks the user to add an account of a specified type. The authenticator
1464 * for this account type processes this request with the appropriate user
1465 * interface. If the user does elect to create a new account, the account
1466 * name is returned.
1467 *
1468 * <p>This method may be called from any thread, but the returned
1469 * {@link AccountManagerFuture} must not be used on the main thread.
Sandra Kwan0b84b452016-01-20 15:25:42 -08001470 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001471 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1472 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1473 * this function in API level 22.
Dan Egnor661f0132010-02-19 11:23:00 -08001474 *
Dan Egnor661f0132010-02-19 11:23:00 -08001475 * @param accountType The type of account to add; must not be null
1476 * @param authTokenType The type of auth token (see {@link #getAuthToken})
1477 * this account will need to be able to generate, null for none
1478 * @param requiredFeatures The features (see {@link #hasFeatures}) this
1479 * account must have, null for none
1480 * @param addAccountOptions Authenticator-specific options for the request,
1481 * may be null or empty
1482 * @param activity The {@link Activity} context to use for launching a new
1483 * authenticator-defined sub-Activity to prompt the user to create an
1484 * account; used only to call startActivity(); if null, the prompt
1485 * will not be launched directly, but the necessary {@link Intent}
1486 * will be returned to the caller instead
1487 * @param callback Callback to invoke when the request completes,
1488 * null for no callback
1489 * @param handler {@link Handler} identifying the callback thread,
1490 * null for the main thread
Doug Zongkerff592dc2010-02-23 12:26:33 -08001491 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
Dan Egnor661f0132010-02-19 11:23:00 -08001492 * these fields if activity was specified and an account was created:
1493 * <ul>
1494 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created
1495 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1496 * </ul>
1497 *
1498 * If no activity was specified, the returned Bundle contains only
1499 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Dan Egnor8e4378b2010-08-02 18:22:09 -07001500 * actual account creation process. If an error occurred,
1501 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001502 * <ul>
1503 * <li> {@link AuthenticatorException} if no authenticator was registered for
1504 * this account type or the authenticator failed to respond
1505 * <li> {@link OperationCanceledException} if the operation was canceled for
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001506 * any reason, including the user canceling the creation process or adding accounts
1507 * (of this type) has been disabled by policy
Dan Egnor661f0132010-02-19 11:23:00 -08001508 * <li> {@link IOException} if the authenticator experienced an I/O problem
1509 * creating a new account, usually because of network trouble
1510 * </ul>
1511 */
1512 public AccountManagerFuture<Bundle> addAccount(final String accountType,
1513 final String authTokenType, final String[] requiredFeatures,
1514 final Bundle addAccountOptions,
1515 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001516 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001517 final Bundle optionsIn = new Bundle();
1518 if (addAccountOptions != null) {
1519 optionsIn.putAll(addAccountOptions);
1520 }
1521 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001522
Dan Egnor661f0132010-02-19 11:23:00 -08001523 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001524 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001525 public void doWork() throws RemoteException {
Amith Yamasani27db4682013-03-30 17:07:47 -07001526 mService.addAccount(mResponse, accountType, authTokenType,
Costin Manolachee5847ad2011-09-14 12:52:19 -07001527 requiredFeatures, activity != null, optionsIn);
Dan Egnor661f0132010-02-19 11:23:00 -08001528 }
1529 }.start();
1530 }
1531
1532 /**
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001533 * @see #addAccount(String, String, String[], Bundle, Activity, AccountManagerCallback, Handler)
1534 * @hide
1535 */
1536 public AccountManagerFuture<Bundle> addAccountAsUser(final String accountType,
1537 final String authTokenType, final String[] requiredFeatures,
1538 final Bundle addAccountOptions, final Activity activity,
1539 AccountManagerCallback<Bundle> callback, Handler handler, final UserHandle userHandle) {
1540 if (accountType == null) throw new IllegalArgumentException("accountType is null");
1541 if (userHandle == null) throw new IllegalArgumentException("userHandle is null");
1542 final Bundle optionsIn = new Bundle();
1543 if (addAccountOptions != null) {
1544 optionsIn.putAll(addAccountOptions);
1545 }
1546 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
1547
1548 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001549 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001550 public void doWork() throws RemoteException {
1551 mService.addAccountAsUser(mResponse, accountType, authTokenType,
1552 requiredFeatures, activity != null, optionsIn, userHandle.getIdentifier());
1553 }
1554 }.start();
1555 }
1556
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001557
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001558 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001559 * Adds shared accounts from a parent user to a secondary user. Adding the shared account
Amith Yamasani67df64b2012-12-14 12:09:36 -08001560 * doesn't take effect immediately. When the target user starts up, any pending shared accounts
1561 * are attempted to be copied to the target user from the primary via calls to the
1562 * authenticator.
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001563 * @param parentUser parent user
1564 * @param user target user
Amith Yamasani67df64b2012-12-14 12:09:36 -08001565 * @hide
1566 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001567 public void addSharedAccountsFromParentUser(UserHandle parentUser, UserHandle user) {
Amith Yamasani67df64b2012-12-14 12:09:36 -08001568 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001569 mService.addSharedAccountsFromParentUser(parentUser.getIdentifier(),
1570 user.getIdentifier());
Amith Yamasani67df64b2012-12-14 12:09:36 -08001571 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001572 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001573 }
1574 }
1575
1576 /**
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001577 * Copies an account from one user to another user.
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001578 * @param account the account to copy
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001579 * @param fromUser the user to copy the account from
1580 * @param toUser the target user
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001581 * @param callback Callback to invoke when the request completes,
1582 * null for no callback
1583 * @param handler {@link Handler} identifying the callback thread,
1584 * null for the main thread
1585 * @return An {@link AccountManagerFuture} which resolves to a Boolean indicated wether it
1586 * succeeded.
1587 * @hide
1588 */
1589 public AccountManagerFuture<Boolean> copyAccountToUser(
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001590 final Account account, final UserHandle fromUser, final UserHandle toUser,
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001591 AccountManagerCallback<Boolean> callback, Handler handler) {
1592 if (account == null) throw new IllegalArgumentException("account is null");
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001593 if (toUser == null || fromUser == null) {
1594 throw new IllegalArgumentException("fromUser and toUser cannot be null");
1595 }
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001596
1597 return new Future2Task<Boolean>(handler, callback) {
1598 @Override
1599 public void doWork() throws RemoteException {
1600 mService.copyAccountToUser(
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001601 mResponse, account, fromUser.getIdentifier(), toUser.getIdentifier());
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001602 }
1603 @Override
1604 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1605 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1606 throw new AuthenticatorException("no result in response");
1607 }
1608 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1609 }
1610 }.start();
1611 }
1612
1613 /**
Amith Yamasani67df64b2012-12-14 12:09:36 -08001614 * @hide
1615 * Removes the shared account.
1616 * @param account the account to remove
1617 * @param user the user to remove the account from
1618 * @return
1619 */
1620 public boolean removeSharedAccount(final Account account, UserHandle user) {
1621 try {
1622 boolean val = mService.removeSharedAccountAsUser(account, user.getIdentifier());
1623 return val;
1624 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001625 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001626 }
1627 }
1628
1629 /**
1630 * @hide
1631 * @param user
1632 * @return
1633 */
1634 public Account[] getSharedAccounts(UserHandle user) {
1635 try {
1636 return mService.getSharedAccountsAsUser(user.getIdentifier());
1637 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001638 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001639 }
1640 }
1641
1642 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001643 * Confirms that the user knows the password for an account to make extra
1644 * sure they are the owner of the account. The user-entered password can
1645 * be supplied directly, otherwise the authenticator for this account type
1646 * prompts the user with the appropriate interface. This method is
1647 * intended for applications which want extra assurance; for example, the
1648 * phone lock screen uses this to let the user unlock the phone with an
1649 * account password if they forget the lock pattern.
1650 *
1651 * <p>If the user-entered password matches a saved password for this
1652 * account, the request is considered valid; otherwise the authenticator
1653 * verifies the password (usually by contacting the server).
1654 *
1655 * <p>This method may be called from any thread, but the returned
1656 * {@link AccountManagerFuture} must not be used on the main thread.
1657 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001658 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1659 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs
1660 * for this function in API level 22.
1661 *
Dan Egnor661f0132010-02-19 11:23:00 -08001662 * @param account The account to confirm password knowledge for
1663 * @param options Authenticator-specific options for the request;
1664 * if the {@link #KEY_PASSWORD} string field is present, the
1665 * authenticator may use it directly rather than prompting the user;
1666 * may be null or empty
1667 * @param activity The {@link Activity} context to use for launching a new
1668 * authenticator-defined sub-Activity to prompt the user to enter a
1669 * password; used only to call startActivity(); if null, the prompt
1670 * will not be launched directly, but the necessary {@link Intent}
1671 * will be returned to the caller instead
1672 * @param callback Callback to invoke when the request completes,
1673 * null for no callback
1674 * @param handler {@link Handler} identifying the callback thread,
1675 * null for the main thread
1676 * @return An {@link AccountManagerFuture} which resolves to a Bundle
1677 * with these fields if activity or password was supplied and
1678 * the account was successfully verified:
1679 * <ul>
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001680 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account verified
Dan Egnor661f0132010-02-19 11:23:00 -08001681 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1682 * <li> {@link #KEY_BOOLEAN_RESULT} - true to indicate success
1683 * </ul>
1684 *
1685 * If no activity or password was specified, the returned Bundle contains
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001686 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
1687 * password prompt.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001688 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001689 * <p>Also the returning Bundle may contain {@link
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -07001690 * #KEY_LAST_AUTHENTICATED_TIME} indicating the last time the
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001691 * credential was validated/created.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001692 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001693 * If an error occurred,{@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001694 * <ul>
1695 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1696 * <li> {@link OperationCanceledException} if the operation was canceled for
1697 * any reason, including the user canceling the password prompt
1698 * <li> {@link IOException} if the authenticator experienced an I/O problem
1699 * verifying the password, usually because of network trouble
1700 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001701 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001702 public AccountManagerFuture<Bundle> confirmCredentials(final Account account,
1703 final Bundle options,
1704 final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001705 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07001706 final Handler handler) {
Amith Yamasani2c7bc262012-11-05 16:46:02 -08001707 return confirmCredentialsAsUser(account, options, activity, callback, handler,
1708 Process.myUserHandle());
1709 }
1710
1711 /**
1712 * @hide
1713 * Same as {@link #confirmCredentials(Account, Bundle, Activity, AccountManagerCallback, Handler)}
1714 * but for the specified user.
1715 */
1716 public AccountManagerFuture<Bundle> confirmCredentialsAsUser(final Account account,
1717 final Bundle options,
1718 final Activity activity,
1719 final AccountManagerCallback<Bundle> callback,
1720 final Handler handler, UserHandle userHandle) {
Fred Quintana382601f2010-03-25 12:25:10 -07001721 if (account == null) throw new IllegalArgumentException("account is null");
Amith Yamasani2c7bc262012-11-05 16:46:02 -08001722 final int userId = userHandle.getIdentifier();
Fred Quintanaa698f422009-04-08 19:14:54 -07001723 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001724 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07001725 public void doWork() throws RemoteException {
Amith Yamasani2c7bc262012-11-05 16:46:02 -08001726 mService.confirmCredentialsAsUser(mResponse, account, options, activity != null,
1727 userId);
Fred Quintanaa698f422009-04-08 19:14:54 -07001728 }
Fred Quintana33269202009-04-20 16:05:10 -07001729 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07001730 }
1731
Fred Quintana756b7352009-10-21 13:43:10 -07001732 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001733 * Asks the user to enter a new password for an account, updating the
1734 * saved credentials for the account. Normally this happens automatically
1735 * when the server rejects credentials during an auth token fetch, but this
1736 * can be invoked directly to ensure we have the correct credentials stored.
Fred Quintana756b7352009-10-21 13:43:10 -07001737 *
Dan Egnor661f0132010-02-19 11:23:00 -08001738 * <p>This method may be called from any thread, but the returned
1739 * {@link AccountManagerFuture} must not be used on the main thread.
1740 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001741 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1742 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1743 * this function in API level 22.
1744 *
Dan Egnor661f0132010-02-19 11:23:00 -08001745 * @param account The account to update credentials for
1746 * @param authTokenType The credentials entered must allow an auth token
1747 * of this type to be created (but no actual auth token is returned);
1748 * may be null
1749 * @param options Authenticator-specific options for the request;
1750 * may be null or empty
1751 * @param activity The {@link Activity} context to use for launching a new
1752 * authenticator-defined sub-Activity to prompt the user to enter a
1753 * password; used only to call startActivity(); if null, the prompt
1754 * will not be launched directly, but the necessary {@link Intent}
1755 * will be returned to the caller instead
1756 * @param callback Callback to invoke when the request completes,
1757 * null for no callback
1758 * @param handler {@link Handler} identifying the callback thread,
1759 * null for the main thread
1760 * @return An {@link AccountManagerFuture} which resolves to a Bundle
1761 * with these fields if an activity was supplied and the account
1762 * credentials were successfully updated:
Fred Quintana756b7352009-10-21 13:43:10 -07001763 * <ul>
Ian Pedowitz6cc066d2015-08-05 14:23:43 +00001764 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account
Dan Egnor661f0132010-02-19 11:23:00 -08001765 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
Fred Quintana756b7352009-10-21 13:43:10 -07001766 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001767 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001768 * If no activity was specified, the returned Bundle contains
Dan Egnor661f0132010-02-19 11:23:00 -08001769 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001770 * password prompt. If an error occurred,
Dan Egnor8e4378b2010-08-02 18:22:09 -07001771 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001772 * <ul>
1773 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1774 * <li> {@link OperationCanceledException} if the operation was canceled for
1775 * any reason, including the user canceling the password prompt
1776 * <li> {@link IOException} if the authenticator experienced an I/O problem
1777 * verifying the password, usually because of network trouble
1778 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001779 */
1780 public AccountManagerFuture<Bundle> updateCredentials(final Account account,
1781 final String authTokenType,
Fred Quintana31957f12009-10-21 13:43:10 -07001782 final Bundle options, final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001783 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07001784 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001785 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07001786 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001787 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07001788 public void doWork() throws RemoteException {
1789 mService.updateCredentials(mResponse, account, authTokenType, activity != null,
Fred Quintana31957f12009-10-21 13:43:10 -07001790 options);
Fred Quintanaa698f422009-04-08 19:14:54 -07001791 }
Fred Quintana33269202009-04-20 16:05:10 -07001792 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07001793 }
1794
Fred Quintana756b7352009-10-21 13:43:10 -07001795 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001796 * Offers the user an opportunity to change an authenticator's settings.
1797 * These properties are for the authenticator in general, not a particular
1798 * account. Not all authenticators support this method.
Fred Quintana756b7352009-10-21 13:43:10 -07001799 *
Dan Egnor661f0132010-02-19 11:23:00 -08001800 * <p>This method may be called from any thread, but the returned
1801 * {@link AccountManagerFuture} must not be used on the main thread.
1802 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001803 * <p>This method requires the caller to have the same signature as the
1804 * authenticator associated with the specified account type.
Dan Egnor661f0132010-02-19 11:23:00 -08001805 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001806 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1807 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs
1808 * for this function in API level 22.
1809 *
Dan Egnor661f0132010-02-19 11:23:00 -08001810 * @param accountType The account type associated with the authenticator
1811 * to adjust
1812 * @param activity The {@link Activity} context to use for launching a new
1813 * authenticator-defined sub-Activity to adjust authenticator settings;
1814 * used only to call startActivity(); if null, the settings dialog will
1815 * not be launched directly, but the necessary {@link Intent} will be
1816 * returned to the caller instead
1817 * @param callback Callback to invoke when the request completes,
1818 * null for no callback
1819 * @param handler {@link Handler} identifying the callback thread,
1820 * null for the main thread
1821 * @return An {@link AccountManagerFuture} which resolves to a Bundle
1822 * which is empty if properties were edited successfully, or
1823 * if no activity was specified, contains only {@link #KEY_INTENT}
1824 * needed to launch the authenticator's settings dialog.
Dan Egnor8e4378b2010-08-02 18:22:09 -07001825 * If an error occurred, {@link AccountManagerFuture#getResult()}
1826 * throws:
Fred Quintana756b7352009-10-21 13:43:10 -07001827 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001828 * <li> {@link AuthenticatorException} if no authenticator was registered for
1829 * this account type or the authenticator failed to respond
1830 * <li> {@link OperationCanceledException} if the operation was canceled for
1831 * any reason, including the user canceling the settings dialog
1832 * <li> {@link IOException} if the authenticator experienced an I/O problem
1833 * updating settings, usually because of network trouble
Fred Quintana756b7352009-10-21 13:43:10 -07001834 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001835 */
1836 public AccountManagerFuture<Bundle> editProperties(final String accountType,
1837 final Activity activity, final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07001838 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001839 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07001840 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001841 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07001842 public void doWork() throws RemoteException {
1843 mService.editProperties(mResponse, accountType, activity != null);
1844 }
Fred Quintana33269202009-04-20 16:05:10 -07001845 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07001846 }
1847
Amith Yamasani12747872015-12-07 14:19:49 -08001848 /**
1849 * @hide
1850 * Checks if the given account exists on any of the users on the device.
1851 * Only the system process can call this method.
1852 *
1853 * @param account The account to check for existence.
1854 * @return whether any user has this account
1855 */
1856 public boolean someUserHasAccount(@NonNull final Account account) {
1857 try {
1858 return mService.someUserHasAccount(account);
1859 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001860 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08001861 }
1862 }
1863
Fred Quintanaa698f422009-04-08 19:14:54 -07001864 private void ensureNotOnMainThread() {
1865 final Looper looper = Looper.myLooper();
1866 if (looper != null && looper == mContext.getMainLooper()) {
Fred Quintana53bd2522010-02-05 15:28:12 -08001867 final IllegalStateException exception = new IllegalStateException(
1868 "calling this from your main thread can lead to deadlock");
1869 Log.e(TAG, "calling this from your main thread can lead to deadlock and/or ANRs",
1870 exception);
Fred Quintana751fdc02010-02-09 14:13:18 -08001871 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.FROYO) {
1872 throw exception;
1873 }
Fred Quintana60307342009-03-24 22:48:12 -07001874 }
1875 }
1876
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001877 private void postToHandler(Handler handler, final AccountManagerCallback<Bundle> callback,
1878 final AccountManagerFuture<Bundle> future) {
Fred Quintanad9d2f112009-04-23 13:36:27 -07001879 handler = handler == null ? mMainHandler : handler;
1880 handler.post(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001881 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07001882 public void run() {
1883 callback.run(future);
1884 }
1885 });
1886 }
Fred Quintana60307342009-03-24 22:48:12 -07001887
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001888 private void postToHandler(Handler handler, final OnAccountsUpdateListener listener,
Fred Quintanad9d2f112009-04-23 13:36:27 -07001889 final Account[] accounts) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001890 final Account[] accountsCopy = new Account[accounts.length];
1891 // send a copy to make sure that one doesn't
1892 // change what another sees
1893 System.arraycopy(accounts, 0, accountsCopy, 0, accountsCopy.length);
1894 handler = (handler == null) ? mMainHandler : handler;
Fred Quintanad9d2f112009-04-23 13:36:27 -07001895 handler.post(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001896 @Override
Fred Quintanad9d2f112009-04-23 13:36:27 -07001897 public void run() {
Costin Manolacheb6437242009-09-10 16:14:12 -07001898 try {
1899 listener.onAccountsUpdated(accountsCopy);
1900 } catch (SQLException e) {
1901 // Better luck next time. If the problem was disk-full,
1902 // the STORAGE_OK intent will re-trigger the update.
1903 Log.e(TAG, "Can't update accounts", e);
1904 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07001905 }
1906 });
1907 }
1908
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001909 private abstract class AmsTask extends FutureTask<Bundle> implements AccountManagerFuture<Bundle> {
Fred Quintanaa698f422009-04-08 19:14:54 -07001910 final IAccountManagerResponse mResponse;
1911 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001912 final AccountManagerCallback<Bundle> mCallback;
Fred Quintanaa698f422009-04-08 19:14:54 -07001913 final Activity mActivity;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001914 public AmsTask(Activity activity, Handler handler, AccountManagerCallback<Bundle> callback) {
Fred Quintanaa698f422009-04-08 19:14:54 -07001915 super(new Callable<Bundle>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001916 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07001917 public Bundle call() throws Exception {
1918 throw new IllegalStateException("this should never be called");
1919 }
1920 });
1921
1922 mHandler = handler;
1923 mCallback = callback;
1924 mActivity = activity;
1925 mResponse = new Response();
Fred Quintana33269202009-04-20 16:05:10 -07001926 }
1927
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001928 public final AccountManagerFuture<Bundle> start() {
1929 try {
1930 doWork();
1931 } catch (RemoteException e) {
1932 setException(e);
1933 }
Fred Quintana33269202009-04-20 16:05:10 -07001934 return this;
Fred Quintana60307342009-03-24 22:48:12 -07001935 }
Fred Quintanaa698f422009-04-08 19:14:54 -07001936
Sandra Kwan0b84b452016-01-20 15:25:42 -08001937 @Override
Fred Quintana96580e02010-03-04 13:42:42 -08001938 protected void set(Bundle bundle) {
1939 // TODO: somehow a null is being set as the result of the Future. Log this
1940 // case to help debug where this is occurring. When this bug is fixed this
1941 // condition statement should be removed.
1942 if (bundle == null) {
1943 Log.e(TAG, "the bundle must not be null", new Exception());
1944 }
1945 super.set(bundle);
1946 }
1947
Fred Quintanaa698f422009-04-08 19:14:54 -07001948 public abstract void doWork() throws RemoteException;
1949
1950 private Bundle internalGetResult(Long timeout, TimeUnit unit)
1951 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08001952 if (!isDone()) {
1953 ensureNotOnMainThread();
1954 }
Fred Quintanaa698f422009-04-08 19:14:54 -07001955 try {
1956 if (timeout == null) {
1957 return get();
1958 } else {
1959 return get(timeout, unit);
1960 }
1961 } catch (CancellationException e) {
1962 throw new OperationCanceledException();
1963 } catch (TimeoutException e) {
1964 // fall through and cancel
1965 } catch (InterruptedException e) {
1966 // fall through and cancel
1967 } catch (ExecutionException e) {
1968 final Throwable cause = e.getCause();
1969 if (cause instanceof IOException) {
1970 throw (IOException) cause;
1971 } else if (cause instanceof UnsupportedOperationException) {
1972 throw new AuthenticatorException(cause);
1973 } else if (cause instanceof AuthenticatorException) {
1974 throw (AuthenticatorException) cause;
1975 } else if (cause instanceof RuntimeException) {
1976 throw (RuntimeException) cause;
1977 } else if (cause instanceof Error) {
1978 throw (Error) cause;
1979 } else {
1980 throw new IllegalStateException(cause);
1981 }
1982 } finally {
1983 cancel(true /* interrupt if running */);
1984 }
1985 throw new OperationCanceledException();
1986 }
1987
Sandra Kwan0b84b452016-01-20 15:25:42 -08001988 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07001989 public Bundle getResult()
1990 throws OperationCanceledException, IOException, AuthenticatorException {
1991 return internalGetResult(null, null);
1992 }
1993
Sandra Kwan0b84b452016-01-20 15:25:42 -08001994 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07001995 public Bundle getResult(long timeout, TimeUnit unit)
1996 throws OperationCanceledException, IOException, AuthenticatorException {
1997 return internalGetResult(timeout, unit);
1998 }
1999
Sandra Kwan0b84b452016-01-20 15:25:42 -08002000 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002001 protected void done() {
2002 if (mCallback != null) {
2003 postToHandler(mHandler, mCallback, this);
2004 }
2005 }
2006
2007 /** Handles the responses from the AccountManager */
2008 private class Response extends IAccountManagerResponse.Stub {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002009 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002010 public void onResult(Bundle bundle) {
Brian Carlstrom46703b02011-04-06 15:41:29 -07002011 Intent intent = bundle.getParcelable(KEY_INTENT);
Fred Quintanaa698f422009-04-08 19:14:54 -07002012 if (intent != null && mActivity != null) {
2013 // since the user provided an Activity we will silently start intents
2014 // that we see
2015 mActivity.startActivity(intent);
2016 // leave the Future running to wait for the real response to this request
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07002017 } else if (bundle.getBoolean("retry")) {
2018 try {
2019 doWork();
2020 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002021 throw e.rethrowFromSystemServer();
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07002022 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002023 } else {
2024 set(bundle);
2025 }
2026 }
2027
Sandra Kwan0b84b452016-01-20 15:25:42 -08002028 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002029 public void onError(int code, String message) {
Alexandra Gherghina999d3942014-07-03 11:40:08 +01002030 if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED
2031 || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) {
2032 // the authenticator indicated that this request was canceled or we were
2033 // forbidden to fulfill; cancel now
Fred Quintanaa698f422009-04-08 19:14:54 -07002034 cancel(true /* mayInterruptIfRunning */);
2035 return;
2036 }
2037 setException(convertErrorToException(code, message));
2038 }
2039 }
2040
Fred Quintana60307342009-03-24 22:48:12 -07002041 }
2042
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002043 private abstract class BaseFutureTask<T> extends FutureTask<T> {
2044 final public IAccountManagerResponse mResponse;
Fred Quintanaa698f422009-04-08 19:14:54 -07002045 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002046
2047 public BaseFutureTask(Handler handler) {
2048 super(new Callable<T>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002049 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002050 public T call() throws Exception {
Fred Quintanaa698f422009-04-08 19:14:54 -07002051 throw new IllegalStateException("this should never be called");
2052 }
2053 });
Fred Quintanaa698f422009-04-08 19:14:54 -07002054 mHandler = handler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002055 mResponse = new Response();
Fred Quintana60307342009-03-24 22:48:12 -07002056 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002057
2058 public abstract void doWork() throws RemoteException;
2059
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002060 public abstract T bundleToResult(Bundle bundle) throws AuthenticatorException;
Fred Quintanaa698f422009-04-08 19:14:54 -07002061
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002062 protected void postRunnableToHandler(Runnable runnable) {
2063 Handler handler = (mHandler == null) ? mMainHandler : mHandler;
2064 handler.post(runnable);
Fred Quintanaa698f422009-04-08 19:14:54 -07002065 }
2066
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002067 protected void startTask() {
Fred Quintanaa698f422009-04-08 19:14:54 -07002068 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002069 doWork();
2070 } catch (RemoteException e) {
2071 setException(e);
Fred Quintanaa698f422009-04-08 19:14:54 -07002072 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002073 }
2074
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002075 protected class Response extends IAccountManagerResponse.Stub {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002076 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002077 public void onResult(Bundle bundle) {
2078 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002079 T result = bundleToResult(bundle);
2080 if (result == null) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002081 return;
2082 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002083 set(result);
2084 return;
Fred Quintanaa698f422009-04-08 19:14:54 -07002085 } catch (ClassCastException e) {
2086 // we will set the exception below
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002087 } catch (AuthenticatorException e) {
2088 // we will set the exception below
Fred Quintanaa698f422009-04-08 19:14:54 -07002089 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002090 onError(ERROR_CODE_INVALID_RESPONSE, "no result in response");
Fred Quintanaa698f422009-04-08 19:14:54 -07002091 }
2092
Sandra Kwan0b84b452016-01-20 15:25:42 -08002093 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002094 public void onError(int code, String message) {
Alexandra Gherghina999d3942014-07-03 11:40:08 +01002095 if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED
2096 || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) {
2097 // the authenticator indicated that this request was canceled or we were
2098 // forbidden to fulfill; cancel now
Fred Quintanaa698f422009-04-08 19:14:54 -07002099 cancel(true /* mayInterruptIfRunning */);
2100 return;
2101 }
2102 setException(convertErrorToException(code, message));
2103 }
2104 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002105 }
2106
2107 private abstract class Future2Task<T>
2108 extends BaseFutureTask<T> implements AccountManagerFuture<T> {
2109 final AccountManagerCallback<T> mCallback;
2110 public Future2Task(Handler handler, AccountManagerCallback<T> callback) {
2111 super(handler);
2112 mCallback = callback;
2113 }
2114
Sandra Kwan0b84b452016-01-20 15:25:42 -08002115 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002116 protected void done() {
2117 if (mCallback != null) {
2118 postRunnableToHandler(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002119 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002120 public void run() {
2121 mCallback.run(Future2Task.this);
2122 }
2123 });
2124 }
2125 }
2126
2127 public Future2Task<T> start() {
2128 startTask();
2129 return this;
2130 }
2131
2132 private T internalGetResult(Long timeout, TimeUnit unit)
2133 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08002134 if (!isDone()) {
2135 ensureNotOnMainThread();
2136 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002137 try {
2138 if (timeout == null) {
2139 return get();
2140 } else {
2141 return get(timeout, unit);
2142 }
2143 } catch (InterruptedException e) {
2144 // fall through and cancel
2145 } catch (TimeoutException e) {
2146 // fall through and cancel
2147 } catch (CancellationException e) {
2148 // fall through and cancel
2149 } catch (ExecutionException e) {
2150 final Throwable cause = e.getCause();
2151 if (cause instanceof IOException) {
2152 throw (IOException) cause;
2153 } else if (cause instanceof UnsupportedOperationException) {
2154 throw new AuthenticatorException(cause);
2155 } else if (cause instanceof AuthenticatorException) {
2156 throw (AuthenticatorException) cause;
2157 } else if (cause instanceof RuntimeException) {
2158 throw (RuntimeException) cause;
2159 } else if (cause instanceof Error) {
2160 throw (Error) cause;
2161 } else {
2162 throw new IllegalStateException(cause);
2163 }
2164 } finally {
2165 cancel(true /* interrupt if running */);
2166 }
2167 throw new OperationCanceledException();
2168 }
2169
Sandra Kwan0b84b452016-01-20 15:25:42 -08002170 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002171 public T getResult()
2172 throws OperationCanceledException, IOException, AuthenticatorException {
2173 return internalGetResult(null, null);
2174 }
2175
Sandra Kwan0b84b452016-01-20 15:25:42 -08002176 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002177 public T getResult(long timeout, TimeUnit unit)
2178 throws OperationCanceledException, IOException, AuthenticatorException {
2179 return internalGetResult(timeout, unit);
2180 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002181
Fred Quintana60307342009-03-24 22:48:12 -07002182 }
2183
Fred Quintanaa698f422009-04-08 19:14:54 -07002184 private Exception convertErrorToException(int code, String message) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002185 if (code == ERROR_CODE_NETWORK_ERROR) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002186 return new IOException(message);
Fred Quintana60307342009-03-24 22:48:12 -07002187 }
Fred Quintana60307342009-03-24 22:48:12 -07002188
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002189 if (code == ERROR_CODE_UNSUPPORTED_OPERATION) {
Fred Quintana33269202009-04-20 16:05:10 -07002190 return new UnsupportedOperationException(message);
Fred Quintana60307342009-03-24 22:48:12 -07002191 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002192
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002193 if (code == ERROR_CODE_INVALID_RESPONSE) {
Fred Quintana33269202009-04-20 16:05:10 -07002194 return new AuthenticatorException(message);
Fred Quintanaa698f422009-04-08 19:14:54 -07002195 }
2196
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002197 if (code == ERROR_CODE_BAD_ARGUMENTS) {
Fred Quintana33269202009-04-20 16:05:10 -07002198 return new IllegalArgumentException(message);
2199 }
2200
2201 return new AuthenticatorException(message);
2202 }
2203
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002204 private class GetAuthTokenByTypeAndFeaturesTask
2205 extends AmsTask implements AccountManagerCallback<Bundle> {
Fred Quintana33269202009-04-20 16:05:10 -07002206 GetAuthTokenByTypeAndFeaturesTask(final String accountType, final String authTokenType,
2207 final String[] features, Activity activityForPrompting,
2208 final Bundle addAccountOptions, final Bundle loginOptions,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002209 AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07002210 super(activityForPrompting, handler, callback);
2211 if (accountType == null) throw new IllegalArgumentException("account type is null");
2212 mAccountType = accountType;
2213 mAuthTokenType = authTokenType;
2214 mFeatures = features;
2215 mAddAccountOptions = addAccountOptions;
2216 mLoginOptions = loginOptions;
2217 mMyCallback = this;
2218 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002219 volatile AccountManagerFuture<Bundle> mFuture = null;
Fred Quintana33269202009-04-20 16:05:10 -07002220 final String mAccountType;
2221 final String mAuthTokenType;
2222 final String[] mFeatures;
2223 final Bundle mAddAccountOptions;
2224 final Bundle mLoginOptions;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002225 final AccountManagerCallback<Bundle> mMyCallback;
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002226 private volatile int mNumAccounts = 0;
Fred Quintana33269202009-04-20 16:05:10 -07002227
Sandra Kwan0b84b452016-01-20 15:25:42 -08002228 @Override
Fred Quintana33269202009-04-20 16:05:10 -07002229 public void doWork() throws RemoteException {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002230 getAccountsByTypeAndFeatures(mAccountType, mFeatures,
2231 new AccountManagerCallback<Account[]>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002232 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002233 public void run(AccountManagerFuture<Account[]> future) {
2234 Account[] accounts;
Fred Quintana33269202009-04-20 16:05:10 -07002235 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002236 accounts = future.getResult();
2237 } catch (OperationCanceledException e) {
2238 setException(e);
2239 return;
2240 } catch (IOException e) {
2241 setException(e);
2242 return;
2243 } catch (AuthenticatorException e) {
2244 setException(e);
2245 return;
Fred Quintana33269202009-04-20 16:05:10 -07002246 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002247
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002248 mNumAccounts = accounts.length;
2249
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002250 if (accounts.length == 0) {
2251 if (mActivity != null) {
2252 // no accounts, add one now. pretend that the user directly
2253 // made this request
2254 mFuture = addAccount(mAccountType, mAuthTokenType, mFeatures,
2255 mAddAccountOptions, mActivity, mMyCallback, mHandler);
2256 } else {
2257 // send result since we can't prompt to add an account
2258 Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002259 result.putString(KEY_ACCOUNT_NAME, null);
2260 result.putString(KEY_ACCOUNT_TYPE, null);
2261 result.putString(KEY_AUTHTOKEN, null);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002262 try {
2263 mResponse.onResult(result);
2264 } catch (RemoteException e) {
2265 // this will never happen
2266 }
2267 // we are done
2268 }
2269 } else if (accounts.length == 1) {
2270 // have a single account, return an authtoken for it
2271 if (mActivity == null) {
2272 mFuture = getAuthToken(accounts[0], mAuthTokenType,
2273 false /* notifyAuthFailure */, mMyCallback, mHandler);
2274 } else {
2275 mFuture = getAuthToken(accounts[0],
2276 mAuthTokenType, mLoginOptions,
Fred Quintana33269202009-04-20 16:05:10 -07002277 mActivity, mMyCallback, mHandler);
2278 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002279 } else {
2280 if (mActivity != null) {
2281 IAccountManagerResponse chooseResponse =
2282 new IAccountManagerResponse.Stub() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002283 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002284 public void onResult(Bundle value) throws RemoteException {
2285 Account account = new Account(
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002286 value.getString(KEY_ACCOUNT_NAME),
2287 value.getString(KEY_ACCOUNT_TYPE));
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002288 mFuture = getAuthToken(account, mAuthTokenType, mLoginOptions,
2289 mActivity, mMyCallback, mHandler);
2290 }
Fred Quintana33269202009-04-20 16:05:10 -07002291
Sandra Kwan0b84b452016-01-20 15:25:42 -08002292 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002293 public void onError(int errorCode, String errorMessage)
2294 throws RemoteException {
2295 mResponse.onError(errorCode, errorMessage);
2296 }
2297 };
2298 // have many accounts, launch the chooser
2299 Intent intent = new Intent();
Amith Yamasani12b8e132013-03-14 10:48:07 -07002300 ComponentName componentName = ComponentName.unflattenFromString(
2301 Resources.getSystem().getString(
2302 R.string.config_chooseAccountActivity));
2303 intent.setClassName(componentName.getPackageName(),
2304 componentName.getClassName());
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002305 intent.putExtra(KEY_ACCOUNTS, accounts);
2306 intent.putExtra(KEY_ACCOUNT_MANAGER_RESPONSE,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002307 new AccountManagerResponse(chooseResponse));
2308 mActivity.startActivity(intent);
2309 // the result will arrive via the IAccountManagerResponse
2310 } else {
2311 // send result since we can't prompt to select an account
2312 Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002313 result.putString(KEY_ACCOUNTS, null);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002314 try {
2315 mResponse.onResult(result);
2316 } catch (RemoteException e) {
2317 // this will never happen
2318 }
2319 // we are done
Fred Quintana33269202009-04-20 16:05:10 -07002320 }
Fred Quintana33269202009-04-20 16:05:10 -07002321 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002322 }}, mHandler);
Fred Quintana33269202009-04-20 16:05:10 -07002323 }
2324
Sandra Kwan0b84b452016-01-20 15:25:42 -08002325 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002326 public void run(AccountManagerFuture<Bundle> future) {
Fred Quintana33269202009-04-20 16:05:10 -07002327 try {
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002328 final Bundle result = future.getResult();
2329 if (mNumAccounts == 0) {
2330 final String accountName = result.getString(KEY_ACCOUNT_NAME);
2331 final String accountType = result.getString(KEY_ACCOUNT_TYPE);
2332 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
2333 setException(new AuthenticatorException("account not in result"));
2334 return;
2335 }
2336 final Account account = new Account(accountName, accountType);
2337 mNumAccounts = 1;
2338 getAuthToken(account, mAuthTokenType, null /* options */, mActivity,
2339 mMyCallback, mHandler);
2340 return;
2341 }
2342 set(result);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002343 } catch (OperationCanceledException e) {
2344 cancel(true /* mayInterruptIfRUnning */);
2345 } catch (IOException e) {
2346 setException(e);
2347 } catch (AuthenticatorException e) {
2348 setException(e);
Fred Quintana33269202009-04-20 16:05:10 -07002349 }
2350 }
2351 }
2352
Fred Quintana756b7352009-10-21 13:43:10 -07002353 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002354 * This convenience helper combines the functionality of
2355 * {@link #getAccountsByTypeAndFeatures}, {@link #getAuthToken}, and
2356 * {@link #addAccount}.
Fred Quintana756b7352009-10-21 13:43:10 -07002357 *
Dan Egnor661f0132010-02-19 11:23:00 -08002358 * <p>This method gets a list of the accounts matching the
2359 * specified type and feature set; if there is exactly one, it is
2360 * used; if there are more than one, the user is prompted to pick one;
2361 * if there are none, the user is prompted to add one. Finally,
2362 * an auth token is acquired for the chosen account.
2363 *
2364 * <p>This method may be called from any thread, but the returned
2365 * {@link AccountManagerFuture} must not be used on the main thread.
2366 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002367 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
2368 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
2369 * this function in API level 22.
2370 *
Dan Egnor661f0132010-02-19 11:23:00 -08002371 * @param accountType The account type required
Doug Zongkerff592dc2010-02-23 12:26:33 -08002372 * (see {@link #getAccountsByType}), must not be null
Dan Egnor661f0132010-02-19 11:23:00 -08002373 * @param authTokenType The desired auth token type
2374 * (see {@link #getAuthToken}), must not be null
2375 * @param features Required features for the account
2376 * (see {@link #getAccountsByTypeAndFeatures}), may be null or empty
2377 * @param activity The {@link Activity} context to use for launching new
2378 * sub-Activities to prompt to add an account, select an account,
2379 * and/or enter a password, as necessary; used only to call
2380 * startActivity(); should not be null
2381 * @param addAccountOptions Authenticator-specific options to use for
2382 * adding new accounts; may be null or empty
2383 * @param getAuthTokenOptions Authenticator-specific options to use for
2384 * getting auth tokens; may be null or empty
2385 * @param callback Callback to invoke when the request completes,
2386 * null for no callback
2387 * @param handler {@link Handler} identifying the callback thread,
2388 * null for the main thread
2389 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
2390 * at least the following fields:
Fred Quintana756b7352009-10-21 13:43:10 -07002391 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002392 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account
2393 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
2394 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
Fred Quintana756b7352009-10-21 13:43:10 -07002395 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002396 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07002397 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08002398 * <ul>
2399 * <li> {@link AuthenticatorException} if no authenticator was registered for
2400 * this account type or the authenticator failed to respond
2401 * <li> {@link OperationCanceledException} if the operation was canceled for
2402 * any reason, including the user canceling any operation
2403 * <li> {@link IOException} if the authenticator experienced an I/O problem
2404 * updating settings, usually because of network trouble
2405 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002406 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002407 public AccountManagerFuture<Bundle> getAuthTokenByFeatures(
Fred Quintana33269202009-04-20 16:05:10 -07002408 final String accountType, final String authTokenType, final String[] features,
Dan Egnor661f0132010-02-19 11:23:00 -08002409 final Activity activity, final Bundle addAccountOptions,
Fred Quintana31957f12009-10-21 13:43:10 -07002410 final Bundle getAuthTokenOptions,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002411 final AccountManagerCallback<Bundle> callback, final Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07002412 if (accountType == null) throw new IllegalArgumentException("account type is null");
2413 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002414 final GetAuthTokenByTypeAndFeaturesTask task =
2415 new GetAuthTokenByTypeAndFeaturesTask(accountType, authTokenType, features,
Dan Egnor661f0132010-02-19 11:23:00 -08002416 activity, addAccountOptions, getAuthTokenOptions, callback, handler);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002417 task.start();
2418 return task;
Fred Quintana60307342009-03-24 22:48:12 -07002419 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002420
Fred Quintana1121bb52011-09-14 23:19:35 -07002421 /**
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002422 * Deprecated in favor of {@link #newChooseAccountIntent(Account, List, String[], String,
2423 * String, String[], Bundle)}.
2424 *
Fred Quintana1121bb52011-09-14 23:19:35 -07002425 * Returns an intent to an {@link Activity} that prompts the user to choose from a list of
2426 * accounts.
2427 * The caller will then typically start the activity by calling
Mark Fickettab249e02012-09-05 09:45:47 -04002428 * <code>startActivityForResult(intent, ...);</code>.
Fred Quintana1121bb52011-09-14 23:19:35 -07002429 * <p>
2430 * On success the activity returns a Bundle with the account name and type specified using
2431 * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}.
2432 * <p>
2433 * The most common case is to call this with one account type, e.g.:
2434 * <p>
kmccormickf783ce52013-03-29 14:31:54 -07002435 * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, false, null,
Fred Quintanad88324d2011-09-19 11:43:05 -07002436 * null, null, null);</pre>
Fred Quintana1121bb52011-09-14 23:19:35 -07002437 * @param selectedAccount if specified, indicates that the {@link Account} is the currently
2438 * selected one, according to the caller's definition of selected.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002439 * @param allowableAccounts an optional {@link List} of accounts that are allowed to be
Fred Quintana1121bb52011-09-14 23:19:35 -07002440 * shown. If not specified then this field will not limit the displayed accounts.
2441 * @param allowableAccountTypes an optional string array of account types. These are used
2442 * 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 -07002443 * when adding an account. If not specified then this field will not limit the displayed
2444 * account types when adding an account.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002445 * @param alwaysPromptForAccount boolean that is ignored.
Fred Quintanad88324d2011-09-19 11:43:05 -07002446 * @param descriptionOverrideText if non-null this string is used as the description in the
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002447 * accounts chooser screen rather than the default
Fred Quintanad88324d2011-09-19 11:43:05 -07002448 * @param addAccountAuthTokenType this string is passed as the {@link #addAccount}
2449 * authTokenType parameter
2450 * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount}
2451 * requiredFeatures parameter
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002452 * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options
Fred Quintanad88324d2011-09-19 11:43:05 -07002453 * parameter
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002454 * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow.
Fred Quintana1121bb52011-09-14 23:19:35 -07002455 */
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002456 @Deprecated
2457 static public Intent newChooseAccountIntent(
2458 Account selectedAccount,
Baligh Uddinf2d248d2015-07-10 03:01:47 +00002459 ArrayList<Account> allowableAccounts,
Fred Quintana1121bb52011-09-14 23:19:35 -07002460 String[] allowableAccountTypes,
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002461 boolean alwaysPromptForAccount,
2462 String descriptionOverrideText,
2463 String addAccountAuthTokenType,
2464 String[] addAccountRequiredFeatures,
Fred Quintana1121bb52011-09-14 23:19:35 -07002465 Bundle addAccountOptions) {
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002466 return newChooseAccountIntent(
2467 selectedAccount,
2468 allowableAccounts,
2469 allowableAccountTypes,
2470 descriptionOverrideText,
2471 addAccountAuthTokenType,
2472 addAccountRequiredFeatures,
2473 addAccountOptions);
2474 }
2475
2476 /**
2477 * Returns an intent to an {@link Activity} that prompts the user to choose from a list of
2478 * accounts.
2479 * The caller will then typically start the activity by calling
2480 * <code>startActivityForResult(intent, ...);</code>.
2481 * <p>
2482 * On success the activity returns a Bundle with the account name and type specified using
2483 * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}.
2484 * <p>
2485 * The most common case is to call this with one account type, e.g.:
2486 * <p>
2487 * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, null, null, null,
2488 * null);</pre>
2489 * @param selectedAccount if specified, indicates that the {@link Account} is the currently
2490 * selected one, according to the caller's definition of selected.
2491 * @param allowableAccounts an optional {@link List} of accounts that are allowed to be
2492 * shown. If not specified then this field will not limit the displayed accounts.
2493 * @param allowableAccountTypes an optional string array of account types. These are used
2494 * both to filter the shown accounts and to filter the list of account types that are shown
2495 * when adding an account. If not specified then this field will not limit the displayed
2496 * account types when adding an account.
2497 * @param descriptionOverrideText if non-null this string is used as the description in the
2498 * accounts chooser screen rather than the default
2499 * @param addAccountAuthTokenType this string is passed as the {@link #addAccount}
2500 * authTokenType parameter
2501 * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount}
2502 * requiredFeatures parameter
2503 * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options
2504 * parameter
2505 * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow.
2506 */
2507 static public Intent newChooseAccountIntent(
2508 Account selectedAccount,
2509 List<Account> allowableAccounts,
2510 String[] allowableAccountTypes,
2511 String descriptionOverrideText,
2512 String addAccountAuthTokenType,
2513 String[] addAccountRequiredFeatures,
2514 Bundle addAccountOptions) {
Fred Quintana1121bb52011-09-14 23:19:35 -07002515 Intent intent = new Intent();
Amith Yamasani12b8e132013-03-14 10:48:07 -07002516 ComponentName componentName = ComponentName.unflattenFromString(
2517 Resources.getSystem().getString(R.string.config_chooseTypeAndAccountActivity));
2518 intent.setClassName(componentName.getPackageName(),
2519 componentName.getClassName());
Fred Quintana1121bb52011-09-14 23:19:35 -07002520 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST,
Craig Lafayette3c9c71d2015-07-14 10:48:46 -04002521 allowableAccounts == null ? null : new ArrayList<Account>(allowableAccounts));
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002522 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
2523 allowableAccountTypes);
Fred Quintana1121bb52011-09-14 23:19:35 -07002524 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,
2525 addAccountOptions);
2526 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_SELECTED_ACCOUNT, selectedAccount);
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002527 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_DESCRIPTION_TEXT_OVERRIDE,
2528 descriptionOverrideText);
2529 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING,
2530 addAccountAuthTokenType);
2531 intent.putExtra(
2532 ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY,
2533 addAccountRequiredFeatures);
Fred Quintana1121bb52011-09-14 23:19:35 -07002534 return intent;
2535 }
2536
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002537 private final HashMap<OnAccountsUpdateListener, Handler> mAccountsUpdatedListeners =
Fred Quintanad9d2f112009-04-23 13:36:27 -07002538 Maps.newHashMap();
2539
Fred Quintanad9d2f112009-04-23 13:36:27 -07002540 /**
2541 * BroadcastReceiver that listens for the LOGIN_ACCOUNTS_CHANGED_ACTION intent
2542 * so that it can read the updated list of accounts and send them to the listener
2543 * in mAccountsUpdatedListeners.
2544 */
2545 private final BroadcastReceiver mAccountsChangedBroadcastReceiver = new BroadcastReceiver() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002546 @Override
Fred Quintanad9d2f112009-04-23 13:36:27 -07002547 public void onReceive(final Context context, final Intent intent) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002548 final Account[] accounts = getAccounts();
2549 // send the result to the listeners
2550 synchronized (mAccountsUpdatedListeners) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002551 for (Map.Entry<OnAccountsUpdateListener, Handler> entry :
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002552 mAccountsUpdatedListeners.entrySet()) {
2553 postToHandler(entry.getValue(), entry.getKey(), accounts);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002554 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002555 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002556 }
2557 };
2558
2559 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002560 * Adds an {@link OnAccountsUpdateListener} to this instance of the
2561 * {@link AccountManager}. This listener will be notified whenever the
2562 * list of accounts on the device changes.
2563 *
2564 * <p>As long as this listener is present, the AccountManager instance
2565 * will not be garbage-collected, and neither will the {@link Context}
2566 * used to retrieve it, which may be a large Activity instance. To avoid
2567 * memory leaks, you must remove this listener before then. Normally
2568 * listeners are added in an Activity or Service's {@link Activity#onCreate}
2569 * and removed in {@link Activity#onDestroy}.
2570 *
Ian Pedowitz6cc066d2015-08-05 14:23:43 +00002571 * <p>The listener will only be informed of accounts that would be returned
2572 * to the caller via {@link #getAccounts()}. Typically this means that to
2573 * get any accounts, the caller will need to be grated the GET_ACCOUNTS
2574 * permission.
Dan Egnor661f0132010-02-19 11:23:00 -08002575 *
Ian Pedowitz6cc066d2015-08-05 14:23:43 +00002576 * <p>It is safe to call this method from the main thread.
Ian Pedowitz845d14d2015-08-04 07:47:37 -07002577 *
Dan Egnor661f0132010-02-19 11:23:00 -08002578 * @param listener The listener to send notifications to
2579 * @param handler {@link Handler} identifying the thread to use
2580 * for notifications, null for the main thread
2581 * @param updateImmediately If true, the listener will be invoked
2582 * (on the handler thread) right away with the current account list
Fred Quintanad9d2f112009-04-23 13:36:27 -07002583 * @throws IllegalArgumentException if listener is null
2584 * @throws IllegalStateException if listener was already added
2585 */
Tor Norbye80b530a2015-04-23 16:36:09 -07002586 @RequiresPermission(GET_ACCOUNTS)
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002587 public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener,
Fred Quintanad9d2f112009-04-23 13:36:27 -07002588 Handler handler, boolean updateImmediately) {
2589 if (listener == null) {
2590 throw new IllegalArgumentException("the listener is null");
2591 }
2592 synchronized (mAccountsUpdatedListeners) {
2593 if (mAccountsUpdatedListeners.containsKey(listener)) {
2594 throw new IllegalStateException("this listener is already added");
2595 }
2596 final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty();
2597
2598 mAccountsUpdatedListeners.put(listener, handler);
2599
2600 if (wasEmpty) {
2601 // Register a broadcast receiver to monitor account changes
2602 IntentFilter intentFilter = new IntentFilter();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002603 intentFilter.addAction(LOGIN_ACCOUNTS_CHANGED_ACTION);
Costin Manolacheb6437242009-09-10 16:14:12 -07002604 // To recover from disk-full.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002605 intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002606 mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter);
2607 }
2608 }
2609
2610 if (updateImmediately) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002611 postToHandler(handler, listener, getAccounts());
Fred Quintanad9d2f112009-04-23 13:36:27 -07002612 }
2613 }
2614
2615 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002616 * Removes an {@link OnAccountsUpdateListener} previously registered with
2617 * {@link #addOnAccountsUpdatedListener}. The listener will no longer
2618 * receive notifications of account changes.
2619 *
2620 * <p>It is safe to call this method from the main thread.
2621 *
2622 * <p>No permission is required to call this method.
2623 *
2624 * @param listener The previously added listener to remove
Fred Quintanad9d2f112009-04-23 13:36:27 -07002625 * @throws IllegalArgumentException if listener is null
2626 * @throws IllegalStateException if listener was not already added
2627 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002628 public void removeOnAccountsUpdatedListener(OnAccountsUpdateListener listener) {
Fred Quintana382601f2010-03-25 12:25:10 -07002629 if (listener == null) throw new IllegalArgumentException("listener is null");
Fred Quintanad9d2f112009-04-23 13:36:27 -07002630 synchronized (mAccountsUpdatedListeners) {
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01002631 if (!mAccountsUpdatedListeners.containsKey(listener)) {
Costin Manolache88a211b2009-10-29 11:30:11 -07002632 Log.e(TAG, "Listener was not previously added");
2633 return;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002634 }
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01002635 mAccountsUpdatedListeners.remove(listener);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002636 if (mAccountsUpdatedListeners.isEmpty()) {
2637 mContext.unregisterReceiver(mAccountsChangedBroadcastReceiver);
2638 }
2639 }
2640 }
Sandra Kwan78812282015-11-04 11:19:47 -08002641
2642 /**
2643 * Asks the user to authenticate with an account of a specified type. The
2644 * authenticator for this account type processes this request with the
2645 * appropriate user interface. If the user does elect to authenticate with a
2646 * new account, a bundle of session data for installing the account later is
2647 * returned with optional account password and account status token.
2648 * <p>
2649 * This method may be called from any thread, but the returned
2650 * {@link AccountManagerFuture} must not be used on the main thread.
2651 * <p>
2652 * <p>
2653 * <b>NOTE:</b> The account will not be installed to the device by calling
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002654 * this api alone. #finishSession should be called after this to install the
2655 * account on device.
Sandra Kwan78812282015-11-04 11:19:47 -08002656 *
2657 * @param accountType The type of account to add; must not be null
2658 * @param authTokenType The type of auth token (see {@link #getAuthToken})
2659 * this account will need to be able to generate, null for none
2660 * @param requiredFeatures The features (see {@link #hasFeatures}) this
2661 * account must have, null for none
2662 * @param options Authenticator-specific options for the request, may be
2663 * null or empty
2664 * @param activity The {@link Activity} context to use for launching a new
2665 * authenticator-defined sub-Activity to prompt the user to
2666 * create an account; used only to call startActivity(); if null,
2667 * the prompt will not be launched directly, but the necessary
2668 * {@link Intent} will be returned to the caller instead
2669 * @param callback Callback to invoke when the request completes, null for
2670 * no callback
2671 * @param handler {@link Handler} identifying the callback thread, null for
2672 * the main thread
2673 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
2674 * these fields if activity was specified and user was authenticated
2675 * with an account:
2676 * <ul>
2677 * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for
2678 * adding the the to the device later.
2679 * <li>{@link #KEY_PASSWORD} - optional, the password or password
2680 * hash of the account.
2681 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
2682 * status of the account
2683 * </ul>
2684 * If no activity was specified, the returned Bundle contains only
2685 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
2686 * actual account creation process. If authenticator doesn't support
2687 * this method, the returned Bundle contains only
2688 * {@link #KEY_ACCOUNT_SESSION_BUNDLE} with encrypted
2689 * {@code options} needed to add account later. If an error
2690 * occurred, {@link AccountManagerFuture#getResult()} throws:
2691 * <ul>
2692 * <li>{@link AuthenticatorException} if no authenticator was
2693 * registered for this account type or the authenticator failed to
2694 * respond
2695 * <li>{@link OperationCanceledException} if the operation was
2696 * canceled for any reason, including the user canceling the
2697 * creation process or adding accounts (of this type) has been
2698 * disabled by policy
2699 * <li>{@link IOException} if the authenticator experienced an I/O
2700 * problem creating a new account, usually because of network
2701 * trouble
2702 * </ul>
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002703 * @see #finishSession
Sandra Kwana578d112015-12-16 16:01:43 -08002704 * @hide
Sandra Kwan78812282015-11-04 11:19:47 -08002705 */
Sandra Kwana578d112015-12-16 16:01:43 -08002706 @SystemApi
Sandra Kwane68c37e2015-11-12 17:11:49 -08002707 public AccountManagerFuture<Bundle> startAddAccountSession(
2708 final String accountType,
2709 final String authTokenType,
2710 final String[] requiredFeatures,
2711 final Bundle options,
2712 final Activity activity,
2713 AccountManagerCallback<Bundle> callback,
2714 Handler handler) {
Sandra Kwan78812282015-11-04 11:19:47 -08002715 if (accountType == null) throw new IllegalArgumentException("accountType is null");
2716 final Bundle optionsIn = new Bundle();
2717 if (options != null) {
2718 optionsIn.putAll(options);
2719 }
2720 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
2721
2722 return new AmsTask(activity, handler, callback) {
2723 @Override
2724 public void doWork() throws RemoteException {
Sandra Kwane68c37e2015-11-12 17:11:49 -08002725 mService.startAddAccountSession(
2726 mResponse,
2727 accountType,
2728 authTokenType,
2729 requiredFeatures,
2730 activity != null,
2731 optionsIn);
2732 }
2733 }.start();
2734 }
2735
2736 /**
2737 * Asks the user to enter a new password for an account but not updating the
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002738 * saved credentials for the account until {@link #finishSession} is called.
Sandra Kwane68c37e2015-11-12 17:11:49 -08002739 * <p>
2740 * This method may be called from any thread, but the returned
2741 * {@link AccountManagerFuture} must not be used on the main thread.
2742 * <p>
2743 * <b>NOTE:</b> The saved credentials for the account alone will not be
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002744 * updated by calling this API alone. #finishSession should be called after
2745 * this to update local credentials
Sandra Kwane68c37e2015-11-12 17:11:49 -08002746 *
2747 * @param account The account to update credentials for
2748 * @param authTokenType The credentials entered must allow an auth token of
2749 * this type to be created (but no actual auth token is
2750 * returned); may be null
2751 * @param options Authenticator-specific options for the request; may be
2752 * null or empty
2753 * @param activity The {@link Activity} context to use for launching a new
2754 * authenticator-defined sub-Activity to prompt the user to enter
2755 * a password; used only to call startActivity(); if null, the
2756 * prompt will not be launched directly, but the necessary
2757 * {@link Intent} will be returned to the caller instead
2758 * @param callback Callback to invoke when the request completes, null for
2759 * no callback
2760 * @param handler {@link Handler} identifying the callback thread, null for
2761 * the main thread
2762 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
2763 * these fields if an activity was supplied and user was
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002764 * successfully re-authenticated to the account:
Sandra Kwane68c37e2015-11-12 17:11:49 -08002765 * <ul>
2766 * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for
2767 * updating the local credentials on device later.
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002768 * <li>{@link #KEY_PASSWORD} - optional, the password or password
2769 * hash of the account
2770 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
2771 * status of the account
Sandra Kwane68c37e2015-11-12 17:11:49 -08002772 * </ul>
2773 * If no activity was specified, the returned Bundle contains
2774 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
2775 * password prompt. If an error occurred,
2776 * {@link AccountManagerFuture#getResult()} throws:
2777 * <ul>
2778 * <li>{@link AuthenticatorException} if the authenticator failed to
2779 * respond
2780 * <li>{@link OperationCanceledException} if the operation was
2781 * canceled for any reason, including the user canceling the
2782 * password prompt
2783 * <li>{@link IOException} if the authenticator experienced an I/O
2784 * problem verifying the password, usually because of network
2785 * trouble
2786 * </ul>
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002787 * @see #finishSession
Sandra Kwana578d112015-12-16 16:01:43 -08002788 * @hide
Sandra Kwane68c37e2015-11-12 17:11:49 -08002789 */
Sandra Kwana578d112015-12-16 16:01:43 -08002790 @SystemApi
Sandra Kwane68c37e2015-11-12 17:11:49 -08002791 public AccountManagerFuture<Bundle> startUpdateCredentialsSession(
2792 final Account account,
2793 final String authTokenType,
2794 final Bundle options,
2795 final Activity activity,
2796 final AccountManagerCallback<Bundle> callback,
2797 final Handler handler) {
2798 if (account == null) {
2799 throw new IllegalArgumentException("account is null");
2800 }
Carlos Valdivia51b651a2016-03-30 13:44:28 -07002801
2802 // Always include the calling package name. This just makes life easier
2803 // down stream.
2804 final Bundle optionsIn = new Bundle();
2805 if (options != null) {
2806 optionsIn.putAll(options);
2807 }
2808 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
2809
Sandra Kwane68c37e2015-11-12 17:11:49 -08002810 return new AmsTask(activity, handler, callback) {
2811 @Override
2812 public void doWork() throws RemoteException {
2813 mService.startUpdateCredentialsSession(
2814 mResponse,
2815 account,
2816 authTokenType,
2817 activity != null,
Carlos Valdivia51b651a2016-03-30 13:44:28 -07002818 optionsIn);
Sandra Kwan78812282015-11-04 11:19:47 -08002819 }
2820 }.start();
2821 }
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002822
2823 /**
2824 * Finishes the session started by {@link #startAddAccountSession} or
2825 * {@link #startUpdateCredentialsSession}. This will either add the account
2826 * to AccountManager or update the local credentials stored.
2827 * <p>
2828 * This method may be called from any thread, but the returned
2829 * {@link AccountManagerFuture} must not be used on the main thread.
2830 *
2831 * @param sessionBundle a {@link Bundle} created by {@link #startAddAccountSession} or
2832 * {@link #startUpdateCredentialsSession}
2833 * @param activity The {@link Activity} context to use for launching a new
2834 * authenticator-defined sub-Activity to prompt the user to
2835 * create an account or reauthenticate existing account; used
2836 * only to call startActivity(); if null, the prompt will not
2837 * be launched directly, but the necessary {@link Intent} will
2838 * be returned to the caller instead
2839 * @param callback Callback to invoke when the request completes, null for
2840 * no callback
2841 * @param handler {@link Handler} identifying the callback thread, null for
2842 * the main thread
2843 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
2844 * these fields if an activity was supplied and an account was added
2845 * to device or local credentials were updated::
2846 * <ul>
2847 * <li>{@link #KEY_ACCOUNT_NAME} - the name of the account created
2848 * <li>{@link #KEY_ACCOUNT_TYPE} - the type of the account
2849 * </ul>
2850 * If no activity was specified and additional information is needed
2851 * from user, the returned Bundle may contains only
2852 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
2853 * actual account creation process. If an error occurred,
2854 * {@link AccountManagerFuture#getResult()} throws:
2855 * <ul>
2856 * <li>{@link AuthenticatorException} if no authenticator was
2857 * registered for this account type or the authenticator failed to
2858 * respond
2859 * <li>{@link OperationCanceledException} if the operation was
2860 * canceled for any reason, including the user canceling the
2861 * creation process or adding accounts (of this type) has been
2862 * disabled by policy
2863 * <li>{@link IOException} if the authenticator experienced an I/O
2864 * problem creating a new account, usually because of network
2865 * trouble
2866 * </ul>
2867 * @see #startAddAccountSession and #startUpdateCredentialsSession
Sandra Kwana578d112015-12-16 16:01:43 -08002868 * @hide
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002869 */
Sandra Kwana578d112015-12-16 16:01:43 -08002870 @SystemApi
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002871 public AccountManagerFuture<Bundle> finishSession(
2872 final Bundle sessionBundle,
2873 final Activity activity,
2874 AccountManagerCallback<Bundle> callback,
2875 Handler handler) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002876 return finishSessionAsUser(
2877 sessionBundle,
2878 activity,
2879 Process.myUserHandle(),
2880 callback,
2881 handler);
2882 }
2883
2884 /**
2885 * @see #finishSession
2886 * @hide
2887 */
2888 @SystemApi
2889 public AccountManagerFuture<Bundle> finishSessionAsUser(
2890 final Bundle sessionBundle,
2891 final Activity activity,
2892 final UserHandle userHandle,
2893 AccountManagerCallback<Bundle> callback,
2894 Handler handler) {
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002895 if (sessionBundle == null) {
2896 throw new IllegalArgumentException("sessionBundle is null");
2897 }
2898
2899 /* Add information required by add account flow */
2900 final Bundle appInfo = new Bundle();
2901 appInfo.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
2902
2903 return new AmsTask(activity, handler, callback) {
2904 @Override
2905 public void doWork() throws RemoteException {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002906 mService.finishSessionAsUser(
2907 mResponse,
2908 sessionBundle,
2909 activity != null,
2910 appInfo,
2911 userHandle.getIdentifier());
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002912 }
2913 }.start();
2914 }
Sandra Kwan390c9d22016-01-12 14:13:37 -08002915
2916 /**
2917 * Checks whether {@link #updateCredentials} or {@link #startUpdateCredentialsSession} should be
2918 * called with respect to the specified account.
2919 * <p>
2920 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
2921 * not be used on the main thread.
2922 *
2923 * @param account The {@link Account} to be checked whether {@link #updateCredentials} or
2924 * {@link #startUpdateCredentialsSession} should be called
2925 * @param statusToken a String of token to check account staus
2926 * @param callback Callback to invoke when the request completes, null for no callback
2927 * @param handler {@link Handler} identifying the callback thread, null for the main thread
2928 * @return An {@link AccountManagerFuture} which resolves to a Boolean, true if the credentials
2929 * of the account should be updated.
2930 * @hide
2931 */
2932 @SystemApi
2933 public AccountManagerFuture<Boolean> isCredentialsUpdateSuggested(
2934 final Account account,
2935 final String statusToken,
2936 AccountManagerCallback<Boolean> callback,
2937 Handler handler) {
2938 if (account == null) {
2939 throw new IllegalArgumentException("account is null");
2940 }
2941
2942 if (TextUtils.isEmpty(statusToken)) {
2943 throw new IllegalArgumentException("status token is empty");
2944 }
2945
2946 return new Future2Task<Boolean>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002947 @Override
Sandra Kwan390c9d22016-01-12 14:13:37 -08002948 public void doWork() throws RemoteException {
2949 mService.isCredentialsUpdateSuggested(
2950 mResponse,
2951 account,
2952 statusToken);
2953 }
Sandra Kwan0b84b452016-01-20 15:25:42 -08002954 @Override
Sandra Kwan390c9d22016-01-12 14:13:37 -08002955 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
2956 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
2957 throw new AuthenticatorException("no result in response");
2958 }
2959 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
2960 }
2961 }.start();
2962 }
Fred Quintana60307342009-03-24 22:48:12 -07002963}