blob: 2156425f8e51d95cf83df7af4be31512ba7c8d77 [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
Fred Quintana60307342009-03-24 22:48:12 -070019import android.app.Activity;
20import android.content.Intent;
21import android.content.Context;
Fred Quintanad9d2f112009-04-23 13:36:27 -070022import android.content.IntentFilter;
23import android.content.BroadcastReceiver;
Costin Manolacheb6437242009-09-10 16:14:12 -070024import android.database.SQLException;
Fred Quintanaa698f422009-04-08 19:14:54 -070025import android.os.Bundle;
26import android.os.Handler;
27import android.os.Looper;
28import android.os.RemoteException;
Fred Quintana33269202009-04-20 16:05:10 -070029import android.os.Parcelable;
Fred Quintana751fdc02010-02-09 14:13:18 -080030import android.os.Build;
Costin Manolacheb6437242009-09-10 16:14:12 -070031import android.util.Log;
Fred Quintanaf0fd8432010-03-08 12:48:05 -080032import android.text.TextUtils;
Fred Quintana60307342009-03-24 22:48:12 -070033
Fred Quintanaa698f422009-04-08 19:14:54 -070034import java.io.IOException;
35import java.util.concurrent.Callable;
36import java.util.concurrent.CancellationException;
37import java.util.concurrent.ExecutionException;
38import java.util.concurrent.FutureTask;
39import java.util.concurrent.TimeoutException;
40import java.util.concurrent.TimeUnit;
Fred Quintanad9d2f112009-04-23 13:36:27 -070041import java.util.HashMap;
42import java.util.Map;
43
44import com.google.android.collect.Maps;
Fred Quintana60307342009-03-24 22:48:12 -070045
46/**
Dan Egnor661f0132010-02-19 11:23:00 -080047 * This class provides access to a centralized registry of the user's
Dan Egnor8e4378b2010-08-02 18:22:09 -070048 * online accounts. The user enters credentials (username and password) once
49 * per account, granting applications access to online resources with
50 * "one-click" approval.
Fred Quintana60307342009-03-24 22:48:12 -070051 *
Dan Egnor661f0132010-02-19 11:23:00 -080052 * <p>Different online services have different ways of handling accounts and
53 * authentication, so the account manager uses pluggable <em>authenticator</em>
Dan Egnor8e4378b2010-08-02 18:22:09 -070054 * modules for different <em>account types</em>. Authenticators (which may be
55 * written by third parties) handle the actual details of validating account
56 * credentials and storing account information. For example, Google, Facebook,
57 * and Microsoft Exchange each have their own authenticator.
Dan Egnor661f0132010-02-19 11:23:00 -080058 *
59 * <p>Many servers support some notion of an <em>authentication token</em>,
60 * which can be used to authenticate a request to the server without sending
61 * the user's actual password. (Auth tokens are normally created with a
62 * separate request which does include the user's credentials.) AccountManager
Dan Egnor8e4378b2010-08-02 18:22:09 -070063 * can generate auth tokens for applications, so the application doesn't need to
64 * handle passwords directly. Auth tokens are normally reusable and cached by
65 * AccountManager, but must be refreshed periodically. It's the responsibility
66 * of applications to <em>invalidate</em> auth tokens when they stop working so
67 * the AccountManager knows it needs to regenerate them.
Dan Egnor661f0132010-02-19 11:23:00 -080068 *
69 * <p>Applications accessing a server normally go through these steps:
70 *
71 * <ul>
72 * <li>Get an instance of AccountManager using {@link #get(Context)}.
73 *
74 * <li>List the available accounts using {@link #getAccountsByType} or
75 * {@link #getAccountsByTypeAndFeatures}. Normally applications will only
76 * be interested in accounts with one particular <em>type</em>, which
77 * identifies the authenticator. Account <em>features</em> are used to
78 * identify particular account subtypes and capabilities. Both the account
79 * type and features are authenticator-specific strings, and must be known by
80 * the application in coordination with its preferred authenticators.
81 *
82 * <li>Select one or more of the available accounts, possibly by asking the
83 * user for their preference. If no suitable accounts are available,
84 * {@link #addAccount} may be called to prompt the user to create an
85 * account of the appropriate type.
86 *
Dan Egnor8e4378b2010-08-02 18:22:09 -070087 * <li><b>Important:</b> If the application is using a previously remembered
88 * account selection, it must make sure the account is still in the list
89 * of accounts returned by {@link #getAccountsByType}. Requesting an auth token
90 * for an account no longer on the device results in an undefined failure.
91 *
Dan Egnor661f0132010-02-19 11:23:00 -080092 * <li>Request an auth token for the selected account(s) using one of the
93 * {@link #getAuthToken} methods or related helpers. Refer to the description
94 * of each method for exact usage and error handling details.
95 *
96 * <li>Make the request using the auth token. The form of the auth token,
97 * the format of the request, and the protocol used are all specific to the
Dan Egnor8e4378b2010-08-02 18:22:09 -070098 * service you are accessing. The application may use whatever network and
99 * protocol libraries are useful.
Dan Egnor661f0132010-02-19 11:23:00 -0800100 *
101 * <li><b>Important:</b> If the request fails with an authentication error,
102 * it could be that a cached auth token is stale and no longer honored by
103 * the server. The application must call {@link #invalidateAuthToken} to remove
104 * the token from the cache, otherwise requests will continue failing! After
105 * invalidating the auth token, immediately go back to the "Request an auth
106 * token" step above. If the process fails the second time, then it can be
107 * treated as a "genuine" authentication failure and the user notified or other
108 * appropriate actions taken.
109 * </ul>
110 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700111 * <p>Some AccountManager methods may need to interact with the user to
Dan Egnor661f0132010-02-19 11:23:00 -0800112 * prompt for credentials, present options, or ask the user to add an account.
113 * The caller may choose whether to allow AccountManager to directly launch the
114 * necessary user interface and wait for the user, or to return an Intent which
115 * the caller may use to launch the interface, or (in some cases) to install a
116 * notification which the user can select at any time to launch the interface.
117 * To have AccountManager launch the interface directly, the caller must supply
118 * the current foreground {@link Activity} context.
119 *
120 * <p>Many AccountManager methods take {@link AccountManagerCallback} and
Dan Egnor8e4378b2010-08-02 18:22:09 -0700121 * {@link Handler} as parameters. These methods return immediately and
Dan Egnor661f0132010-02-19 11:23:00 -0800122 * run asynchronously. If a callback is provided then
123 * {@link AccountManagerCallback#run} will be invoked on the Handler's
124 * thread when the request completes, successfully or not.
Dan Egnor8e4378b2010-08-02 18:22:09 -0700125 * The result is retrieved by calling {@link AccountManagerFuture#getResult()}
126 * on the {@link AccountManagerFuture} returned by the method (and also passed
127 * to the callback). This method waits for the operation to complete (if
128 * necessary) and either returns the result or throws an exception if an error
129 * occurred during the operation. To make the request synchronously, call
Dan Egnor661f0132010-02-19 11:23:00 -0800130 * {@link AccountManagerFuture#getResult()} immediately on receiving the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700131 * future from the method; no callback need be supplied.
Dan Egnor661f0132010-02-19 11:23:00 -0800132 *
133 * <p>Requests which may block, including
134 * {@link AccountManagerFuture#getResult()}, must never be called on
135 * the application's main event thread. These operations throw
136 * {@link IllegalStateException} if they are used on the main thread.
Fred Quintana60307342009-03-24 22:48:12 -0700137 */
138public class AccountManager {
139 private static final String TAG = "AccountManager";
140
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700141 public static final int ERROR_CODE_REMOTE_EXCEPTION = 1;
142 public static final int ERROR_CODE_NETWORK_ERROR = 3;
143 public static final int ERROR_CODE_CANCELED = 4;
144 public static final int ERROR_CODE_INVALID_RESPONSE = 5;
145 public static final int ERROR_CODE_UNSUPPORTED_OPERATION = 6;
146 public static final int ERROR_CODE_BAD_ARGUMENTS = 7;
147 public static final int ERROR_CODE_BAD_REQUEST = 8;
Fred Quintana756b7352009-10-21 13:43:10 -0700148
Dan Egnor661f0132010-02-19 11:23:00 -0800149 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700150 * Bundle key used for the {@link String} account name in results
Dan Egnor661f0132010-02-19 11:23:00 -0800151 * from methods which return information about a particular account.
152 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700153 public static final String KEY_ACCOUNT_NAME = "authAccount";
Dan Egnor661f0132010-02-19 11:23:00 -0800154
155 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700156 * Bundle key used for the {@link String} account type in results
Dan Egnor661f0132010-02-19 11:23:00 -0800157 * from methods which return information about a particular account.
158 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700159 public static final String KEY_ACCOUNT_TYPE = "accountType";
Dan Egnor661f0132010-02-19 11:23:00 -0800160
161 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700162 * Bundle key used for the auth token value in results
Dan Egnor661f0132010-02-19 11:23:00 -0800163 * from {@link #getAuthToken} and friends.
164 */
165 public static final String KEY_AUTHTOKEN = "authtoken";
166
167 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700168 * Bundle key used for an {@link Intent} in results from methods that
Dan Egnor661f0132010-02-19 11:23:00 -0800169 * may require the caller to interact with the user. The Intent can
170 * be used to start the corresponding user interface activity.
171 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700172 public static final String KEY_INTENT = "intent";
Dan Egnor661f0132010-02-19 11:23:00 -0800173
174 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700175 * Bundle key used to supply the password directly in options to
Dan Egnor661f0132010-02-19 11:23:00 -0800176 * {@link #confirmCredentials}, rather than prompting the user with
177 * the standard password prompt.
178 */
179 public static final String KEY_PASSWORD = "password";
180
181 public static final String KEY_ACCOUNTS = "accounts";
Brian Carlstrom46703b02011-04-06 15:41:29 -0700182
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700183 public static final String KEY_ACCOUNT_AUTHENTICATOR_RESPONSE = "accountAuthenticatorResponse";
184 public static final String KEY_ACCOUNT_MANAGER_RESPONSE = "accountManagerResponse";
Dan Egnor661f0132010-02-19 11:23:00 -0800185 public static final String KEY_AUTHENTICATOR_TYPES = "authenticator_types";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700186 public static final String KEY_AUTH_FAILED_MESSAGE = "authFailedMessage";
187 public static final String KEY_AUTH_TOKEN_LABEL = "authTokenLabelKey";
Dan Egnor661f0132010-02-19 11:23:00 -0800188 public static final String KEY_BOOLEAN_RESULT = "booleanResult";
189 public static final String KEY_ERROR_CODE = "errorCode";
190 public static final String KEY_ERROR_MESSAGE = "errorMessage";
191 public static final String KEY_USERDATA = "userdata";
Costin Manolachea40c6302010-12-13 14:50:45 -0800192 /**
193 * Authenticators using 'customTokens' option will also get the UID of the
194 * caller
195 */
196 public static final String KEY_CALLER_UID = "callerUid";
197 public static final String KEY_CALLER_PID = "callerPid";
Dan Egnor661f0132010-02-19 11:23:00 -0800198
Costin Manolached6060452011-01-24 16:11:36 -0800199 /**
200 * Boolean, if set and 'customTokens' the authenticator is responsible for
201 * notifications.
202 * @hide
203 */
204 public static final String KEY_NOTIFY_ON_FAILURE = "notifyOnAuthFailure";
205
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700206 public static final String ACTION_AUTHENTICATOR_INTENT =
207 "android.accounts.AccountAuthenticator";
208 public static final String AUTHENTICATOR_META_DATA_NAME =
Dan Egnor661f0132010-02-19 11:23:00 -0800209 "android.accounts.AccountAuthenticator";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700210 public static final String AUTHENTICATOR_ATTRIBUTES_NAME = "account-authenticator";
211
Fred Quintana60307342009-03-24 22:48:12 -0700212 private final Context mContext;
213 private final IAccountManager mService;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700214 private final Handler mMainHandler;
Dan Egnor661f0132010-02-19 11:23:00 -0800215
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700216 /**
217 * Action sent as a broadcast Intent by the AccountsService
Dan Egnor661f0132010-02-19 11:23:00 -0800218 * when accounts are added, accounts are removed, or an
219 * account's credentials (saved password, etc) are changed.
220 *
221 * @see #addOnAccountsUpdatedListener
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700222 */
223 public static final String LOGIN_ACCOUNTS_CHANGED_ACTION =
224 "android.accounts.LOGIN_ACCOUNTS_CHANGED";
Fred Quintana60307342009-03-24 22:48:12 -0700225
Fred Quintana33269202009-04-20 16:05:10 -0700226 /**
227 * @hide
228 */
Fred Quintana60307342009-03-24 22:48:12 -0700229 public AccountManager(Context context, IAccountManager service) {
230 mContext = context;
231 mService = service;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700232 mMainHandler = new Handler(mContext.getMainLooper());
Fred Quintana60307342009-03-24 22:48:12 -0700233 }
234
Fred Quintana0eabf022009-04-27 15:08:17 -0700235 /**
236 * @hide used for testing only
237 */
238 public AccountManager(Context context, IAccountManager service, Handler handler) {
239 mContext = context;
240 mService = service;
241 mMainHandler = handler;
242 }
243
Fred Quintana756b7352009-10-21 13:43:10 -0700244 /**
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800245 * @hide for internal use only
246 */
247 public static Bundle sanitizeResult(Bundle result) {
Fred Quintana382601f2010-03-25 12:25:10 -0700248 if (result != null) {
249 if (result.containsKey(KEY_AUTHTOKEN)
250 && !TextUtils.isEmpty(result.getString(KEY_AUTHTOKEN))) {
251 final Bundle newResult = new Bundle(result);
252 newResult.putString(KEY_AUTHTOKEN, "<omitted for logging purposes>");
253 return newResult;
254 }
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800255 }
256 return result;
257 }
258
259 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800260 * Gets an AccountManager instance associated with a Context.
261 * The {@link Context} will be used as long as the AccountManager is
262 * active, so make sure to use a {@link Context} whose lifetime is
263 * commensurate with any listeners registered to
264 * {@link #addOnAccountsUpdatedListener} or similar methods.
265 *
266 * <p>It is safe to call this method from the main thread.
267 *
268 * <p>No permission is required to call this method.
269 *
Fred Quintana756b7352009-10-21 13:43:10 -0700270 * @param context The {@link Context} to use when necessary
Dan Egnor661f0132010-02-19 11:23:00 -0800271 * @return An {@link AccountManager} instance
Fred Quintana756b7352009-10-21 13:43:10 -0700272 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700273 public static AccountManager get(Context context) {
Fred Quintana382601f2010-03-25 12:25:10 -0700274 if (context == null) throw new IllegalArgumentException("context is null");
Fred Quintanaa698f422009-04-08 19:14:54 -0700275 return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
276 }
277
Fred Quintana756b7352009-10-21 13:43:10 -0700278 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800279 * Gets the saved password associated with the account.
280 * This is intended for authenticators and related code; applications
281 * should get an auth token instead.
282 *
283 * <p>It is safe to call this method from the main thread.
284 *
285 * <p>This method requires the caller to hold the permission
286 * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS}
287 * and to have the same UID as the account's authenticator.
288 *
289 * @param account The account to query for a password
290 * @return The account's password, null if none or if the account doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700291 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700292 public String getPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -0700293 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintana60307342009-03-24 22:48:12 -0700294 try {
295 return mService.getPassword(account);
296 } catch (RemoteException e) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700297 // will never happen
Fred Quintana60307342009-03-24 22:48:12 -0700298 throw new RuntimeException(e);
299 }
300 }
301
Fred Quintana756b7352009-10-21 13:43:10 -0700302 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800303 * Gets the user data named by "key" associated with the account.
304 * This is intended for authenticators and related code to store
305 * arbitrary metadata along with accounts. The meaning of the keys
306 * and values is up to the authenticator for the account.
307 *
308 * <p>It is safe to call this method from the main thread.
309 *
310 * <p>This method requires the caller to hold the permission
311 * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS}
312 * and to have the same UID as the account's authenticator.
313 *
314 * @param account The account to query for user data
315 * @return The user data, null if the account or key doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700316 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700317 public String getUserData(final Account account, final String key) {
Fred Quintana382601f2010-03-25 12:25:10 -0700318 if (account == null) throw new IllegalArgumentException("account is null");
319 if (key == null) throw new IllegalArgumentException("key is null");
Fred Quintana60307342009-03-24 22:48:12 -0700320 try {
321 return mService.getUserData(account, key);
322 } catch (RemoteException e) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700323 // will never happen
Fred Quintana60307342009-03-24 22:48:12 -0700324 throw new RuntimeException(e);
325 }
326 }
327
Fred Quintana756b7352009-10-21 13:43:10 -0700328 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800329 * Lists the currently registered authenticators.
330 *
331 * <p>It is safe to call this method from the main thread.
332 *
333 * <p>No permission is required to call this method.
334 *
335 * @return An array of {@link AuthenticatorDescription} for every
336 * authenticator known to the AccountManager service. Empty (never
337 * null) if no authenticators are known.
Fred Quintana756b7352009-10-21 13:43:10 -0700338 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700339 public AuthenticatorDescription[] getAuthenticatorTypes() {
Fred Quintanaa698f422009-04-08 19:14:54 -0700340 try {
341 return mService.getAuthenticatorTypes();
342 } catch (RemoteException e) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700343 // will never happen
Fred Quintanaa698f422009-04-08 19:14:54 -0700344 throw new RuntimeException(e);
345 }
346 }
347
Fred Quintana756b7352009-10-21 13:43:10 -0700348 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800349 * Lists all accounts of any type registered on the device.
350 * Equivalent to getAccountsByType(null).
351 *
352 * <p>It is safe to call this method from the main thread.
353 *
354 * <p>This method requires the caller to hold the permission
355 * {@link android.Manifest.permission#GET_ACCOUNTS}.
356 *
357 * @return An array of {@link Account}, one for each account. Empty
358 * (never null) if no accounts have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700359 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700360 public Account[] getAccounts() {
Fred Quintana60307342009-03-24 22:48:12 -0700361 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700362 return mService.getAccounts(null);
Fred Quintana60307342009-03-24 22:48:12 -0700363 } catch (RemoteException e) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700364 // won't ever happen
Fred Quintana60307342009-03-24 22:48:12 -0700365 throw new RuntimeException(e);
366 }
367 }
368
Fred Quintana756b7352009-10-21 13:43:10 -0700369 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800370 * Lists all accounts of a particular type. The account type is a
371 * string token corresponding to the authenticator and useful domain
372 * of the account. For example, there are types corresponding to Google
373 * and Facebook. The exact string token to use will be published somewhere
374 * associated with the authenticator in question.
375 *
376 * <p>It is safe to call this method from the main thread.
377 *
378 * <p>This method requires the caller to hold the permission
379 * {@link android.Manifest.permission#GET_ACCOUNTS}.
380 *
381 * @param type The type of accounts to return, null to retrieve all accounts
382 * @return An array of {@link Account}, one per matching account. Empty
383 * (never null) if no accounts of the specified type have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700384 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700385 public Account[] getAccountsByType(String type) {
Fred Quintana60307342009-03-24 22:48:12 -0700386 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700387 return mService.getAccounts(type);
Fred Quintana60307342009-03-24 22:48:12 -0700388 } catch (RemoteException e) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700389 // won't ever happen
Fred Quintana60307342009-03-24 22:48:12 -0700390 throw new RuntimeException(e);
391 }
392 }
393
Fred Quintana756b7352009-10-21 13:43:10 -0700394 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800395 * Finds out whether a particular account has all the specified features.
396 * Account features are authenticator-specific string tokens identifying
397 * boolean account properties. For example, features are used to tell
398 * whether Google accounts have a particular service (such as Google
399 * Calendar or Google Talk) enabled. The feature names and their meanings
400 * are published somewhere associated with the authenticator in question.
401 *
402 * <p>This method may be called from any thread, but the returned
403 * {@link AccountManagerFuture} must not be used on the main thread.
404 *
405 * <p>This method requires the caller to hold the permission
406 * {@link android.Manifest.permission#GET_ACCOUNTS}.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800407 *
408 * @param account The {@link Account} to test
Dan Egnor661f0132010-02-19 11:23:00 -0800409 * @param features An array of the account features to check
410 * @param callback Callback to invoke when the request completes,
411 * null for no callback
412 * @param handler {@link Handler} identifying the callback thread,
413 * null for the main thread
414 * @return An {@link AccountManagerFuture} which resolves to a Boolean,
415 * true if the account exists and has all of the specified features.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800416 */
Fred Quintana3084a6f2010-01-14 18:02:03 -0800417 public AccountManagerFuture<Boolean> hasFeatures(final Account account,
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800418 final String[] features,
419 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700420 if (account == null) throw new IllegalArgumentException("account is null");
421 if (features == null) throw new IllegalArgumentException("features is null");
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800422 return new Future2Task<Boolean>(handler, callback) {
423 public void doWork() throws RemoteException {
Fred Quintana3084a6f2010-01-14 18:02:03 -0800424 mService.hasFeatures(mResponse, account, features);
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800425 }
426 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
427 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
428 throw new AuthenticatorException("no result in response");
429 }
430 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
431 }
432 }.start();
433 }
434
435 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800436 * Lists all accounts of a type which have certain features. The account
437 * type identifies the authenticator (see {@link #getAccountsByType}).
438 * Account features are authenticator-specific string tokens identifying
439 * boolean account properties (see {@link #hasFeatures}).
Fred Quintana756b7352009-10-21 13:43:10 -0700440 *
Dan Egnor661f0132010-02-19 11:23:00 -0800441 * <p>Unlike {@link #getAccountsByType}, this method calls the authenticator,
442 * which may contact the server or do other work to check account features,
443 * so the method returns an {@link AccountManagerFuture}.
Fred Quintanaa698f422009-04-08 19:14:54 -0700444 *
Dan Egnor661f0132010-02-19 11:23:00 -0800445 * <p>This method may be called from any thread, but the returned
446 * {@link AccountManagerFuture} must not be used on the main thread.
Fred Quintana756b7352009-10-21 13:43:10 -0700447 *
Dan Egnor661f0132010-02-19 11:23:00 -0800448 * <p>This method requires the caller to hold the permission
449 * {@link android.Manifest.permission#GET_ACCOUNTS}.
Fred Quintana756b7352009-10-21 13:43:10 -0700450 *
Dan Egnor661f0132010-02-19 11:23:00 -0800451 * @param type The type of accounts to return, must not be null
452 * @param features An array of the account features to require,
453 * may be null or empty
454 * @param callback Callback to invoke when the request completes,
455 * null for no callback
456 * @param handler {@link Handler} identifying the callback thread,
457 * null for the main thread
458 * @return An {@link AccountManagerFuture} which resolves to an array of
459 * {@link Account}, one per account of the specified type which
460 * matches the requested features.
Fred Quintana8570f742010-02-18 10:32:54 -0800461 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700462 public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures(
463 final String type, final String[] features,
464 AccountManagerCallback<Account[]> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700465 if (type == null) throw new IllegalArgumentException("type is null");
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700466 return new Future2Task<Account[]>(handler, callback) {
467 public void doWork() throws RemoteException {
468 mService.getAccountsByFeatures(mResponse, type, features);
469 }
470 public Account[] bundleToResult(Bundle bundle) throws AuthenticatorException {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700471 if (!bundle.containsKey(KEY_ACCOUNTS)) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700472 throw new AuthenticatorException("no result in response");
473 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700474 final Parcelable[] parcelables = bundle.getParcelableArray(KEY_ACCOUNTS);
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700475 Account[] descs = new Account[parcelables.length];
476 for (int i = 0; i < parcelables.length; i++) {
477 descs[i] = (Account) parcelables[i];
478 }
479 return descs;
480 }
481 }.start();
482 }
483
Fred Quintana756b7352009-10-21 13:43:10 -0700484 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800485 * Adds an account directly to the AccountManager. Normally used by sign-up
486 * wizards associated with authenticators, not directly by applications.
Fred Quintana756b7352009-10-21 13:43:10 -0700487 *
Dan Egnor661f0132010-02-19 11:23:00 -0800488 * <p>It is safe to call this method from the main thread.
489 *
490 * <p>This method requires the caller to hold the permission
491 * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS}
492 * and to have the same UID as the added account's authenticator.
493 *
494 * @param account The {@link Account} to add
495 * @param password The password to associate with the account, null for none
496 * @param userdata String values to use for the account's userdata, null for none
Dan Egnor8e4378b2010-08-02 18:22:09 -0700497 * @return True if the account was successfully added, false if the account
Dan Egnor661f0132010-02-19 11:23:00 -0800498 * already exists, the account is null, or another error occurs.
499 */
500 public boolean addAccountExplicitly(Account account, String password, Bundle userdata) {
Fred Quintana382601f2010-03-25 12:25:10 -0700501 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800502 try {
503 return mService.addAccount(account, password, userdata);
504 } catch (RemoteException e) {
505 // won't ever happen
506 throw new RuntimeException(e);
507 }
508 }
509
510 /**
511 * Removes an account from the AccountManager. Does nothing if the account
512 * does not exist. Does not delete the account from the server.
513 * The authenticator may have its own policies preventing account
514 * deletion, in which case the account will not be deleted.
515 *
516 * <p>This method may be called from any thread, but the returned
517 * {@link AccountManagerFuture} must not be used on the main thread.
518 *
519 * <p>This method requires the caller to hold the permission
520 * {@link android.Manifest.permission#MANAGE_ACCOUNTS}.
521 *
522 * @param account The {@link Account} to remove
523 * @param callback Callback to invoke when the request completes,
524 * null for no callback
525 * @param handler {@link Handler} identifying the callback thread,
526 * null for the main thread
527 * @return An {@link AccountManagerFuture} which resolves to a Boolean,
528 * true if the account has been successfully removed,
529 * false if the authenticator forbids deleting this account.
530 */
531 public AccountManagerFuture<Boolean> removeAccount(final Account account,
532 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700533 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800534 return new Future2Task<Boolean>(handler, callback) {
535 public void doWork() throws RemoteException {
536 mService.removeAccount(mResponse, account);
537 }
538 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
539 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
540 throw new AuthenticatorException("no result in response");
541 }
542 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
543 }
544 }.start();
545 }
546
547 /**
548 * Removes an auth token from the AccountManager's cache. Does nothing if
549 * the auth token is not currently in the cache. Applications must call this
550 * method when the auth token is found to have expired or otherwise become
551 * invalid for authenticating requests. The AccountManager does not validate
552 * or expire cached auth tokens otherwise.
553 *
554 * <p>It is safe to call this method from the main thread.
555 *
556 * <p>This method requires the caller to hold the permission
Fred Quintanab38eb142010-02-24 13:40:54 -0800557 * {@link android.Manifest.permission#MANAGE_ACCOUNTS} or
558 * {@link android.Manifest.permission#USE_CREDENTIALS}
Dan Egnor661f0132010-02-19 11:23:00 -0800559 *
Fred Quintanaf35b68f2010-04-01 11:36:00 -0700560 * @param accountType The account type of the auth token to invalidate, must not be null
561 * @param authToken The auth token to invalidate, may be null
Dan Egnor661f0132010-02-19 11:23:00 -0800562 */
563 public void invalidateAuthToken(final String accountType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -0700564 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800565 try {
Fred Quintanaf35b68f2010-04-01 11:36:00 -0700566 if (authToken != null) {
567 mService.invalidateAuthToken(accountType, authToken);
568 }
Dan Egnor661f0132010-02-19 11:23:00 -0800569 } catch (RemoteException e) {
570 // won't ever happen
571 throw new RuntimeException(e);
572 }
573 }
574
575 /**
576 * Gets an auth token from the AccountManager's cache. If no auth
577 * token is cached for this account, null will be returned -- a new
578 * auth token will not be generated, and the server will not be contacted.
579 * Intended for use by the authenticator, not directly by applications.
580 *
581 * <p>It is safe to call this method from the main thread.
582 *
583 * <p>This method requires the caller to hold the permission
584 * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS}
585 * and to have the same UID as the account's authenticator.
586 *
587 * @param account The account to fetch an auth token for
588 * @param authTokenType The type of auth token to fetch, see {#getAuthToken}
589 * @return The cached auth token for this account and type, or null if
590 * no auth token is cached or the account does not exist.
591 */
592 public String peekAuthToken(final Account account, final String authTokenType) {
Fred Quintana382601f2010-03-25 12:25:10 -0700593 if (account == null) throw new IllegalArgumentException("account is null");
594 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800595 try {
596 return mService.peekAuthToken(account, authTokenType);
597 } catch (RemoteException e) {
598 // won't ever happen
599 throw new RuntimeException(e);
600 }
601 }
602
603 /**
604 * Sets or forgets a saved password. This modifies the local copy of the
605 * password used to automatically authenticate the user; it does
606 * not change the user's account password on the server. Intended for use
607 * by the authenticator, not directly by applications.
608 *
609 * <p>It is safe to call this method from the main thread.
610 *
611 * <p>This method requires the caller to hold the permission
612 * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS}
613 * and have the same UID as the account's authenticator.
614 *
615 * @param account The account to set a password for
616 * @param password The password to set, null to clear the password
617 */
618 public void setPassword(final Account account, final String password) {
Fred Quintana382601f2010-03-25 12:25:10 -0700619 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800620 try {
621 mService.setPassword(account, password);
622 } catch (RemoteException e) {
623 // won't ever happen
624 throw new RuntimeException(e);
625 }
626 }
627
628 /**
629 * Forgets a saved password. This erases the local copy of the password;
630 * it does not change the user's account password on the server.
631 * Has the same effect as setPassword(account, null) but requires fewer
632 * permissions, and may be used by applications or management interfaces
633 * to "sign out" from an account.
634 *
635 * <p>It is safe to call this method from the main thread.
636 *
637 * <p>This method requires the caller to hold the permission
638 * {@link android.Manifest.permission#MANAGE_ACCOUNTS}
639 *
640 * @param account The account whose password to clear
641 */
642 public void clearPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -0700643 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800644 try {
645 mService.clearPassword(account);
646 } catch (RemoteException e) {
647 // won't ever happen
648 throw new RuntimeException(e);
649 }
650 }
651
652 /**
653 * Sets one userdata key for an account. Intended by use for the
654 * authenticator to stash state for itself, not directly by applications.
655 * The meaning of the keys and values is up to the authenticator.
656 *
657 * <p>It is safe to call this method from the main thread.
658 *
659 * <p>This method requires the caller to hold the permission
660 * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS}
661 * and to have the same UID as the account's authenticator.
662 *
663 * @param account The account to set the userdata for
664 * @param key The userdata key to set. Must not be null
665 * @param value The value to set, null to clear this userdata key
666 */
667 public void setUserData(final Account account, final String key, final String value) {
Fred Quintana382601f2010-03-25 12:25:10 -0700668 if (account == null) throw new IllegalArgumentException("account is null");
669 if (key == null) throw new IllegalArgumentException("key is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800670 try {
671 mService.setUserData(account, key, value);
672 } catch (RemoteException e) {
673 // won't ever happen
674 throw new RuntimeException(e);
675 }
676 }
677
678 /**
679 * Adds an auth token to the AccountManager cache for an account.
680 * If the account does not exist then this call has no effect.
681 * Replaces any previous auth token for this account and auth token type.
682 * Intended for use by the authenticator, not directly by applications.
683 *
684 * <p>It is safe to call this method from the main thread.
685 *
686 * <p>This method requires the caller to hold the permission
687 * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS}
688 * and to have the same UID as the account's authenticator.
689 *
690 * @param account The account to set an auth token for
691 * @param authTokenType The type of the auth token, see {#getAuthToken}
692 * @param authToken The auth token to add to the cache
693 */
694 public void setAuthToken(Account account, final String authTokenType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -0700695 if (account == null) throw new IllegalArgumentException("account is null");
696 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800697 try {
698 mService.setAuthToken(account, authTokenType, authToken);
699 } catch (RemoteException e) {
700 // won't ever happen
701 throw new RuntimeException(e);
702 }
703 }
704
705 /**
706 * This convenience helper synchronously gets an auth token with
707 * {@link #getAuthToken(Account, String, boolean, AccountManagerCallback, Handler)}.
708 *
709 * <p>This method may block while a network request completes, and must
710 * never be made from the main thread.
711 *
712 * <p>This method requires the caller to hold the permission
713 * {@link android.Manifest.permission#USE_CREDENTIALS}.
714 *
715 * @param account The account to fetch an auth token for
716 * @param authTokenType The auth token type, see {#link getAuthToken}
717 * @param notifyAuthFailure If true, display a notification and return null
718 * if authentication fails; if false, prompt and wait for the user to
719 * re-enter correct credentials before returning
720 * @return An auth token of the specified type for this account, or null
721 * if authentication fails or none can be fetched.
722 * @throws AuthenticatorException if the authenticator failed to respond
723 * @throws OperationCanceledException if the request was canceled for any
724 * reason, including the user canceling a credential request
725 * @throws java.io.IOException if the authenticator experienced an I/O problem
726 * creating a new auth token, usually because of network trouble
727 */
728 public String blockingGetAuthToken(Account account, String authTokenType,
729 boolean notifyAuthFailure)
730 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana382601f2010-03-25 12:25:10 -0700731 if (account == null) throw new IllegalArgumentException("account is null");
732 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800733 Bundle bundle = getAuthToken(account, authTokenType, notifyAuthFailure, null /* callback */,
734 null /* handler */).getResult();
Fred Quintana96580e02010-03-04 13:42:42 -0800735 if (bundle == null) {
736 // This should never happen, but it does, occasionally. If it does return null to
737 // signify that we were not able to get the authtoken.
738 // TODO: remove this when the bug is found that sometimes causes a null bundle to be
739 // returned
740 Log.e(TAG, "blockingGetAuthToken: null was returned from getResult() for "
741 + account + ", authTokenType " + authTokenType);
742 return null;
743 }
Dan Egnor661f0132010-02-19 11:23:00 -0800744 return bundle.getString(KEY_AUTHTOKEN);
745 }
746
747 /**
748 * Gets an auth token of the specified type for a particular account,
749 * prompting the user for credentials if necessary. This method is
750 * intended for applications running in the foreground where it makes
751 * sense to ask the user directly for a password.
752 *
753 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -0700754 * type, then it is returned. Otherwise, if a saved password is
755 * available, it is sent to the server to generate a new auth token.
756 * Otherwise, the user is prompted to enter a password.
Dan Egnor661f0132010-02-19 11:23:00 -0800757 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700758 * <p>Some authenticators have auth token <em>types</em>, whose value
759 * is authenticator-dependent. Some services use different token types to
760 * access different functionality -- for example, Google uses different auth
761 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -0800762 *
763 * <p>This method may be called from any thread, but the returned
764 * {@link AccountManagerFuture} must not be used on the main thread.
765 *
766 * <p>This method requires the caller to hold the permission
767 * {@link android.Manifest.permission#USE_CREDENTIALS}.
768 *
769 * @param account The account to fetch an auth token for
770 * @param authTokenType The auth token type, an authenticator-dependent
771 * string token, must not be null
772 * @param options Authenticator-specific options for the request,
773 * may be null or empty
774 * @param activity The {@link Activity} context to use for launching a new
775 * authenticator-defined sub-Activity to prompt the user for a password
776 * if necessary; used only to call startActivity(); must not be null.
777 * @param callback Callback to invoke when the request completes,
778 * null for no callback
779 * @param handler {@link Handler} identifying the callback thread,
780 * null for the main thread
781 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
782 * at least the following fields:
Fred Quintana756b7352009-10-21 13:43:10 -0700783 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -0800784 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
785 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
786 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
Fred Quintana756b7352009-10-21 13:43:10 -0700787 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -0800788 *
789 * (Other authenticator-specific values may be returned.) If an auth token
790 * could not be fetched, {@link AccountManagerFuture#getResult()} throws:
791 * <ul>
792 * <li> {@link AuthenticatorException} if the authenticator failed to respond
793 * <li> {@link OperationCanceledException} if the operation is canceled for
794 * any reason, incluidng the user canceling a credential request
795 * <li> {@link IOException} if the authenticator experienced an I/O problem
796 * creating a new auth token, usually because of network trouble
797 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -0700798 * If the account is no longer present on the device, the return value is
799 * authenticator-dependent. The caller should verify the validity of the
800 * account before requesting an auth token.
Dan Egnor661f0132010-02-19 11:23:00 -0800801 */
802 public AccountManagerFuture<Bundle> getAuthToken(
803 final Account account, final String authTokenType, final Bundle options,
804 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700805 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800806 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
807 return new AmsTask(activity, handler, callback) {
808 public void doWork() throws RemoteException {
809 mService.getAuthToken(mResponse, account, authTokenType,
810 false /* notifyOnAuthFailure */, true /* expectActivityLaunch */,
811 options);
812 }
813 }.start();
814 }
815
816 /**
817 * Gets an auth token of the specified type for a particular account,
818 * optionally raising a notification if the user must enter credentials.
819 * This method is intended for background tasks and services where the
820 * user should not be immediately interrupted with a password prompt.
821 *
822 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -0700823 * type, then it is returned. Otherwise, if a saved password is
824 * available, it is sent to the server to generate a new auth token.
825 * Otherwise, an {@link Intent} is returned which, when started, will
826 * prompt the user for a password. If the notifyAuthFailure parameter is
827 * set, a status bar notification is also created with the same Intent,
Dan Egnor661f0132010-02-19 11:23:00 -0800828 * alerting the user that they need to enter a password at some point.
829 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700830 * <p>In that case, you may need to wait until the user responds, which
831 * could take hours or days or forever. When the user does respond and
832 * supply a new password, the account manager will broadcast the
833 * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent, which applications can
834 * use to try again.
Dan Egnor661f0132010-02-19 11:23:00 -0800835 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700836 * <p>If notifyAuthFailure is not set, it is the application's
837 * responsibility to launch the returned Intent at some point.
838 * Either way, the result from this call will not wait for user action.
Dan Egnor661f0132010-02-19 11:23:00 -0800839 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700840 * <p>Some authenticators have auth token <em>types</em>, whose value
841 * is authenticator-dependent. Some services use different token types to
842 * access different functionality -- for example, Google uses different auth
843 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -0800844 *
845 * <p>This method may be called from any thread, but the returned
846 * {@link AccountManagerFuture} must not be used on the main thread.
847 *
848 * <p>This method requires the caller to hold the permission
849 * {@link android.Manifest.permission#USE_CREDENTIALS}.
850 *
851 * @param account The account to fetch an auth token for
852 * @param authTokenType The auth token type, an authenticator-dependent
853 * string token, must not be null
Dan Egnor661f0132010-02-19 11:23:00 -0800854 * @param notifyAuthFailure True to add a notification to prompt the
855 * user for a password if necessary, false to leave that to the caller
856 * @param callback Callback to invoke when the request completes,
857 * null for no callback
858 * @param handler {@link Handler} identifying the callback thread,
859 * null for the main thread
860 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
861 * at least the following fields on success:
862 * <ul>
863 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
864 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
865 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
866 * </ul>
867 *
868 * (Other authenticator-specific values may be returned.) If the user
869 * must enter credentials, the returned Bundle contains only
870 * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt.
871 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700872 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -0800873 * <ul>
874 * <li> {@link AuthenticatorException} if the authenticator failed to respond
875 * <li> {@link OperationCanceledException} if the operation is canceled for
876 * any reason, incluidng the user canceling a credential request
877 * <li> {@link IOException} if the authenticator experienced an I/O problem
878 * creating a new auth token, usually because of network trouble
879 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -0700880 * If the account is no longer present on the device, the return value is
881 * authenticator-dependent. The caller should verify the validity of the
882 * account before requesting an auth token.
Dan Egnor661f0132010-02-19 11:23:00 -0800883 */
884 public AccountManagerFuture<Bundle> getAuthToken(
885 final Account account, final String authTokenType, final boolean notifyAuthFailure,
886 AccountManagerCallback<Bundle> callback, Handler handler) {
887 if (account == null) throw new IllegalArgumentException("account is null");
888 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
889 return new AmsTask(null, handler, callback) {
890 public void doWork() throws RemoteException {
891 mService.getAuthToken(mResponse, account, authTokenType,
892 notifyAuthFailure, false /* expectActivityLaunch */, null /* options */);
893 }
894 }.start();
895 }
896
897 /**
898 * Asks the user to add an account of a specified type. The authenticator
899 * for this account type processes this request with the appropriate user
900 * interface. If the user does elect to create a new account, the account
901 * name is returned.
902 *
903 * <p>This method may be called from any thread, but the returned
904 * {@link AccountManagerFuture} must not be used on the main thread.
905 *
906 * <p>This method requires the caller to hold the permission
907 * {@link android.Manifest.permission#MANAGE_ACCOUNTS}.
908 *
909 * @param accountType The type of account to add; must not be null
910 * @param authTokenType The type of auth token (see {@link #getAuthToken})
911 * this account will need to be able to generate, null for none
912 * @param requiredFeatures The features (see {@link #hasFeatures}) this
913 * account must have, null for none
914 * @param addAccountOptions Authenticator-specific options for the request,
915 * may be null or empty
916 * @param activity The {@link Activity} context to use for launching a new
917 * authenticator-defined sub-Activity to prompt the user to create an
918 * account; used only to call startActivity(); if null, the prompt
919 * will not be launched directly, but the necessary {@link Intent}
920 * will be returned to the caller instead
921 * @param callback Callback to invoke when the request completes,
922 * null for no callback
923 * @param handler {@link Handler} identifying the callback thread,
924 * null for the main thread
Doug Zongkerff592dc2010-02-23 12:26:33 -0800925 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
Dan Egnor661f0132010-02-19 11:23:00 -0800926 * these fields if activity was specified and an account was created:
927 * <ul>
928 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created
929 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
930 * </ul>
931 *
932 * If no activity was specified, the returned Bundle contains only
933 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700934 * actual account creation process. If an error occurred,
935 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -0800936 * <ul>
937 * <li> {@link AuthenticatorException} if no authenticator was registered for
938 * this account type or the authenticator failed to respond
939 * <li> {@link OperationCanceledException} if the operation was canceled for
940 * any reason, including the user canceling the creation process
941 * <li> {@link IOException} if the authenticator experienced an I/O problem
942 * creating a new account, usually because of network trouble
943 * </ul>
944 */
945 public AccountManagerFuture<Bundle> addAccount(final String accountType,
946 final String authTokenType, final String[] requiredFeatures,
947 final Bundle addAccountOptions,
948 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700949 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800950 return new AmsTask(activity, handler, callback) {
951 public void doWork() throws RemoteException {
Dan Egnor661f0132010-02-19 11:23:00 -0800952 mService.addAcount(mResponse, accountType, authTokenType,
953 requiredFeatures, activity != null, addAccountOptions);
954 }
955 }.start();
956 }
957
958 /**
959 * Confirms that the user knows the password for an account to make extra
960 * sure they are the owner of the account. The user-entered password can
961 * be supplied directly, otherwise the authenticator for this account type
962 * prompts the user with the appropriate interface. This method is
963 * intended for applications which want extra assurance; for example, the
964 * phone lock screen uses this to let the user unlock the phone with an
965 * account password if they forget the lock pattern.
966 *
967 * <p>If the user-entered password matches a saved password for this
968 * account, the request is considered valid; otherwise the authenticator
969 * verifies the password (usually by contacting the server).
970 *
971 * <p>This method may be called from any thread, but the returned
972 * {@link AccountManagerFuture} must not be used on the main thread.
973 *
974 * <p>This method requires the caller to hold the permission
975 * {@link android.Manifest.permission#MANAGE_ACCOUNTS}.
976 *
977 * @param account The account to confirm password knowledge for
978 * @param options Authenticator-specific options for the request;
979 * if the {@link #KEY_PASSWORD} string field is present, the
980 * authenticator may use it directly rather than prompting the user;
981 * may be null or empty
982 * @param activity The {@link Activity} context to use for launching a new
983 * authenticator-defined sub-Activity to prompt the user to enter a
984 * password; used only to call startActivity(); if null, the prompt
985 * will not be launched directly, but the necessary {@link Intent}
986 * will be returned to the caller instead
987 * @param callback Callback to invoke when the request completes,
988 * null for no callback
989 * @param handler {@link Handler} identifying the callback thread,
990 * null for the main thread
991 * @return An {@link AccountManagerFuture} which resolves to a Bundle
992 * with these fields if activity or password was supplied and
993 * the account was successfully verified:
994 * <ul>
995 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created
996 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
997 * <li> {@link #KEY_BOOLEAN_RESULT} - true to indicate success
998 * </ul>
999 *
1000 * If no activity or password was specified, the returned Bundle contains
1001 * only {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Dan Egnor8e4378b2010-08-02 18:22:09 -07001002 * password prompt. If an error occurred,
1003 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001004 * <ul>
1005 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1006 * <li> {@link OperationCanceledException} if the operation was canceled for
1007 * any reason, including the user canceling the password prompt
1008 * <li> {@link IOException} if the authenticator experienced an I/O problem
1009 * verifying the password, usually because of network trouble
1010 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001011 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001012 public AccountManagerFuture<Bundle> confirmCredentials(final Account account,
1013 final Bundle options,
1014 final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001015 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07001016 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001017 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07001018 return new AmsTask(activity, handler, callback) {
1019 public void doWork() throws RemoteException {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001020 mService.confirmCredentials(mResponse, account, options, activity != null);
Fred Quintanaa698f422009-04-08 19:14:54 -07001021 }
Fred Quintana33269202009-04-20 16:05:10 -07001022 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07001023 }
1024
Fred Quintana756b7352009-10-21 13:43:10 -07001025 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001026 * Asks the user to enter a new password for an account, updating the
1027 * saved credentials for the account. Normally this happens automatically
1028 * when the server rejects credentials during an auth token fetch, but this
1029 * can be invoked directly to ensure we have the correct credentials stored.
Fred Quintana756b7352009-10-21 13:43:10 -07001030 *
Dan Egnor661f0132010-02-19 11:23:00 -08001031 * <p>This method may be called from any thread, but the returned
1032 * {@link AccountManagerFuture} must not be used on the main thread.
1033 *
1034 * <p>This method requires the caller to hold the permission
1035 * {@link android.Manifest.permission#MANAGE_ACCOUNTS}.
1036 *
1037 * @param account The account to update credentials for
1038 * @param authTokenType The credentials entered must allow an auth token
1039 * of this type to be created (but no actual auth token is returned);
1040 * may be null
1041 * @param options Authenticator-specific options for the request;
1042 * may be null or empty
1043 * @param activity The {@link Activity} context to use for launching a new
1044 * authenticator-defined sub-Activity to prompt the user to enter a
1045 * password; used only to call startActivity(); if null, the prompt
1046 * will not be launched directly, but the necessary {@link Intent}
1047 * will be returned to the caller instead
1048 * @param callback Callback to invoke when the request completes,
1049 * null for no callback
1050 * @param handler {@link Handler} identifying the callback thread,
1051 * null for the main thread
1052 * @return An {@link AccountManagerFuture} which resolves to a Bundle
1053 * with these fields if an activity was supplied and the account
1054 * credentials were successfully updated:
Fred Quintana756b7352009-10-21 13:43:10 -07001055 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001056 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created
1057 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
Fred Quintana756b7352009-10-21 13:43:10 -07001058 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001059 *
1060 * If no activity was specified, the returned Bundle contains only
1061 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Dan Egnor8e4378b2010-08-02 18:22:09 -07001062 * password prompt. If an error occurred,
1063 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001064 * <ul>
1065 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1066 * <li> {@link OperationCanceledException} if the operation was canceled for
1067 * any reason, including the user canceling the password prompt
1068 * <li> {@link IOException} if the authenticator experienced an I/O problem
1069 * verifying the password, usually because of network trouble
1070 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001071 */
1072 public AccountManagerFuture<Bundle> updateCredentials(final Account account,
1073 final String authTokenType,
Fred Quintana31957f12009-10-21 13:43:10 -07001074 final Bundle options, final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001075 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07001076 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001077 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07001078 return new AmsTask(activity, handler, callback) {
1079 public void doWork() throws RemoteException {
1080 mService.updateCredentials(mResponse, account, authTokenType, activity != null,
Fred Quintana31957f12009-10-21 13:43:10 -07001081 options);
Fred Quintanaa698f422009-04-08 19:14:54 -07001082 }
Fred Quintana33269202009-04-20 16:05:10 -07001083 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07001084 }
1085
Fred Quintana756b7352009-10-21 13:43:10 -07001086 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001087 * Offers the user an opportunity to change an authenticator's settings.
1088 * These properties are for the authenticator in general, not a particular
1089 * account. Not all authenticators support this method.
Fred Quintana756b7352009-10-21 13:43:10 -07001090 *
Dan Egnor661f0132010-02-19 11:23:00 -08001091 * <p>This method may be called from any thread, but the returned
1092 * {@link AccountManagerFuture} must not be used on the main thread.
1093 *
1094 * <p>This method requires the caller to hold the permission
1095 * {@link android.Manifest.permission#MANAGE_ACCOUNTS}.
1096 *
1097 * @param accountType The account type associated with the authenticator
1098 * to adjust
1099 * @param activity The {@link Activity} context to use for launching a new
1100 * authenticator-defined sub-Activity to adjust authenticator settings;
1101 * used only to call startActivity(); if null, the settings dialog will
1102 * not be launched directly, but the necessary {@link Intent} will be
1103 * returned to the caller instead
1104 * @param callback Callback to invoke when the request completes,
1105 * null for no callback
1106 * @param handler {@link Handler} identifying the callback thread,
1107 * null for the main thread
1108 * @return An {@link AccountManagerFuture} which resolves to a Bundle
1109 * which is empty if properties were edited successfully, or
1110 * if no activity was specified, contains only {@link #KEY_INTENT}
1111 * needed to launch the authenticator's settings dialog.
Dan Egnor8e4378b2010-08-02 18:22:09 -07001112 * If an error occurred, {@link AccountManagerFuture#getResult()}
1113 * throws:
Fred Quintana756b7352009-10-21 13:43:10 -07001114 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001115 * <li> {@link AuthenticatorException} if no authenticator was registered for
1116 * this account type or the authenticator failed to respond
1117 * <li> {@link OperationCanceledException} if the operation was canceled for
1118 * any reason, including the user canceling the settings dialog
1119 * <li> {@link IOException} if the authenticator experienced an I/O problem
1120 * updating settings, usually because of network trouble
Fred Quintana756b7352009-10-21 13:43:10 -07001121 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001122 */
1123 public AccountManagerFuture<Bundle> editProperties(final String accountType,
1124 final Activity activity, final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07001125 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001126 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07001127 return new AmsTask(activity, handler, callback) {
1128 public void doWork() throws RemoteException {
1129 mService.editProperties(mResponse, accountType, activity != null);
1130 }
Fred Quintana33269202009-04-20 16:05:10 -07001131 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07001132 }
1133
1134 private void ensureNotOnMainThread() {
1135 final Looper looper = Looper.myLooper();
1136 if (looper != null && looper == mContext.getMainLooper()) {
Fred Quintana53bd2522010-02-05 15:28:12 -08001137 final IllegalStateException exception = new IllegalStateException(
1138 "calling this from your main thread can lead to deadlock");
1139 Log.e(TAG, "calling this from your main thread can lead to deadlock and/or ANRs",
1140 exception);
Fred Quintana751fdc02010-02-09 14:13:18 -08001141 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.FROYO) {
1142 throw exception;
1143 }
Fred Quintana60307342009-03-24 22:48:12 -07001144 }
1145 }
1146
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001147 private void postToHandler(Handler handler, final AccountManagerCallback<Bundle> callback,
1148 final AccountManagerFuture<Bundle> future) {
Fred Quintanad9d2f112009-04-23 13:36:27 -07001149 handler = handler == null ? mMainHandler : handler;
1150 handler.post(new Runnable() {
Fred Quintanaa698f422009-04-08 19:14:54 -07001151 public void run() {
1152 callback.run(future);
1153 }
1154 });
1155 }
Fred Quintana60307342009-03-24 22:48:12 -07001156
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001157 private void postToHandler(Handler handler, final OnAccountsUpdateListener listener,
Fred Quintanad9d2f112009-04-23 13:36:27 -07001158 final Account[] accounts) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001159 final Account[] accountsCopy = new Account[accounts.length];
1160 // send a copy to make sure that one doesn't
1161 // change what another sees
1162 System.arraycopy(accounts, 0, accountsCopy, 0, accountsCopy.length);
1163 handler = (handler == null) ? mMainHandler : handler;
Fred Quintanad9d2f112009-04-23 13:36:27 -07001164 handler.post(new Runnable() {
1165 public void run() {
Costin Manolacheb6437242009-09-10 16:14:12 -07001166 try {
1167 listener.onAccountsUpdated(accountsCopy);
1168 } catch (SQLException e) {
1169 // Better luck next time. If the problem was disk-full,
1170 // the STORAGE_OK intent will re-trigger the update.
1171 Log.e(TAG, "Can't update accounts", e);
1172 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07001173 }
1174 });
1175 }
1176
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001177 private abstract class AmsTask extends FutureTask<Bundle> implements AccountManagerFuture<Bundle> {
Fred Quintanaa698f422009-04-08 19:14:54 -07001178 final IAccountManagerResponse mResponse;
1179 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001180 final AccountManagerCallback<Bundle> mCallback;
Fred Quintanaa698f422009-04-08 19:14:54 -07001181 final Activity mActivity;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001182 public AmsTask(Activity activity, Handler handler, AccountManagerCallback<Bundle> callback) {
Fred Quintanaa698f422009-04-08 19:14:54 -07001183 super(new Callable<Bundle>() {
1184 public Bundle call() throws Exception {
1185 throw new IllegalStateException("this should never be called");
1186 }
1187 });
1188
1189 mHandler = handler;
1190 mCallback = callback;
1191 mActivity = activity;
1192 mResponse = new Response();
Fred Quintana33269202009-04-20 16:05:10 -07001193 }
1194
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001195 public final AccountManagerFuture<Bundle> start() {
1196 try {
1197 doWork();
1198 } catch (RemoteException e) {
1199 setException(e);
1200 }
Fred Quintana33269202009-04-20 16:05:10 -07001201 return this;
Fred Quintana60307342009-03-24 22:48:12 -07001202 }
Fred Quintanaa698f422009-04-08 19:14:54 -07001203
Fred Quintana96580e02010-03-04 13:42:42 -08001204 protected void set(Bundle bundle) {
1205 // TODO: somehow a null is being set as the result of the Future. Log this
1206 // case to help debug where this is occurring. When this bug is fixed this
1207 // condition statement should be removed.
1208 if (bundle == null) {
1209 Log.e(TAG, "the bundle must not be null", new Exception());
1210 }
1211 super.set(bundle);
1212 }
1213
Fred Quintanaa698f422009-04-08 19:14:54 -07001214 public abstract void doWork() throws RemoteException;
1215
1216 private Bundle internalGetResult(Long timeout, TimeUnit unit)
1217 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08001218 if (!isDone()) {
1219 ensureNotOnMainThread();
1220 }
Fred Quintanaa698f422009-04-08 19:14:54 -07001221 try {
1222 if (timeout == null) {
1223 return get();
1224 } else {
1225 return get(timeout, unit);
1226 }
1227 } catch (CancellationException e) {
1228 throw new OperationCanceledException();
1229 } catch (TimeoutException e) {
1230 // fall through and cancel
1231 } catch (InterruptedException e) {
1232 // fall through and cancel
1233 } catch (ExecutionException e) {
1234 final Throwable cause = e.getCause();
1235 if (cause instanceof IOException) {
1236 throw (IOException) cause;
1237 } else if (cause instanceof UnsupportedOperationException) {
1238 throw new AuthenticatorException(cause);
1239 } else if (cause instanceof AuthenticatorException) {
1240 throw (AuthenticatorException) cause;
1241 } else if (cause instanceof RuntimeException) {
1242 throw (RuntimeException) cause;
1243 } else if (cause instanceof Error) {
1244 throw (Error) cause;
1245 } else {
1246 throw new IllegalStateException(cause);
1247 }
1248 } finally {
1249 cancel(true /* interrupt if running */);
1250 }
1251 throw new OperationCanceledException();
1252 }
1253
1254 public Bundle getResult()
1255 throws OperationCanceledException, IOException, AuthenticatorException {
1256 return internalGetResult(null, null);
1257 }
1258
1259 public Bundle getResult(long timeout, TimeUnit unit)
1260 throws OperationCanceledException, IOException, AuthenticatorException {
1261 return internalGetResult(timeout, unit);
1262 }
1263
1264 protected void done() {
1265 if (mCallback != null) {
1266 postToHandler(mHandler, mCallback, this);
1267 }
1268 }
1269
1270 /** Handles the responses from the AccountManager */
1271 private class Response extends IAccountManagerResponse.Stub {
1272 public void onResult(Bundle bundle) {
Brian Carlstrom46703b02011-04-06 15:41:29 -07001273 Intent intent = bundle.getParcelable(KEY_INTENT);
Fred Quintanaa698f422009-04-08 19:14:54 -07001274 if (intent != null && mActivity != null) {
1275 // since the user provided an Activity we will silently start intents
1276 // that we see
1277 mActivity.startActivity(intent);
1278 // leave the Future running to wait for the real response to this request
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07001279 } else if (bundle.getBoolean("retry")) {
1280 try {
1281 doWork();
1282 } catch (RemoteException e) {
1283 // this will only happen if the system process is dead, which means
1284 // we will be dying ourselves
1285 }
Fred Quintanaa698f422009-04-08 19:14:54 -07001286 } else {
1287 set(bundle);
1288 }
1289 }
1290
1291 public void onError(int code, String message) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001292 if (code == ERROR_CODE_CANCELED) {
Fred Quintanaa698f422009-04-08 19:14:54 -07001293 // the authenticator indicated that this request was canceled, do so now
1294 cancel(true /* mayInterruptIfRunning */);
1295 return;
1296 }
1297 setException(convertErrorToException(code, message));
1298 }
1299 }
1300
Fred Quintana60307342009-03-24 22:48:12 -07001301 }
1302
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001303 private abstract class BaseFutureTask<T> extends FutureTask<T> {
1304 final public IAccountManagerResponse mResponse;
Fred Quintanaa698f422009-04-08 19:14:54 -07001305 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001306
1307 public BaseFutureTask(Handler handler) {
1308 super(new Callable<T>() {
1309 public T call() throws Exception {
Fred Quintanaa698f422009-04-08 19:14:54 -07001310 throw new IllegalStateException("this should never be called");
1311 }
1312 });
Fred Quintanaa698f422009-04-08 19:14:54 -07001313 mHandler = handler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001314 mResponse = new Response();
Fred Quintana60307342009-03-24 22:48:12 -07001315 }
Fred Quintanaa698f422009-04-08 19:14:54 -07001316
1317 public abstract void doWork() throws RemoteException;
1318
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001319 public abstract T bundleToResult(Bundle bundle) throws AuthenticatorException;
Fred Quintanaa698f422009-04-08 19:14:54 -07001320
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001321 protected void postRunnableToHandler(Runnable runnable) {
1322 Handler handler = (mHandler == null) ? mMainHandler : mHandler;
1323 handler.post(runnable);
Fred Quintanaa698f422009-04-08 19:14:54 -07001324 }
1325
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001326 protected void startTask() {
Fred Quintanaa698f422009-04-08 19:14:54 -07001327 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001328 doWork();
1329 } catch (RemoteException e) {
1330 setException(e);
Fred Quintanaa698f422009-04-08 19:14:54 -07001331 }
Fred Quintanaa698f422009-04-08 19:14:54 -07001332 }
1333
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001334 protected class Response extends IAccountManagerResponse.Stub {
Fred Quintanaa698f422009-04-08 19:14:54 -07001335 public void onResult(Bundle bundle) {
1336 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001337 T result = bundleToResult(bundle);
1338 if (result == null) {
Fred Quintanaa698f422009-04-08 19:14:54 -07001339 return;
1340 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001341 set(result);
1342 return;
Fred Quintanaa698f422009-04-08 19:14:54 -07001343 } catch (ClassCastException e) {
1344 // we will set the exception below
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001345 } catch (AuthenticatorException e) {
1346 // we will set the exception below
Fred Quintanaa698f422009-04-08 19:14:54 -07001347 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001348 onError(ERROR_CODE_INVALID_RESPONSE, "no result in response");
Fred Quintanaa698f422009-04-08 19:14:54 -07001349 }
1350
1351 public void onError(int code, String message) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001352 if (code == ERROR_CODE_CANCELED) {
Fred Quintanaa698f422009-04-08 19:14:54 -07001353 cancel(true /* mayInterruptIfRunning */);
1354 return;
1355 }
1356 setException(convertErrorToException(code, message));
1357 }
1358 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001359 }
1360
1361 private abstract class Future2Task<T>
1362 extends BaseFutureTask<T> implements AccountManagerFuture<T> {
1363 final AccountManagerCallback<T> mCallback;
1364 public Future2Task(Handler handler, AccountManagerCallback<T> callback) {
1365 super(handler);
1366 mCallback = callback;
1367 }
1368
1369 protected void done() {
1370 if (mCallback != null) {
1371 postRunnableToHandler(new Runnable() {
1372 public void run() {
1373 mCallback.run(Future2Task.this);
1374 }
1375 });
1376 }
1377 }
1378
1379 public Future2Task<T> start() {
1380 startTask();
1381 return this;
1382 }
1383
1384 private T internalGetResult(Long timeout, TimeUnit unit)
1385 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08001386 if (!isDone()) {
1387 ensureNotOnMainThread();
1388 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001389 try {
1390 if (timeout == null) {
1391 return get();
1392 } else {
1393 return get(timeout, unit);
1394 }
1395 } catch (InterruptedException e) {
1396 // fall through and cancel
1397 } catch (TimeoutException e) {
1398 // fall through and cancel
1399 } catch (CancellationException e) {
1400 // fall through and cancel
1401 } catch (ExecutionException e) {
1402 final Throwable cause = e.getCause();
1403 if (cause instanceof IOException) {
1404 throw (IOException) cause;
1405 } else if (cause instanceof UnsupportedOperationException) {
1406 throw new AuthenticatorException(cause);
1407 } else if (cause instanceof AuthenticatorException) {
1408 throw (AuthenticatorException) cause;
1409 } else if (cause instanceof RuntimeException) {
1410 throw (RuntimeException) cause;
1411 } else if (cause instanceof Error) {
1412 throw (Error) cause;
1413 } else {
1414 throw new IllegalStateException(cause);
1415 }
1416 } finally {
1417 cancel(true /* interrupt if running */);
1418 }
1419 throw new OperationCanceledException();
1420 }
1421
1422 public T getResult()
1423 throws OperationCanceledException, IOException, AuthenticatorException {
1424 return internalGetResult(null, null);
1425 }
1426
1427 public T getResult(long timeout, TimeUnit unit)
1428 throws OperationCanceledException, IOException, AuthenticatorException {
1429 return internalGetResult(timeout, unit);
1430 }
Fred Quintanaa698f422009-04-08 19:14:54 -07001431
Fred Quintana60307342009-03-24 22:48:12 -07001432 }
1433
Fred Quintanaa698f422009-04-08 19:14:54 -07001434 private Exception convertErrorToException(int code, String message) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001435 if (code == ERROR_CODE_NETWORK_ERROR) {
Fred Quintanaa698f422009-04-08 19:14:54 -07001436 return new IOException(message);
Fred Quintana60307342009-03-24 22:48:12 -07001437 }
Fred Quintana60307342009-03-24 22:48:12 -07001438
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001439 if (code == ERROR_CODE_UNSUPPORTED_OPERATION) {
Fred Quintana33269202009-04-20 16:05:10 -07001440 return new UnsupportedOperationException(message);
Fred Quintana60307342009-03-24 22:48:12 -07001441 }
Fred Quintanaa698f422009-04-08 19:14:54 -07001442
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001443 if (code == ERROR_CODE_INVALID_RESPONSE) {
Fred Quintana33269202009-04-20 16:05:10 -07001444 return new AuthenticatorException(message);
Fred Quintanaa698f422009-04-08 19:14:54 -07001445 }
1446
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001447 if (code == ERROR_CODE_BAD_ARGUMENTS) {
Fred Quintana33269202009-04-20 16:05:10 -07001448 return new IllegalArgumentException(message);
1449 }
1450
1451 return new AuthenticatorException(message);
1452 }
1453
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001454 private class GetAuthTokenByTypeAndFeaturesTask
1455 extends AmsTask implements AccountManagerCallback<Bundle> {
Fred Quintana33269202009-04-20 16:05:10 -07001456 GetAuthTokenByTypeAndFeaturesTask(final String accountType, final String authTokenType,
1457 final String[] features, Activity activityForPrompting,
1458 final Bundle addAccountOptions, final Bundle loginOptions,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001459 AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07001460 super(activityForPrompting, handler, callback);
1461 if (accountType == null) throw new IllegalArgumentException("account type is null");
1462 mAccountType = accountType;
1463 mAuthTokenType = authTokenType;
1464 mFeatures = features;
1465 mAddAccountOptions = addAccountOptions;
1466 mLoginOptions = loginOptions;
1467 mMyCallback = this;
1468 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001469 volatile AccountManagerFuture<Bundle> mFuture = null;
Fred Quintana33269202009-04-20 16:05:10 -07001470 final String mAccountType;
1471 final String mAuthTokenType;
1472 final String[] mFeatures;
1473 final Bundle mAddAccountOptions;
1474 final Bundle mLoginOptions;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001475 final AccountManagerCallback<Bundle> mMyCallback;
Fred Quintanaf0fd8432010-03-08 12:48:05 -08001476 private volatile int mNumAccounts = 0;
Fred Quintana33269202009-04-20 16:05:10 -07001477
1478 public void doWork() throws RemoteException {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001479 getAccountsByTypeAndFeatures(mAccountType, mFeatures,
1480 new AccountManagerCallback<Account[]>() {
1481 public void run(AccountManagerFuture<Account[]> future) {
1482 Account[] accounts;
Fred Quintana33269202009-04-20 16:05:10 -07001483 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001484 accounts = future.getResult();
1485 } catch (OperationCanceledException e) {
1486 setException(e);
1487 return;
1488 } catch (IOException e) {
1489 setException(e);
1490 return;
1491 } catch (AuthenticatorException e) {
1492 setException(e);
1493 return;
Fred Quintana33269202009-04-20 16:05:10 -07001494 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001495
Fred Quintanaf0fd8432010-03-08 12:48:05 -08001496 mNumAccounts = accounts.length;
1497
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001498 if (accounts.length == 0) {
1499 if (mActivity != null) {
1500 // no accounts, add one now. pretend that the user directly
1501 // made this request
1502 mFuture = addAccount(mAccountType, mAuthTokenType, mFeatures,
1503 mAddAccountOptions, mActivity, mMyCallback, mHandler);
1504 } else {
1505 // send result since we can't prompt to add an account
1506 Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001507 result.putString(KEY_ACCOUNT_NAME, null);
1508 result.putString(KEY_ACCOUNT_TYPE, null);
1509 result.putString(KEY_AUTHTOKEN, null);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001510 try {
1511 mResponse.onResult(result);
1512 } catch (RemoteException e) {
1513 // this will never happen
1514 }
1515 // we are done
1516 }
1517 } else if (accounts.length == 1) {
1518 // have a single account, return an authtoken for it
1519 if (mActivity == null) {
1520 mFuture = getAuthToken(accounts[0], mAuthTokenType,
1521 false /* notifyAuthFailure */, mMyCallback, mHandler);
1522 } else {
1523 mFuture = getAuthToken(accounts[0],
1524 mAuthTokenType, mLoginOptions,
Fred Quintana33269202009-04-20 16:05:10 -07001525 mActivity, mMyCallback, mHandler);
1526 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001527 } else {
1528 if (mActivity != null) {
1529 IAccountManagerResponse chooseResponse =
1530 new IAccountManagerResponse.Stub() {
1531 public void onResult(Bundle value) throws RemoteException {
1532 Account account = new Account(
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001533 value.getString(KEY_ACCOUNT_NAME),
1534 value.getString(KEY_ACCOUNT_TYPE));
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001535 mFuture = getAuthToken(account, mAuthTokenType, mLoginOptions,
1536 mActivity, mMyCallback, mHandler);
1537 }
Fred Quintana33269202009-04-20 16:05:10 -07001538
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001539 public void onError(int errorCode, String errorMessage)
1540 throws RemoteException {
1541 mResponse.onError(errorCode, errorMessage);
1542 }
1543 };
1544 // have many accounts, launch the chooser
1545 Intent intent = new Intent();
1546 intent.setClassName("android",
1547 "android.accounts.ChooseAccountActivity");
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001548 intent.putExtra(KEY_ACCOUNTS, accounts);
1549 intent.putExtra(KEY_ACCOUNT_MANAGER_RESPONSE,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001550 new AccountManagerResponse(chooseResponse));
1551 mActivity.startActivity(intent);
1552 // the result will arrive via the IAccountManagerResponse
1553 } else {
1554 // send result since we can't prompt to select an account
1555 Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001556 result.putString(KEY_ACCOUNTS, null);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001557 try {
1558 mResponse.onResult(result);
1559 } catch (RemoteException e) {
1560 // this will never happen
1561 }
1562 // we are done
Fred Quintana33269202009-04-20 16:05:10 -07001563 }
Fred Quintana33269202009-04-20 16:05:10 -07001564 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001565 }}, mHandler);
Fred Quintana33269202009-04-20 16:05:10 -07001566 }
1567
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001568 public void run(AccountManagerFuture<Bundle> future) {
Fred Quintana33269202009-04-20 16:05:10 -07001569 try {
Fred Quintanaf0fd8432010-03-08 12:48:05 -08001570 final Bundle result = future.getResult();
1571 if (mNumAccounts == 0) {
1572 final String accountName = result.getString(KEY_ACCOUNT_NAME);
1573 final String accountType = result.getString(KEY_ACCOUNT_TYPE);
1574 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
1575 setException(new AuthenticatorException("account not in result"));
1576 return;
1577 }
1578 final Account account = new Account(accountName, accountType);
1579 mNumAccounts = 1;
1580 getAuthToken(account, mAuthTokenType, null /* options */, mActivity,
1581 mMyCallback, mHandler);
1582 return;
1583 }
1584 set(result);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001585 } catch (OperationCanceledException e) {
1586 cancel(true /* mayInterruptIfRUnning */);
1587 } catch (IOException e) {
1588 setException(e);
1589 } catch (AuthenticatorException e) {
1590 setException(e);
Fred Quintana33269202009-04-20 16:05:10 -07001591 }
1592 }
1593 }
1594
Fred Quintana756b7352009-10-21 13:43:10 -07001595 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001596 * This convenience helper combines the functionality of
1597 * {@link #getAccountsByTypeAndFeatures}, {@link #getAuthToken}, and
1598 * {@link #addAccount}.
Fred Quintana756b7352009-10-21 13:43:10 -07001599 *
Dan Egnor661f0132010-02-19 11:23:00 -08001600 * <p>This method gets a list of the accounts matching the
1601 * specified type and feature set; if there is exactly one, it is
1602 * used; if there are more than one, the user is prompted to pick one;
1603 * if there are none, the user is prompted to add one. Finally,
1604 * an auth token is acquired for the chosen account.
1605 *
1606 * <p>This method may be called from any thread, but the returned
1607 * {@link AccountManagerFuture} must not be used on the main thread.
1608 *
1609 * <p>This method requires the caller to hold the permission
1610 * {@link android.Manifest.permission#MANAGE_ACCOUNTS}.
1611 *
1612 * @param accountType The account type required
Doug Zongkerff592dc2010-02-23 12:26:33 -08001613 * (see {@link #getAccountsByType}), must not be null
Dan Egnor661f0132010-02-19 11:23:00 -08001614 * @param authTokenType The desired auth token type
1615 * (see {@link #getAuthToken}), must not be null
1616 * @param features Required features for the account
1617 * (see {@link #getAccountsByTypeAndFeatures}), may be null or empty
1618 * @param activity The {@link Activity} context to use for launching new
1619 * sub-Activities to prompt to add an account, select an account,
1620 * and/or enter a password, as necessary; used only to call
1621 * startActivity(); should not be null
1622 * @param addAccountOptions Authenticator-specific options to use for
1623 * adding new accounts; may be null or empty
1624 * @param getAuthTokenOptions Authenticator-specific options to use for
1625 * getting auth tokens; may be null or empty
1626 * @param callback Callback to invoke when the request completes,
1627 * null for no callback
1628 * @param handler {@link Handler} identifying the callback thread,
1629 * null for the main thread
1630 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1631 * at least the following fields:
Fred Quintana756b7352009-10-21 13:43:10 -07001632 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001633 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account
1634 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1635 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
Fred Quintana756b7352009-10-21 13:43:10 -07001636 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001637 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001638 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001639 * <ul>
1640 * <li> {@link AuthenticatorException} if no authenticator was registered for
1641 * this account type or the authenticator failed to respond
1642 * <li> {@link OperationCanceledException} if the operation was canceled for
1643 * any reason, including the user canceling any operation
1644 * <li> {@link IOException} if the authenticator experienced an I/O problem
1645 * updating settings, usually because of network trouble
1646 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001647 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001648 public AccountManagerFuture<Bundle> getAuthTokenByFeatures(
Fred Quintana33269202009-04-20 16:05:10 -07001649 final String accountType, final String authTokenType, final String[] features,
Dan Egnor661f0132010-02-19 11:23:00 -08001650 final Activity activity, final Bundle addAccountOptions,
Fred Quintana31957f12009-10-21 13:43:10 -07001651 final Bundle getAuthTokenOptions,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001652 final AccountManagerCallback<Bundle> callback, final Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07001653 if (accountType == null) throw new IllegalArgumentException("account type is null");
1654 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001655 final GetAuthTokenByTypeAndFeaturesTask task =
1656 new GetAuthTokenByTypeAndFeaturesTask(accountType, authTokenType, features,
Dan Egnor661f0132010-02-19 11:23:00 -08001657 activity, addAccountOptions, getAuthTokenOptions, callback, handler);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001658 task.start();
1659 return task;
Fred Quintana60307342009-03-24 22:48:12 -07001660 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07001661
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001662 private final HashMap<OnAccountsUpdateListener, Handler> mAccountsUpdatedListeners =
Fred Quintanad9d2f112009-04-23 13:36:27 -07001663 Maps.newHashMap();
1664
Fred Quintanad9d2f112009-04-23 13:36:27 -07001665 /**
1666 * BroadcastReceiver that listens for the LOGIN_ACCOUNTS_CHANGED_ACTION intent
1667 * so that it can read the updated list of accounts and send them to the listener
1668 * in mAccountsUpdatedListeners.
1669 */
1670 private final BroadcastReceiver mAccountsChangedBroadcastReceiver = new BroadcastReceiver() {
1671 public void onReceive(final Context context, final Intent intent) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001672 final Account[] accounts = getAccounts();
1673 // send the result to the listeners
1674 synchronized (mAccountsUpdatedListeners) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001675 for (Map.Entry<OnAccountsUpdateListener, Handler> entry :
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001676 mAccountsUpdatedListeners.entrySet()) {
1677 postToHandler(entry.getValue(), entry.getKey(), accounts);
Fred Quintanad9d2f112009-04-23 13:36:27 -07001678 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001679 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07001680 }
1681 };
1682
1683 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001684 * Adds an {@link OnAccountsUpdateListener} to this instance of the
1685 * {@link AccountManager}. This listener will be notified whenever the
1686 * list of accounts on the device changes.
1687 *
1688 * <p>As long as this listener is present, the AccountManager instance
1689 * will not be garbage-collected, and neither will the {@link Context}
1690 * used to retrieve it, which may be a large Activity instance. To avoid
1691 * memory leaks, you must remove this listener before then. Normally
1692 * listeners are added in an Activity or Service's {@link Activity#onCreate}
1693 * and removed in {@link Activity#onDestroy}.
1694 *
1695 * <p>It is safe to call this method from the main thread.
1696 *
1697 * <p>No permission is required to call this method.
1698 *
1699 * @param listener The listener to send notifications to
1700 * @param handler {@link Handler} identifying the thread to use
1701 * for notifications, null for the main thread
1702 * @param updateImmediately If true, the listener will be invoked
1703 * (on the handler thread) right away with the current account list
Fred Quintanad9d2f112009-04-23 13:36:27 -07001704 * @throws IllegalArgumentException if listener is null
1705 * @throws IllegalStateException if listener was already added
1706 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001707 public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener,
Fred Quintanad9d2f112009-04-23 13:36:27 -07001708 Handler handler, boolean updateImmediately) {
1709 if (listener == null) {
1710 throw new IllegalArgumentException("the listener is null");
1711 }
1712 synchronized (mAccountsUpdatedListeners) {
1713 if (mAccountsUpdatedListeners.containsKey(listener)) {
1714 throw new IllegalStateException("this listener is already added");
1715 }
1716 final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty();
1717
1718 mAccountsUpdatedListeners.put(listener, handler);
1719
1720 if (wasEmpty) {
1721 // Register a broadcast receiver to monitor account changes
1722 IntentFilter intentFilter = new IntentFilter();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001723 intentFilter.addAction(LOGIN_ACCOUNTS_CHANGED_ACTION);
Costin Manolacheb6437242009-09-10 16:14:12 -07001724 // To recover from disk-full.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001725 intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
Fred Quintanad9d2f112009-04-23 13:36:27 -07001726 mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter);
1727 }
1728 }
1729
1730 if (updateImmediately) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07001731 postToHandler(handler, listener, getAccounts());
Fred Quintanad9d2f112009-04-23 13:36:27 -07001732 }
1733 }
1734
1735 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001736 * Removes an {@link OnAccountsUpdateListener} previously registered with
1737 * {@link #addOnAccountsUpdatedListener}. The listener will no longer
1738 * receive notifications of account changes.
1739 *
1740 * <p>It is safe to call this method from the main thread.
1741 *
1742 * <p>No permission is required to call this method.
1743 *
1744 * @param listener The previously added listener to remove
Fred Quintanad9d2f112009-04-23 13:36:27 -07001745 * @throws IllegalArgumentException if listener is null
1746 * @throws IllegalStateException if listener was not already added
1747 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001748 public void removeOnAccountsUpdatedListener(OnAccountsUpdateListener listener) {
Fred Quintana382601f2010-03-25 12:25:10 -07001749 if (listener == null) throw new IllegalArgumentException("listener is null");
Fred Quintanad9d2f112009-04-23 13:36:27 -07001750 synchronized (mAccountsUpdatedListeners) {
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01001751 if (!mAccountsUpdatedListeners.containsKey(listener)) {
Costin Manolache88a211b2009-10-29 11:30:11 -07001752 Log.e(TAG, "Listener was not previously added");
1753 return;
Fred Quintanad9d2f112009-04-23 13:36:27 -07001754 }
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01001755 mAccountsUpdatedListeners.remove(listener);
Fred Quintanad9d2f112009-04-23 13:36:27 -07001756 if (mAccountsUpdatedListeners.isEmpty()) {
1757 mContext.unregisterReceiver(mAccountsChangedBroadcastReceiver);
1758 }
1759 }
1760 }
Fred Quintana60307342009-03-24 22:48:12 -07001761}