blob: a209d2809ac3579a1c605e976aaa69d5023aaee0 [file] [log] [blame]
Fred Quintana60307342009-03-24 22:48:12 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.accounts;
18
Sandra Kwan390c9d22016-01-12 14:13:37 -080019import static android.Manifest.permission.GET_ACCOUNTS;
20
Dmitry Dementyev52745472016-12-02 10:27:45 -080021import android.annotation.IntDef;
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -070022import android.annotation.NonNull;
Tor Norbye80b530a2015-04-23 16:36:09 -070023import android.annotation.RequiresPermission;
Jeff Sharkey32ee8ee2017-03-08 20:17:51 -070024import android.annotation.SdkConstant;
Tor Norbye80b530a2015-04-23 16:36:09 -070025import android.annotation.Size;
Sandra Kwana578d112015-12-16 16:01:43 -080026import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060027import android.annotation.SystemService;
Jeff Sharkey32ee8ee2017-03-08 20:17:51 -070028import android.annotation.SdkConstant.SdkConstantType;
29import android.annotation.BroadcastBehavior;
Fred Quintana60307342009-03-24 22:48:12 -070030import android.app.Activity;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080031import android.content.BroadcastReceiver;
Amith Yamasani12b8e132013-03-14 10:48:07 -070032import android.content.ComponentName;
Fred Quintana60307342009-03-24 22:48:12 -070033import android.content.Context;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080034import android.content.Intent;
Fred Quintanad9d2f112009-04-23 13:36:27 -070035import android.content.IntentFilter;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070036import android.content.IntentSender;
Amith Yamasani12b8e132013-03-14 10:48:07 -070037import android.content.res.Resources;
Dmitry Dementyev52745472016-12-02 10:27:45 -080038import android.content.pm.ApplicationInfo;
39import android.content.pm.PackageManager;
Costin Manolacheb6437242009-09-10 16:14:12 -070040import android.database.SQLException;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080041import android.os.Build;
Fred Quintanaa698f422009-04-08 19:14:54 -070042import android.os.Bundle;
43import android.os.Handler;
44import android.os.Looper;
Fred Quintana33269202009-04-20 16:05:10 -070045import android.os.Parcelable;
Amith Yamasani2c7bc262012-11-05 16:46:02 -080046import android.os.Process;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080047import android.os.RemoteException;
Amith Yamasani2c7bc262012-11-05 16:46:02 -080048import android.os.UserHandle;
Fred Quintanaf0fd8432010-03-08 12:48:05 -080049import android.text.TextUtils;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080050import android.util.Log;
51
52import com.android.internal.R;
53import com.google.android.collect.Maps;
Fred Quintana60307342009-03-24 22:48:12 -070054
Fred Quintanaa698f422009-04-08 19:14:54 -070055import java.io.IOException;
Dmitry Dementyev52745472016-12-02 10:27:45 -080056import java.lang.annotation.Retention;
57import java.lang.annotation.RetentionPolicy;
58import java.lang.SuppressWarnings;
Fred Quintana1121bb52011-09-14 23:19:35 -070059import java.util.ArrayList;
Dmitry Dementyev01985ff2017-01-19 16:03:39 -080060import java.util.Arrays;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080061import java.util.HashMap;
Dmitry Dementyev01985ff2017-01-19 16:03:39 -080062import java.util.HashSet;
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -070063import java.util.List;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080064import java.util.Map;
Dmitry Dementyev01985ff2017-01-19 16:03:39 -080065import java.util.Set;
Fred Quintanaa698f422009-04-08 19:14:54 -070066import java.util.concurrent.Callable;
67import java.util.concurrent.CancellationException;
68import java.util.concurrent.ExecutionException;
69import java.util.concurrent.FutureTask;
Fred Quintanaa698f422009-04-08 19:14:54 -070070import java.util.concurrent.TimeUnit;
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -080071import java.util.concurrent.TimeoutException;
Fred Quintana60307342009-03-24 22:48:12 -070072
73/**
Dan Egnor661f0132010-02-19 11:23:00 -080074 * This class provides access to a centralized registry of the user's
Dan Egnor8e4378b2010-08-02 18:22:09 -070075 * online accounts. The user enters credentials (username and password) once
76 * per account, granting applications access to online resources with
77 * "one-click" approval.
Fred Quintana60307342009-03-24 22:48:12 -070078 *
Dan Egnor661f0132010-02-19 11:23:00 -080079 * <p>Different online services have different ways of handling accounts and
80 * authentication, so the account manager uses pluggable <em>authenticator</em>
Dan Egnor8e4378b2010-08-02 18:22:09 -070081 * modules for different <em>account types</em>. Authenticators (which may be
82 * written by third parties) handle the actual details of validating account
83 * credentials and storing account information. For example, Google, Facebook,
84 * and Microsoft Exchange each have their own authenticator.
Dan Egnor661f0132010-02-19 11:23:00 -080085 *
86 * <p>Many servers support some notion of an <em>authentication token</em>,
87 * which can be used to authenticate a request to the server without sending
88 * the user's actual password. (Auth tokens are normally created with a
89 * separate request which does include the user's credentials.) AccountManager
Dan Egnor8e4378b2010-08-02 18:22:09 -070090 * can generate auth tokens for applications, so the application doesn't need to
91 * handle passwords directly. Auth tokens are normally reusable and cached by
92 * AccountManager, but must be refreshed periodically. It's the responsibility
93 * of applications to <em>invalidate</em> auth tokens when they stop working so
94 * the AccountManager knows it needs to regenerate them.
Dan Egnor661f0132010-02-19 11:23:00 -080095 *
96 * <p>Applications accessing a server normally go through these steps:
97 *
98 * <ul>
99 * <li>Get an instance of AccountManager using {@link #get(Context)}.
100 *
101 * <li>List the available accounts using {@link #getAccountsByType} or
102 * {@link #getAccountsByTypeAndFeatures}. Normally applications will only
103 * be interested in accounts with one particular <em>type</em>, which
104 * identifies the authenticator. Account <em>features</em> are used to
105 * identify particular account subtypes and capabilities. Both the account
106 * type and features are authenticator-specific strings, and must be known by
107 * the application in coordination with its preferred authenticators.
108 *
109 * <li>Select one or more of the available accounts, possibly by asking the
110 * user for their preference. If no suitable accounts are available,
111 * {@link #addAccount} may be called to prompt the user to create an
112 * account of the appropriate type.
113 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700114 * <li><b>Important:</b> If the application is using a previously remembered
115 * account selection, it must make sure the account is still in the list
116 * of accounts returned by {@link #getAccountsByType}. Requesting an auth token
117 * for an account no longer on the device results in an undefined failure.
118 *
Dan Egnor661f0132010-02-19 11:23:00 -0800119 * <li>Request an auth token for the selected account(s) using one of the
120 * {@link #getAuthToken} methods or related helpers. Refer to the description
121 * of each method for exact usage and error handling details.
122 *
123 * <li>Make the request using the auth token. The form of the auth token,
124 * the format of the request, and the protocol used are all specific to the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700125 * service you are accessing. The application may use whatever network and
126 * protocol libraries are useful.
Dan Egnor661f0132010-02-19 11:23:00 -0800127 *
128 * <li><b>Important:</b> If the request fails with an authentication error,
129 * it could be that a cached auth token is stale and no longer honored by
130 * the server. The application must call {@link #invalidateAuthToken} to remove
131 * the token from the cache, otherwise requests will continue failing! After
132 * invalidating the auth token, immediately go back to the "Request an auth
133 * token" step above. If the process fails the second time, then it can be
134 * treated as a "genuine" authentication failure and the user notified or other
135 * appropriate actions taken.
136 * </ul>
137 *
Dan Egnor8e4378b2010-08-02 18:22:09 -0700138 * <p>Some AccountManager methods may need to interact with the user to
Dan Egnor661f0132010-02-19 11:23:00 -0800139 * prompt for credentials, present options, or ask the user to add an account.
140 * The caller may choose whether to allow AccountManager to directly launch the
141 * necessary user interface and wait for the user, or to return an Intent which
142 * the caller may use to launch the interface, or (in some cases) to install a
143 * notification which the user can select at any time to launch the interface.
144 * To have AccountManager launch the interface directly, the caller must supply
145 * the current foreground {@link Activity} context.
146 *
147 * <p>Many AccountManager methods take {@link AccountManagerCallback} and
Dan Egnor8e4378b2010-08-02 18:22:09 -0700148 * {@link Handler} as parameters. These methods return immediately and
Dan Egnor661f0132010-02-19 11:23:00 -0800149 * run asynchronously. If a callback is provided then
150 * {@link AccountManagerCallback#run} will be invoked on the Handler's
151 * thread when the request completes, successfully or not.
Dan Egnor8e4378b2010-08-02 18:22:09 -0700152 * The result is retrieved by calling {@link AccountManagerFuture#getResult()}
153 * on the {@link AccountManagerFuture} returned by the method (and also passed
154 * to the callback). This method waits for the operation to complete (if
155 * necessary) and either returns the result or throws an exception if an error
156 * occurred during the operation. To make the request synchronously, call
Dan Egnor661f0132010-02-19 11:23:00 -0800157 * {@link AccountManagerFuture#getResult()} immediately on receiving the
Dan Egnor8e4378b2010-08-02 18:22:09 -0700158 * future from the method; no callback need be supplied.
Dan Egnor661f0132010-02-19 11:23:00 -0800159 *
160 * <p>Requests which may block, including
161 * {@link AccountManagerFuture#getResult()}, must never be called on
162 * the application's main event thread. These operations throw
163 * {@link IllegalStateException} if they are used on the main thread.
Fred Quintana60307342009-03-24 22:48:12 -0700164 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600165@SystemService(Context.ACCOUNT_SERVICE)
Fred Quintana60307342009-03-24 22:48:12 -0700166public class AccountManager {
Dmitry Dementyev52745472016-12-02 10:27:45 -0800167
Fred Quintana60307342009-03-24 22:48:12 -0700168 private static final String TAG = "AccountManager";
169
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700170 public static final int ERROR_CODE_REMOTE_EXCEPTION = 1;
171 public static final int ERROR_CODE_NETWORK_ERROR = 3;
172 public static final int ERROR_CODE_CANCELED = 4;
173 public static final int ERROR_CODE_INVALID_RESPONSE = 5;
174 public static final int ERROR_CODE_UNSUPPORTED_OPERATION = 6;
175 public static final int ERROR_CODE_BAD_ARGUMENTS = 7;
176 public static final int ERROR_CODE_BAD_REQUEST = 8;
Jatin Lodhia3df7d692013-03-27 10:57:23 -0700177 public static final int ERROR_CODE_BAD_AUTHENTICATION = 9;
Fred Quintana756b7352009-10-21 13:43:10 -0700178
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800179 /** @hide */
180 public static final int ERROR_CODE_USER_RESTRICTED = 100;
Alexandra Gherghina999d3942014-07-03 11:40:08 +0100181 /** @hide */
182 public static final int ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE = 101;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800183
Dan Egnor661f0132010-02-19 11:23:00 -0800184 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700185 * Bundle key used for the {@link String} account name in results
Dan Egnor661f0132010-02-19 11:23:00 -0800186 * from methods which return information about a particular account.
187 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700188 public static final String KEY_ACCOUNT_NAME = "authAccount";
Dan Egnor661f0132010-02-19 11:23:00 -0800189
190 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700191 * Bundle key used for the {@link String} account type in results
Dan Egnor661f0132010-02-19 11:23:00 -0800192 * from methods which return information about a particular account.
193 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700194 public static final String KEY_ACCOUNT_TYPE = "accountType";
Dan Egnor661f0132010-02-19 11:23:00 -0800195
196 /**
Svet Ganovd4696122016-09-24 13:57:45 -0700197 * Bundle key used for the account access id used for noting the
198 * account was accessed when unmarshaled from a parcel.
Svet Ganovf6d424f12016-09-20 20:18:53 -0700199 *
200 * @hide
201 */
Svet Ganovd4696122016-09-24 13:57:45 -0700202 public static final String KEY_ACCOUNT_ACCESS_ID = "accountAccessId";
Svet Ganovf6d424f12016-09-20 20:18:53 -0700203
204 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700205 * Bundle key used for the auth token value in results
Dan Egnor661f0132010-02-19 11:23:00 -0800206 * from {@link #getAuthToken} and friends.
207 */
208 public static final String KEY_AUTHTOKEN = "authtoken";
209
210 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700211 * Bundle key used for an {@link Intent} in results from methods that
Dan Egnor661f0132010-02-19 11:23:00 -0800212 * may require the caller to interact with the user. The Intent can
213 * be used to start the corresponding user interface activity.
214 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700215 public static final String KEY_INTENT = "intent";
Dan Egnor661f0132010-02-19 11:23:00 -0800216
217 /**
Dan Egnor8e4378b2010-08-02 18:22:09 -0700218 * Bundle key used to supply the password directly in options to
Dan Egnor661f0132010-02-19 11:23:00 -0800219 * {@link #confirmCredentials}, rather than prompting the user with
220 * the standard password prompt.
221 */
222 public static final String KEY_PASSWORD = "password";
223
224 public static final String KEY_ACCOUNTS = "accounts";
Brian Carlstrom46703b02011-04-06 15:41:29 -0700225
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700226 public static final String KEY_ACCOUNT_AUTHENTICATOR_RESPONSE = "accountAuthenticatorResponse";
227 public static final String KEY_ACCOUNT_MANAGER_RESPONSE = "accountManagerResponse";
Dan Egnor661f0132010-02-19 11:23:00 -0800228 public static final String KEY_AUTHENTICATOR_TYPES = "authenticator_types";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700229 public static final String KEY_AUTH_FAILED_MESSAGE = "authFailedMessage";
230 public static final String KEY_AUTH_TOKEN_LABEL = "authTokenLabelKey";
Dan Egnor661f0132010-02-19 11:23:00 -0800231 public static final String KEY_BOOLEAN_RESULT = "booleanResult";
232 public static final String KEY_ERROR_CODE = "errorCode";
233 public static final String KEY_ERROR_MESSAGE = "errorMessage";
234 public static final String KEY_USERDATA = "userdata";
Costin Manolacheb61e8fb2011-09-08 11:26:09 -0700235
Costin Manolachea40c6302010-12-13 14:50:45 -0800236 /**
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800237 * Bundle key used to supply the last time the credentials of the account
238 * were authenticated successfully. Time is specified in milliseconds since
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700239 * epoch. Associated time is updated on successful authentication of account
240 * on adding account, confirming credentials, or updating credentials.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800241 */
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -0700242 public static final String KEY_LAST_AUTHENTICATED_TIME = "lastAuthenticatedTime";
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -0800243
244 /**
sunjian87d0c3c2017-07-14 11:54:46 -0700245 * The UID of caller app.
Costin Manolachea40c6302010-12-13 14:50:45 -0800246 */
247 public static final String KEY_CALLER_UID = "callerUid";
sunjian87d0c3c2017-07-14 11:54:46 -0700248
249 /**
250 * The process id of caller app.
251 */
Costin Manolachea40c6302010-12-13 14:50:45 -0800252 public static final String KEY_CALLER_PID = "callerPid";
Dan Egnor661f0132010-02-19 11:23:00 -0800253
Costin Manolached6060452011-01-24 16:11:36 -0800254 /**
Fred Quintanaad93a322011-09-08 13:21:01 -0700255 * The Android package of the caller will be set in the options bundle by the
256 * {@link AccountManager} and will be passed to the AccountManagerService and
257 * to the AccountAuthenticators. The uid of the caller will be known by the
258 * AccountManagerService as well as the AccountAuthenticators so they will be able to
259 * verify that the package is consistent with the uid (a uid might be shared by many
260 * packages).
261 */
262 public static final String KEY_ANDROID_PACKAGE_NAME = "androidPackageName";
263
264 /**
Costin Manolached6060452011-01-24 16:11:36 -0800265 * Boolean, if set and 'customTokens' the authenticator is responsible for
266 * notifications.
267 * @hide
268 */
269 public static final String KEY_NOTIFY_ON_FAILURE = "notifyOnAuthFailure";
270
Sandra Kwan78812282015-11-04 11:19:47 -0800271 /**
272 * Bundle key used for a {@link Bundle} in result from
273 * {@link #startAddAccountSession} and friends which returns session data
274 * for installing an account later.
275 */
276 public static final String KEY_ACCOUNT_SESSION_BUNDLE = "accountSessionBundle";
277
278 /**
279 * Bundle key used for the {@link String} account status token in result
280 * from {@link #startAddAccountSession} and friends which returns
281 * information about a particular account.
282 */
283 public static final String KEY_ACCOUNT_STATUS_TOKEN = "accountStatusToken";
284
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700285 public static final String ACTION_AUTHENTICATOR_INTENT =
286 "android.accounts.AccountAuthenticator";
287 public static final String AUTHENTICATOR_META_DATA_NAME =
Dan Egnor661f0132010-02-19 11:23:00 -0800288 "android.accounts.AccountAuthenticator";
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700289 public static final String AUTHENTICATOR_ATTRIBUTES_NAME = "account-authenticator";
290
Dmitry Dementyev52745472016-12-02 10:27:45 -0800291 /** @hide */
292 @Retention(RetentionPolicy.SOURCE)
293 @IntDef({VISIBILITY_UNDEFINED, VISIBILITY_VISIBLE, VISIBILITY_USER_MANAGED_VISIBLE,
294 VISIBILITY_NOT_VISIBLE, VISIBILITY_USER_MANAGED_NOT_VISIBLE})
295 public @interface AccountVisibility {
296 }
297
298 /**
Dmitry Dementyev06f32e02017-02-16 17:47:48 -0800299 * Account visibility was not set. Default visibility value will be used.
300 * See {@link #PACKAGE_NAME_KEY_LEGACY_VISIBLE}, {@link #PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE}
Dmitry Dementyev52745472016-12-02 10:27:45 -0800301 */
302 public static final int VISIBILITY_UNDEFINED = 0;
303
304 /**
305 * Account is always visible to given application and only authenticator can revoke visibility.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800306 */
307 public static final int VISIBILITY_VISIBLE = 1;
308
309 /**
310 * Account is visible to given application, but user can revoke visibility.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800311 */
312 public static final int VISIBILITY_USER_MANAGED_VISIBLE = 2;
313
314 /**
315 * Account is not visible to given application and only authenticator can grant visibility.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800316 */
317 public static final int VISIBILITY_NOT_VISIBLE = 3;
318
319 /**
320 * Account is not visible to given application, but user can reveal it, for example, using
321 * {@link #newChooseAccountIntent(Account, List, String[], String, String, String[], Bundle)}
Dmitry Dementyev52745472016-12-02 10:27:45 -0800322 */
323 public static final int VISIBILITY_USER_MANAGED_NOT_VISIBLE = 4;
324
325 /**
Svet Ganovf6d424f12016-09-20 20:18:53 -0700326 * Token type for the special case where a UID has access only to an account
327 * but no authenticator specific auth token types.
Dmitry Dementyev2e22cfb2017-01-09 18:42:14 +0000328 *
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700329 * @hide
330 */
Svet Ganovf6d424f12016-09-20 20:18:53 -0700331 public static final String ACCOUNT_ACCESS_TOKEN_TYPE =
332 "com.android.AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE";
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700333
Fred Quintana60307342009-03-24 22:48:12 -0700334 private final Context mContext;
335 private final IAccountManager mService;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700336 private final Handler mMainHandler;
Dan Egnor661f0132010-02-19 11:23:00 -0800337
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700338 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800339 * Action sent as a broadcast Intent by the AccountsService when accounts are added, accounts
340 * are removed, or an account's credentials (saved password, etc) are changed.
Dan Egnor661f0132010-02-19 11:23:00 -0800341 *
342 * @see #addOnAccountsUpdatedListener
Dmitry Dementyeva461e302017-04-12 11:00:48 -0700343 * @see #ACTION_ACCOUNT_REMOVED
Dmitry Dementyev52745472016-12-02 10:27:45 -0800344 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700345 * @deprecated use {@link #addOnAccountsUpdatedListener} to get account updates in runtime.
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700346 */
Jeff Sharkey32ee8ee2017-03-08 20:17:51 -0700347 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
348 @BroadcastBehavior(includeBackground = true)
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700349 public static final String LOGIN_ACCOUNTS_CHANGED_ACTION =
350 "android.accounts.LOGIN_ACCOUNTS_CHANGED";
Fred Quintana60307342009-03-24 22:48:12 -0700351
Fred Quintana33269202009-04-20 16:05:10 -0700352 /**
Dmitry Dementyevb6a7dc02017-04-18 13:43:31 -0700353 * Action sent as a broadcast Intent by the AccountsService when any account is removed
354 * or renamed. Only applications which were able to see the account will receive the intent.
355 * Intent extra will include the following fields:
356 * <ul>
357 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the removed account
358 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
359 * </ul>
Dmitry Dementyeva461e302017-04-12 11:00:48 -0700360 */
361 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
362 @BroadcastBehavior(includeBackground = true)
363 public static final String ACTION_ACCOUNT_REMOVED =
364 "android.accounts.action.ACCOUNT_REMOVED";
365
366 /**
Dmitry Dementyev8882d882017-03-14 17:25:46 -0700367 * Action sent as a broadcast Intent to specific package by the AccountsService
368 * when account visibility or account's credentials (saved password, etc) are changed.
369 *
370 * @see #addOnAccountsUpdatedListener
371 *
372 * @hide
373 */
374 public static final String ACTION_VISIBLE_ACCOUNTS_CHANGED =
375 "android.accounts.action.VISIBLE_ACCOUNTS_CHANGED";
376
377 /**
Dmitry Dementyevd6f06722017-04-05 12:43:26 -0700378 * Key to set visibility for applications which satisfy one of the following conditions:
379 * <ul>
380 * <li>Target API level below {@link android.os.Build.VERSION_CODES#O} and have
381 * deprecated {@link android.Manifest.permission#GET_ACCOUNTS} permission.
382 * </li>
383 * <li> Have {@link android.Manifest.permission#GET_ACCOUNTS_PRIVILEGED} permission. </li>
384 * <li> Have the same signature as authenticator. </li>
385 * <li> Have {@link android.Manifest.permission#READ_CONTACTS} permission and
386 * account type may be associated with contacts data - (verified by
387 * {@link android.Manifest.permission#WRITE_CONTACTS} permission check for the authenticator).
388 * </li>
389 * </ul>
390 * See {@link #getAccountVisibility}. If the value was not set by authenticator
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800391 * {@link #VISIBILITY_USER_MANAGED_VISIBLE} is used.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800392 */
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700393 public static final String PACKAGE_NAME_KEY_LEGACY_VISIBLE =
394 "android:accounts:key_legacy_visible";
395
396 /**
397 * Key to set default visibility for applications which don't satisfy conditions in
398 * {@link PACKAGE_NAME_KEY_LEGACY_VISIBLE}. If the value was not set by authenticator
399 * {@link #VISIBILITY_USER_MANAGED_NOT_VISIBLE} is used.
400 */
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800401 public static final String PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE =
Dmitry Dementyevcbe1bd12017-04-25 17:02:47 -0700402 "android:accounts:key_legacy_not_visible";
Dmitry Dementyev52745472016-12-02 10:27:45 -0800403
404 /**
Fred Quintana33269202009-04-20 16:05:10 -0700405 * @hide
406 */
Fred Quintana60307342009-03-24 22:48:12 -0700407 public AccountManager(Context context, IAccountManager service) {
408 mContext = context;
409 mService = service;
Fred Quintanad9d2f112009-04-23 13:36:27 -0700410 mMainHandler = new Handler(mContext.getMainLooper());
Fred Quintana60307342009-03-24 22:48:12 -0700411 }
412
Fred Quintana0eabf022009-04-27 15:08:17 -0700413 /**
414 * @hide used for testing only
415 */
416 public AccountManager(Context context, IAccountManager service, Handler handler) {
417 mContext = context;
418 mService = service;
419 mMainHandler = handler;
420 }
421
Fred Quintana756b7352009-10-21 13:43:10 -0700422 /**
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800423 * @hide for internal use only
424 */
425 public static Bundle sanitizeResult(Bundle result) {
Fred Quintana382601f2010-03-25 12:25:10 -0700426 if (result != null) {
427 if (result.containsKey(KEY_AUTHTOKEN)
428 && !TextUtils.isEmpty(result.getString(KEY_AUTHTOKEN))) {
429 final Bundle newResult = new Bundle(result);
430 newResult.putString(KEY_AUTHTOKEN, "<omitted for logging purposes>");
431 return newResult;
432 }
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800433 }
434 return result;
435 }
436
437 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800438 * Gets an AccountManager instance associated with a Context.
439 * The {@link Context} will be used as long as the AccountManager is
440 * active, so make sure to use a {@link Context} whose lifetime is
441 * commensurate with any listeners registered to
442 * {@link #addOnAccountsUpdatedListener} or similar methods.
443 *
444 * <p>It is safe to call this method from the main thread.
445 *
446 * <p>No permission is required to call this method.
447 *
Fred Quintana756b7352009-10-21 13:43:10 -0700448 * @param context The {@link Context} to use when necessary
Dan Egnor661f0132010-02-19 11:23:00 -0800449 * @return An {@link AccountManager} instance
Fred Quintana756b7352009-10-21 13:43:10 -0700450 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700451 public static AccountManager get(Context context) {
Fred Quintana382601f2010-03-25 12:25:10 -0700452 if (context == null) throw new IllegalArgumentException("context is null");
Fred Quintanaa698f422009-04-08 19:14:54 -0700453 return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
454 }
455
Fred Quintana756b7352009-10-21 13:43:10 -0700456 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800457 * Gets the saved password associated with the account. This is intended for authenticators and
458 * related code; applications should get an auth token instead.
Dan Egnor661f0132010-02-19 11:23:00 -0800459 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800460 * <p>
461 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800462 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800463 * <p>
464 * This method requires the caller to have a signature match with the authenticator that owns
465 * the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800466 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800467 * <p>
468 * <b>NOTE:</b> If targeting your app to work on API level
469 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before, AUTHENTICATE_ACCOUNTS
470 * permission is needed for those platforms. See docs for this function in API level
471 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700472 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700473 * @param account The account to query for a password. Must not be {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -0800474 * @return The account's password, null if none or if the account doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700475 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700476 public String getPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -0700477 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintana60307342009-03-24 22:48:12 -0700478 try {
479 return mService.getPassword(account);
480 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700481 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700482 }
483 }
484
Fred Quintana756b7352009-10-21 13:43:10 -0700485 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800486 * Gets the user data named by "key" associated with the account. This is intended for
487 * authenticators and related code to store arbitrary metadata along with accounts. The meaning
488 * of the keys and values is up to the authenticator for the account.
Dan Egnor661f0132010-02-19 11:23:00 -0800489 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800490 * <p>
491 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800492 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800493 * <p>
494 * This method requires the caller to have a signature match with the authenticator that owns
495 * the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800496 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800497 * <p>
498 * <b>NOTE:</b> If targeting your app to work on API level
499 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before, AUTHENTICATE_ACCOUNTS
500 * permission is needed for those platforms. See docs for this function in API level
501 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700502 *
Dan Egnor661f0132010-02-19 11:23:00 -0800503 * @param account The account to query for user data
504 * @return The user data, null if the account or key doesn't exist
Fred Quintana756b7352009-10-21 13:43:10 -0700505 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700506 public String getUserData(final Account account, final String key) {
Fred Quintana382601f2010-03-25 12:25:10 -0700507 if (account == null) throw new IllegalArgumentException("account is null");
508 if (key == null) throw new IllegalArgumentException("key is null");
Fred Quintana60307342009-03-24 22:48:12 -0700509 try {
510 return mService.getUserData(account, key);
511 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700512 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700513 }
514 }
515
Fred Quintana756b7352009-10-21 13:43:10 -0700516 /**
Dan Egnor661f0132010-02-19 11:23:00 -0800517 * Lists the currently registered authenticators.
518 *
519 * <p>It is safe to call this method from the main thread.
520 *
521 * <p>No permission is required to call this method.
522 *
523 * @return An array of {@link AuthenticatorDescription} for every
524 * authenticator known to the AccountManager service. Empty (never
525 * null) if no authenticators are known.
Fred Quintana756b7352009-10-21 13:43:10 -0700526 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700527 public AuthenticatorDescription[] getAuthenticatorTypes() {
Fred Quintanaa698f422009-04-08 19:14:54 -0700528 try {
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100529 return mService.getAuthenticatorTypes(UserHandle.getCallingUserId());
530 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700531 throw e.rethrowFromSystemServer();
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100532 }
533 }
534
535 /**
536 * @hide
537 * Lists the currently registered authenticators for a given user id.
538 *
539 * <p>It is safe to call this method from the main thread.
540 *
541 * <p>The caller has to be in the same user or have the permission
542 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
543 *
544 * @return An array of {@link AuthenticatorDescription} for every
545 * authenticator known to the AccountManager service. Empty (never
546 * null) if no authenticators are known.
547 */
548 public AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) {
549 try {
550 return mService.getAuthenticatorTypes(userId);
Fred Quintanaa698f422009-04-08 19:14:54 -0700551 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700552 throw e.rethrowFromSystemServer();
Fred Quintanaa698f422009-04-08 19:14:54 -0700553 }
554 }
555
Fred Quintana756b7352009-10-21 13:43:10 -0700556 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800557 * Lists all accounts visible to the caller regardless of type. Equivalent to
558 * getAccountsByType(null). These accounts may be visible because the user granted access to the
559 * account, or the AbstractAcccountAuthenticator managing the account did so or because the
560 * client shares a signature with the managing AbstractAccountAuthenticator.
Dan Egnor661f0132010-02-19 11:23:00 -0800561 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800562 * <p>
563 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800564 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800565 * @return An array of {@link Account}, one for each account. Empty (never null) if no accounts
566 * have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700567 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700568 @NonNull
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700569 public Account[] getAccounts() {
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000570 try {
571 return mService.getAccounts(null, mContext.getOpPackageName());
572 } catch (RemoteException e) {
573 throw e.rethrowFromSystemServer();
574 }
Fred Quintana60307342009-03-24 22:48:12 -0700575 }
576
Fred Quintana756b7352009-10-21 13:43:10 -0700577 /**
Amith Yamasani27db4682013-03-30 17:07:47 -0700578 * @hide
Dmitry Dementyev52745472016-12-02 10:27:45 -0800579 * Lists all accounts visible to caller regardless of type for a given user id. Equivalent to
580 * getAccountsByType(null).
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100581 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800582 * <p>
583 * It is safe to call this method from the main thread.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100584 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800585 * @return An array of {@link Account}, one for each account. Empty (never null) if no accounts
586 * have been added.
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100587 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700588 @NonNull
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100589 public Account[] getAccountsAsUser(int userId) {
590 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700591 return mService.getAccountsAsUser(null, userId, mContext.getOpPackageName());
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100592 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700593 throw e.rethrowFromSystemServer();
Alexandra Gherghinac1cf1612014-06-05 10:49:14 +0100594 }
595 }
596
597 /**
598 * @hide
Amith Yamasani27db4682013-03-30 17:07:47 -0700599 * For use by internal activities. Returns the list of accounts that the calling package
600 * is authorized to use, particularly for shared accounts.
601 * @param packageName package name of the calling app.
602 * @param uid the uid of the calling app.
603 * @return the accounts that are available to this package and user.
604 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700605 @NonNull
Amith Yamasani27db4682013-03-30 17:07:47 -0700606 public Account[] getAccountsForPackage(String packageName, int uid) {
607 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700608 return mService.getAccountsForPackage(packageName, uid, mContext.getOpPackageName());
Amith Yamasani27db4682013-03-30 17:07:47 -0700609 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700610 throw re.rethrowFromSystemServer();
Amith Yamasani27db4682013-03-30 17:07:47 -0700611 }
612 }
613
614 /**
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700615 * Returns the accounts visible to the specified package in an environment where some apps are
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800616 * not authorized to view all accounts. This method can only be called by system apps and
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700617 * authenticators managing the type.
618 * Beginning API level {@link android.os.Build.VERSION_CODES#O} it also return accounts
619 * which user can make visible to the application (see {@link VISIBILITY_USER_MANAGED_VISIBLE}).
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800620 *
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700621 * @param type The type of accounts to return, null to retrieve all accounts
622 * @param packageName The package name of the app for which the accounts are to be returned
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800623 * @return An array of {@link Account}, one per matching account. Empty (never null) if no
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700624 * accounts of the specified type can be accessed by the package.
625 *
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700626 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700627 @NonNull
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700628 public Account[] getAccountsByTypeForPackage(String type, String packageName) {
629 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700630 return mService.getAccountsByTypeForPackage(type, packageName,
631 mContext.getOpPackageName());
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700632 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700633 throw re.rethrowFromSystemServer();
Amith Yamasani3b458ad2013-04-18 18:40:07 -0700634 }
635 }
636
637 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800638 * Lists all accounts of particular type visible to the caller. These accounts may be visible
639 * because the user granted access to the account, or the AbstractAcccountAuthenticator managing
640 * the account did so or because the client shares a signature with the managing
641 * AbstractAccountAuthenticator.
Dan Egnor661f0132010-02-19 11:23:00 -0800642 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800643 * <p>
644 * The account type is a string token corresponding to the authenticator and useful domain of
645 * the account. For example, there are types corresponding to Google and Facebook. The exact
646 * string token to use will be published somewhere associated with the authenticator in
647 * question.
Dan Egnor661f0132010-02-19 11:23:00 -0800648 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800649 * <p>
650 * It is safe to call this method from the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800651 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800652 * <p>
653 * Caller targeting API level {@link android.os.Build.VERSION_CODES#O} and above, will get list
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700654 * of accounts made visible to it by user
655 * (see {@link #newChooseAccountIntent(Account, List, String[], String,
656 * String, String[], Bundle)}) or AbstractAcccountAuthenticator
657 * using {@link setAccountVisibility}.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800658 * {@link android.Manifest.permission#GET_ACCOUNTS} permission is not used.
659 *
660 * <p>
661 * Caller targeting API level below {@link android.os.Build.VERSION_CODES#O} that have not been
662 * granted the {@link android.Manifest.permission#GET_ACCOUNTS} permission, will only see those
663 * accounts managed by AbstractAccountAuthenticators whose signature matches the client.
664 *
665 * <p>
666 * <b>NOTE:</b> If targeting your app to work on API level
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800667 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before,
668 * {@link android.Manifest.permission#GET_ACCOUNTS} permission is
Dmitry Dementyev52745472016-12-02 10:27:45 -0800669 * needed for those platforms, irrespective of uid or signature match. See docs for this
670 * function in API level {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
Ian Pedowitz358e51f2016-03-15 17:08:27 +0000671 *
672 * @param type The type of accounts to return, null to retrieve all accounts
Dmitry Dementyev52745472016-12-02 10:27:45 -0800673 * @return An array of {@link Account}, one per matching account. Empty (never null) if no
674 * accounts of the specified type have been added.
Fred Quintana756b7352009-10-21 13:43:10 -0700675 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700676 @NonNull
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700677 public Account[] getAccountsByType(String type) {
Amith Yamasani2c7bc262012-11-05 16:46:02 -0800678 return getAccountsByTypeAsUser(type, Process.myUserHandle());
679 }
680
681 /** @hide Same as {@link #getAccountsByType(String)} but for a specific user. */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -0700682 @NonNull
Amith Yamasani2c7bc262012-11-05 16:46:02 -0800683 public Account[] getAccountsByTypeAsUser(String type, UserHandle userHandle) {
Fred Quintana60307342009-03-24 22:48:12 -0700684 try {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700685 return mService.getAccountsAsUser(type, userHandle.getIdentifier(),
686 mContext.getOpPackageName());
Fred Quintana60307342009-03-24 22:48:12 -0700687 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700688 throw e.rethrowFromSystemServer();
Fred Quintana60307342009-03-24 22:48:12 -0700689 }
690 }
691
Fred Quintana756b7352009-10-21 13:43:10 -0700692 /**
Fred Quintanad9640ec2012-05-23 12:37:00 -0700693 * Change whether or not an app (identified by its uid) is allowed to retrieve an authToken
694 * for an account.
695 * <p>
696 * This is only meant to be used by system activities and is not in the SDK.
697 * @param account The account whose permissions are being modified
698 * @param authTokenType The type of token whose permissions are being modified
699 * @param uid The uid that identifies the app which is being granted or revoked permission.
700 * @param value true is permission is being granted, false for revoked
701 * @hide
702 */
703 public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) {
704 try {
705 mService.updateAppPermission(account, authTokenType, uid, value);
706 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700707 throw e.rethrowFromSystemServer();
Fred Quintanad9640ec2012-05-23 12:37:00 -0700708 }
709 }
710
711 /**
712 * Get the user-friendly label associated with an authenticator's auth token.
713 * @param accountType the type of the authenticator. must not be null.
714 * @param authTokenType the token type. must not be null.
715 * @param callback callback to invoke when the result is available. may be null.
716 * @param handler the handler on which to invoke the callback, or null for the main thread
717 * @return a future containing the label string
718 * @hide
719 */
720 public AccountManagerFuture<String> getAuthTokenLabel(
721 final String accountType, final String authTokenType,
722 AccountManagerCallback<String> callback, Handler handler) {
723 if (accountType == null) throw new IllegalArgumentException("accountType is null");
724 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
725 return new Future2Task<String>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800726 @Override
Fred Quintanad9640ec2012-05-23 12:37:00 -0700727 public void doWork() throws RemoteException {
728 mService.getAuthTokenLabel(mResponse, accountType, authTokenType);
729 }
730
731 @Override
732 public String bundleToResult(Bundle bundle) throws AuthenticatorException {
733 if (!bundle.containsKey(KEY_AUTH_TOKEN_LABEL)) {
734 throw new AuthenticatorException("no result in response");
735 }
736 return bundle.getString(KEY_AUTH_TOKEN_LABEL);
737 }
738 }.start();
739 }
740
741 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800742 * Finds out whether a particular account has all the specified features. Account features are
743 * authenticator-specific string tokens identifying boolean account properties. For example,
744 * features are used to tell whether Google accounts have a particular service (such as Google
745 * Calendar or Google Talk) enabled. The feature names and their meanings are published
746 * somewhere associated with the authenticator in question.
Dan Egnor661f0132010-02-19 11:23:00 -0800747 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800748 * <p>
749 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
750 * not be used on the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -0800751 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800752 * <p>
753 * If caller target API level is below {@link android.os.Build.VERSION_CODES#O}, it is
754 * required to hold the permission {@link android.Manifest.permission#GET_ACCOUNTS} or have a
755 * signature match with the AbstractAccountAuthenticator that manages the account.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800756 *
757 * @param account The {@link Account} to test
Dan Egnor661f0132010-02-19 11:23:00 -0800758 * @param features An array of the account features to check
Dmitry Dementyev52745472016-12-02 10:27:45 -0800759 * @param callback Callback to invoke when the request completes, null for no callback
760 * @param handler {@link Handler} identifying the callback thread, null for the main thread
761 * @return An {@link AccountManagerFuture} which resolves to a Boolean, true if the account
762 * exists and has all of the specified features.
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800763 */
Fred Quintana3084a6f2010-01-14 18:02:03 -0800764 public AccountManagerFuture<Boolean> hasFeatures(final Account account,
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800765 final String[] features,
766 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700767 if (account == null) throw new IllegalArgumentException("account is null");
768 if (features == null) throw new IllegalArgumentException("features is null");
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800769 return new Future2Task<Boolean>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800770 @Override
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800771 public void doWork() throws RemoteException {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700772 mService.hasFeatures(mResponse, account, features, mContext.getOpPackageName());
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800773 }
Sandra Kwan0b84b452016-01-20 15:25:42 -0800774 @Override
Fred Quintanabb68a4f2010-01-13 17:28:39 -0800775 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
776 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
777 throw new AuthenticatorException("no result in response");
778 }
779 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
780 }
781 }.start();
782 }
783
784 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800785 * Lists all accounts of a type which have certain features. The account type identifies the
786 * authenticator (see {@link #getAccountsByType}). Account features are authenticator-specific
787 * string tokens identifying boolean account properties (see {@link #hasFeatures}).
Fred Quintana756b7352009-10-21 13:43:10 -0700788 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800789 * <p>
790 * Unlike {@link #getAccountsByType}, this method calls the authenticator, which may contact the
791 * server or do other work to check account features, so the method returns an
792 * {@link AccountManagerFuture}.
Fred Quintanaa698f422009-04-08 19:14:54 -0700793 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800794 * <p>
795 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
796 * not be used on the main thread.
Fred Quintana756b7352009-10-21 13:43:10 -0700797 *
Dmitry Dementyev52745472016-12-02 10:27:45 -0800798 * <p>
799 * Caller targeting API level {@link android.os.Build.VERSION_CODES#O} and above, will get list
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700800 * of accounts made visible to it by user
801 * (see {@link #newChooseAccountIntent(Account, List, String[], String,
802 * String, String[], Bundle)}) or AbstractAcccountAuthenticator
803 * using {@link setAccountVisibility}.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800804 * {@link android.Manifest.permission#GET_ACCOUNTS} permission is not used.
805 *
806 * <p>
807 * Caller targeting API level below {@link android.os.Build.VERSION_CODES#O} that have not been
808 * granted the {@link android.Manifest.permission#GET_ACCOUNTS} permission, will only see those
809 * accounts managed by AbstractAccountAuthenticators whose signature matches the client.
810 * <p>
811 * <b>NOTE:</b> If targeting your app to work on API level
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800812 * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1} and before,
813 * {@link android.Manifest.permission#GET_ACCOUNTS} permission is
Dmitry Dementyev52745472016-12-02 10:27:45 -0800814 * needed for those platforms, irrespective of uid or signature match. See docs for this
815 * function in API level {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
816 *
Fred Quintana756b7352009-10-21 13:43:10 -0700817 *
Dan Egnor661f0132010-02-19 11:23:00 -0800818 * @param type The type of accounts to return, must not be null
Dmitry Dementyev52745472016-12-02 10:27:45 -0800819 * @param features An array of the account features to require, may be null or empty *
820 * @param callback Callback to invoke when the request completes, null for no callback
821 * @param handler {@link Handler} identifying the callback thread, null for the main thread
822 * @return An {@link AccountManagerFuture} which resolves to an array of {@link Account}, one
823 * per account of the specified type which matches the requested features.
Fred Quintana8570f742010-02-18 10:32:54 -0800824 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700825 public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures(
826 final String type, final String[] features,
827 AccountManagerCallback<Account[]> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -0700828 if (type == null) throw new IllegalArgumentException("type is null");
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700829 return new Future2Task<Account[]>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -0800830 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700831 public void doWork() throws RemoteException {
Svetoslavf3f02ac2015-09-08 14:36:35 -0700832 mService.getAccountsByFeatures(mResponse, type, features,
833 mContext.getOpPackageName());
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700834 }
Sandra Kwan0b84b452016-01-20 15:25:42 -0800835 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700836 public Account[] bundleToResult(Bundle bundle) throws AuthenticatorException {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700837 if (!bundle.containsKey(KEY_ACCOUNTS)) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700838 throw new AuthenticatorException("no result in response");
839 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700840 final Parcelable[] parcelables = bundle.getParcelableArray(KEY_ACCOUNTS);
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700841 Account[] descs = new Account[parcelables.length];
842 for (int i = 0; i < parcelables.length; i++) {
843 descs[i] = (Account) parcelables[i];
844 }
845 return descs;
846 }
847 }.start();
848 }
849
Fred Quintana756b7352009-10-21 13:43:10 -0700850 /**
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700851 * Adds an account directly to the AccountManager. Normally used by sign-up
Dan Egnor661f0132010-02-19 11:23:00 -0800852 * wizards associated with authenticators, not directly by applications.
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700853 * <p>Calling this method does not update the last authenticated timestamp,
854 * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
855 * {@link #notifyAccountAuthenticated(Account)} after getting success.
856 * However, if this method is called when it is triggered by addAccount() or
857 * addAccountAsUser() or similar functions, then there is no need to update
858 * timestamp manually as it is updated automatically by framework on
859 * successful completion of the mentioned functions.
Dan Egnor661f0132010-02-19 11:23:00 -0800860 * <p>It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -0700861 * <p>This method requires the caller to have a signature match with the
862 * authenticator that owns the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -0800863 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -0700864 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
865 * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs
866 * for this function in API level 22.
867 *
Dan Egnor661f0132010-02-19 11:23:00 -0800868 * @param account The {@link Account} to add
869 * @param password The password to associate with the account, null for none
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700870 * @param userdata String values to use for the account's userdata, null for
871 * none
Dan Egnor8e4378b2010-08-02 18:22:09 -0700872 * @return True if the account was successfully added, false if the account
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -0700873 * already exists, the account is null, or another error occurs.
Dan Egnor661f0132010-02-19 11:23:00 -0800874 */
875 public boolean addAccountExplicitly(Account account, String password, Bundle userdata) {
Fred Quintana382601f2010-03-25 12:25:10 -0700876 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -0800877 try {
Amith Yamasani27db4682013-03-30 17:07:47 -0700878 return mService.addAccountExplicitly(account, password, userdata);
Dan Egnor661f0132010-02-19 11:23:00 -0800879 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700880 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -0800881 }
882 }
883
884 /**
Dmitry Dementyev85d8a342017-05-02 17:33:17 -0700885 * Adds an account directly to the AccountManager. Additionally it specifies Account visibility
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800886 * for given list of packages.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800887 * <p>
888 * Normally used by sign-up wizards associated with authenticators, not directly by
889 * applications.
890 * <p>
891 * Calling this method does not update the last authenticated timestamp, referred by
892 * {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700893 * {@link #notifyAccountAuthenticated(Account)} after getting success.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800894 * <p>
895 * It is safe to call this method from the main thread.
896 * <p>
897 * This method requires the caller to have a signature match with the authenticator that owns
898 * the specified account.
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700899 *
900 * @param account The {@link Account} to add
901 * @param password The password to associate with the account, null for none
Dmitry Dementyev52745472016-12-02 10:27:45 -0800902 * @param extras String values to use for the account's userdata, null for none
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800903 * @param visibility Map from packageName to visibility values which will be set before account
904 * is added. See {@link #getAccountVisibility} for possible values.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800905 *
906 * @return True if the account was successfully added, false if the account already exists, the
907 * account is null, or another error occurs.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800908 */
909 public boolean addAccountExplicitly(Account account, String password, Bundle extras,
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800910 Map<String, Integer> visibility) {
Dmitry Dementyev52745472016-12-02 10:27:45 -0800911 if (account == null)
912 throw new IllegalArgumentException("account is null");
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700913 try {
Dmitry Dementyev52745472016-12-02 10:27:45 -0800914 return mService.addAccountExplicitlyWithVisibility(account, password, extras,
915 visibility);
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700916 } catch (RemoteException e) {
917 throw e.rethrowFromSystemServer();
918 }
919 }
920
921 /**
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800922 * Returns package names and visibility which were explicitly set for given account.
Dmitry Dementyev2ae64092017-01-09 12:48:18 -0800923 * <p>
924 * This method requires the caller to have a signature match with the authenticator that owns
925 * the specified account.
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700926 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700927 * @param account The account for which visibility data should be returned
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700928 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700929 * @return Map from package names to visibility for given account
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700930 */
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800931 public Map<String, Integer> getPackagesAndVisibilityForAccount(Account account) {
Dmitry Dementyev01985ff2017-01-19 16:03:39 -0800932 try {
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800933 if (account == null)
934 throw new IllegalArgumentException("account is null");
Dmitry Dementyev01985ff2017-01-19 16:03:39 -0800935 @SuppressWarnings("unchecked")
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800936 Map<String, Integer> result = (Map<String, Integer>) mService
937 .getPackagesAndVisibilityForAccount(account);
Dmitry Dementyev01985ff2017-01-19 16:03:39 -0800938 return result;
939 } catch (RemoteException re) {
940 throw re.rethrowFromSystemServer();
941 }
Tejas Khorana5edff3b2016-06-28 20:59:52 -0700942 }
943
944 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -0800945 * Gets all accounts of given type and their visibility for specific package. This method
946 * requires the caller to have a signature match with the authenticator that manages
947 * accountType. It is a helper method which combines calls to {@link #getAccountsByType} by
948 * authenticator and {@link #getAccountVisibility} for every returned account.
949 *
950 * <p>
951 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700952 * @param packageName Package name
953 * @param accountType {@link Account} type
Dmitry Dementyev52745472016-12-02 10:27:45 -0800954 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700955 * @return Map with visibility for all accounts of given type
956 * See {@link #getAccountVisibility} for possible values
Dmitry Dementyev52745472016-12-02 10:27:45 -0800957 */
958 public Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName,
959 String accountType) {
960 try {
961 @SuppressWarnings("unchecked")
962 Map<Account, Integer> result = (Map<Account, Integer>) mService
963 .getAccountsAndVisibilityForPackage(packageName, accountType);
964 return result;
965 } catch (RemoteException re) {
966 throw re.rethrowFromSystemServer();
967 }
968 }
969
970 /**
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700971 * Set visibility value of given account to certain package.
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800972 * Package name must match installed application, or be equal to
973 * {@link #PACKAGE_NAME_KEY_LEGACY_VISIBLE} or {@link #PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE}.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800974 * <p>
Dmitry Dementyev06f32e02017-02-16 17:47:48 -0800975 * Possible visibility values:
976 * <ul>
977 * <li>{@link #VISIBILITY_UNDEFINED}</li>
978 * <li>{@link #VISIBILITY_VISIBLE}</li>
979 * <li>{@link #VISIBILITY_USER_MANAGED_VISIBLE}</li>
980 * <li>{@link #VISIBILITY_NOT_VISIBLE}
981 * <li>{@link #VISIBILITY_USER_MANAGED_NOT_VISIBLE}</li>
982 * </ul>
Dmitry Dementyev52745472016-12-02 10:27:45 -0800983 * <p>
984 * This method requires the caller to have a signature match with the authenticator that owns
985 * the specified account.
986 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700987 * @param account {@link Account} to update visibility
988 * @param packageName Package name of the application to modify account visibility
989 * @param visibility New visibility value
Dmitry Dementyev52745472016-12-02 10:27:45 -0800990 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -0700991 * @return True, if visibility value was successfully updated.
Dmitry Dementyev52745472016-12-02 10:27:45 -0800992 */
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800993 public boolean setAccountVisibility(Account account, String packageName,
Dmitry Dementyev52745472016-12-02 10:27:45 -0800994 @AccountVisibility int visibility) {
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800995 if (account == null)
996 throw new IllegalArgumentException("account is null");
Dmitry Dementyev52745472016-12-02 10:27:45 -0800997 try {
Dmitry Dementyeve366f822017-01-31 10:25:10 -0800998 return mService.setAccountVisibility(account, packageName, visibility);
Dmitry Dementyev52745472016-12-02 10:27:45 -0800999 } catch (RemoteException re) {
1000 throw re.rethrowFromSystemServer();
1001 }
1002 }
1003
1004 /**
Dmitry Dementyeve366f822017-01-31 10:25:10 -08001005 * Get visibility of certain account for given application. Possible returned values are:
Dmitry Dementyev52745472016-12-02 10:27:45 -08001006 * <ul>
1007 * <li>{@link #VISIBILITY_VISIBLE}</li>
1008 * <li>{@link #VISIBILITY_USER_MANAGED_VISIBLE}</li>
1009 * <li>{@link #VISIBILITY_NOT_VISIBLE}
1010 * <li>{@link #VISIBILITY_USER_MANAGED_NOT_VISIBLE}</li>
1011 * </ul>
1012 *
1013 * <p>
1014 * This method requires the caller to have a signature match with the authenticator that owns
1015 * the specified account.
1016 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -07001017 * @param account {@link Account} to get visibility
Dmitry Dementyeve366f822017-01-31 10:25:10 -08001018 * @param packageName Package name of the application to get account visibility
Dmitry Dementyev52745472016-12-02 10:27:45 -08001019 *
Dmitry Dementyev0f8bba02017-03-20 10:57:01 -07001020 * @return int Visibility of given account.
Dmitry Dementyev52745472016-12-02 10:27:45 -08001021 */
Dmitry Dementyeve366f822017-01-31 10:25:10 -08001022 public @AccountVisibility int getAccountVisibility(Account account, String packageName) {
1023 if (account == null)
1024 throw new IllegalArgumentException("account is null");
Dmitry Dementyev52745472016-12-02 10:27:45 -08001025 try {
Dmitry Dementyeve366f822017-01-31 10:25:10 -08001026 return mService.getAccountVisibility(account, packageName);
Tejas Khorana5edff3b2016-06-28 20:59:52 -07001027 } catch (RemoteException re) {
1028 throw re.rethrowFromSystemServer();
1029 }
1030 }
1031
1032 /**
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -07001033 * Notifies the system that the account has just been authenticated. This
1034 * information may be used by other applications to verify the account. This
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001035 * should be called only when the user has entered correct credentials for
1036 * the account.
1037 * <p>
1038 * It is not safe to call this method from the main thread. As such, call it
1039 * from another thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001040 * <p>This method requires the caller to have a signature match with the
1041 * authenticator that owns the specified account.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001042 *
1043 * @param account The {@link Account} to be updated.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001044 * @return boolean {@code true} if the authentication of the account has been successfully
1045 * acknowledged. Otherwise {@code false}.
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001046 */
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -07001047 public boolean notifyAccountAuthenticated(Account account) {
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001048 if (account == null)
1049 throw new IllegalArgumentException("account is null");
1050 try {
1051 return mService.accountAuthenticated(account);
1052 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001053 throw e.rethrowFromSystemServer();
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001054 }
1055 }
1056
1057 /**
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001058 * Rename the specified {@link Account}. This is equivalent to removing
1059 * the existing account and adding a new renamed account with the old
1060 * account's user data.
1061 *
1062 * <p>It is safe to call this method from the main thread.
1063 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001064 * <p>This method requires the caller to have a signature match with the
1065 * authenticator that manages the specified account.
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001066 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001067 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1068 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1069 * is needed for those platforms. See docs for this function in API level 22.
1070 *
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001071 * @param account The {@link Account} to rename
1072 * @param newName String name to be associated with the account.
1073 * @param callback Callback to invoke when the request completes, null for
1074 * no callback
1075 * @param handler {@link Handler} identifying the callback thread, null for
1076 * the main thread
1077 * @return An {@link AccountManagerFuture} which resolves to the Account
1078 * after the name change. If successful the account's name will be the
1079 * specified new name.
1080 */
1081 public AccountManagerFuture<Account> renameAccount(
1082 final Account account,
Tor Norbye80b530a2015-04-23 16:36:09 -07001083 @Size(min = 1) final String newName,
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001084 AccountManagerCallback<Account> callback,
1085 Handler handler) {
1086 if (account == null) throw new IllegalArgumentException("account is null.");
1087 if (TextUtils.isEmpty(newName)) {
1088 throw new IllegalArgumentException("newName is empty or null.");
1089 }
1090 return new Future2Task<Account>(handler, callback) {
1091 @Override
1092 public void doWork() throws RemoteException {
1093 mService.renameAccount(mResponse, account, newName);
1094 }
1095 @Override
1096 public Account bundleToResult(Bundle bundle) throws AuthenticatorException {
1097 String name = bundle.getString(KEY_ACCOUNT_NAME);
1098 String type = bundle.getString(KEY_ACCOUNT_TYPE);
Svet Ganovc1c0d1c2016-09-23 19:15:47 -07001099 String accessId = bundle.getString(KEY_ACCOUNT_ACCESS_ID);
1100 return new Account(name, type, accessId);
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001101 }
1102 }.start();
1103 }
1104
1105 /**
1106 * Gets the previous name associated with the account or {@code null}, if
Dmitry Dementyev8882d882017-03-14 17:25:46 -07001107 * none. This is intended so that clients of
1108 * {@link OnAccountsUpdateListener} can determine if an
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001109 * authenticator has renamed an account.
1110 *
1111 * <p>It is safe to call this method from the main thread.
1112 *
1113 * @param account The account to query for a previous name.
1114 * @return The account's previous name, null if the account has never been
1115 * renamed.
1116 */
1117 public String getPreviousName(final Account account) {
1118 if (account == null) throw new IllegalArgumentException("account is null");
1119 try {
1120 return mService.getPreviousName(account);
1121 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001122 throw e.rethrowFromSystemServer();
Carlos Valdiviaf193b9a2014-07-18 01:34:57 -07001123 }
1124 }
1125
1126 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001127 * Removes an account from the AccountManager. Does nothing if the account
1128 * does not exist. Does not delete the account from the server.
1129 * The authenticator may have its own policies preventing account
1130 * deletion, in which case the account will not be deleted.
1131 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001132 * <p>This method requires the caller to have a signature match with the
1133 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001134 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001135 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1136 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1137 * this function in API level 22.
1138 *
Dan Egnor661f0132010-02-19 11:23:00 -08001139 * @param account The {@link Account} to remove
1140 * @param callback Callback to invoke when the request completes,
1141 * null for no callback
1142 * @param handler {@link Handler} identifying the callback thread,
1143 * null for the main thread
1144 * @return An {@link AccountManagerFuture} which resolves to a Boolean,
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001145 * true if the account has been successfully removed
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001146 * @deprecated use
1147 * {@link #removeAccount(Account, Activity, AccountManagerCallback, Handler)}
1148 * instead
Dan Egnor661f0132010-02-19 11:23:00 -08001149 */
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001150 @Deprecated
Dan Egnor661f0132010-02-19 11:23:00 -08001151 public AccountManagerFuture<Boolean> removeAccount(final Account account,
1152 AccountManagerCallback<Boolean> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001153 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001154 return new Future2Task<Boolean>(handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001155 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001156 public void doWork() throws RemoteException {
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001157 mService.removeAccount(mResponse, account, false);
Dan Egnor661f0132010-02-19 11:23:00 -08001158 }
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001159 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001160 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1161 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1162 throw new AuthenticatorException("no result in response");
1163 }
1164 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1165 }
1166 }.start();
1167 }
1168
1169 /**
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001170 * Removes an account from the AccountManager. Does nothing if the account
1171 * does not exist. Does not delete the account from the server.
1172 * The authenticator may have its own policies preventing account
1173 * deletion, in which case the account will not be deleted.
1174 *
1175 * <p>This method may be called from any thread, but the returned
1176 * {@link AccountManagerFuture} must not be used on the main thread.
1177 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001178 * <p>This method requires the caller to have a signature match with the
1179 * authenticator that manages the specified account.
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001180 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001181 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1182 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1183 * this function in API level 22.
1184 *
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001185 * @param account The {@link Account} to remove
1186 * @param activity The {@link Activity} context to use for launching a new
1187 * authenticator-defined sub-Activity to prompt the user to delete an
1188 * account; used only to call startActivity(); if null, the prompt
1189 * will not be launched directly, but the {@link Intent} may be
1190 * returned to the caller instead
1191 * @param callback Callback to invoke when the request completes,
1192 * null for no callback
1193 * @param handler {@link Handler} identifying the callback thread,
1194 * null for the main thread
1195 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1196 * {@link #KEY_BOOLEAN_RESULT} if activity was specified and an account
1197 * was removed or if active. If no activity was specified, the returned
1198 * Bundle contains only {@link #KEY_INTENT} with the {@link Intent}
1199 * needed to launch the actual account removal process, if authenticator
1200 * needs the activity launch. If an error occurred,
1201 * {@link AccountManagerFuture#getResult()} throws:
1202 * <ul>
1203 * <li> {@link AuthenticatorException} if no authenticator was registered for
1204 * this account type or the authenticator failed to respond
1205 * <li> {@link OperationCanceledException} if the operation was canceled for
1206 * any reason, including the user canceling the creation process or
1207 * adding accounts (of this type) has been disabled by policy
1208 * </ul>
1209 */
1210 public AccountManagerFuture<Bundle> removeAccount(final Account account,
1211 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
1212 if (account == null) throw new IllegalArgumentException("account is null");
1213 return new AmsTask(activity, handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001214 @Override
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001215 public void doWork() throws RemoteException {
1216 mService.removeAccount(mResponse, account, activity != null);
1217 }
1218 }.start();
1219 }
1220
1221 /**
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001222 * @see #removeAccount(Account, AccountManagerCallback, Handler)
1223 * @hide
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001224 * @deprecated use
1225 * {@link #removeAccountAsUser(Account, Activity, AccountManagerCallback, Handler)}
1226 * instead
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001227 */
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001228 @Deprecated
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001229 public AccountManagerFuture<Boolean> removeAccountAsUser(final Account account,
1230 AccountManagerCallback<Boolean> callback, Handler handler,
1231 final UserHandle userHandle) {
1232 if (account == null) throw new IllegalArgumentException("account is null");
1233 if (userHandle == null) throw new IllegalArgumentException("userHandle is null");
1234 return new Future2Task<Boolean>(handler, callback) {
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001235 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001236 public void doWork() throws RemoteException {
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001237 mService.removeAccountAsUser(mResponse, account, false, userHandle.getIdentifier());
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001238 }
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001239 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001240 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1241 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1242 throw new AuthenticatorException("no result in response");
1243 }
1244 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1245 }
1246 }.start();
1247 }
1248
1249 /**
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001250 * @see #removeAccount(Account, Activity, AccountManagerCallback, Handler)
1251 * @hide
1252 */
1253 public AccountManagerFuture<Bundle> removeAccountAsUser(final Account account,
1254 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler,
1255 final UserHandle userHandle) {
1256 if (account == null)
1257 throw new IllegalArgumentException("account is null");
1258 if (userHandle == null)
1259 throw new IllegalArgumentException("userHandle is null");
1260 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001261 @Override
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001262 public void doWork() throws RemoteException {
1263 mService.removeAccountAsUser(mResponse, account, activity != null,
1264 userHandle.getIdentifier());
1265 }
1266 }.start();
1267 }
1268
1269 /**
1270 * Removes an account directly. Normally used by authenticators, not
1271 * directly by applications. Does not delete the account from the server.
1272 * The authenticator may have its own policies preventing account deletion,
1273 * in which case the account will not be deleted.
1274 * <p>
1275 * It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001276 * <p>This method requires the caller to have a signature match with the
1277 * authenticator that manages the specified account.
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001278 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001279 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1280 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1281 * is needed for those platforms. See docs for this function in API level 22.
1282 *
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001283 * @param account The {@link Account} to delete.
1284 * @return True if the account was successfully deleted, false if the
1285 * account did not exist, the account is null, or another error
1286 * occurs.
1287 */
1288 public boolean removeAccountExplicitly(Account account) {
1289 if (account == null) throw new IllegalArgumentException("account is null");
1290 try {
1291 return mService.removeAccountExplicitly(account);
1292 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001293 throw e.rethrowFromSystemServer();
Simranjit Singh Kohli8778f992014-11-05 21:33:55 -08001294 }
1295 }
1296
1297 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001298 * Removes an auth token from the AccountManager's cache. Does nothing if
1299 * the auth token is not currently in the cache. Applications must call this
1300 * method when the auth token is found to have expired or otherwise become
1301 * invalid for authenticating requests. The AccountManager does not validate
1302 * or expire cached auth tokens otherwise.
1303 *
1304 * <p>It is safe to call this method from the main thread.
1305 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001306 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1307 * MANAGE_ACCOUNTS or USE_CREDENTIALS permission is needed for those
1308 * platforms. See docs for this function in API level 22.
1309 *
Fred Quintanaf35b68f2010-04-01 11:36:00 -07001310 * @param accountType The account type of the auth token to invalidate, must not be null
1311 * @param authToken The auth token to invalidate, may be null
Dan Egnor661f0132010-02-19 11:23:00 -08001312 */
1313 public void invalidateAuthToken(final String accountType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -07001314 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001315 try {
Fred Quintanaf35b68f2010-04-01 11:36:00 -07001316 if (authToken != null) {
1317 mService.invalidateAuthToken(accountType, authToken);
1318 }
Dan Egnor661f0132010-02-19 11:23:00 -08001319 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001320 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001321 }
1322 }
1323
1324 /**
1325 * Gets an auth token from the AccountManager's cache. If no auth
1326 * token is cached for this account, null will be returned -- a new
1327 * auth token will not be generated, and the server will not be contacted.
1328 * Intended for use by the authenticator, not directly by applications.
1329 *
1330 * <p>It is safe to call this method from the main thread.
1331 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001332 * <p>This method requires the caller to have a signature match with the
1333 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001334 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001335 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1336 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1337 * is needed for those platforms. See docs for this function in API level 22.
1338 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001339 * @param account The account for which an auth token is to be fetched. Cannot be {@code null}.
Sandra Kwan0b84b452016-01-20 15:25:42 -08001340 * @param authTokenType The type of auth token to fetch. Cannot be {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -08001341 * @return The cached auth token for this account and type, or null if
1342 * no auth token is cached or the account does not exist.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001343 * @see #getAuthToken
Dan Egnor661f0132010-02-19 11:23:00 -08001344 */
1345 public String peekAuthToken(final Account account, final String authTokenType) {
Fred Quintana382601f2010-03-25 12:25:10 -07001346 if (account == null) throw new IllegalArgumentException("account is null");
1347 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001348 try {
1349 return mService.peekAuthToken(account, authTokenType);
1350 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001351 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001352 }
1353 }
1354
1355 /**
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -07001356 * Sets or forgets a saved password. This modifies the local copy of the
1357 * password used to automatically authenticate the user; it does not change
1358 * the user's account password on the server. Intended for use by the
1359 * authenticator, not directly by applications.
1360 * <p>Calling this method does not update the last authenticated timestamp,
1361 * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
1362 * {@link #notifyAccountAuthenticated(Account)} after getting success.
Dan Egnor661f0132010-02-19 11:23:00 -08001363 * <p>It is safe to call this method from the main thread.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001364 * <p>This method requires the caller to have a signature match with the
1365 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001366 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001367 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1368 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1369 * is needed for those platforms. See docs for this function in API level 22.
1370 *
Simranjit Singh Kohli0b8a7c02015-06-19 12:45:27 -07001371 * @param account The account whose password is to be set. Cannot be
1372 * {@code null}.
Dan Egnor661f0132010-02-19 11:23:00 -08001373 * @param password The password to set, null to clear the password
1374 */
1375 public void setPassword(final Account account, final String password) {
Fred Quintana382601f2010-03-25 12:25:10 -07001376 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001377 try {
1378 mService.setPassword(account, password);
1379 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001380 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001381 }
1382 }
1383
1384 /**
1385 * Forgets a saved password. This erases the local copy of the password;
1386 * it does not change the user's account password on the server.
1387 * Has the same effect as setPassword(account, null) but requires fewer
1388 * permissions, and may be used by applications or management interfaces
1389 * to "sign out" from an account.
1390 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001391 * <p>This method only successfully clear the account's password when the
1392 * caller has the same signature as the authenticator that owns the
1393 * specified account. Otherwise, this method will silently fail.
Dan Egnor661f0132010-02-19 11:23:00 -08001394 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001395 * <p>It is safe to call this method from the main thread.
Carlos Valdiviadcddc472015-06-11 20:04:04 +00001396 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001397 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1398 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1399 * this function in API level 22.
1400 *
Dan Egnor661f0132010-02-19 11:23:00 -08001401 * @param account The account whose password to clear
1402 */
1403 public void clearPassword(final Account account) {
Fred Quintana382601f2010-03-25 12:25:10 -07001404 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001405 try {
1406 mService.clearPassword(account);
1407 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001408 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001409 }
1410 }
1411
1412 /**
1413 * Sets one userdata key for an account. Intended by use for the
1414 * authenticator to stash state for itself, not directly by applications.
1415 * The meaning of the keys and values is up to the authenticator.
1416 *
1417 * <p>It is safe to call this method from the main thread.
1418 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001419 * <p>This method requires the caller to have a signature match with the
1420 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001421 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001422 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1423 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1424 * is needed for those platforms. See docs for this function in API level 22.
1425 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001426 * @param account Account whose user data is to be set. Must not be {@code null}.
1427 * @param key String user data key to set. Must not be null
1428 * @param value String value to set, {@code null} to clear this user data key
Dan Egnor661f0132010-02-19 11:23:00 -08001429 */
1430 public void setUserData(final Account account, final String key, final String value) {
Fred Quintana382601f2010-03-25 12:25:10 -07001431 if (account == null) throw new IllegalArgumentException("account is null");
1432 if (key == null) throw new IllegalArgumentException("key is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001433 try {
1434 mService.setUserData(account, key, value);
1435 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001436 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001437 }
1438 }
1439
1440 /**
1441 * Adds an auth token to the AccountManager cache for an account.
1442 * If the account does not exist then this call has no effect.
1443 * Replaces any previous auth token for this account and auth token type.
1444 * Intended for use by the authenticator, not directly by applications.
1445 *
1446 * <p>It is safe to call this method from the main thread.
1447 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001448 * <p>This method requires the caller to have a signature match with the
1449 * authenticator that manages the specified account.
Dan Egnor661f0132010-02-19 11:23:00 -08001450 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001451 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1452 * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator
1453 * is needed for those platforms. See docs for this function in API level 22.
1454 *
Dan Egnor661f0132010-02-19 11:23:00 -08001455 * @param account The account to set an auth token for
1456 * @param authTokenType The type of the auth token, see {#getAuthToken}
1457 * @param authToken The auth token to add to the cache
1458 */
1459 public void setAuthToken(Account account, final String authTokenType, final String authToken) {
Fred Quintana382601f2010-03-25 12:25:10 -07001460 if (account == null) throw new IllegalArgumentException("account is null");
1461 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001462 try {
1463 mService.setAuthToken(account, authTokenType, authToken);
1464 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001465 throw e.rethrowFromSystemServer();
Dan Egnor661f0132010-02-19 11:23:00 -08001466 }
1467 }
1468
1469 /**
1470 * This convenience helper synchronously gets an auth token with
1471 * {@link #getAuthToken(Account, String, boolean, AccountManagerCallback, Handler)}.
1472 *
1473 * <p>This method may block while a network request completes, and must
1474 * never be made from the main thread.
1475 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001476 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1477 * USE_CREDENTIALS permission is needed for those platforms. See docs for
1478 * this function in API level 22.
1479 *
Dan Egnor661f0132010-02-19 11:23:00 -08001480 * @param account The account to fetch an auth token for
Joe Malinb6a35262013-06-03 09:49:04 -07001481 * @param authTokenType The auth token type, see {@link #getAuthToken getAuthToken()}
Dan Egnor661f0132010-02-19 11:23:00 -08001482 * @param notifyAuthFailure If true, display a notification and return null
1483 * if authentication fails; if false, prompt and wait for the user to
1484 * re-enter correct credentials before returning
1485 * @return An auth token of the specified type for this account, or null
1486 * if authentication fails or none can be fetched.
1487 * @throws AuthenticatorException if the authenticator failed to respond
1488 * @throws OperationCanceledException if the request was canceled for any
1489 * reason, including the user canceling a credential request
1490 * @throws java.io.IOException if the authenticator experienced an I/O problem
1491 * creating a new auth token, usually because of network trouble
1492 */
1493 public String blockingGetAuthToken(Account account, String authTokenType,
1494 boolean notifyAuthFailure)
1495 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana382601f2010-03-25 12:25:10 -07001496 if (account == null) throw new IllegalArgumentException("account is null");
1497 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001498 Bundle bundle = getAuthToken(account, authTokenType, notifyAuthFailure, null /* callback */,
1499 null /* handler */).getResult();
Fred Quintana96580e02010-03-04 13:42:42 -08001500 if (bundle == null) {
1501 // This should never happen, but it does, occasionally. If it does return null to
1502 // signify that we were not able to get the authtoken.
1503 // TODO: remove this when the bug is found that sometimes causes a null bundle to be
1504 // returned
1505 Log.e(TAG, "blockingGetAuthToken: null was returned from getResult() for "
1506 + account + ", authTokenType " + authTokenType);
1507 return null;
1508 }
Dan Egnor661f0132010-02-19 11:23:00 -08001509 return bundle.getString(KEY_AUTHTOKEN);
1510 }
1511
1512 /**
1513 * Gets an auth token of the specified type for a particular account,
1514 * prompting the user for credentials if necessary. This method is
1515 * intended for applications running in the foreground where it makes
1516 * sense to ask the user directly for a password.
1517 *
1518 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -07001519 * type, then it is returned. Otherwise, if a saved password is
1520 * available, it is sent to the server to generate a new auth token.
1521 * Otherwise, the user is prompted to enter a password.
Dan Egnor661f0132010-02-19 11:23:00 -08001522 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001523 * <p>Some authenticators have auth token <em>types</em>, whose value
1524 * is authenticator-dependent. Some services use different token types to
1525 * access different functionality -- for example, Google uses different auth
1526 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -08001527 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001528 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
Sandra Kwan0b84b452016-01-20 15:25:42 -08001529 * USE_CREDENTIALS permission is needed for those platforms. See docs for
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001530 * this function in API level 22.
1531 *
Dan Egnor661f0132010-02-19 11:23:00 -08001532 * <p>This method may be called from any thread, but the returned
1533 * {@link AccountManagerFuture} must not be used on the main thread.
1534 *
Dan Egnor661f0132010-02-19 11:23:00 -08001535 * @param account The account to fetch an auth token for
1536 * @param authTokenType The auth token type, an authenticator-dependent
1537 * string token, must not be null
1538 * @param options Authenticator-specific options for the request,
1539 * may be null or empty
1540 * @param activity The {@link Activity} context to use for launching a new
1541 * authenticator-defined sub-Activity to prompt the user for a password
1542 * if necessary; used only to call startActivity(); must not be null.
1543 * @param callback Callback to invoke when the request completes,
1544 * null for no callback
1545 * @param handler {@link Handler} identifying the callback thread,
1546 * null for the main thread
1547 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1548 * at least the following fields:
Fred Quintana756b7352009-10-21 13:43:10 -07001549 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001550 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1551 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1552 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
Fred Quintana756b7352009-10-21 13:43:10 -07001553 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08001554 *
1555 * (Other authenticator-specific values may be returned.) If an auth token
1556 * could not be fetched, {@link AccountManagerFuture#getResult()} throws:
1557 * <ul>
1558 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1559 * <li> {@link OperationCanceledException} if the operation is canceled for
1560 * any reason, incluidng the user canceling a credential request
1561 * <li> {@link IOException} if the authenticator experienced an I/O problem
1562 * creating a new auth token, usually because of network trouble
1563 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -07001564 * If the account is no longer present on the device, the return value is
1565 * authenticator-dependent. The caller should verify the validity of the
1566 * account before requesting an auth token.
Dan Egnor661f0132010-02-19 11:23:00 -08001567 */
1568 public AccountManagerFuture<Bundle> getAuthToken(
1569 final Account account, final String authTokenType, final Bundle options,
1570 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001571 if (account == null) throw new IllegalArgumentException("account is null");
Dan Egnor661f0132010-02-19 11:23:00 -08001572 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001573 final Bundle optionsIn = new Bundle();
1574 if (options != null) {
1575 optionsIn.putAll(options);
1576 }
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001577 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Dan Egnor661f0132010-02-19 11:23:00 -08001578 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001579 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001580 public void doWork() throws RemoteException {
1581 mService.getAuthToken(mResponse, account, authTokenType,
1582 false /* notifyOnAuthFailure */, true /* expectActivityLaunch */,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001583 optionsIn);
Dan Egnor661f0132010-02-19 11:23:00 -08001584 }
1585 }.start();
1586 }
1587
1588 /**
1589 * Gets an auth token of the specified type for a particular account,
1590 * optionally raising a notification if the user must enter credentials.
1591 * This method is intended for background tasks and services where the
1592 * user should not be immediately interrupted with a password prompt.
1593 *
1594 * <p>If a previously generated auth token is cached for this account and
Dan Egnor8e4378b2010-08-02 18:22:09 -07001595 * type, then it is returned. Otherwise, if a saved password is
1596 * available, it is sent to the server to generate a new auth token.
1597 * Otherwise, an {@link Intent} is returned which, when started, will
1598 * prompt the user for a password. If the notifyAuthFailure parameter is
1599 * set, a status bar notification is also created with the same Intent,
Dan Egnor661f0132010-02-19 11:23:00 -08001600 * alerting the user that they need to enter a password at some point.
1601 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001602 * <p>In that case, you may need to wait until the user responds, which
1603 * could take hours or days or forever. When the user does respond and
1604 * supply a new password, the account manager will broadcast the
Dmitry Dementyev8882d882017-03-14 17:25:46 -07001605 * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent and
1606 * notify {@link OnAccountsUpdateListener} which applications can
Dan Egnor8e4378b2010-08-02 18:22:09 -07001607 * use to try again.
Dan Egnor661f0132010-02-19 11:23:00 -08001608 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001609 * <p>If notifyAuthFailure is not set, it is the application's
1610 * responsibility to launch the returned Intent at some point.
1611 * Either way, the result from this call will not wait for user action.
Dan Egnor661f0132010-02-19 11:23:00 -08001612 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001613 * <p>Some authenticators have auth token <em>types</em>, whose value
1614 * is authenticator-dependent. Some services use different token types to
1615 * access different functionality -- for example, Google uses different auth
1616 * tokens to access Gmail and Google Calendar for the same account.
Dan Egnor661f0132010-02-19 11:23:00 -08001617 *
1618 * <p>This method may be called from any thread, but the returned
1619 * {@link AccountManagerFuture} must not be used on the main thread.
1620 *
Dan Egnor661f0132010-02-19 11:23:00 -08001621 * @param account The account to fetch an auth token for
1622 * @param authTokenType The auth token type, an authenticator-dependent
1623 * string token, must not be null
Dan Egnor661f0132010-02-19 11:23:00 -08001624 * @param notifyAuthFailure True to add a notification to prompt the
1625 * user for a password if necessary, false to leave that to the caller
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 on success:
1632 * <ul>
1633 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1634 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1635 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
1636 * </ul>
1637 *
1638 * (Other authenticator-specific values may be returned.) If the user
1639 * must enter credentials, the returned Bundle contains only
1640 * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt.
1641 *
Dan Egnor8e4378b2010-08-02 18:22:09 -07001642 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001643 * <ul>
1644 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1645 * <li> {@link OperationCanceledException} if the operation is canceled for
1646 * any reason, incluidng the user canceling a credential request
1647 * <li> {@link IOException} if the authenticator experienced an I/O problem
1648 * creating a new auth token, usually because of network trouble
1649 * </ul>
Dan Egnor8e4378b2010-08-02 18:22:09 -07001650 * If the account is no longer present on the device, the return value is
1651 * authenticator-dependent. The caller should verify the validity of the
1652 * account before requesting an auth token.
Fred Quintanaad93a322011-09-08 13:21:01 -07001653 * @deprecated use {@link #getAuthToken(Account, String, android.os.Bundle,
1654 * boolean, AccountManagerCallback, android.os.Handler)} instead
Dan Egnor661f0132010-02-19 11:23:00 -08001655 */
Fred Quintanaad93a322011-09-08 13:21:01 -07001656 @Deprecated
Dan Egnor661f0132010-02-19 11:23:00 -08001657 public AccountManagerFuture<Bundle> getAuthToken(
Jatin Lodhia3df7d692013-03-27 10:57:23 -07001658 final Account account, final String authTokenType,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001659 final boolean notifyAuthFailure,
Dan Egnor661f0132010-02-19 11:23:00 -08001660 AccountManagerCallback<Bundle> callback, Handler handler) {
Jatin Lodhia3df7d692013-03-27 10:57:23 -07001661 return getAuthToken(account, authTokenType, null, notifyAuthFailure, callback,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001662 handler);
Dan Egnor661f0132010-02-19 11:23:00 -08001663 }
1664
1665 /**
Fred Quintanaad93a322011-09-08 13:21:01 -07001666 * Gets an auth token of the specified type for a particular account,
1667 * optionally raising a notification if the user must enter credentials.
1668 * This method is intended for background tasks and services where the
1669 * user should not be immediately interrupted with a password prompt.
1670 *
1671 * <p>If a previously generated auth token is cached for this account and
1672 * type, then it is returned. Otherwise, if a saved password is
1673 * available, it is sent to the server to generate a new auth token.
1674 * Otherwise, an {@link Intent} is returned which, when started, will
1675 * prompt the user for a password. If the notifyAuthFailure parameter is
1676 * set, a status bar notification is also created with the same Intent,
1677 * alerting the user that they need to enter a password at some point.
1678 *
1679 * <p>In that case, you may need to wait until the user responds, which
1680 * could take hours or days or forever. When the user does respond and
1681 * supply a new password, the account manager will broadcast the
Dmitry Dementyev8882d882017-03-14 17:25:46 -07001682 * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent and
1683 * notify {@link OnAccountsUpdateListener} which applications can
Fred Quintanaad93a322011-09-08 13:21:01 -07001684 * use to try again.
1685 *
1686 * <p>If notifyAuthFailure is not set, it is the application's
1687 * responsibility to launch the returned Intent at some point.
1688 * Either way, the result from this call will not wait for user action.
1689 *
1690 * <p>Some authenticators have auth token <em>types</em>, whose value
1691 * is authenticator-dependent. Some services use different token types to
1692 * access different functionality -- for example, Google uses different auth
1693 * tokens to access Gmail and Google Calendar for the same account.
1694 *
1695 * <p>This method may be called from any thread, but the returned
1696 * {@link AccountManagerFuture} must not be used on the main thread.
1697 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001698 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
Sandra Kwan0b84b452016-01-20 15:25:42 -08001699 * USE_CREDENTIALS permission is needed for those platforms. See docs for
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001700 * this function in API level 22.
1701 *
Fred Quintanaad93a322011-09-08 13:21:01 -07001702 * @param account The account to fetch an auth token for
1703 * @param authTokenType The auth token type, an authenticator-dependent
1704 * string token, must not be null
1705 * @param options Authenticator-specific options for the request,
1706 * may be null or empty
1707 * @param notifyAuthFailure True to add a notification to prompt the
1708 * user for a password if necessary, false to leave that to the caller
1709 * @param callback Callback to invoke when the request completes,
1710 * null for no callback
1711 * @param handler {@link Handler} identifying the callback thread,
1712 * null for the main thread
1713 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
1714 * at least the following fields on success:
1715 * <ul>
1716 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied
1717 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1718 * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted
1719 * </ul>
1720 *
1721 * (Other authenticator-specific values may be returned.) If the user
1722 * must enter credentials, the returned Bundle contains only
1723 * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt.
1724 *
1725 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
1726 * <ul>
1727 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1728 * <li> {@link OperationCanceledException} if the operation is canceled for
1729 * any reason, incluidng the user canceling a credential request
1730 * <li> {@link IOException} if the authenticator experienced an I/O problem
1731 * creating a new auth token, usually because of network trouble
1732 * </ul>
1733 * If the account is no longer present on the device, the return value is
1734 * authenticator-dependent. The caller should verify the validity of the
1735 * account before requesting an auth token.
1736 */
1737 public AccountManagerFuture<Bundle> getAuthToken(
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001738 final Account account, final String authTokenType, final Bundle options,
1739 final boolean notifyAuthFailure,
Fred Quintanaad93a322011-09-08 13:21:01 -07001740 AccountManagerCallback<Bundle> callback, Handler handler) {
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001741
Fred Quintanaad93a322011-09-08 13:21:01 -07001742 if (account == null) throw new IllegalArgumentException("account is null");
1743 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001744 final Bundle optionsIn = new Bundle();
1745 if (options != null) {
1746 optionsIn.putAll(options);
1747 }
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001748 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Fred Quintanaad93a322011-09-08 13:21:01 -07001749 return new AmsTask(null, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001750 @Override
Fred Quintanaad93a322011-09-08 13:21:01 -07001751 public void doWork() throws RemoteException {
1752 mService.getAuthToken(mResponse, account, authTokenType,
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001753 notifyAuthFailure, false /* expectActivityLaunch */, optionsIn);
Fred Quintanaad93a322011-09-08 13:21:01 -07001754 }
1755 }.start();
1756 }
1757
1758 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001759 * Asks the user to add an account of a specified type. The authenticator
1760 * for this account type processes this request with the appropriate user
1761 * interface. If the user does elect to create a new account, the account
1762 * name is returned.
1763 *
1764 * <p>This method may be called from any thread, but the returned
1765 * {@link AccountManagerFuture} must not be used on the main thread.
Sandra Kwan0b84b452016-01-20 15:25:42 -08001766 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001767 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1768 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
1769 * this function in API level 22.
Dan Egnor661f0132010-02-19 11:23:00 -08001770 *
Dan Egnor661f0132010-02-19 11:23:00 -08001771 * @param accountType The type of account to add; must not be null
1772 * @param authTokenType The type of auth token (see {@link #getAuthToken})
1773 * this account will need to be able to generate, null for none
1774 * @param requiredFeatures The features (see {@link #hasFeatures}) this
1775 * account must have, null for none
1776 * @param addAccountOptions Authenticator-specific options for the request,
1777 * may be null or empty
1778 * @param activity The {@link Activity} context to use for launching a new
1779 * authenticator-defined sub-Activity to prompt the user to create an
1780 * account; used only to call startActivity(); if null, the prompt
1781 * will not be launched directly, but the necessary {@link Intent}
1782 * will be returned to the caller instead
1783 * @param callback Callback to invoke when the request completes,
1784 * null for no callback
1785 * @param handler {@link Handler} identifying the callback thread,
1786 * null for the main thread
Doug Zongkerff592dc2010-02-23 12:26:33 -08001787 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
Dan Egnor661f0132010-02-19 11:23:00 -08001788 * these fields if activity was specified and an account was created:
1789 * <ul>
1790 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created
1791 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1792 * </ul>
1793 *
1794 * If no activity was specified, the returned Bundle contains only
1795 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Dan Egnor8e4378b2010-08-02 18:22:09 -07001796 * actual account creation process. If an error occurred,
1797 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001798 * <ul>
1799 * <li> {@link AuthenticatorException} if no authenticator was registered for
1800 * this account type or the authenticator failed to respond
1801 * <li> {@link OperationCanceledException} if the operation was canceled for
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001802 * any reason, including the user canceling the creation process or adding accounts
1803 * (of this type) has been disabled by policy
Dan Egnor661f0132010-02-19 11:23:00 -08001804 * <li> {@link IOException} if the authenticator experienced an I/O problem
1805 * creating a new account, usually because of network trouble
1806 * </ul>
1807 */
1808 public AccountManagerFuture<Bundle> addAccount(final String accountType,
1809 final String authTokenType, final String[] requiredFeatures,
1810 final Bundle addAccountOptions,
1811 final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07001812 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Costin Manolachee5847ad2011-09-14 12:52:19 -07001813 final Bundle optionsIn = new Bundle();
1814 if (addAccountOptions != null) {
1815 optionsIn.putAll(addAccountOptions);
1816 }
1817 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
Costin Manolacheb61e8fb2011-09-08 11:26:09 -07001818
Dan Egnor661f0132010-02-19 11:23:00 -08001819 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001820 @Override
Dan Egnor661f0132010-02-19 11:23:00 -08001821 public void doWork() throws RemoteException {
Amith Yamasani27db4682013-03-30 17:07:47 -07001822 mService.addAccount(mResponse, accountType, authTokenType,
Costin Manolachee5847ad2011-09-14 12:52:19 -07001823 requiredFeatures, activity != null, optionsIn);
Dan Egnor661f0132010-02-19 11:23:00 -08001824 }
1825 }.start();
1826 }
1827
1828 /**
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001829 * @see #addAccount(String, String, String[], Bundle, Activity, AccountManagerCallback, Handler)
1830 * @hide
1831 */
1832 public AccountManagerFuture<Bundle> addAccountAsUser(final String accountType,
1833 final String authTokenType, final String[] requiredFeatures,
1834 final Bundle addAccountOptions, final Activity activity,
1835 AccountManagerCallback<Bundle> callback, Handler handler, final UserHandle userHandle) {
1836 if (accountType == null) throw new IllegalArgumentException("accountType is null");
1837 if (userHandle == null) throw new IllegalArgumentException("userHandle is null");
1838 final Bundle optionsIn = new Bundle();
1839 if (addAccountOptions != null) {
1840 optionsIn.putAll(addAccountOptions);
1841 }
1842 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
1843
1844 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08001845 @Override
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001846 public void doWork() throws RemoteException {
1847 mService.addAccountAsUser(mResponse, accountType, authTokenType,
1848 requiredFeatures, activity != null, optionsIn, userHandle.getIdentifier());
1849 }
1850 }.start();
1851 }
1852
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001853
Alexandra Gherghina999d3942014-07-03 11:40:08 +01001854 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001855 * Adds shared accounts from a parent user to a secondary user. Adding the shared account
Amith Yamasani67df64b2012-12-14 12:09:36 -08001856 * doesn't take effect immediately. When the target user starts up, any pending shared accounts
1857 * are attempted to be copied to the target user from the primary via calls to the
1858 * authenticator.
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001859 * @param parentUser parent user
1860 * @param user target user
Amith Yamasani67df64b2012-12-14 12:09:36 -08001861 * @hide
1862 */
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001863 public void addSharedAccountsFromParentUser(UserHandle parentUser, UserHandle user) {
Amith Yamasani67df64b2012-12-14 12:09:36 -08001864 try {
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001865 mService.addSharedAccountsFromParentUser(parentUser.getIdentifier(),
Sudheer Shankaf88ebeb2017-02-14 18:30:40 -08001866 user.getIdentifier(), mContext.getOpPackageName());
Amith Yamasani67df64b2012-12-14 12:09:36 -08001867 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001868 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001869 }
1870 }
1871
1872 /**
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001873 * Copies an account from one user to another user.
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001874 * @param account the account to copy
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001875 * @param fromUser the user to copy the account from
1876 * @param toUser the target user
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001877 * @param callback Callback to invoke when the request completes,
1878 * null for no callback
1879 * @param handler {@link Handler} identifying the callback thread,
1880 * null for the main thread
1881 * @return An {@link AccountManagerFuture} which resolves to a Boolean indicated wether it
1882 * succeeded.
1883 * @hide
1884 */
1885 public AccountManagerFuture<Boolean> copyAccountToUser(
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001886 final Account account, final UserHandle fromUser, final UserHandle toUser,
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001887 AccountManagerCallback<Boolean> callback, Handler handler) {
1888 if (account == null) throw new IllegalArgumentException("account is null");
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001889 if (toUser == null || fromUser == null) {
1890 throw new IllegalArgumentException("fromUser and toUser cannot be null");
1891 }
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001892
1893 return new Future2Task<Boolean>(handler, callback) {
1894 @Override
1895 public void doWork() throws RemoteException {
1896 mService.copyAccountToUser(
Xiaohui Chen75f68ce2015-08-14 09:28:07 -07001897 mResponse, account, fromUser.getIdentifier(), toUser.getIdentifier());
Esteban Talavera22dc3b72014-10-31 15:41:12 +00001898 }
1899 @Override
1900 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
1901 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
1902 throw new AuthenticatorException("no result in response");
1903 }
1904 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
1905 }
1906 }.start();
1907 }
1908
1909 /**
Amith Yamasani67df64b2012-12-14 12:09:36 -08001910 * @hide
1911 * Removes the shared account.
1912 * @param account the account to remove
1913 * @param user the user to remove the account from
1914 * @return
1915 */
1916 public boolean removeSharedAccount(final Account account, UserHandle user) {
1917 try {
1918 boolean val = mService.removeSharedAccountAsUser(account, user.getIdentifier());
1919 return val;
1920 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001921 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001922 }
1923 }
1924
1925 /**
1926 * @hide
1927 * @param user
1928 * @return
1929 */
1930 public Account[] getSharedAccounts(UserHandle user) {
1931 try {
1932 return mService.getSharedAccountsAsUser(user.getIdentifier());
1933 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001934 throw re.rethrowFromSystemServer();
Amith Yamasani67df64b2012-12-14 12:09:36 -08001935 }
1936 }
1937
1938 /**
Dan Egnor661f0132010-02-19 11:23:00 -08001939 * Confirms that the user knows the password for an account to make extra
1940 * sure they are the owner of the account. The user-entered password can
1941 * be supplied directly, otherwise the authenticator for this account type
1942 * prompts the user with the appropriate interface. This method is
1943 * intended for applications which want extra assurance; for example, the
1944 * phone lock screen uses this to let the user unlock the phone with an
1945 * account password if they forget the lock pattern.
1946 *
1947 * <p>If the user-entered password matches a saved password for this
1948 * account, the request is considered valid; otherwise the authenticator
1949 * verifies the password (usually by contacting the server).
1950 *
1951 * <p>This method may be called from any thread, but the returned
1952 * {@link AccountManagerFuture} must not be used on the main thread.
1953 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07001954 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
1955 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs
1956 * for this function in API level 22.
1957 *
Dan Egnor661f0132010-02-19 11:23:00 -08001958 * @param account The account to confirm password knowledge for
1959 * @param options Authenticator-specific options for the request;
1960 * if the {@link #KEY_PASSWORD} string field is present, the
1961 * authenticator may use it directly rather than prompting the user;
1962 * may be null or empty
1963 * @param activity The {@link Activity} context to use for launching a new
1964 * authenticator-defined sub-Activity to prompt the user to enter a
1965 * password; used only to call startActivity(); if null, the prompt
1966 * will not be launched directly, but the necessary {@link Intent}
1967 * will be returned to the caller instead
1968 * @param callback Callback to invoke when the request completes,
1969 * null for no callback
1970 * @param handler {@link Handler} identifying the callback thread,
1971 * null for the main thread
1972 * @return An {@link AccountManagerFuture} which resolves to a Bundle
1973 * with these fields if activity or password was supplied and
1974 * the account was successfully verified:
1975 * <ul>
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001976 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account verified
Dan Egnor661f0132010-02-19 11:23:00 -08001977 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
1978 * <li> {@link #KEY_BOOLEAN_RESULT} - true to indicate success
1979 * </ul>
1980 *
1981 * If no activity or password was specified, the returned Bundle contains
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001982 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
1983 * password prompt.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001984 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001985 * <p>Also the returning Bundle may contain {@link
Simranjit Singh Kohli1663b442015-04-28 11:11:12 -07001986 * #KEY_LAST_AUTHENTICATED_TIME} indicating the last time the
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001987 * credential was validated/created.
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07001988 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08001989 * If an error occurred,{@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08001990 * <ul>
1991 * <li> {@link AuthenticatorException} if the authenticator failed to respond
1992 * <li> {@link OperationCanceledException} if the operation was canceled for
1993 * any reason, including the user canceling the password prompt
1994 * <li> {@link IOException} if the authenticator experienced an I/O problem
1995 * verifying the password, usually because of network trouble
1996 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07001997 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07001998 public AccountManagerFuture<Bundle> confirmCredentials(final Account account,
1999 final Bundle options,
2000 final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002001 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07002002 final Handler handler) {
Amith Yamasani2c7bc262012-11-05 16:46:02 -08002003 return confirmCredentialsAsUser(account, options, activity, callback, handler,
2004 Process.myUserHandle());
2005 }
2006
2007 /**
2008 * @hide
2009 * Same as {@link #confirmCredentials(Account, Bundle, Activity, AccountManagerCallback, Handler)}
2010 * but for the specified user.
2011 */
2012 public AccountManagerFuture<Bundle> confirmCredentialsAsUser(final Account account,
2013 final Bundle options,
2014 final Activity activity,
2015 final AccountManagerCallback<Bundle> callback,
2016 final Handler handler, UserHandle userHandle) {
Fred Quintana382601f2010-03-25 12:25:10 -07002017 if (account == null) throw new IllegalArgumentException("account is null");
Amith Yamasani2c7bc262012-11-05 16:46:02 -08002018 final int userId = userHandle.getIdentifier();
Fred Quintanaa698f422009-04-08 19:14:54 -07002019 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002020 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002021 public void doWork() throws RemoteException {
Amith Yamasani2c7bc262012-11-05 16:46:02 -08002022 mService.confirmCredentialsAsUser(mResponse, account, options, activity != null,
2023 userId);
Fred Quintanaa698f422009-04-08 19:14:54 -07002024 }
Fred Quintana33269202009-04-20 16:05:10 -07002025 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07002026 }
2027
Fred Quintana756b7352009-10-21 13:43:10 -07002028 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002029 * Asks the user to enter a new password for an account, updating the
2030 * saved credentials for the account. Normally this happens automatically
2031 * when the server rejects credentials during an auth token fetch, but this
2032 * can be invoked directly to ensure we have the correct credentials stored.
Fred Quintana756b7352009-10-21 13:43:10 -07002033 *
Dan Egnor661f0132010-02-19 11:23:00 -08002034 * <p>This method may be called from any thread, but the returned
2035 * {@link AccountManagerFuture} must not be used on the main thread.
2036 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002037 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
2038 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for
2039 * this function in API level 22.
2040 *
Dan Egnor661f0132010-02-19 11:23:00 -08002041 * @param account The account to update credentials for
2042 * @param authTokenType The credentials entered must allow an auth token
2043 * of this type to be created (but no actual auth token is returned);
2044 * may be null
2045 * @param options Authenticator-specific options for the request;
2046 * may be null or empty
2047 * @param activity The {@link Activity} context to use for launching a new
2048 * authenticator-defined sub-Activity to prompt the user to enter a
2049 * password; used only to call startActivity(); if null, the prompt
2050 * will not be launched directly, but the necessary {@link Intent}
2051 * will be returned to the caller instead
2052 * @param callback Callback to invoke when the request completes,
2053 * null for no callback
2054 * @param handler {@link Handler} identifying the callback thread,
2055 * null for the main thread
2056 * @return An {@link AccountManagerFuture} which resolves to a Bundle
2057 * with these fields if an activity was supplied and the account
2058 * credentials were successfully updated:
Fred Quintana756b7352009-10-21 13:43:10 -07002059 * <ul>
Ian Pedowitz6cc066d2015-08-05 14:23:43 +00002060 * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account
Dan Egnor661f0132010-02-19 11:23:00 -08002061 * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account
Fred Quintana756b7352009-10-21 13:43:10 -07002062 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002063 *
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08002064 * If no activity was specified, the returned Bundle contains
Dan Egnor661f0132010-02-19 11:23:00 -08002065 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
Simranjit Singh Kohli6c7c4ad2015-02-23 18:11:14 -08002066 * password prompt. If an error occurred,
Dan Egnor8e4378b2010-08-02 18:22:09 -07002067 * {@link AccountManagerFuture#getResult()} throws:
Dan Egnor661f0132010-02-19 11:23:00 -08002068 * <ul>
2069 * <li> {@link AuthenticatorException} if the authenticator failed to respond
2070 * <li> {@link OperationCanceledException} if the operation was canceled for
2071 * any reason, including the user canceling the password prompt
2072 * <li> {@link IOException} if the authenticator experienced an I/O problem
2073 * verifying the password, usually because of network trouble
2074 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002075 */
2076 public AccountManagerFuture<Bundle> updateCredentials(final Account account,
2077 final String authTokenType,
Fred Quintana31957f12009-10-21 13:43:10 -07002078 final Bundle options, final Activity activity,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002079 final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07002080 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07002081 if (account == null) throw new IllegalArgumentException("account is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07002082 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002083 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002084 public void doWork() throws RemoteException {
2085 mService.updateCredentials(mResponse, account, authTokenType, activity != null,
Fred Quintana31957f12009-10-21 13:43:10 -07002086 options);
Fred Quintanaa698f422009-04-08 19:14:54 -07002087 }
Fred Quintana33269202009-04-20 16:05:10 -07002088 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07002089 }
2090
Fred Quintana756b7352009-10-21 13:43:10 -07002091 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002092 * Offers the user an opportunity to change an authenticator's settings.
2093 * These properties are for the authenticator in general, not a particular
2094 * account. Not all authenticators support this method.
Fred Quintana756b7352009-10-21 13:43:10 -07002095 *
Dan Egnor661f0132010-02-19 11:23:00 -08002096 * <p>This method may be called from any thread, but the returned
2097 * {@link AccountManagerFuture} must not be used on the main thread.
2098 *
Carlos Valdivia6eb73a52015-06-11 13:07:11 -07002099 * <p>This method requires the caller to have the same signature as the
2100 * authenticator associated with the specified account type.
Dan Egnor661f0132010-02-19 11:23:00 -08002101 *
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002102 * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
2103 * MANAGE_ACCOUNTS permission is needed for those platforms. See docs
2104 * for this function in API level 22.
2105 *
Dan Egnor661f0132010-02-19 11:23:00 -08002106 * @param accountType The account type associated with the authenticator
2107 * to adjust
2108 * @param activity The {@link Activity} context to use for launching a new
2109 * authenticator-defined sub-Activity to adjust authenticator settings;
2110 * used only to call startActivity(); if null, the settings dialog will
2111 * not be launched directly, but the necessary {@link Intent} will be
2112 * returned to the caller instead
2113 * @param callback Callback to invoke when the request completes,
2114 * null for no callback
2115 * @param handler {@link Handler} identifying the callback thread,
2116 * null for the main thread
2117 * @return An {@link AccountManagerFuture} which resolves to a Bundle
2118 * which is empty if properties were edited successfully, or
2119 * if no activity was specified, contains only {@link #KEY_INTENT}
2120 * needed to launch the authenticator's settings dialog.
Dan Egnor8e4378b2010-08-02 18:22:09 -07002121 * If an error occurred, {@link AccountManagerFuture#getResult()}
2122 * throws:
Fred Quintana756b7352009-10-21 13:43:10 -07002123 * <ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002124 * <li> {@link AuthenticatorException} if no authenticator was registered for
2125 * this account type or the authenticator failed to respond
2126 * <li> {@link OperationCanceledException} if the operation was canceled for
2127 * any reason, including the user canceling the settings dialog
2128 * <li> {@link IOException} if the authenticator experienced an I/O problem
2129 * updating settings, usually because of network trouble
Fred Quintana756b7352009-10-21 13:43:10 -07002130 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002131 */
2132 public AccountManagerFuture<Bundle> editProperties(final String accountType,
2133 final Activity activity, final AccountManagerCallback<Bundle> callback,
Fred Quintanaa698f422009-04-08 19:14:54 -07002134 final Handler handler) {
Fred Quintana382601f2010-03-25 12:25:10 -07002135 if (accountType == null) throw new IllegalArgumentException("accountType is null");
Fred Quintanaa698f422009-04-08 19:14:54 -07002136 return new AmsTask(activity, handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002137 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002138 public void doWork() throws RemoteException {
2139 mService.editProperties(mResponse, accountType, activity != null);
2140 }
Fred Quintana33269202009-04-20 16:05:10 -07002141 }.start();
Fred Quintanaa698f422009-04-08 19:14:54 -07002142 }
2143
Amith Yamasani12747872015-12-07 14:19:49 -08002144 /**
2145 * @hide
2146 * Checks if the given account exists on any of the users on the device.
2147 * Only the system process can call this method.
2148 *
2149 * @param account The account to check for existence.
2150 * @return whether any user has this account
2151 */
2152 public boolean someUserHasAccount(@NonNull final Account account) {
2153 try {
2154 return mService.someUserHasAccount(account);
2155 } catch (RemoteException re) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002156 throw re.rethrowFromSystemServer();
Amith Yamasani12747872015-12-07 14:19:49 -08002157 }
2158 }
2159
Fred Quintanaa698f422009-04-08 19:14:54 -07002160 private void ensureNotOnMainThread() {
2161 final Looper looper = Looper.myLooper();
2162 if (looper != null && looper == mContext.getMainLooper()) {
Fred Quintana53bd2522010-02-05 15:28:12 -08002163 final IllegalStateException exception = new IllegalStateException(
2164 "calling this from your main thread can lead to deadlock");
2165 Log.e(TAG, "calling this from your main thread can lead to deadlock and/or ANRs",
2166 exception);
Fred Quintana751fdc02010-02-09 14:13:18 -08002167 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.FROYO) {
2168 throw exception;
2169 }
Fred Quintana60307342009-03-24 22:48:12 -07002170 }
2171 }
2172
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002173 private void postToHandler(Handler handler, final AccountManagerCallback<Bundle> callback,
2174 final AccountManagerFuture<Bundle> future) {
Fred Quintanad9d2f112009-04-23 13:36:27 -07002175 handler = handler == null ? mMainHandler : handler;
2176 handler.post(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002177 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002178 public void run() {
2179 callback.run(future);
2180 }
2181 });
2182 }
Fred Quintana60307342009-03-24 22:48:12 -07002183
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002184 private void postToHandler(Handler handler, final OnAccountsUpdateListener listener,
Fred Quintanad9d2f112009-04-23 13:36:27 -07002185 final Account[] accounts) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002186 final Account[] accountsCopy = new Account[accounts.length];
2187 // send a copy to make sure that one doesn't
2188 // change what another sees
2189 System.arraycopy(accounts, 0, accountsCopy, 0, accountsCopy.length);
2190 handler = (handler == null) ? mMainHandler : handler;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002191 handler.post(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002192 @Override
Fred Quintanad9d2f112009-04-23 13:36:27 -07002193 public void run() {
Hiroaki Kuriyama8d3e1592015-10-09 16:30:03 +02002194 synchronized (mAccountsUpdatedListeners) {
2195 try {
2196 if (mAccountsUpdatedListeners.containsKey(listener)) {
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002197 Set<String> types = mAccountsUpdatedListenersTypes.get(listener);
2198 if (types != null) {
2199 // filter by account type;
2200 ArrayList<Account> filtered = new ArrayList<>();
2201 for (Account account : accountsCopy) {
2202 if (types.contains(account.type)) {
2203 filtered.add(account);
2204 }
2205 }
2206 listener.onAccountsUpdated(
2207 filtered.toArray(new Account[filtered.size()]));
2208 } else {
2209 listener.onAccountsUpdated(accountsCopy);
2210 }
Hiroaki Kuriyama8d3e1592015-10-09 16:30:03 +02002211 }
2212 } catch (SQLException e) {
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002213 // Better luck next time. If the problem was disk-full,
Hiroaki Kuriyama8d3e1592015-10-09 16:30:03 +02002214 // the STORAGE_OK intent will re-trigger the update.
2215 Log.e(TAG, "Can't update accounts", e);
2216 }
Costin Manolacheb6437242009-09-10 16:14:12 -07002217 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002218 }
2219 });
2220 }
2221
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002222 private abstract class AmsTask extends FutureTask<Bundle> implements AccountManagerFuture<Bundle> {
Fred Quintanaa698f422009-04-08 19:14:54 -07002223 final IAccountManagerResponse mResponse;
2224 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002225 final AccountManagerCallback<Bundle> mCallback;
Fred Quintanaa698f422009-04-08 19:14:54 -07002226 final Activity mActivity;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002227 public AmsTask(Activity activity, Handler handler, AccountManagerCallback<Bundle> callback) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002228 super(new Callable<Bundle>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002229 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002230 public Bundle call() throws Exception {
2231 throw new IllegalStateException("this should never be called");
2232 }
2233 });
2234
2235 mHandler = handler;
2236 mCallback = callback;
2237 mActivity = activity;
2238 mResponse = new Response();
Fred Quintana33269202009-04-20 16:05:10 -07002239 }
2240
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002241 public final AccountManagerFuture<Bundle> start() {
2242 try {
2243 doWork();
2244 } catch (RemoteException e) {
2245 setException(e);
2246 }
Fred Quintana33269202009-04-20 16:05:10 -07002247 return this;
Fred Quintana60307342009-03-24 22:48:12 -07002248 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002249
Sandra Kwan0b84b452016-01-20 15:25:42 -08002250 @Override
Fred Quintana96580e02010-03-04 13:42:42 -08002251 protected void set(Bundle bundle) {
2252 // TODO: somehow a null is being set as the result of the Future. Log this
2253 // case to help debug where this is occurring. When this bug is fixed this
2254 // condition statement should be removed.
2255 if (bundle == null) {
2256 Log.e(TAG, "the bundle must not be null", new Exception());
2257 }
2258 super.set(bundle);
2259 }
2260
Fred Quintanaa698f422009-04-08 19:14:54 -07002261 public abstract void doWork() throws RemoteException;
2262
2263 private Bundle internalGetResult(Long timeout, TimeUnit unit)
2264 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08002265 if (!isDone()) {
2266 ensureNotOnMainThread();
2267 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002268 try {
2269 if (timeout == null) {
2270 return get();
2271 } else {
2272 return get(timeout, unit);
2273 }
2274 } catch (CancellationException e) {
2275 throw new OperationCanceledException();
2276 } catch (TimeoutException e) {
2277 // fall through and cancel
2278 } catch (InterruptedException e) {
2279 // fall through and cancel
2280 } catch (ExecutionException e) {
2281 final Throwable cause = e.getCause();
2282 if (cause instanceof IOException) {
2283 throw (IOException) cause;
2284 } else if (cause instanceof UnsupportedOperationException) {
2285 throw new AuthenticatorException(cause);
2286 } else if (cause instanceof AuthenticatorException) {
2287 throw (AuthenticatorException) cause;
2288 } else if (cause instanceof RuntimeException) {
2289 throw (RuntimeException) cause;
2290 } else if (cause instanceof Error) {
2291 throw (Error) cause;
2292 } else {
2293 throw new IllegalStateException(cause);
2294 }
2295 } finally {
2296 cancel(true /* interrupt if running */);
2297 }
2298 throw new OperationCanceledException();
2299 }
2300
Sandra Kwan0b84b452016-01-20 15:25:42 -08002301 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002302 public Bundle getResult()
2303 throws OperationCanceledException, IOException, AuthenticatorException {
2304 return internalGetResult(null, null);
2305 }
2306
Sandra Kwan0b84b452016-01-20 15:25:42 -08002307 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002308 public Bundle getResult(long timeout, TimeUnit unit)
2309 throws OperationCanceledException, IOException, AuthenticatorException {
2310 return internalGetResult(timeout, unit);
2311 }
2312
Sandra Kwan0b84b452016-01-20 15:25:42 -08002313 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002314 protected void done() {
2315 if (mCallback != null) {
2316 postToHandler(mHandler, mCallback, this);
2317 }
2318 }
2319
2320 /** Handles the responses from the AccountManager */
2321 private class Response extends IAccountManagerResponse.Stub {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002322 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002323 public void onResult(Bundle bundle) {
Brian Carlstrom46703b02011-04-06 15:41:29 -07002324 Intent intent = bundle.getParcelable(KEY_INTENT);
Fred Quintanaa698f422009-04-08 19:14:54 -07002325 if (intent != null && mActivity != null) {
2326 // since the user provided an Activity we will silently start intents
2327 // that we see
2328 mActivity.startActivity(intent);
2329 // leave the Future running to wait for the real response to this request
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07002330 } else if (bundle.getBoolean("retry")) {
2331 try {
2332 doWork();
2333 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002334 throw e.rethrowFromSystemServer();
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07002335 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002336 } else {
2337 set(bundle);
2338 }
2339 }
2340
Sandra Kwan0b84b452016-01-20 15:25:42 -08002341 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002342 public void onError(int code, String message) {
Alexandra Gherghina999d3942014-07-03 11:40:08 +01002343 if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED
2344 || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) {
2345 // the authenticator indicated that this request was canceled or we were
2346 // forbidden to fulfill; cancel now
Fred Quintanaa698f422009-04-08 19:14:54 -07002347 cancel(true /* mayInterruptIfRunning */);
2348 return;
2349 }
2350 setException(convertErrorToException(code, message));
2351 }
2352 }
2353
Fred Quintana60307342009-03-24 22:48:12 -07002354 }
2355
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002356 private abstract class BaseFutureTask<T> extends FutureTask<T> {
2357 final public IAccountManagerResponse mResponse;
Fred Quintanaa698f422009-04-08 19:14:54 -07002358 final Handler mHandler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002359
2360 public BaseFutureTask(Handler handler) {
2361 super(new Callable<T>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002362 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002363 public T call() throws Exception {
Fred Quintanaa698f422009-04-08 19:14:54 -07002364 throw new IllegalStateException("this should never be called");
2365 }
2366 });
Fred Quintanaa698f422009-04-08 19:14:54 -07002367 mHandler = handler;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002368 mResponse = new Response();
Fred Quintana60307342009-03-24 22:48:12 -07002369 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002370
2371 public abstract void doWork() throws RemoteException;
2372
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002373 public abstract T bundleToResult(Bundle bundle) throws AuthenticatorException;
Fred Quintanaa698f422009-04-08 19:14:54 -07002374
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002375 protected void postRunnableToHandler(Runnable runnable) {
2376 Handler handler = (mHandler == null) ? mMainHandler : mHandler;
2377 handler.post(runnable);
Fred Quintanaa698f422009-04-08 19:14:54 -07002378 }
2379
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002380 protected void startTask() {
Fred Quintanaa698f422009-04-08 19:14:54 -07002381 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002382 doWork();
2383 } catch (RemoteException e) {
2384 setException(e);
Fred Quintanaa698f422009-04-08 19:14:54 -07002385 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002386 }
2387
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002388 protected class Response extends IAccountManagerResponse.Stub {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002389 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002390 public void onResult(Bundle bundle) {
2391 try {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002392 T result = bundleToResult(bundle);
2393 if (result == null) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002394 return;
2395 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002396 set(result);
2397 return;
Fred Quintanaa698f422009-04-08 19:14:54 -07002398 } catch (ClassCastException e) {
2399 // we will set the exception below
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002400 } catch (AuthenticatorException e) {
2401 // we will set the exception below
Fred Quintanaa698f422009-04-08 19:14:54 -07002402 }
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002403 onError(ERROR_CODE_INVALID_RESPONSE, "no result in response");
Fred Quintanaa698f422009-04-08 19:14:54 -07002404 }
2405
Sandra Kwan0b84b452016-01-20 15:25:42 -08002406 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -07002407 public void onError(int code, String message) {
Alexandra Gherghina999d3942014-07-03 11:40:08 +01002408 if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED
2409 || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) {
2410 // the authenticator indicated that this request was canceled or we were
2411 // forbidden to fulfill; cancel now
Fred Quintanaa698f422009-04-08 19:14:54 -07002412 cancel(true /* mayInterruptIfRunning */);
2413 return;
2414 }
2415 setException(convertErrorToException(code, message));
2416 }
2417 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002418 }
2419
2420 private abstract class Future2Task<T>
2421 extends BaseFutureTask<T> implements AccountManagerFuture<T> {
2422 final AccountManagerCallback<T> mCallback;
2423 public Future2Task(Handler handler, AccountManagerCallback<T> callback) {
2424 super(handler);
2425 mCallback = callback;
2426 }
2427
Sandra Kwan0b84b452016-01-20 15:25:42 -08002428 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002429 protected void done() {
2430 if (mCallback != null) {
2431 postRunnableToHandler(new Runnable() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002432 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002433 public void run() {
2434 mCallback.run(Future2Task.this);
2435 }
2436 });
2437 }
2438 }
2439
2440 public Future2Task<T> start() {
2441 startTask();
2442 return this;
2443 }
2444
2445 private T internalGetResult(Long timeout, TimeUnit unit)
2446 throws OperationCanceledException, IOException, AuthenticatorException {
Fred Quintana53bd2522010-02-05 15:28:12 -08002447 if (!isDone()) {
2448 ensureNotOnMainThread();
2449 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002450 try {
2451 if (timeout == null) {
2452 return get();
2453 } else {
2454 return get(timeout, unit);
2455 }
2456 } catch (InterruptedException e) {
2457 // fall through and cancel
2458 } catch (TimeoutException e) {
2459 // fall through and cancel
2460 } catch (CancellationException e) {
2461 // fall through and cancel
2462 } catch (ExecutionException e) {
2463 final Throwable cause = e.getCause();
2464 if (cause instanceof IOException) {
2465 throw (IOException) cause;
2466 } else if (cause instanceof UnsupportedOperationException) {
2467 throw new AuthenticatorException(cause);
2468 } else if (cause instanceof AuthenticatorException) {
2469 throw (AuthenticatorException) cause;
2470 } else if (cause instanceof RuntimeException) {
2471 throw (RuntimeException) cause;
2472 } else if (cause instanceof Error) {
2473 throw (Error) cause;
2474 } else {
2475 throw new IllegalStateException(cause);
2476 }
2477 } finally {
2478 cancel(true /* interrupt if running */);
2479 }
2480 throw new OperationCanceledException();
2481 }
2482
Sandra Kwan0b84b452016-01-20 15:25:42 -08002483 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002484 public T getResult()
2485 throws OperationCanceledException, IOException, AuthenticatorException {
2486 return internalGetResult(null, null);
2487 }
2488
Sandra Kwan0b84b452016-01-20 15:25:42 -08002489 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002490 public T getResult(long timeout, TimeUnit unit)
2491 throws OperationCanceledException, IOException, AuthenticatorException {
2492 return internalGetResult(timeout, unit);
2493 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002494
Fred Quintana60307342009-03-24 22:48:12 -07002495 }
2496
Fred Quintanaa698f422009-04-08 19:14:54 -07002497 private Exception convertErrorToException(int code, String message) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002498 if (code == ERROR_CODE_NETWORK_ERROR) {
Fred Quintanaa698f422009-04-08 19:14:54 -07002499 return new IOException(message);
Fred Quintana60307342009-03-24 22:48:12 -07002500 }
Fred Quintana60307342009-03-24 22:48:12 -07002501
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002502 if (code == ERROR_CODE_UNSUPPORTED_OPERATION) {
Fred Quintana33269202009-04-20 16:05:10 -07002503 return new UnsupportedOperationException(message);
Fred Quintana60307342009-03-24 22:48:12 -07002504 }
Fred Quintanaa698f422009-04-08 19:14:54 -07002505
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002506 if (code == ERROR_CODE_INVALID_RESPONSE) {
Fred Quintana33269202009-04-20 16:05:10 -07002507 return new AuthenticatorException(message);
Fred Quintanaa698f422009-04-08 19:14:54 -07002508 }
2509
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002510 if (code == ERROR_CODE_BAD_ARGUMENTS) {
Fred Quintana33269202009-04-20 16:05:10 -07002511 return new IllegalArgumentException(message);
2512 }
2513
2514 return new AuthenticatorException(message);
2515 }
2516
sunjianf29d5492017-05-11 15:42:31 -07002517 private void getAccountByTypeAndFeatures(String accountType, String[] features,
2518 AccountManagerCallback<Bundle> callback, Handler handler) {
2519 (new AmsTask(null, handler, callback) {
2520 @Override
2521 public void doWork() throws RemoteException {
2522 mService.getAccountByTypeAndFeatures(mResponse, accountType, features,
2523 mContext.getOpPackageName());
2524 }
2525
2526 }).start();
2527 }
2528
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002529 private class GetAuthTokenByTypeAndFeaturesTask
2530 extends AmsTask implements AccountManagerCallback<Bundle> {
Fred Quintana33269202009-04-20 16:05:10 -07002531 GetAuthTokenByTypeAndFeaturesTask(final String accountType, final String authTokenType,
2532 final String[] features, Activity activityForPrompting,
2533 final Bundle addAccountOptions, final Bundle loginOptions,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002534 AccountManagerCallback<Bundle> callback, Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07002535 super(activityForPrompting, handler, callback);
2536 if (accountType == null) throw new IllegalArgumentException("account type is null");
2537 mAccountType = accountType;
2538 mAuthTokenType = authTokenType;
2539 mFeatures = features;
2540 mAddAccountOptions = addAccountOptions;
2541 mLoginOptions = loginOptions;
2542 mMyCallback = this;
2543 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002544 volatile AccountManagerFuture<Bundle> mFuture = null;
Fred Quintana33269202009-04-20 16:05:10 -07002545 final String mAccountType;
2546 final String mAuthTokenType;
2547 final String[] mFeatures;
2548 final Bundle mAddAccountOptions;
2549 final Bundle mLoginOptions;
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002550 final AccountManagerCallback<Bundle> mMyCallback;
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002551 private volatile int mNumAccounts = 0;
Fred Quintana33269202009-04-20 16:05:10 -07002552
Sandra Kwan0b84b452016-01-20 15:25:42 -08002553 @Override
Fred Quintana33269202009-04-20 16:05:10 -07002554 public void doWork() throws RemoteException {
sunjianf29d5492017-05-11 15:42:31 -07002555 getAccountByTypeAndFeatures(mAccountType, mFeatures,
2556 new AccountManagerCallback<Bundle>() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002557 @Override
sunjianf29d5492017-05-11 15:42:31 -07002558 public void run(AccountManagerFuture<Bundle> future) {
2559 String accountName = null;
2560 String accountType = null;
Fred Quintana33269202009-04-20 16:05:10 -07002561 try {
sunjianf29d5492017-05-11 15:42:31 -07002562 Bundle result = future.getResult();
2563 accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME);
2564 accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002565 } catch (OperationCanceledException e) {
2566 setException(e);
2567 return;
2568 } catch (IOException e) {
2569 setException(e);
2570 return;
2571 } catch (AuthenticatorException e) {
2572 setException(e);
2573 return;
Fred Quintana33269202009-04-20 16:05:10 -07002574 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002575
sunjianf29d5492017-05-11 15:42:31 -07002576 if (accountName == null) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002577 if (mActivity != null) {
2578 // no accounts, add one now. pretend that the user directly
2579 // made this request
2580 mFuture = addAccount(mAccountType, mAuthTokenType, mFeatures,
2581 mAddAccountOptions, mActivity, mMyCallback, mHandler);
2582 } else {
2583 // send result since we can't prompt to add an account
2584 Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002585 result.putString(KEY_ACCOUNT_NAME, null);
2586 result.putString(KEY_ACCOUNT_TYPE, null);
2587 result.putString(KEY_AUTHTOKEN, null);
Svet Ganovc1c0d1c2016-09-23 19:15:47 -07002588 result.putBinder(KEY_ACCOUNT_ACCESS_ID, null);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002589 try {
2590 mResponse.onResult(result);
2591 } catch (RemoteException e) {
2592 // this will never happen
2593 }
2594 // we are done
2595 }
sunjianf29d5492017-05-11 15:42:31 -07002596 } else {
2597 mNumAccounts = 1;
2598 Account account = new Account(accountName, accountType);
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002599 // have a single account, return an authtoken for it
2600 if (mActivity == null) {
sunjianf29d5492017-05-11 15:42:31 -07002601 mFuture = getAuthToken(account, mAuthTokenType,
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002602 false /* notifyAuthFailure */, mMyCallback, mHandler);
2603 } else {
sunjianf29d5492017-05-11 15:42:31 -07002604 mFuture = getAuthToken(account, mAuthTokenType, mLoginOptions,
Fred Quintana33269202009-04-20 16:05:10 -07002605 mActivity, mMyCallback, mHandler);
2606 }
Fred Quintana33269202009-04-20 16:05:10 -07002607 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002608 }}, mHandler);
Fred Quintana33269202009-04-20 16:05:10 -07002609 }
2610
Sandra Kwan0b84b452016-01-20 15:25:42 -08002611 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002612 public void run(AccountManagerFuture<Bundle> future) {
Fred Quintana33269202009-04-20 16:05:10 -07002613 try {
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002614 final Bundle result = future.getResult();
2615 if (mNumAccounts == 0) {
2616 final String accountName = result.getString(KEY_ACCOUNT_NAME);
2617 final String accountType = result.getString(KEY_ACCOUNT_TYPE);
2618 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
2619 setException(new AuthenticatorException("account not in result"));
2620 return;
2621 }
Svet Ganovc1c0d1c2016-09-23 19:15:47 -07002622 final String accessId = result.getString(KEY_ACCOUNT_ACCESS_ID);
2623 final Account account = new Account(accountName, accountType, accessId);
Fred Quintanaf0fd8432010-03-08 12:48:05 -08002624 mNumAccounts = 1;
2625 getAuthToken(account, mAuthTokenType, null /* options */, mActivity,
2626 mMyCallback, mHandler);
2627 return;
2628 }
2629 set(result);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002630 } catch (OperationCanceledException e) {
2631 cancel(true /* mayInterruptIfRUnning */);
2632 } catch (IOException e) {
2633 setException(e);
2634 } catch (AuthenticatorException e) {
2635 setException(e);
Fred Quintana33269202009-04-20 16:05:10 -07002636 }
2637 }
2638 }
2639
Fred Quintana756b7352009-10-21 13:43:10 -07002640 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -08002641 * This convenience helper combines the functionality of {@link #getAccountsByTypeAndFeatures},
2642 * {@link #getAuthToken}, and {@link #addAccount}.
Fred Quintana756b7352009-10-21 13:43:10 -07002643 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002644 * <p>
2645 * This method gets a list of the accounts matching specific type and feature set which are
Dmitry Dementyev85d8a342017-05-02 17:33:17 -07002646 * visible to the caller (see {@link #getAccountsByType} for details);
2647 * if there is exactly one already visible account, it is used; if there are some
Dmitry Dementyev52745472016-12-02 10:27:45 -08002648 * accounts for which user grant visibility, the user is prompted to pick one; if there are
2649 * none, the user is prompted to add one. Finally, an auth token is acquired for the chosen
2650 * account.
Dan Egnor661f0132010-02-19 11:23:00 -08002651 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002652 * <p>
2653 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
2654 * not be used on the main thread.
Dan Egnor661f0132010-02-19 11:23:00 -08002655 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002656 * <p>
2657 * <b>NOTE:</b> If targeting your app to work on API level 22 and before, MANAGE_ACCOUNTS
2658 * permission is needed for those platforms. See docs for this function in API level 22.
Simranjit Singh Kohli210bace2015-07-29 16:34:49 -07002659 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002660 * @param accountType The account type required (see {@link #getAccountsByType}), must not be
2661 * null
2662 * @param authTokenType The desired auth token type (see {@link #getAuthToken}), must not be
2663 * null
2664 * @param features Required features for the account (see
2665 * {@link #getAccountsByTypeAndFeatures}), may be null or empty
2666 * @param activity The {@link Activity} context to use for launching new sub-Activities to
2667 * prompt to add an account, select an account, and/or enter a password, as necessary;
2668 * used only to call startActivity(); should not be null
2669 * @param addAccountOptions Authenticator-specific options to use for adding new accounts; may
2670 * be null or empty
2671 * @param getAuthTokenOptions Authenticator-specific options to use for getting auth tokens; may
2672 * be null or empty
2673 * @param callback Callback to invoke when the request completes, null for no callback
2674 * @param handler {@link Handler} identifying the callback thread, null for the main thread
2675 * @return An {@link AccountManagerFuture} which resolves to a Bundle with at least the
2676 * following fields:
2677 * <ul>
2678 * <li>{@link #KEY_ACCOUNT_NAME} - the name of the account
2679 * <li>{@link #KEY_ACCOUNT_TYPE} - the type of the account
2680 * <li>{@link #KEY_AUTHTOKEN} - the auth token you wanted
2681 * </ul>
Dan Egnor661f0132010-02-19 11:23:00 -08002682 *
Dmitry Dementyev52745472016-12-02 10:27:45 -08002683 * If an error occurred, {@link AccountManagerFuture#getResult()} throws:
2684 * <ul>
2685 * <li>{@link AuthenticatorException} if no authenticator was registered for this
2686 * account type or the authenticator failed to respond
2687 * <li>{@link OperationCanceledException} if the operation was canceled for any reason,
2688 * including the user canceling any operation
2689 * <li>{@link IOException} if the authenticator experienced an I/O problem updating
2690 * settings, usually because of network trouble
2691 * </ul>
Fred Quintana756b7352009-10-21 13:43:10 -07002692 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002693 public AccountManagerFuture<Bundle> getAuthTokenByFeatures(
Fred Quintana33269202009-04-20 16:05:10 -07002694 final String accountType, final String authTokenType, final String[] features,
Dan Egnor661f0132010-02-19 11:23:00 -08002695 final Activity activity, final Bundle addAccountOptions,
sunjianf29d5492017-05-11 15:42:31 -07002696 final Bundle getAuthTokenOptions, final AccountManagerCallback<Bundle> callback,
2697 final Handler handler) {
Fred Quintana33269202009-04-20 16:05:10 -07002698 if (accountType == null) throw new IllegalArgumentException("account type is null");
2699 if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002700 final GetAuthTokenByTypeAndFeaturesTask task =
2701 new GetAuthTokenByTypeAndFeaturesTask(accountType, authTokenType, features,
Dan Egnor661f0132010-02-19 11:23:00 -08002702 activity, addAccountOptions, getAuthTokenOptions, callback, handler);
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002703 task.start();
2704 return task;
Fred Quintana60307342009-03-24 22:48:12 -07002705 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002706
Fred Quintana1121bb52011-09-14 23:19:35 -07002707 /**
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002708 * Deprecated in favor of {@link #newChooseAccountIntent(Account, List, String[], String,
2709 * String, String[], Bundle)}.
2710 *
Fred Quintana1121bb52011-09-14 23:19:35 -07002711 * Returns an intent to an {@link Activity} that prompts the user to choose from a list of
2712 * accounts.
2713 * The caller will then typically start the activity by calling
Mark Fickettab249e02012-09-05 09:45:47 -04002714 * <code>startActivityForResult(intent, ...);</code>.
Fred Quintana1121bb52011-09-14 23:19:35 -07002715 * <p>
2716 * On success the activity returns a Bundle with the account name and type specified using
2717 * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}.
Dmitry Dementyev85d8a342017-05-02 17:33:17 -07002718 * Chosen account is marked as {@link #VISIBILITY_USER_MANAGED_VISIBLE} to the caller
2719 * (see {@link setAccountVisibility}) and will be returned to it in consequent
2720 * {@link #getAccountsByType}) calls.
Fred Quintana1121bb52011-09-14 23:19:35 -07002721 * <p>
2722 * The most common case is to call this with one account type, e.g.:
2723 * <p>
kmccormickf783ce52013-03-29 14:31:54 -07002724 * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, false, null,
Fred Quintanad88324d2011-09-19 11:43:05 -07002725 * null, null, null);</pre>
Fred Quintana1121bb52011-09-14 23:19:35 -07002726 * @param selectedAccount if specified, indicates that the {@link Account} is the currently
2727 * selected one, according to the caller's definition of selected.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002728 * @param allowableAccounts an optional {@link List} of accounts that are allowed to be
Fred Quintana1121bb52011-09-14 23:19:35 -07002729 * shown. If not specified then this field will not limit the displayed accounts.
2730 * @param allowableAccountTypes an optional string array of account types. These are used
2731 * both to filter the shown accounts and to filter the list of account types that are shown
Simranjit Singh Kohli734f8fb2015-05-22 14:00:32 -07002732 * when adding an account. If not specified then this field will not limit the displayed
2733 * account types when adding an account.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002734 * @param alwaysPromptForAccount boolean that is ignored.
Fred Quintanad88324d2011-09-19 11:43:05 -07002735 * @param descriptionOverrideText if non-null this string is used as the description in the
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002736 * accounts chooser screen rather than the default
Fred Quintanad88324d2011-09-19 11:43:05 -07002737 * @param addAccountAuthTokenType this string is passed as the {@link #addAccount}
2738 * authTokenType parameter
2739 * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount}
2740 * requiredFeatures parameter
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002741 * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options
Fred Quintanad88324d2011-09-19 11:43:05 -07002742 * parameter
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002743 * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow.
Fred Quintana1121bb52011-09-14 23:19:35 -07002744 */
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002745 @Deprecated
2746 static public Intent newChooseAccountIntent(
2747 Account selectedAccount,
Baligh Uddinf2d248d2015-07-10 03:01:47 +00002748 ArrayList<Account> allowableAccounts,
Fred Quintana1121bb52011-09-14 23:19:35 -07002749 String[] allowableAccountTypes,
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002750 boolean alwaysPromptForAccount,
2751 String descriptionOverrideText,
2752 String addAccountAuthTokenType,
2753 String[] addAccountRequiredFeatures,
Fred Quintana1121bb52011-09-14 23:19:35 -07002754 Bundle addAccountOptions) {
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002755 return newChooseAccountIntent(
2756 selectedAccount,
2757 allowableAccounts,
2758 allowableAccountTypes,
2759 descriptionOverrideText,
2760 addAccountAuthTokenType,
2761 addAccountRequiredFeatures,
2762 addAccountOptions);
2763 }
2764
2765 /**
2766 * Returns an intent to an {@link Activity} that prompts the user to choose from a list of
2767 * accounts.
2768 * The caller will then typically start the activity by calling
2769 * <code>startActivityForResult(intent, ...);</code>.
2770 * <p>
2771 * On success the activity returns a Bundle with the account name and type specified using
2772 * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}.
Dmitry Dementyev85d8a342017-05-02 17:33:17 -07002773 * Chosen account is marked as {@link #VISIBILITY_USER_MANAGED_VISIBLE} to the caller
2774 * (see {@link setAccountVisibility}) and will be returned to it in consequent
2775 * {@link #getAccountsByType}) calls.
Carlos Valdiviaa3db8ac2015-07-10 13:04:43 -07002776 * <p>
2777 * The most common case is to call this with one account type, e.g.:
2778 * <p>
2779 * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, null, null, null,
2780 * null);</pre>
2781 * @param selectedAccount if specified, indicates that the {@link Account} is the currently
2782 * selected one, according to the caller's definition of selected.
2783 * @param allowableAccounts an optional {@link List} of accounts that are allowed to be
2784 * shown. If not specified then this field will not limit the displayed accounts.
2785 * @param allowableAccountTypes an optional string array of account types. These are used
2786 * both to filter the shown accounts and to filter the list of account types that are shown
2787 * when adding an account. If not specified then this field will not limit the displayed
2788 * account types when adding an account.
2789 * @param descriptionOverrideText if non-null this string is used as the description in the
2790 * accounts chooser screen rather than the default
2791 * @param addAccountAuthTokenType this string is passed as the {@link #addAccount}
2792 * authTokenType parameter
2793 * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount}
2794 * requiredFeatures parameter
2795 * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options
2796 * parameter
2797 * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow.
2798 */
2799 static public Intent newChooseAccountIntent(
2800 Account selectedAccount,
2801 List<Account> allowableAccounts,
2802 String[] allowableAccountTypes,
2803 String descriptionOverrideText,
2804 String addAccountAuthTokenType,
2805 String[] addAccountRequiredFeatures,
2806 Bundle addAccountOptions) {
Fred Quintana1121bb52011-09-14 23:19:35 -07002807 Intent intent = new Intent();
Amith Yamasani12b8e132013-03-14 10:48:07 -07002808 ComponentName componentName = ComponentName.unflattenFromString(
2809 Resources.getSystem().getString(R.string.config_chooseTypeAndAccountActivity));
2810 intent.setClassName(componentName.getPackageName(),
2811 componentName.getClassName());
Fred Quintana1121bb52011-09-14 23:19:35 -07002812 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST,
Craig Lafayette3c9c71d2015-07-14 10:48:46 -04002813 allowableAccounts == null ? null : new ArrayList<Account>(allowableAccounts));
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002814 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
2815 allowableAccountTypes);
Fred Quintana1121bb52011-09-14 23:19:35 -07002816 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,
2817 addAccountOptions);
2818 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_SELECTED_ACCOUNT, selectedAccount);
Fred Quintanab04fe4e2011-09-16 21:17:21 -07002819 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_DESCRIPTION_TEXT_OVERRIDE,
2820 descriptionOverrideText);
2821 intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING,
2822 addAccountAuthTokenType);
2823 intent.putExtra(
2824 ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY,
2825 addAccountRequiredFeatures);
Fred Quintana1121bb52011-09-14 23:19:35 -07002826 return intent;
2827 }
2828
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002829 private final HashMap<OnAccountsUpdateListener, Handler> mAccountsUpdatedListeners =
Fred Quintanad9d2f112009-04-23 13:36:27 -07002830 Maps.newHashMap();
2831
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002832 private final HashMap<OnAccountsUpdateListener, Set<String> > mAccountsUpdatedListenersTypes =
2833 Maps.newHashMap();
2834
Fred Quintanad9d2f112009-04-23 13:36:27 -07002835 /**
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002836 * BroadcastReceiver that listens for the ACTION_VISIBLE_ACCOUNTS_CHANGED intent
Fred Quintanad9d2f112009-04-23 13:36:27 -07002837 * so that it can read the updated list of accounts and send them to the listener
2838 * in mAccountsUpdatedListeners.
2839 */
2840 private final BroadcastReceiver mAccountsChangedBroadcastReceiver = new BroadcastReceiver() {
Sandra Kwan0b84b452016-01-20 15:25:42 -08002841 @Override
Fred Quintanad9d2f112009-04-23 13:36:27 -07002842 public void onReceive(final Context context, final Intent intent) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002843 final Account[] accounts = getAccounts();
2844 // send the result to the listeners
2845 synchronized (mAccountsUpdatedListeners) {
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002846 for (Map.Entry<OnAccountsUpdateListener, Handler> entry :
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002847 mAccountsUpdatedListeners.entrySet()) {
2848 postToHandler(entry.getValue(), entry.getKey(), accounts);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002849 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002850 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002851 }
2852 };
2853
2854 /**
Dmitry Dementyev52745472016-12-02 10:27:45 -08002855 * Adds an {@link OnAccountsUpdateListener} to this instance of the {@link AccountManager}. This
2856 * listener will be notified whenever user or AbstractAcccountAuthenticator made changes to
Dmitry Dementyev2ae64092017-01-09 12:48:18 -08002857 * accounts of any type related to the caller. This method is equivalent to
2858 * addOnAccountsUpdatedListener(listener, handler, updateImmediately, null)
Dan Egnor661f0132010-02-19 11:23:00 -08002859 *
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002860 * @see #addOnAccountsUpdatedListener(OnAccountsUpdateListener, Handler, boolean,
Dmitry Dementyev2ae64092017-01-09 12:48:18 -08002861 * String[])
2862 */
2863 public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener,
2864 Handler handler, boolean updateImmediately) {
2865 addOnAccountsUpdatedListener(listener, handler,updateImmediately, null);
2866 }
2867
2868 /**
2869 * Adds an {@link OnAccountsUpdateListener} to this instance of the {@link AccountManager}. This
2870 * listener will be notified whenever user or AbstractAcccountAuthenticator made changes to
2871 * accounts of given types related to the caller -
2872 * either list of accounts returned by {@link #getAccounts()}
2873 * was changed, or new account was added for which user can grant access to the caller.
Dmitry Dementyev52745472016-12-02 10:27:45 -08002874 * <p>
2875 * As long as this listener is present, the AccountManager instance will not be
2876 * garbage-collected, and neither will the {@link Context} used to retrieve it, which may be a
2877 * large Activity instance. To avoid memory leaks, you must remove this listener before then.
2878 * Normally listeners are added in an Activity or Service's {@link Activity#onCreate} and
2879 * removed in {@link Activity#onDestroy}.
Dmitry Dementyev52745472016-12-02 10:27:45 -08002880 * <p>
2881 * It is safe to call this method from the main thread.
Ian Pedowitz845d14d2015-08-04 07:47:37 -07002882 *
Dan Egnor661f0132010-02-19 11:23:00 -08002883 * @param listener The listener to send notifications to
Dmitry Dementyev52745472016-12-02 10:27:45 -08002884 * @param handler {@link Handler} identifying the thread to use for notifications, null for the
2885 * main thread
2886 * @param updateImmediately If true, the listener will be invoked (on the handler thread) right
2887 * away with the current account list
Dmitry Dementyev2ae64092017-01-09 12:48:18 -08002888 * @param accountTypes If set, only changes to accounts of given types will be reported.
Fred Quintanad9d2f112009-04-23 13:36:27 -07002889 * @throws IllegalArgumentException if listener is null
2890 * @throws IllegalStateException if listener was already added
2891 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002892 public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener,
Dmitry Dementyev2ae64092017-01-09 12:48:18 -08002893 Handler handler, boolean updateImmediately, String[] accountTypes) {
Fred Quintanad9d2f112009-04-23 13:36:27 -07002894 if (listener == null) {
2895 throw new IllegalArgumentException("the listener is null");
2896 }
2897 synchronized (mAccountsUpdatedListeners) {
2898 if (mAccountsUpdatedListeners.containsKey(listener)) {
2899 throw new IllegalStateException("this listener is already added");
2900 }
2901 final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty();
2902
2903 mAccountsUpdatedListeners.put(listener, handler);
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002904 if (accountTypes != null) {
2905 mAccountsUpdatedListenersTypes.put(listener,
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002906 new HashSet<String>(Arrays.asList(accountTypes)));
2907 } else {
2908 mAccountsUpdatedListenersTypes.put(listener, null);
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002909 }
Dmitry Dementyev52745472016-12-02 10:27:45 -08002910
Fred Quintanad9d2f112009-04-23 13:36:27 -07002911 if (wasEmpty) {
2912 // Register a broadcast receiver to monitor account changes
2913 IntentFilter intentFilter = new IntentFilter();
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002914 intentFilter.addAction(ACTION_VISIBLE_ACCOUNTS_CHANGED);
Costin Manolacheb6437242009-09-10 16:14:12 -07002915 // To recover from disk-full.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002916 intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002917 mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter);
2918 }
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002919
2920 try {
2921 // Notify AccountManagedService about new receiver.
2922 // The receiver must be unregistered later exactly one time
2923 mService.registerAccountListener(accountTypes, mContext.getOpPackageName());
2924 } catch (RemoteException e) {
2925 throw e.rethrowFromSystemServer();
2926 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002927 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002928 if (updateImmediately) {
Fred Quintanaffd0cb042009-08-15 21:45:26 -07002929 postToHandler(handler, listener, getAccounts());
Fred Quintanad9d2f112009-04-23 13:36:27 -07002930 }
2931 }
2932
2933 /**
Dan Egnor661f0132010-02-19 11:23:00 -08002934 * Removes an {@link OnAccountsUpdateListener} previously registered with
2935 * {@link #addOnAccountsUpdatedListener}. The listener will no longer
2936 * receive notifications of account changes.
2937 *
2938 * <p>It is safe to call this method from the main thread.
2939 *
2940 * <p>No permission is required to call this method.
2941 *
2942 * @param listener The previously added listener to remove
Fred Quintanad9d2f112009-04-23 13:36:27 -07002943 * @throws IllegalArgumentException if listener is null
2944 * @throws IllegalStateException if listener was not already added
2945 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -07002946 public void removeOnAccountsUpdatedListener(OnAccountsUpdateListener listener) {
Fred Quintana382601f2010-03-25 12:25:10 -07002947 if (listener == null) throw new IllegalArgumentException("listener is null");
Fred Quintanad9d2f112009-04-23 13:36:27 -07002948 synchronized (mAccountsUpdatedListeners) {
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01002949 if (!mAccountsUpdatedListeners.containsKey(listener)) {
Costin Manolache88a211b2009-10-29 11:30:11 -07002950 Log.e(TAG, "Listener was not previously added");
2951 return;
Fred Quintanad9d2f112009-04-23 13:36:27 -07002952 }
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002953 Set<String> accountTypes = mAccountsUpdatedListenersTypes.get(listener);
2954 String[] accountsArray;
2955 if (accountTypes != null) {
2956 accountsArray = accountTypes.toArray(new String[accountTypes.size()]);
2957 } else {
2958 accountsArray = null;
2959 }
Bryan Mawhinney5be61f52009-09-24 14:50:25 +01002960 mAccountsUpdatedListeners.remove(listener);
Dmitry Dementyev01985ff2017-01-19 16:03:39 -08002961 mAccountsUpdatedListenersTypes.remove(listener);
Fred Quintanad9d2f112009-04-23 13:36:27 -07002962 if (mAccountsUpdatedListeners.isEmpty()) {
2963 mContext.unregisterReceiver(mAccountsChangedBroadcastReceiver);
2964 }
Dmitry Dementyev8882d882017-03-14 17:25:46 -07002965 try {
2966 mService.unregisterAccountListener(accountsArray, mContext.getOpPackageName());
2967 } catch (RemoteException e) {
2968 throw e.rethrowFromSystemServer();
2969 }
Fred Quintanad9d2f112009-04-23 13:36:27 -07002970 }
2971 }
Sandra Kwan78812282015-11-04 11:19:47 -08002972
2973 /**
2974 * Asks the user to authenticate with an account of a specified type. The
2975 * authenticator for this account type processes this request with the
2976 * appropriate user interface. If the user does elect to authenticate with a
2977 * new account, a bundle of session data for installing the account later is
2978 * returned with optional account password and account status token.
2979 * <p>
2980 * This method may be called from any thread, but the returned
2981 * {@link AccountManagerFuture} must not be used on the main thread.
2982 * <p>
2983 * <p>
2984 * <b>NOTE:</b> The account will not be installed to the device by calling
Sandra Kwan920f6ef2015-11-10 14:13:29 -08002985 * this api alone. #finishSession should be called after this to install the
2986 * account on device.
Sandra Kwan78812282015-11-04 11:19:47 -08002987 *
2988 * @param accountType The type of account to add; must not be null
2989 * @param authTokenType The type of auth token (see {@link #getAuthToken})
2990 * this account will need to be able to generate, null for none
2991 * @param requiredFeatures The features (see {@link #hasFeatures}) this
2992 * account must have, null for none
2993 * @param options Authenticator-specific options for the request, may be
2994 * null or empty
2995 * @param activity The {@link Activity} context to use for launching a new
2996 * authenticator-defined sub-Activity to prompt the user to
2997 * create an account; used only to call startActivity(); if null,
2998 * the prompt will not be launched directly, but the necessary
2999 * {@link Intent} will be returned to the caller instead
3000 * @param callback Callback to invoke when the request completes, null for
3001 * no callback
3002 * @param handler {@link Handler} identifying the callback thread, null for
3003 * the main thread
3004 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
3005 * these fields if activity was specified and user was authenticated
3006 * with an account:
3007 * <ul>
3008 * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for
3009 * adding the the to the device later.
Sandra Kwan78812282015-11-04 11:19:47 -08003010 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
3011 * status of the account
3012 * </ul>
3013 * If no activity was specified, the returned Bundle contains only
3014 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
3015 * actual account creation process. If authenticator doesn't support
3016 * this method, the returned Bundle contains only
3017 * {@link #KEY_ACCOUNT_SESSION_BUNDLE} with encrypted
3018 * {@code options} needed to add account later. If an error
3019 * occurred, {@link AccountManagerFuture#getResult()} throws:
3020 * <ul>
3021 * <li>{@link AuthenticatorException} if no authenticator was
3022 * registered for this account type or the authenticator failed to
3023 * respond
3024 * <li>{@link OperationCanceledException} if the operation was
3025 * canceled for any reason, including the user canceling the
3026 * creation process or adding accounts (of this type) has been
3027 * disabled by policy
3028 * <li>{@link IOException} if the authenticator experienced an I/O
3029 * problem creating a new account, usually because of network
3030 * trouble
3031 * </ul>
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003032 * @see #finishSession
Sandra Kwan78812282015-11-04 11:19:47 -08003033 */
Sandra Kwane68c37e2015-11-12 17:11:49 -08003034 public AccountManagerFuture<Bundle> startAddAccountSession(
3035 final String accountType,
3036 final String authTokenType,
3037 final String[] requiredFeatures,
3038 final Bundle options,
3039 final Activity activity,
3040 AccountManagerCallback<Bundle> callback,
3041 Handler handler) {
Sandra Kwan78812282015-11-04 11:19:47 -08003042 if (accountType == null) throw new IllegalArgumentException("accountType is null");
3043 final Bundle optionsIn = new Bundle();
3044 if (options != null) {
3045 optionsIn.putAll(options);
3046 }
3047 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
3048
3049 return new AmsTask(activity, handler, callback) {
3050 @Override
3051 public void doWork() throws RemoteException {
Sandra Kwane68c37e2015-11-12 17:11:49 -08003052 mService.startAddAccountSession(
3053 mResponse,
3054 accountType,
3055 authTokenType,
3056 requiredFeatures,
3057 activity != null,
3058 optionsIn);
3059 }
3060 }.start();
3061 }
3062
3063 /**
3064 * Asks the user to enter a new password for an account but not updating the
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003065 * saved credentials for the account until {@link #finishSession} is called.
Sandra Kwane68c37e2015-11-12 17:11:49 -08003066 * <p>
3067 * This method may be called from any thread, but the returned
3068 * {@link AccountManagerFuture} must not be used on the main thread.
3069 * <p>
3070 * <b>NOTE:</b> The saved credentials for the account alone will not be
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003071 * updated by calling this API alone. #finishSession should be called after
3072 * this to update local credentials
Sandra Kwane68c37e2015-11-12 17:11:49 -08003073 *
3074 * @param account The account to update credentials for
3075 * @param authTokenType The credentials entered must allow an auth token of
3076 * this type to be created (but no actual auth token is
3077 * returned); may be null
3078 * @param options Authenticator-specific options for the request; may be
3079 * null or empty
3080 * @param activity The {@link Activity} context to use for launching a new
3081 * authenticator-defined sub-Activity to prompt the user to enter
3082 * a password; used only to call startActivity(); if null, the
3083 * prompt will not be launched directly, but the necessary
3084 * {@link Intent} will be returned to the caller instead
3085 * @param callback Callback to invoke when the request completes, null for
3086 * no callback
3087 * @param handler {@link Handler} identifying the callback thread, null for
3088 * the main thread
3089 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
3090 * these fields if an activity was supplied and user was
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003091 * successfully re-authenticated to the account:
Sandra Kwane68c37e2015-11-12 17:11:49 -08003092 * <ul>
3093 * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for
3094 * updating the local credentials on device later.
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003095 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
3096 * status of the account
Sandra Kwane68c37e2015-11-12 17:11:49 -08003097 * </ul>
3098 * If no activity was specified, the returned Bundle contains
3099 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
3100 * password prompt. If an error occurred,
3101 * {@link AccountManagerFuture#getResult()} throws:
3102 * <ul>
3103 * <li>{@link AuthenticatorException} if the authenticator failed to
3104 * respond
3105 * <li>{@link OperationCanceledException} if the operation was
3106 * canceled for any reason, including the user canceling the
3107 * password prompt
3108 * <li>{@link IOException} if the authenticator experienced an I/O
3109 * problem verifying the password, usually because of network
3110 * trouble
3111 * </ul>
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003112 * @see #finishSession
Sandra Kwane68c37e2015-11-12 17:11:49 -08003113 */
3114 public AccountManagerFuture<Bundle> startUpdateCredentialsSession(
3115 final Account account,
3116 final String authTokenType,
3117 final Bundle options,
3118 final Activity activity,
3119 final AccountManagerCallback<Bundle> callback,
3120 final Handler handler) {
3121 if (account == null) {
3122 throw new IllegalArgumentException("account is null");
3123 }
Carlos Valdivia51b651a2016-03-30 13:44:28 -07003124
3125 // Always include the calling package name. This just makes life easier
3126 // down stream.
3127 final Bundle optionsIn = new Bundle();
3128 if (options != null) {
3129 optionsIn.putAll(options);
3130 }
3131 optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
3132
Sandra Kwane68c37e2015-11-12 17:11:49 -08003133 return new AmsTask(activity, handler, callback) {
3134 @Override
3135 public void doWork() throws RemoteException {
3136 mService.startUpdateCredentialsSession(
3137 mResponse,
3138 account,
3139 authTokenType,
3140 activity != null,
Carlos Valdivia51b651a2016-03-30 13:44:28 -07003141 optionsIn);
Sandra Kwan78812282015-11-04 11:19:47 -08003142 }
3143 }.start();
3144 }
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003145
3146 /**
3147 * Finishes the session started by {@link #startAddAccountSession} or
3148 * {@link #startUpdateCredentialsSession}. This will either add the account
3149 * to AccountManager or update the local credentials stored.
3150 * <p>
3151 * This method may be called from any thread, but the returned
3152 * {@link AccountManagerFuture} must not be used on the main thread.
3153 *
3154 * @param sessionBundle a {@link Bundle} created by {@link #startAddAccountSession} or
3155 * {@link #startUpdateCredentialsSession}
3156 * @param activity The {@link Activity} context to use for launching a new
3157 * authenticator-defined sub-Activity to prompt the user to
3158 * create an account or reauthenticate existing account; used
3159 * only to call startActivity(); if null, the prompt will not
3160 * be launched directly, but the necessary {@link Intent} will
3161 * be returned to the caller instead
3162 * @param callback Callback to invoke when the request completes, null for
3163 * no callback
3164 * @param handler {@link Handler} identifying the callback thread, null for
3165 * the main thread
3166 * @return An {@link AccountManagerFuture} which resolves to a Bundle with
3167 * these fields if an activity was supplied and an account was added
3168 * to device or local credentials were updated::
3169 * <ul>
3170 * <li>{@link #KEY_ACCOUNT_NAME} - the name of the account created
3171 * <li>{@link #KEY_ACCOUNT_TYPE} - the type of the account
Hongming Jin8b442752016-06-26 10:36:21 -07003172 * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check
3173 * status of the account
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003174 * </ul>
3175 * If no activity was specified and additional information is needed
3176 * from user, the returned Bundle may contains only
3177 * {@link #KEY_INTENT} with the {@link Intent} needed to launch the
3178 * actual account creation process. If an error occurred,
3179 * {@link AccountManagerFuture#getResult()} throws:
3180 * <ul>
3181 * <li>{@link AuthenticatorException} if no authenticator was
3182 * registered for this account type or the authenticator failed to
3183 * respond
3184 * <li>{@link OperationCanceledException} if the operation was
3185 * canceled for any reason, including the user canceling the
3186 * creation process or adding accounts (of this type) has been
3187 * disabled by policy
3188 * <li>{@link IOException} if the authenticator experienced an I/O
3189 * problem creating a new account, usually because of network
3190 * trouble
3191 * </ul>
3192 * @see #startAddAccountSession and #startUpdateCredentialsSession
3193 */
3194 public AccountManagerFuture<Bundle> finishSession(
3195 final Bundle sessionBundle,
3196 final Activity activity,
3197 AccountManagerCallback<Bundle> callback,
3198 Handler handler) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003199 return finishSessionAsUser(
3200 sessionBundle,
3201 activity,
3202 Process.myUserHandle(),
3203 callback,
3204 handler);
3205 }
3206
3207 /**
3208 * @see #finishSession
3209 * @hide
3210 */
3211 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06003212 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
Sandra Kwan0b84b452016-01-20 15:25:42 -08003213 public AccountManagerFuture<Bundle> finishSessionAsUser(
3214 final Bundle sessionBundle,
3215 final Activity activity,
3216 final UserHandle userHandle,
3217 AccountManagerCallback<Bundle> callback,
3218 Handler handler) {
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003219 if (sessionBundle == null) {
3220 throw new IllegalArgumentException("sessionBundle is null");
3221 }
3222
3223 /* Add information required by add account flow */
3224 final Bundle appInfo = new Bundle();
3225 appInfo.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName());
3226
3227 return new AmsTask(activity, handler, callback) {
3228 @Override
3229 public void doWork() throws RemoteException {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003230 mService.finishSessionAsUser(
3231 mResponse,
3232 sessionBundle,
3233 activity != null,
3234 appInfo,
3235 userHandle.getIdentifier());
Sandra Kwan920f6ef2015-11-10 14:13:29 -08003236 }
3237 }.start();
3238 }
Sandra Kwan390c9d22016-01-12 14:13:37 -08003239
3240 /**
3241 * Checks whether {@link #updateCredentials} or {@link #startUpdateCredentialsSession} should be
3242 * called with respect to the specified account.
3243 * <p>
3244 * This method may be called from any thread, but the returned {@link AccountManagerFuture} must
3245 * not be used on the main thread.
3246 *
3247 * @param account The {@link Account} to be checked whether {@link #updateCredentials} or
3248 * {@link #startUpdateCredentialsSession} should be called
3249 * @param statusToken a String of token to check account staus
3250 * @param callback Callback to invoke when the request completes, null for no callback
3251 * @param handler {@link Handler} identifying the callback thread, null for the main thread
3252 * @return An {@link AccountManagerFuture} which resolves to a Boolean, true if the credentials
3253 * of the account should be updated.
Sandra Kwan390c9d22016-01-12 14:13:37 -08003254 */
Sandra Kwan390c9d22016-01-12 14:13:37 -08003255 public AccountManagerFuture<Boolean> isCredentialsUpdateSuggested(
3256 final Account account,
3257 final String statusToken,
3258 AccountManagerCallback<Boolean> callback,
3259 Handler handler) {
3260 if (account == null) {
3261 throw new IllegalArgumentException("account is null");
3262 }
3263
3264 if (TextUtils.isEmpty(statusToken)) {
3265 throw new IllegalArgumentException("status token is empty");
3266 }
3267
3268 return new Future2Task<Boolean>(handler, callback) {
Sandra Kwan0b84b452016-01-20 15:25:42 -08003269 @Override
Sandra Kwan390c9d22016-01-12 14:13:37 -08003270 public void doWork() throws RemoteException {
3271 mService.isCredentialsUpdateSuggested(
3272 mResponse,
3273 account,
3274 statusToken);
3275 }
Sandra Kwan0b84b452016-01-20 15:25:42 -08003276 @Override
Sandra Kwan390c9d22016-01-12 14:13:37 -08003277 public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException {
3278 if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) {
3279 throw new AuthenticatorException("no result in response");
3280 }
3281 return bundle.getBoolean(KEY_BOOLEAN_RESULT);
3282 }
3283 }.start();
3284 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003285
3286 /**
3287 * Gets whether a given package under a user has access to an account.
3288 * Can be called only from the system UID.
3289 *
3290 * @param account The account for which to check.
3291 * @param packageName The package for which to check.
3292 * @param userHandle The user for which to check.
3293 * @return True if the package can access the account.
3294 *
3295 * @hide
3296 */
3297 public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName,
3298 @NonNull UserHandle userHandle) {
3299 try {
3300 return mService.hasAccountAccess(account, packageName, userHandle);
3301 } catch (RemoteException e) {
3302 throw e.rethrowFromSystemServer();
3303 }
3304 }
3305
3306 /**
3307 * Creates an intent to request access to a given account for a UID.
3308 * The returned intent should be stated for a result where {@link
3309 * Activity#RESULT_OK} result means access was granted whereas {@link
3310 * Activity#RESULT_CANCELED} result means access wasn't granted. Can
3311 * be called only from the system UID.
3312 *
3313 * @param account The account for which to request.
3314 * @param packageName The package name which to request.
3315 * @param userHandle The user for which to request.
3316 * @return The intent to request account access or null if the package
3317 * doesn't exist.
3318 *
3319 * @hide
3320 */
3321 public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account,
3322 @NonNull String packageName, @NonNull UserHandle userHandle) {
3323 try {
3324 return mService.createRequestAccountAccessIntentSenderAsUser(account, packageName,
3325 userHandle);
3326 } catch (RemoteException e) {
3327 throw e.rethrowFromSystemServer();
3328 }
3329 }
Fred Quintana60307342009-03-24 22:48:12 -07003330}