blob: 8185818ccd2946c324d21a17b6e9901cdb30962b [file] [log] [blame]
Fred Quintana60307342009-03-24 22:48:12 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.accounts;
18
Sandra Kwan390c9d22016-01-12 14:13:37 -080019import static android.Manifest.permission.GET_ACCOUNTS;
20
Dmitry Dementyev52745472016-12-02 10:27:45 -080021import android.annotation.IntDef;
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -070022import android.annotation.NonNull;
Tor Norbye80b530a2015-04-23 16:36:09 -070023import android.annotation.RequiresPermission;
24import android.annotation.Size;
Sandra Kwana578d112015-12-16 16:01:43 -080025import android.annotation.SystemApi;
Fred Quintana60307342009-03-24 22:48:12 -070026import android.app.Activity;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080027import android.content.BroadcastReceiver;
Amith Yamasani12b8e132013-03-14 10:48:07 -070028import android.content.ComponentName;
Fred Quintana60307342009-03-24 22:48:12 -070029import android.content.Context;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080030import android.content.Intent;
Fred Quintanad9d2f112009-04-23 13:36:27 -070031import android.content.IntentFilter;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070032import android.content.IntentSender;
Amith Yamasani12b8e132013-03-14 10:48:07 -070033import android.content.res.Resources;
Dmitry Dementyev52745472016-12-02 10:27:45 -080034import android.content.pm.ApplicationInfo;
35import android.content.pm.PackageManager;
Costin Manolacheb6437242009-09-10 16:14:12 -070036import android.database.SQLException;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080037import android.os.Build;
Fred Quintanaa698f422009-04-08 19:14:54 -070038import android.os.Bundle;
39import android.os.Handler;
40import android.os.Looper;
Fred Quintana33269202009-04-20 16:05:10 -070041import android.os.Parcelable;
Amith Yamasani2c7bc262012-11-05 16:46:02 -080042import android.os.Process;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080043import android.os.RemoteException;
Amith Yamasani2c7bc262012-11-05 16:46:02 -080044import android.os.UserHandle;
Fred Quintanaf0fd8432010-03-08 12:48:05 -080045import android.text.TextUtils;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080046import android.util.Log;
47
48import com.android.internal.R;
49import com.google.android.collect.Maps;
Fred Quintana60307342009-03-24 22:48:12 -070050
Fred Quintanaa698f422009-04-08 19:14:54 -070051import java.io.IOException;
Dmitry Dementyev52745472016-12-02 10:27:45 -080052import java.lang.annotation.Retention;
53import java.lang.annotation.RetentionPolicy;
54import java.lang.SuppressWarnings;
Fred Quintana1121bb52011-09-14 23:19:35 -070055import java.util.ArrayList;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080056import java.util.HashMap;
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -070057import java.util.List;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080058import java.util.Map;
Fred Quintanaa698f422009-04-08 19:14:54 -070059import java.util.concurrent.Callable;
60import java.util.concurrent.CancellationException;
61import java.util.concurrent.ExecutionException;
62import java.util.concurrent.FutureTask;
Fred Quintanaa698f422009-04-08 19:14:54 -070063import java.util.concurrent.TimeUnit;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080064import java.util.concurrent.TimeoutException;
Fred Quintana60307342009-03-24 22:48:12 -070065
66/**
Dan Egnor661f0132010-02-19 11:23:00 -080067 * This class provides access to a centralized registry of the user's
Dan Egnor8e4378b2010-08-02 18:22:09 -070068 * online accounts. The user enters credentials (username and password) once
69 * per account, granting applications access to online resources with
70 * "one-click" approval.
Fred Quintana60307342009-03-24 22:48:12 -070071 *
Dan Egnor661f0132010-02-19 11:23:00 -080072 * <p>Different online services have different ways of handling accounts and
73 * authentication, so the account manager uses pluggable <em>authenticator</em>
Dan Egnor8e4378b2010-08-02 18:22:09 -070074 * modules for different <em>account types</em>. Authenticators (which may be
75 * written by third parties) handle the actual details of validating account
76 * credentials and storing account information. For example, Google, Facebook,
77 * and Microsoft Exchange each have their own authenticator.
Dan Egnor661f0132010-02-19 11:23:00 -080078 *
79 * <p>Many servers support some notion of an <em>authentication token</em>,
80 * which can be used to authenticate a request to the server without sending
81 * the user's actual password. (Auth tokens are normally created with a
82 * separate request which does include the user's credentials.) AccountManager
Dan Egnor8e4378b2010-08-02 18:22:09 -070083 * can generate auth tokens for applications, so the application doesn't need to
84 * handle passwords directly. Auth tokens are normally reusable and cached by
85 * AccountManager, but must be refreshed periodically. It's the responsibility
86 * of applications to <em>invalidate</em> auth tokens when they stop working so
87 * the AccountManager knows it needs to regenerate them.
Dan Egnor661f0132010-02-19 11:23:00 -080088 *
89 * <p>Applications accessing a server normally go through these steps:
90 *
91 * <ul>
92 * <li>Get an instance of AccountManager using {@link #get(Context)}.
93 *
94 * <li>List the available accounts using {@link #getAccountsByType} or
95 * {@link #getAccountsByTypeAndFeatures}. Normally applications will only
96 * be interested in accounts with one particular <em>type</em>, which
97 * identifies the authenticator. Account <em>features</em> are used to
98 * identify particular account subtypes and capabilities. Both the account
99 * type and features are authenticator-specific strings, and must be known by
100 * the application in coordination with its preferred authenticators.
101 *
102 * <li>Select one or more of the available accounts, possibly by asking the
103 * user for their preference. If no suitable accounts are available,
104 * {@link #addAccount} may be called to prompt the user to create an
105 * account of the appropriate type.
106 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700107 * <li><b>Important:</b> If the application is using a previously remembered
108 * account selection, it must make sure the account is still in the list
109 * of accounts returned by {@link #getAccountsByType}. Requesting an auth token
110 * for an account no longer on the device results in an undefined failure.
111 *
Dan Egnor661f0132010-02-19 11:23:00 -0800112 * <li>Request an auth token for the selected account(s) using one of the
113 * {@link #getAuthToken} methods or related helpers. Refer to the description
114 * of each method for exact usage and error handling details.
115 *
116 * <li>Make the request using the auth token. The form of the auth token,
117 * the format of the request, and the protocol used are all specific to the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700118 * service you are accessing. The application may use whatever network and
119 * protocol libraries are useful.
Dan Egnor661f0132010-02-19 11:23:00 -0800120 *
121 * <li><b>Important:</b> If the request fails with an authentication error,
122 * it could be that a cached auth token is stale and no longer honored by
123 * the server. The application must call {@link #invalidateAuthToken} to remove
124 * the token from the cache, otherwise requests will continue failing! After
125 * invalidating the auth token, immediately go back to the "Request an auth
126 * token" step above. If the process fails the second time, then it can be
127 * treated as a "genuine" authentication failure and the user notified or other
128 * appropriate actions taken.
129 * </ul>
130 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700131 * <p>Some AccountManager methods may need to interact with the user to
Dan Egnor661f0132010-02-19 11:23:00 -0800132 * prompt for credentials, present options, or ask the user to add an account.
133 * The caller may choose whether to allow AccountManager to directly launch the
134 * necessary user interface and wait for the user, or to return an Intent which
135 * the caller may use to launch the interface, or (in some cases) to install a
136 * notification which the user can select at any time to launch the interface.
137 * To have AccountManager launch the interface directly, the caller must supply
138 * the current foreground {@link Activity} context.
139 *
140 * <p>Many AccountManager methods take {@link AccountManagerCallback} and
Dan Egnor8e4378b2010-08-02 18:22:09 -0700141 * {@link Handler} as parameters. These methods return immediately and
Dan Egnor661f0132010-02-19 11:23:00 -0800142 * run asynchronously. If a callback is provided then
143 * {@link AccountManagerCallback#run} will be invoked on the Handler's
144 * thread when the request completes, successfully or not.
Dan Egnor8e4378b2010-08-02 18:22:09 -0700145 * The result is retrieved by calling {@link AccountManagerFuture#getResult()}
146 * on the {@link AccountManagerFuture} returned by the method (and also passed
147 * to the callback). This method waits for the operation to complete (if
148 * necessary) and either returns the result or throws an exception if an error
149 * occurred during the operation. To make the request synchronously, call
Dan Egnor661f0132010-02-19 11:23:00 -0800150 * {@link AccountManagerFuture#getResult()} immediately on receiving the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700151 * future from the method; no callback need be supplied.
Dan Egnor661f0132010-02-19 11:23:00 -0800152 *
153 * <p>Requests which may block, including
154 * {@link AccountManagerFuture#getResult()}, must never be called on
155 * the application's main event thread. These operations throw
156 * {@link IllegalStateException} if they are used on the main thread.
Fred Quintana60307342009-03-24 22:48:12 -0700157 */
158public class AccountManager {
Dmitry Dementyev52745472016-12-02 10:27:45 -0800159
Fred Quintana60307342009-03-24 22:48:12 -0700160 private static final String TAG = "AccountManager";
161
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700162 public static final int ERROR_CODE_REMOTE_EXCEPTION = 1;
163 public static final int ERROR_CODE_NETWORK_ERROR = 3;
164 public static final int ERROR_CODE_CANCELED = 4;
165 public static final int ERROR_CODE_INVALID_RESPONSE = 5;
166 public static final int ERROR_CODE_UNSUPPORTED_OPERATION = 6;
167 public static final int ERROR_CODE_BAD_ARGUMENTS = 7;
168 public static final int ERROR_CODE_BAD_REQUEST = 8;
Jatin Lodhia3df7d692013-03-27 10:57:23 -0700169 public static final int ERROR_CODE_BAD_AUTHENTICATION = 9;
Fred Quintana756b7352009-10-21 13:43:10 -0700170
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800171 /** @hide */
172 public static final int ERROR_CODE_USER_RESTRICTED = 100;
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100173 /** @hide */
174 public static final int ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE = 101;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800175
Dan Egnor661f0132010-02-19 11:23:00 -0800176 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700177 * Bundle key used for the {@link String} account name in results
Dan Egnor661f0132010-02-19 11:23:00 -0800178 * from methods which return information about a particular account.
179 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700180 public static final String KEY_ACCOUNT_NAME = "authAccount";
Dan Egnor661f0132010-02-19 11:23:00 -0800181
182 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700183 * Bundle key used for the {@link String} account type in results
Dan Egnor661f0132010-02-19 11:23:00 -0800184 * from methods which return information about a particular account.
185 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700186 public static final String KEY_ACCOUNT_TYPE = "accountType";
Dan Egnor661f0132010-02-19 11:23:00 -0800187
188 /**
Svet Ganovd4696122016-09-24 13:57:45 -0700189 * Bundle key used for the account access id used for noting the
190 * account was accessed when unmarshaled from a parcel.
Svet Ganovf6d424f12016-09-20 20:18:53 -0700191 *
192 * @hide
193 */
Svet Ganovd4696122016-09-24 13:57:45 -0700194 public static final String KEY_ACCOUNT_ACCESS_ID = "accountAccessId";
Svet Ganovf6d424f12016-09-20 20:18:53 -0700195
196 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700197 * Bundle key used for the auth token value in results
Dan Egnor661f0132010-02-19 11:23:00 -0800198 * from {@link #getAuthToken} and friends.
199 */
200 public static final String KEY_AUTHTOKEN = "authtoken";
201
202 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700203 * Bundle key used for an {@link Intent} in results from methods that
Dan Egnor661f0132010-02-19 11:23:00 -0800204 * may require the caller to interact with the user. The Intent can
205 * be used to start the corresponding user interface activity.
206 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700207 public static final String KEY_INTENT = "intent";
Dan Egnor661f0132010-02-19 11:23:00 -0800208
209 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700210 * Bundle key used to supply the password directly in options to
Dan Egnor661f0132010-02-19 11:23:00 -0800211 * {@link #confirmCredentials}, rather than prompting the user with
212 * the standard password prompt.
213 */
214 public static final String KEY_PASSWORD = "password";
215
216 public static final String KEY_ACCOUNTS = "accounts";
Brian Carlstrom46703b02011-04-06 15:41:29 -0700217
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700218 public static final String KEY_ACCOUNT_AUTHENTICATOR_RESPONSE = "accountAuthenticatorResponse";
219 public static final String KEY_ACCOUNT_MANAGER_RESPONSE = "accountManagerResponse";
Dan Egnor661f0132010-02-19 11:23:00 -0800220 public static final String KEY_AUTHENTICATOR_TYPES = "authenticator_types";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700221 public static final String KEY_AUTH_FAILED_MESSAGE = "authFailedMessage";
222 public static final String KEY_AUTH_TOKEN_LABEL = "authTokenLabelKey";
Dan Egnor661f0132010-02-19 11:23:00 -0800223 public static final String KEY_BOOLEAN_RESULT = "booleanResult";
224 public static final String KEY_ERROR_CODE = "errorCode";
225 public static final String KEY_ERROR_MESSAGE = "errorMessage";
226 public static final String KEY_USERDATA = "userdata";
Costin Manolacheb61e8fb2011-09-08 11:26:09 -0700227
Costin Manolachea40c6302010-12-13 14:50:45 -0800228 /**
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800229 * Bundle key used to supply the last time the credentials of the account
230 * were authenticated successfully. Time is specified in milliseconds since
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700231 * epoch. Associated time is updated on successful authentication of account
232 * on adding account, confirming credentials, or updating credentials.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800233 */
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -0700234 public static final String KEY_LAST_AUTHENTICATED_TIME = "lastAuthenticatedTime";
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800235
236 /**
Costin Manolachea40c6302010-12-13 14:50:45 -0800237 * Authenticators using 'customTokens' option will also get the UID of the
238 * caller
239 */
240 public static final String KEY_CALLER_UID = "callerUid";
241 public static final String KEY_CALLER_PID = "callerPid";
Dan Egnor661f0132010-02-19 11:23:00 -0800242
Costin Manolached6060452011-01-24 16:11:36 -0800243 /**
Fred Quintanaad93a322011-09-08 13:21:01 -0700244 * The Android package of the caller will be set in the options bundle by the
245 * {@link AccountManager} and will be passed to the AccountManagerService and
246 * to the AccountAuthenticators. The uid of the caller will be known by the
247 * AccountManagerService as well as the AccountAuthenticators so they will be able to
248 * verify that the package is consistent with the uid (a uid might be shared by many
249 * packages).
250 */
251 public static final String KEY_ANDROID_PACKAGE_NAME = "androidPackageName";
252
253 /**
Costin Manolached6060452011-01-24 16:11:36 -0800254 * Boolean, if set and 'customTokens' the authenticator is responsible for
255 * notifications.
256 * @hide
257 */
258 public static final String KEY_NOTIFY_ON_FAILURE = "notifyOnAuthFailure";
259
Sandra Kwan78812282015-11-04 11:19:47 -0800260 /**
261 * Bundle key used for a {@link Bundle} in result from
262 * {@link #startAddAccountSession} and friends which returns session data
263 * for installing an account later.
264 */
265 public static final String KEY_ACCOUNT_SESSION_BUNDLE = "accountSessionBundle";
266
267 /**
268 * Bundle key used for the {@link String} account status token in result
269 * from {@link #startAddAccountSession} and friends which returns
270 * information about a particular account.
271 */
272 public static final String KEY_ACCOUNT_STATUS_TOKEN = "accountStatusToken";
273
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700274 public static final String ACTION_AUTHENTICATOR_INTENT =
275 "android.accounts.AccountAuthenticator";
276 public static final String AUTHENTICATOR_META_DATA_NAME =
Dan Egnor661f0132010-02-19 11:23:00 -0800277 "android.accounts.AccountAuthenticator";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700278 public static final String AUTHENTICATOR_ATTRIBUTES_NAME = "account-authenticator";
279
Dmitry Dementyev52745472016-12-02 10:27:45 -0800280 /** @hide */
281 @Retention(RetentionPolicy.SOURCE)
282 @IntDef({VISIBILITY_UNDEFINED, VISIBILITY_VISIBLE, VISIBILITY_USER_MANAGED_VISIBLE,
283 VISIBILITY_NOT_VISIBLE, VISIBILITY_USER_MANAGED_NOT_VISIBLE})
284 public @interface AccountVisibility {
285 }
286
287 /**
288 * Account visibility was not set.
289 * @hide
290 */
291 public static final int VISIBILITY_UNDEFINED = 0;
292
293 /**
294 * Account is always visible to given application and only authenticator can revoke visibility.
295 * @hide
296 */
297 public static final int VISIBILITY_VISIBLE = 1;
298
299 /**
300 * Account is visible to given application, but user can revoke visibility.
301 * @hide
302 */
303 public static final int VISIBILITY_USER_MANAGED_VISIBLE = 2;
304
305 /**
306 * Account is not visible to given application and only authenticator can grant visibility.
307 * @hide
308 */
309 public static final int VISIBILITY_NOT_VISIBLE = 3;
310
311 /**
312 * Account is not visible to given application, but user can reveal it, for example, using
313 * {@link #newChooseAccountIntent(Account, List, String[], String, String, String[], Bundle)}
314 * @hide
315 */
316 public static final int VISIBILITY_USER_MANAGED_NOT_VISIBLE = 4;
317
318 /**
319 * Key to manifest entry with a list of account types in which application is interested.
320 * Example value: "com.google;com.customtype". If it is specified then the application
321 * will only get notifications related to the types in the list (see
322 * {@link #ACTION_VISIBLE_ACCOUNTS_CHANGED}). Authenticators managing whitelisted types will be
323 * able to know about the application using {@link #ACTION_ACCOUNTS_LISTENER_PACKAGE_INSTALLED}
324 * @hide
325 */
326 public static final String SUPPORTED_ACCOUNT_TYPES = "android.accounts.SupportedAccountTypes";
327
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700328 /**
Svet Ganovf6d424f12016-09-20 20:18:53 -0700329 * Token type for the special case where a UID has access only to an account
330 * but no authenticator specific auth token types.
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700331 *
332 * @hide
333 */
Svet Ganovf6d424f12016-09-20 20:18:53 -0700334 public static final String ACCOUNT_ACCESS_TOKEN_TYPE =
335 "com.android.AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE";
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700336
Fred Quintana60307342009-03-24 22:48:12 -0700337 private final Context mContext;
338 private final IAccountManager mService;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700339 private final Handler mMainHandler;
Dan Egnor661f0132010-02-19 11:23:00 -0800340
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700341 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800342 * Action sent as a broadcast Intent by the AccountsService when accounts are added, accounts
343 * are removed, or an account's credentials (saved password, etc) are changed.
Dan Egnor661f0132010-02-19 11:23:00 -0800344 *
345 * @see #addOnAccountsUpdatedListener
Dmitry Dementyev52745472016-12-02 10:27:45 -0800346 *
347 * Deprecated - use ACTION_VISIBLE_ACCOUNTS_CHANGED instead.
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700348 */
349 public static final String LOGIN_ACCOUNTS_CHANGED_ACTION =
350 "android.accounts.LOGIN_ACCOUNTS_CHANGED";
Fred Quintana60307342009-03-24 22:48:12 -0700351
Fred Quintana33269202009-04-20 16:05:10 -0700352 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800353 * Action sent as a broadcast Intent by the AccountsService when accounts potentially visible to
354 * the applications are added, accounts are removed, or an account's credentials (saved
355 * password, etc) are changed. List of supported account types shoud be specified in the
356 * Manifest file using {@link #SUPPORTED_ACCOUNT_TYPES}
357 *
358 * @see #addOnAccountsUpdatedListener
359 * @hide
360 */
361 public static final String ACTION_VISIBLE_ACCOUNTS_CHANGED =
362 "android.accounts.action.VISIBLE_ACCOUNTS_CHANGED";
363
364 /**
365 * Authenticators may subscribe to get notifications about apps interested in their managed account
366 * types using {@link #SUPPORTED_ACCOUNT_TYPES}.
367 * @hide
368 */
369 public static final String ACTION_ACCOUNTS_LISTENER_PACKAGE_INSTALLED =
370 "android.accounts.action.ACCOUNTS_LISTENER_PACKAGE_INSTALLED";
371
372 /**
373 * Uid key to set default visibility for applications targeting API level
374 * {@link android.os.Build.VERSION_CODES#O} or above. See {@link #getAccountVisibility}. If the
375 * value was not set by authenticator USER_MANAGED_NOT_VISIBLE is used.
376 * @hide
377 */
378 public static final int DEFAULT_VISIBILITY = -2;
379
380 /**
381 * Uid key to set visibility for applications targeting API level below
382 * {@link android.os.Build.VERSION_CODES#O}, which were able to see the account before. It
383 * includes applications with GET_ACCOUNTS permission or with the same signature as
384 * authenticator. See {@link #getAccountVisibility}. If the value was not set by authenticator
385 * USER_MANAGED_VISIBLE is used.
386 * @hide
387 */
388 public static final int DEFAULT_LEGACY_VISIBILITY = -3;
389
390 /**
Fred Quintana33269202009-04-20 16:05:10 -0700391 * @hide
392 */
Fred Quintana60307342009-03-24 22:48:12 -0700393 public AccountManager(Context context, IAccountManager service) {
394 mContext = context;
395 mService = service;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700396 mMainHandler = new Handler(mContext.getMainLooper());
Fred Quintana60307342009-03-24 22:48:12 -0700397 }
398
Fred Quintana0eabf022009-04-27 15:08:17 -0700399 /**
400 * @hide used for testing only
401 */
402 public AccountManager(Context context, IAccountManager service, Handler handler) {
403 mContext = context;
404 mService = service;
405 mMainHandler = handler;
406 }
407
Fred Quintana756b7352009-10-21 13:43:10 -0700408 /**
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800409 * @hide for internal use only
410 */
411 public static Bundle sanitizeResult(Bundle result) {
Fred Quintana382601f2010-03-25 12:25:10 -0700412 if (result != null) {
413 if (result.containsKey(KEY_AUTHTOKEN)
414 && !TextUtils.isEmpty(result.getString(KEY_AUTHTOKEN))) {
415 final Bundle newResult = new Bundle(result);
416 newResult.putString(KEY_AUTHTOKEN, "<omitted for logging purposes>");
417 return newResult;
418 }
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800419 }
420 return result;
421 }
422
423 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800424 * Gets an AccountManager instance associated with a Context.
425 * The {@link Context} will be used as long as the AccountManager is
426 * active, so make sure to use a {@link Context} whose lifetime is
427 * commensurate with any listeners registered to
428 * {@link #addOnAccountsUpdatedListener} or similar methods.
429 *
430 * <p>It is safe to call this method from the main thread.
431 *
432 * <p>No permission is required to call this method.
433 *
Fred Quintana756b7352009-10-21 13:43:10 -0700434 * @param context The {@link Context} to use when necessary
Dan Egnor661f0132010-02-19 11:23:00 -0800435 * @return An {@link AccountManager} instance
Fred Quintana756b7352009-10-21 13:43:10 -0700436 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700437 public static AccountManager get(Context context) {
Fred Quintana382601f2010-03-25 12:25:10 -0700438 if (context == null) throw new IllegalArgumentException("context is null");
Fred Quintanaa698f422009-04-08 19:14:54 -0700439 return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
440 }
441
Fred Quintana756b7352009-10-21 13:43:10 -0700442 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800443 * Gets the saved password associated with the account. This is intended for authenticators and
444 * related code; applications should get an auth token instead.
Dan Egnor661f0132010-02-19 11:23:00 -0800445 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800446 * <p>
447 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800448 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800449 * <p>
450 * This method requires the caller to have a signature match with the authenticator that owns
451 * the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800452 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800453 * <p>
454 * <b>NOTE:</b> If targeting your app to work on API level
455 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before, AUTHENTICATE_ACCOUNTS
456 * permission is needed for those platforms. See docs for this function in API level
457 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700458 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700459 * @param account The account to query for a password. Must not be {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -0800460 * @return The account's password, null if none or if the account doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700461 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700462 public String getPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -0700463 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintana60307342009-03-24 22:48:12 -0700464 try {
465 return mService.getPassword(account);
466 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700467 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700468 }
469 }
470
Fred Quintana756b7352009-10-21 13:43:10 -0700471 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800472 * Gets the user data named by "key" associated with the account. This is intended for
473 * authenticators and related code to store arbitrary metadata along with accounts. The meaning
474 * of the keys and values is up to the authenticator for the account.
Dan Egnor661f0132010-02-19 11:23:00 -0800475 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800476 * <p>
477 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800478 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800479 * <p>
480 * This method requires the caller to have a signature match with the authenticator that owns
481 * the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800482 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800483 * <p>
484 * <b>NOTE:</b> If targeting your app to work on API level
485 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before, AUTHENTICATE_ACCOUNTS
486 * permission is needed for those platforms. See docs for this function in API level
487 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700488 *
Dan Egnor661f0132010-02-19 11:23:00 -0800489 * @param account The account to query for user data
490 * @return The user data, null if the account or key doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700491 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700492 public String getUserData(final Account account, final String key) {
Fred Quintana382601f2010-03-25 12:25:10 -0700493 if (account == null) throw new IllegalArgumentException("account is null");
494 if (key == null) throw new IllegalArgumentException("key is null");
Fred Quintana60307342009-03-24 22:48:12 -0700495 try {
496 return mService.getUserData(account, key);
497 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700498 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700499 }
500 }
501
Fred Quintana756b7352009-10-21 13:43:10 -0700502 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800503 * Lists the currently registered authenticators.
504 *
505 * <p>It is safe to call this method from the main thread.
506 *
507 * <p>No permission is required to call this method.
508 *
509 * @return An array of {@link AuthenticatorDescription} for every
510 * authenticator known to the AccountManager service. Empty (never
511 * null) if no authenticators are known.
Fred Quintana756b7352009-10-21 13:43:10 -0700512 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700513 public AuthenticatorDescription[] getAuthenticatorTypes() {
Fred Quintanaa698f422009-04-08 19:14:54 -0700514 try {
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100515 return mService.getAuthenticatorTypes(UserHandle.getCallingUserId());
516 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700517 throw e.rethrowFromSystemServer();
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100518 }
519 }
520
521 /**
522 * @hide
523 * Lists the currently registered authenticators for a given user id.
524 *
525 * <p>It is safe to call this method from the main thread.
526 *
527 * <p>The caller has to be in the same user or have the permission
528 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
529 *
530 * @return An array of {@link AuthenticatorDescription} for every
531 * authenticator known to the AccountManager service. Empty (never
532 * null) if no authenticators are known.
533 */
534 public AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) {
535 try {
536 return mService.getAuthenticatorTypes(userId);
Fred Quintanaa698f422009-04-08 19:14:54 -0700537 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700538 throw e.rethrowFromSystemServer();
Fred Quintanaa698f422009-04-08 19:14:54 -0700539 }
540 }
541
Fred Quintana756b7352009-10-21 13:43:10 -0700542 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800543 * Lists all accounts visible to the caller regardless of type. Equivalent to
544 * getAccountsByType(null). These accounts may be visible because the user granted access to the
545 * account, or the AbstractAcccountAuthenticator managing the account did so or because the
546 * client shares a signature with the managing AbstractAccountAuthenticator.
Dan Egnor661f0132010-02-19 11:23:00 -0800547 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800548 * <p>
549 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800550 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800551 * @return An array of {@link Account}, one for each account. Empty (never null) if no accounts
552 * have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700553 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700554 @NonNull
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700555 public Account[] getAccounts() {
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000556 try {
557 return mService.getAccounts(null, mContext.getOpPackageName());
558 } catch (RemoteException e) {
559 throw e.rethrowFromSystemServer();
560 }
Fred Quintana60307342009-03-24 22:48:12 -0700561 }
562
Fred Quintana756b7352009-10-21 13:43:10 -0700563 /**
Amith Yamasani27db4682013-03-30 17:07:47 -0700564 * @hide
Dmitry Dementyev52745472016-12-02 10:27:45 -0800565 * Lists all accounts visible to caller regardless of type for a given user id. Equivalent to
566 * getAccountsByType(null).
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100567 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800568 * <p>
569 * It is safe to call this method from the main thread.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100570 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800571 * @return An array of {@link Account}, one for each account. Empty (never null) if no accounts
572 * have been added.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100573 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700574 @NonNull
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100575 public Account[] getAccountsAsUser(int userId) {
576 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700577 return mService.getAccountsAsUser(null, userId, mContext.getOpPackageName());
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100578 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700579 throw e.rethrowFromSystemServer();
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100580 }
581 }
582
583 /**
584 * @hide
Amith Yamasani27db4682013-03-30 17:07:47 -0700585 * For use by internal activities. Returns the list of accounts that the calling package
586 * is authorized to use, particularly for shared accounts.
587 * @param packageName package name of the calling app.
588 * @param uid the uid of the calling app.
589 * @return the accounts that are available to this package and user.
590 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700591 @NonNull
Amith Yamasani27db4682013-03-30 17:07:47 -0700592 public Account[] getAccountsForPackage(String packageName, int uid) {
593 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700594 return mService.getAccountsForPackage(packageName, uid, mContext.getOpPackageName());
Amith Yamasani27db4682013-03-30 17:07:47 -0700595 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700596 throw re.rethrowFromSystemServer();
Amith Yamasani27db4682013-03-30 17:07:47 -0700597 }
598 }
599
600 /**
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700601 * Returns the accounts visible to the specified package, in an environment where some apps
602 * are not authorized to view all accounts. This method can only be called by system apps.
603 * @param type The type of accounts to return, null to retrieve all accounts
604 * @param packageName The package name of the app for which the accounts are to be returned
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000605 * @return An array of {@link Account}, one per matching account. Empty
606 * (never null) if no accounts of the specified type have been added.
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700607 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700608 @NonNull
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700609 public Account[] getAccountsByTypeForPackage(String type, String packageName) {
610 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700611 return mService.getAccountsByTypeForPackage(type, packageName,
612 mContext.getOpPackageName());
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700613 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700614 throw re.rethrowFromSystemServer();
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700615 }
616 }
617
618 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800619 * Lists all accounts of particular type visible to the caller. These accounts may be visible
620 * because the user granted access to the account, or the AbstractAcccountAuthenticator managing
621 * the account did so or because the client shares a signature with the managing
622 * AbstractAccountAuthenticator.
Dan Egnor661f0132010-02-19 11:23:00 -0800623 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800624 * <p>
625 * The account type is a string token corresponding to the authenticator and useful domain of
626 * the account. For example, there are types corresponding to Google and Facebook. The exact
627 * string token to use will be published somewhere associated with the authenticator in
628 * question.
Dan Egnor661f0132010-02-19 11:23:00 -0800629 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800630 * <p>
631 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800632 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800633 * <p>
634 * Caller targeting API level {@link android.os.Build.VERSION_CODES#O} and above, will get list
635 * of accounts made visible to it by user or AbstractAcccountAuthenticator and
636 * {@link android.Manifest.permission#GET_ACCOUNTS} permission is not used.
637 *
638 * <p>
639 * Caller targeting API level below {@link android.os.Build.VERSION_CODES#O} that have not been
640 * granted the {@link android.Manifest.permission#GET_ACCOUNTS} permission, will only see those
641 * accounts managed by AbstractAccountAuthenticators whose signature matches the client.
642 *
643 * <p>
644 * <b>NOTE:</b> If targeting your app to work on API level
645 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before, GET_ACCOUNTS permission is
646 * needed for those platforms, irrespective of uid or signature match. See docs for this
647 * function in API level {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000648 *
649 * @param type The type of accounts to return, null to retrieve all accounts
Dmitry Dementyev52745472016-12-02 10:27:45 -0800650 * @return An array of {@link Account}, one per matching account. Empty (never null) if no
651 * accounts of the specified type have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700652 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700653 @NonNull
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700654 public Account[] getAccountsByType(String type) {
Amith Yamasani2c7bc262012-11-05 16:46:02 -0800655 return getAccountsByTypeAsUser(type, Process.myUserHandle());
656 }
657
658 /** @hide Same as {@link #getAccountsByType(String)} but for a specific user. */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700659 @NonNull
Amith Yamasani2c7bc262012-11-05 16:46:02 -0800660 public Account[] getAccountsByTypeAsUser(String type, UserHandle userHandle) {
Fred Quintana60307342009-03-24 22:48:12 -0700661 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700662 return mService.getAccountsAsUser(type, userHandle.getIdentifier(),
663 mContext.getOpPackageName());
Fred Quintana60307342009-03-24 22:48:12 -0700664 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700665 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700666 }
667 }
668
Fred Quintana756b7352009-10-21 13:43:10 -0700669 /**
Fred Quintanad9640ec2012-05-23 12:37:00 -0700670 * Change whether or not an app (identified by its uid) is allowed to retrieve an authToken
671 * for an account.
672 * <p>
673 * This is only meant to be used by system activities and is not in the SDK.
674 * @param account The account whose permissions are being modified
675 * @param authTokenType The type of token whose permissions are being modified
676 * @param uid The uid that identifies the app which is being granted or revoked permission.
677 * @param value true is permission is being granted, false for revoked
678 * @hide
679 */
680 public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) {
681 try {
682 mService.updateAppPermission(account, authTokenType, uid, value);
683 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700684 throw e.rethrowFromSystemServer();
Fred Quintanad9640ec2012-05-23 12:37:00 -0700685 }
686 }
687
688 /**
689 * Get the user-friendly label associated with an authenticator's auth token.
690 * @param accountType the type of the authenticator. must not be null.
691 * @param authTokenType the token type. must not be null.
692 * @param callback callback to invoke when the result is available. may be null.
693 * @param handler the handler on which to invoke the callback, or null for the main thread
694 * @return a future containing the label string
695 * @hide
696 */
697 public AccountManagerFuture<String> getAuthTokenLabel(
698 final String accountType, final String authTokenType,
699 AccountManagerCallback<String> callback, Handler handler) {
700 if (accountType == null) throw new IllegalArgumentException("accountType is null");
701 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
702 return new Future2Task<String>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800703 @Override
Fred Quintanad9640ec2012-05-23 12:37:00 -0700704 public void doWork() throws RemoteException {
705 mService.getAuthTokenLabel(mResponse, accountType, authTokenType);
706 }
707
708 @Override
709 public String bundleToResult(Bundle bundle) throws AuthenticatorException {
710 if (!bundle.containsKey(KEY_AUTH_TOKEN_LABEL)) {
711 throw new AuthenticatorException("no result in response");
712 }
713 return bundle.getString(KEY_AUTH_TOKEN_LABEL);
714 }
715 }.start();
716 }
717
718 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800719 * Finds out whether a particular account has all the specified features. Account features are
720 * authenticator-specific string tokens identifying boolean account properties. For example,
721 * features are used to tell whether Google accounts have a particular service (such as Google
722 * Calendar or Google Talk) enabled. The feature names and their meanings are published
723 * somewhere associated with the authenticator in question.
Dan Egnor661f0132010-02-19 11:23:00 -0800724 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800725 * <p>
726 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
727 * not be used on the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800728 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800729 * <p>
730 * If caller target API level is below {@link android.os.Build.VERSION_CODES#O}, it is
731 * required to hold the permission {@link android.Manifest.permission#GET_ACCOUNTS} or have a
732 * signature match with the AbstractAccountAuthenticator that manages the account.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800733 *
734 * @param account The {@link Account} to test
Dan Egnor661f0132010-02-19 11:23:00 -0800735 * @param features An array of the account features to check
Dmitry Dementyev52745472016-12-02 10:27:45 -0800736 * @param callback Callback to invoke when the request completes, null for no callback
737 * @param handler {@link Handler} identifying the callback thread, null for the main thread
738 * @return An {@link AccountManagerFuture} which resolves to a Boolean, true if the account
739 * exists and has all of the specified features.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800740 */
Fred Quintana3084a6f2010-01-14 18:02:03 -0800741 public AccountManagerFuture<Boolean> hasFeatures(final Account account,
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800742 final String[] features,
743 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700744 if (account == null) throw new IllegalArgumentException("account is null");
745 if (features == null) throw new IllegalArgumentException("features is null");
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800746 return new Future2Task<Boolean>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800747 @Override
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800748 public void doWork() throws RemoteException {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700749 mService.hasFeatures(mResponse, account, features, mContext.getOpPackageName());
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800750 }
Sandra Kwan0b84b452016-01-20 15:25:42 -0800751 @Override
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800752 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
753 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
754 throw new AuthenticatorException("no result in response");
755 }
756 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
757 }
758 }.start();
759 }
760
761 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800762 * Lists all accounts of a type which have certain features. The account type identifies the
763 * authenticator (see {@link #getAccountsByType}). Account features are authenticator-specific
764 * string tokens identifying boolean account properties (see {@link #hasFeatures}).
Fred Quintana756b7352009-10-21 13:43:10 -0700765 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800766 * <p>
767 * Unlike {@link #getAccountsByType}, this method calls the authenticator, which may contact the
768 * server or do other work to check account features, so the method returns an
769 * {@link AccountManagerFuture}.
Fred Quintanaa698f422009-04-08 19:14:54 -0700770 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800771 * <p>
772 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
773 * not be used on the main thread.
Fred Quintana756b7352009-10-21 13:43:10 -0700774 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800775 * <p>
776 * Caller targeting API level {@link android.os.Build.VERSION_CODES#O} and above, will get list
777 * of accounts made visible to it by user or AbstractAcccountAuthenticator and
778 * {@link android.Manifest.permission#GET_ACCOUNTS} permission is not used.
779 *
780 * <p>
781 * Caller targeting API level below {@link android.os.Build.VERSION_CODES#O} that have not been
782 * granted the {@link android.Manifest.permission#GET_ACCOUNTS} permission, will only see those
783 * accounts managed by AbstractAccountAuthenticators whose signature matches the client.
784 * <p>
785 * <b>NOTE:</b> If targeting your app to work on API level
786 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before, GET_ACCOUNTS permission is
787 * needed for those platforms, irrespective of uid or signature match. See docs for this
788 * function in API level {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
789 *
Fred Quintana756b7352009-10-21 13:43:10 -0700790 *
Dan Egnor661f0132010-02-19 11:23:00 -0800791 * @param type The type of accounts to return, must not be null
Dmitry Dementyev52745472016-12-02 10:27:45 -0800792 * @param features An array of the account features to require, may be null or empty *
793 * @param callback Callback to invoke when the request completes, null for no callback
794 * @param handler {@link Handler} identifying the callback thread, null for the main thread
795 * @return An {@link AccountManagerFuture} which resolves to an array of {@link Account}, one
796 * per account of the specified type which matches the requested features.
Fred Quintana8570f742010-02-18 10:32:54 -0800797 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700798 public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures(
799 final String type, final String[] features,
800 AccountManagerCallback<Account[]> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700801 if (type == null) throw new IllegalArgumentException("type is null");
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700802 return new Future2Task<Account[]>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800803 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700804 public void doWork() throws RemoteException {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700805 mService.getAccountsByFeatures(mResponse, type, features,
806 mContext.getOpPackageName());
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700807 }
Sandra Kwan0b84b452016-01-20 15:25:42 -0800808 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700809 public Account[] bundleToResult(Bundle bundle) throws AuthenticatorException {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700810 if (!bundle.containsKey(KEY_ACCOUNTS)) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700811 throw new AuthenticatorException("no result in response");
812 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700813 final Parcelable[] parcelables = bundle.getParcelableArray(KEY_ACCOUNTS);
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700814 Account[] descs = new Account[parcelables.length];
815 for (int i = 0; i < parcelables.length; i++) {
816 descs[i] = (Account) parcelables[i];
817 }
818 return descs;
819 }
820 }.start();
821 }
822
Fred Quintana756b7352009-10-21 13:43:10 -0700823 /**
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700824 * Adds an account directly to the AccountManager. Normally used by sign-up
Dan Egnor661f0132010-02-19 11:23:00 -0800825 * wizards associated with authenticators, not directly by applications.
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700826 * <p>Calling this method does not update the last authenticated timestamp,
827 * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
828 * {@link #notifyAccountAuthenticated(Account)} after getting success.
829 * However, if this method is called when it is triggered by addAccount() or
830 * addAccountAsUser() or similar functions, then there is no need to update
831 * timestamp manually as it is updated automatically by framework on
832 * successful completion of the mentioned functions.
Dan Egnor661f0132010-02-19 11:23:00 -0800833 * <p>It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700834 * <p>This method requires the caller to have a signature match with the
835 * authenticator that owns the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800836 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700837 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
838 * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs
839 * for this function in API level 22.
840 *
Dan Egnor661f0132010-02-19 11:23:00 -0800841 * @param account The {@link Account} to add
842 * @param password The password to associate with the account, null for none
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700843 * @param userdata String values to use for the account's userdata, null for
844 * none
Dan Egnor8e4378b2010-08-02 18:22:09 -0700845 * @return True if the account was successfully added, false if the account
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700846 * already exists, the account is null, or another error occurs.
Dan Egnor661f0132010-02-19 11:23:00 -0800847 */
848 public boolean addAccountExplicitly(Account account, String password, Bundle userdata) {
Fred Quintana382601f2010-03-25 12:25:10 -0700849 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800850 try {
Amith Yamasani27db4682013-03-30 17:07:47 -0700851 return mService.addAccountExplicitly(account, password, userdata);
Dan Egnor661f0132010-02-19 11:23:00 -0800852 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700853 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -0800854 }
855 }
856
857 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800858 * Adds an account directly to the AccountManager. Additionally this makes the Account visible
859 * to desired UIDs of applications on the device, and sends directed broadcasts to these
860 * individual applications.
861 * <p>
862 * Normally used by sign-up wizards associated with authenticators, not directly by
863 * applications.
864 * <p>
865 * Calling this method does not update the last authenticated timestamp, referred by
866 * {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700867 * {@link #notifyAccountAuthenticated(Account)} after getting success.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800868 * <p>
869 * It is safe to call this method from the main thread.
870 * <p>
871 * This method requires the caller to have a signature match with the authenticator that owns
872 * the specified account.
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700873 *
874 * @param account The {@link Account} to add
875 * @param password The password to associate with the account, null for none
Dmitry Dementyev52745472016-12-02 10:27:45 -0800876 * @param extras String values to use for the account's userdata, null for none
877 * @param selectedUids Array of uids whose associated applications can access this account
878 * without any additional user approval.
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700879 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800880 * @return True if the account was successfully added, false if the account already exists, the
881 * account is null, or another error occurs.
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700882 */
883 public boolean addAccountExplicitly(Account account, String password, Bundle extras,
Dmitry Dementyev52745472016-12-02 10:27:45 -0800884 int[] selectedUids) {
885 return false; // TODO remove this method.
886 }
887
888 /**
889 * Adds an account directly to the AccountManager. Additionally this makes the Account visible
890 * to desired UIDs of applications on the device, and sends directed broadcasts to these
891 * individual applications.
892 * <p>
893 * Normally used by sign-up wizards associated with authenticators, not directly by
894 * applications.
895 * <p>
896 * Calling this method does not update the last authenticated timestamp, referred by
897 * {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
898 * {@link #notifyAccountAuthenticated(Account)} after getting success.
899 * <p>
900 * It is safe to call this method from the main thread.
901 * <p>
902 * This method requires the caller to have a signature match with the authenticator that owns
903 * the specified account.
904 *
905 * @param account The {@link Account} to add
906 * @param password The password to associate with the account, null for none
907 * @param extras String values to use for the account's userdata, null for none
908 * @param visibility Map from uid to visibility values which will be set before account is
909 * added. See getAccountVisibility for possilbe values.
910 *
911 * @return True if the account was successfully added, false if the account already exists, the
912 * account is null, or another error occurs.
913 * @hide
914 */
915 public boolean addAccountExplicitly(Account account, String password, Bundle extras,
916 Map<Integer, Integer> visibility) {
917 if (account == null)
918 throw new IllegalArgumentException("account is null");
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700919 try {
Dmitry Dementyev52745472016-12-02 10:27:45 -0800920 return mService.addAccountExplicitlyWithVisibility(account, password, extras,
921 visibility);
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700922 } catch (RemoteException e) {
923 throw e.rethrowFromSystemServer();
924 }
925 }
926
927 /**
928 * Returns all UIDs for applications that requested the account type.
929 * <p>This method requires the caller to have a signature match with the authenticator
930 * that owns the specified account.
931 *
932 * @param accountType The account type to be authenticated.
933 *
934 * @return array of all UIDs that support accounts of this
935 * account type that seek approval (to be used to know which accounts for
936 * the authenticator to include in addAccountExplicitly). Null if none.
937 */
938 public int[] getRequestingUidsForType(String accountType) {
939 try {
940 return mService.getRequestingUidsForType(accountType);
941 } catch (RemoteException re) {
942 throw re.rethrowFromSystemServer();
943 }
944 }
945
946 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800947 * Gets all accounts of given type and their visibility for specific package. This method
948 * requires the caller to have a signature match with the authenticator that manages
949 * accountType. It is a helper method which combines calls to {@link #getAccountsByType} by
950 * authenticator and {@link #getAccountVisibility} for every returned account.
951 *
952 * <p>
953 *
954 * @param packageName Package name.
955 * @param accountType Account type.
956 *
957 * @return Map with visibility for all accounts of given type. See {@link #getAccountVisibility}
958 * for possilbe values.
959 * @hide
960 */
961 public Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName,
962 String accountType) {
963 try {
964 @SuppressWarnings("unchecked")
965 Map<Account, Integer> result = (Map<Account, Integer>) mService
966 .getAccountsAndVisibilityForPackage(packageName, accountType);
967 return result;
968 } catch (RemoteException re) {
969 throw re.rethrowFromSystemServer();
970 }
971 }
972
973 /**
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700974 * Gives a certain UID, represented a application, access to an account
Dmitry Dementyev52745472016-12-02 10:27:45 -0800975 * <p>
976 * This method requires the caller to have a signature match with the authenticator that owns
977 * the specified account.
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700978 *
979 * @param account Account to make visible.
980 * @param uid The UID of the application to add account access.
981 *
982 * @return True if account made visible to application and was not previously visible.
983 */
984 public boolean makeAccountVisible(Account account, int uid) {
985 try {
Dmitry Dementyev52745472016-12-02 10:27:45 -0800986 return mService.setAccountVisibility(account, uid, VISIBILITY_USER_MANAGED_VISIBLE);
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700987 } catch (RemoteException re) {
988 throw re.rethrowFromSystemServer();
989 }
990 }
991
992 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800993 * Removes visibility of certain account of a process identified by a given UID to an
994 * application. This is called by the Authenticator.
995 * <p>
996 * This method requires the caller to have a signature match with the authenticator that owns
997 * the specified account.
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700998 *
999 * @param account Remove visibility of this account..
1000 * @param uid The UID of the application to remove account access.
1001 *
1002 * @return True if application access to account removed and was previously visible.
1003 */
1004 public boolean removeAccountVisibility(Account account, int uid) {
1005 try {
Dmitry Dementyev52745472016-12-02 10:27:45 -08001006 return mService.setAccountVisibility(account, uid, VISIBILITY_USER_MANAGED_NOT_VISIBLE);
Tejas Khorana5edff3b2016-06-28 20:59:52 -07001007 } catch (RemoteException re) {
1008 throw re.rethrowFromSystemServer();
1009 }
1010 }
1011
1012 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -08001013 * Checks visibility of certain account of a process identified by a given UID. This is called
1014 * by the Authenticator.
1015 * <p>
1016 * This method requires the caller to have a signature match with the authenticator that owns
1017 * the specified account.
Tejas Khorana5edff3b2016-06-28 20:59:52 -07001018 *
1019 * @param account Account to check visibility.
1020 * @param uid The UID of the application to check account access.
1021 *
1022 * @return True if application has access to the account
1023 */
1024 public boolean isAccountVisible(Account account, int uid) {
1025 try {
Dmitry Dementyev52745472016-12-02 10:27:45 -08001026 Integer visibility = mService.getAccountVisibility(account, uid);
1027 return visibility == VISIBILITY_USER_MANAGED_NOT_VISIBLE
1028 || visibility == VISIBILITY_VISIBLE;
1029 } catch (RemoteException re) {
1030 throw re.rethrowFromSystemServer();
1031 }
1032 }
1033
1034 /**
1035 * Set visibility value of given account to certain UID.
1036 * <p>
1037 * See {@link #getAccountVisibility} for possible values.
1038 * <p>
1039 * This method requires the caller to have a signature match with the authenticator that owns
1040 * the specified account.
1041 *
1042 * @param account Account to make visible.
1043 * @param uid The UID of the application to modify account visibility.
1044 * @param visibility - new visibility value.
1045 *
1046 * @return True if visibility value was succesfully updated.
1047 * @hide
1048 */
1049 public boolean setAccountVisibility(Account account, int uid,
1050 @AccountVisibility int visibility) {
1051 try {
1052 return mService.setAccountVisibility(account, uid, visibility);
1053 } catch (RemoteException re) {
1054 throw re.rethrowFromSystemServer();
1055 }
1056 }
1057
1058 /**
1059 * Gets visibility of certain account for given UID. Possible returned values are:
1060 * <ul>
1061 * <li>{@link #VISIBILITY_VISIBLE}</li>
1062 * <li>{@link #VISIBILITY_USER_MANAGED_VISIBLE}</li>
1063 * <li>{@link #VISIBILITY_NOT_VISIBLE}
1064 * <li>{@link #VISIBILITY_USER_MANAGED_NOT_VISIBLE}</li>
1065 * </ul>
1066 *
1067 * <p>
1068 * This method requires the caller to have a signature match with the authenticator that owns
1069 * the specified account.
1070 *
1071 * @param account Account to get visibility.
1072 * @param uid The UID of the application to get account visibility.
1073 *
1074 * @return int Visibility for given account and uid.
1075 * @hide
1076 */
1077 public @AccountVisibility int getAccountVisibility(Account account, int uid) {
1078 try {
1079 return mService.getAccountVisibility(account, uid);
Tejas Khorana5edff3b2016-06-28 20:59:52 -07001080 } catch (RemoteException re) {
1081 throw re.rethrowFromSystemServer();
1082 }
1083 }
1084
1085 /**
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -07001086 * Notifies the system that the account has just been authenticated. This
1087 * information may be used by other applications to verify the account. This
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001088 * should be called only when the user has entered correct credentials for
1089 * the account.
1090 * <p>
1091 * It is not safe to call this method from the main thread. As such, call it
1092 * from another thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001093 * <p>This method requires the caller to have a signature match with the
1094 * authenticator that owns the specified account.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001095 *
1096 * @param account The {@link Account} to be updated.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001097 * @return boolean {@code true} if the authentication of the account has been successfully
1098 * acknowledged. Otherwise {@code false}.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001099 */
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -07001100 public boolean notifyAccountAuthenticated(Account account) {
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001101 if (account == null)
1102 throw new IllegalArgumentException("account is null");
1103 try {
1104 return mService.accountAuthenticated(account);
1105 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001106 throw e.rethrowFromSystemServer();
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001107 }
1108 }
1109
1110 /**
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001111 * Rename the specified {@link Account}. This is equivalent to removing
1112 * the existing account and adding a new renamed account with the old
1113 * account's user data.
1114 *
1115 * <p>It is safe to call this method from the main thread.
1116 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001117 * <p>This method requires the caller to have a signature match with the
1118 * authenticator that manages the specified account.
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001119 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001120 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1121 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1122 * is needed for those platforms. See docs for this function in API level 22.
1123 *
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001124 * @param account The {@link Account} to rename
1125 * @param newName String name to be associated with the account.
1126 * @param callback Callback to invoke when the request completes, null for
1127 * no callback
1128 * @param handler {@link Handler} identifying the callback thread, null for
1129 * the main thread
1130 * @return An {@link AccountManagerFuture} which resolves to the Account
1131 * after the name change. If successful the account's name will be the
1132 * specified new name.
1133 */
1134 public AccountManagerFuture<Account> renameAccount(
1135 final Account account,
Tor Norbye80b530a2015-04-23 16:36:09 -07001136 @Size(min = 1) final String newName,
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001137 AccountManagerCallback<Account> callback,
1138 Handler handler) {
1139 if (account == null) throw new IllegalArgumentException("account is null.");
1140 if (TextUtils.isEmpty(newName)) {
1141 throw new IllegalArgumentException("newName is empty or null.");
1142 }
1143 return new Future2Task<Account>(handler, callback) {
1144 @Override
1145 public void doWork() throws RemoteException {
1146 mService.renameAccount(mResponse, account, newName);
1147 }
1148 @Override
1149 public Account bundleToResult(Bundle bundle) throws AuthenticatorException {
1150 String name = bundle.getString(KEY_ACCOUNT_NAME);
1151 String type = bundle.getString(KEY_ACCOUNT_TYPE);
Svet Ganovc1c0d1c2016-09-23 19:15:47 -07001152 String accessId = bundle.getString(KEY_ACCOUNT_ACCESS_ID);
1153 return new Account(name, type, accessId);
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001154 }
1155 }.start();
1156 }
1157
1158 /**
1159 * Gets the previous name associated with the account or {@code null}, if
1160 * none. This is intended so that clients of {@link
1161 * #LOGIN_ACCOUNTS_CHANGED_ACTION} broadcasts can determine if an
1162 * authenticator has renamed an account.
1163 *
1164 * <p>It is safe to call this method from the main thread.
1165 *
1166 * @param account The account to query for a previous name.
1167 * @return The account's previous name, null if the account has never been
1168 * renamed.
1169 */
1170 public String getPreviousName(final Account account) {
1171 if (account == null) throw new IllegalArgumentException("account is null");
1172 try {
1173 return mService.getPreviousName(account);
1174 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001175 throw e.rethrowFromSystemServer();
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001176 }
1177 }
1178
1179 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001180 * Removes an account from the AccountManager. Does nothing if the account
1181 * does not exist. Does not delete the account from the server.
1182 * The authenticator may have its own policies preventing account
1183 * deletion, in which case the account will not be deleted.
1184 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001185 * <p>This method requires the caller to have a signature match with the
1186 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001187 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001188 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1189 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1190 * this function in API level 22.
1191 *
Dan Egnor661f0132010-02-19 11:23:00 -08001192 * @param account The {@link Account} to remove
1193 * @param callback Callback to invoke when the request completes,
1194 * null for no callback
1195 * @param handler {@link Handler} identifying the callback thread,
1196 * null for the main thread
1197 * @return An {@link AccountManagerFuture} which resolves to a Boolean,
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001198 * true if the account has been successfully removed
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001199 * @deprecated use
1200 * {@link #removeAccount(Account, Activity, AccountManagerCallback, Handler)}
1201 * instead
Dan Egnor661f0132010-02-19 11:23:00 -08001202 */
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001203 @Deprecated
Dan Egnor661f0132010-02-19 11:23:00 -08001204 public AccountManagerFuture<Boolean> removeAccount(final Account account,
1205 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001206 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001207 return new Future2Task<Boolean>(handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001208 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001209 public void doWork() throws RemoteException {
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001210 mService.removeAccount(mResponse, account, false);
Dan Egnor661f0132010-02-19 11:23:00 -08001211 }
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001212 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001213 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1214 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1215 throw new AuthenticatorException("no result in response");
1216 }
1217 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1218 }
1219 }.start();
1220 }
1221
1222 /**
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001223 * Removes an account from the AccountManager. Does nothing if the account
1224 * does not exist. Does not delete the account from the server.
1225 * The authenticator may have its own policies preventing account
1226 * deletion, in which case the account will not be deleted.
1227 *
1228 * <p>This method may be called from any thread, but the returned
1229 * {@link AccountManagerFuture} must not be used on the main thread.
1230 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001231 * <p>This method requires the caller to have a signature match with the
1232 * authenticator that manages the specified account.
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -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,
1235 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1236 * this function in API level 22.
1237 *
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001238 * @param account The {@link Account} to remove
1239 * @param activity The {@link Activity} context to use for launching a new
1240 * authenticator-defined sub-Activity to prompt the user to delete an
1241 * account; used only to call startActivity(); if null, the prompt
1242 * will not be launched directly, but the {@link Intent} may be
1243 * returned to the caller instead
1244 * @param callback Callback to invoke when the request completes,
1245 * null for no callback
1246 * @param handler {@link Handler} identifying the callback thread,
1247 * null for the main thread
1248 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1249 * {@link #KEY_BOOLEAN_RESULT} if activity was specified and an account
1250 * was removed or if active. If no activity was specified, the returned
1251 * Bundle contains only {@link #KEY_INTENT} with the {@link Intent}
1252 * needed to launch the actual account removal process, if authenticator
1253 * needs the activity launch. If an error occurred,
1254 * {@link AccountManagerFuture#getResult()} throws:
1255 * <ul>
1256 * <li> {@link AuthenticatorException} if no authenticator was registered for
1257 * this account type or the authenticator failed to respond
1258 * <li> {@link OperationCanceledException} if the operation was canceled for
1259 * any reason, including the user canceling the creation process or
1260 * adding accounts (of this type) has been disabled by policy
1261 * </ul>
1262 */
1263 public AccountManagerFuture<Bundle> removeAccount(final Account account,
1264 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
1265 if (account == null) throw new IllegalArgumentException("account is null");
1266 return new AmsTask(activity, handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001267 @Override
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001268 public void doWork() throws RemoteException {
1269 mService.removeAccount(mResponse, account, activity != null);
1270 }
1271 }.start();
1272 }
1273
1274 /**
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001275 * @see #removeAccount(Account, AccountManagerCallback, Handler)
1276 * @hide
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001277 * @deprecated use
1278 * {@link #removeAccountAsUser(Account, Activity, AccountManagerCallback, Handler)}
1279 * instead
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001280 */
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001281 @Deprecated
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001282 public AccountManagerFuture<Boolean> removeAccountAsUser(final Account account,
1283 AccountManagerCallback<Boolean> callback, Handler handler,
1284 final UserHandle userHandle) {
1285 if (account == null) throw new IllegalArgumentException("account is null");
1286 if (userHandle == null) throw new IllegalArgumentException("userHandle is null");
1287 return new Future2Task<Boolean>(handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001288 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001289 public void doWork() throws RemoteException {
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001290 mService.removeAccountAsUser(mResponse, account, false, userHandle.getIdentifier());
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001291 }
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001292 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001293 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1294 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1295 throw new AuthenticatorException("no result in response");
1296 }
1297 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1298 }
1299 }.start();
1300 }
1301
1302 /**
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001303 * @see #removeAccount(Account, Activity, AccountManagerCallback, Handler)
1304 * @hide
1305 */
1306 public AccountManagerFuture<Bundle> removeAccountAsUser(final Account account,
1307 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler,
1308 final UserHandle userHandle) {
1309 if (account == null)
1310 throw new IllegalArgumentException("account is null");
1311 if (userHandle == null)
1312 throw new IllegalArgumentException("userHandle is null");
1313 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001314 @Override
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001315 public void doWork() throws RemoteException {
1316 mService.removeAccountAsUser(mResponse, account, activity != null,
1317 userHandle.getIdentifier());
1318 }
1319 }.start();
1320 }
1321
1322 /**
1323 * Removes an account directly. Normally used by authenticators, not
1324 * directly by applications. Does not delete the account from the server.
1325 * The authenticator may have its own policies preventing account deletion,
1326 * in which case the account will not be deleted.
1327 * <p>
1328 * It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001329 * <p>This method requires the caller to have a signature match with the
1330 * authenticator that manages the specified account.
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001331 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001332 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1333 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1334 * is needed for those platforms. See docs for this function in API level 22.
1335 *
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001336 * @param account The {@link Account} to delete.
1337 * @return True if the account was successfully deleted, false if the
1338 * account did not exist, the account is null, or another error
1339 * occurs.
1340 */
1341 public boolean removeAccountExplicitly(Account account) {
1342 if (account == null) throw new IllegalArgumentException("account is null");
1343 try {
1344 return mService.removeAccountExplicitly(account);
1345 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001346 throw e.rethrowFromSystemServer();
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001347 }
1348 }
1349
1350 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001351 * Removes an auth token from the AccountManager's cache. Does nothing if
1352 * the auth token is not currently in the cache. Applications must call this
1353 * method when the auth token is found to have expired or otherwise become
1354 * invalid for authenticating requests. The AccountManager does not validate
1355 * or expire cached auth tokens otherwise.
1356 *
1357 * <p>It is safe to call this method from the main thread.
1358 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001359 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1360 * MANAGE_ACCOUNTS or USE_CREDENTIALS permission is needed for those
1361 * platforms. See docs for this function in API level 22.
1362 *
Fred Quintanaf35b68f2010-04-01 11:36:00 -07001363 * @param accountType The account type of the auth token to invalidate, must not be null
1364 * @param authToken The auth token to invalidate, may be null
Dan Egnor661f0132010-02-19 11:23:00 -08001365 */
1366 public void invalidateAuthToken(final String accountType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -07001367 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001368 try {
Fred Quintanaf35b68f2010-04-01 11:36:00 -07001369 if (authToken != null) {
1370 mService.invalidateAuthToken(accountType, authToken);
1371 }
Dan Egnor661f0132010-02-19 11:23:00 -08001372 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001373 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001374 }
1375 }
1376
1377 /**
1378 * Gets an auth token from the AccountManager's cache. If no auth
1379 * token is cached for this account, null will be returned -- a new
1380 * auth token will not be generated, and the server will not be contacted.
1381 * Intended for use by the authenticator, not directly by applications.
1382 *
1383 * <p>It is safe to call this method from the main thread.
1384 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001385 * <p>This method requires the caller to have a signature match with the
1386 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001387 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001388 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1389 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1390 * is needed for those platforms. See docs for this function in API level 22.
1391 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001392 * @param account The account for which an auth token is to be fetched. Cannot be {@code null}.
Sandra Kwan0b84b452016-01-20 15:25:42 -08001393 * @param authTokenType The type of auth token to fetch. Cannot be {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -08001394 * @return The cached auth token for this account and type, or null if
1395 * no auth token is cached or the account does not exist.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001396 * @see #getAuthToken
Dan Egnor661f0132010-02-19 11:23:00 -08001397 */
1398 public String peekAuthToken(final Account account, final String authTokenType) {
Fred Quintana382601f2010-03-25 12:25:10 -07001399 if (account == null) throw new IllegalArgumentException("account is null");
1400 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001401 try {
1402 return mService.peekAuthToken(account, authTokenType);
1403 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001404 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001405 }
1406 }
1407
1408 /**
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -07001409 * Sets or forgets a saved password. This modifies the local copy of the
1410 * password used to automatically authenticate the user; it does not change
1411 * the user's account password on the server. Intended for use by the
1412 * authenticator, not directly by applications.
1413 * <p>Calling this method does not update the last authenticated timestamp,
1414 * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
1415 * {@link #notifyAccountAuthenticated(Account)} after getting success.
Dan Egnor661f0132010-02-19 11:23:00 -08001416 * <p>It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001417 * <p>This method requires the caller to have a signature match with the
1418 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001419 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001420 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1421 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1422 * is needed for those platforms. See docs for this function in API level 22.
1423 *
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -07001424 * @param account The account whose password is to be set. Cannot be
1425 * {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -08001426 * @param password The password to set, null to clear the password
1427 */
1428 public void setPassword(final Account account, final String password) {
Fred Quintana382601f2010-03-25 12:25:10 -07001429 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001430 try {
1431 mService.setPassword(account, password);
1432 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001433 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001434 }
1435 }
1436
1437 /**
1438 * Forgets a saved password. This erases the local copy of the password;
1439 * it does not change the user's account password on the server.
1440 * Has the same effect as setPassword(account, null) but requires fewer
1441 * permissions, and may be used by applications or management interfaces
1442 * to "sign out" from an account.
1443 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001444 * <p>This method only successfully clear the account's password when the
1445 * caller has the same signature as the authenticator that owns the
1446 * specified account. Otherwise, this method will silently fail.
Dan Egnor661f0132010-02-19 11:23:00 -08001447 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001448 * <p>It is safe to call this method from the main thread.
Carlos Valdiviadcddc472015-06-11 20:04:04 +00001449 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001450 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1451 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1452 * this function in API level 22.
1453 *
Dan Egnor661f0132010-02-19 11:23:00 -08001454 * @param account The account whose password to clear
1455 */
1456 public void clearPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -07001457 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001458 try {
1459 mService.clearPassword(account);
1460 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001461 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001462 }
1463 }
1464
1465 /**
1466 * Sets one userdata key for an account. Intended by use for the
1467 * authenticator to stash state for itself, not directly by applications.
1468 * The meaning of the keys and values is up to the authenticator.
1469 *
1470 * <p>It is safe to call this method from the main thread.
1471 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001472 * <p>This method requires the caller to have a signature match with the
1473 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001474 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001475 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1476 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1477 * is needed for those platforms. See docs for this function in API level 22.
1478 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001479 * @param account Account whose user data is to be set. Must not be {@code null}.
1480 * @param key String user data key to set. Must not be null
1481 * @param value String value to set, {@code null} to clear this user data key
Dan Egnor661f0132010-02-19 11:23:00 -08001482 */
1483 public void setUserData(final Account account, final String key, final String value) {
Fred Quintana382601f2010-03-25 12:25:10 -07001484 if (account == null) throw new IllegalArgumentException("account is null");
1485 if (key == null) throw new IllegalArgumentException("key is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001486 try {
1487 mService.setUserData(account, key, value);
1488 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001489 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001490 }
1491 }
1492
1493 /**
1494 * Adds an auth token to the AccountManager cache for an account.
1495 * If the account does not exist then this call has no effect.
1496 * Replaces any previous auth token for this account and auth token type.
1497 * Intended for use by the authenticator, not directly by applications.
1498 *
1499 * <p>It is safe to call this method from the main thread.
1500 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001501 * <p>This method requires the caller to have a signature match with the
1502 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001503 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001504 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1505 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1506 * is needed for those platforms. See docs for this function in API level 22.
1507 *
Dan Egnor661f0132010-02-19 11:23:00 -08001508 * @param account The account to set an auth token for
1509 * @param authTokenType The type of the auth token, see {#getAuthToken}
1510 * @param authToken The auth token to add to the cache
1511 */
1512 public void setAuthToken(Account account, final String authTokenType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -07001513 if (account == null) throw new IllegalArgumentException("account is null");
1514 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001515 try {
1516 mService.setAuthToken(account, authTokenType, authToken);
1517 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001518 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001519 }
1520 }
1521
1522 /**
1523 * This convenience helper synchronously gets an auth token with
1524 * {@link #getAuthToken(Account, String, boolean, AccountManagerCallback, Handler)}.
1525 *
1526 * <p>This method may block while a network request completes, and must
1527 * never be made from the main thread.
1528 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001529 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1530 * USE_CREDENTIALS permission is needed for those platforms. See docs for
1531 * this function in API level 22.
1532 *
Dan Egnor661f0132010-02-19 11:23:00 -08001533 * @param account The account to fetch an auth token for
Joe Malinb6a35262013-06-03 09:49:04 -07001534 * @param authTokenType The auth token type, see {@link #getAuthToken getAuthToken()}
Dan Egnor661f0132010-02-19 11:23:00 -08001535 * @param notifyAuthFailure If true, display a notification and return null
1536 * if authentication fails; if false, prompt and wait for the user to
1537 * re-enter correct credentials before returning
1538 * @return An auth token of the specified type for this account, or null
1539 * if authentication fails or none can be fetched.
1540 * @throws AuthenticatorException if the authenticator failed to respond
1541 * @throws OperationCanceledException if the request was canceled for any
1542 * reason, including the user canceling a credential request
1543 * @throws java.io.IOException if the authenticator experienced an I/O problem
1544 * creating a new auth token, usually because of network trouble
1545 */
1546 public String blockingGetAuthToken(Account account, String authTokenType,
1547 boolean notifyAuthFailure)
1548 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana382601f2010-03-25 12:25:10 -07001549 if (account == null) throw new IllegalArgumentException("account is null");
1550 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001551 Bundle bundle = getAuthToken(account, authTokenType, notifyAuthFailure, null /* callback */,
1552 null /* handler */).getResult();
Fred Quintana96580e02010-03-04 13:42:42 -08001553 if (bundle == null) {
1554 // This should never happen, but it does, occasionally. If it does return null to
1555 // signify that we were not able to get the authtoken.
1556 // TODO: remove this when the bug is found that sometimes causes a null bundle to be
1557 // returned
1558 Log.e(TAG, "blockingGetAuthToken: null was returned from getResult() for "
1559 + account + ", authTokenType " + authTokenType);
1560 return null;
1561 }
Dan Egnor661f0132010-02-19 11:23:00 -08001562 return bundle.getString(KEY_AUTHTOKEN);
1563 }
1564
1565 /**
1566 * Gets an auth token of the specified type for a particular account,
1567 * prompting the user for credentials if necessary. This method is
1568 * intended for applications running in the foreground where it makes
1569 * sense to ask the user directly for a password.
1570 *
1571 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -07001572 * type, then it is returned. Otherwise, if a saved password is
1573 * available, it is sent to the server to generate a new auth token.
1574 * Otherwise, the user is prompted to enter a password.
Dan Egnor661f0132010-02-19 11:23:00 -08001575 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001576 * <p>Some authenticators have auth token <em>types</em>, whose value
1577 * is authenticator-dependent. Some services use different token types to
1578 * access different functionality -- for example, Google uses different auth
1579 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -08001580 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001581 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
Sandra Kwan0b84b452016-01-20 15:25:42 -08001582 * USE_CREDENTIALS permission is needed for those platforms. See docs for
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001583 * this function in API level 22.
1584 *
Dan Egnor661f0132010-02-19 11:23:00 -08001585 * <p>This method may be called from any thread, but the returned
1586 * {@link AccountManagerFuture} must not be used on the main thread.
1587 *
Dan Egnor661f0132010-02-19 11:23:00 -08001588 * @param account The account to fetch an auth token for
1589 * @param authTokenType The auth token type, an authenticator-dependent
1590 * string token, must not be null
1591 * @param options Authenticator-specific options for the request,
1592 * may be null or empty
1593 * @param activity The {@link Activity} context to use for launching a new
1594 * authenticator-defined sub-Activity to prompt the user for a password
1595 * if necessary; used only to call startActivity(); must not be null.
1596 * @param callback Callback to invoke when the request completes,
1597 * null for no callback
1598 * @param handler {@link Handler} identifying the callback thread,
1599 * null for the main thread
1600 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1601 * at least the following fields:
Fred Quintana756b7352009-10-21 13:43:10 -07001602 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001603 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1604 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1605 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
Fred Quintana756b7352009-10-21 13:43:10 -07001606 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001607 *
1608 * (Other authenticator-specific values may be returned.) If an auth token
1609 * could not be fetched, {@link AccountManagerFuture#getResult()} throws:
1610 * <ul>
1611 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1612 * <li> {@link OperationCanceledException} if the operation is canceled for
1613 * any reason, incluidng the user canceling a credential request
1614 * <li> {@link IOException} if the authenticator experienced an I/O problem
1615 * creating a new auth token, usually because of network trouble
1616 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -07001617 * If the account is no longer present on the device, the return value is
1618 * authenticator-dependent. The caller should verify the validity of the
1619 * account before requesting an auth token.
Dan Egnor661f0132010-02-19 11:23:00 -08001620 */
1621 public AccountManagerFuture<Bundle> getAuthToken(
1622 final Account account, final String authTokenType, final Bundle options,
1623 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001624 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001625 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001626 final Bundle optionsIn = new Bundle();
1627 if (options != null) {
1628 optionsIn.putAll(options);
1629 }
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001630 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Dan Egnor661f0132010-02-19 11:23:00 -08001631 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001632 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001633 public void doWork() throws RemoteException {
1634 mService.getAuthToken(mResponse, account, authTokenType,
1635 false /* notifyOnAuthFailure */, true /* expectActivityLaunch */,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001636 optionsIn);
Dan Egnor661f0132010-02-19 11:23:00 -08001637 }
1638 }.start();
1639 }
1640
1641 /**
1642 * Gets an auth token of the specified type for a particular account,
1643 * optionally raising a notification if the user must enter credentials.
1644 * This method is intended for background tasks and services where the
1645 * user should not be immediately interrupted with a password prompt.
1646 *
1647 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -07001648 * type, then it is returned. Otherwise, if a saved password is
1649 * available, it is sent to the server to generate a new auth token.
1650 * Otherwise, an {@link Intent} is returned which, when started, will
1651 * prompt the user for a password. If the notifyAuthFailure parameter is
1652 * set, a status bar notification is also created with the same Intent,
Dan Egnor661f0132010-02-19 11:23:00 -08001653 * alerting the user that they need to enter a password at some point.
1654 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001655 * <p>In that case, you may need to wait until the user responds, which
1656 * could take hours or days or forever. When the user does respond and
1657 * supply a new password, the account manager will broadcast the
1658 * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent, which applications can
1659 * use to try again.
Dan Egnor661f0132010-02-19 11:23:00 -08001660 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001661 * <p>If notifyAuthFailure is not set, it is the application's
1662 * responsibility to launch the returned Intent at some point.
1663 * Either way, the result from this call will not wait for user action.
Dan Egnor661f0132010-02-19 11:23:00 -08001664 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001665 * <p>Some authenticators have auth token <em>types</em>, whose value
1666 * is authenticator-dependent. Some services use different token types to
1667 * access different functionality -- for example, Google uses different auth
1668 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -08001669 *
1670 * <p>This method may be called from any thread, but the returned
1671 * {@link AccountManagerFuture} must not be used on the main thread.
1672 *
Dan Egnor661f0132010-02-19 11:23:00 -08001673 * @param account The account to fetch an auth token for
1674 * @param authTokenType The auth token type, an authenticator-dependent
1675 * string token, must not be null
Dan Egnor661f0132010-02-19 11:23:00 -08001676 * @param notifyAuthFailure True to add a notification to prompt the
1677 * user for a password if necessary, false to leave that to the caller
1678 * @param callback Callback to invoke when the request completes,
1679 * null for no callback
1680 * @param handler {@link Handler} identifying the callback thread,
1681 * null for the main thread
1682 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1683 * at least the following fields on success:
1684 * <ul>
1685 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1686 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1687 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
1688 * </ul>
1689 *
1690 * (Other authenticator-specific values may be returned.) If the user
1691 * must enter credentials, the returned Bundle contains only
1692 * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt.
1693 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001694 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001695 * <ul>
1696 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1697 * <li> {@link OperationCanceledException} if the operation is canceled for
1698 * any reason, incluidng the user canceling a credential request
1699 * <li> {@link IOException} if the authenticator experienced an I/O problem
1700 * creating a new auth token, usually because of network trouble
1701 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -07001702 * If the account is no longer present on the device, the return value is
1703 * authenticator-dependent. The caller should verify the validity of the
1704 * account before requesting an auth token.
Fred Quintanaad93a322011-09-08 13:21:01 -07001705 * @deprecated use {@link #getAuthToken(Account, String, android.os.Bundle,
1706 * boolean, AccountManagerCallback, android.os.Handler)} instead
Dan Egnor661f0132010-02-19 11:23:00 -08001707 */
Fred Quintanaad93a322011-09-08 13:21:01 -07001708 @Deprecated
Dan Egnor661f0132010-02-19 11:23:00 -08001709 public AccountManagerFuture<Bundle> getAuthToken(
Jatin Lodhia3df7d692013-03-27 10:57:23 -07001710 final Account account, final String authTokenType,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001711 final boolean notifyAuthFailure,
Dan Egnor661f0132010-02-19 11:23:00 -08001712 AccountManagerCallback<Bundle> callback, Handler handler) {
Jatin Lodhia3df7d692013-03-27 10:57:23 -07001713 return getAuthToken(account, authTokenType, null, notifyAuthFailure, callback,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001714 handler);
Dan Egnor661f0132010-02-19 11:23:00 -08001715 }
1716
1717 /**
Fred Quintanaad93a322011-09-08 13:21:01 -07001718 * Gets an auth token of the specified type for a particular account,
1719 * optionally raising a notification if the user must enter credentials.
1720 * This method is intended for background tasks and services where the
1721 * user should not be immediately interrupted with a password prompt.
1722 *
1723 * <p>If a previously generated auth token is cached for this account and
1724 * type, then it is returned. Otherwise, if a saved password is
1725 * available, it is sent to the server to generate a new auth token.
1726 * Otherwise, an {@link Intent} is returned which, when started, will
1727 * prompt the user for a password. If the notifyAuthFailure parameter is
1728 * set, a status bar notification is also created with the same Intent,
1729 * alerting the user that they need to enter a password at some point.
1730 *
1731 * <p>In that case, you may need to wait until the user responds, which
1732 * could take hours or days or forever. When the user does respond and
1733 * supply a new password, the account manager will broadcast the
1734 * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent, which applications can
1735 * use to try again.
1736 *
1737 * <p>If notifyAuthFailure is not set, it is the application's
1738 * responsibility to launch the returned Intent at some point.
1739 * Either way, the result from this call will not wait for user action.
1740 *
1741 * <p>Some authenticators have auth token <em>types</em>, whose value
1742 * is authenticator-dependent. Some services use different token types to
1743 * access different functionality -- for example, Google uses different auth
1744 * tokens to access Gmail and Google Calendar for the same account.
1745 *
1746 * <p>This method may be called from any thread, but the returned
1747 * {@link AccountManagerFuture} must not be used on the main thread.
1748 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001749 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
Sandra Kwan0b84b452016-01-20 15:25:42 -08001750 * USE_CREDENTIALS permission is needed for those platforms. See docs for
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001751 * this function in API level 22.
1752 *
Fred Quintanaad93a322011-09-08 13:21:01 -07001753 * @param account The account to fetch an auth token for
1754 * @param authTokenType The auth token type, an authenticator-dependent
1755 * string token, must not be null
1756 * @param options Authenticator-specific options for the request,
1757 * may be null or empty
1758 * @param notifyAuthFailure True to add a notification to prompt the
1759 * user for a password if necessary, false to leave that to the caller
1760 * @param callback Callback to invoke when the request completes,
1761 * null for no callback
1762 * @param handler {@link Handler} identifying the callback thread,
1763 * null for the main thread
1764 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1765 * at least the following fields on success:
1766 * <ul>
1767 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1768 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1769 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
1770 * </ul>
1771 *
1772 * (Other authenticator-specific values may be returned.) If the user
1773 * must enter credentials, the returned Bundle contains only
1774 * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt.
1775 *
1776 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
1777 * <ul>
1778 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1779 * <li> {@link OperationCanceledException} if the operation is canceled for
1780 * any reason, incluidng the user canceling a credential request
1781 * <li> {@link IOException} if the authenticator experienced an I/O problem
1782 * creating a new auth token, usually because of network trouble
1783 * </ul>
1784 * If the account is no longer present on the device, the return value is
1785 * authenticator-dependent. The caller should verify the validity of the
1786 * account before requesting an auth token.
1787 */
1788 public AccountManagerFuture<Bundle> getAuthToken(
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001789 final Account account, final String authTokenType, final Bundle options,
1790 final boolean notifyAuthFailure,
Fred Quintanaad93a322011-09-08 13:21:01 -07001791 AccountManagerCallback<Bundle> callback, Handler handler) {
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001792
Fred Quintanaad93a322011-09-08 13:21:01 -07001793 if (account == null) throw new IllegalArgumentException("account is null");
1794 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001795 final Bundle optionsIn = new Bundle();
1796 if (options != null) {
1797 optionsIn.putAll(options);
1798 }
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001799 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Fred Quintanaad93a322011-09-08 13:21:01 -07001800 return new AmsTask(null, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001801 @Override
Fred Quintanaad93a322011-09-08 13:21:01 -07001802 public void doWork() throws RemoteException {
1803 mService.getAuthToken(mResponse, account, authTokenType,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001804 notifyAuthFailure, false /* expectActivityLaunch */, optionsIn);
Fred Quintanaad93a322011-09-08 13:21:01 -07001805 }
1806 }.start();
1807 }
1808
1809 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001810 * Asks the user to add an account of a specified type. The authenticator
1811 * for this account type processes this request with the appropriate user
1812 * interface. If the user does elect to create a new account, the account
1813 * name is returned.
1814 *
1815 * <p>This method may be called from any thread, but the returned
1816 * {@link AccountManagerFuture} must not be used on the main thread.
Sandra Kwan0b84b452016-01-20 15:25:42 -08001817 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001818 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1819 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1820 * this function in API level 22.
Dan Egnor661f0132010-02-19 11:23:00 -08001821 *
Dan Egnor661f0132010-02-19 11:23:00 -08001822 * @param accountType The type of account to add; must not be null
1823 * @param authTokenType The type of auth token (see {@link #getAuthToken})
1824 * this account will need to be able to generate, null for none
1825 * @param requiredFeatures The features (see {@link #hasFeatures}) this
1826 * account must have, null for none
1827 * @param addAccountOptions Authenticator-specific options for the request,
1828 * may be null or empty
1829 * @param activity The {@link Activity} context to use for launching a new
1830 * authenticator-defined sub-Activity to prompt the user to create an
1831 * account; used only to call startActivity(); if null, the prompt
1832 * will not be launched directly, but the necessary {@link Intent}
1833 * will be returned to the caller instead
1834 * @param callback Callback to invoke when the request completes,
1835 * null for no callback
1836 * @param handler {@link Handler} identifying the callback thread,
1837 * null for the main thread
Doug Zongkerff592dc2010-02-23 12:26:33 -08001838 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
Dan Egnor661f0132010-02-19 11:23:00 -08001839 * these fields if activity was specified and an account was created:
1840 * <ul>
1841 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created
1842 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1843 * </ul>
1844 *
1845 * If no activity was specified, the returned Bundle contains only
1846 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Dan Egnor8e4378b2010-08-02 18:22:09 -07001847 * actual account creation process. If an error occurred,
1848 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001849 * <ul>
1850 * <li> {@link AuthenticatorException} if no authenticator was registered for
1851 * this account type or the authenticator failed to respond
1852 * <li> {@link OperationCanceledException} if the operation was canceled for
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001853 * any reason, including the user canceling the creation process or adding accounts
1854 * (of this type) has been disabled by policy
Dan Egnor661f0132010-02-19 11:23:00 -08001855 * <li> {@link IOException} if the authenticator experienced an I/O problem
1856 * creating a new account, usually because of network trouble
1857 * </ul>
1858 */
1859 public AccountManagerFuture<Bundle> addAccount(final String accountType,
1860 final String authTokenType, final String[] requiredFeatures,
1861 final Bundle addAccountOptions,
1862 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001863 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001864 final Bundle optionsIn = new Bundle();
1865 if (addAccountOptions != null) {
1866 optionsIn.putAll(addAccountOptions);
1867 }
1868 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001869
Dan Egnor661f0132010-02-19 11:23:00 -08001870 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001871 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001872 public void doWork() throws RemoteException {
Amith Yamasani27db4682013-03-30 17:07:47 -07001873 mService.addAccount(mResponse, accountType, authTokenType,
Costin Manolachee5847ad2011-09-14 12:52:19 -07001874 requiredFeatures, activity != null, optionsIn);
Dan Egnor661f0132010-02-19 11:23:00 -08001875 }
1876 }.start();
1877 }
1878
1879 /**
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001880 * @see #addAccount(String, String, String[], Bundle, Activity, AccountManagerCallback, Handler)
1881 * @hide
1882 */
1883 public AccountManagerFuture<Bundle> addAccountAsUser(final String accountType,
1884 final String authTokenType, final String[] requiredFeatures,
1885 final Bundle addAccountOptions, final Activity activity,
1886 AccountManagerCallback<Bundle> callback, Handler handler, final UserHandle userHandle) {
1887 if (accountType == null) throw new IllegalArgumentException("accountType is null");
1888 if (userHandle == null) throw new IllegalArgumentException("userHandle is null");
1889 final Bundle optionsIn = new Bundle();
1890 if (addAccountOptions != null) {
1891 optionsIn.putAll(addAccountOptions);
1892 }
1893 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
1894
1895 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001896 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001897 public void doWork() throws RemoteException {
1898 mService.addAccountAsUser(mResponse, accountType, authTokenType,
1899 requiredFeatures, activity != null, optionsIn, userHandle.getIdentifier());
1900 }
1901 }.start();
1902 }
1903
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001904
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001905 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001906 * Adds shared accounts from a parent user to a secondary user. Adding the shared account
Amith Yamasani67df64b2012-12-14 12:09:36 -08001907 * doesn't take effect immediately. When the target user starts up, any pending shared accounts
1908 * are attempted to be copied to the target user from the primary via calls to the
1909 * authenticator.
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001910 * @param parentUser parent user
1911 * @param user target user
Amith Yamasani67df64b2012-12-14 12:09:36 -08001912 * @hide
1913 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001914 public void addSharedAccountsFromParentUser(UserHandle parentUser, UserHandle user) {
Amith Yamasani67df64b2012-12-14 12:09:36 -08001915 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001916 mService.addSharedAccountsFromParentUser(parentUser.getIdentifier(),
1917 user.getIdentifier());
Amith Yamasani67df64b2012-12-14 12:09:36 -08001918 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001919 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001920 }
1921 }
1922
1923 /**
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001924 * Copies an account from one user to another user.
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001925 * @param account the account to copy
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001926 * @param fromUser the user to copy the account from
1927 * @param toUser the target user
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001928 * @param callback Callback to invoke when the request completes,
1929 * null for no callback
1930 * @param handler {@link Handler} identifying the callback thread,
1931 * null for the main thread
1932 * @return An {@link AccountManagerFuture} which resolves to a Boolean indicated wether it
1933 * succeeded.
1934 * @hide
1935 */
1936 public AccountManagerFuture<Boolean> copyAccountToUser(
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001937 final Account account, final UserHandle fromUser, final UserHandle toUser,
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001938 AccountManagerCallback<Boolean> callback, Handler handler) {
1939 if (account == null) throw new IllegalArgumentException("account is null");
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001940 if (toUser == null || fromUser == null) {
1941 throw new IllegalArgumentException("fromUser and toUser cannot be null");
1942 }
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001943
1944 return new Future2Task<Boolean>(handler, callback) {
1945 @Override
1946 public void doWork() throws RemoteException {
1947 mService.copyAccountToUser(
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001948 mResponse, account, fromUser.getIdentifier(), toUser.getIdentifier());
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001949 }
1950 @Override
1951 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1952 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1953 throw new AuthenticatorException("no result in response");
1954 }
1955 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1956 }
1957 }.start();
1958 }
1959
1960 /**
Amith Yamasani67df64b2012-12-14 12:09:36 -08001961 * @hide
1962 * Removes the shared account.
1963 * @param account the account to remove
1964 * @param user the user to remove the account from
1965 * @return
1966 */
1967 public boolean removeSharedAccount(final Account account, UserHandle user) {
1968 try {
1969 boolean val = mService.removeSharedAccountAsUser(account, user.getIdentifier());
1970 return val;
1971 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001972 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001973 }
1974 }
1975
1976 /**
1977 * @hide
1978 * @param user
1979 * @return
1980 */
1981 public Account[] getSharedAccounts(UserHandle user) {
1982 try {
1983 return mService.getSharedAccountsAsUser(user.getIdentifier());
1984 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001985 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001986 }
1987 }
1988
1989 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001990 * Confirms that the user knows the password for an account to make extra
1991 * sure they are the owner of the account. The user-entered password can
1992 * be supplied directly, otherwise the authenticator for this account type
1993 * prompts the user with the appropriate interface. This method is
1994 * intended for applications which want extra assurance; for example, the
1995 * phone lock screen uses this to let the user unlock the phone with an
1996 * account password if they forget the lock pattern.
1997 *
1998 * <p>If the user-entered password matches a saved password for this
1999 * account, the request is considered valid; otherwise the authenticator
2000 * verifies the password (usually by contacting the server).
2001 *
2002 * <p>This method may be called from any thread, but the returned
2003 * {@link AccountManagerFuture} must not be used on the main thread.
2004 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002005 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
2006 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs
2007 * for this function in API level 22.
2008 *
Dan Egnor661f0132010-02-19 11:23:00 -08002009 * @param account The account to confirm password knowledge for
2010 * @param options Authenticator-specific options for the request;
2011 * if the {@link #KEY_PASSWORD} string field is present, the
2012 * authenticator may use it directly rather than prompting the user;
2013 * may be null or empty
2014 * @param activity The {@link Activity} context to use for launching a new
2015 * authenticator-defined sub-Activity to prompt the user to enter a
2016 * password; used only to call startActivity(); if null, the prompt
2017 * will not be launched directly, but the necessary {@link Intent}
2018 * will be returned to the caller instead
2019 * @param callback Callback to invoke when the request completes,
2020 * null for no callback
2021 * @param handler {@link Handler} identifying the callback thread,
2022 * null for the main thread
2023 * @return An {@link AccountManagerFuture} which resolves to a Bundle
2024 * with these fields if activity or password was supplied and
2025 * the account was successfully verified:
2026 * <ul>
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08002027 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account verified
Dan Egnor661f0132010-02-19 11:23:00 -08002028 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
2029 * <li> {@link #KEY_BOOLEAN_RESULT} - true to indicate success
2030 * </ul>
2031 *
2032 * If no activity or password was specified, the returned Bundle contains
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08002033 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
2034 * password prompt.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07002035 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08002036 * <p>Also the returning Bundle may contain {@link
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -07002037 * #KEY_LAST_AUTHENTICATED_TIME} indicating the last time the
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08002038 * credential was validated/created.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07002039 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08002040 * If an error occurred,{@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08002041 * <ul>
2042 * <li> {@link AuthenticatorException} if the authenticator failed to respond
2043 * <li> {@link OperationCanceledException} if the operation was canceled for
2044 * any reason, including the user canceling the password prompt
2045 * <li> {@link IOException} if the authenticator experienced an I/O problem
2046 * verifying the password, usually because of network trouble
2047 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002048 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002049 public AccountManagerFuture<Bundle> confirmCredentials(final Account account,
2050 final Bundle options,
2051 final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002052 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07002053 final Handler handler) {
Amith Yamasani2c7bc262012-11-05 16:46:02 -08002054 return confirmCredentialsAsUser(account, options, activity, callback, handler,
2055 Process.myUserHandle());
2056 }
2057
2058 /**
2059 * @hide
2060 * Same as {@link #confirmCredentials(Account, Bundle, Activity, AccountManagerCallback, Handler)}
2061 * but for the specified user.
2062 */
2063 public AccountManagerFuture<Bundle> confirmCredentialsAsUser(final Account account,
2064 final Bundle options,
2065 final Activity activity,
2066 final AccountManagerCallback<Bundle> callback,
2067 final Handler handler, UserHandle userHandle) {
Fred Quintana382601f2010-03-25 12:25:10 -07002068 if (account == null) throw new IllegalArgumentException("account is null");
Amith Yamasani2c7bc262012-11-05 16:46:02 -08002069 final int userId = userHandle.getIdentifier();
Fred Quintanaa698f422009-04-08 19:14:54 -07002070 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002071 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002072 public void doWork() throws RemoteException {
Amith Yamasani2c7bc262012-11-05 16:46:02 -08002073 mService.confirmCredentialsAsUser(mResponse, account, options, activity != null,
2074 userId);
Fred Quintanaa698f422009-04-08 19:14:54 -07002075 }
Fred Quintana33269202009-04-20 16:05:10 -07002076 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07002077 }
2078
Fred Quintana756b7352009-10-21 13:43:10 -07002079 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002080 * Asks the user to enter a new password for an account, updating the
2081 * saved credentials for the account. Normally this happens automatically
2082 * when the server rejects credentials during an auth token fetch, but this
2083 * can be invoked directly to ensure we have the correct credentials stored.
Fred Quintana756b7352009-10-21 13:43:10 -07002084 *
Dan Egnor661f0132010-02-19 11:23:00 -08002085 * <p>This method may be called from any thread, but the returned
2086 * {@link AccountManagerFuture} must not be used on the main thread.
2087 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002088 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
2089 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
2090 * this function in API level 22.
2091 *
Dan Egnor661f0132010-02-19 11:23:00 -08002092 * @param account The account to update credentials for
2093 * @param authTokenType The credentials entered must allow an auth token
2094 * of this type to be created (but no actual auth token is returned);
2095 * may be null
2096 * @param options Authenticator-specific options for the request;
2097 * may be null or empty
2098 * @param activity The {@link Activity} context to use for launching a new
2099 * authenticator-defined sub-Activity to prompt the user to enter a
2100 * password; used only to call startActivity(); if null, the prompt
2101 * will not be launched directly, but the necessary {@link Intent}
2102 * will be returned to the caller instead
2103 * @param callback Callback to invoke when the request completes,
2104 * null for no callback
2105 * @param handler {@link Handler} identifying the callback thread,
2106 * null for the main thread
2107 * @return An {@link AccountManagerFuture} which resolves to a Bundle
2108 * with these fields if an activity was supplied and the account
2109 * credentials were successfully updated:
Fred Quintana756b7352009-10-21 13:43:10 -07002110 * <ul>
Ian Pedowitz6cc066d2015-08-05 14:23:43 +00002111 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account
Dan Egnor661f0132010-02-19 11:23:00 -08002112 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
Fred Quintana756b7352009-10-21 13:43:10 -07002113 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002114 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08002115 * If no activity was specified, the returned Bundle contains
Dan Egnor661f0132010-02-19 11:23:00 -08002116 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08002117 * password prompt. If an error occurred,
Dan Egnor8e4378b2010-08-02 18:22:09 -07002118 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08002119 * <ul>
2120 * <li> {@link AuthenticatorException} if the authenticator failed to respond
2121 * <li> {@link OperationCanceledException} if the operation was canceled for
2122 * any reason, including the user canceling the password prompt
2123 * <li> {@link IOException} if the authenticator experienced an I/O problem
2124 * verifying the password, usually because of network trouble
2125 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002126 */
2127 public AccountManagerFuture<Bundle> updateCredentials(final Account account,
2128 final String authTokenType,
Fred Quintana31957f12009-10-21 13:43:10 -07002129 final Bundle options, final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002130 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07002131 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07002132 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07002133 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002134 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002135 public void doWork() throws RemoteException {
2136 mService.updateCredentials(mResponse, account, authTokenType, activity != null,
Fred Quintana31957f12009-10-21 13:43:10 -07002137 options);
Fred Quintanaa698f422009-04-08 19:14:54 -07002138 }
Fred Quintana33269202009-04-20 16:05:10 -07002139 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07002140 }
2141
Fred Quintana756b7352009-10-21 13:43:10 -07002142 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002143 * Offers the user an opportunity to change an authenticator's settings.
2144 * These properties are for the authenticator in general, not a particular
2145 * account. Not all authenticators support this method.
Fred Quintana756b7352009-10-21 13:43:10 -07002146 *
Dan Egnor661f0132010-02-19 11:23:00 -08002147 * <p>This method may be called from any thread, but the returned
2148 * {@link AccountManagerFuture} must not be used on the main thread.
2149 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07002150 * <p>This method requires the caller to have the same signature as the
2151 * authenticator associated with the specified account type.
Dan Egnor661f0132010-02-19 11:23:00 -08002152 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002153 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
2154 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs
2155 * for this function in API level 22.
2156 *
Dan Egnor661f0132010-02-19 11:23:00 -08002157 * @param accountType The account type associated with the authenticator
2158 * to adjust
2159 * @param activity The {@link Activity} context to use for launching a new
2160 * authenticator-defined sub-Activity to adjust authenticator settings;
2161 * used only to call startActivity(); if null, the settings dialog will
2162 * not be launched directly, but the necessary {@link Intent} will be
2163 * returned to the caller instead
2164 * @param callback Callback to invoke when the request completes,
2165 * null for no callback
2166 * @param handler {@link Handler} identifying the callback thread,
2167 * null for the main thread
2168 * @return An {@link AccountManagerFuture} which resolves to a Bundle
2169 * which is empty if properties were edited successfully, or
2170 * if no activity was specified, contains only {@link #KEY_INTENT}
2171 * needed to launch the authenticator's settings dialog.
Dan Egnor8e4378b2010-08-02 18:22:09 -07002172 * If an error occurred, {@link AccountManagerFuture#getResult()}
2173 * throws:
Fred Quintana756b7352009-10-21 13:43:10 -07002174 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002175 * <li> {@link AuthenticatorException} if no authenticator was registered for
2176 * this account type or the authenticator failed to respond
2177 * <li> {@link OperationCanceledException} if the operation was canceled for
2178 * any reason, including the user canceling the settings dialog
2179 * <li> {@link IOException} if the authenticator experienced an I/O problem
2180 * updating settings, usually because of network trouble
Fred Quintana756b7352009-10-21 13:43:10 -07002181 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002182 */
2183 public AccountManagerFuture<Bundle> editProperties(final String accountType,
2184 final Activity activity, final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07002185 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07002186 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07002187 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002188 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002189 public void doWork() throws RemoteException {
2190 mService.editProperties(mResponse, accountType, activity != null);
2191 }
Fred Quintana33269202009-04-20 16:05:10 -07002192 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07002193 }
2194
Amith Yamasani12747872015-12-07 14:19:49 -08002195 /**
2196 * @hide
2197 * Checks if the given account exists on any of the users on the device.
2198 * Only the system process can call this method.
2199 *
2200 * @param account The account to check for existence.
2201 * @return whether any user has this account
2202 */
2203 public boolean someUserHasAccount(@NonNull final Account account) {
2204 try {
2205 return mService.someUserHasAccount(account);
2206 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002207 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002208 }
2209 }
2210
Fred Quintanaa698f422009-04-08 19:14:54 -07002211 private void ensureNotOnMainThread() {
2212 final Looper looper = Looper.myLooper();
2213 if (looper != null && looper == mContext.getMainLooper()) {
Fred Quintana53bd2522010-02-05 15:28:12 -08002214 final IllegalStateException exception = new IllegalStateException(
2215 "calling this from your main thread can lead to deadlock");
2216 Log.e(TAG, "calling this from your main thread can lead to deadlock and/or ANRs",
2217 exception);
Fred Quintana751fdc02010-02-09 14:13:18 -08002218 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.FROYO) {
2219 throw exception;
2220 }
Fred Quintana60307342009-03-24 22:48:12 -07002221 }
2222 }
2223
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002224 private void postToHandler(Handler handler, final AccountManagerCallback<Bundle> callback,
2225 final AccountManagerFuture<Bundle> future) {
Fred Quintanad9d2f112009-04-23 13:36:27 -07002226 handler = handler == null ? mMainHandler : handler;
2227 handler.post(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002228 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002229 public void run() {
2230 callback.run(future);
2231 }
2232 });
2233 }
Fred Quintana60307342009-03-24 22:48:12 -07002234
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002235 private void postToHandler(Handler handler, final OnAccountsUpdateListener listener,
Fred Quintanad9d2f112009-04-23 13:36:27 -07002236 final Account[] accounts) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002237 final Account[] accountsCopy = new Account[accounts.length];
2238 // send a copy to make sure that one doesn't
2239 // change what another sees
2240 System.arraycopy(accounts, 0, accountsCopy, 0, accountsCopy.length);
2241 handler = (handler == null) ? mMainHandler : handler;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002242 handler.post(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002243 @Override
Fred Quintanad9d2f112009-04-23 13:36:27 -07002244 public void run() {
Hiroaki Kuriyama8d3e1592015-10-09 16:30:03 +02002245 synchronized (mAccountsUpdatedListeners) {
2246 try {
2247 if (mAccountsUpdatedListeners.containsKey(listener)) {
2248 listener.onAccountsUpdated(accountsCopy);
2249 }
2250 } catch (SQLException e) {
2251 // Better luck next time. If the problem was disk-full,
2252 // the STORAGE_OK intent will re-trigger the update.
2253 Log.e(TAG, "Can't update accounts", e);
2254 }
Costin Manolacheb6437242009-09-10 16:14:12 -07002255 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002256 }
2257 });
2258 }
2259
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002260 private abstract class AmsTask extends FutureTask<Bundle> implements AccountManagerFuture<Bundle> {
Fred Quintanaa698f422009-04-08 19:14:54 -07002261 final IAccountManagerResponse mResponse;
2262 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002263 final AccountManagerCallback<Bundle> mCallback;
Fred Quintanaa698f422009-04-08 19:14:54 -07002264 final Activity mActivity;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002265 public AmsTask(Activity activity, Handler handler, AccountManagerCallback<Bundle> callback) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002266 super(new Callable<Bundle>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002267 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002268 public Bundle call() throws Exception {
2269 throw new IllegalStateException("this should never be called");
2270 }
2271 });
2272
2273 mHandler = handler;
2274 mCallback = callback;
2275 mActivity = activity;
2276 mResponse = new Response();
Fred Quintana33269202009-04-20 16:05:10 -07002277 }
2278
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002279 public final AccountManagerFuture<Bundle> start() {
2280 try {
2281 doWork();
2282 } catch (RemoteException e) {
2283 setException(e);
2284 }
Fred Quintana33269202009-04-20 16:05:10 -07002285 return this;
Fred Quintana60307342009-03-24 22:48:12 -07002286 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002287
Sandra Kwan0b84b452016-01-20 15:25:42 -08002288 @Override
Fred Quintana96580e02010-03-04 13:42:42 -08002289 protected void set(Bundle bundle) {
2290 // TODO: somehow a null is being set as the result of the Future. Log this
2291 // case to help debug where this is occurring. When this bug is fixed this
2292 // condition statement should be removed.
2293 if (bundle == null) {
2294 Log.e(TAG, "the bundle must not be null", new Exception());
2295 }
2296 super.set(bundle);
2297 }
2298
Fred Quintanaa698f422009-04-08 19:14:54 -07002299 public abstract void doWork() throws RemoteException;
2300
2301 private Bundle internalGetResult(Long timeout, TimeUnit unit)
2302 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08002303 if (!isDone()) {
2304 ensureNotOnMainThread();
2305 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002306 try {
2307 if (timeout == null) {
2308 return get();
2309 } else {
2310 return get(timeout, unit);
2311 }
2312 } catch (CancellationException e) {
2313 throw new OperationCanceledException();
2314 } catch (TimeoutException e) {
2315 // fall through and cancel
2316 } catch (InterruptedException e) {
2317 // fall through and cancel
2318 } catch (ExecutionException e) {
2319 final Throwable cause = e.getCause();
2320 if (cause instanceof IOException) {
2321 throw (IOException) cause;
2322 } else if (cause instanceof UnsupportedOperationException) {
2323 throw new AuthenticatorException(cause);
2324 } else if (cause instanceof AuthenticatorException) {
2325 throw (AuthenticatorException) cause;
2326 } else if (cause instanceof RuntimeException) {
2327 throw (RuntimeException) cause;
2328 } else if (cause instanceof Error) {
2329 throw (Error) cause;
2330 } else {
2331 throw new IllegalStateException(cause);
2332 }
2333 } finally {
2334 cancel(true /* interrupt if running */);
2335 }
2336 throw new OperationCanceledException();
2337 }
2338
Sandra Kwan0b84b452016-01-20 15:25:42 -08002339 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002340 public Bundle getResult()
2341 throws OperationCanceledException, IOException, AuthenticatorException {
2342 return internalGetResult(null, null);
2343 }
2344
Sandra Kwan0b84b452016-01-20 15:25:42 -08002345 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002346 public Bundle getResult(long timeout, TimeUnit unit)
2347 throws OperationCanceledException, IOException, AuthenticatorException {
2348 return internalGetResult(timeout, unit);
2349 }
2350
Sandra Kwan0b84b452016-01-20 15:25:42 -08002351 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002352 protected void done() {
2353 if (mCallback != null) {
2354 postToHandler(mHandler, mCallback, this);
2355 }
2356 }
2357
2358 /** Handles the responses from the AccountManager */
2359 private class Response extends IAccountManagerResponse.Stub {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002360 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002361 public void onResult(Bundle bundle) {
Brian Carlstrom46703b02011-04-06 15:41:29 -07002362 Intent intent = bundle.getParcelable(KEY_INTENT);
Fred Quintanaa698f422009-04-08 19:14:54 -07002363 if (intent != null && mActivity != null) {
2364 // since the user provided an Activity we will silently start intents
2365 // that we see
2366 mActivity.startActivity(intent);
2367 // leave the Future running to wait for the real response to this request
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07002368 } else if (bundle.getBoolean("retry")) {
2369 try {
2370 doWork();
2371 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002372 throw e.rethrowFromSystemServer();
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07002373 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002374 } else {
2375 set(bundle);
2376 }
2377 }
2378
Sandra Kwan0b84b452016-01-20 15:25:42 -08002379 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002380 public void onError(int code, String message) {
Alexandra Gherghina999d3942014-07-03 11:40:08 +01002381 if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED
2382 || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) {
2383 // the authenticator indicated that this request was canceled or we were
2384 // forbidden to fulfill; cancel now
Fred Quintanaa698f422009-04-08 19:14:54 -07002385 cancel(true /* mayInterruptIfRunning */);
2386 return;
2387 }
2388 setException(convertErrorToException(code, message));
2389 }
2390 }
2391
Fred Quintana60307342009-03-24 22:48:12 -07002392 }
2393
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002394 private abstract class BaseFutureTask<T> extends FutureTask<T> {
2395 final public IAccountManagerResponse mResponse;
Fred Quintanaa698f422009-04-08 19:14:54 -07002396 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002397
2398 public BaseFutureTask(Handler handler) {
2399 super(new Callable<T>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002400 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002401 public T call() throws Exception {
Fred Quintanaa698f422009-04-08 19:14:54 -07002402 throw new IllegalStateException("this should never be called");
2403 }
2404 });
Fred Quintanaa698f422009-04-08 19:14:54 -07002405 mHandler = handler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002406 mResponse = new Response();
Fred Quintana60307342009-03-24 22:48:12 -07002407 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002408
2409 public abstract void doWork() throws RemoteException;
2410
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002411 public abstract T bundleToResult(Bundle bundle) throws AuthenticatorException;
Fred Quintanaa698f422009-04-08 19:14:54 -07002412
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002413 protected void postRunnableToHandler(Runnable runnable) {
2414 Handler handler = (mHandler == null) ? mMainHandler : mHandler;
2415 handler.post(runnable);
Fred Quintanaa698f422009-04-08 19:14:54 -07002416 }
2417
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002418 protected void startTask() {
Fred Quintanaa698f422009-04-08 19:14:54 -07002419 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002420 doWork();
2421 } catch (RemoteException e) {
2422 setException(e);
Fred Quintanaa698f422009-04-08 19:14:54 -07002423 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002424 }
2425
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002426 protected class Response extends IAccountManagerResponse.Stub {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002427 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002428 public void onResult(Bundle bundle) {
2429 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002430 T result = bundleToResult(bundle);
2431 if (result == null) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002432 return;
2433 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002434 set(result);
2435 return;
Fred Quintanaa698f422009-04-08 19:14:54 -07002436 } catch (ClassCastException e) {
2437 // we will set the exception below
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002438 } catch (AuthenticatorException e) {
2439 // we will set the exception below
Fred Quintanaa698f422009-04-08 19:14:54 -07002440 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002441 onError(ERROR_CODE_INVALID_RESPONSE, "no result in response");
Fred Quintanaa698f422009-04-08 19:14:54 -07002442 }
2443
Sandra Kwan0b84b452016-01-20 15:25:42 -08002444 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002445 public void onError(int code, String message) {
Alexandra Gherghina999d3942014-07-03 11:40:08 +01002446 if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED
2447 || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) {
2448 // the authenticator indicated that this request was canceled or we were
2449 // forbidden to fulfill; cancel now
Fred Quintanaa698f422009-04-08 19:14:54 -07002450 cancel(true /* mayInterruptIfRunning */);
2451 return;
2452 }
2453 setException(convertErrorToException(code, message));
2454 }
2455 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002456 }
2457
2458 private abstract class Future2Task<T>
2459 extends BaseFutureTask<T> implements AccountManagerFuture<T> {
2460 final AccountManagerCallback<T> mCallback;
2461 public Future2Task(Handler handler, AccountManagerCallback<T> callback) {
2462 super(handler);
2463 mCallback = callback;
2464 }
2465
Sandra Kwan0b84b452016-01-20 15:25:42 -08002466 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002467 protected void done() {
2468 if (mCallback != null) {
2469 postRunnableToHandler(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002470 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002471 public void run() {
2472 mCallback.run(Future2Task.this);
2473 }
2474 });
2475 }
2476 }
2477
2478 public Future2Task<T> start() {
2479 startTask();
2480 return this;
2481 }
2482
2483 private T internalGetResult(Long timeout, TimeUnit unit)
2484 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08002485 if (!isDone()) {
2486 ensureNotOnMainThread();
2487 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002488 try {
2489 if (timeout == null) {
2490 return get();
2491 } else {
2492 return get(timeout, unit);
2493 }
2494 } catch (InterruptedException e) {
2495 // fall through and cancel
2496 } catch (TimeoutException e) {
2497 // fall through and cancel
2498 } catch (CancellationException e) {
2499 // fall through and cancel
2500 } catch (ExecutionException e) {
2501 final Throwable cause = e.getCause();
2502 if (cause instanceof IOException) {
2503 throw (IOException) cause;
2504 } else if (cause instanceof UnsupportedOperationException) {
2505 throw new AuthenticatorException(cause);
2506 } else if (cause instanceof AuthenticatorException) {
2507 throw (AuthenticatorException) cause;
2508 } else if (cause instanceof RuntimeException) {
2509 throw (RuntimeException) cause;
2510 } else if (cause instanceof Error) {
2511 throw (Error) cause;
2512 } else {
2513 throw new IllegalStateException(cause);
2514 }
2515 } finally {
2516 cancel(true /* interrupt if running */);
2517 }
2518 throw new OperationCanceledException();
2519 }
2520
Sandra Kwan0b84b452016-01-20 15:25:42 -08002521 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002522 public T getResult()
2523 throws OperationCanceledException, IOException, AuthenticatorException {
2524 return internalGetResult(null, null);
2525 }
2526
Sandra Kwan0b84b452016-01-20 15:25:42 -08002527 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002528 public T getResult(long timeout, TimeUnit unit)
2529 throws OperationCanceledException, IOException, AuthenticatorException {
2530 return internalGetResult(timeout, unit);
2531 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002532
Fred Quintana60307342009-03-24 22:48:12 -07002533 }
2534
Fred Quintanaa698f422009-04-08 19:14:54 -07002535 private Exception convertErrorToException(int code, String message) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002536 if (code == ERROR_CODE_NETWORK_ERROR) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002537 return new IOException(message);
Fred Quintana60307342009-03-24 22:48:12 -07002538 }
Fred Quintana60307342009-03-24 22:48:12 -07002539
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002540 if (code == ERROR_CODE_UNSUPPORTED_OPERATION) {
Fred Quintana33269202009-04-20 16:05:10 -07002541 return new UnsupportedOperationException(message);
Fred Quintana60307342009-03-24 22:48:12 -07002542 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002543
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002544 if (code == ERROR_CODE_INVALID_RESPONSE) {
Fred Quintana33269202009-04-20 16:05:10 -07002545 return new AuthenticatorException(message);
Fred Quintanaa698f422009-04-08 19:14:54 -07002546 }
2547
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002548 if (code == ERROR_CODE_BAD_ARGUMENTS) {
Fred Quintana33269202009-04-20 16:05:10 -07002549 return new IllegalArgumentException(message);
2550 }
2551
2552 return new AuthenticatorException(message);
2553 }
2554
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002555 private class GetAuthTokenByTypeAndFeaturesTask
2556 extends AmsTask implements AccountManagerCallback<Bundle> {
Fred Quintana33269202009-04-20 16:05:10 -07002557 GetAuthTokenByTypeAndFeaturesTask(final String accountType, final String authTokenType,
2558 final String[] features, Activity activityForPrompting,
2559 final Bundle addAccountOptions, final Bundle loginOptions,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002560 AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07002561 super(activityForPrompting, handler, callback);
2562 if (accountType == null) throw new IllegalArgumentException("account type is null");
2563 mAccountType = accountType;
2564 mAuthTokenType = authTokenType;
2565 mFeatures = features;
2566 mAddAccountOptions = addAccountOptions;
2567 mLoginOptions = loginOptions;
2568 mMyCallback = this;
2569 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002570 volatile AccountManagerFuture<Bundle> mFuture = null;
Fred Quintana33269202009-04-20 16:05:10 -07002571 final String mAccountType;
2572 final String mAuthTokenType;
2573 final String[] mFeatures;
2574 final Bundle mAddAccountOptions;
2575 final Bundle mLoginOptions;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002576 final AccountManagerCallback<Bundle> mMyCallback;
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002577 private volatile int mNumAccounts = 0;
Fred Quintana33269202009-04-20 16:05:10 -07002578
Sandra Kwan0b84b452016-01-20 15:25:42 -08002579 @Override
Fred Quintana33269202009-04-20 16:05:10 -07002580 public void doWork() throws RemoteException {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002581 getAccountsByTypeAndFeatures(mAccountType, mFeatures,
2582 new AccountManagerCallback<Account[]>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002583 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002584 public void run(AccountManagerFuture<Account[]> future) {
2585 Account[] accounts;
Fred Quintana33269202009-04-20 16:05:10 -07002586 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002587 accounts = future.getResult();
2588 } catch (OperationCanceledException e) {
2589 setException(e);
2590 return;
2591 } catch (IOException e) {
2592 setException(e);
2593 return;
2594 } catch (AuthenticatorException e) {
2595 setException(e);
2596 return;
Fred Quintana33269202009-04-20 16:05:10 -07002597 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002598
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002599 mNumAccounts = accounts.length;
2600
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002601 if (accounts.length == 0) {
2602 if (mActivity != null) {
2603 // no accounts, add one now. pretend that the user directly
2604 // made this request
2605 mFuture = addAccount(mAccountType, mAuthTokenType, mFeatures,
2606 mAddAccountOptions, mActivity, mMyCallback, mHandler);
2607 } else {
2608 // send result since we can't prompt to add an account
2609 Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002610 result.putString(KEY_ACCOUNT_NAME, null);
2611 result.putString(KEY_ACCOUNT_TYPE, null);
2612 result.putString(KEY_AUTHTOKEN, null);
Svet Ganovc1c0d1c2016-09-23 19:15:47 -07002613 result.putBinder(KEY_ACCOUNT_ACCESS_ID, null);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002614 try {
2615 mResponse.onResult(result);
2616 } catch (RemoteException e) {
2617 // this will never happen
2618 }
2619 // we are done
2620 }
2621 } else if (accounts.length == 1) {
2622 // have a single account, return an authtoken for it
2623 if (mActivity == null) {
2624 mFuture = getAuthToken(accounts[0], mAuthTokenType,
2625 false /* notifyAuthFailure */, mMyCallback, mHandler);
2626 } else {
2627 mFuture = getAuthToken(accounts[0],
2628 mAuthTokenType, mLoginOptions,
Fred Quintana33269202009-04-20 16:05:10 -07002629 mActivity, mMyCallback, mHandler);
2630 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002631 } else {
2632 if (mActivity != null) {
2633 IAccountManagerResponse chooseResponse =
2634 new IAccountManagerResponse.Stub() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002635 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002636 public void onResult(Bundle value) throws RemoteException {
2637 Account account = new Account(
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002638 value.getString(KEY_ACCOUNT_NAME),
Svet Ganovf6d424f12016-09-20 20:18:53 -07002639 value.getString(KEY_ACCOUNT_TYPE),
Svet Ganovc1c0d1c2016-09-23 19:15:47 -07002640 value.getString(KEY_ACCOUNT_ACCESS_ID));
Svet Ganovf6d424f12016-09-20 20:18:53 -07002641 mFuture = getAuthToken(account, mAuthTokenType,
2642 mLoginOptions, mActivity, mMyCallback,
2643 mHandler);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002644 }
Fred Quintana33269202009-04-20 16:05:10 -07002645
Sandra Kwan0b84b452016-01-20 15:25:42 -08002646 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002647 public void onError(int errorCode, String errorMessage)
2648 throws RemoteException {
2649 mResponse.onError(errorCode, errorMessage);
2650 }
2651 };
2652 // have many accounts, launch the chooser
2653 Intent intent = new Intent();
Dmitry Dementyev52745472016-12-02 10:27:45 -08002654 // TODO - this activity will not include
2655 // USER_MANAGED_NOT_VISIBLE
2656 // accounts. We need to move method to service
Amith Yamasani12b8e132013-03-14 10:48:07 -07002657 ComponentName componentName = ComponentName.unflattenFromString(
2658 Resources.getSystem().getString(
2659 R.string.config_chooseAccountActivity));
2660 intent.setClassName(componentName.getPackageName(),
2661 componentName.getClassName());
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002662 intent.putExtra(KEY_ACCOUNTS, accounts);
2663 intent.putExtra(KEY_ACCOUNT_MANAGER_RESPONSE,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002664 new AccountManagerResponse(chooseResponse));
2665 mActivity.startActivity(intent);
2666 // the result will arrive via the IAccountManagerResponse
2667 } else {
2668 // send result since we can't prompt to select an account
2669 Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002670 result.putString(KEY_ACCOUNTS, null);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002671 try {
2672 mResponse.onResult(result);
2673 } catch (RemoteException e) {
2674 // this will never happen
2675 }
2676 // we are done
Fred Quintana33269202009-04-20 16:05:10 -07002677 }
Fred Quintana33269202009-04-20 16:05:10 -07002678 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002679 }}, mHandler);
Fred Quintana33269202009-04-20 16:05:10 -07002680 }
2681
Sandra Kwan0b84b452016-01-20 15:25:42 -08002682 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002683 public void run(AccountManagerFuture<Bundle> future) {
Fred Quintana33269202009-04-20 16:05:10 -07002684 try {
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002685 final Bundle result = future.getResult();
2686 if (mNumAccounts == 0) {
2687 final String accountName = result.getString(KEY_ACCOUNT_NAME);
2688 final String accountType = result.getString(KEY_ACCOUNT_TYPE);
2689 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
2690 setException(new AuthenticatorException("account not in result"));
2691 return;
2692 }
Svet Ganovc1c0d1c2016-09-23 19:15:47 -07002693 final String accessId = result.getString(KEY_ACCOUNT_ACCESS_ID);
2694 final Account account = new Account(accountName, accountType, accessId);
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002695 mNumAccounts = 1;
2696 getAuthToken(account, mAuthTokenType, null /* options */, mActivity,
2697 mMyCallback, mHandler);
2698 return;
2699 }
2700 set(result);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002701 } catch (OperationCanceledException e) {
2702 cancel(true /* mayInterruptIfRUnning */);
2703 } catch (IOException e) {
2704 setException(e);
2705 } catch (AuthenticatorException e) {
2706 setException(e);
Fred Quintana33269202009-04-20 16:05:10 -07002707 }
2708 }
2709 }
2710
Fred Quintana756b7352009-10-21 13:43:10 -07002711 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -08002712 * This convenience helper combines the functionality of {@link #getAccountsByTypeAndFeatures},
2713 * {@link #getAuthToken}, and {@link #addAccount}.
Fred Quintana756b7352009-10-21 13:43:10 -07002714 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002715 * <p>
2716 * This method gets a list of the accounts matching specific type and feature set which are
2717 * visible to the caller or for which user can grant access (see {@link #getAccountsByType} for
2718 * details); if there is exactly one already visible account, it is used; if there are some
2719 * accounts for which user grant visibility, the user is prompted to pick one; if there are
2720 * none, the user is prompted to add one. Finally, an auth token is acquired for the chosen
2721 * account.
Dan Egnor661f0132010-02-19 11:23:00 -08002722 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002723 * <p>
2724 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
2725 * not be used on the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -08002726 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002727 * <p>
2728 * <b>NOTE:</b> If targeting your app to work on API level 22 and before, MANAGE_ACCOUNTS
2729 * permission is needed for those platforms. See docs for this function in API level 22.
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002730 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002731 * @param accountType The account type required (see {@link #getAccountsByType}), must not be
2732 * null
2733 * @param authTokenType The desired auth token type (see {@link #getAuthToken}), must not be
2734 * null
2735 * @param features Required features for the account (see
2736 * {@link #getAccountsByTypeAndFeatures}), may be null or empty
2737 * @param activity The {@link Activity} context to use for launching new sub-Activities to
2738 * prompt to add an account, select an account, and/or enter a password, as necessary;
2739 * used only to call startActivity(); should not be null
2740 * @param addAccountOptions Authenticator-specific options to use for adding new accounts; may
2741 * be null or empty
2742 * @param getAuthTokenOptions Authenticator-specific options to use for getting auth tokens; may
2743 * be null or empty
2744 * @param callback Callback to invoke when the request completes, null for no callback
2745 * @param handler {@link Handler} identifying the callback thread, null for the main thread
2746 * @return An {@link AccountManagerFuture} which resolves to a Bundle with at least the
2747 * following fields:
2748 * <ul>
2749 * <li>{@link #KEY_ACCOUNT_NAME} - the name of the account
2750 * <li>{@link #KEY_ACCOUNT_TYPE} - the type of the account
2751 * <li>{@link #KEY_AUTHTOKEN} - the auth token you wanted
2752 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002753 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002754 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
2755 * <ul>
2756 * <li>{@link AuthenticatorException} if no authenticator was registered for this
2757 * account type or the authenticator failed to respond
2758 * <li>{@link OperationCanceledException} if the operation was canceled for any reason,
2759 * including the user canceling any operation
2760 * <li>{@link IOException} if the authenticator experienced an I/O problem updating
2761 * settings, usually because of network trouble
2762 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002763 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002764 public AccountManagerFuture<Bundle> getAuthTokenByFeatures(
Fred Quintana33269202009-04-20 16:05:10 -07002765 final String accountType, final String authTokenType, final String[] features,
Dan Egnor661f0132010-02-19 11:23:00 -08002766 final Activity activity, final Bundle addAccountOptions,
Fred Quintana31957f12009-10-21 13:43:10 -07002767 final Bundle getAuthTokenOptions,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002768 final AccountManagerCallback<Bundle> callback, final Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07002769 if (accountType == null) throw new IllegalArgumentException("account type is null");
2770 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002771 final GetAuthTokenByTypeAndFeaturesTask task =
2772 new GetAuthTokenByTypeAndFeaturesTask(accountType, authTokenType, features,
Dan Egnor661f0132010-02-19 11:23:00 -08002773 activity, addAccountOptions, getAuthTokenOptions, callback, handler);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002774 task.start();
2775 return task;
Fred Quintana60307342009-03-24 22:48:12 -07002776 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002777
Fred Quintana1121bb52011-09-14 23:19:35 -07002778 /**
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002779 * Deprecated in favor of {@link #newChooseAccountIntent(Account, List, String[], String,
2780 * String, String[], Bundle)}.
2781 *
Fred Quintana1121bb52011-09-14 23:19:35 -07002782 * Returns an intent to an {@link Activity} that prompts the user to choose from a list of
2783 * accounts.
2784 * The caller will then typically start the activity by calling
Mark Fickettab249e02012-09-05 09:45:47 -04002785 * <code>startActivityForResult(intent, ...);</code>.
Fred Quintana1121bb52011-09-14 23:19:35 -07002786 * <p>
2787 * On success the activity returns a Bundle with the account name and type specified using
2788 * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}.
2789 * <p>
2790 * The most common case is to call this with one account type, e.g.:
2791 * <p>
kmccormickf783ce52013-03-29 14:31:54 -07002792 * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, false, null,
Fred Quintanad88324d2011-09-19 11:43:05 -07002793 * null, null, null);</pre>
Fred Quintana1121bb52011-09-14 23:19:35 -07002794 * @param selectedAccount if specified, indicates that the {@link Account} is the currently
2795 * selected one, according to the caller's definition of selected.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002796 * @param allowableAccounts an optional {@link List} of accounts that are allowed to be
Fred Quintana1121bb52011-09-14 23:19:35 -07002797 * shown. If not specified then this field will not limit the displayed accounts.
2798 * @param allowableAccountTypes an optional string array of account types. These are used
2799 * 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 -07002800 * when adding an account. If not specified then this field will not limit the displayed
2801 * account types when adding an account.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002802 * @param alwaysPromptForAccount boolean that is ignored.
Fred Quintanad88324d2011-09-19 11:43:05 -07002803 * @param descriptionOverrideText if non-null this string is used as the description in the
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002804 * accounts chooser screen rather than the default
Fred Quintanad88324d2011-09-19 11:43:05 -07002805 * @param addAccountAuthTokenType this string is passed as the {@link #addAccount}
2806 * authTokenType parameter
2807 * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount}
2808 * requiredFeatures parameter
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002809 * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options
Fred Quintanad88324d2011-09-19 11:43:05 -07002810 * parameter
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002811 * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow.
Fred Quintana1121bb52011-09-14 23:19:35 -07002812 */
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002813 @Deprecated
2814 static public Intent newChooseAccountIntent(
2815 Account selectedAccount,
Baligh Uddinf2d248d2015-07-10 03:01:47 +00002816 ArrayList<Account> allowableAccounts,
Fred Quintana1121bb52011-09-14 23:19:35 -07002817 String[] allowableAccountTypes,
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002818 boolean alwaysPromptForAccount,
2819 String descriptionOverrideText,
2820 String addAccountAuthTokenType,
2821 String[] addAccountRequiredFeatures,
Fred Quintana1121bb52011-09-14 23:19:35 -07002822 Bundle addAccountOptions) {
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002823 return newChooseAccountIntent(
2824 selectedAccount,
2825 allowableAccounts,
2826 allowableAccountTypes,
2827 descriptionOverrideText,
2828 addAccountAuthTokenType,
2829 addAccountRequiredFeatures,
2830 addAccountOptions);
2831 }
2832
2833 /**
2834 * Returns an intent to an {@link Activity} that prompts the user to choose from a list of
2835 * accounts.
2836 * The caller will then typically start the activity by calling
2837 * <code>startActivityForResult(intent, ...);</code>.
2838 * <p>
2839 * On success the activity returns a Bundle with the account name and type specified using
2840 * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}.
2841 * <p>
2842 * The most common case is to call this with one account type, e.g.:
2843 * <p>
2844 * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, null, null, null,
2845 * null);</pre>
2846 * @param selectedAccount if specified, indicates that the {@link Account} is the currently
2847 * selected one, according to the caller's definition of selected.
2848 * @param allowableAccounts an optional {@link List} of accounts that are allowed to be
2849 * shown. If not specified then this field will not limit the displayed accounts.
2850 * @param allowableAccountTypes an optional string array of account types. These are used
2851 * both to filter the shown accounts and to filter the list of account types that are shown
2852 * when adding an account. If not specified then this field will not limit the displayed
2853 * account types when adding an account.
2854 * @param descriptionOverrideText if non-null this string is used as the description in the
2855 * accounts chooser screen rather than the default
2856 * @param addAccountAuthTokenType this string is passed as the {@link #addAccount}
2857 * authTokenType parameter
2858 * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount}
2859 * requiredFeatures parameter
2860 * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options
2861 * parameter
2862 * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow.
2863 */
2864 static public Intent newChooseAccountIntent(
2865 Account selectedAccount,
2866 List<Account> allowableAccounts,
2867 String[] allowableAccountTypes,
2868 String descriptionOverrideText,
2869 String addAccountAuthTokenType,
2870 String[] addAccountRequiredFeatures,
2871 Bundle addAccountOptions) {
Fred Quintana1121bb52011-09-14 23:19:35 -07002872 Intent intent = new Intent();
Amith Yamasani12b8e132013-03-14 10:48:07 -07002873 ComponentName componentName = ComponentName.unflattenFromString(
2874 Resources.getSystem().getString(R.string.config_chooseTypeAndAccountActivity));
2875 intent.setClassName(componentName.getPackageName(),
2876 componentName.getClassName());
Fred Quintana1121bb52011-09-14 23:19:35 -07002877 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST,
Craig Lafayette3c9c71d2015-07-14 10:48:46 -04002878 allowableAccounts == null ? null : new ArrayList<Account>(allowableAccounts));
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002879 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
2880 allowableAccountTypes);
Fred Quintana1121bb52011-09-14 23:19:35 -07002881 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,
2882 addAccountOptions);
2883 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_SELECTED_ACCOUNT, selectedAccount);
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002884 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_DESCRIPTION_TEXT_OVERRIDE,
2885 descriptionOverrideText);
2886 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING,
2887 addAccountAuthTokenType);
2888 intent.putExtra(
2889 ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY,
2890 addAccountRequiredFeatures);
Fred Quintana1121bb52011-09-14 23:19:35 -07002891 return intent;
2892 }
2893
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002894 private final HashMap<OnAccountsUpdateListener, Handler> mAccountsUpdatedListeners =
Fred Quintanad9d2f112009-04-23 13:36:27 -07002895 Maps.newHashMap();
2896
Fred Quintanad9d2f112009-04-23 13:36:27 -07002897 /**
2898 * BroadcastReceiver that listens for the LOGIN_ACCOUNTS_CHANGED_ACTION intent
2899 * so that it can read the updated list of accounts and send them to the listener
2900 * in mAccountsUpdatedListeners.
2901 */
2902 private final BroadcastReceiver mAccountsChangedBroadcastReceiver = new BroadcastReceiver() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002903 @Override
Fred Quintanad9d2f112009-04-23 13:36:27 -07002904 public void onReceive(final Context context, final Intent intent) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002905 final Account[] accounts = getAccounts();
2906 // send the result to the listeners
2907 synchronized (mAccountsUpdatedListeners) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002908 for (Map.Entry<OnAccountsUpdateListener, Handler> entry :
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002909 mAccountsUpdatedListeners.entrySet()) {
2910 postToHandler(entry.getValue(), entry.getKey(), accounts);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002911 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002912 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002913 }
2914 };
2915
2916 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -08002917 * Adds an {@link OnAccountsUpdateListener} to this instance of the {@link AccountManager}. This
2918 * listener will be notified whenever user or AbstractAcccountAuthenticator made changes to
2919 * accounts related to the caller - either list of accounts returned by {@link #getAccounts()}
2920 * was changed, or new account was added for which user can grant access to the caller.
Dan Egnor661f0132010-02-19 11:23:00 -08002921 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002922 * <p>
2923 * As long as this listener is present, the AccountManager instance will not be
2924 * garbage-collected, and neither will the {@link Context} used to retrieve it, which may be a
2925 * large Activity instance. To avoid memory leaks, you must remove this listener before then.
2926 * Normally listeners are added in an Activity or Service's {@link Activity#onCreate} and
2927 * removed in {@link Activity#onDestroy}.
Dan Egnor661f0132010-02-19 11:23:00 -08002928 *
Dan Egnor661f0132010-02-19 11:23:00 -08002929 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002930 * If SUPPORTED_ACCOUNT_TYPES is specified in the manifest file, listener will only be
2931 * notified about whitelisted types.
2932 *
2933 * <p>
2934 * It is safe to call this method from the main thread.
Ian Pedowitz845d14d2015-08-04 07:47:37 -07002935 *
Dan Egnor661f0132010-02-19 11:23:00 -08002936 * @param listener The listener to send notifications to
Dmitry Dementyev52745472016-12-02 10:27:45 -08002937 * @param handler {@link Handler} identifying the thread to use for notifications, null for the
2938 * main thread
2939 * @param updateImmediately If true, the listener will be invoked (on the handler thread) right
2940 * away with the current account list
Fred Quintanad9d2f112009-04-23 13:36:27 -07002941 * @throws IllegalArgumentException if listener is null
2942 * @throws IllegalStateException if listener was already added
2943 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002944 public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener,
Fred Quintanad9d2f112009-04-23 13:36:27 -07002945 Handler handler, boolean updateImmediately) {
2946 if (listener == null) {
2947 throw new IllegalArgumentException("the listener is null");
2948 }
2949 synchronized (mAccountsUpdatedListeners) {
2950 if (mAccountsUpdatedListeners.containsKey(listener)) {
2951 throw new IllegalStateException("this listener is already added");
2952 }
2953 final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty();
2954
2955 mAccountsUpdatedListeners.put(listener, handler);
2956
Dmitry Dementyev52745472016-12-02 10:27:45 -08002957
Fred Quintanad9d2f112009-04-23 13:36:27 -07002958 if (wasEmpty) {
2959 // Register a broadcast receiver to monitor account changes
2960 IntentFilter intentFilter = new IntentFilter();
Dmitry Dementyev52745472016-12-02 10:27:45 -08002961 if (isVisibleAccountsChangedBroadcastSupported()) {
2962 intentFilter.addAction(ACTION_VISIBLE_ACCOUNTS_CHANGED);
2963 } else {
2964 intentFilter.addAction(LOGIN_ACCOUNTS_CHANGED_ACTION);
2965 }
Costin Manolacheb6437242009-09-10 16:14:12 -07002966 // To recover from disk-full.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002967 intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
Dmitry Dementyev52745472016-12-02 10:27:45 -08002968 // Register a broadcast receiver to monitor account changes
Fred Quintanad9d2f112009-04-23 13:36:27 -07002969 mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter);
2970 }
2971 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002972 if (updateImmediately) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002973 postToHandler(handler, listener, getAccounts());
Fred Quintanad9d2f112009-04-23 13:36:27 -07002974 }
2975 }
2976
2977 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -08002978 * @hide
2979 */
2980 private boolean isVisibleAccountsChangedBroadcastSupported() {
2981 String interestedTypes = null;
2982 try {
2983 String packageName = mContext.getOpPackageName();
2984 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(packageName,
2985 PackageManager.GET_META_DATA);
2986 Bundle b = ai.metaData;
2987 if (b == null) {
2988 return false;
2989 }
2990 interestedTypes = b.getString(SUPPORTED_ACCOUNT_TYPES);
2991 } catch (PackageManager.NameNotFoundException e) {
2992 return false;
2993 }
2994 return !TextUtils.isEmpty(interestedTypes);
2995 }
2996
2997 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002998 * Removes an {@link OnAccountsUpdateListener} previously registered with
2999 * {@link #addOnAccountsUpdatedListener}. The listener will no longer
3000 * receive notifications of account changes.
3001 *
3002 * <p>It is safe to call this method from the main thread.
3003 *
3004 * <p>No permission is required to call this method.
3005 *
3006 * @param listener The previously added listener to remove
Fred Quintanad9d2f112009-04-23 13:36:27 -07003007 * @throws IllegalArgumentException if listener is null
3008 * @throws IllegalStateException if listener was not already added
3009 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07003010 public void removeOnAccountsUpdatedListener(OnAccountsUpdateListener listener) {
Fred Quintana382601f2010-03-25 12:25:10 -07003011 if (listener == null) throw new IllegalArgumentException("listener is null");
Fred Quintanad9d2f112009-04-23 13:36:27 -07003012 synchronized (mAccountsUpdatedListeners) {
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01003013 if (!mAccountsUpdatedListeners.containsKey(listener)) {
Costin Manolache88a211b2009-10-29 11:30:11 -07003014 Log.e(TAG, "Listener was not previously added");
3015 return;
Fred Quintanad9d2f112009-04-23 13:36:27 -07003016 }
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01003017 mAccountsUpdatedListeners.remove(listener);
Fred Quintanad9d2f112009-04-23 13:36:27 -07003018 if (mAccountsUpdatedListeners.isEmpty()) {
3019 mContext.unregisterReceiver(mAccountsChangedBroadcastReceiver);
3020 }
3021 }
3022 }
Sandra Kwan78812282015-11-04 11:19:47 -08003023
3024 /**
3025 * Asks the user to authenticate with an account of a specified type. The
3026 * authenticator for this account type processes this request with the
3027 * appropriate user interface. If the user does elect to authenticate with a
3028 * new account, a bundle of session data for installing the account later is
3029 * returned with optional account password and account status token.
3030 * <p>
3031 * This method may be called from any thread, but the returned
3032 * {@link AccountManagerFuture} must not be used on the main thread.
3033 * <p>
3034 * <p>
3035 * <b>NOTE:</b> The account will not be installed to the device by calling
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003036 * this api alone. #finishSession should be called after this to install the
3037 * account on device.
Sandra Kwan78812282015-11-04 11:19:47 -08003038 *
3039 * @param accountType The type of account to add; must not be null
3040 * @param authTokenType The type of auth token (see {@link #getAuthToken})
3041 * this account will need to be able to generate, null for none
3042 * @param requiredFeatures The features (see {@link #hasFeatures}) this
3043 * account must have, null for none
3044 * @param options Authenticator-specific options for the request, may be
3045 * null or empty
3046 * @param activity The {@link Activity} context to use for launching a new
3047 * authenticator-defined sub-Activity to prompt the user to
3048 * create an account; used only to call startActivity(); if null,
3049 * the prompt will not be launched directly, but the necessary
3050 * {@link Intent} will be returned to the caller instead
3051 * @param callback Callback to invoke when the request completes, null for
3052 * no callback
3053 * @param handler {@link Handler} identifying the callback thread, null for
3054 * the main thread
3055 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
3056 * these fields if activity was specified and user was authenticated
3057 * with an account:
3058 * <ul>
3059 * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for
3060 * adding the the to the device later.
Sandra Kwan78812282015-11-04 11:19:47 -08003061 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
3062 * status of the account
3063 * </ul>
3064 * If no activity was specified, the returned Bundle contains only
3065 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
3066 * actual account creation process. If authenticator doesn't support
3067 * this method, the returned Bundle contains only
3068 * {@link #KEY_ACCOUNT_SESSION_BUNDLE} with encrypted
3069 * {@code options} needed to add account later. If an error
3070 * occurred, {@link AccountManagerFuture#getResult()} throws:
3071 * <ul>
3072 * <li>{@link AuthenticatorException} if no authenticator was
3073 * registered for this account type or the authenticator failed to
3074 * respond
3075 * <li>{@link OperationCanceledException} if the operation was
3076 * canceled for any reason, including the user canceling the
3077 * creation process or adding accounts (of this type) has been
3078 * disabled by policy
3079 * <li>{@link IOException} if the authenticator experienced an I/O
3080 * problem creating a new account, usually because of network
3081 * trouble
3082 * </ul>
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003083 * @see #finishSession
Sandra Kwan78812282015-11-04 11:19:47 -08003084 */
Sandra Kwane68c37e2015-11-12 17:11:49 -08003085 public AccountManagerFuture<Bundle> startAddAccountSession(
3086 final String accountType,
3087 final String authTokenType,
3088 final String[] requiredFeatures,
3089 final Bundle options,
3090 final Activity activity,
3091 AccountManagerCallback<Bundle> callback,
3092 Handler handler) {
Sandra Kwan78812282015-11-04 11:19:47 -08003093 if (accountType == null) throw new IllegalArgumentException("accountType is null");
3094 final Bundle optionsIn = new Bundle();
3095 if (options != null) {
3096 optionsIn.putAll(options);
3097 }
3098 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
3099
3100 return new AmsTask(activity, handler, callback) {
3101 @Override
3102 public void doWork() throws RemoteException {
Sandra Kwane68c37e2015-11-12 17:11:49 -08003103 mService.startAddAccountSession(
3104 mResponse,
3105 accountType,
3106 authTokenType,
3107 requiredFeatures,
3108 activity != null,
3109 optionsIn);
3110 }
3111 }.start();
3112 }
3113
3114 /**
3115 * Asks the user to enter a new password for an account but not updating the
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003116 * saved credentials for the account until {@link #finishSession} is called.
Sandra Kwane68c37e2015-11-12 17:11:49 -08003117 * <p>
3118 * This method may be called from any thread, but the returned
3119 * {@link AccountManagerFuture} must not be used on the main thread.
3120 * <p>
3121 * <b>NOTE:</b> The saved credentials for the account alone will not be
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003122 * updated by calling this API alone. #finishSession should be called after
3123 * this to update local credentials
Sandra Kwane68c37e2015-11-12 17:11:49 -08003124 *
3125 * @param account The account to update credentials for
3126 * @param authTokenType The credentials entered must allow an auth token of
3127 * this type to be created (but no actual auth token is
3128 * returned); may be null
3129 * @param options Authenticator-specific options for the request; may be
3130 * null or empty
3131 * @param activity The {@link Activity} context to use for launching a new
3132 * authenticator-defined sub-Activity to prompt the user to enter
3133 * a password; used only to call startActivity(); if null, the
3134 * prompt will not be launched directly, but the necessary
3135 * {@link Intent} will be returned to the caller instead
3136 * @param callback Callback to invoke when the request completes, null for
3137 * no callback
3138 * @param handler {@link Handler} identifying the callback thread, null for
3139 * the main thread
3140 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
3141 * these fields if an activity was supplied and user was
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003142 * successfully re-authenticated to the account:
Sandra Kwane68c37e2015-11-12 17:11:49 -08003143 * <ul>
3144 * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for
3145 * updating the local credentials on device later.
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003146 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
3147 * status of the account
Sandra Kwane68c37e2015-11-12 17:11:49 -08003148 * </ul>
3149 * If no activity was specified, the returned Bundle contains
3150 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
3151 * password prompt. If an error occurred,
3152 * {@link AccountManagerFuture#getResult()} throws:
3153 * <ul>
3154 * <li>{@link AuthenticatorException} if the authenticator failed to
3155 * respond
3156 * <li>{@link OperationCanceledException} if the operation was
3157 * canceled for any reason, including the user canceling the
3158 * password prompt
3159 * <li>{@link IOException} if the authenticator experienced an I/O
3160 * problem verifying the password, usually because of network
3161 * trouble
3162 * </ul>
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003163 * @see #finishSession
Sandra Kwane68c37e2015-11-12 17:11:49 -08003164 */
3165 public AccountManagerFuture<Bundle> startUpdateCredentialsSession(
3166 final Account account,
3167 final String authTokenType,
3168 final Bundle options,
3169 final Activity activity,
3170 final AccountManagerCallback<Bundle> callback,
3171 final Handler handler) {
3172 if (account == null) {
3173 throw new IllegalArgumentException("account is null");
3174 }
Carlos Valdivia51b651a2016-03-30 13:44:28 -07003175
3176 // Always include the calling package name. This just makes life easier
3177 // down stream.
3178 final Bundle optionsIn = new Bundle();
3179 if (options != null) {
3180 optionsIn.putAll(options);
3181 }
3182 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
3183
Sandra Kwane68c37e2015-11-12 17:11:49 -08003184 return new AmsTask(activity, handler, callback) {
3185 @Override
3186 public void doWork() throws RemoteException {
3187 mService.startUpdateCredentialsSession(
3188 mResponse,
3189 account,
3190 authTokenType,
3191 activity != null,
Carlos Valdivia51b651a2016-03-30 13:44:28 -07003192 optionsIn);
Sandra Kwan78812282015-11-04 11:19:47 -08003193 }
3194 }.start();
3195 }
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003196
3197 /**
3198 * Finishes the session started by {@link #startAddAccountSession} or
3199 * {@link #startUpdateCredentialsSession}. This will either add the account
3200 * to AccountManager or update the local credentials stored.
3201 * <p>
3202 * This method may be called from any thread, but the returned
3203 * {@link AccountManagerFuture} must not be used on the main thread.
3204 *
3205 * @param sessionBundle a {@link Bundle} created by {@link #startAddAccountSession} or
3206 * {@link #startUpdateCredentialsSession}
3207 * @param activity The {@link Activity} context to use for launching a new
3208 * authenticator-defined sub-Activity to prompt the user to
3209 * create an account or reauthenticate existing account; used
3210 * only to call startActivity(); if null, the prompt will not
3211 * be launched directly, but the necessary {@link Intent} will
3212 * be returned to the caller instead
3213 * @param callback Callback to invoke when the request completes, null for
3214 * no callback
3215 * @param handler {@link Handler} identifying the callback thread, null for
3216 * the main thread
3217 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
3218 * these fields if an activity was supplied and an account was added
3219 * to device or local credentials were updated::
3220 * <ul>
3221 * <li>{@link #KEY_ACCOUNT_NAME} - the name of the account created
3222 * <li>{@link #KEY_ACCOUNT_TYPE} - the type of the account
Hongming Jin8b442752016-06-26 10:36:21 -07003223 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
3224 * status of the account
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003225 * </ul>
3226 * If no activity was specified and additional information is needed
3227 * from user, the returned Bundle may contains only
3228 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
3229 * actual account creation process. If an error occurred,
3230 * {@link AccountManagerFuture#getResult()} throws:
3231 * <ul>
3232 * <li>{@link AuthenticatorException} if no authenticator was
3233 * registered for this account type or the authenticator failed to
3234 * respond
3235 * <li>{@link OperationCanceledException} if the operation was
3236 * canceled for any reason, including the user canceling the
3237 * creation process or adding accounts (of this type) has been
3238 * disabled by policy
3239 * <li>{@link IOException} if the authenticator experienced an I/O
3240 * problem creating a new account, usually because of network
3241 * trouble
3242 * </ul>
3243 * @see #startAddAccountSession and #startUpdateCredentialsSession
3244 */
3245 public AccountManagerFuture<Bundle> finishSession(
3246 final Bundle sessionBundle,
3247 final Activity activity,
3248 AccountManagerCallback<Bundle> callback,
3249 Handler handler) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003250 return finishSessionAsUser(
3251 sessionBundle,
3252 activity,
3253 Process.myUserHandle(),
3254 callback,
3255 handler);
3256 }
3257
3258 /**
3259 * @see #finishSession
3260 * @hide
3261 */
3262 @SystemApi
3263 public AccountManagerFuture<Bundle> finishSessionAsUser(
3264 final Bundle sessionBundle,
3265 final Activity activity,
3266 final UserHandle userHandle,
3267 AccountManagerCallback<Bundle> callback,
3268 Handler handler) {
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003269 if (sessionBundle == null) {
3270 throw new IllegalArgumentException("sessionBundle is null");
3271 }
3272
3273 /* Add information required by add account flow */
3274 final Bundle appInfo = new Bundle();
3275 appInfo.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
3276
3277 return new AmsTask(activity, handler, callback) {
3278 @Override
3279 public void doWork() throws RemoteException {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003280 mService.finishSessionAsUser(
3281 mResponse,
3282 sessionBundle,
3283 activity != null,
3284 appInfo,
3285 userHandle.getIdentifier());
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003286 }
3287 }.start();
3288 }
Sandra Kwan390c9d22016-01-12 14:13:37 -08003289
3290 /**
3291 * Checks whether {@link #updateCredentials} or {@link #startUpdateCredentialsSession} should be
3292 * called with respect to the specified account.
3293 * <p>
3294 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
3295 * not be used on the main thread.
3296 *
3297 * @param account The {@link Account} to be checked whether {@link #updateCredentials} or
3298 * {@link #startUpdateCredentialsSession} should be called
3299 * @param statusToken a String of token to check account staus
3300 * @param callback Callback to invoke when the request completes, null for no callback
3301 * @param handler {@link Handler} identifying the callback thread, null for the main thread
3302 * @return An {@link AccountManagerFuture} which resolves to a Boolean, true if the credentials
3303 * of the account should be updated.
Sandra Kwan390c9d22016-01-12 14:13:37 -08003304 */
Sandra Kwan390c9d22016-01-12 14:13:37 -08003305 public AccountManagerFuture<Boolean> isCredentialsUpdateSuggested(
3306 final Account account,
3307 final String statusToken,
3308 AccountManagerCallback<Boolean> callback,
3309 Handler handler) {
3310 if (account == null) {
3311 throw new IllegalArgumentException("account is null");
3312 }
3313
3314 if (TextUtils.isEmpty(statusToken)) {
3315 throw new IllegalArgumentException("status token is empty");
3316 }
3317
3318 return new Future2Task<Boolean>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003319 @Override
Sandra Kwan390c9d22016-01-12 14:13:37 -08003320 public void doWork() throws RemoteException {
3321 mService.isCredentialsUpdateSuggested(
3322 mResponse,
3323 account,
3324 statusToken);
3325 }
Sandra Kwan0b84b452016-01-20 15:25:42 -08003326 @Override
Sandra Kwan390c9d22016-01-12 14:13:37 -08003327 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
3328 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
3329 throw new AuthenticatorException("no result in response");
3330 }
3331 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
3332 }
3333 }.start();
3334 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003335
3336 /**
3337 * Gets whether a given package under a user has access to an account.
3338 * Can be called only from the system UID.
3339 *
3340 * @param account The account for which to check.
3341 * @param packageName The package for which to check.
3342 * @param userHandle The user for which to check.
3343 * @return True if the package can access the account.
3344 *
3345 * @hide
3346 */
3347 public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName,
3348 @NonNull UserHandle userHandle) {
3349 try {
3350 return mService.hasAccountAccess(account, packageName, userHandle);
3351 } catch (RemoteException e) {
3352 throw e.rethrowFromSystemServer();
3353 }
3354 }
3355
3356 /**
3357 * Creates an intent to request access to a given account for a UID.
3358 * The returned intent should be stated for a result where {@link
3359 * Activity#RESULT_OK} result means access was granted whereas {@link
3360 * Activity#RESULT_CANCELED} result means access wasn't granted. Can
3361 * be called only from the system UID.
3362 *
3363 * @param account The account for which to request.
3364 * @param packageName The package name which to request.
3365 * @param userHandle The user for which to request.
3366 * @return The intent to request account access or null if the package
3367 * doesn't exist.
3368 *
3369 * @hide
3370 */
3371 public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account,
3372 @NonNull String packageName, @NonNull UserHandle userHandle) {
3373 try {
3374 return mService.createRequestAccountAccessIntentSenderAsUser(account, packageName,
3375 userHandle);
3376 } catch (RemoteException e) {
3377 throw e.rethrowFromSystemServer();
3378 }
3379 }
Fred Quintana60307342009-03-24 22:48:12 -07003380}