blob: a3b3a9f21954219e6371913688741893a7a82f19 [file] [log] [blame]
Fred Quintana60307342009-03-24 22:48:12 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.accounts;
18
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070019import android.Manifest;
Sandra Kwana578d112015-12-16 16:01:43 -080020import android.annotation.SystemApi;
Sandra Kwan390c9d22016-01-12 14:13:37 -080021import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.os.Binder;
25import android.os.Bundle;
26import android.os.IBinder;
27import android.os.RemoteException;
28import android.text.TextUtils;
Fred Quintanaf0fd8432010-03-08 12:48:05 -080029import android.util.Log;
30
31import java.util.Arrays;
Fred Quintana60307342009-03-24 22:48:12 -070032
33/**
Fred Quintana756b7352009-10-21 13:43:10 -070034 * Abstract base class for creating AccountAuthenticators.
35 * In order to be an authenticator one must extend this class, provider implementations for the
36 * abstract methods and write a service that returns the result of {@link #getIBinder()}
37 * in the service's {@link android.app.Service#onBind(android.content.Intent)} when invoked
38 * with an intent with action {@link AccountManager#ACTION_AUTHENTICATOR_INTENT}. This service
39 * must specify the following intent filter and metadata tags in its AndroidManifest.xml file
40 * <pre>
41 * &lt;intent-filter&gt;
42 * &lt;action android:name="android.accounts.AccountAuthenticator" /&gt;
43 * &lt;/intent-filter&gt;
44 * &lt;meta-data android:name="android.accounts.AccountAuthenticator"
45 * android:resource="@xml/authenticator" /&gt;
46 * </pre>
47 * The <code>android:resource</code> attribute must point to a resource that looks like:
48 * <pre>
49 * &lt;account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
50 * android:accountType="typeOfAuthenticator"
51 * android:icon="@drawable/icon"
52 * android:smallIcon="@drawable/miniIcon"
53 * android:label="@string/label"
54 * android:accountPreferences="@xml/account_preferences"
55 * /&gt;
56 * </pre>
57 * Replace the icons and labels with your own resources. The <code>android:accountType</code>
58 * attribute must be a string that uniquely identifies your authenticator and will be the same
59 * string that user will use when making calls on the {@link AccountManager} and it also
60 * corresponds to {@link Account#type} for your accounts. One user of the android:icon is the
61 * "Account & Sync" settings page and one user of the android:smallIcon is the Contact Application's
62 * tab panels.
63 * <p>
Ken Wakasaf76a50c2012-03-09 19:56:35 +090064 * The preferences attribute points to a PreferenceScreen xml hierarchy that contains
Fred Quintana756b7352009-10-21 13:43:10 -070065 * a list of PreferenceScreens that can be invoked to manage the authenticator. An example is:
66 * <pre>
67 * &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"&gt;
68 * &lt;PreferenceCategory android:title="@string/title_fmt" /&gt;
69 * &lt;PreferenceScreen
70 * android:key="key1"
71 * android:title="@string/key1_action"
72 * android:summary="@string/key1_summary"&gt;
73 * &lt;intent
74 * android:action="key1.ACTION"
75 * android:targetPackage="key1.package"
76 * android:targetClass="key1.class" /&gt;
77 * &lt;/PreferenceScreen&gt;
78 * &lt;/PreferenceScreen&gt;
79 * </pre>
80 *
81 * <p>
82 * The standard pattern for implementing any of the abstract methods is the following:
83 * <ul>
84 * <li> If the supplied arguments are enough for the authenticator to fully satisfy the request
85 * then it will do so and return a {@link Bundle} that contains the results.
86 * <li> If the authenticator needs information from the user to satisfy the request then it
87 * will create an {@link Intent} to an activity that will prompt the user for the information
88 * and then carry out the request. This intent must be returned in a Bundle as key
89 * {@link AccountManager#KEY_INTENT}.
90 * <p>
91 * The activity needs to return the final result when it is complete so the Intent should contain
92 * the {@link AccountAuthenticatorResponse} as {@link AccountManager#KEY_ACCOUNT_MANAGER_RESPONSE}.
93 * The activity must then call {@link AccountAuthenticatorResponse#onResult} or
94 * {@link AccountAuthenticatorResponse#onError} when it is complete.
Fred Quintana31957f12009-10-21 13:43:10 -070095 * <li> If the authenticator cannot synchronously process the request and return a result then it
Ed Heyld6f158b2009-10-29 10:18:45 -070096 * may choose to return null and then use the AccountManagerResponse to send the result
Fred Quintana31957f12009-10-21 13:43:10 -070097 * when it has completed the request.
Fred Quintana756b7352009-10-21 13:43:10 -070098 * </ul>
99 * <p>
100 * The following descriptions of each of the abstract authenticator methods will not describe the
101 * possible asynchronous nature of the request handling and will instead just describe the input
102 * parameters and the expected result.
103 * <p>
104 * When writing an activity to satisfy these requests one must pass in the AccountManagerResponse
105 * and return the result via that response when the activity finishes (or whenever else the
106 * activity author deems it is the correct time to respond).
107 * The {@link AccountAuthenticatorActivity} handles this, so one may wish to extend that when
108 * writing activities to handle these requests.
Fred Quintana60307342009-03-24 22:48:12 -0700109 */
110public abstract class AbstractAccountAuthenticator {
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800111 private static final String TAG = "AccountAuthenticator";
112
Carlos Valdivia91979be2015-05-22 14:11:35 -0700113 /**
114 * Bundle key used for the {@code long} expiration time (in millis from the unix epoch) of the
115 * associated auth token.
116 *
117 * @see #getAuthToken
118 */
119 public static final String KEY_CUSTOM_TOKEN_EXPIRY = "android.accounts.expiry";
120
Sandra Kwan78812282015-11-04 11:19:47 -0800121 /**
122 * Bundle key used for the {@link String} account type in session bundle.
123 * This is used in the default implementation of
Sandra Kwane68c37e2015-11-12 17:11:49 -0800124 * {@link #startAddAccountSession} and {@link startUpdateCredentialsSession}.
Sandra Kwan78812282015-11-04 11:19:47 -0800125 */
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800126 private static final String KEY_AUTH_TOKEN_TYPE =
127 "android.accounts.AbstractAccountAuthenticato.KEY_AUTH_TOKEN_TYPE";
Sandra Kwan78812282015-11-04 11:19:47 -0800128 /**
129 * Bundle key used for the {@link String} array of required features in
130 * session bundle. This is used in the default implementation of
Sandra Kwane68c37e2015-11-12 17:11:49 -0800131 * {@link #startAddAccountSession} and {@link startUpdateCredentialsSession}.
Sandra Kwan78812282015-11-04 11:19:47 -0800132 */
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800133 private static final String KEY_REQUIRED_FEATURES =
134 "android.accounts.AbstractAccountAuthenticator.KEY_REQUIRED_FEATURES";
Sandra Kwan78812282015-11-04 11:19:47 -0800135 /**
136 * Bundle key used for the {@link Bundle} options in session bundle. This is
Sandra Kwane68c37e2015-11-12 17:11:49 -0800137 * used in default implementation of {@link #startAddAccountSession} and
138 * {@link startUpdateCredentialsSession}.
Sandra Kwan78812282015-11-04 11:19:47 -0800139 */
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800140 private static final String KEY_OPTIONS =
141 "android.accounts.AbstractAccountAuthenticator.KEY_OPTIONS";
Sandra Kwane68c37e2015-11-12 17:11:49 -0800142 /**
143 * Bundle key used for the {@link Account} account in session bundle. This is used
144 * used in default implementation of {@link startUpdateCredentialsSession}.
145 */
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800146 private static final String KEY_ACCOUNT =
147 "android.accounts.AbstractAccountAuthenticator.KEY_ACCOUNT";
Sandra Kwan78812282015-11-04 11:19:47 -0800148
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700149 private final Context mContext;
150
151 public AbstractAccountAuthenticator(Context context) {
152 mContext = context;
153 }
154
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700155 private class Transport extends IAccountAuthenticator.Stub {
Carlos Valdivia91979be2015-05-22 14:11:35 -0700156 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -0700157 public void addAccount(IAccountAuthenticatorResponse response, String accountType,
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800158 String authTokenType, String[] features, Bundle options)
Fred Quintana60307342009-03-24 22:48:12 -0700159 throws RemoteException {
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800160 if (Log.isLoggable(TAG, Log.VERBOSE)) {
161 Log.v(TAG, "addAccount: accountType " + accountType
162 + ", authTokenType " + authTokenType
163 + ", features " + (features == null ? "[]" : Arrays.toString(features)));
164 }
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700165 checkBinderPermission();
Fred Quintanaa698f422009-04-08 19:14:54 -0700166 try {
Fred Quintana31957f12009-10-21 13:43:10 -0700167 final Bundle result = AbstractAccountAuthenticator.this.addAccount(
Fred Quintanaa698f422009-04-08 19:14:54 -0700168 new AccountAuthenticatorResponse(response),
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800169 accountType, authTokenType, features, options);
170 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Ian Pedowitz6cc066d2015-08-05 14:23:43 +0000171 if (result != null) {
172 result.keySet(); // force it to be unparcelled
173 }
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800174 Log.v(TAG, "addAccount: result " + AccountManager.sanitizeResult(result));
175 }
Fred Quintana31957f12009-10-21 13:43:10 -0700176 if (result != null) {
177 response.onResult(result);
tiansimingf58793c2017-09-03 16:48:34 +0800178 } else {
179 response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE,
180 "null bundle returned");
Fred Quintana31957f12009-10-21 13:43:10 -0700181 }
Fred Quintana5d1a0c32011-06-23 16:37:58 -0700182 } catch (Exception e) {
183 handleException(response, "addAccount", accountType, e);
Fred Quintanaa698f422009-04-08 19:14:54 -0700184 }
Fred Quintana60307342009-03-24 22:48:12 -0700185 }
186
Carlos Valdivia91979be2015-05-22 14:11:35 -0700187 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -0700188 public void confirmCredentials(IAccountAuthenticatorResponse response,
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700189 Account account, Bundle options) throws RemoteException {
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800190 if (Log.isLoggable(TAG, Log.VERBOSE)) {
191 Log.v(TAG, "confirmCredentials: " + account);
192 }
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700193 checkBinderPermission();
Fred Quintanaa698f422009-04-08 19:14:54 -0700194 try {
Fred Quintana31957f12009-10-21 13:43:10 -0700195 final Bundle result = AbstractAccountAuthenticator.this.confirmCredentials(
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700196 new AccountAuthenticatorResponse(response), account, options);
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800197 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Ian Pedowitz6cc066d2015-08-05 14:23:43 +0000198 if (result != null) {
199 result.keySet(); // force it to be unparcelled
200 }
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800201 Log.v(TAG, "confirmCredentials: result "
202 + AccountManager.sanitizeResult(result));
203 }
Fred Quintana31957f12009-10-21 13:43:10 -0700204 if (result != null) {
205 response.onResult(result);
206 }
Fred Quintana5d1a0c32011-06-23 16:37:58 -0700207 } catch (Exception e) {
208 handleException(response, "confirmCredentials", account.toString(), e);
Fred Quintanaa698f422009-04-08 19:14:54 -0700209 }
Fred Quintana60307342009-03-24 22:48:12 -0700210 }
211
Carlos Valdivia91979be2015-05-22 14:11:35 -0700212 @Override
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700213 public void getAuthTokenLabel(IAccountAuthenticatorResponse response,
214 String authTokenType)
215 throws RemoteException {
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800216 if (Log.isLoggable(TAG, Log.VERBOSE)) {
217 Log.v(TAG, "getAuthTokenLabel: authTokenType " + authTokenType);
218 }
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700219 checkBinderPermission();
220 try {
221 Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700222 result.putString(AccountManager.KEY_AUTH_TOKEN_LABEL,
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700223 AbstractAccountAuthenticator.this.getAuthTokenLabel(authTokenType));
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800224 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Ian Pedowitz6cc066d2015-08-05 14:23:43 +0000225 if (result != null) {
226 result.keySet(); // force it to be unparcelled
227 }
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800228 Log.v(TAG, "getAuthTokenLabel: result "
229 + AccountManager.sanitizeResult(result));
230 }
Fred Quintana5d1a0c32011-06-23 16:37:58 -0700231 response.onResult(result);
232 } catch (Exception e) {
233 handleException(response, "getAuthTokenLabel", authTokenType, e);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700234 }
235 }
236
Carlos Valdivia91979be2015-05-22 14:11:35 -0700237 @Override
Fred Quintana60307342009-03-24 22:48:12 -0700238 public void getAuthToken(IAccountAuthenticatorResponse response,
Fred Quintanaa698f422009-04-08 19:14:54 -0700239 Account account, String authTokenType, Bundle loginOptions)
Fred Quintana60307342009-03-24 22:48:12 -0700240 throws RemoteException {
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800241 if (Log.isLoggable(TAG, Log.VERBOSE)) {
242 Log.v(TAG, "getAuthToken: " + account
243 + ", authTokenType " + authTokenType);
244 }
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700245 checkBinderPermission();
Fred Quintanaa698f422009-04-08 19:14:54 -0700246 try {
247 final Bundle result = AbstractAccountAuthenticator.this.getAuthToken(
248 new AccountAuthenticatorResponse(response), account,
249 authTokenType, loginOptions);
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800250 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Ian Pedowitz6cc066d2015-08-05 14:23:43 +0000251 if (result != null) {
252 result.keySet(); // force it to be unparcelled
253 }
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800254 Log.v(TAG, "getAuthToken: result " + AccountManager.sanitizeResult(result));
255 }
Fred Quintanaa698f422009-04-08 19:14:54 -0700256 if (result != null) {
257 response.onResult(result);
258 }
Fred Quintana5d1a0c32011-06-23 16:37:58 -0700259 } catch (Exception e) {
260 handleException(response, "getAuthToken",
261 account.toString() + "," + authTokenType, e);
Fred Quintanaa698f422009-04-08 19:14:54 -0700262 }
Fred Quintana60307342009-03-24 22:48:12 -0700263 }
264
Carlos Valdivia91979be2015-05-22 14:11:35 -0700265 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -0700266 public void updateCredentials(IAccountAuthenticatorResponse response, Account account,
267 String authTokenType, Bundle loginOptions) throws RemoteException {
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800268 if (Log.isLoggable(TAG, Log.VERBOSE)) {
269 Log.v(TAG, "updateCredentials: " + account
270 + ", authTokenType " + authTokenType);
271 }
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700272 checkBinderPermission();
Fred Quintanaa698f422009-04-08 19:14:54 -0700273 try {
Fred Quintana31957f12009-10-21 13:43:10 -0700274 final Bundle result = AbstractAccountAuthenticator.this.updateCredentials(
Fred Quintanaa698f422009-04-08 19:14:54 -0700275 new AccountAuthenticatorResponse(response), account,
276 authTokenType, loginOptions);
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800277 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Ian Pedowitz6cc066d2015-08-05 14:23:43 +0000278 // Result may be null.
279 if (result != null) {
280 result.keySet(); // force it to be unparcelled
281 }
Fred Quintanaf0fd8432010-03-08 12:48:05 -0800282 Log.v(TAG, "updateCredentials: result "
283 + AccountManager.sanitizeResult(result));
284 }
Fred Quintana31957f12009-10-21 13:43:10 -0700285 if (result != null) {
286 response.onResult(result);
287 }
Fred Quintana5d1a0c32011-06-23 16:37:58 -0700288 } catch (Exception e) {
289 handleException(response, "updateCredentials",
290 account.toString() + "," + authTokenType, e);
Fred Quintanaa698f422009-04-08 19:14:54 -0700291 }
Fred Quintana60307342009-03-24 22:48:12 -0700292 }
293
Carlos Valdivia91979be2015-05-22 14:11:35 -0700294 @Override
Fred Quintanaa698f422009-04-08 19:14:54 -0700295 public void editProperties(IAccountAuthenticatorResponse response,
296 String accountType) throws RemoteException {
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700297 checkBinderPermission();
Fred Quintanaa698f422009-04-08 19:14:54 -0700298 try {
Fred Quintana31957f12009-10-21 13:43:10 -0700299 final Bundle result = AbstractAccountAuthenticator.this.editProperties(
Fred Quintana60307342009-03-24 22:48:12 -0700300 new AccountAuthenticatorResponse(response), accountType);
Fred Quintana31957f12009-10-21 13:43:10 -0700301 if (result != null) {
302 response.onResult(result);
303 }
Fred Quintana5d1a0c32011-06-23 16:37:58 -0700304 } catch (Exception e) {
305 handleException(response, "editProperties", accountType, e);
Fred Quintanaa698f422009-04-08 19:14:54 -0700306 }
Fred Quintana60307342009-03-24 22:48:12 -0700307 }
Fred Quintana33269202009-04-20 16:05:10 -0700308
Carlos Valdivia91979be2015-05-22 14:11:35 -0700309 @Override
Fred Quintana33269202009-04-20 16:05:10 -0700310 public void hasFeatures(IAccountAuthenticatorResponse response,
311 Account account, String[] features) throws RemoteException {
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700312 checkBinderPermission();
Fred Quintana33269202009-04-20 16:05:10 -0700313 try {
Fred Quintana31957f12009-10-21 13:43:10 -0700314 final Bundle result = AbstractAccountAuthenticator.this.hasFeatures(
Fred Quintana33269202009-04-20 16:05:10 -0700315 new AccountAuthenticatorResponse(response), account, features);
Fred Quintana31957f12009-10-21 13:43:10 -0700316 if (result != null) {
317 response.onResult(result);
318 }
Fred Quintana5d1a0c32011-06-23 16:37:58 -0700319 } catch (Exception e) {
320 handleException(response, "hasFeatures", account.toString(), e);
Fred Quintana33269202009-04-20 16:05:10 -0700321 }
322 }
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700323
Carlos Valdivia91979be2015-05-22 14:11:35 -0700324 @Override
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700325 public void getAccountRemovalAllowed(IAccountAuthenticatorResponse response,
326 Account account) throws RemoteException {
327 checkBinderPermission();
328 try {
329 final Bundle result = AbstractAccountAuthenticator.this.getAccountRemovalAllowed(
330 new AccountAuthenticatorResponse(response), account);
331 if (result != null) {
332 response.onResult(result);
333 }
Fred Quintana5d1a0c32011-06-23 16:37:58 -0700334 } catch (Exception e) {
335 handleException(response, "getAccountRemovalAllowed", account.toString(), e);
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700336 }
337 }
Amith Yamasani67df64b2012-12-14 12:09:36 -0800338
Carlos Valdivia91979be2015-05-22 14:11:35 -0700339 @Override
Amith Yamasani67df64b2012-12-14 12:09:36 -0800340 public void getAccountCredentialsForCloning(IAccountAuthenticatorResponse response,
341 Account account) throws RemoteException {
342 checkBinderPermission();
343 try {
344 final Bundle result =
345 AbstractAccountAuthenticator.this.getAccountCredentialsForCloning(
346 new AccountAuthenticatorResponse(response), account);
347 if (result != null) {
348 response.onResult(result);
349 }
350 } catch (Exception e) {
351 handleException(response, "getAccountCredentialsForCloning", account.toString(), e);
352 }
353 }
354
Carlos Valdivia91979be2015-05-22 14:11:35 -0700355 @Override
Amith Yamasani67df64b2012-12-14 12:09:36 -0800356 public void addAccountFromCredentials(IAccountAuthenticatorResponse response,
357 Account account,
358 Bundle accountCredentials) throws RemoteException {
359 checkBinderPermission();
360 try {
361 final Bundle result =
362 AbstractAccountAuthenticator.this.addAccountFromCredentials(
363 new AccountAuthenticatorResponse(response), account,
364 accountCredentials);
365 if (result != null) {
366 response.onResult(result);
367 }
368 } catch (Exception e) {
369 handleException(response, "addAccountFromCredentials", account.toString(), e);
370 }
371 }
Sandra Kwan78812282015-11-04 11:19:47 -0800372
373 @Override
374 public void startAddAccountSession(IAccountAuthenticatorResponse response,
375 String accountType, String authTokenType, String[] features, Bundle options)
376 throws RemoteException {
377 if (Log.isLoggable(TAG, Log.VERBOSE)) {
378 Log.v(TAG,
379 "startAddAccountSession: accountType " + accountType
380 + ", authTokenType " + authTokenType
381 + ", features " + (features == null ? "[]" : Arrays.toString(features)));
382 }
383 checkBinderPermission();
384 try {
385 final Bundle result = AbstractAccountAuthenticator.this.startAddAccountSession(
386 new AccountAuthenticatorResponse(response), accountType, authTokenType,
387 features, options);
388 if (Log.isLoggable(TAG, Log.VERBOSE)) {
389 if (result != null) {
390 result.keySet(); // force it to be unparcelled
391 }
392 Log.v(TAG, "startAddAccountSession: result "
393 + AccountManager.sanitizeResult(result));
394 }
395 if (result != null) {
396 response.onResult(result);
397 }
398 } catch (Exception e) {
399 handleException(response, "startAddAccountSession", accountType, e);
400 }
401 }
Sandra Kwane68c37e2015-11-12 17:11:49 -0800402
403 @Override
404 public void startUpdateCredentialsSession(
405 IAccountAuthenticatorResponse response,
406 Account account,
407 String authTokenType,
408 Bundle loginOptions) throws RemoteException {
409 if (Log.isLoggable(TAG, Log.VERBOSE)) {
410 Log.v(TAG, "startUpdateCredentialsSession: "
411 + account
412 + ", authTokenType "
413 + authTokenType);
414 }
415 checkBinderPermission();
416 try {
417 final Bundle result = AbstractAccountAuthenticator.this
418 .startUpdateCredentialsSession(
419 new AccountAuthenticatorResponse(response),
420 account,
421 authTokenType,
422 loginOptions);
423 if (Log.isLoggable(TAG, Log.VERBOSE)) {
424 // Result may be null.
425 if (result != null) {
426 result.keySet(); // force it to be unparcelled
427 }
428 Log.v(TAG, "startUpdateCredentialsSession: result "
429 + AccountManager.sanitizeResult(result));
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800430
Sandra Kwane68c37e2015-11-12 17:11:49 -0800431 }
432 if (result != null) {
433 response.onResult(result);
434 }
435 } catch (Exception e) {
436 handleException(response, "startUpdateCredentialsSession",
437 account.toString() + "," + authTokenType, e);
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800438
439 }
440 }
441
Sandra Kwan390c9d22016-01-12 14:13:37 -0800442 @Override
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800443 public void finishSession(
444 IAccountAuthenticatorResponse response,
445 String accountType,
446 Bundle sessionBundle) throws RemoteException {
447 if (Log.isLoggable(TAG, Log.VERBOSE)) {
448 Log.v(TAG, "finishSession: accountType " + accountType);
449 }
450 checkBinderPermission();
451 try {
452 final Bundle result = AbstractAccountAuthenticator.this.finishSession(
453 new AccountAuthenticatorResponse(response), accountType, sessionBundle);
454 if (result != null) {
455 result.keySet(); // force it to be unparcelled
456 }
457 if (Log.isLoggable(TAG, Log.VERBOSE)) {
458 Log.v(TAG, "finishSession: result " + AccountManager.sanitizeResult(result));
459 }
460 if (result != null) {
461 response.onResult(result);
462 }
463 } catch (Exception e) {
464 handleException(response, "finishSession", accountType, e);
465
Sandra Kwane68c37e2015-11-12 17:11:49 -0800466 }
467 }
Sandra Kwan390c9d22016-01-12 14:13:37 -0800468
469 @Override
470 public void isCredentialsUpdateSuggested(
471 IAccountAuthenticatorResponse response,
472 Account account,
473 String statusToken) throws RemoteException {
474 checkBinderPermission();
475 try {
476 final Bundle result = AbstractAccountAuthenticator.this
477 .isCredentialsUpdateSuggested(
478 new AccountAuthenticatorResponse(response), account, statusToken);
479 if (result != null) {
480 response.onResult(result);
481 }
482 } catch (Exception e) {
483 handleException(response, "isCredentialsUpdateSuggested", account.toString(), e);
484 }
485 }
Fred Quintana60307342009-03-24 22:48:12 -0700486 }
487
Fred Quintana5d1a0c32011-06-23 16:37:58 -0700488 private void handleException(IAccountAuthenticatorResponse response, String method,
489 String data, Exception e) throws RemoteException {
490 if (e instanceof NetworkErrorException) {
491 if (Log.isLoggable(TAG, Log.VERBOSE)) {
492 Log.v(TAG, method + "(" + data + ")", e);
493 }
494 response.onError(AccountManager.ERROR_CODE_NETWORK_ERROR, e.getMessage());
495 } else if (e instanceof UnsupportedOperationException) {
496 if (Log.isLoggable(TAG, Log.VERBOSE)) {
497 Log.v(TAG, method + "(" + data + ")", e);
498 }
499 response.onError(AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION,
500 method + " not supported");
501 } else if (e instanceof IllegalArgumentException) {
502 if (Log.isLoggable(TAG, Log.VERBOSE)) {
503 Log.v(TAG, method + "(" + data + ")", e);
504 }
505 response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS,
506 method + " not supported");
507 } else {
508 Log.w(TAG, method + "(" + data + ")", e);
509 response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION,
510 method + " failed");
511 }
512 }
513
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700514 private void checkBinderPermission() {
515 final int uid = Binder.getCallingUid();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700516 final String perm = Manifest.permission.ACCOUNT_MANAGER;
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700517 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
518 throw new SecurityException("caller uid " + uid + " lacks " + perm);
519 }
520 }
521
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700522 private Transport mTransport = new Transport();
Fred Quintana60307342009-03-24 22:48:12 -0700523
524 /**
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700525 * @return the IBinder for the AccountAuthenticator
Fred Quintana60307342009-03-24 22:48:12 -0700526 */
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700527 public final IBinder getIBinder() {
528 return mTransport.asBinder();
Fred Quintana60307342009-03-24 22:48:12 -0700529 }
530
531 /**
Fred Quintanaa698f422009-04-08 19:14:54 -0700532 * Returns a Bundle that contains the Intent of the activity that can be used to edit the
533 * properties. In order to indicate success the activity should call response.setResult()
534 * with a non-null Bundle.
535 * @param response used to set the result for the request. If the Constants.INTENT_KEY
536 * is set in the bundle then this response field is to be used for sending future
537 * results if and when the Intent is started.
538 * @param accountType the AccountType whose properties are to be edited.
539 * @return a Bundle containing the result or the Intent to start to continue the request.
540 * If this is null then the request is considered to still be active and the result should
541 * sent later using response.
Fred Quintana60307342009-03-24 22:48:12 -0700542 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700543 public abstract Bundle editProperties(AccountAuthenticatorResponse response,
544 String accountType);
Fred Quintana756b7352009-10-21 13:43:10 -0700545
546 /**
547 * Adds an account of the specified accountType.
548 * @param response to send the result back to the AccountManager, will never be null
549 * @param accountType the type of account to add, will never be null
550 * @param authTokenType the type of auth token to retrieve after adding the account, may be null
551 * @param requiredFeatures a String array of authenticator-specific features that the added
552 * account must support, may be null
sunjian87d0c3c2017-07-14 11:54:46 -0700553 * @param options a Bundle of authenticator-specific options. It always contains
554 * {@link AccountManager#KEY_CALLER_PID} and {@link AccountManager#KEY_CALLER_UID}
555 * fields which will let authenticator know the identity of the caller.
Fred Quintana756b7352009-10-21 13:43:10 -0700556 * @return a Bundle result or null if the result is to be returned via the response. The result
557 * will contain either:
558 * <ul>
559 * <li> {@link AccountManager#KEY_INTENT}, or
560 * <li> {@link AccountManager#KEY_ACCOUNT_NAME} and {@link AccountManager#KEY_ACCOUNT_TYPE} of
Fred Quintana8570f742010-02-18 10:32:54 -0800561 * the account that was added, or
Fred Quintana756b7352009-10-21 13:43:10 -0700562 * <li> {@link AccountManager#KEY_ERROR_CODE} and {@link AccountManager#KEY_ERROR_MESSAGE} to
563 * indicate an error
564 * </ul>
565 * @throws NetworkErrorException if the authenticator could not honor the request due to a
566 * network error
567 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700568 public abstract Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
Fred Quintana33269202009-04-20 16:05:10 -0700569 String authTokenType, String[] requiredFeatures, Bundle options)
570 throws NetworkErrorException;
Fred Quintana756b7352009-10-21 13:43:10 -0700571
572 /**
573 * Checks that the user knows the credentials of an account.
574 * @param response to send the result back to the AccountManager, will never be null
575 * @param account the account whose credentials are to be checked, will never be null
576 * @param options a Bundle of authenticator-specific options, may be null
577 * @return a Bundle result or null if the result is to be returned via the response. The result
578 * will contain either:
579 * <ul>
580 * <li> {@link AccountManager#KEY_INTENT}, or
581 * <li> {@link AccountManager#KEY_BOOLEAN_RESULT}, true if the check succeeded, false otherwise
582 * <li> {@link AccountManager#KEY_ERROR_CODE} and {@link AccountManager#KEY_ERROR_MESSAGE} to
583 * indicate an error
584 * </ul>
Fred Quintana31957f12009-10-21 13:43:10 -0700585 * @throws NetworkErrorException if the authenticator could not honor the request due to a
586 * network error
Fred Quintana756b7352009-10-21 13:43:10 -0700587 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700588 public abstract Bundle confirmCredentials(AccountAuthenticatorResponse response,
Fred Quintana31957f12009-10-21 13:43:10 -0700589 Account account, Bundle options)
590 throws NetworkErrorException;
Carlos Valdivia91979be2015-05-22 14:11:35 -0700591
Fred Quintana756b7352009-10-21 13:43:10 -0700592 /**
Carlos Valdivia91979be2015-05-22 14:11:35 -0700593 * Gets an authtoken for an account.
594 *
595 * If not {@code null}, the resultant {@link Bundle} will contain different sets of keys
596 * depending on whether a token was successfully issued and, if not, whether one
597 * could be issued via some {@link android.app.Activity}.
598 * <p>
599 * If a token cannot be provided without some additional activity, the Bundle should contain
600 * {@link AccountManager#KEY_INTENT} with an associated {@link Intent}. On the other hand, if
601 * there is no such activity, then a Bundle containing
602 * {@link AccountManager#KEY_ERROR_CODE} and {@link AccountManager#KEY_ERROR_MESSAGE} should be
603 * returned.
604 * <p>
605 * If a token can be successfully issued, the implementation should return the
606 * {@link AccountManager#KEY_ACCOUNT_NAME} and {@link AccountManager#KEY_ACCOUNT_TYPE} of the
607 * account associated with the token as well as the {@link AccountManager#KEY_AUTHTOKEN}. In
608 * addition {@link AbstractAccountAuthenticator} implementations that declare themselves
609 * {@code android:customTokens=true} may also provide a non-negative {@link
610 * #KEY_CUSTOM_TOKEN_EXPIRY} long value containing the expiration timestamp of the expiration
sunjian87d0c3c2017-07-14 11:54:46 -0700611 * time (in millis since the unix epoch), tokens will be cached in memory based on
612 * application's packageName/signature for however long that was specified.
Carlos Valdivia91979be2015-05-22 14:11:35 -0700613 * <p>
614 * Implementers should assume that tokens will be cached on the basis of account and
615 * authTokenType. The system may ignore the contents of the supplied options Bundle when
616 * determining to re-use a cached token. Furthermore, implementers should assume a supplied
617 * expiration time will be treated as non-binding advice.
618 * <p>
sunjian87d0c3c2017-07-14 11:54:46 -0700619 * Finally, note that for {@code android:customTokens=false} authenticators, tokens are cached
Carlos Valdivia91979be2015-05-22 14:11:35 -0700620 * indefinitely until some client calls {@link
621 * AccountManager#invalidateAuthToken(String,String)}.
622 *
Fred Quintana756b7352009-10-21 13:43:10 -0700623 * @param response to send the result back to the AccountManager, will never be null
624 * @param account the account whose credentials are to be retrieved, will never be null
625 * @param authTokenType the type of auth token to retrieve, will never be null
sunjian87d0c3c2017-07-14 11:54:46 -0700626 * @param options a Bundle of authenticator-specific options. It always contains
627 * {@link AccountManager#KEY_CALLER_PID} and {@link AccountManager#KEY_CALLER_UID}
628 * fields which will let authenticator know the identity of the caller.
Carlos Valdivia91979be2015-05-22 14:11:35 -0700629 * @return a Bundle result or null if the result is to be returned via the response.
Fred Quintana756b7352009-10-21 13:43:10 -0700630 * @throws NetworkErrorException if the authenticator could not honor the request due to a
631 * network error
632 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700633 public abstract Bundle getAuthToken(AccountAuthenticatorResponse response,
Fred Quintana31957f12009-10-21 13:43:10 -0700634 Account account, String authTokenType, Bundle options)
Fred Quintanaa698f422009-04-08 19:14:54 -0700635 throws NetworkErrorException;
Fred Quintana756b7352009-10-21 13:43:10 -0700636
637 /**
638 * Ask the authenticator for a localized label for the given authTokenType.
639 * @param authTokenType the authTokenType whose label is to be returned, will never be null
640 * @return the localized label of the auth token type, may be null if the type isn't known
641 */
Fred Quintanad4a1d2e2009-07-16 16:36:38 -0700642 public abstract String getAuthTokenLabel(String authTokenType);
Fred Quintana756b7352009-10-21 13:43:10 -0700643
644 /**
645 * Update the locally stored credentials for an account.
646 * @param response to send the result back to the AccountManager, will never be null
647 * @param account the account whose credentials are to be updated, will never be null
648 * @param authTokenType the type of auth token to retrieve after updating the credentials,
649 * may be null
Fred Quintana31957f12009-10-21 13:43:10 -0700650 * @param options a Bundle of authenticator-specific options, may be null
Fred Quintana756b7352009-10-21 13:43:10 -0700651 * @return a Bundle result or null if the result is to be returned via the response. The result
652 * will contain either:
653 * <ul>
654 * <li> {@link AccountManager#KEY_INTENT}, or
655 * <li> {@link AccountManager#KEY_ACCOUNT_NAME} and {@link AccountManager#KEY_ACCOUNT_TYPE} of
Ian Pedowitz6cc066d2015-08-05 14:23:43 +0000656 * the account whose credentials were updated, or
Fred Quintana756b7352009-10-21 13:43:10 -0700657 * <li> {@link AccountManager#KEY_ERROR_CODE} and {@link AccountManager#KEY_ERROR_MESSAGE} to
658 * indicate an error
659 * </ul>
Fred Quintana31957f12009-10-21 13:43:10 -0700660 * @throws NetworkErrorException if the authenticator could not honor the request due to a
661 * network error
Fred Quintana756b7352009-10-21 13:43:10 -0700662 */
Fred Quintanaa698f422009-04-08 19:14:54 -0700663 public abstract Bundle updateCredentials(AccountAuthenticatorResponse response,
Fred Quintana31957f12009-10-21 13:43:10 -0700664 Account account, String authTokenType, Bundle options) throws NetworkErrorException;
Fred Quintana8570f742010-02-18 10:32:54 -0800665
Fred Quintana756b7352009-10-21 13:43:10 -0700666 /**
667 * Checks if the account supports all the specified authenticator specific features.
668 * @param response to send the result back to the AccountManager, will never be null
669 * @param account the account to check, will never be null
670 * @param features an array of features to check, will never be null
671 * @return a Bundle result or null if the result is to be returned via the response. The result
672 * will contain either:
673 * <ul>
674 * <li> {@link AccountManager#KEY_INTENT}, or
675 * <li> {@link AccountManager#KEY_BOOLEAN_RESULT}, true if the account has all the features,
676 * false otherwise
677 * <li> {@link AccountManager#KEY_ERROR_CODE} and {@link AccountManager#KEY_ERROR_MESSAGE} to
678 * indicate an error
679 * </ul>
680 * @throws NetworkErrorException if the authenticator could not honor the request due to a
681 * network error
682 */
Fred Quintana33269202009-04-20 16:05:10 -0700683 public abstract Bundle hasFeatures(AccountAuthenticatorResponse response,
684 Account account, String[] features) throws NetworkErrorException;
Fred Quintana756b7352009-10-21 13:43:10 -0700685
686 /**
687 * Checks if the removal of this account is allowed.
688 * @param response to send the result back to the AccountManager, will never be null
689 * @param account the account to check, will never be null
690 * @return a Bundle result or null if the result is to be returned via the response. The result
691 * will contain either:
692 * <ul>
693 * <li> {@link AccountManager#KEY_INTENT}, or
694 * <li> {@link AccountManager#KEY_BOOLEAN_RESULT}, true if the removal of the account is
695 * allowed, false otherwise
696 * <li> {@link AccountManager#KEY_ERROR_CODE} and {@link AccountManager#KEY_ERROR_MESSAGE} to
697 * indicate an error
698 * </ul>
699 * @throws NetworkErrorException if the authenticator could not honor the request due to a
700 * network error
701 */
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700702 public Bundle getAccountRemovalAllowed(AccountAuthenticatorResponse response,
703 Account account) throws NetworkErrorException {
704 final Bundle result = new Bundle();
Fred Quintanaf7ae77c2009-10-02 17:19:31 -0700705 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
Fred Quintanaffd0cb042009-08-15 21:45:26 -0700706 return result;
707 }
Amith Yamasani67df64b2012-12-14 12:09:36 -0800708
709 /**
Amith Yamasani67df64b2012-12-14 12:09:36 -0800710 * Returns a Bundle that contains whatever is required to clone the account on a different
711 * user. The Bundle is passed to the authenticator instance in the target user via
712 * {@link #addAccountFromCredentials(AccountAuthenticatorResponse, Account, Bundle)}.
713 * The default implementation returns null, indicating that cloning is not supported.
714 * @param response to send the result back to the AccountManager, will never be null
715 * @param account the account to clone, will never be null
716 * @return a Bundle result or null if the result is to be returned via the response.
717 * @throws NetworkErrorException
Elliot Waite54de7742017-01-11 15:30:35 -0800718 * @see #addAccountFromCredentials(AccountAuthenticatorResponse, Account, Bundle)
Amith Yamasani67df64b2012-12-14 12:09:36 -0800719 */
720 public Bundle getAccountCredentialsForCloning(final AccountAuthenticatorResponse response,
721 final Account account) throws NetworkErrorException {
722 new Thread(new Runnable() {
Carlos Valdivia91979be2015-05-22 14:11:35 -0700723 @Override
Amith Yamasani67df64b2012-12-14 12:09:36 -0800724 public void run() {
725 Bundle result = new Bundle();
726 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
727 response.onResult(result);
728 }
729 }).start();
730 return null;
731 }
732
733 /**
Amith Yamasani67df64b2012-12-14 12:09:36 -0800734 * Creates an account based on credentials provided by the authenticator instance of another
735 * user on the device, who has chosen to share the account with this user.
736 * @param response to send the result back to the AccountManager, will never be null
737 * @param account the account to clone, will never be null
738 * @param accountCredentials the Bundle containing the required credentials to create the
739 * account. Contents of the Bundle are only meaningful to the authenticator. This Bundle is
740 * provided by {@link #getAccountCredentialsForCloning(AccountAuthenticatorResponse, Account)}.
741 * @return a Bundle result or null if the result is to be returned via the response.
742 * @throws NetworkErrorException
Elliot Waite54de7742017-01-11 15:30:35 -0800743 * @see #getAccountCredentialsForCloning(AccountAuthenticatorResponse, Account)
Amith Yamasani67df64b2012-12-14 12:09:36 -0800744 */
745 public Bundle addAccountFromCredentials(final AccountAuthenticatorResponse response,
746 Account account,
747 Bundle accountCredentials) throws NetworkErrorException {
748 new Thread(new Runnable() {
Carlos Valdivia91979be2015-05-22 14:11:35 -0700749 @Override
Amith Yamasani67df64b2012-12-14 12:09:36 -0800750 public void run() {
751 Bundle result = new Bundle();
752 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
753 response.onResult(result);
754 }
755 }).start();
756 return null;
757 }
Sandra Kwan78812282015-11-04 11:19:47 -0800758
759 /**
760 * Starts the add account session to authenticate user to an account of the
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800761 * specified accountType. No file I/O should be performed in this call.
762 * Account should be added to device only when {@link #finishSession} is
763 * called after this.
764 * <p>
765 * Note: when overriding this method, {@link #finishSession} should be
766 * overridden too.
767 * </p>
Sandra Kwan78812282015-11-04 11:19:47 -0800768 *
769 * @param response to send the result back to the AccountManager, will never
770 * be null
771 * @param accountType the type of account to authenticate with, will never
772 * be null
773 * @param authTokenType the type of auth token to retrieve after
774 * authenticating with the account, may be null
775 * @param requiredFeatures a String array of authenticator-specific features
776 * that the account authenticated with must support, may be null
777 * @param options a Bundle of authenticator-specific options, may be null
778 * @return a Bundle result or null if the result is to be returned via the
779 * response. The result will contain either:
780 * <ul>
781 * <li>{@link AccountManager#KEY_INTENT}, or
782 * <li>{@link AccountManager#KEY_ACCOUNT_SESSION_BUNDLE} for adding
783 * the account to device later, and if account is authenticated,
784 * optional {@link AccountManager#KEY_PASSWORD} and
785 * {@link AccountManager#KEY_ACCOUNT_STATUS_TOKEN} for checking the
786 * status of the account, or
787 * <li>{@link AccountManager#KEY_ERROR_CODE} and
788 * {@link AccountManager#KEY_ERROR_MESSAGE} to indicate an error
789 * </ul>
790 * @throws NetworkErrorException if the authenticator could not honor the
791 * request due to a network error
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800792 * @see #finishSession(AccountAuthenticatorResponse, String, Bundle)
Sandra Kwan78812282015-11-04 11:19:47 -0800793 */
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800794 public Bundle startAddAccountSession(
795 final AccountAuthenticatorResponse response,
796 final String accountType,
797 final String authTokenType,
798 final String[] requiredFeatures,
Sandra Kwan78812282015-11-04 11:19:47 -0800799 final Bundle options)
800 throws NetworkErrorException {
801 new Thread(new Runnable() {
802 @Override
803 public void run() {
804 Bundle sessionBundle = new Bundle();
805 sessionBundle.putString(KEY_AUTH_TOKEN_TYPE, authTokenType);
806 sessionBundle.putStringArray(KEY_REQUIRED_FEATURES, requiredFeatures);
807 sessionBundle.putBundle(KEY_OPTIONS, options);
808 Bundle result = new Bundle();
809 result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, sessionBundle);
810 response.onResult(result);
811 }
812
813 }).start();
814 return null;
815 }
Sandra Kwane68c37e2015-11-12 17:11:49 -0800816
817 /**
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800818 * Asks user to re-authenticate for an account but defers updating the
819 * locally stored credentials. No file I/O should be performed in this call.
820 * Local credentials should be updated only when {@link #finishSession} is
821 * called after this.
822 * <p>
823 * Note: when overriding this method, {@link #finishSession} should be
824 * overridden too.
825 * </p>
Sandra Kwane68c37e2015-11-12 17:11:49 -0800826 *
827 * @param response to send the result back to the AccountManager, will never
828 * be null
829 * @param account the account whose credentials are to be updated, will
830 * never be null
831 * @param authTokenType the type of auth token to retrieve after updating
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800832 * the credentials, may be null
Sandra Kwane68c37e2015-11-12 17:11:49 -0800833 * @param options a Bundle of authenticator-specific options, may be null
834 * @return a Bundle result or null if the result is to be returned via the
835 * response. The result will contain either:
836 * <ul>
837 * <li>{@link AccountManager#KEY_INTENT}, or
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800838 * <li>{@link AccountManager#KEY_ACCOUNT_SESSION_BUNDLE} for
839 * updating the locally stored credentials later, and if account is
840 * re-authenticated, optional {@link AccountManager#KEY_PASSWORD}
841 * and {@link AccountManager#KEY_ACCOUNT_STATUS_TOKEN} for checking
842 * the status of the account later, or
Sandra Kwane68c37e2015-11-12 17:11:49 -0800843 * <li>{@link AccountManager#KEY_ERROR_CODE} and
844 * {@link AccountManager#KEY_ERROR_MESSAGE} to indicate an error
845 * </ul>
846 * @throws NetworkErrorException if the authenticator could not honor the
847 * request due to a network error
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800848 * @see #finishSession(AccountAuthenticatorResponse, String, Bundle)
Sandra Kwane68c37e2015-11-12 17:11:49 -0800849 */
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800850 public Bundle startUpdateCredentialsSession(
851 final AccountAuthenticatorResponse response,
852 final Account account,
853 final String authTokenType,
854 final Bundle options) throws NetworkErrorException {
Sandra Kwane68c37e2015-11-12 17:11:49 -0800855 new Thread(new Runnable() {
856 @Override
857 public void run() {
858 Bundle sessionBundle = new Bundle();
859 sessionBundle.putString(KEY_AUTH_TOKEN_TYPE, authTokenType);
860 sessionBundle.putParcelable(KEY_ACCOUNT, account);
861 sessionBundle.putBundle(KEY_OPTIONS, options);
862 Bundle result = new Bundle();
863 result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, sessionBundle);
864 response.onResult(result);
865 }
866
867 }).start();
868 return null;
869 }
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800870
871 /**
872 * Finishes the session started by #startAddAccountSession or
873 * #startUpdateCredentials by installing the account to device with
874 * AccountManager, or updating the local credentials. File I/O may be
875 * performed in this call.
876 * <p>
877 * Note: when overriding this method, {@link #startAddAccountSession} and
878 * {@link #startUpdateCredentialsSession} should be overridden too.
879 * </p>
880 *
881 * @param response to send the result back to the AccountManager, will never
882 * be null
883 * @param accountType the type of account to authenticate with, will never
884 * be null
885 * @param sessionBundle a bundle of session data created by
886 * {@link #startAddAccountSession} used for adding account to
887 * device, or by {@link #startUpdateCredentialsSession} used for
888 * updating local credentials.
889 * @return a Bundle result or null if the result is to be returned via the
890 * response. The result will contain either:
891 * <ul>
892 * <li>{@link AccountManager#KEY_INTENT}, or
893 * <li>{@link AccountManager#KEY_ACCOUNT_NAME} and
894 * {@link AccountManager#KEY_ACCOUNT_TYPE} of the account that was
Hongming Jin8b442752016-06-26 10:36:21 -0700895 * added or local credentials were updated, and optional
896 * {@link AccountManager#KEY_ACCOUNT_STATUS_TOKEN} for checking
897 * the status of the account later, or
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800898 * <li>{@link AccountManager#KEY_ERROR_CODE} and
899 * {@link AccountManager#KEY_ERROR_MESSAGE} to indicate an error
900 * </ul>
Sandra Kwan390c9d22016-01-12 14:13:37 -0800901 * @throws NetworkErrorException if the authenticator could not honor the request due to a
902 * network error
Sandra Kwan920f6ef2015-11-10 14:13:29 -0800903 * @see #startAddAccountSession and #startUpdateCredentialsSession
904 */
905 public Bundle finishSession(
906 final AccountAuthenticatorResponse response,
907 final String accountType,
908 final Bundle sessionBundle) throws NetworkErrorException {
909 if (TextUtils.isEmpty(accountType)) {
910 Log.e(TAG, "Account type cannot be empty.");
911 Bundle result = new Bundle();
912 result.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_BAD_ARGUMENTS);
913 result.putString(AccountManager.KEY_ERROR_MESSAGE,
914 "accountType cannot be empty.");
915 return result;
916 }
917
918 if (sessionBundle == null) {
919 Log.e(TAG, "Session bundle cannot be null.");
920 Bundle result = new Bundle();
921 result.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_BAD_ARGUMENTS);
922 result.putString(AccountManager.KEY_ERROR_MESSAGE,
923 "sessionBundle cannot be null.");
924 return result;
925 }
926
927 if (!sessionBundle.containsKey(KEY_AUTH_TOKEN_TYPE)) {
928 // We cannot handle Session bundle not created by default startAddAccountSession(...)
929 // nor startUpdateCredentialsSession(...) implementation. Return error.
930 Bundle result = new Bundle();
931 result.putInt(AccountManager.KEY_ERROR_CODE,
932 AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION);
933 result.putString(AccountManager.KEY_ERROR_MESSAGE,
934 "Authenticator must override finishSession if startAddAccountSession"
935 + " or startUpdateCredentialsSession is overridden.");
936 response.onResult(result);
937 return result;
938 }
939 String authTokenType = sessionBundle.getString(KEY_AUTH_TOKEN_TYPE);
940 Bundle options = sessionBundle.getBundle(KEY_OPTIONS);
941 String[] requiredFeatures = sessionBundle.getStringArray(KEY_REQUIRED_FEATURES);
942 Account account = sessionBundle.getParcelable(KEY_ACCOUNT);
943 boolean containsKeyAccount = sessionBundle.containsKey(KEY_ACCOUNT);
944
945 // Actual options passed to add account or update credentials flow.
946 Bundle sessionOptions = new Bundle(sessionBundle);
947 // Remove redundant extras in session bundle before passing it to addAccount(...) or
948 // updateCredentials(...).
949 sessionOptions.remove(KEY_AUTH_TOKEN_TYPE);
950 sessionOptions.remove(KEY_REQUIRED_FEATURES);
951 sessionOptions.remove(KEY_OPTIONS);
952 sessionOptions.remove(KEY_ACCOUNT);
953
954 if (options != null) {
955 // options may contains old system info such as
956 // AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or update
957 // credentials flow, we should replace with the new values of the current call added
958 // to sessionBundle by AccountManager or AccountManagerService.
959 options.putAll(sessionOptions);
960 sessionOptions = options;
961 }
962
963 // Session bundle created by startUpdateCredentialsSession default implementation should
964 // contain KEY_ACCOUNT.
965 if (containsKeyAccount) {
966 return updateCredentials(response, account, authTokenType, options);
967 }
968 // Otherwise, session bundle was created by startAddAccountSession default implementation.
969 return addAccount(response, accountType, authTokenType, requiredFeatures, sessionOptions);
970 }
Sandra Kwan390c9d22016-01-12 14:13:37 -0800971
972 /**
973 * Checks if update of the account credentials is suggested.
974 *
975 * @param response to send the result back to the AccountManager, will never be null.
976 * @param account the account to check, will never be null
977 * @param statusToken a String of token to check if update of credentials is suggested.
978 * @return a Bundle result or null if the result is to be returned via the response. The result
979 * will contain either:
980 * <ul>
981 * <li>{@link AccountManager#KEY_BOOLEAN_RESULT}, true if update of account's
982 * credentials is suggested, false otherwise
983 * <li>{@link AccountManager#KEY_ERROR_CODE} and
984 * {@link AccountManager#KEY_ERROR_MESSAGE} to indicate an error
985 * </ul>
986 * @throws NetworkErrorException if the authenticator could not honor the request due to a
987 * network error
Sandra Kwan390c9d22016-01-12 14:13:37 -0800988 */
Sandra Kwan390c9d22016-01-12 14:13:37 -0800989 public Bundle isCredentialsUpdateSuggested(
990 final AccountAuthenticatorResponse response,
991 Account account,
992 String statusToken) throws NetworkErrorException {
993 Bundle result = new Bundle();
994 result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
995 return result;
996 }
Fred Quintana60307342009-03-24 22:48:12 -0700997}