Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.accounts; |
| 18 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 19 | import static android.Manifest.permission.GET_ACCOUNTS; |
| 20 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Tor Norbye | 80b530a | 2015-04-23 16:36:09 -0700 | [diff] [blame] | 22 | import android.annotation.RequiresPermission; |
| 23 | import android.annotation.Size; |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 24 | import android.annotation.SystemApi; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 25 | import android.app.Activity; |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 26 | import android.content.BroadcastReceiver; |
Amith Yamasani | 12b8e13 | 2013-03-14 10:48:07 -0700 | [diff] [blame] | 27 | import android.content.ComponentName; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 28 | import android.content.Context; |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 29 | import android.content.Intent; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 30 | import android.content.IntentFilter; |
Amith Yamasani | 12b8e13 | 2013-03-14 10:48:07 -0700 | [diff] [blame] | 31 | import android.content.res.Resources; |
Costin Manolache | b643724 | 2009-09-10 16:14:12 -0700 | [diff] [blame] | 32 | import android.database.SQLException; |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 33 | import android.os.Build; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 34 | import android.os.Bundle; |
| 35 | import android.os.Handler; |
| 36 | import android.os.Looper; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 37 | import android.os.Parcelable; |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 38 | import android.os.Process; |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 39 | import android.os.RemoteException; |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 40 | import android.os.UserHandle; |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 41 | import android.text.TextUtils; |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 42 | import android.util.Log; |
| 43 | |
| 44 | import com.android.internal.R; |
| 45 | import com.google.android.collect.Maps; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 46 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 47 | import java.io.IOException; |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 48 | import java.util.ArrayList; |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 49 | import java.util.HashMap; |
Carlos Valdivia | a3db8ac | 2015-07-10 13:04:43 -0700 | [diff] [blame] | 50 | import java.util.List; |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 51 | import java.util.Map; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 52 | import java.util.concurrent.Callable; |
| 53 | import java.util.concurrent.CancellationException; |
| 54 | import java.util.concurrent.ExecutionException; |
| 55 | import java.util.concurrent.FutureTask; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 56 | import java.util.concurrent.TimeUnit; |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 57 | import java.util.concurrent.TimeoutException; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 60 | * This class provides access to a centralized registry of the user's |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 61 | * online accounts. The user enters credentials (username and password) once |
| 62 | * per account, granting applications access to online resources with |
| 63 | * "one-click" approval. |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 64 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 65 | * <p>Different online services have different ways of handling accounts and |
| 66 | * authentication, so the account manager uses pluggable <em>authenticator</em> |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 67 | * modules for different <em>account types</em>. Authenticators (which may be |
| 68 | * written by third parties) handle the actual details of validating account |
| 69 | * credentials and storing account information. For example, Google, Facebook, |
| 70 | * and Microsoft Exchange each have their own authenticator. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 71 | * |
| 72 | * <p>Many servers support some notion of an <em>authentication token</em>, |
| 73 | * which can be used to authenticate a request to the server without sending |
| 74 | * the user's actual password. (Auth tokens are normally created with a |
| 75 | * separate request which does include the user's credentials.) AccountManager |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 76 | * can generate auth tokens for applications, so the application doesn't need to |
| 77 | * handle passwords directly. Auth tokens are normally reusable and cached by |
| 78 | * AccountManager, but must be refreshed periodically. It's the responsibility |
| 79 | * of applications to <em>invalidate</em> auth tokens when they stop working so |
| 80 | * the AccountManager knows it needs to regenerate them. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 81 | * |
| 82 | * <p>Applications accessing a server normally go through these steps: |
| 83 | * |
| 84 | * <ul> |
| 85 | * <li>Get an instance of AccountManager using {@link #get(Context)}. |
| 86 | * |
| 87 | * <li>List the available accounts using {@link #getAccountsByType} or |
| 88 | * {@link #getAccountsByTypeAndFeatures}. Normally applications will only |
| 89 | * be interested in accounts with one particular <em>type</em>, which |
| 90 | * identifies the authenticator. Account <em>features</em> are used to |
| 91 | * identify particular account subtypes and capabilities. Both the account |
| 92 | * type and features are authenticator-specific strings, and must be known by |
| 93 | * the application in coordination with its preferred authenticators. |
| 94 | * |
| 95 | * <li>Select one or more of the available accounts, possibly by asking the |
| 96 | * user for their preference. If no suitable accounts are available, |
| 97 | * {@link #addAccount} may be called to prompt the user to create an |
| 98 | * account of the appropriate type. |
| 99 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 100 | * <li><b>Important:</b> If the application is using a previously remembered |
| 101 | * account selection, it must make sure the account is still in the list |
| 102 | * of accounts returned by {@link #getAccountsByType}. Requesting an auth token |
| 103 | * for an account no longer on the device results in an undefined failure. |
| 104 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 105 | * <li>Request an auth token for the selected account(s) using one of the |
| 106 | * {@link #getAuthToken} methods or related helpers. Refer to the description |
| 107 | * of each method for exact usage and error handling details. |
| 108 | * |
| 109 | * <li>Make the request using the auth token. The form of the auth token, |
| 110 | * the format of the request, and the protocol used are all specific to the |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 111 | * service you are accessing. The application may use whatever network and |
| 112 | * protocol libraries are useful. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 113 | * |
| 114 | * <li><b>Important:</b> If the request fails with an authentication error, |
| 115 | * it could be that a cached auth token is stale and no longer honored by |
| 116 | * the server. The application must call {@link #invalidateAuthToken} to remove |
| 117 | * the token from the cache, otherwise requests will continue failing! After |
| 118 | * invalidating the auth token, immediately go back to the "Request an auth |
| 119 | * token" step above. If the process fails the second time, then it can be |
| 120 | * treated as a "genuine" authentication failure and the user notified or other |
| 121 | * appropriate actions taken. |
| 122 | * </ul> |
| 123 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 124 | * <p>Some AccountManager methods may need to interact with the user to |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 125 | * prompt for credentials, present options, or ask the user to add an account. |
| 126 | * The caller may choose whether to allow AccountManager to directly launch the |
| 127 | * necessary user interface and wait for the user, or to return an Intent which |
| 128 | * the caller may use to launch the interface, or (in some cases) to install a |
| 129 | * notification which the user can select at any time to launch the interface. |
| 130 | * To have AccountManager launch the interface directly, the caller must supply |
| 131 | * the current foreground {@link Activity} context. |
| 132 | * |
| 133 | * <p>Many AccountManager methods take {@link AccountManagerCallback} and |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 134 | * {@link Handler} as parameters. These methods return immediately and |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 135 | * run asynchronously. If a callback is provided then |
| 136 | * {@link AccountManagerCallback#run} will be invoked on the Handler's |
| 137 | * thread when the request completes, successfully or not. |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 138 | * The result is retrieved by calling {@link AccountManagerFuture#getResult()} |
| 139 | * on the {@link AccountManagerFuture} returned by the method (and also passed |
| 140 | * to the callback). This method waits for the operation to complete (if |
| 141 | * necessary) and either returns the result or throws an exception if an error |
| 142 | * occurred during the operation. To make the request synchronously, call |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 143 | * {@link AccountManagerFuture#getResult()} immediately on receiving the |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 144 | * future from the method; no callback need be supplied. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 145 | * |
| 146 | * <p>Requests which may block, including |
| 147 | * {@link AccountManagerFuture#getResult()}, must never be called on |
| 148 | * the application's main event thread. These operations throw |
| 149 | * {@link IllegalStateException} if they are used on the main thread. |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 150 | */ |
| 151 | public class AccountManager { |
| 152 | private static final String TAG = "AccountManager"; |
| 153 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 154 | public static final int ERROR_CODE_REMOTE_EXCEPTION = 1; |
| 155 | public static final int ERROR_CODE_NETWORK_ERROR = 3; |
| 156 | public static final int ERROR_CODE_CANCELED = 4; |
| 157 | public static final int ERROR_CODE_INVALID_RESPONSE = 5; |
| 158 | public static final int ERROR_CODE_UNSUPPORTED_OPERATION = 6; |
| 159 | public static final int ERROR_CODE_BAD_ARGUMENTS = 7; |
| 160 | public static final int ERROR_CODE_BAD_REQUEST = 8; |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 161 | public static final int ERROR_CODE_BAD_AUTHENTICATION = 9; |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 162 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 163 | /** @hide */ |
| 164 | public static final int ERROR_CODE_USER_RESTRICTED = 100; |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 165 | /** @hide */ |
| 166 | public static final int ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE = 101; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 167 | |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 168 | /** |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 169 | * Bundle key used for the {@link String} account name in results |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 170 | * from methods which return information about a particular account. |
| 171 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 172 | public static final String KEY_ACCOUNT_NAME = "authAccount"; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 173 | |
| 174 | /** |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 175 | * Bundle key used for the {@link String} account type in results |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 176 | * from methods which return information about a particular account. |
| 177 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 178 | public static final String KEY_ACCOUNT_TYPE = "accountType"; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 179 | |
| 180 | /** |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 181 | * Bundle key used for the auth token value in results |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 182 | * from {@link #getAuthToken} and friends. |
| 183 | */ |
| 184 | public static final String KEY_AUTHTOKEN = "authtoken"; |
| 185 | |
| 186 | /** |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 187 | * Bundle key used for an {@link Intent} in results from methods that |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 188 | * may require the caller to interact with the user. The Intent can |
| 189 | * be used to start the corresponding user interface activity. |
| 190 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 191 | public static final String KEY_INTENT = "intent"; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 192 | |
| 193 | /** |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 194 | * Bundle key used to supply the password directly in options to |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 195 | * {@link #confirmCredentials}, rather than prompting the user with |
| 196 | * the standard password prompt. |
| 197 | */ |
| 198 | public static final String KEY_PASSWORD = "password"; |
| 199 | |
| 200 | public static final String KEY_ACCOUNTS = "accounts"; |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 201 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 202 | public static final String KEY_ACCOUNT_AUTHENTICATOR_RESPONSE = "accountAuthenticatorResponse"; |
| 203 | public static final String KEY_ACCOUNT_MANAGER_RESPONSE = "accountManagerResponse"; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 204 | public static final String KEY_AUTHENTICATOR_TYPES = "authenticator_types"; |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 205 | public static final String KEY_AUTH_FAILED_MESSAGE = "authFailedMessage"; |
| 206 | public static final String KEY_AUTH_TOKEN_LABEL = "authTokenLabelKey"; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 207 | public static final String KEY_BOOLEAN_RESULT = "booleanResult"; |
| 208 | public static final String KEY_ERROR_CODE = "errorCode"; |
| 209 | public static final String KEY_ERROR_MESSAGE = "errorMessage"; |
| 210 | public static final String KEY_USERDATA = "userdata"; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 211 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 212 | /** |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 213 | * Bundle key used to supply the last time the credentials of the account |
| 214 | * were authenticated successfully. Time is specified in milliseconds since |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 215 | * epoch. Associated time is updated on successful authentication of account |
| 216 | * on adding account, confirming credentials, or updating credentials. |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 217 | */ |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 218 | public static final String KEY_LAST_AUTHENTICATED_TIME = "lastAuthenticatedTime"; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 219 | |
| 220 | /** |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 221 | * Authenticators using 'customTokens' option will also get the UID of the |
| 222 | * caller |
| 223 | */ |
| 224 | public static final String KEY_CALLER_UID = "callerUid"; |
| 225 | public static final String KEY_CALLER_PID = "callerPid"; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 226 | |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 227 | /** |
Fred Quintana | ad93a32 | 2011-09-08 13:21:01 -0700 | [diff] [blame] | 228 | * The Android package of the caller will be set in the options bundle by the |
| 229 | * {@link AccountManager} and will be passed to the AccountManagerService and |
| 230 | * to the AccountAuthenticators. The uid of the caller will be known by the |
| 231 | * AccountManagerService as well as the AccountAuthenticators so they will be able to |
| 232 | * verify that the package is consistent with the uid (a uid might be shared by many |
| 233 | * packages). |
| 234 | */ |
| 235 | public static final String KEY_ANDROID_PACKAGE_NAME = "androidPackageName"; |
| 236 | |
| 237 | /** |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 238 | * Boolean, if set and 'customTokens' the authenticator is responsible for |
| 239 | * notifications. |
| 240 | * @hide |
| 241 | */ |
| 242 | public static final String KEY_NOTIFY_ON_FAILURE = "notifyOnAuthFailure"; |
| 243 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 244 | /** |
| 245 | * Bundle key used for a {@link Bundle} in result from |
| 246 | * {@link #startAddAccountSession} and friends which returns session data |
| 247 | * for installing an account later. |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 248 | * @hide |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 249 | */ |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 250 | @SystemApi |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 251 | public static final String KEY_ACCOUNT_SESSION_BUNDLE = "accountSessionBundle"; |
| 252 | |
| 253 | /** |
| 254 | * Bundle key used for the {@link String} account status token in result |
| 255 | * from {@link #startAddAccountSession} and friends which returns |
| 256 | * information about a particular account. |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 257 | * @hide |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 258 | */ |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 259 | @SystemApi |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 260 | public static final String KEY_ACCOUNT_STATUS_TOKEN = "accountStatusToken"; |
| 261 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 262 | public static final String ACTION_AUTHENTICATOR_INTENT = |
| 263 | "android.accounts.AccountAuthenticator"; |
| 264 | public static final String AUTHENTICATOR_META_DATA_NAME = |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 265 | "android.accounts.AccountAuthenticator"; |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 266 | public static final String AUTHENTICATOR_ATTRIBUTES_NAME = "account-authenticator"; |
| 267 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 268 | private final Context mContext; |
| 269 | private final IAccountManager mService; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 270 | private final Handler mMainHandler; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 271 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 272 | /** |
| 273 | * Action sent as a broadcast Intent by the AccountsService |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 274 | * when accounts are added, accounts are removed, or an |
| 275 | * account's credentials (saved password, etc) are changed. |
| 276 | * |
| 277 | * @see #addOnAccountsUpdatedListener |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 278 | */ |
| 279 | public static final String LOGIN_ACCOUNTS_CHANGED_ACTION = |
| 280 | "android.accounts.LOGIN_ACCOUNTS_CHANGED"; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 281 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 282 | /** |
| 283 | * @hide |
| 284 | */ |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 285 | public AccountManager(Context context, IAccountManager service) { |
| 286 | mContext = context; |
| 287 | mService = service; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 288 | mMainHandler = new Handler(mContext.getMainLooper()); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 291 | /** |
| 292 | * @hide used for testing only |
| 293 | */ |
| 294 | public AccountManager(Context context, IAccountManager service, Handler handler) { |
| 295 | mContext = context; |
| 296 | mService = service; |
| 297 | mMainHandler = handler; |
| 298 | } |
| 299 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 300 | /** |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 301 | * @hide for internal use only |
| 302 | */ |
| 303 | public static Bundle sanitizeResult(Bundle result) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 304 | if (result != null) { |
| 305 | if (result.containsKey(KEY_AUTHTOKEN) |
| 306 | && !TextUtils.isEmpty(result.getString(KEY_AUTHTOKEN))) { |
| 307 | final Bundle newResult = new Bundle(result); |
| 308 | newResult.putString(KEY_AUTHTOKEN, "<omitted for logging purposes>"); |
| 309 | return newResult; |
| 310 | } |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 311 | } |
| 312 | return result; |
| 313 | } |
| 314 | |
| 315 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 316 | * Gets an AccountManager instance associated with a Context. |
| 317 | * The {@link Context} will be used as long as the AccountManager is |
| 318 | * active, so make sure to use a {@link Context} whose lifetime is |
| 319 | * commensurate with any listeners registered to |
| 320 | * {@link #addOnAccountsUpdatedListener} or similar methods. |
| 321 | * |
| 322 | * <p>It is safe to call this method from the main thread. |
| 323 | * |
| 324 | * <p>No permission is required to call this method. |
| 325 | * |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 326 | * @param context The {@link Context} to use when necessary |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 327 | * @return An {@link AccountManager} instance |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 328 | */ |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 329 | public static AccountManager get(Context context) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 330 | if (context == null) throw new IllegalArgumentException("context is null"); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 331 | return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE); |
| 332 | } |
| 333 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 334 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 335 | * Gets the saved password associated with the account. |
| 336 | * This is intended for authenticators and related code; applications |
| 337 | * should get an auth token instead. |
| 338 | * |
| 339 | * <p>It is safe to call this method from the main thread. |
| 340 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 341 | * <p>This method requires the caller to have a signature match with the |
| 342 | * authenticator that owns the specified account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 343 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 344 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 345 | * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs for |
| 346 | * this function in API level 22. |
| 347 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 348 | * @param account The account to query for a password. Must not be {@code null}. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 349 | * @return The account's password, null if none or if the account doesn't exist |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 350 | */ |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 351 | public String getPassword(final Account account) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 352 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 353 | try { |
| 354 | return mService.getPassword(account); |
| 355 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 356 | throw e.rethrowFromSystemServer(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 360 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 361 | * Gets the user data named by "key" associated with the account. |
| 362 | * This is intended for authenticators and related code to store |
| 363 | * arbitrary metadata along with accounts. The meaning of the keys |
| 364 | * and values is up to the authenticator for the account. |
| 365 | * |
| 366 | * <p>It is safe to call this method from the main thread. |
| 367 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 368 | * <p>This method requires the caller to have a signature match with the |
| 369 | * authenticator that owns the specified account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 370 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 371 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 372 | * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs |
| 373 | * for this function in API level 22. |
| 374 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 375 | * @param account The account to query for user data |
| 376 | * @return The user data, null if the account or key doesn't exist |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 377 | */ |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 378 | public String getUserData(final Account account, final String key) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 379 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 380 | if (key == null) throw new IllegalArgumentException("key is null"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 381 | try { |
| 382 | return mService.getUserData(account, key); |
| 383 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 384 | throw e.rethrowFromSystemServer(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 388 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 389 | * Lists the currently registered authenticators. |
| 390 | * |
| 391 | * <p>It is safe to call this method from the main thread. |
| 392 | * |
| 393 | * <p>No permission is required to call this method. |
| 394 | * |
| 395 | * @return An array of {@link AuthenticatorDescription} for every |
| 396 | * authenticator known to the AccountManager service. Empty (never |
| 397 | * null) if no authenticators are known. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 398 | */ |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 399 | public AuthenticatorDescription[] getAuthenticatorTypes() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 400 | try { |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 401 | return mService.getAuthenticatorTypes(UserHandle.getCallingUserId()); |
| 402 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 403 | throw e.rethrowFromSystemServer(); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * @hide |
| 409 | * Lists the currently registered authenticators for a given user id. |
| 410 | * |
| 411 | * <p>It is safe to call this method from the main thread. |
| 412 | * |
| 413 | * <p>The caller has to be in the same user or have the permission |
| 414 | * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}. |
| 415 | * |
| 416 | * @return An array of {@link AuthenticatorDescription} for every |
| 417 | * authenticator known to the AccountManager service. Empty (never |
| 418 | * null) if no authenticators are known. |
| 419 | */ |
| 420 | public AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) { |
| 421 | try { |
| 422 | return mService.getAuthenticatorTypes(userId); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 423 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 424 | throw e.rethrowFromSystemServer(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 428 | /** |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 429 | * Lists all accounts of any type registered on the device. |
| 430 | * Equivalent to getAccountsByType(null). |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 431 | * |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 432 | * <p>It is safe to call this method from the main thread. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 433 | * |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 434 | * <p>Clients of this method that have not been granted the |
| 435 | * {@link android.Manifest.permission#GET_ACCOUNTS} permission, |
| 436 | * will only see those accounts managed by AbstractAccountAuthenticators whose |
| 437 | * signature matches the client. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 438 | * |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 439 | * @return An array of {@link Account}, one for each account. Empty |
| 440 | * (never null) if no accounts have been added. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 441 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 442 | @NonNull |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 443 | @RequiresPermission(GET_ACCOUNTS) |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 444 | public Account[] getAccounts() { |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 445 | try { |
| 446 | return mService.getAccounts(null, mContext.getOpPackageName()); |
| 447 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 448 | throw e.rethrowFromSystemServer(); |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 449 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 452 | /** |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 453 | * @hide |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 454 | * Lists all accounts of any type registered on the device for a given |
| 455 | * user id. Equivalent to getAccountsByType(null). |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 456 | * |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 457 | * <p>It is safe to call this method from the main thread. |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 458 | * |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 459 | * <p>Clients of this method that have not been granted the |
| 460 | * {@link android.Manifest.permission#GET_ACCOUNTS} permission, |
| 461 | * will only see those accounts managed by AbstractAccountAuthenticators whose |
| 462 | * signature matches the client. |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 463 | * |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 464 | * @return An array of {@link Account}, one for each account. Empty |
| 465 | * (never null) if no accounts have been added. |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 466 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 467 | @NonNull |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 468 | @RequiresPermission(GET_ACCOUNTS) |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 469 | public Account[] getAccountsAsUser(int userId) { |
| 470 | try { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 471 | return mService.getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 472 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 473 | throw e.rethrowFromSystemServer(); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * @hide |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 479 | * For use by internal activities. Returns the list of accounts that the calling package |
| 480 | * is authorized to use, particularly for shared accounts. |
| 481 | * @param packageName package name of the calling app. |
| 482 | * @param uid the uid of the calling app. |
| 483 | * @return the accounts that are available to this package and user. |
| 484 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 485 | @NonNull |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 486 | public Account[] getAccountsForPackage(String packageName, int uid) { |
| 487 | try { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 488 | return mService.getAccountsForPackage(packageName, uid, mContext.getOpPackageName()); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 489 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 490 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
| 494 | /** |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 495 | * Returns the accounts visible to the specified package, in an environment where some apps |
| 496 | * are not authorized to view all accounts. This method can only be called by system apps. |
| 497 | * @param type The type of accounts to return, null to retrieve all accounts |
| 498 | * @param packageName The package name of the app for which the accounts are to be returned |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 499 | * @return An array of {@link Account}, one per matching account. Empty |
| 500 | * (never null) if no accounts of the specified type have been added. |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 501 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 502 | @NonNull |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 503 | public Account[] getAccountsByTypeForPackage(String type, String packageName) { |
| 504 | try { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 505 | return mService.getAccountsByTypeForPackage(type, packageName, |
| 506 | mContext.getOpPackageName()); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 507 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 508 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | /** |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 513 | * Lists all accounts of a particular type. The account type is a |
| 514 | * string token corresponding to the authenticator and useful domain |
| 515 | * of the account. For example, there are types corresponding to Google |
| 516 | * and Facebook. The exact string token to use will be published somewhere |
| 517 | * associated with the authenticator in question. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 518 | * |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 519 | * <p>It is safe to call this method from the main thread. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 520 | * |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 521 | * <p>Clients of this method that have not been granted the |
| 522 | * {@link android.Manifest.permission#GET_ACCOUNTS} permission, |
| 523 | * will only see those accounts managed by AbstractAccountAuthenticators whose |
| 524 | * signature matches the client. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 525 | * |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 526 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 527 | * GET_ACCOUNTS permission is needed for those platforms, irrespective of uid |
| 528 | * or signature match. See docs for this function in API level 22. |
| 529 | * |
| 530 | * @param type The type of accounts to return, null to retrieve all accounts |
| 531 | * @return An array of {@link Account}, one per matching account. Empty |
| 532 | * (never null) if no accounts of the specified type have been added. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 533 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 534 | @NonNull |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 535 | @RequiresPermission(GET_ACCOUNTS) |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 536 | public Account[] getAccountsByType(String type) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 537 | return getAccountsByTypeAsUser(type, Process.myUserHandle()); |
| 538 | } |
| 539 | |
| 540 | /** @hide Same as {@link #getAccountsByType(String)} but for a specific user. */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 541 | @NonNull |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 542 | public Account[] getAccountsByTypeAsUser(String type, UserHandle userHandle) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 543 | try { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 544 | return mService.getAccountsAsUser(type, userHandle.getIdentifier(), |
| 545 | mContext.getOpPackageName()); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 546 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 547 | throw e.rethrowFromSystemServer(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 551 | /** |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 552 | * Change whether or not an app (identified by its uid) is allowed to retrieve an authToken |
| 553 | * for an account. |
| 554 | * <p> |
| 555 | * This is only meant to be used by system activities and is not in the SDK. |
| 556 | * @param account The account whose permissions are being modified |
| 557 | * @param authTokenType The type of token whose permissions are being modified |
| 558 | * @param uid The uid that identifies the app which is being granted or revoked permission. |
| 559 | * @param value true is permission is being granted, false for revoked |
| 560 | * @hide |
| 561 | */ |
| 562 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) { |
| 563 | try { |
| 564 | mService.updateAppPermission(account, authTokenType, uid, value); |
| 565 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 566 | throw e.rethrowFromSystemServer(); |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Get the user-friendly label associated with an authenticator's auth token. |
| 572 | * @param accountType the type of the authenticator. must not be null. |
| 573 | * @param authTokenType the token type. must not be null. |
| 574 | * @param callback callback to invoke when the result is available. may be null. |
| 575 | * @param handler the handler on which to invoke the callback, or null for the main thread |
| 576 | * @return a future containing the label string |
| 577 | * @hide |
| 578 | */ |
| 579 | public AccountManagerFuture<String> getAuthTokenLabel( |
| 580 | final String accountType, final String authTokenType, |
| 581 | AccountManagerCallback<String> callback, Handler handler) { |
| 582 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 583 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 584 | return new Future2Task<String>(handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 585 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 586 | public void doWork() throws RemoteException { |
| 587 | mService.getAuthTokenLabel(mResponse, accountType, authTokenType); |
| 588 | } |
| 589 | |
| 590 | @Override |
| 591 | public String bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 592 | if (!bundle.containsKey(KEY_AUTH_TOKEN_LABEL)) { |
| 593 | throw new AuthenticatorException("no result in response"); |
| 594 | } |
| 595 | return bundle.getString(KEY_AUTH_TOKEN_LABEL); |
| 596 | } |
| 597 | }.start(); |
| 598 | } |
| 599 | |
| 600 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 601 | * Finds out whether a particular account has all the specified features. |
| 602 | * Account features are authenticator-specific string tokens identifying |
| 603 | * boolean account properties. For example, features are used to tell |
| 604 | * whether Google accounts have a particular service (such as Google |
| 605 | * Calendar or Google Talk) enabled. The feature names and their meanings |
| 606 | * are published somewhere associated with the authenticator in question. |
| 607 | * |
| 608 | * <p>This method may be called from any thread, but the returned |
| 609 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 610 | * |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 611 | * <p>This method requires the caller to hold the permission |
| 612 | * {@link android.Manifest.permission#GET_ACCOUNTS} or be a signature |
| 613 | * match with the AbstractAccountAuthenticator that manages the account. |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 614 | * |
| 615 | * @param account The {@link Account} to test |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 616 | * @param features An array of the account features to check |
| 617 | * @param callback Callback to invoke when the request completes, |
| 618 | * null for no callback |
| 619 | * @param handler {@link Handler} identifying the callback thread, |
| 620 | * null for the main thread |
| 621 | * @return An {@link AccountManagerFuture} which resolves to a Boolean, |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 622 | * true if the account exists and has all of the specified features. |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 623 | */ |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 624 | @RequiresPermission(GET_ACCOUNTS) |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 625 | public AccountManagerFuture<Boolean> hasFeatures(final Account account, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 626 | final String[] features, |
| 627 | AccountManagerCallback<Boolean> callback, Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 628 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 629 | if (features == null) throw new IllegalArgumentException("features is null"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 630 | return new Future2Task<Boolean>(handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 631 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 632 | public void doWork() throws RemoteException { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 633 | mService.hasFeatures(mResponse, account, features, mContext.getOpPackageName()); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 634 | } |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 635 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 636 | public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 637 | if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) { |
| 638 | throw new AuthenticatorException("no result in response"); |
| 639 | } |
| 640 | return bundle.getBoolean(KEY_BOOLEAN_RESULT); |
| 641 | } |
| 642 | }.start(); |
| 643 | } |
| 644 | |
| 645 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 646 | * Lists all accounts of a type which have certain features. The account |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 647 | * type identifies the authenticator (see {@link #getAccountsByType}). |
| 648 | * Account features are authenticator-specific string tokens identifying |
| 649 | * boolean account properties (see {@link #hasFeatures}). |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 650 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 651 | * <p>Unlike {@link #getAccountsByType}, this method calls the authenticator, |
| 652 | * which may contact the server or do other work to check account features, |
| 653 | * so the method returns an {@link AccountManagerFuture}. |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 654 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 655 | * <p>This method may be called from any thread, but the returned |
| 656 | * {@link AccountManagerFuture} must not be used on the main thread. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 657 | * |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 658 | * <p>Clients of this method that have not been granted the |
| 659 | * {@link android.Manifest.permission#GET_ACCOUNTS} permission, |
| 660 | * will only see those accounts managed by AbstractAccountAuthenticators whose |
| 661 | * signature matches the client. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 662 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 663 | * @param type The type of accounts to return, must not be null |
| 664 | * @param features An array of the account features to require, |
| 665 | * may be null or empty |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 666 | * |
| 667 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 668 | * GET_ACCOUNTS permission is needed for those platforms, irrespective of uid |
| 669 | * or signature match. See docs for this function in API level 22. |
| 670 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 671 | * @param callback Callback to invoke when the request completes, |
| 672 | * null for no callback |
| 673 | * @param handler {@link Handler} identifying the callback thread, |
| 674 | * null for the main thread |
| 675 | * @return An {@link AccountManagerFuture} which resolves to an array of |
| 676 | * {@link Account}, one per account of the specified type which |
| 677 | * matches the requested features. |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 678 | */ |
Carlos Valdivia | bf33bd4 | 2016-02-02 20:20:42 +0000 | [diff] [blame] | 679 | @RequiresPermission(GET_ACCOUNTS) |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 680 | public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures( |
| 681 | final String type, final String[] features, |
| 682 | AccountManagerCallback<Account[]> callback, Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 683 | if (type == null) throw new IllegalArgumentException("type is null"); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 684 | return new Future2Task<Account[]>(handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 685 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 686 | public void doWork() throws RemoteException { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 687 | mService.getAccountsByFeatures(mResponse, type, features, |
| 688 | mContext.getOpPackageName()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 689 | } |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 690 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 691 | public Account[] bundleToResult(Bundle bundle) throws AuthenticatorException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 692 | if (!bundle.containsKey(KEY_ACCOUNTS)) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 693 | throw new AuthenticatorException("no result in response"); |
| 694 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 695 | final Parcelable[] parcelables = bundle.getParcelableArray(KEY_ACCOUNTS); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 696 | Account[] descs = new Account[parcelables.length]; |
| 697 | for (int i = 0; i < parcelables.length; i++) { |
| 698 | descs[i] = (Account) parcelables[i]; |
| 699 | } |
| 700 | return descs; |
| 701 | } |
| 702 | }.start(); |
| 703 | } |
| 704 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 705 | /** |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 706 | * Adds an account directly to the AccountManager. Normally used by sign-up |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 707 | * wizards associated with authenticators, not directly by applications. |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 708 | * <p>Calling this method does not update the last authenticated timestamp, |
| 709 | * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call |
| 710 | * {@link #notifyAccountAuthenticated(Account)} after getting success. |
| 711 | * However, if this method is called when it is triggered by addAccount() or |
| 712 | * addAccountAsUser() or similar functions, then there is no need to update |
| 713 | * timestamp manually as it is updated automatically by framework on |
| 714 | * successful completion of the mentioned functions. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 715 | * <p>It is safe to call this method from the main thread. |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 716 | * <p>This method requires the caller to have a signature match with the |
| 717 | * authenticator that owns the specified account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 718 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 719 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 720 | * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs |
| 721 | * for this function in API level 22. |
| 722 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 723 | * @param account The {@link Account} to add |
| 724 | * @param password The password to associate with the account, null for none |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 725 | * @param userdata String values to use for the account's userdata, null for |
| 726 | * none |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 727 | * @return True if the account was successfully added, false if the account |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 728 | * already exists, the account is null, or another error occurs. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 729 | */ |
| 730 | public boolean addAccountExplicitly(Account account, String password, Bundle userdata) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 731 | if (account == null) throw new IllegalArgumentException("account is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 732 | try { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 733 | return mService.addAccountExplicitly(account, password, userdata); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 734 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 735 | throw e.rethrowFromSystemServer(); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
| 739 | /** |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 740 | * Notifies the system that the account has just been authenticated. This |
| 741 | * information may be used by other applications to verify the account. This |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 742 | * should be called only when the user has entered correct credentials for |
| 743 | * the account. |
| 744 | * <p> |
| 745 | * It is not safe to call this method from the main thread. As such, call it |
| 746 | * from another thread. |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 747 | * <p>This method requires the caller to have a signature match with the |
| 748 | * authenticator that owns the specified account. |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 749 | * |
| 750 | * @param account The {@link Account} to be updated. |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 751 | * @return boolean {@code true} if the authentication of the account has been successfully |
| 752 | * acknowledged. Otherwise {@code false}. |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 753 | */ |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 754 | public boolean notifyAccountAuthenticated(Account account) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 755 | if (account == null) |
| 756 | throw new IllegalArgumentException("account is null"); |
| 757 | try { |
| 758 | return mService.accountAuthenticated(account); |
| 759 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 760 | throw e.rethrowFromSystemServer(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
| 764 | /** |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 765 | * Rename the specified {@link Account}. This is equivalent to removing |
| 766 | * the existing account and adding a new renamed account with the old |
| 767 | * account's user data. |
| 768 | * |
| 769 | * <p>It is safe to call this method from the main thread. |
| 770 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 771 | * <p>This method requires the caller to have a signature match with the |
| 772 | * authenticator that manages the specified account. |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 773 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 774 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 775 | * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator |
| 776 | * is needed for those platforms. See docs for this function in API level 22. |
| 777 | * |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 778 | * @param account The {@link Account} to rename |
| 779 | * @param newName String name to be associated with the account. |
| 780 | * @param callback Callback to invoke when the request completes, null for |
| 781 | * no callback |
| 782 | * @param handler {@link Handler} identifying the callback thread, null for |
| 783 | * the main thread |
| 784 | * @return An {@link AccountManagerFuture} which resolves to the Account |
| 785 | * after the name change. If successful the account's name will be the |
| 786 | * specified new name. |
| 787 | */ |
| 788 | public AccountManagerFuture<Account> renameAccount( |
| 789 | final Account account, |
Tor Norbye | 80b530a | 2015-04-23 16:36:09 -0700 | [diff] [blame] | 790 | @Size(min = 1) final String newName, |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 791 | AccountManagerCallback<Account> callback, |
| 792 | Handler handler) { |
| 793 | if (account == null) throw new IllegalArgumentException("account is null."); |
| 794 | if (TextUtils.isEmpty(newName)) { |
| 795 | throw new IllegalArgumentException("newName is empty or null."); |
| 796 | } |
| 797 | return new Future2Task<Account>(handler, callback) { |
| 798 | @Override |
| 799 | public void doWork() throws RemoteException { |
| 800 | mService.renameAccount(mResponse, account, newName); |
| 801 | } |
| 802 | @Override |
| 803 | public Account bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 804 | String name = bundle.getString(KEY_ACCOUNT_NAME); |
| 805 | String type = bundle.getString(KEY_ACCOUNT_TYPE); |
| 806 | return new Account(name, type); |
| 807 | } |
| 808 | }.start(); |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Gets the previous name associated with the account or {@code null}, if |
| 813 | * none. This is intended so that clients of {@link |
| 814 | * #LOGIN_ACCOUNTS_CHANGED_ACTION} broadcasts can determine if an |
| 815 | * authenticator has renamed an account. |
| 816 | * |
| 817 | * <p>It is safe to call this method from the main thread. |
| 818 | * |
| 819 | * @param account The account to query for a previous name. |
| 820 | * @return The account's previous name, null if the account has never been |
| 821 | * renamed. |
| 822 | */ |
| 823 | public String getPreviousName(final Account account) { |
| 824 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 825 | try { |
| 826 | return mService.getPreviousName(account); |
| 827 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 828 | throw e.rethrowFromSystemServer(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
| 832 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 833 | * Removes an account from the AccountManager. Does nothing if the account |
| 834 | * does not exist. Does not delete the account from the server. |
| 835 | * The authenticator may have its own policies preventing account |
| 836 | * deletion, in which case the account will not be deleted. |
| 837 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 838 | * <p>This method requires the caller to have a signature match with the |
| 839 | * authenticator that manages the specified account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 840 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 841 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 842 | * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for |
| 843 | * this function in API level 22. |
| 844 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 845 | * @param account The {@link Account} to remove |
| 846 | * @param callback Callback to invoke when the request completes, |
| 847 | * null for no callback |
| 848 | * @param handler {@link Handler} identifying the callback thread, |
| 849 | * null for the main thread |
| 850 | * @return An {@link AccountManagerFuture} which resolves to a Boolean, |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 851 | * true if the account has been successfully removed |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 852 | * @deprecated use |
| 853 | * {@link #removeAccount(Account, Activity, AccountManagerCallback, Handler)} |
| 854 | * instead |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 855 | */ |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 856 | @Deprecated |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 857 | public AccountManagerFuture<Boolean> removeAccount(final Account account, |
| 858 | AccountManagerCallback<Boolean> callback, Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 859 | if (account == null) throw new IllegalArgumentException("account is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 860 | return new Future2Task<Boolean>(handler, callback) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 861 | @Override |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 862 | public void doWork() throws RemoteException { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 863 | mService.removeAccount(mResponse, account, false); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 864 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 865 | @Override |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 866 | public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 867 | if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) { |
| 868 | throw new AuthenticatorException("no result in response"); |
| 869 | } |
| 870 | return bundle.getBoolean(KEY_BOOLEAN_RESULT); |
| 871 | } |
| 872 | }.start(); |
| 873 | } |
| 874 | |
| 875 | /** |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 876 | * Removes an account from the AccountManager. Does nothing if the account |
| 877 | * does not exist. Does not delete the account from the server. |
| 878 | * The authenticator may have its own policies preventing account |
| 879 | * deletion, in which case the account will not be deleted. |
| 880 | * |
| 881 | * <p>This method may be called from any thread, but the returned |
| 882 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 883 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 884 | * <p>This method requires the caller to have a signature match with the |
| 885 | * authenticator that manages the specified account. |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 886 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 887 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 888 | * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for |
| 889 | * this function in API level 22. |
| 890 | * |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 891 | * @param account The {@link Account} to remove |
| 892 | * @param activity The {@link Activity} context to use for launching a new |
| 893 | * authenticator-defined sub-Activity to prompt the user to delete an |
| 894 | * account; used only to call startActivity(); if null, the prompt |
| 895 | * will not be launched directly, but the {@link Intent} may be |
| 896 | * returned to the caller instead |
| 897 | * @param callback Callback to invoke when the request completes, |
| 898 | * null for no callback |
| 899 | * @param handler {@link Handler} identifying the callback thread, |
| 900 | * null for the main thread |
| 901 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
| 902 | * {@link #KEY_BOOLEAN_RESULT} if activity was specified and an account |
| 903 | * was removed or if active. If no activity was specified, the returned |
| 904 | * Bundle contains only {@link #KEY_INTENT} with the {@link Intent} |
| 905 | * needed to launch the actual account removal process, if authenticator |
| 906 | * needs the activity launch. If an error occurred, |
| 907 | * {@link AccountManagerFuture#getResult()} throws: |
| 908 | * <ul> |
| 909 | * <li> {@link AuthenticatorException} if no authenticator was registered for |
| 910 | * this account type or the authenticator failed to respond |
| 911 | * <li> {@link OperationCanceledException} if the operation was canceled for |
| 912 | * any reason, including the user canceling the creation process or |
| 913 | * adding accounts (of this type) has been disabled by policy |
| 914 | * </ul> |
| 915 | */ |
| 916 | public AccountManagerFuture<Bundle> removeAccount(final Account account, |
| 917 | final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) { |
| 918 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 919 | return new AmsTask(activity, handler, callback) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 920 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 921 | public void doWork() throws RemoteException { |
| 922 | mService.removeAccount(mResponse, account, activity != null); |
| 923 | } |
| 924 | }.start(); |
| 925 | } |
| 926 | |
| 927 | /** |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 928 | * @see #removeAccount(Account, AccountManagerCallback, Handler) |
| 929 | * @hide |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 930 | * @deprecated use |
| 931 | * {@link #removeAccountAsUser(Account, Activity, AccountManagerCallback, Handler)} |
| 932 | * instead |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 933 | */ |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 934 | @Deprecated |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 935 | public AccountManagerFuture<Boolean> removeAccountAsUser(final Account account, |
| 936 | AccountManagerCallback<Boolean> callback, Handler handler, |
| 937 | final UserHandle userHandle) { |
| 938 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 939 | if (userHandle == null) throw new IllegalArgumentException("userHandle is null"); |
| 940 | return new Future2Task<Boolean>(handler, callback) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 941 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 942 | public void doWork() throws RemoteException { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 943 | mService.removeAccountAsUser(mResponse, account, false, userHandle.getIdentifier()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 944 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 945 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 946 | public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 947 | if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) { |
| 948 | throw new AuthenticatorException("no result in response"); |
| 949 | } |
| 950 | return bundle.getBoolean(KEY_BOOLEAN_RESULT); |
| 951 | } |
| 952 | }.start(); |
| 953 | } |
| 954 | |
| 955 | /** |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 956 | * @see #removeAccount(Account, Activity, AccountManagerCallback, Handler) |
| 957 | * @hide |
| 958 | */ |
| 959 | public AccountManagerFuture<Bundle> removeAccountAsUser(final Account account, |
| 960 | final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler, |
| 961 | final UserHandle userHandle) { |
| 962 | if (account == null) |
| 963 | throw new IllegalArgumentException("account is null"); |
| 964 | if (userHandle == null) |
| 965 | throw new IllegalArgumentException("userHandle is null"); |
| 966 | return new AmsTask(activity, handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 967 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 968 | public void doWork() throws RemoteException { |
| 969 | mService.removeAccountAsUser(mResponse, account, activity != null, |
| 970 | userHandle.getIdentifier()); |
| 971 | } |
| 972 | }.start(); |
| 973 | } |
| 974 | |
| 975 | /** |
| 976 | * Removes an account directly. Normally used by authenticators, not |
| 977 | * directly by applications. Does not delete the account from the server. |
| 978 | * The authenticator may have its own policies preventing account deletion, |
| 979 | * in which case the account will not be deleted. |
| 980 | * <p> |
| 981 | * It is safe to call this method from the main thread. |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 982 | * <p>This method requires the caller to have a signature match with the |
| 983 | * authenticator that manages the specified account. |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 984 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 985 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 986 | * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator |
| 987 | * is needed for those platforms. See docs for this function in API level 22. |
| 988 | * |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 989 | * @param account The {@link Account} to delete. |
| 990 | * @return True if the account was successfully deleted, false if the |
| 991 | * account did not exist, the account is null, or another error |
| 992 | * occurs. |
| 993 | */ |
| 994 | public boolean removeAccountExplicitly(Account account) { |
| 995 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 996 | try { |
| 997 | return mService.removeAccountExplicitly(account); |
| 998 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 999 | throw e.rethrowFromSystemServer(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1004 | * Removes an auth token from the AccountManager's cache. Does nothing if |
| 1005 | * the auth token is not currently in the cache. Applications must call this |
| 1006 | * method when the auth token is found to have expired or otherwise become |
| 1007 | * invalid for authenticating requests. The AccountManager does not validate |
| 1008 | * or expire cached auth tokens otherwise. |
| 1009 | * |
| 1010 | * <p>It is safe to call this method from the main thread. |
| 1011 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1012 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 1013 | * MANAGE_ACCOUNTS or USE_CREDENTIALS permission is needed for those |
| 1014 | * platforms. See docs for this function in API level 22. |
| 1015 | * |
Fred Quintana | f35b68f | 2010-04-01 11:36:00 -0700 | [diff] [blame] | 1016 | * @param accountType The account type of the auth token to invalidate, must not be null |
| 1017 | * @param authToken The auth token to invalidate, may be null |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1018 | */ |
| 1019 | public void invalidateAuthToken(final String accountType, final String authToken) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1020 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1021 | try { |
Fred Quintana | f35b68f | 2010-04-01 11:36:00 -0700 | [diff] [blame] | 1022 | if (authToken != null) { |
| 1023 | mService.invalidateAuthToken(accountType, authToken); |
| 1024 | } |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1025 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 1026 | throw e.rethrowFromSystemServer(); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * Gets an auth token from the AccountManager's cache. If no auth |
| 1032 | * token is cached for this account, null will be returned -- a new |
| 1033 | * auth token will not be generated, and the server will not be contacted. |
| 1034 | * Intended for use by the authenticator, not directly by applications. |
| 1035 | * |
| 1036 | * <p>It is safe to call this method from the main thread. |
| 1037 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1038 | * <p>This method requires the caller to have a signature match with the |
| 1039 | * authenticator that manages the specified account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1040 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1041 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 1042 | * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator |
| 1043 | * is needed for those platforms. See docs for this function in API level 22. |
| 1044 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1045 | * @param account The account for which an auth token is to be fetched. Cannot be {@code null}. |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1046 | * @param authTokenType The type of auth token to fetch. Cannot be {@code null}. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1047 | * @return The cached auth token for this account and type, or null if |
| 1048 | * no auth token is cached or the account does not exist. |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1049 | * @see #getAuthToken |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1050 | */ |
| 1051 | public String peekAuthToken(final Account account, final String authTokenType) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1052 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1053 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1054 | try { |
| 1055 | return mService.peekAuthToken(account, authTokenType); |
| 1056 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 1057 | throw e.rethrowFromSystemServer(); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | /** |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 1062 | * Sets or forgets a saved password. This modifies the local copy of the |
| 1063 | * password used to automatically authenticate the user; it does not change |
| 1064 | * the user's account password on the server. Intended for use by the |
| 1065 | * authenticator, not directly by applications. |
| 1066 | * <p>Calling this method does not update the last authenticated timestamp, |
| 1067 | * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call |
| 1068 | * {@link #notifyAccountAuthenticated(Account)} after getting success. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1069 | * <p>It is safe to call this method from the main thread. |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1070 | * <p>This method requires the caller to have a signature match with the |
| 1071 | * authenticator that manages the specified account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1072 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1073 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 1074 | * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator |
| 1075 | * is needed for those platforms. See docs for this function in API level 22. |
| 1076 | * |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 1077 | * @param account The account whose password is to be set. Cannot be |
| 1078 | * {@code null}. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1079 | * @param password The password to set, null to clear the password |
| 1080 | */ |
| 1081 | public void setPassword(final Account account, final String password) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1082 | if (account == null) throw new IllegalArgumentException("account is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1083 | try { |
| 1084 | mService.setPassword(account, password); |
| 1085 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 1086 | throw e.rethrowFromSystemServer(); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * Forgets a saved password. This erases the local copy of the password; |
| 1092 | * it does not change the user's account password on the server. |
| 1093 | * Has the same effect as setPassword(account, null) but requires fewer |
| 1094 | * permissions, and may be used by applications or management interfaces |
| 1095 | * to "sign out" from an account. |
| 1096 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1097 | * <p>This method only successfully clear the account's password when the |
| 1098 | * caller has the same signature as the authenticator that owns the |
| 1099 | * specified account. Otherwise, this method will silently fail. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1100 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1101 | * <p>It is safe to call this method from the main thread. |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 1102 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1103 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 1104 | * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for |
| 1105 | * this function in API level 22. |
| 1106 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1107 | * @param account The account whose password to clear |
| 1108 | */ |
| 1109 | public void clearPassword(final Account account) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1110 | if (account == null) throw new IllegalArgumentException("account is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1111 | try { |
| 1112 | mService.clearPassword(account); |
| 1113 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 1114 | throw e.rethrowFromSystemServer(); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * Sets one userdata key for an account. Intended by use for the |
| 1120 | * authenticator to stash state for itself, not directly by applications. |
| 1121 | * The meaning of the keys and values is up to the authenticator. |
| 1122 | * |
| 1123 | * <p>It is safe to call this method from the main thread. |
| 1124 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1125 | * <p>This method requires the caller to have a signature match with the |
| 1126 | * authenticator that manages the specified account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1127 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1128 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 1129 | * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator |
| 1130 | * is needed for those platforms. See docs for this function in API level 22. |
| 1131 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1132 | * @param account Account whose user data is to be set. Must not be {@code null}. |
| 1133 | * @param key String user data key to set. Must not be null |
| 1134 | * @param value String value to set, {@code null} to clear this user data key |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1135 | */ |
| 1136 | public void setUserData(final Account account, final String key, final String value) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1137 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1138 | if (key == null) throw new IllegalArgumentException("key is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1139 | try { |
| 1140 | mService.setUserData(account, key, value); |
| 1141 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 1142 | throw e.rethrowFromSystemServer(); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | /** |
| 1147 | * Adds an auth token to the AccountManager cache for an account. |
| 1148 | * If the account does not exist then this call has no effect. |
| 1149 | * Replaces any previous auth token for this account and auth token type. |
| 1150 | * Intended for use by the authenticator, not directly by applications. |
| 1151 | * |
| 1152 | * <p>It is safe to call this method from the main thread. |
| 1153 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1154 | * <p>This method requires the caller to have a signature match with the |
| 1155 | * authenticator that manages the specified account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1156 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1157 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 1158 | * AUTHENTICATE_ACCOUNTS permission and same UID as account's authenticator |
| 1159 | * is needed for those platforms. See docs for this function in API level 22. |
| 1160 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1161 | * @param account The account to set an auth token for |
| 1162 | * @param authTokenType The type of the auth token, see {#getAuthToken} |
| 1163 | * @param authToken The auth token to add to the cache |
| 1164 | */ |
| 1165 | public void setAuthToken(Account account, final String authTokenType, final String authToken) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1166 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1167 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1168 | try { |
| 1169 | mService.setAuthToken(account, authTokenType, authToken); |
| 1170 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 1171 | throw e.rethrowFromSystemServer(); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * This convenience helper synchronously gets an auth token with |
| 1177 | * {@link #getAuthToken(Account, String, boolean, AccountManagerCallback, Handler)}. |
| 1178 | * |
| 1179 | * <p>This method may block while a network request completes, and must |
| 1180 | * never be made from the main thread. |
| 1181 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1182 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 1183 | * USE_CREDENTIALS permission is needed for those platforms. See docs for |
| 1184 | * this function in API level 22. |
| 1185 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1186 | * @param account The account to fetch an auth token for |
Joe Malin | b6a3526 | 2013-06-03 09:49:04 -0700 | [diff] [blame] | 1187 | * @param authTokenType The auth token type, see {@link #getAuthToken getAuthToken()} |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1188 | * @param notifyAuthFailure If true, display a notification and return null |
| 1189 | * if authentication fails; if false, prompt and wait for the user to |
| 1190 | * re-enter correct credentials before returning |
| 1191 | * @return An auth token of the specified type for this account, or null |
| 1192 | * if authentication fails or none can be fetched. |
| 1193 | * @throws AuthenticatorException if the authenticator failed to respond |
| 1194 | * @throws OperationCanceledException if the request was canceled for any |
| 1195 | * reason, including the user canceling a credential request |
| 1196 | * @throws java.io.IOException if the authenticator experienced an I/O problem |
| 1197 | * creating a new auth token, usually because of network trouble |
| 1198 | */ |
| 1199 | public String blockingGetAuthToken(Account account, String authTokenType, |
| 1200 | boolean notifyAuthFailure) |
| 1201 | throws OperationCanceledException, IOException, AuthenticatorException { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1202 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1203 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1204 | Bundle bundle = getAuthToken(account, authTokenType, notifyAuthFailure, null /* callback */, |
| 1205 | null /* handler */).getResult(); |
Fred Quintana | 96580e0 | 2010-03-04 13:42:42 -0800 | [diff] [blame] | 1206 | if (bundle == null) { |
| 1207 | // This should never happen, but it does, occasionally. If it does return null to |
| 1208 | // signify that we were not able to get the authtoken. |
| 1209 | // TODO: remove this when the bug is found that sometimes causes a null bundle to be |
| 1210 | // returned |
| 1211 | Log.e(TAG, "blockingGetAuthToken: null was returned from getResult() for " |
| 1212 | + account + ", authTokenType " + authTokenType); |
| 1213 | return null; |
| 1214 | } |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1215 | return bundle.getString(KEY_AUTHTOKEN); |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * Gets an auth token of the specified type for a particular account, |
| 1220 | * prompting the user for credentials if necessary. This method is |
| 1221 | * intended for applications running in the foreground where it makes |
| 1222 | * sense to ask the user directly for a password. |
| 1223 | * |
| 1224 | * <p>If a previously generated auth token is cached for this account and |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1225 | * type, then it is returned. Otherwise, if a saved password is |
| 1226 | * available, it is sent to the server to generate a new auth token. |
| 1227 | * Otherwise, the user is prompted to enter a password. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1228 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1229 | * <p>Some authenticators have auth token <em>types</em>, whose value |
| 1230 | * is authenticator-dependent. Some services use different token types to |
| 1231 | * access different functionality -- for example, Google uses different auth |
| 1232 | * tokens to access Gmail and Google Calendar for the same account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1233 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1234 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1235 | * USE_CREDENTIALS permission is needed for those platforms. See docs for |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1236 | * this function in API level 22. |
| 1237 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1238 | * <p>This method may be called from any thread, but the returned |
| 1239 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 1240 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1241 | * @param account The account to fetch an auth token for |
| 1242 | * @param authTokenType The auth token type, an authenticator-dependent |
| 1243 | * string token, must not be null |
| 1244 | * @param options Authenticator-specific options for the request, |
| 1245 | * may be null or empty |
| 1246 | * @param activity The {@link Activity} context to use for launching a new |
| 1247 | * authenticator-defined sub-Activity to prompt the user for a password |
| 1248 | * if necessary; used only to call startActivity(); must not be null. |
| 1249 | * @param callback Callback to invoke when the request completes, |
| 1250 | * null for no callback |
| 1251 | * @param handler {@link Handler} identifying the callback thread, |
| 1252 | * null for the main thread |
| 1253 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
| 1254 | * at least the following fields: |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1255 | * <ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1256 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied |
| 1257 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 1258 | * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1259 | * </ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1260 | * |
| 1261 | * (Other authenticator-specific values may be returned.) If an auth token |
| 1262 | * could not be fetched, {@link AccountManagerFuture#getResult()} throws: |
| 1263 | * <ul> |
| 1264 | * <li> {@link AuthenticatorException} if the authenticator failed to respond |
| 1265 | * <li> {@link OperationCanceledException} if the operation is canceled for |
| 1266 | * any reason, incluidng the user canceling a credential request |
| 1267 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 1268 | * creating a new auth token, usually because of network trouble |
| 1269 | * </ul> |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1270 | * If the account is no longer present on the device, the return value is |
| 1271 | * authenticator-dependent. The caller should verify the validity of the |
| 1272 | * account before requesting an auth token. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1273 | */ |
| 1274 | public AccountManagerFuture<Bundle> getAuthToken( |
| 1275 | final Account account, final String authTokenType, final Bundle options, |
| 1276 | final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1277 | if (account == null) throw new IllegalArgumentException("account is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1278 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Costin Manolache | e5847ad | 2011-09-14 12:52:19 -0700 | [diff] [blame] | 1279 | final Bundle optionsIn = new Bundle(); |
| 1280 | if (options != null) { |
| 1281 | optionsIn.putAll(options); |
| 1282 | } |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1283 | optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName()); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1284 | return new AmsTask(activity, handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1285 | @Override |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1286 | public void doWork() throws RemoteException { |
| 1287 | mService.getAuthToken(mResponse, account, authTokenType, |
| 1288 | false /* notifyOnAuthFailure */, true /* expectActivityLaunch */, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1289 | optionsIn); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1290 | } |
| 1291 | }.start(); |
| 1292 | } |
| 1293 | |
| 1294 | /** |
| 1295 | * Gets an auth token of the specified type for a particular account, |
| 1296 | * optionally raising a notification if the user must enter credentials. |
| 1297 | * This method is intended for background tasks and services where the |
| 1298 | * user should not be immediately interrupted with a password prompt. |
| 1299 | * |
| 1300 | * <p>If a previously generated auth token is cached for this account and |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1301 | * type, then it is returned. Otherwise, if a saved password is |
| 1302 | * available, it is sent to the server to generate a new auth token. |
| 1303 | * Otherwise, an {@link Intent} is returned which, when started, will |
| 1304 | * prompt the user for a password. If the notifyAuthFailure parameter is |
| 1305 | * set, a status bar notification is also created with the same Intent, |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1306 | * alerting the user that they need to enter a password at some point. |
| 1307 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1308 | * <p>In that case, you may need to wait until the user responds, which |
| 1309 | * could take hours or days or forever. When the user does respond and |
| 1310 | * supply a new password, the account manager will broadcast the |
| 1311 | * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent, which applications can |
| 1312 | * use to try again. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1313 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1314 | * <p>If notifyAuthFailure is not set, it is the application's |
| 1315 | * responsibility to launch the returned Intent at some point. |
| 1316 | * Either way, the result from this call will not wait for user action. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1317 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1318 | * <p>Some authenticators have auth token <em>types</em>, whose value |
| 1319 | * is authenticator-dependent. Some services use different token types to |
| 1320 | * access different functionality -- for example, Google uses different auth |
| 1321 | * tokens to access Gmail and Google Calendar for the same account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1322 | * |
| 1323 | * <p>This method may be called from any thread, but the returned |
| 1324 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 1325 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1326 | * @param account The account to fetch an auth token for |
| 1327 | * @param authTokenType The auth token type, an authenticator-dependent |
| 1328 | * string token, must not be null |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1329 | * @param notifyAuthFailure True to add a notification to prompt the |
| 1330 | * user for a password if necessary, false to leave that to the caller |
| 1331 | * @param callback Callback to invoke when the request completes, |
| 1332 | * null for no callback |
| 1333 | * @param handler {@link Handler} identifying the callback thread, |
| 1334 | * null for the main thread |
| 1335 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
| 1336 | * at least the following fields on success: |
| 1337 | * <ul> |
| 1338 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied |
| 1339 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 1340 | * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted |
| 1341 | * </ul> |
| 1342 | * |
| 1343 | * (Other authenticator-specific values may be returned.) If the user |
| 1344 | * must enter credentials, the returned Bundle contains only |
| 1345 | * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt. |
| 1346 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1347 | * If an error occurred, {@link AccountManagerFuture#getResult()} throws: |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1348 | * <ul> |
| 1349 | * <li> {@link AuthenticatorException} if the authenticator failed to respond |
| 1350 | * <li> {@link OperationCanceledException} if the operation is canceled for |
| 1351 | * any reason, incluidng the user canceling a credential request |
| 1352 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 1353 | * creating a new auth token, usually because of network trouble |
| 1354 | * </ul> |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1355 | * If the account is no longer present on the device, the return value is |
| 1356 | * authenticator-dependent. The caller should verify the validity of the |
| 1357 | * account before requesting an auth token. |
Fred Quintana | ad93a32 | 2011-09-08 13:21:01 -0700 | [diff] [blame] | 1358 | * @deprecated use {@link #getAuthToken(Account, String, android.os.Bundle, |
| 1359 | * boolean, AccountManagerCallback, android.os.Handler)} instead |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1360 | */ |
Fred Quintana | ad93a32 | 2011-09-08 13:21:01 -0700 | [diff] [blame] | 1361 | @Deprecated |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1362 | public AccountManagerFuture<Bundle> getAuthToken( |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1363 | final Account account, final String authTokenType, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1364 | final boolean notifyAuthFailure, |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1365 | AccountManagerCallback<Bundle> callback, Handler handler) { |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1366 | return getAuthToken(account, authTokenType, null, notifyAuthFailure, callback, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1367 | handler); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | /** |
Fred Quintana | ad93a32 | 2011-09-08 13:21:01 -0700 | [diff] [blame] | 1371 | * Gets an auth token of the specified type for a particular account, |
| 1372 | * optionally raising a notification if the user must enter credentials. |
| 1373 | * This method is intended for background tasks and services where the |
| 1374 | * user should not be immediately interrupted with a password prompt. |
| 1375 | * |
| 1376 | * <p>If a previously generated auth token is cached for this account and |
| 1377 | * type, then it is returned. Otherwise, if a saved password is |
| 1378 | * available, it is sent to the server to generate a new auth token. |
| 1379 | * Otherwise, an {@link Intent} is returned which, when started, will |
| 1380 | * prompt the user for a password. If the notifyAuthFailure parameter is |
| 1381 | * set, a status bar notification is also created with the same Intent, |
| 1382 | * alerting the user that they need to enter a password at some point. |
| 1383 | * |
| 1384 | * <p>In that case, you may need to wait until the user responds, which |
| 1385 | * could take hours or days or forever. When the user does respond and |
| 1386 | * supply a new password, the account manager will broadcast the |
| 1387 | * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent, which applications can |
| 1388 | * use to try again. |
| 1389 | * |
| 1390 | * <p>If notifyAuthFailure is not set, it is the application's |
| 1391 | * responsibility to launch the returned Intent at some point. |
| 1392 | * Either way, the result from this call will not wait for user action. |
| 1393 | * |
| 1394 | * <p>Some authenticators have auth token <em>types</em>, whose value |
| 1395 | * is authenticator-dependent. Some services use different token types to |
| 1396 | * access different functionality -- for example, Google uses different auth |
| 1397 | * tokens to access Gmail and Google Calendar for the same account. |
| 1398 | * |
| 1399 | * <p>This method may be called from any thread, but the returned |
| 1400 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 1401 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1402 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1403 | * USE_CREDENTIALS permission is needed for those platforms. See docs for |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1404 | * this function in API level 22. |
| 1405 | * |
Fred Quintana | ad93a32 | 2011-09-08 13:21:01 -0700 | [diff] [blame] | 1406 | * @param account The account to fetch an auth token for |
| 1407 | * @param authTokenType The auth token type, an authenticator-dependent |
| 1408 | * string token, must not be null |
| 1409 | * @param options Authenticator-specific options for the request, |
| 1410 | * may be null or empty |
| 1411 | * @param notifyAuthFailure True to add a notification to prompt the |
| 1412 | * user for a password if necessary, false to leave that to the caller |
| 1413 | * @param callback Callback to invoke when the request completes, |
| 1414 | * null for no callback |
| 1415 | * @param handler {@link Handler} identifying the callback thread, |
| 1416 | * null for the main thread |
| 1417 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
| 1418 | * at least the following fields on success: |
| 1419 | * <ul> |
| 1420 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied |
| 1421 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 1422 | * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted |
| 1423 | * </ul> |
| 1424 | * |
| 1425 | * (Other authenticator-specific values may be returned.) If the user |
| 1426 | * must enter credentials, the returned Bundle contains only |
| 1427 | * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt. |
| 1428 | * |
| 1429 | * If an error occurred, {@link AccountManagerFuture#getResult()} throws: |
| 1430 | * <ul> |
| 1431 | * <li> {@link AuthenticatorException} if the authenticator failed to respond |
| 1432 | * <li> {@link OperationCanceledException} if the operation is canceled for |
| 1433 | * any reason, incluidng the user canceling a credential request |
| 1434 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 1435 | * creating a new auth token, usually because of network trouble |
| 1436 | * </ul> |
| 1437 | * If the account is no longer present on the device, the return value is |
| 1438 | * authenticator-dependent. The caller should verify the validity of the |
| 1439 | * account before requesting an auth token. |
| 1440 | */ |
| 1441 | public AccountManagerFuture<Bundle> getAuthToken( |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1442 | final Account account, final String authTokenType, final Bundle options, |
| 1443 | final boolean notifyAuthFailure, |
Fred Quintana | ad93a32 | 2011-09-08 13:21:01 -0700 | [diff] [blame] | 1444 | AccountManagerCallback<Bundle> callback, Handler handler) { |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1445 | |
Fred Quintana | ad93a32 | 2011-09-08 13:21:01 -0700 | [diff] [blame] | 1446 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1447 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Costin Manolache | e5847ad | 2011-09-14 12:52:19 -0700 | [diff] [blame] | 1448 | final Bundle optionsIn = new Bundle(); |
| 1449 | if (options != null) { |
| 1450 | optionsIn.putAll(options); |
| 1451 | } |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1452 | optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName()); |
Fred Quintana | ad93a32 | 2011-09-08 13:21:01 -0700 | [diff] [blame] | 1453 | return new AmsTask(null, handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1454 | @Override |
Fred Quintana | ad93a32 | 2011-09-08 13:21:01 -0700 | [diff] [blame] | 1455 | public void doWork() throws RemoteException { |
| 1456 | mService.getAuthToken(mResponse, account, authTokenType, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1457 | notifyAuthFailure, false /* expectActivityLaunch */, optionsIn); |
Fred Quintana | ad93a32 | 2011-09-08 13:21:01 -0700 | [diff] [blame] | 1458 | } |
| 1459 | }.start(); |
| 1460 | } |
| 1461 | |
| 1462 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1463 | * Asks the user to add an account of a specified type. The authenticator |
| 1464 | * for this account type processes this request with the appropriate user |
| 1465 | * interface. If the user does elect to create a new account, the account |
| 1466 | * name is returned. |
| 1467 | * |
| 1468 | * <p>This method may be called from any thread, but the returned |
| 1469 | * {@link AccountManagerFuture} must not be used on the main thread. |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1470 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1471 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 1472 | * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for |
| 1473 | * this function in API level 22. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1474 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1475 | * @param accountType The type of account to add; must not be null |
| 1476 | * @param authTokenType The type of auth token (see {@link #getAuthToken}) |
| 1477 | * this account will need to be able to generate, null for none |
| 1478 | * @param requiredFeatures The features (see {@link #hasFeatures}) this |
| 1479 | * account must have, null for none |
| 1480 | * @param addAccountOptions Authenticator-specific options for the request, |
| 1481 | * may be null or empty |
| 1482 | * @param activity The {@link Activity} context to use for launching a new |
| 1483 | * authenticator-defined sub-Activity to prompt the user to create an |
| 1484 | * account; used only to call startActivity(); if null, the prompt |
| 1485 | * will not be launched directly, but the necessary {@link Intent} |
| 1486 | * will be returned to the caller instead |
| 1487 | * @param callback Callback to invoke when the request completes, |
| 1488 | * null for no callback |
| 1489 | * @param handler {@link Handler} identifying the callback thread, |
| 1490 | * null for the main thread |
Doug Zongker | ff592dc | 2010-02-23 12:26:33 -0800 | [diff] [blame] | 1491 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1492 | * these fields if activity was specified and an account was created: |
| 1493 | * <ul> |
| 1494 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created |
| 1495 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 1496 | * </ul> |
| 1497 | * |
| 1498 | * If no activity was specified, the returned Bundle contains only |
| 1499 | * {@link #KEY_INTENT} with the {@link Intent} needed to launch the |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1500 | * actual account creation process. If an error occurred, |
| 1501 | * {@link AccountManagerFuture#getResult()} throws: |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1502 | * <ul> |
| 1503 | * <li> {@link AuthenticatorException} if no authenticator was registered for |
| 1504 | * this account type or the authenticator failed to respond |
| 1505 | * <li> {@link OperationCanceledException} if the operation was canceled for |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1506 | * any reason, including the user canceling the creation process or adding accounts |
| 1507 | * (of this type) has been disabled by policy |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1508 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 1509 | * creating a new account, usually because of network trouble |
| 1510 | * </ul> |
| 1511 | */ |
| 1512 | public AccountManagerFuture<Bundle> addAccount(final String accountType, |
| 1513 | final String authTokenType, final String[] requiredFeatures, |
| 1514 | final Bundle addAccountOptions, |
| 1515 | final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1516 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | e5847ad | 2011-09-14 12:52:19 -0700 | [diff] [blame] | 1517 | final Bundle optionsIn = new Bundle(); |
| 1518 | if (addAccountOptions != null) { |
| 1519 | optionsIn.putAll(addAccountOptions); |
| 1520 | } |
| 1521 | optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName()); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1522 | |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1523 | return new AmsTask(activity, handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1524 | @Override |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1525 | public void doWork() throws RemoteException { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1526 | mService.addAccount(mResponse, accountType, authTokenType, |
Costin Manolache | e5847ad | 2011-09-14 12:52:19 -0700 | [diff] [blame] | 1527 | requiredFeatures, activity != null, optionsIn); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1528 | } |
| 1529 | }.start(); |
| 1530 | } |
| 1531 | |
| 1532 | /** |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1533 | * @see #addAccount(String, String, String[], Bundle, Activity, AccountManagerCallback, Handler) |
| 1534 | * @hide |
| 1535 | */ |
| 1536 | public AccountManagerFuture<Bundle> addAccountAsUser(final String accountType, |
| 1537 | final String authTokenType, final String[] requiredFeatures, |
| 1538 | final Bundle addAccountOptions, final Activity activity, |
| 1539 | AccountManagerCallback<Bundle> callback, Handler handler, final UserHandle userHandle) { |
| 1540 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 1541 | if (userHandle == null) throw new IllegalArgumentException("userHandle is null"); |
| 1542 | final Bundle optionsIn = new Bundle(); |
| 1543 | if (addAccountOptions != null) { |
| 1544 | optionsIn.putAll(addAccountOptions); |
| 1545 | } |
| 1546 | optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName()); |
| 1547 | |
| 1548 | return new AmsTask(activity, handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1549 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1550 | public void doWork() throws RemoteException { |
| 1551 | mService.addAccountAsUser(mResponse, accountType, authTokenType, |
| 1552 | requiredFeatures, activity != null, optionsIn, userHandle.getIdentifier()); |
| 1553 | } |
| 1554 | }.start(); |
| 1555 | } |
| 1556 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 1557 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1558 | /** |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 1559 | * Adds shared accounts from a parent user to a secondary user. Adding the shared account |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1560 | * doesn't take effect immediately. When the target user starts up, any pending shared accounts |
| 1561 | * are attempted to be copied to the target user from the primary via calls to the |
| 1562 | * authenticator. |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 1563 | * @param parentUser parent user |
| 1564 | * @param user target user |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1565 | * @hide |
| 1566 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 1567 | public void addSharedAccountsFromParentUser(UserHandle parentUser, UserHandle user) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1568 | try { |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 1569 | mService.addSharedAccountsFromParentUser(parentUser.getIdentifier(), |
| 1570 | user.getIdentifier()); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1571 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 1572 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | /** |
Xiaohui Chen | 75f68ce | 2015-08-14 09:28:07 -0700 | [diff] [blame] | 1577 | * Copies an account from one user to another user. |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1578 | * @param account the account to copy |
Xiaohui Chen | 75f68ce | 2015-08-14 09:28:07 -0700 | [diff] [blame] | 1579 | * @param fromUser the user to copy the account from |
| 1580 | * @param toUser the target user |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1581 | * @param callback Callback to invoke when the request completes, |
| 1582 | * null for no callback |
| 1583 | * @param handler {@link Handler} identifying the callback thread, |
| 1584 | * null for the main thread |
| 1585 | * @return An {@link AccountManagerFuture} which resolves to a Boolean indicated wether it |
| 1586 | * succeeded. |
| 1587 | * @hide |
| 1588 | */ |
| 1589 | public AccountManagerFuture<Boolean> copyAccountToUser( |
Xiaohui Chen | 75f68ce | 2015-08-14 09:28:07 -0700 | [diff] [blame] | 1590 | final Account account, final UserHandle fromUser, final UserHandle toUser, |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1591 | AccountManagerCallback<Boolean> callback, Handler handler) { |
| 1592 | if (account == null) throw new IllegalArgumentException("account is null"); |
Xiaohui Chen | 75f68ce | 2015-08-14 09:28:07 -0700 | [diff] [blame] | 1593 | if (toUser == null || fromUser == null) { |
| 1594 | throw new IllegalArgumentException("fromUser and toUser cannot be null"); |
| 1595 | } |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1596 | |
| 1597 | return new Future2Task<Boolean>(handler, callback) { |
| 1598 | @Override |
| 1599 | public void doWork() throws RemoteException { |
| 1600 | mService.copyAccountToUser( |
Xiaohui Chen | 75f68ce | 2015-08-14 09:28:07 -0700 | [diff] [blame] | 1601 | mResponse, account, fromUser.getIdentifier(), toUser.getIdentifier()); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1602 | } |
| 1603 | @Override |
| 1604 | public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 1605 | if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) { |
| 1606 | throw new AuthenticatorException("no result in response"); |
| 1607 | } |
| 1608 | return bundle.getBoolean(KEY_BOOLEAN_RESULT); |
| 1609 | } |
| 1610 | }.start(); |
| 1611 | } |
| 1612 | |
| 1613 | /** |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1614 | * @hide |
| 1615 | * Removes the shared account. |
| 1616 | * @param account the account to remove |
| 1617 | * @param user the user to remove the account from |
| 1618 | * @return |
| 1619 | */ |
| 1620 | public boolean removeSharedAccount(final Account account, UserHandle user) { |
| 1621 | try { |
| 1622 | boolean val = mService.removeSharedAccountAsUser(account, user.getIdentifier()); |
| 1623 | return val; |
| 1624 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 1625 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | /** |
| 1630 | * @hide |
| 1631 | * @param user |
| 1632 | * @return |
| 1633 | */ |
| 1634 | public Account[] getSharedAccounts(UserHandle user) { |
| 1635 | try { |
| 1636 | return mService.getSharedAccountsAsUser(user.getIdentifier()); |
| 1637 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 1638 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1643 | * Confirms that the user knows the password for an account to make extra |
| 1644 | * sure they are the owner of the account. The user-entered password can |
| 1645 | * be supplied directly, otherwise the authenticator for this account type |
| 1646 | * prompts the user with the appropriate interface. This method is |
| 1647 | * intended for applications which want extra assurance; for example, the |
| 1648 | * phone lock screen uses this to let the user unlock the phone with an |
| 1649 | * account password if they forget the lock pattern. |
| 1650 | * |
| 1651 | * <p>If the user-entered password matches a saved password for this |
| 1652 | * account, the request is considered valid; otherwise the authenticator |
| 1653 | * verifies the password (usually by contacting the server). |
| 1654 | * |
| 1655 | * <p>This method may be called from any thread, but the returned |
| 1656 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 1657 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1658 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 1659 | * MANAGE_ACCOUNTS permission is needed for those platforms. See docs |
| 1660 | * for this function in API level 22. |
| 1661 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1662 | * @param account The account to confirm password knowledge for |
| 1663 | * @param options Authenticator-specific options for the request; |
| 1664 | * if the {@link #KEY_PASSWORD} string field is present, the |
| 1665 | * authenticator may use it directly rather than prompting the user; |
| 1666 | * may be null or empty |
| 1667 | * @param activity The {@link Activity} context to use for launching a new |
| 1668 | * authenticator-defined sub-Activity to prompt the user to enter a |
| 1669 | * password; used only to call startActivity(); if null, the prompt |
| 1670 | * will not be launched directly, but the necessary {@link Intent} |
| 1671 | * will be returned to the caller instead |
| 1672 | * @param callback Callback to invoke when the request completes, |
| 1673 | * null for no callback |
| 1674 | * @param handler {@link Handler} identifying the callback thread, |
| 1675 | * null for the main thread |
| 1676 | * @return An {@link AccountManagerFuture} which resolves to a Bundle |
| 1677 | * with these fields if activity or password was supplied and |
| 1678 | * the account was successfully verified: |
| 1679 | * <ul> |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1680 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account verified |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1681 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 1682 | * <li> {@link #KEY_BOOLEAN_RESULT} - true to indicate success |
| 1683 | * </ul> |
| 1684 | * |
| 1685 | * If no activity or password was specified, the returned Bundle contains |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1686 | * {@link #KEY_INTENT} with the {@link Intent} needed to launch the |
| 1687 | * password prompt. |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1688 | * |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1689 | * <p>Also the returning Bundle may contain {@link |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 1690 | * #KEY_LAST_AUTHENTICATED_TIME} indicating the last time the |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1691 | * credential was validated/created. |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1692 | * |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1693 | * If an error occurred,{@link AccountManagerFuture#getResult()} throws: |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1694 | * <ul> |
| 1695 | * <li> {@link AuthenticatorException} if the authenticator failed to respond |
| 1696 | * <li> {@link OperationCanceledException} if the operation was canceled for |
| 1697 | * any reason, including the user canceling the password prompt |
| 1698 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 1699 | * verifying the password, usually because of network trouble |
| 1700 | * </ul> |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1701 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1702 | public AccountManagerFuture<Bundle> confirmCredentials(final Account account, |
| 1703 | final Bundle options, |
| 1704 | final Activity activity, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1705 | final AccountManagerCallback<Bundle> callback, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1706 | final Handler handler) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1707 | return confirmCredentialsAsUser(account, options, activity, callback, handler, |
| 1708 | Process.myUserHandle()); |
| 1709 | } |
| 1710 | |
| 1711 | /** |
| 1712 | * @hide |
| 1713 | * Same as {@link #confirmCredentials(Account, Bundle, Activity, AccountManagerCallback, Handler)} |
| 1714 | * but for the specified user. |
| 1715 | */ |
| 1716 | public AccountManagerFuture<Bundle> confirmCredentialsAsUser(final Account account, |
| 1717 | final Bundle options, |
| 1718 | final Activity activity, |
| 1719 | final AccountManagerCallback<Bundle> callback, |
| 1720 | final Handler handler, UserHandle userHandle) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1721 | if (account == null) throw new IllegalArgumentException("account is null"); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1722 | final int userId = userHandle.getIdentifier(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1723 | return new AmsTask(activity, handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1724 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1725 | public void doWork() throws RemoteException { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 1726 | mService.confirmCredentialsAsUser(mResponse, account, options, activity != null, |
| 1727 | userId); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1728 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1729 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1730 | } |
| 1731 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1732 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1733 | * Asks the user to enter a new password for an account, updating the |
| 1734 | * saved credentials for the account. Normally this happens automatically |
| 1735 | * when the server rejects credentials during an auth token fetch, but this |
| 1736 | * can be invoked directly to ensure we have the correct credentials stored. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1737 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1738 | * <p>This method may be called from any thread, but the returned |
| 1739 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 1740 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1741 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 1742 | * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for |
| 1743 | * this function in API level 22. |
| 1744 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1745 | * @param account The account to update credentials for |
| 1746 | * @param authTokenType The credentials entered must allow an auth token |
| 1747 | * of this type to be created (but no actual auth token is returned); |
| 1748 | * may be null |
| 1749 | * @param options Authenticator-specific options for the request; |
| 1750 | * may be null or empty |
| 1751 | * @param activity The {@link Activity} context to use for launching a new |
| 1752 | * authenticator-defined sub-Activity to prompt the user to enter a |
| 1753 | * password; used only to call startActivity(); if null, the prompt |
| 1754 | * will not be launched directly, but the necessary {@link Intent} |
| 1755 | * will be returned to the caller instead |
| 1756 | * @param callback Callback to invoke when the request completes, |
| 1757 | * null for no callback |
| 1758 | * @param handler {@link Handler} identifying the callback thread, |
| 1759 | * null for the main thread |
| 1760 | * @return An {@link AccountManagerFuture} which resolves to a Bundle |
| 1761 | * with these fields if an activity was supplied and the account |
| 1762 | * credentials were successfully updated: |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1763 | * <ul> |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1764 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1765 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1766 | * </ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1767 | * |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1768 | * If no activity was specified, the returned Bundle contains |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1769 | * {@link #KEY_INTENT} with the {@link Intent} needed to launch the |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1770 | * password prompt. If an error occurred, |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1771 | * {@link AccountManagerFuture#getResult()} throws: |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1772 | * <ul> |
| 1773 | * <li> {@link AuthenticatorException} if the authenticator failed to respond |
| 1774 | * <li> {@link OperationCanceledException} if the operation was canceled for |
| 1775 | * any reason, including the user canceling the password prompt |
| 1776 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 1777 | * verifying the password, usually because of network trouble |
| 1778 | * </ul> |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1779 | */ |
| 1780 | public AccountManagerFuture<Bundle> updateCredentials(final Account account, |
| 1781 | final String authTokenType, |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1782 | final Bundle options, final Activity activity, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1783 | final AccountManagerCallback<Bundle> callback, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1784 | final Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1785 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1786 | return new AmsTask(activity, handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1787 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1788 | public void doWork() throws RemoteException { |
| 1789 | mService.updateCredentials(mResponse, account, authTokenType, activity != null, |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1790 | options); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1791 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1792 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1795 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1796 | * Offers the user an opportunity to change an authenticator's settings. |
| 1797 | * These properties are for the authenticator in general, not a particular |
| 1798 | * account. Not all authenticators support this method. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1799 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1800 | * <p>This method may be called from any thread, but the returned |
| 1801 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 1802 | * |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1803 | * <p>This method requires the caller to have the same signature as the |
| 1804 | * authenticator associated with the specified account type. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1805 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 1806 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 1807 | * MANAGE_ACCOUNTS permission is needed for those platforms. See docs |
| 1808 | * for this function in API level 22. |
| 1809 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1810 | * @param accountType The account type associated with the authenticator |
| 1811 | * to adjust |
| 1812 | * @param activity The {@link Activity} context to use for launching a new |
| 1813 | * authenticator-defined sub-Activity to adjust authenticator settings; |
| 1814 | * used only to call startActivity(); if null, the settings dialog will |
| 1815 | * not be launched directly, but the necessary {@link Intent} will be |
| 1816 | * returned to the caller instead |
| 1817 | * @param callback Callback to invoke when the request completes, |
| 1818 | * null for no callback |
| 1819 | * @param handler {@link Handler} identifying the callback thread, |
| 1820 | * null for the main thread |
| 1821 | * @return An {@link AccountManagerFuture} which resolves to a Bundle |
| 1822 | * which is empty if properties were edited successfully, or |
| 1823 | * if no activity was specified, contains only {@link #KEY_INTENT} |
| 1824 | * needed to launch the authenticator's settings dialog. |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1825 | * If an error occurred, {@link AccountManagerFuture#getResult()} |
| 1826 | * throws: |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1827 | * <ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1828 | * <li> {@link AuthenticatorException} if no authenticator was registered for |
| 1829 | * this account type or the authenticator failed to respond |
| 1830 | * <li> {@link OperationCanceledException} if the operation was canceled for |
| 1831 | * any reason, including the user canceling the settings dialog |
| 1832 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 1833 | * updating settings, usually because of network trouble |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1834 | * </ul> |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1835 | */ |
| 1836 | public AccountManagerFuture<Bundle> editProperties(final String accountType, |
| 1837 | final Activity activity, final AccountManagerCallback<Bundle> callback, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1838 | final Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1839 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1840 | return new AmsTask(activity, handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1841 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1842 | public void doWork() throws RemoteException { |
| 1843 | mService.editProperties(mResponse, accountType, activity != null); |
| 1844 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1845 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1848 | /** |
| 1849 | * @hide |
| 1850 | * Checks if the given account exists on any of the users on the device. |
| 1851 | * Only the system process can call this method. |
| 1852 | * |
| 1853 | * @param account The account to check for existence. |
| 1854 | * @return whether any user has this account |
| 1855 | */ |
| 1856 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 1857 | try { |
| 1858 | return mService.someUserHasAccount(account); |
| 1859 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 1860 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1861 | } |
| 1862 | } |
| 1863 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1864 | private void ensureNotOnMainThread() { |
| 1865 | final Looper looper = Looper.myLooper(); |
| 1866 | if (looper != null && looper == mContext.getMainLooper()) { |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 1867 | final IllegalStateException exception = new IllegalStateException( |
| 1868 | "calling this from your main thread can lead to deadlock"); |
| 1869 | Log.e(TAG, "calling this from your main thread can lead to deadlock and/or ANRs", |
| 1870 | exception); |
Fred Quintana | 751fdc0 | 2010-02-09 14:13:18 -0800 | [diff] [blame] | 1871 | if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.FROYO) { |
| 1872 | throw exception; |
| 1873 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1877 | private void postToHandler(Handler handler, final AccountManagerCallback<Bundle> callback, |
| 1878 | final AccountManagerFuture<Bundle> future) { |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1879 | handler = handler == null ? mMainHandler : handler; |
| 1880 | handler.post(new Runnable() { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1881 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1882 | public void run() { |
| 1883 | callback.run(future); |
| 1884 | } |
| 1885 | }); |
| 1886 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1887 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1888 | private void postToHandler(Handler handler, final OnAccountsUpdateListener listener, |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1889 | final Account[] accounts) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1890 | final Account[] accountsCopy = new Account[accounts.length]; |
| 1891 | // send a copy to make sure that one doesn't |
| 1892 | // change what another sees |
| 1893 | System.arraycopy(accounts, 0, accountsCopy, 0, accountsCopy.length); |
| 1894 | handler = (handler == null) ? mMainHandler : handler; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1895 | handler.post(new Runnable() { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1896 | @Override |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1897 | public void run() { |
Costin Manolache | b643724 | 2009-09-10 16:14:12 -0700 | [diff] [blame] | 1898 | try { |
| 1899 | listener.onAccountsUpdated(accountsCopy); |
| 1900 | } catch (SQLException e) { |
| 1901 | // Better luck next time. If the problem was disk-full, |
| 1902 | // the STORAGE_OK intent will re-trigger the update. |
| 1903 | Log.e(TAG, "Can't update accounts", e); |
| 1904 | } |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1905 | } |
| 1906 | }); |
| 1907 | } |
| 1908 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1909 | private abstract class AmsTask extends FutureTask<Bundle> implements AccountManagerFuture<Bundle> { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1910 | final IAccountManagerResponse mResponse; |
| 1911 | final Handler mHandler; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1912 | final AccountManagerCallback<Bundle> mCallback; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1913 | final Activity mActivity; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1914 | public AmsTask(Activity activity, Handler handler, AccountManagerCallback<Bundle> callback) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1915 | super(new Callable<Bundle>() { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1916 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1917 | public Bundle call() throws Exception { |
| 1918 | throw new IllegalStateException("this should never be called"); |
| 1919 | } |
| 1920 | }); |
| 1921 | |
| 1922 | mHandler = handler; |
| 1923 | mCallback = callback; |
| 1924 | mActivity = activity; |
| 1925 | mResponse = new Response(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1926 | } |
| 1927 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1928 | public final AccountManagerFuture<Bundle> start() { |
| 1929 | try { |
| 1930 | doWork(); |
| 1931 | } catch (RemoteException e) { |
| 1932 | setException(e); |
| 1933 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1934 | return this; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1935 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1936 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1937 | @Override |
Fred Quintana | 96580e0 | 2010-03-04 13:42:42 -0800 | [diff] [blame] | 1938 | protected void set(Bundle bundle) { |
| 1939 | // TODO: somehow a null is being set as the result of the Future. Log this |
| 1940 | // case to help debug where this is occurring. When this bug is fixed this |
| 1941 | // condition statement should be removed. |
| 1942 | if (bundle == null) { |
| 1943 | Log.e(TAG, "the bundle must not be null", new Exception()); |
| 1944 | } |
| 1945 | super.set(bundle); |
| 1946 | } |
| 1947 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1948 | public abstract void doWork() throws RemoteException; |
| 1949 | |
| 1950 | private Bundle internalGetResult(Long timeout, TimeUnit unit) |
| 1951 | throws OperationCanceledException, IOException, AuthenticatorException { |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 1952 | if (!isDone()) { |
| 1953 | ensureNotOnMainThread(); |
| 1954 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1955 | try { |
| 1956 | if (timeout == null) { |
| 1957 | return get(); |
| 1958 | } else { |
| 1959 | return get(timeout, unit); |
| 1960 | } |
| 1961 | } catch (CancellationException e) { |
| 1962 | throw new OperationCanceledException(); |
| 1963 | } catch (TimeoutException e) { |
| 1964 | // fall through and cancel |
| 1965 | } catch (InterruptedException e) { |
| 1966 | // fall through and cancel |
| 1967 | } catch (ExecutionException e) { |
| 1968 | final Throwable cause = e.getCause(); |
| 1969 | if (cause instanceof IOException) { |
| 1970 | throw (IOException) cause; |
| 1971 | } else if (cause instanceof UnsupportedOperationException) { |
| 1972 | throw new AuthenticatorException(cause); |
| 1973 | } else if (cause instanceof AuthenticatorException) { |
| 1974 | throw (AuthenticatorException) cause; |
| 1975 | } else if (cause instanceof RuntimeException) { |
| 1976 | throw (RuntimeException) cause; |
| 1977 | } else if (cause instanceof Error) { |
| 1978 | throw (Error) cause; |
| 1979 | } else { |
| 1980 | throw new IllegalStateException(cause); |
| 1981 | } |
| 1982 | } finally { |
| 1983 | cancel(true /* interrupt if running */); |
| 1984 | } |
| 1985 | throw new OperationCanceledException(); |
| 1986 | } |
| 1987 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1988 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1989 | public Bundle getResult() |
| 1990 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 1991 | return internalGetResult(null, null); |
| 1992 | } |
| 1993 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 1994 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1995 | public Bundle getResult(long timeout, TimeUnit unit) |
| 1996 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 1997 | return internalGetResult(timeout, unit); |
| 1998 | } |
| 1999 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2000 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2001 | protected void done() { |
| 2002 | if (mCallback != null) { |
| 2003 | postToHandler(mHandler, mCallback, this); |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | /** Handles the responses from the AccountManager */ |
| 2008 | private class Response extends IAccountManagerResponse.Stub { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2009 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2010 | public void onResult(Bundle bundle) { |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 2011 | Intent intent = bundle.getParcelable(KEY_INTENT); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2012 | if (intent != null && mActivity != null) { |
| 2013 | // since the user provided an Activity we will silently start intents |
| 2014 | // that we see |
| 2015 | mActivity.startActivity(intent); |
| 2016 | // leave the Future running to wait for the real response to this request |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2017 | } else if (bundle.getBoolean("retry")) { |
| 2018 | try { |
| 2019 | doWork(); |
| 2020 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame^] | 2021 | throw e.rethrowFromSystemServer(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2022 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2023 | } else { |
| 2024 | set(bundle); |
| 2025 | } |
| 2026 | } |
| 2027 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2028 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2029 | public void onError(int code, String message) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2030 | if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED |
| 2031 | || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 2032 | // the authenticator indicated that this request was canceled or we were |
| 2033 | // forbidden to fulfill; cancel now |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2034 | cancel(true /* mayInterruptIfRunning */); |
| 2035 | return; |
| 2036 | } |
| 2037 | setException(convertErrorToException(code, message)); |
| 2038 | } |
| 2039 | } |
| 2040 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2041 | } |
| 2042 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2043 | private abstract class BaseFutureTask<T> extends FutureTask<T> { |
| 2044 | final public IAccountManagerResponse mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2045 | final Handler mHandler; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2046 | |
| 2047 | public BaseFutureTask(Handler handler) { |
| 2048 | super(new Callable<T>() { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2049 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2050 | public T call() throws Exception { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2051 | throw new IllegalStateException("this should never be called"); |
| 2052 | } |
| 2053 | }); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2054 | mHandler = handler; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2055 | mResponse = new Response(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2056 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2057 | |
| 2058 | public abstract void doWork() throws RemoteException; |
| 2059 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2060 | public abstract T bundleToResult(Bundle bundle) throws AuthenticatorException; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2061 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2062 | protected void postRunnableToHandler(Runnable runnable) { |
| 2063 | Handler handler = (mHandler == null) ? mMainHandler : mHandler; |
| 2064 | handler.post(runnable); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2065 | } |
| 2066 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2067 | protected void startTask() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2068 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2069 | doWork(); |
| 2070 | } catch (RemoteException e) { |
| 2071 | setException(e); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2072 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2073 | } |
| 2074 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2075 | protected class Response extends IAccountManagerResponse.Stub { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2076 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2077 | public void onResult(Bundle bundle) { |
| 2078 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2079 | T result = bundleToResult(bundle); |
| 2080 | if (result == null) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2081 | return; |
| 2082 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2083 | set(result); |
| 2084 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2085 | } catch (ClassCastException e) { |
| 2086 | // we will set the exception below |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2087 | } catch (AuthenticatorException e) { |
| 2088 | // we will set the exception below |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2089 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2090 | onError(ERROR_CODE_INVALID_RESPONSE, "no result in response"); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2093 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2094 | public void onError(int code, String message) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2095 | if (code == ERROR_CODE_CANCELED || code == ERROR_CODE_USER_RESTRICTED |
| 2096 | || code == ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 2097 | // the authenticator indicated that this request was canceled or we were |
| 2098 | // forbidden to fulfill; cancel now |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2099 | cancel(true /* mayInterruptIfRunning */); |
| 2100 | return; |
| 2101 | } |
| 2102 | setException(convertErrorToException(code, message)); |
| 2103 | } |
| 2104 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | private abstract class Future2Task<T> |
| 2108 | extends BaseFutureTask<T> implements AccountManagerFuture<T> { |
| 2109 | final AccountManagerCallback<T> mCallback; |
| 2110 | public Future2Task(Handler handler, AccountManagerCallback<T> callback) { |
| 2111 | super(handler); |
| 2112 | mCallback = callback; |
| 2113 | } |
| 2114 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2115 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2116 | protected void done() { |
| 2117 | if (mCallback != null) { |
| 2118 | postRunnableToHandler(new Runnable() { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2119 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2120 | public void run() { |
| 2121 | mCallback.run(Future2Task.this); |
| 2122 | } |
| 2123 | }); |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | public Future2Task<T> start() { |
| 2128 | startTask(); |
| 2129 | return this; |
| 2130 | } |
| 2131 | |
| 2132 | private T internalGetResult(Long timeout, TimeUnit unit) |
| 2133 | throws OperationCanceledException, IOException, AuthenticatorException { |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 2134 | if (!isDone()) { |
| 2135 | ensureNotOnMainThread(); |
| 2136 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2137 | try { |
| 2138 | if (timeout == null) { |
| 2139 | return get(); |
| 2140 | } else { |
| 2141 | return get(timeout, unit); |
| 2142 | } |
| 2143 | } catch (InterruptedException e) { |
| 2144 | // fall through and cancel |
| 2145 | } catch (TimeoutException e) { |
| 2146 | // fall through and cancel |
| 2147 | } catch (CancellationException e) { |
| 2148 | // fall through and cancel |
| 2149 | } catch (ExecutionException e) { |
| 2150 | final Throwable cause = e.getCause(); |
| 2151 | if (cause instanceof IOException) { |
| 2152 | throw (IOException) cause; |
| 2153 | } else if (cause instanceof UnsupportedOperationException) { |
| 2154 | throw new AuthenticatorException(cause); |
| 2155 | } else if (cause instanceof AuthenticatorException) { |
| 2156 | throw (AuthenticatorException) cause; |
| 2157 | } else if (cause instanceof RuntimeException) { |
| 2158 | throw (RuntimeException) cause; |
| 2159 | } else if (cause instanceof Error) { |
| 2160 | throw (Error) cause; |
| 2161 | } else { |
| 2162 | throw new IllegalStateException(cause); |
| 2163 | } |
| 2164 | } finally { |
| 2165 | cancel(true /* interrupt if running */); |
| 2166 | } |
| 2167 | throw new OperationCanceledException(); |
| 2168 | } |
| 2169 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2170 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2171 | public T getResult() |
| 2172 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 2173 | return internalGetResult(null, null); |
| 2174 | } |
| 2175 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2176 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2177 | public T getResult(long timeout, TimeUnit unit) |
| 2178 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 2179 | return internalGetResult(timeout, unit); |
| 2180 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2181 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2182 | } |
| 2183 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2184 | private Exception convertErrorToException(int code, String message) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2185 | if (code == ERROR_CODE_NETWORK_ERROR) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2186 | return new IOException(message); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2187 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2188 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2189 | if (code == ERROR_CODE_UNSUPPORTED_OPERATION) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2190 | return new UnsupportedOperationException(message); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2191 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2192 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2193 | if (code == ERROR_CODE_INVALID_RESPONSE) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2194 | return new AuthenticatorException(message); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2197 | if (code == ERROR_CODE_BAD_ARGUMENTS) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2198 | return new IllegalArgumentException(message); |
| 2199 | } |
| 2200 | |
| 2201 | return new AuthenticatorException(message); |
| 2202 | } |
| 2203 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2204 | private class GetAuthTokenByTypeAndFeaturesTask |
| 2205 | extends AmsTask implements AccountManagerCallback<Bundle> { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2206 | GetAuthTokenByTypeAndFeaturesTask(final String accountType, final String authTokenType, |
| 2207 | final String[] features, Activity activityForPrompting, |
| 2208 | final Bundle addAccountOptions, final Bundle loginOptions, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2209 | AccountManagerCallback<Bundle> callback, Handler handler) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2210 | super(activityForPrompting, handler, callback); |
| 2211 | if (accountType == null) throw new IllegalArgumentException("account type is null"); |
| 2212 | mAccountType = accountType; |
| 2213 | mAuthTokenType = authTokenType; |
| 2214 | mFeatures = features; |
| 2215 | mAddAccountOptions = addAccountOptions; |
| 2216 | mLoginOptions = loginOptions; |
| 2217 | mMyCallback = this; |
| 2218 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2219 | volatile AccountManagerFuture<Bundle> mFuture = null; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2220 | final String mAccountType; |
| 2221 | final String mAuthTokenType; |
| 2222 | final String[] mFeatures; |
| 2223 | final Bundle mAddAccountOptions; |
| 2224 | final Bundle mLoginOptions; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2225 | final AccountManagerCallback<Bundle> mMyCallback; |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 2226 | private volatile int mNumAccounts = 0; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2227 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2228 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2229 | public void doWork() throws RemoteException { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2230 | getAccountsByTypeAndFeatures(mAccountType, mFeatures, |
| 2231 | new AccountManagerCallback<Account[]>() { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2232 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2233 | public void run(AccountManagerFuture<Account[]> future) { |
| 2234 | Account[] accounts; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2235 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2236 | accounts = future.getResult(); |
| 2237 | } catch (OperationCanceledException e) { |
| 2238 | setException(e); |
| 2239 | return; |
| 2240 | } catch (IOException e) { |
| 2241 | setException(e); |
| 2242 | return; |
| 2243 | } catch (AuthenticatorException e) { |
| 2244 | setException(e); |
| 2245 | return; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2246 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2247 | |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 2248 | mNumAccounts = accounts.length; |
| 2249 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2250 | if (accounts.length == 0) { |
| 2251 | if (mActivity != null) { |
| 2252 | // no accounts, add one now. pretend that the user directly |
| 2253 | // made this request |
| 2254 | mFuture = addAccount(mAccountType, mAuthTokenType, mFeatures, |
| 2255 | mAddAccountOptions, mActivity, mMyCallback, mHandler); |
| 2256 | } else { |
| 2257 | // send result since we can't prompt to add an account |
| 2258 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2259 | result.putString(KEY_ACCOUNT_NAME, null); |
| 2260 | result.putString(KEY_ACCOUNT_TYPE, null); |
| 2261 | result.putString(KEY_AUTHTOKEN, null); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2262 | try { |
| 2263 | mResponse.onResult(result); |
| 2264 | } catch (RemoteException e) { |
| 2265 | // this will never happen |
| 2266 | } |
| 2267 | // we are done |
| 2268 | } |
| 2269 | } else if (accounts.length == 1) { |
| 2270 | // have a single account, return an authtoken for it |
| 2271 | if (mActivity == null) { |
| 2272 | mFuture = getAuthToken(accounts[0], mAuthTokenType, |
| 2273 | false /* notifyAuthFailure */, mMyCallback, mHandler); |
| 2274 | } else { |
| 2275 | mFuture = getAuthToken(accounts[0], |
| 2276 | mAuthTokenType, mLoginOptions, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2277 | mActivity, mMyCallback, mHandler); |
| 2278 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2279 | } else { |
| 2280 | if (mActivity != null) { |
| 2281 | IAccountManagerResponse chooseResponse = |
| 2282 | new IAccountManagerResponse.Stub() { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2283 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2284 | public void onResult(Bundle value) throws RemoteException { |
| 2285 | Account account = new Account( |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2286 | value.getString(KEY_ACCOUNT_NAME), |
| 2287 | value.getString(KEY_ACCOUNT_TYPE)); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2288 | mFuture = getAuthToken(account, mAuthTokenType, mLoginOptions, |
| 2289 | mActivity, mMyCallback, mHandler); |
| 2290 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2291 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2292 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2293 | public void onError(int errorCode, String errorMessage) |
| 2294 | throws RemoteException { |
| 2295 | mResponse.onError(errorCode, errorMessage); |
| 2296 | } |
| 2297 | }; |
| 2298 | // have many accounts, launch the chooser |
| 2299 | Intent intent = new Intent(); |
Amith Yamasani | 12b8e13 | 2013-03-14 10:48:07 -0700 | [diff] [blame] | 2300 | ComponentName componentName = ComponentName.unflattenFromString( |
| 2301 | Resources.getSystem().getString( |
| 2302 | R.string.config_chooseAccountActivity)); |
| 2303 | intent.setClassName(componentName.getPackageName(), |
| 2304 | componentName.getClassName()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2305 | intent.putExtra(KEY_ACCOUNTS, accounts); |
| 2306 | intent.putExtra(KEY_ACCOUNT_MANAGER_RESPONSE, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2307 | new AccountManagerResponse(chooseResponse)); |
| 2308 | mActivity.startActivity(intent); |
| 2309 | // the result will arrive via the IAccountManagerResponse |
| 2310 | } else { |
| 2311 | // send result since we can't prompt to select an account |
| 2312 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2313 | result.putString(KEY_ACCOUNTS, null); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2314 | try { |
| 2315 | mResponse.onResult(result); |
| 2316 | } catch (RemoteException e) { |
| 2317 | // this will never happen |
| 2318 | } |
| 2319 | // we are done |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2320 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2321 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2322 | }}, mHandler); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2325 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2326 | public void run(AccountManagerFuture<Bundle> future) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2327 | try { |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 2328 | final Bundle result = future.getResult(); |
| 2329 | if (mNumAccounts == 0) { |
| 2330 | final String accountName = result.getString(KEY_ACCOUNT_NAME); |
| 2331 | final String accountType = result.getString(KEY_ACCOUNT_TYPE); |
| 2332 | if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) { |
| 2333 | setException(new AuthenticatorException("account not in result")); |
| 2334 | return; |
| 2335 | } |
| 2336 | final Account account = new Account(accountName, accountType); |
| 2337 | mNumAccounts = 1; |
| 2338 | getAuthToken(account, mAuthTokenType, null /* options */, mActivity, |
| 2339 | mMyCallback, mHandler); |
| 2340 | return; |
| 2341 | } |
| 2342 | set(result); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2343 | } catch (OperationCanceledException e) { |
| 2344 | cancel(true /* mayInterruptIfRUnning */); |
| 2345 | } catch (IOException e) { |
| 2346 | setException(e); |
| 2347 | } catch (AuthenticatorException e) { |
| 2348 | setException(e); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2349 | } |
| 2350 | } |
| 2351 | } |
| 2352 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2353 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2354 | * This convenience helper combines the functionality of |
| 2355 | * {@link #getAccountsByTypeAndFeatures}, {@link #getAuthToken}, and |
| 2356 | * {@link #addAccount}. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2357 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2358 | * <p>This method gets a list of the accounts matching the |
| 2359 | * specified type and feature set; if there is exactly one, it is |
| 2360 | * used; if there are more than one, the user is prompted to pick one; |
| 2361 | * if there are none, the user is prompted to add one. Finally, |
| 2362 | * an auth token is acquired for the chosen account. |
| 2363 | * |
| 2364 | * <p>This method may be called from any thread, but the returned |
| 2365 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 2366 | * |
Simranjit Singh Kohli | 210bace | 2015-07-29 16:34:49 -0700 | [diff] [blame] | 2367 | * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before, |
| 2368 | * MANAGE_ACCOUNTS permission is needed for those platforms. See docs for |
| 2369 | * this function in API level 22. |
| 2370 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2371 | * @param accountType The account type required |
Doug Zongker | ff592dc | 2010-02-23 12:26:33 -0800 | [diff] [blame] | 2372 | * (see {@link #getAccountsByType}), must not be null |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2373 | * @param authTokenType The desired auth token type |
| 2374 | * (see {@link #getAuthToken}), must not be null |
| 2375 | * @param features Required features for the account |
| 2376 | * (see {@link #getAccountsByTypeAndFeatures}), may be null or empty |
| 2377 | * @param activity The {@link Activity} context to use for launching new |
| 2378 | * sub-Activities to prompt to add an account, select an account, |
| 2379 | * and/or enter a password, as necessary; used only to call |
| 2380 | * startActivity(); should not be null |
| 2381 | * @param addAccountOptions Authenticator-specific options to use for |
| 2382 | * adding new accounts; may be null or empty |
| 2383 | * @param getAuthTokenOptions Authenticator-specific options to use for |
| 2384 | * getting auth tokens; may be null or empty |
| 2385 | * @param callback Callback to invoke when the request completes, |
| 2386 | * null for no callback |
| 2387 | * @param handler {@link Handler} identifying the callback thread, |
| 2388 | * null for the main thread |
| 2389 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
| 2390 | * at least the following fields: |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2391 | * <ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2392 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account |
| 2393 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 2394 | * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2395 | * </ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2396 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 2397 | * If an error occurred, {@link AccountManagerFuture#getResult()} throws: |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2398 | * <ul> |
| 2399 | * <li> {@link AuthenticatorException} if no authenticator was registered for |
| 2400 | * this account type or the authenticator failed to respond |
| 2401 | * <li> {@link OperationCanceledException} if the operation was canceled for |
| 2402 | * any reason, including the user canceling any operation |
| 2403 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 2404 | * updating settings, usually because of network trouble |
| 2405 | * </ul> |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2406 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2407 | public AccountManagerFuture<Bundle> getAuthTokenByFeatures( |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2408 | final String accountType, final String authTokenType, final String[] features, |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2409 | final Activity activity, final Bundle addAccountOptions, |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2410 | final Bundle getAuthTokenOptions, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2411 | final AccountManagerCallback<Bundle> callback, final Handler handler) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2412 | if (accountType == null) throw new IllegalArgumentException("account type is null"); |
| 2413 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2414 | final GetAuthTokenByTypeAndFeaturesTask task = |
| 2415 | new GetAuthTokenByTypeAndFeaturesTask(accountType, authTokenType, features, |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2416 | activity, addAccountOptions, getAuthTokenOptions, callback, handler); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2417 | task.start(); |
| 2418 | return task; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2419 | } |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2420 | |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2421 | /** |
Carlos Valdivia | a3db8ac | 2015-07-10 13:04:43 -0700 | [diff] [blame] | 2422 | * Deprecated in favor of {@link #newChooseAccountIntent(Account, List, String[], String, |
| 2423 | * String, String[], Bundle)}. |
| 2424 | * |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2425 | * Returns an intent to an {@link Activity} that prompts the user to choose from a list of |
| 2426 | * accounts. |
| 2427 | * The caller will then typically start the activity by calling |
Mark Fickett | ab249e0 | 2012-09-05 09:45:47 -0400 | [diff] [blame] | 2428 | * <code>startActivityForResult(intent, ...);</code>. |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2429 | * <p> |
| 2430 | * On success the activity returns a Bundle with the account name and type specified using |
| 2431 | * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}. |
| 2432 | * <p> |
| 2433 | * The most common case is to call this with one account type, e.g.: |
| 2434 | * <p> |
kmccormick | f783ce5 | 2013-03-29 14:31:54 -0700 | [diff] [blame] | 2435 | * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, false, null, |
Fred Quintana | d88324d | 2011-09-19 11:43:05 -0700 | [diff] [blame] | 2436 | * null, null, null);</pre> |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2437 | * @param selectedAccount if specified, indicates that the {@link Account} is the currently |
| 2438 | * selected one, according to the caller's definition of selected. |
Carlos Valdivia | a3db8ac | 2015-07-10 13:04:43 -0700 | [diff] [blame] | 2439 | * @param allowableAccounts an optional {@link List} of accounts that are allowed to be |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2440 | * shown. If not specified then this field will not limit the displayed accounts. |
| 2441 | * @param allowableAccountTypes an optional string array of account types. These are used |
| 2442 | * both to filter the shown accounts and to filter the list of account types that are shown |
Simranjit Singh Kohli | 734f8fb | 2015-05-22 14:00:32 -0700 | [diff] [blame] | 2443 | * when adding an account. If not specified then this field will not limit the displayed |
| 2444 | * account types when adding an account. |
Carlos Valdivia | a3db8ac | 2015-07-10 13:04:43 -0700 | [diff] [blame] | 2445 | * @param alwaysPromptForAccount boolean that is ignored. |
Fred Quintana | d88324d | 2011-09-19 11:43:05 -0700 | [diff] [blame] | 2446 | * @param descriptionOverrideText if non-null this string is used as the description in the |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 2447 | * accounts chooser screen rather than the default |
Fred Quintana | d88324d | 2011-09-19 11:43:05 -0700 | [diff] [blame] | 2448 | * @param addAccountAuthTokenType this string is passed as the {@link #addAccount} |
| 2449 | * authTokenType parameter |
| 2450 | * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount} |
| 2451 | * requiredFeatures parameter |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 2452 | * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options |
Fred Quintana | d88324d | 2011-09-19 11:43:05 -0700 | [diff] [blame] | 2453 | * parameter |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 2454 | * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow. |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2455 | */ |
Carlos Valdivia | a3db8ac | 2015-07-10 13:04:43 -0700 | [diff] [blame] | 2456 | @Deprecated |
| 2457 | static public Intent newChooseAccountIntent( |
| 2458 | Account selectedAccount, |
Baligh Uddin | f2d248d | 2015-07-10 03:01:47 +0000 | [diff] [blame] | 2459 | ArrayList<Account> allowableAccounts, |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2460 | String[] allowableAccountTypes, |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 2461 | boolean alwaysPromptForAccount, |
| 2462 | String descriptionOverrideText, |
| 2463 | String addAccountAuthTokenType, |
| 2464 | String[] addAccountRequiredFeatures, |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2465 | Bundle addAccountOptions) { |
Carlos Valdivia | a3db8ac | 2015-07-10 13:04:43 -0700 | [diff] [blame] | 2466 | return newChooseAccountIntent( |
| 2467 | selectedAccount, |
| 2468 | allowableAccounts, |
| 2469 | allowableAccountTypes, |
| 2470 | descriptionOverrideText, |
| 2471 | addAccountAuthTokenType, |
| 2472 | addAccountRequiredFeatures, |
| 2473 | addAccountOptions); |
| 2474 | } |
| 2475 | |
| 2476 | /** |
| 2477 | * Returns an intent to an {@link Activity} that prompts the user to choose from a list of |
| 2478 | * accounts. |
| 2479 | * The caller will then typically start the activity by calling |
| 2480 | * <code>startActivityForResult(intent, ...);</code>. |
| 2481 | * <p> |
| 2482 | * On success the activity returns a Bundle with the account name and type specified using |
| 2483 | * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}. |
| 2484 | * <p> |
| 2485 | * The most common case is to call this with one account type, e.g.: |
| 2486 | * <p> |
| 2487 | * <pre> newChooseAccountIntent(null, null, new String[]{"com.google"}, null, null, null, |
| 2488 | * null);</pre> |
| 2489 | * @param selectedAccount if specified, indicates that the {@link Account} is the currently |
| 2490 | * selected one, according to the caller's definition of selected. |
| 2491 | * @param allowableAccounts an optional {@link List} of accounts that are allowed to be |
| 2492 | * shown. If not specified then this field will not limit the displayed accounts. |
| 2493 | * @param allowableAccountTypes an optional string array of account types. These are used |
| 2494 | * both to filter the shown accounts and to filter the list of account types that are shown |
| 2495 | * when adding an account. If not specified then this field will not limit the displayed |
| 2496 | * account types when adding an account. |
| 2497 | * @param descriptionOverrideText if non-null this string is used as the description in the |
| 2498 | * accounts chooser screen rather than the default |
| 2499 | * @param addAccountAuthTokenType this string is passed as the {@link #addAccount} |
| 2500 | * authTokenType parameter |
| 2501 | * @param addAccountRequiredFeatures this string array is passed as the {@link #addAccount} |
| 2502 | * requiredFeatures parameter |
| 2503 | * @param addAccountOptions This {@link Bundle} is passed as the {@link #addAccount} options |
| 2504 | * parameter |
| 2505 | * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow. |
| 2506 | */ |
| 2507 | static public Intent newChooseAccountIntent( |
| 2508 | Account selectedAccount, |
| 2509 | List<Account> allowableAccounts, |
| 2510 | String[] allowableAccountTypes, |
| 2511 | String descriptionOverrideText, |
| 2512 | String addAccountAuthTokenType, |
| 2513 | String[] addAccountRequiredFeatures, |
| 2514 | Bundle addAccountOptions) { |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2515 | Intent intent = new Intent(); |
Amith Yamasani | 12b8e13 | 2013-03-14 10:48:07 -0700 | [diff] [blame] | 2516 | ComponentName componentName = ComponentName.unflattenFromString( |
| 2517 | Resources.getSystem().getString(R.string.config_chooseTypeAndAccountActivity)); |
| 2518 | intent.setClassName(componentName.getPackageName(), |
| 2519 | componentName.getClassName()); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2520 | intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST, |
Craig Lafayette | 3c9c71d | 2015-07-14 10:48:46 -0400 | [diff] [blame] | 2521 | allowableAccounts == null ? null : new ArrayList<Account>(allowableAccounts)); |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 2522 | intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY, |
| 2523 | allowableAccountTypes); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2524 | intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE, |
| 2525 | addAccountOptions); |
| 2526 | intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_SELECTED_ACCOUNT, selectedAccount); |
Fred Quintana | b04fe4e | 2011-09-16 21:17:21 -0700 | [diff] [blame] | 2527 | intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_DESCRIPTION_TEXT_OVERRIDE, |
| 2528 | descriptionOverrideText); |
| 2529 | intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING, |
| 2530 | addAccountAuthTokenType); |
| 2531 | intent.putExtra( |
| 2532 | ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY, |
| 2533 | addAccountRequiredFeatures); |
Fred Quintana | 1121bb5 | 2011-09-14 23:19:35 -0700 | [diff] [blame] | 2534 | return intent; |
| 2535 | } |
| 2536 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2537 | private final HashMap<OnAccountsUpdateListener, Handler> mAccountsUpdatedListeners = |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2538 | Maps.newHashMap(); |
| 2539 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2540 | /** |
| 2541 | * BroadcastReceiver that listens for the LOGIN_ACCOUNTS_CHANGED_ACTION intent |
| 2542 | * so that it can read the updated list of accounts and send them to the listener |
| 2543 | * in mAccountsUpdatedListeners. |
| 2544 | */ |
| 2545 | private final BroadcastReceiver mAccountsChangedBroadcastReceiver = new BroadcastReceiver() { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2546 | @Override |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2547 | public void onReceive(final Context context, final Intent intent) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2548 | final Account[] accounts = getAccounts(); |
| 2549 | // send the result to the listeners |
| 2550 | synchronized (mAccountsUpdatedListeners) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2551 | for (Map.Entry<OnAccountsUpdateListener, Handler> entry : |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2552 | mAccountsUpdatedListeners.entrySet()) { |
| 2553 | postToHandler(entry.getValue(), entry.getKey(), accounts); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2554 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2555 | } |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2556 | } |
| 2557 | }; |
| 2558 | |
| 2559 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2560 | * Adds an {@link OnAccountsUpdateListener} to this instance of the |
| 2561 | * {@link AccountManager}. This listener will be notified whenever the |
| 2562 | * list of accounts on the device changes. |
| 2563 | * |
| 2564 | * <p>As long as this listener is present, the AccountManager instance |
| 2565 | * will not be garbage-collected, and neither will the {@link Context} |
| 2566 | * used to retrieve it, which may be a large Activity instance. To avoid |
| 2567 | * memory leaks, you must remove this listener before then. Normally |
| 2568 | * listeners are added in an Activity or Service's {@link Activity#onCreate} |
| 2569 | * and removed in {@link Activity#onDestroy}. |
| 2570 | * |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2571 | * <p>The listener will only be informed of accounts that would be returned |
| 2572 | * to the caller via {@link #getAccounts()}. Typically this means that to |
| 2573 | * get any accounts, the caller will need to be grated the GET_ACCOUNTS |
| 2574 | * permission. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2575 | * |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2576 | * <p>It is safe to call this method from the main thread. |
Ian Pedowitz | 845d14d | 2015-08-04 07:47:37 -0700 | [diff] [blame] | 2577 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2578 | * @param listener The listener to send notifications to |
| 2579 | * @param handler {@link Handler} identifying the thread to use |
| 2580 | * for notifications, null for the main thread |
| 2581 | * @param updateImmediately If true, the listener will be invoked |
| 2582 | * (on the handler thread) right away with the current account list |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2583 | * @throws IllegalArgumentException if listener is null |
| 2584 | * @throws IllegalStateException if listener was already added |
| 2585 | */ |
Tor Norbye | 80b530a | 2015-04-23 16:36:09 -0700 | [diff] [blame] | 2586 | @RequiresPermission(GET_ACCOUNTS) |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2587 | public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener, |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2588 | Handler handler, boolean updateImmediately) { |
| 2589 | if (listener == null) { |
| 2590 | throw new IllegalArgumentException("the listener is null"); |
| 2591 | } |
| 2592 | synchronized (mAccountsUpdatedListeners) { |
| 2593 | if (mAccountsUpdatedListeners.containsKey(listener)) { |
| 2594 | throw new IllegalStateException("this listener is already added"); |
| 2595 | } |
| 2596 | final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty(); |
| 2597 | |
| 2598 | mAccountsUpdatedListeners.put(listener, handler); |
| 2599 | |
| 2600 | if (wasEmpty) { |
| 2601 | // Register a broadcast receiver to monitor account changes |
| 2602 | IntentFilter intentFilter = new IntentFilter(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2603 | intentFilter.addAction(LOGIN_ACCOUNTS_CHANGED_ACTION); |
Costin Manolache | b643724 | 2009-09-10 16:14:12 -0700 | [diff] [blame] | 2604 | // To recover from disk-full. |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2605 | intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2606 | mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter); |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | if (updateImmediately) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2611 | postToHandler(handler, listener, getAccounts()); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2612 | } |
| 2613 | } |
| 2614 | |
| 2615 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 2616 | * Removes an {@link OnAccountsUpdateListener} previously registered with |
| 2617 | * {@link #addOnAccountsUpdatedListener}. The listener will no longer |
| 2618 | * receive notifications of account changes. |
| 2619 | * |
| 2620 | * <p>It is safe to call this method from the main thread. |
| 2621 | * |
| 2622 | * <p>No permission is required to call this method. |
| 2623 | * |
| 2624 | * @param listener The previously added listener to remove |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2625 | * @throws IllegalArgumentException if listener is null |
| 2626 | * @throws IllegalStateException if listener was not already added |
| 2627 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2628 | public void removeOnAccountsUpdatedListener(OnAccountsUpdateListener listener) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2629 | if (listener == null) throw new IllegalArgumentException("listener is null"); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2630 | synchronized (mAccountsUpdatedListeners) { |
Bryan Mawhinney | 5be61f5 | 2009-09-24 14:50:25 +0100 | [diff] [blame] | 2631 | if (!mAccountsUpdatedListeners.containsKey(listener)) { |
Costin Manolache | 88a211b | 2009-10-29 11:30:11 -0700 | [diff] [blame] | 2632 | Log.e(TAG, "Listener was not previously added"); |
| 2633 | return; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2634 | } |
Bryan Mawhinney | 5be61f5 | 2009-09-24 14:50:25 +0100 | [diff] [blame] | 2635 | mAccountsUpdatedListeners.remove(listener); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2636 | if (mAccountsUpdatedListeners.isEmpty()) { |
| 2637 | mContext.unregisterReceiver(mAccountsChangedBroadcastReceiver); |
| 2638 | } |
| 2639 | } |
| 2640 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 2641 | |
| 2642 | /** |
| 2643 | * Asks the user to authenticate with an account of a specified type. The |
| 2644 | * authenticator for this account type processes this request with the |
| 2645 | * appropriate user interface. If the user does elect to authenticate with a |
| 2646 | * new account, a bundle of session data for installing the account later is |
| 2647 | * returned with optional account password and account status token. |
| 2648 | * <p> |
| 2649 | * This method may be called from any thread, but the returned |
| 2650 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 2651 | * <p> |
| 2652 | * <p> |
| 2653 | * <b>NOTE:</b> The account will not be installed to the device by calling |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2654 | * this api alone. #finishSession should be called after this to install the |
| 2655 | * account on device. |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 2656 | * |
| 2657 | * @param accountType The type of account to add; must not be null |
| 2658 | * @param authTokenType The type of auth token (see {@link #getAuthToken}) |
| 2659 | * this account will need to be able to generate, null for none |
| 2660 | * @param requiredFeatures The features (see {@link #hasFeatures}) this |
| 2661 | * account must have, null for none |
| 2662 | * @param options Authenticator-specific options for the request, may be |
| 2663 | * null or empty |
| 2664 | * @param activity The {@link Activity} context to use for launching a new |
| 2665 | * authenticator-defined sub-Activity to prompt the user to |
| 2666 | * create an account; used only to call startActivity(); if null, |
| 2667 | * the prompt will not be launched directly, but the necessary |
| 2668 | * {@link Intent} will be returned to the caller instead |
| 2669 | * @param callback Callback to invoke when the request completes, null for |
| 2670 | * no callback |
| 2671 | * @param handler {@link Handler} identifying the callback thread, null for |
| 2672 | * the main thread |
| 2673 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
| 2674 | * these fields if activity was specified and user was authenticated |
| 2675 | * with an account: |
| 2676 | * <ul> |
| 2677 | * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for |
| 2678 | * adding the the to the device later. |
| 2679 | * <li>{@link #KEY_PASSWORD} - optional, the password or password |
| 2680 | * hash of the account. |
| 2681 | * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check |
| 2682 | * status of the account |
| 2683 | * </ul> |
| 2684 | * If no activity was specified, the returned Bundle contains only |
| 2685 | * {@link #KEY_INTENT} with the {@link Intent} needed to launch the |
| 2686 | * actual account creation process. If authenticator doesn't support |
| 2687 | * this method, the returned Bundle contains only |
| 2688 | * {@link #KEY_ACCOUNT_SESSION_BUNDLE} with encrypted |
| 2689 | * {@code options} needed to add account later. If an error |
| 2690 | * occurred, {@link AccountManagerFuture#getResult()} throws: |
| 2691 | * <ul> |
| 2692 | * <li>{@link AuthenticatorException} if no authenticator was |
| 2693 | * registered for this account type or the authenticator failed to |
| 2694 | * respond |
| 2695 | * <li>{@link OperationCanceledException} if the operation was |
| 2696 | * canceled for any reason, including the user canceling the |
| 2697 | * creation process or adding accounts (of this type) has been |
| 2698 | * disabled by policy |
| 2699 | * <li>{@link IOException} if the authenticator experienced an I/O |
| 2700 | * problem creating a new account, usually because of network |
| 2701 | * trouble |
| 2702 | * </ul> |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2703 | * @see #finishSession |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 2704 | * @hide |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 2705 | */ |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 2706 | @SystemApi |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 2707 | public AccountManagerFuture<Bundle> startAddAccountSession( |
| 2708 | final String accountType, |
| 2709 | final String authTokenType, |
| 2710 | final String[] requiredFeatures, |
| 2711 | final Bundle options, |
| 2712 | final Activity activity, |
| 2713 | AccountManagerCallback<Bundle> callback, |
| 2714 | Handler handler) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 2715 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 2716 | final Bundle optionsIn = new Bundle(); |
| 2717 | if (options != null) { |
| 2718 | optionsIn.putAll(options); |
| 2719 | } |
| 2720 | optionsIn.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName()); |
| 2721 | |
| 2722 | return new AmsTask(activity, handler, callback) { |
| 2723 | @Override |
| 2724 | public void doWork() throws RemoteException { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 2725 | mService.startAddAccountSession( |
| 2726 | mResponse, |
| 2727 | accountType, |
| 2728 | authTokenType, |
| 2729 | requiredFeatures, |
| 2730 | activity != null, |
| 2731 | optionsIn); |
| 2732 | } |
| 2733 | }.start(); |
| 2734 | } |
| 2735 | |
| 2736 | /** |
| 2737 | * Asks the user to enter a new password for an account but not updating the |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2738 | * saved credentials for the account until {@link #finishSession} is called. |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 2739 | * <p> |
| 2740 | * This method may be called from any thread, but the returned |
| 2741 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 2742 | * <p> |
| 2743 | * <b>NOTE:</b> The saved credentials for the account alone will not be |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2744 | * updated by calling this API alone. #finishSession should be called after |
| 2745 | * this to update local credentials |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 2746 | * |
| 2747 | * @param account The account to update credentials for |
| 2748 | * @param authTokenType The credentials entered must allow an auth token of |
| 2749 | * this type to be created (but no actual auth token is |
| 2750 | * returned); may be null |
| 2751 | * @param options Authenticator-specific options for the request; may be |
| 2752 | * null or empty |
| 2753 | * @param activity The {@link Activity} context to use for launching a new |
| 2754 | * authenticator-defined sub-Activity to prompt the user to enter |
| 2755 | * a password; used only to call startActivity(); if null, the |
| 2756 | * prompt will not be launched directly, but the necessary |
| 2757 | * {@link Intent} will be returned to the caller instead |
| 2758 | * @param callback Callback to invoke when the request completes, null for |
| 2759 | * no callback |
| 2760 | * @param handler {@link Handler} identifying the callback thread, null for |
| 2761 | * the main thread |
| 2762 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
| 2763 | * these fields if an activity was supplied and user was |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2764 | * successfully re-authenticated to the account: |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 2765 | * <ul> |
| 2766 | * <li>{@link #KEY_ACCOUNT_SESSION_BUNDLE} - encrypted Bundle for |
| 2767 | * updating the local credentials on device later. |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2768 | * <li>{@link #KEY_PASSWORD} - optional, the password or password |
| 2769 | * hash of the account |
| 2770 | * <li>{@link #KEY_ACCOUNT_STATUS_TOKEN} - optional, token to check |
| 2771 | * status of the account |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 2772 | * </ul> |
| 2773 | * If no activity was specified, the returned Bundle contains |
| 2774 | * {@link #KEY_INTENT} with the {@link Intent} needed to launch the |
| 2775 | * password prompt. If an error occurred, |
| 2776 | * {@link AccountManagerFuture#getResult()} throws: |
| 2777 | * <ul> |
| 2778 | * <li>{@link AuthenticatorException} if the authenticator failed to |
| 2779 | * respond |
| 2780 | * <li>{@link OperationCanceledException} if the operation was |
| 2781 | * canceled for any reason, including the user canceling the |
| 2782 | * password prompt |
| 2783 | * <li>{@link IOException} if the authenticator experienced an I/O |
| 2784 | * problem verifying the password, usually because of network |
| 2785 | * trouble |
| 2786 | * </ul> |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2787 | * @see #finishSession |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 2788 | * @hide |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 2789 | */ |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 2790 | @SystemApi |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 2791 | public AccountManagerFuture<Bundle> startUpdateCredentialsSession( |
| 2792 | final Account account, |
| 2793 | final String authTokenType, |
| 2794 | final Bundle options, |
| 2795 | final Activity activity, |
| 2796 | final AccountManagerCallback<Bundle> callback, |
| 2797 | final Handler handler) { |
| 2798 | if (account == null) { |
| 2799 | throw new IllegalArgumentException("account is null"); |
| 2800 | } |
| 2801 | return new AmsTask(activity, handler, callback) { |
| 2802 | @Override |
| 2803 | public void doWork() throws RemoteException { |
| 2804 | mService.startUpdateCredentialsSession( |
| 2805 | mResponse, |
| 2806 | account, |
| 2807 | authTokenType, |
| 2808 | activity != null, |
| 2809 | options); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 2810 | } |
| 2811 | }.start(); |
| 2812 | } |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2813 | |
| 2814 | /** |
| 2815 | * Finishes the session started by {@link #startAddAccountSession} or |
| 2816 | * {@link #startUpdateCredentialsSession}. This will either add the account |
| 2817 | * to AccountManager or update the local credentials stored. |
| 2818 | * <p> |
| 2819 | * This method may be called from any thread, but the returned |
| 2820 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 2821 | * |
| 2822 | * @param sessionBundle a {@link Bundle} created by {@link #startAddAccountSession} or |
| 2823 | * {@link #startUpdateCredentialsSession} |
| 2824 | * @param activity The {@link Activity} context to use for launching a new |
| 2825 | * authenticator-defined sub-Activity to prompt the user to |
| 2826 | * create an account or reauthenticate existing account; used |
| 2827 | * only to call startActivity(); if null, the prompt will not |
| 2828 | * be launched directly, but the necessary {@link Intent} will |
| 2829 | * be returned to the caller instead |
| 2830 | * @param callback Callback to invoke when the request completes, null for |
| 2831 | * no callback |
| 2832 | * @param handler {@link Handler} identifying the callback thread, null for |
| 2833 | * the main thread |
| 2834 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
| 2835 | * these fields if an activity was supplied and an account was added |
| 2836 | * to device or local credentials were updated:: |
| 2837 | * <ul> |
| 2838 | * <li>{@link #KEY_ACCOUNT_NAME} - the name of the account created |
| 2839 | * <li>{@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 2840 | * </ul> |
| 2841 | * If no activity was specified and additional information is needed |
| 2842 | * from user, the returned Bundle may contains only |
| 2843 | * {@link #KEY_INTENT} with the {@link Intent} needed to launch the |
| 2844 | * actual account creation process. If an error occurred, |
| 2845 | * {@link AccountManagerFuture#getResult()} throws: |
| 2846 | * <ul> |
| 2847 | * <li>{@link AuthenticatorException} if no authenticator was |
| 2848 | * registered for this account type or the authenticator failed to |
| 2849 | * respond |
| 2850 | * <li>{@link OperationCanceledException} if the operation was |
| 2851 | * canceled for any reason, including the user canceling the |
| 2852 | * creation process or adding accounts (of this type) has been |
| 2853 | * disabled by policy |
| 2854 | * <li>{@link IOException} if the authenticator experienced an I/O |
| 2855 | * problem creating a new account, usually because of network |
| 2856 | * trouble |
| 2857 | * </ul> |
| 2858 | * @see #startAddAccountSession and #startUpdateCredentialsSession |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 2859 | * @hide |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2860 | */ |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 2861 | @SystemApi |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2862 | public AccountManagerFuture<Bundle> finishSession( |
| 2863 | final Bundle sessionBundle, |
| 2864 | final Activity activity, |
| 2865 | AccountManagerCallback<Bundle> callback, |
| 2866 | Handler handler) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2867 | return finishSessionAsUser( |
| 2868 | sessionBundle, |
| 2869 | activity, |
| 2870 | Process.myUserHandle(), |
| 2871 | callback, |
| 2872 | handler); |
| 2873 | } |
| 2874 | |
| 2875 | /** |
| 2876 | * @see #finishSession |
| 2877 | * @hide |
| 2878 | */ |
| 2879 | @SystemApi |
| 2880 | public AccountManagerFuture<Bundle> finishSessionAsUser( |
| 2881 | final Bundle sessionBundle, |
| 2882 | final Activity activity, |
| 2883 | final UserHandle userHandle, |
| 2884 | AccountManagerCallback<Bundle> callback, |
| 2885 | Handler handler) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2886 | if (sessionBundle == null) { |
| 2887 | throw new IllegalArgumentException("sessionBundle is null"); |
| 2888 | } |
| 2889 | |
| 2890 | /* Add information required by add account flow */ |
| 2891 | final Bundle appInfo = new Bundle(); |
| 2892 | appInfo.putString(KEY_ANDROID_PACKAGE_NAME, mContext.getPackageName()); |
| 2893 | |
| 2894 | return new AmsTask(activity, handler, callback) { |
| 2895 | @Override |
| 2896 | public void doWork() throws RemoteException { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2897 | mService.finishSessionAsUser( |
| 2898 | mResponse, |
| 2899 | sessionBundle, |
| 2900 | activity != null, |
| 2901 | appInfo, |
| 2902 | userHandle.getIdentifier()); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 2903 | } |
| 2904 | }.start(); |
| 2905 | } |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 2906 | |
| 2907 | /** |
| 2908 | * Checks whether {@link #updateCredentials} or {@link #startUpdateCredentialsSession} should be |
| 2909 | * called with respect to the specified account. |
| 2910 | * <p> |
| 2911 | * This method may be called from any thread, but the returned {@link AccountManagerFuture} must |
| 2912 | * not be used on the main thread. |
| 2913 | * |
| 2914 | * @param account The {@link Account} to be checked whether {@link #updateCredentials} or |
| 2915 | * {@link #startUpdateCredentialsSession} should be called |
| 2916 | * @param statusToken a String of token to check account staus |
| 2917 | * @param callback Callback to invoke when the request completes, null for no callback |
| 2918 | * @param handler {@link Handler} identifying the callback thread, null for the main thread |
| 2919 | * @return An {@link AccountManagerFuture} which resolves to a Boolean, true if the credentials |
| 2920 | * of the account should be updated. |
| 2921 | * @hide |
| 2922 | */ |
| 2923 | @SystemApi |
| 2924 | public AccountManagerFuture<Boolean> isCredentialsUpdateSuggested( |
| 2925 | final Account account, |
| 2926 | final String statusToken, |
| 2927 | AccountManagerCallback<Boolean> callback, |
| 2928 | Handler handler) { |
| 2929 | if (account == null) { |
| 2930 | throw new IllegalArgumentException("account is null"); |
| 2931 | } |
| 2932 | |
| 2933 | if (TextUtils.isEmpty(statusToken)) { |
| 2934 | throw new IllegalArgumentException("status token is empty"); |
| 2935 | } |
| 2936 | |
| 2937 | return new Future2Task<Boolean>(handler, callback) { |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2938 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 2939 | public void doWork() throws RemoteException { |
| 2940 | mService.isCredentialsUpdateSuggested( |
| 2941 | mResponse, |
| 2942 | account, |
| 2943 | statusToken); |
| 2944 | } |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 2945 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 2946 | public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 2947 | if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) { |
| 2948 | throw new AuthenticatorException("no result in response"); |
| 2949 | } |
| 2950 | return bundle.getBoolean(KEY_BOOLEAN_RESULT); |
| 2951 | } |
| 2952 | }.start(); |
| 2953 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2954 | } |