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 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.content.Intent; |
| 21 | import android.content.Context; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 22 | import android.content.IntentFilter; |
| 23 | import android.content.BroadcastReceiver; |
Costin Manolache | b643724 | 2009-09-10 16:14:12 -0700 | [diff] [blame] | 24 | import android.database.SQLException; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.os.Handler; |
| 27 | import android.os.Looper; |
| 28 | import android.os.RemoteException; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 29 | import android.os.Parcelable; |
Fred Quintana | 751fdc0 | 2010-02-09 14:13:18 -0800 | [diff] [blame] | 30 | import android.os.Build; |
Costin Manolache | b643724 | 2009-09-10 16:14:12 -0700 | [diff] [blame] | 31 | import android.util.Log; |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 32 | import android.text.TextUtils; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 33 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 34 | import java.io.IOException; |
| 35 | import java.util.concurrent.Callable; |
| 36 | import java.util.concurrent.CancellationException; |
| 37 | import java.util.concurrent.ExecutionException; |
| 38 | import java.util.concurrent.FutureTask; |
| 39 | import java.util.concurrent.TimeoutException; |
| 40 | import java.util.concurrent.TimeUnit; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 41 | import java.util.HashMap; |
| 42 | import java.util.Map; |
| 43 | |
| 44 | import com.google.android.collect.Maps; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 47 | * This class provides access to a centralized registry of the user's |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 48 | * online accounts. The user enters credentials (username and password) once |
| 49 | * per account, granting applications access to online resources with |
| 50 | * "one-click" approval. |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 51 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 52 | * <p>Different online services have different ways of handling accounts and |
| 53 | * authentication, so the account manager uses pluggable <em>authenticator</em> |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 54 | * modules for different <em>account types</em>. Authenticators (which may be |
| 55 | * written by third parties) handle the actual details of validating account |
| 56 | * credentials and storing account information. For example, Google, Facebook, |
| 57 | * and Microsoft Exchange each have their own authenticator. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 58 | * |
| 59 | * <p>Many servers support some notion of an <em>authentication token</em>, |
| 60 | * which can be used to authenticate a request to the server without sending |
| 61 | * the user's actual password. (Auth tokens are normally created with a |
| 62 | * separate request which does include the user's credentials.) AccountManager |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 63 | * can generate auth tokens for applications, so the application doesn't need to |
| 64 | * handle passwords directly. Auth tokens are normally reusable and cached by |
| 65 | * AccountManager, but must be refreshed periodically. It's the responsibility |
| 66 | * of applications to <em>invalidate</em> auth tokens when they stop working so |
| 67 | * the AccountManager knows it needs to regenerate them. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 68 | * |
| 69 | * <p>Applications accessing a server normally go through these steps: |
| 70 | * |
| 71 | * <ul> |
| 72 | * <li>Get an instance of AccountManager using {@link #get(Context)}. |
| 73 | * |
| 74 | * <li>List the available accounts using {@link #getAccountsByType} or |
| 75 | * {@link #getAccountsByTypeAndFeatures}. Normally applications will only |
| 76 | * be interested in accounts with one particular <em>type</em>, which |
| 77 | * identifies the authenticator. Account <em>features</em> are used to |
| 78 | * identify particular account subtypes and capabilities. Both the account |
| 79 | * type and features are authenticator-specific strings, and must be known by |
| 80 | * the application in coordination with its preferred authenticators. |
| 81 | * |
| 82 | * <li>Select one or more of the available accounts, possibly by asking the |
| 83 | * user for their preference. If no suitable accounts are available, |
| 84 | * {@link #addAccount} may be called to prompt the user to create an |
| 85 | * account of the appropriate type. |
| 86 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 87 | * <li><b>Important:</b> If the application is using a previously remembered |
| 88 | * account selection, it must make sure the account is still in the list |
| 89 | * of accounts returned by {@link #getAccountsByType}. Requesting an auth token |
| 90 | * for an account no longer on the device results in an undefined failure. |
| 91 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 92 | * <li>Request an auth token for the selected account(s) using one of the |
| 93 | * {@link #getAuthToken} methods or related helpers. Refer to the description |
| 94 | * of each method for exact usage and error handling details. |
| 95 | * |
| 96 | * <li>Make the request using the auth token. The form of the auth token, |
| 97 | * the format of the request, and the protocol used are all specific to the |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 98 | * service you are accessing. The application may use whatever network and |
| 99 | * protocol libraries are useful. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 100 | * |
| 101 | * <li><b>Important:</b> If the request fails with an authentication error, |
| 102 | * it could be that a cached auth token is stale and no longer honored by |
| 103 | * the server. The application must call {@link #invalidateAuthToken} to remove |
| 104 | * the token from the cache, otherwise requests will continue failing! After |
| 105 | * invalidating the auth token, immediately go back to the "Request an auth |
| 106 | * token" step above. If the process fails the second time, then it can be |
| 107 | * treated as a "genuine" authentication failure and the user notified or other |
| 108 | * appropriate actions taken. |
| 109 | * </ul> |
| 110 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 111 | * <p>Some AccountManager methods may need to interact with the user to |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 112 | * prompt for credentials, present options, or ask the user to add an account. |
| 113 | * The caller may choose whether to allow AccountManager to directly launch the |
| 114 | * necessary user interface and wait for the user, or to return an Intent which |
| 115 | * the caller may use to launch the interface, or (in some cases) to install a |
| 116 | * notification which the user can select at any time to launch the interface. |
| 117 | * To have AccountManager launch the interface directly, the caller must supply |
| 118 | * the current foreground {@link Activity} context. |
| 119 | * |
| 120 | * <p>Many AccountManager methods take {@link AccountManagerCallback} and |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 121 | * {@link Handler} as parameters. These methods return immediately and |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 122 | * run asynchronously. If a callback is provided then |
| 123 | * {@link AccountManagerCallback#run} will be invoked on the Handler's |
| 124 | * thread when the request completes, successfully or not. |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 125 | * The result is retrieved by calling {@link AccountManagerFuture#getResult()} |
| 126 | * on the {@link AccountManagerFuture} returned by the method (and also passed |
| 127 | * to the callback). This method waits for the operation to complete (if |
| 128 | * necessary) and either returns the result or throws an exception if an error |
| 129 | * occurred during the operation. To make the request synchronously, call |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 130 | * {@link AccountManagerFuture#getResult()} immediately on receiving the |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 131 | * future from the method; no callback need be supplied. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 132 | * |
| 133 | * <p>Requests which may block, including |
| 134 | * {@link AccountManagerFuture#getResult()}, must never be called on |
| 135 | * the application's main event thread. These operations throw |
| 136 | * {@link IllegalStateException} if they are used on the main thread. |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 137 | */ |
| 138 | public class AccountManager { |
| 139 | private static final String TAG = "AccountManager"; |
| 140 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 141 | public static final int ERROR_CODE_REMOTE_EXCEPTION = 1; |
| 142 | public static final int ERROR_CODE_NETWORK_ERROR = 3; |
| 143 | public static final int ERROR_CODE_CANCELED = 4; |
| 144 | public static final int ERROR_CODE_INVALID_RESPONSE = 5; |
| 145 | public static final int ERROR_CODE_UNSUPPORTED_OPERATION = 6; |
| 146 | public static final int ERROR_CODE_BAD_ARGUMENTS = 7; |
| 147 | public static final int ERROR_CODE_BAD_REQUEST = 8; |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 148 | |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 149 | /** |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 150 | * Bundle key used for the {@link String} account name in results |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 151 | * from methods which return information about a particular account. |
| 152 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 153 | public static final String KEY_ACCOUNT_NAME = "authAccount"; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 154 | |
| 155 | /** |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 156 | * Bundle key used for the {@link String} account type in results |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 157 | * from methods which return information about a particular account. |
| 158 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 159 | public static final String KEY_ACCOUNT_TYPE = "accountType"; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 160 | |
| 161 | /** |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 162 | * Bundle key used for the auth token value in results |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 163 | * from {@link #getAuthToken} and friends. |
| 164 | */ |
| 165 | public static final String KEY_AUTHTOKEN = "authtoken"; |
| 166 | |
| 167 | /** |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 168 | * Bundle key used for an {@link Intent} in results from methods that |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 169 | * may require the caller to interact with the user. The Intent can |
| 170 | * be used to start the corresponding user interface activity. |
| 171 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 172 | public static final String KEY_INTENT = "intent"; |
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 to supply the password directly in options to |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 176 | * {@link #confirmCredentials}, rather than prompting the user with |
| 177 | * the standard password prompt. |
| 178 | */ |
| 179 | public static final String KEY_PASSWORD = "password"; |
| 180 | |
| 181 | public static final String KEY_ACCOUNTS = "accounts"; |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 182 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 183 | public static final String KEY_ACCOUNT_AUTHENTICATOR_RESPONSE = "accountAuthenticatorResponse"; |
| 184 | public static final String KEY_ACCOUNT_MANAGER_RESPONSE = "accountManagerResponse"; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 185 | public static final String KEY_AUTHENTICATOR_TYPES = "authenticator_types"; |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 186 | public static final String KEY_AUTH_FAILED_MESSAGE = "authFailedMessage"; |
| 187 | public static final String KEY_AUTH_TOKEN_LABEL = "authTokenLabelKey"; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 188 | public static final String KEY_BOOLEAN_RESULT = "booleanResult"; |
| 189 | public static final String KEY_ERROR_CODE = "errorCode"; |
| 190 | public static final String KEY_ERROR_MESSAGE = "errorMessage"; |
| 191 | public static final String KEY_USERDATA = "userdata"; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 192 | /** |
| 193 | * Authenticators using 'customTokens' option will also get the UID of the |
| 194 | * caller |
| 195 | */ |
| 196 | public static final String KEY_CALLER_UID = "callerUid"; |
| 197 | public static final String KEY_CALLER_PID = "callerPid"; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 198 | |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 199 | /** |
| 200 | * Boolean, if set and 'customTokens' the authenticator is responsible for |
| 201 | * notifications. |
| 202 | * @hide |
| 203 | */ |
| 204 | public static final String KEY_NOTIFY_ON_FAILURE = "notifyOnAuthFailure"; |
| 205 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 206 | public static final String ACTION_AUTHENTICATOR_INTENT = |
| 207 | "android.accounts.AccountAuthenticator"; |
| 208 | public static final String AUTHENTICATOR_META_DATA_NAME = |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 209 | "android.accounts.AccountAuthenticator"; |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 210 | public static final String AUTHENTICATOR_ATTRIBUTES_NAME = "account-authenticator"; |
| 211 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 212 | private final Context mContext; |
| 213 | private final IAccountManager mService; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 214 | private final Handler mMainHandler; |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 215 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 216 | /** |
| 217 | * Action sent as a broadcast Intent by the AccountsService |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 218 | * when accounts are added, accounts are removed, or an |
| 219 | * account's credentials (saved password, etc) are changed. |
| 220 | * |
| 221 | * @see #addOnAccountsUpdatedListener |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 222 | */ |
| 223 | public static final String LOGIN_ACCOUNTS_CHANGED_ACTION = |
| 224 | "android.accounts.LOGIN_ACCOUNTS_CHANGED"; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 225 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 226 | /** |
| 227 | * @hide |
| 228 | */ |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 229 | public AccountManager(Context context, IAccountManager service) { |
| 230 | mContext = context; |
| 231 | mService = service; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 232 | mMainHandler = new Handler(mContext.getMainLooper()); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 235 | /** |
| 236 | * @hide used for testing only |
| 237 | */ |
| 238 | public AccountManager(Context context, IAccountManager service, Handler handler) { |
| 239 | mContext = context; |
| 240 | mService = service; |
| 241 | mMainHandler = handler; |
| 242 | } |
| 243 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 244 | /** |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 245 | * @hide for internal use only |
| 246 | */ |
| 247 | public static Bundle sanitizeResult(Bundle result) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 248 | if (result != null) { |
| 249 | if (result.containsKey(KEY_AUTHTOKEN) |
| 250 | && !TextUtils.isEmpty(result.getString(KEY_AUTHTOKEN))) { |
| 251 | final Bundle newResult = new Bundle(result); |
| 252 | newResult.putString(KEY_AUTHTOKEN, "<omitted for logging purposes>"); |
| 253 | return newResult; |
| 254 | } |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 255 | } |
| 256 | return result; |
| 257 | } |
| 258 | |
| 259 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 260 | * Gets an AccountManager instance associated with a Context. |
| 261 | * The {@link Context} will be used as long as the AccountManager is |
| 262 | * active, so make sure to use a {@link Context} whose lifetime is |
| 263 | * commensurate with any listeners registered to |
| 264 | * {@link #addOnAccountsUpdatedListener} or similar methods. |
| 265 | * |
| 266 | * <p>It is safe to call this method from the main thread. |
| 267 | * |
| 268 | * <p>No permission is required to call this method. |
| 269 | * |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 270 | * @param context The {@link Context} to use when necessary |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 271 | * @return An {@link AccountManager} instance |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 272 | */ |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 273 | public static AccountManager get(Context context) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 274 | if (context == null) throw new IllegalArgumentException("context is null"); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 275 | return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE); |
| 276 | } |
| 277 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 278 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 279 | * Gets the saved password associated with the account. |
| 280 | * This is intended for authenticators and related code; applications |
| 281 | * should get an auth token instead. |
| 282 | * |
| 283 | * <p>It is safe to call this method from the main thread. |
| 284 | * |
| 285 | * <p>This method requires the caller to hold the permission |
| 286 | * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS} |
| 287 | * and to have the same UID as the account's authenticator. |
| 288 | * |
| 289 | * @param account The account to query for a password |
| 290 | * @return The account's password, null if none or if the account doesn't exist |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 291 | */ |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 292 | public String getPassword(final Account account) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 293 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 294 | try { |
| 295 | return mService.getPassword(account); |
| 296 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 297 | // will never happen |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 298 | throw new RuntimeException(e); |
| 299 | } |
| 300 | } |
| 301 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 302 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 303 | * Gets the user data named by "key" associated with the account. |
| 304 | * This is intended for authenticators and related code to store |
| 305 | * arbitrary metadata along with accounts. The meaning of the keys |
| 306 | * and values is up to the authenticator for the account. |
| 307 | * |
| 308 | * <p>It is safe to call this method from the main thread. |
| 309 | * |
| 310 | * <p>This method requires the caller to hold the permission |
| 311 | * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS} |
| 312 | * and to have the same UID as the account's authenticator. |
| 313 | * |
| 314 | * @param account The account to query for user data |
| 315 | * @return The user data, null if the account or key doesn't exist |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 316 | */ |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 317 | public String getUserData(final Account account, final String key) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 318 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 319 | if (key == null) throw new IllegalArgumentException("key is null"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 320 | try { |
| 321 | return mService.getUserData(account, key); |
| 322 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 323 | // will never happen |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 324 | throw new RuntimeException(e); |
| 325 | } |
| 326 | } |
| 327 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 328 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 329 | * Lists the currently registered authenticators. |
| 330 | * |
| 331 | * <p>It is safe to call this method from the main thread. |
| 332 | * |
| 333 | * <p>No permission is required to call this method. |
| 334 | * |
| 335 | * @return An array of {@link AuthenticatorDescription} for every |
| 336 | * authenticator known to the AccountManager service. Empty (never |
| 337 | * null) if no authenticators are known. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 338 | */ |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 339 | public AuthenticatorDescription[] getAuthenticatorTypes() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 340 | try { |
| 341 | return mService.getAuthenticatorTypes(); |
| 342 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 343 | // will never happen |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 344 | throw new RuntimeException(e); |
| 345 | } |
| 346 | } |
| 347 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 348 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 349 | * Lists all accounts of any type registered on the device. |
| 350 | * Equivalent to getAccountsByType(null). |
| 351 | * |
| 352 | * <p>It is safe to call this method from the main thread. |
| 353 | * |
| 354 | * <p>This method requires the caller to hold the permission |
| 355 | * {@link android.Manifest.permission#GET_ACCOUNTS}. |
| 356 | * |
| 357 | * @return An array of {@link Account}, one for each account. Empty |
| 358 | * (never null) if no accounts have been added. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 359 | */ |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 360 | public Account[] getAccounts() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 361 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 362 | return mService.getAccounts(null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 363 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 364 | // won't ever happen |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 365 | throw new RuntimeException(e); |
| 366 | } |
| 367 | } |
| 368 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 369 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 370 | * Lists all accounts of a particular type. The account type is a |
| 371 | * string token corresponding to the authenticator and useful domain |
| 372 | * of the account. For example, there are types corresponding to Google |
| 373 | * and Facebook. The exact string token to use will be published somewhere |
| 374 | * associated with the authenticator in question. |
| 375 | * |
| 376 | * <p>It is safe to call this method from the main thread. |
| 377 | * |
| 378 | * <p>This method requires the caller to hold the permission |
| 379 | * {@link android.Manifest.permission#GET_ACCOUNTS}. |
| 380 | * |
| 381 | * @param type The type of accounts to return, null to retrieve all accounts |
| 382 | * @return An array of {@link Account}, one per matching account. Empty |
| 383 | * (never null) if no accounts of the specified type have been added. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 384 | */ |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 385 | public Account[] getAccountsByType(String type) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 386 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 387 | return mService.getAccounts(type); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 388 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 389 | // won't ever happen |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 390 | throw new RuntimeException(e); |
| 391 | } |
| 392 | } |
| 393 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 394 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 395 | * Finds out whether a particular account has all the specified features. |
| 396 | * Account features are authenticator-specific string tokens identifying |
| 397 | * boolean account properties. For example, features are used to tell |
| 398 | * whether Google accounts have a particular service (such as Google |
| 399 | * Calendar or Google Talk) enabled. The feature names and their meanings |
| 400 | * are published somewhere associated with the authenticator in question. |
| 401 | * |
| 402 | * <p>This method may be called from any thread, but the returned |
| 403 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 404 | * |
| 405 | * <p>This method requires the caller to hold the permission |
| 406 | * {@link android.Manifest.permission#GET_ACCOUNTS}. |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 407 | * |
| 408 | * @param account The {@link Account} to test |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 409 | * @param features An array of the account features to check |
| 410 | * @param callback Callback to invoke when the request completes, |
| 411 | * null for no callback |
| 412 | * @param handler {@link Handler} identifying the callback thread, |
| 413 | * null for the main thread |
| 414 | * @return An {@link AccountManagerFuture} which resolves to a Boolean, |
| 415 | * true if the account exists and has all of the specified features. |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 416 | */ |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 417 | public AccountManagerFuture<Boolean> hasFeatures(final Account account, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 418 | final String[] features, |
| 419 | AccountManagerCallback<Boolean> callback, Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 420 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 421 | if (features == null) throw new IllegalArgumentException("features is null"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 422 | return new Future2Task<Boolean>(handler, callback) { |
| 423 | public void doWork() throws RemoteException { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 424 | mService.hasFeatures(mResponse, account, features); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 425 | } |
| 426 | public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 427 | if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) { |
| 428 | throw new AuthenticatorException("no result in response"); |
| 429 | } |
| 430 | return bundle.getBoolean(KEY_BOOLEAN_RESULT); |
| 431 | } |
| 432 | }.start(); |
| 433 | } |
| 434 | |
| 435 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 436 | * Lists all accounts of a type which have certain features. The account |
| 437 | * type identifies the authenticator (see {@link #getAccountsByType}). |
| 438 | * Account features are authenticator-specific string tokens identifying |
| 439 | * boolean account properties (see {@link #hasFeatures}). |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 440 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 441 | * <p>Unlike {@link #getAccountsByType}, this method calls the authenticator, |
| 442 | * which may contact the server or do other work to check account features, |
| 443 | * so the method returns an {@link AccountManagerFuture}. |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 444 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 445 | * <p>This method may be called from any thread, but the returned |
| 446 | * {@link AccountManagerFuture} must not be used on the main thread. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 447 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 448 | * <p>This method requires the caller to hold the permission |
| 449 | * {@link android.Manifest.permission#GET_ACCOUNTS}. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 450 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 451 | * @param type The type of accounts to return, must not be null |
| 452 | * @param features An array of the account features to require, |
| 453 | * may be null or empty |
| 454 | * @param callback Callback to invoke when the request completes, |
| 455 | * null for no callback |
| 456 | * @param handler {@link Handler} identifying the callback thread, |
| 457 | * null for the main thread |
| 458 | * @return An {@link AccountManagerFuture} which resolves to an array of |
| 459 | * {@link Account}, one per account of the specified type which |
| 460 | * matches the requested features. |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 461 | */ |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 462 | public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures( |
| 463 | final String type, final String[] features, |
| 464 | AccountManagerCallback<Account[]> callback, Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 465 | if (type == null) throw new IllegalArgumentException("type is null"); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 466 | return new Future2Task<Account[]>(handler, callback) { |
| 467 | public void doWork() throws RemoteException { |
| 468 | mService.getAccountsByFeatures(mResponse, type, features); |
| 469 | } |
| 470 | public Account[] bundleToResult(Bundle bundle) throws AuthenticatorException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 471 | if (!bundle.containsKey(KEY_ACCOUNTS)) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 472 | throw new AuthenticatorException("no result in response"); |
| 473 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 474 | final Parcelable[] parcelables = bundle.getParcelableArray(KEY_ACCOUNTS); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 475 | Account[] descs = new Account[parcelables.length]; |
| 476 | for (int i = 0; i < parcelables.length; i++) { |
| 477 | descs[i] = (Account) parcelables[i]; |
| 478 | } |
| 479 | return descs; |
| 480 | } |
| 481 | }.start(); |
| 482 | } |
| 483 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 484 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 485 | * Adds an account directly to the AccountManager. Normally used by sign-up |
| 486 | * wizards associated with authenticators, not directly by applications. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 487 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 488 | * <p>It is safe to call this method from the main thread. |
| 489 | * |
| 490 | * <p>This method requires the caller to hold the permission |
| 491 | * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS} |
| 492 | * and to have the same UID as the added account's authenticator. |
| 493 | * |
| 494 | * @param account The {@link Account} to add |
| 495 | * @param password The password to associate with the account, null for none |
| 496 | * @param userdata String values to use for the account's userdata, null for none |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 497 | * @return True if the account was successfully added, false if the account |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 498 | * already exists, the account is null, or another error occurs. |
| 499 | */ |
| 500 | public boolean addAccountExplicitly(Account account, String password, Bundle userdata) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 501 | if (account == null) throw new IllegalArgumentException("account is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 502 | try { |
| 503 | return mService.addAccount(account, password, userdata); |
| 504 | } catch (RemoteException e) { |
| 505 | // won't ever happen |
| 506 | throw new RuntimeException(e); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Removes an account from the AccountManager. Does nothing if the account |
| 512 | * does not exist. Does not delete the account from the server. |
| 513 | * The authenticator may have its own policies preventing account |
| 514 | * deletion, in which case the account will not be deleted. |
| 515 | * |
| 516 | * <p>This method may be called from any thread, but the returned |
| 517 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 518 | * |
| 519 | * <p>This method requires the caller to hold the permission |
| 520 | * {@link android.Manifest.permission#MANAGE_ACCOUNTS}. |
| 521 | * |
| 522 | * @param account The {@link Account} to remove |
| 523 | * @param callback Callback to invoke when the request completes, |
| 524 | * null for no callback |
| 525 | * @param handler {@link Handler} identifying the callback thread, |
| 526 | * null for the main thread |
| 527 | * @return An {@link AccountManagerFuture} which resolves to a Boolean, |
| 528 | * true if the account has been successfully removed, |
| 529 | * false if the authenticator forbids deleting this account. |
| 530 | */ |
| 531 | public AccountManagerFuture<Boolean> removeAccount(final Account account, |
| 532 | AccountManagerCallback<Boolean> callback, Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 533 | if (account == null) throw new IllegalArgumentException("account is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 534 | return new Future2Task<Boolean>(handler, callback) { |
| 535 | public void doWork() throws RemoteException { |
| 536 | mService.removeAccount(mResponse, account); |
| 537 | } |
| 538 | public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 539 | if (!bundle.containsKey(KEY_BOOLEAN_RESULT)) { |
| 540 | throw new AuthenticatorException("no result in response"); |
| 541 | } |
| 542 | return bundle.getBoolean(KEY_BOOLEAN_RESULT); |
| 543 | } |
| 544 | }.start(); |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Removes an auth token from the AccountManager's cache. Does nothing if |
| 549 | * the auth token is not currently in the cache. Applications must call this |
| 550 | * method when the auth token is found to have expired or otherwise become |
| 551 | * invalid for authenticating requests. The AccountManager does not validate |
| 552 | * or expire cached auth tokens otherwise. |
| 553 | * |
| 554 | * <p>It is safe to call this method from the main thread. |
| 555 | * |
| 556 | * <p>This method requires the caller to hold the permission |
Fred Quintana | b38eb14 | 2010-02-24 13:40:54 -0800 | [diff] [blame] | 557 | * {@link android.Manifest.permission#MANAGE_ACCOUNTS} or |
| 558 | * {@link android.Manifest.permission#USE_CREDENTIALS} |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 559 | * |
Fred Quintana | f35b68f | 2010-04-01 11:36:00 -0700 | [diff] [blame] | 560 | * @param accountType The account type of the auth token to invalidate, must not be null |
| 561 | * @param authToken The auth token to invalidate, may be null |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 562 | */ |
| 563 | public void invalidateAuthToken(final String accountType, final String authToken) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 564 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 565 | try { |
Fred Quintana | f35b68f | 2010-04-01 11:36:00 -0700 | [diff] [blame] | 566 | if (authToken != null) { |
| 567 | mService.invalidateAuthToken(accountType, authToken); |
| 568 | } |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 569 | } catch (RemoteException e) { |
| 570 | // won't ever happen |
| 571 | throw new RuntimeException(e); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Gets an auth token from the AccountManager's cache. If no auth |
| 577 | * token is cached for this account, null will be returned -- a new |
| 578 | * auth token will not be generated, and the server will not be contacted. |
| 579 | * Intended for use by the authenticator, not directly by applications. |
| 580 | * |
| 581 | * <p>It is safe to call this method from the main thread. |
| 582 | * |
| 583 | * <p>This method requires the caller to hold the permission |
| 584 | * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS} |
| 585 | * and to have the same UID as the account's authenticator. |
| 586 | * |
| 587 | * @param account The account to fetch an auth token for |
| 588 | * @param authTokenType The type of auth token to fetch, see {#getAuthToken} |
| 589 | * @return The cached auth token for this account and type, or null if |
| 590 | * no auth token is cached or the account does not exist. |
| 591 | */ |
| 592 | public String peekAuthToken(final Account account, final String authTokenType) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 593 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 594 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 595 | try { |
| 596 | return mService.peekAuthToken(account, authTokenType); |
| 597 | } catch (RemoteException e) { |
| 598 | // won't ever happen |
| 599 | throw new RuntimeException(e); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Sets or forgets a saved password. This modifies the local copy of the |
| 605 | * password used to automatically authenticate the user; it does |
| 606 | * not change the user's account password on the server. Intended for use |
| 607 | * by the authenticator, not directly by applications. |
| 608 | * |
| 609 | * <p>It is safe to call this method from the main thread. |
| 610 | * |
| 611 | * <p>This method requires the caller to hold the permission |
| 612 | * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS} |
| 613 | * and have the same UID as the account's authenticator. |
| 614 | * |
| 615 | * @param account The account to set a password for |
| 616 | * @param password The password to set, null to clear the password |
| 617 | */ |
| 618 | public void setPassword(final Account account, final String password) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 619 | if (account == null) throw new IllegalArgumentException("account is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 620 | try { |
| 621 | mService.setPassword(account, password); |
| 622 | } catch (RemoteException e) { |
| 623 | // won't ever happen |
| 624 | throw new RuntimeException(e); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Forgets a saved password. This erases the local copy of the password; |
| 630 | * it does not change the user's account password on the server. |
| 631 | * Has the same effect as setPassword(account, null) but requires fewer |
| 632 | * permissions, and may be used by applications or management interfaces |
| 633 | * to "sign out" from an account. |
| 634 | * |
| 635 | * <p>It is safe to call this method from the main thread. |
| 636 | * |
| 637 | * <p>This method requires the caller to hold the permission |
| 638 | * {@link android.Manifest.permission#MANAGE_ACCOUNTS} |
| 639 | * |
| 640 | * @param account The account whose password to clear |
| 641 | */ |
| 642 | public void clearPassword(final Account account) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 643 | if (account == null) throw new IllegalArgumentException("account is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 644 | try { |
| 645 | mService.clearPassword(account); |
| 646 | } catch (RemoteException e) { |
| 647 | // won't ever happen |
| 648 | throw new RuntimeException(e); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Sets one userdata key for an account. Intended by use for the |
| 654 | * authenticator to stash state for itself, not directly by applications. |
| 655 | * The meaning of the keys and values is up to the authenticator. |
| 656 | * |
| 657 | * <p>It is safe to call this method from the main thread. |
| 658 | * |
| 659 | * <p>This method requires the caller to hold the permission |
| 660 | * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS} |
| 661 | * and to have the same UID as the account's authenticator. |
| 662 | * |
| 663 | * @param account The account to set the userdata for |
| 664 | * @param key The userdata key to set. Must not be null |
| 665 | * @param value The value to set, null to clear this userdata key |
| 666 | */ |
| 667 | public void setUserData(final Account account, final String key, final String value) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 668 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 669 | if (key == null) throw new IllegalArgumentException("key is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 670 | try { |
| 671 | mService.setUserData(account, key, value); |
| 672 | } catch (RemoteException e) { |
| 673 | // won't ever happen |
| 674 | throw new RuntimeException(e); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Adds an auth token to the AccountManager cache for an account. |
| 680 | * If the account does not exist then this call has no effect. |
| 681 | * Replaces any previous auth token for this account and auth token type. |
| 682 | * Intended for use by the authenticator, not directly by applications. |
| 683 | * |
| 684 | * <p>It is safe to call this method from the main thread. |
| 685 | * |
| 686 | * <p>This method requires the caller to hold the permission |
| 687 | * {@link android.Manifest.permission#AUTHENTICATE_ACCOUNTS} |
| 688 | * and to have the same UID as the account's authenticator. |
| 689 | * |
| 690 | * @param account The account to set an auth token for |
| 691 | * @param authTokenType The type of the auth token, see {#getAuthToken} |
| 692 | * @param authToken The auth token to add to the cache |
| 693 | */ |
| 694 | public void setAuthToken(Account account, final String authTokenType, final String authToken) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 695 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 696 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 697 | try { |
| 698 | mService.setAuthToken(account, authTokenType, authToken); |
| 699 | } catch (RemoteException e) { |
| 700 | // won't ever happen |
| 701 | throw new RuntimeException(e); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * This convenience helper synchronously gets an auth token with |
| 707 | * {@link #getAuthToken(Account, String, boolean, AccountManagerCallback, Handler)}. |
| 708 | * |
| 709 | * <p>This method may block while a network request completes, and must |
| 710 | * never be made from the main thread. |
| 711 | * |
| 712 | * <p>This method requires the caller to hold the permission |
| 713 | * {@link android.Manifest.permission#USE_CREDENTIALS}. |
| 714 | * |
| 715 | * @param account The account to fetch an auth token for |
| 716 | * @param authTokenType The auth token type, see {#link getAuthToken} |
| 717 | * @param notifyAuthFailure If true, display a notification and return null |
| 718 | * if authentication fails; if false, prompt and wait for the user to |
| 719 | * re-enter correct credentials before returning |
| 720 | * @return An auth token of the specified type for this account, or null |
| 721 | * if authentication fails or none can be fetched. |
| 722 | * @throws AuthenticatorException if the authenticator failed to respond |
| 723 | * @throws OperationCanceledException if the request was canceled for any |
| 724 | * reason, including the user canceling a credential request |
| 725 | * @throws java.io.IOException if the authenticator experienced an I/O problem |
| 726 | * creating a new auth token, usually because of network trouble |
| 727 | */ |
| 728 | public String blockingGetAuthToken(Account account, String authTokenType, |
| 729 | boolean notifyAuthFailure) |
| 730 | throws OperationCanceledException, IOException, AuthenticatorException { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 731 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 732 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 733 | Bundle bundle = getAuthToken(account, authTokenType, notifyAuthFailure, null /* callback */, |
| 734 | null /* handler */).getResult(); |
Fred Quintana | 96580e0 | 2010-03-04 13:42:42 -0800 | [diff] [blame] | 735 | if (bundle == null) { |
| 736 | // This should never happen, but it does, occasionally. If it does return null to |
| 737 | // signify that we were not able to get the authtoken. |
| 738 | // TODO: remove this when the bug is found that sometimes causes a null bundle to be |
| 739 | // returned |
| 740 | Log.e(TAG, "blockingGetAuthToken: null was returned from getResult() for " |
| 741 | + account + ", authTokenType " + authTokenType); |
| 742 | return null; |
| 743 | } |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 744 | return bundle.getString(KEY_AUTHTOKEN); |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Gets an auth token of the specified type for a particular account, |
| 749 | * prompting the user for credentials if necessary. This method is |
| 750 | * intended for applications running in the foreground where it makes |
| 751 | * sense to ask the user directly for a password. |
| 752 | * |
| 753 | * <p>If a previously generated auth token is cached for this account and |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 754 | * type, then it is returned. Otherwise, if a saved password is |
| 755 | * available, it is sent to the server to generate a new auth token. |
| 756 | * Otherwise, the user is prompted to enter a password. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 757 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 758 | * <p>Some authenticators have auth token <em>types</em>, whose value |
| 759 | * is authenticator-dependent. Some services use different token types to |
| 760 | * access different functionality -- for example, Google uses different auth |
| 761 | * tokens to access Gmail and Google Calendar for the same account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 762 | * |
| 763 | * <p>This method may be called from any thread, but the returned |
| 764 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 765 | * |
| 766 | * <p>This method requires the caller to hold the permission |
| 767 | * {@link android.Manifest.permission#USE_CREDENTIALS}. |
| 768 | * |
| 769 | * @param account The account to fetch an auth token for |
| 770 | * @param authTokenType The auth token type, an authenticator-dependent |
| 771 | * string token, must not be null |
| 772 | * @param options Authenticator-specific options for the request, |
| 773 | * may be null or empty |
| 774 | * @param activity The {@link Activity} context to use for launching a new |
| 775 | * authenticator-defined sub-Activity to prompt the user for a password |
| 776 | * if necessary; used only to call startActivity(); must not be null. |
| 777 | * @param callback Callback to invoke when the request completes, |
| 778 | * null for no callback |
| 779 | * @param handler {@link Handler} identifying the callback thread, |
| 780 | * null for the main thread |
| 781 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
| 782 | * at least the following fields: |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 783 | * <ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 784 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied |
| 785 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 786 | * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 787 | * </ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 788 | * |
| 789 | * (Other authenticator-specific values may be returned.) If an auth token |
| 790 | * could not be fetched, {@link AccountManagerFuture#getResult()} throws: |
| 791 | * <ul> |
| 792 | * <li> {@link AuthenticatorException} if the authenticator failed to respond |
| 793 | * <li> {@link OperationCanceledException} if the operation is canceled for |
| 794 | * any reason, incluidng the user canceling a credential request |
| 795 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 796 | * creating a new auth token, usually because of network trouble |
| 797 | * </ul> |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 798 | * If the account is no longer present on the device, the return value is |
| 799 | * authenticator-dependent. The caller should verify the validity of the |
| 800 | * account before requesting an auth token. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 801 | */ |
| 802 | public AccountManagerFuture<Bundle> getAuthToken( |
| 803 | final Account account, final String authTokenType, final Bundle options, |
| 804 | final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 805 | if (account == null) throw new IllegalArgumentException("account is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 806 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 807 | return new AmsTask(activity, handler, callback) { |
| 808 | public void doWork() throws RemoteException { |
| 809 | mService.getAuthToken(mResponse, account, authTokenType, |
| 810 | false /* notifyOnAuthFailure */, true /* expectActivityLaunch */, |
| 811 | options); |
| 812 | } |
| 813 | }.start(); |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Gets an auth token of the specified type for a particular account, |
| 818 | * optionally raising a notification if the user must enter credentials. |
| 819 | * This method is intended for background tasks and services where the |
| 820 | * user should not be immediately interrupted with a password prompt. |
| 821 | * |
| 822 | * <p>If a previously generated auth token is cached for this account and |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 823 | * type, then it is returned. Otherwise, if a saved password is |
| 824 | * available, it is sent to the server to generate a new auth token. |
| 825 | * Otherwise, an {@link Intent} is returned which, when started, will |
| 826 | * prompt the user for a password. If the notifyAuthFailure parameter is |
| 827 | * set, a status bar notification is also created with the same Intent, |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 828 | * alerting the user that they need to enter a password at some point. |
| 829 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 830 | * <p>In that case, you may need to wait until the user responds, which |
| 831 | * could take hours or days or forever. When the user does respond and |
| 832 | * supply a new password, the account manager will broadcast the |
| 833 | * {@link #LOGIN_ACCOUNTS_CHANGED_ACTION} Intent, which applications can |
| 834 | * use to try again. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 835 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 836 | * <p>If notifyAuthFailure is not set, it is the application's |
| 837 | * responsibility to launch the returned Intent at some point. |
| 838 | * Either way, the result from this call will not wait for user action. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 839 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 840 | * <p>Some authenticators have auth token <em>types</em>, whose value |
| 841 | * is authenticator-dependent. Some services use different token types to |
| 842 | * access different functionality -- for example, Google uses different auth |
| 843 | * tokens to access Gmail and Google Calendar for the same account. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 844 | * |
| 845 | * <p>This method may be called from any thread, but the returned |
| 846 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 847 | * |
| 848 | * <p>This method requires the caller to hold the permission |
| 849 | * {@link android.Manifest.permission#USE_CREDENTIALS}. |
| 850 | * |
| 851 | * @param account The account to fetch an auth token for |
| 852 | * @param authTokenType The auth token type, an authenticator-dependent |
| 853 | * string token, must not be null |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 854 | * @param notifyAuthFailure True to add a notification to prompt the |
| 855 | * user for a password if necessary, false to leave that to the caller |
| 856 | * @param callback Callback to invoke when the request completes, |
| 857 | * null for no callback |
| 858 | * @param handler {@link Handler} identifying the callback thread, |
| 859 | * null for the main thread |
| 860 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
| 861 | * at least the following fields on success: |
| 862 | * <ul> |
| 863 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account you supplied |
| 864 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 865 | * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted |
| 866 | * </ul> |
| 867 | * |
| 868 | * (Other authenticator-specific values may be returned.) If the user |
| 869 | * must enter credentials, the returned Bundle contains only |
| 870 | * {@link #KEY_INTENT} with the {@link Intent} needed to launch a prompt. |
| 871 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 872 | * If an error occurred, {@link AccountManagerFuture#getResult()} throws: |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 873 | * <ul> |
| 874 | * <li> {@link AuthenticatorException} if the authenticator failed to respond |
| 875 | * <li> {@link OperationCanceledException} if the operation is canceled for |
| 876 | * any reason, incluidng the user canceling a credential request |
| 877 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 878 | * creating a new auth token, usually because of network trouble |
| 879 | * </ul> |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 880 | * If the account is no longer present on the device, the return value is |
| 881 | * authenticator-dependent. The caller should verify the validity of the |
| 882 | * account before requesting an auth token. |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 883 | */ |
| 884 | public AccountManagerFuture<Bundle> getAuthToken( |
| 885 | final Account account, final String authTokenType, final boolean notifyAuthFailure, |
| 886 | AccountManagerCallback<Bundle> callback, Handler handler) { |
| 887 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 888 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 889 | return new AmsTask(null, handler, callback) { |
| 890 | public void doWork() throws RemoteException { |
| 891 | mService.getAuthToken(mResponse, account, authTokenType, |
| 892 | notifyAuthFailure, false /* expectActivityLaunch */, null /* options */); |
| 893 | } |
| 894 | }.start(); |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * Asks the user to add an account of a specified type. The authenticator |
| 899 | * for this account type processes this request with the appropriate user |
| 900 | * interface. If the user does elect to create a new account, the account |
| 901 | * name is returned. |
| 902 | * |
| 903 | * <p>This method may be called from any thread, but the returned |
| 904 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 905 | * |
| 906 | * <p>This method requires the caller to hold the permission |
| 907 | * {@link android.Manifest.permission#MANAGE_ACCOUNTS}. |
| 908 | * |
| 909 | * @param accountType The type of account to add; must not be null |
| 910 | * @param authTokenType The type of auth token (see {@link #getAuthToken}) |
| 911 | * this account will need to be able to generate, null for none |
| 912 | * @param requiredFeatures The features (see {@link #hasFeatures}) this |
| 913 | * account must have, null for none |
| 914 | * @param addAccountOptions Authenticator-specific options for the request, |
| 915 | * may be null or empty |
| 916 | * @param activity The {@link Activity} context to use for launching a new |
| 917 | * authenticator-defined sub-Activity to prompt the user to create an |
| 918 | * account; used only to call startActivity(); if null, the prompt |
| 919 | * will not be launched directly, but the necessary {@link Intent} |
| 920 | * will be returned to the caller instead |
| 921 | * @param callback Callback to invoke when the request completes, |
| 922 | * null for no callback |
| 923 | * @param handler {@link Handler} identifying the callback thread, |
| 924 | * null for the main thread |
Doug Zongker | ff592dc | 2010-02-23 12:26:33 -0800 | [diff] [blame] | 925 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 926 | * these fields if activity was specified and an account was created: |
| 927 | * <ul> |
| 928 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created |
| 929 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 930 | * </ul> |
| 931 | * |
| 932 | * If no activity was specified, the returned Bundle contains only |
| 933 | * {@link #KEY_INTENT} with the {@link Intent} needed to launch the |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 934 | * actual account creation process. If an error occurred, |
| 935 | * {@link AccountManagerFuture#getResult()} throws: |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 936 | * <ul> |
| 937 | * <li> {@link AuthenticatorException} if no authenticator was registered for |
| 938 | * this account type or the authenticator failed to respond |
| 939 | * <li> {@link OperationCanceledException} if the operation was canceled for |
| 940 | * any reason, including the user canceling the creation process |
| 941 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 942 | * creating a new account, usually because of network trouble |
| 943 | * </ul> |
| 944 | */ |
| 945 | public AccountManagerFuture<Bundle> addAccount(final String accountType, |
| 946 | final String authTokenType, final String[] requiredFeatures, |
| 947 | final Bundle addAccountOptions, |
| 948 | final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 949 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 950 | return new AmsTask(activity, handler, callback) { |
| 951 | public void doWork() throws RemoteException { |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 952 | mService.addAcount(mResponse, accountType, authTokenType, |
| 953 | requiredFeatures, activity != null, addAccountOptions); |
| 954 | } |
| 955 | }.start(); |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * Confirms that the user knows the password for an account to make extra |
| 960 | * sure they are the owner of the account. The user-entered password can |
| 961 | * be supplied directly, otherwise the authenticator for this account type |
| 962 | * prompts the user with the appropriate interface. This method is |
| 963 | * intended for applications which want extra assurance; for example, the |
| 964 | * phone lock screen uses this to let the user unlock the phone with an |
| 965 | * account password if they forget the lock pattern. |
| 966 | * |
| 967 | * <p>If the user-entered password matches a saved password for this |
| 968 | * account, the request is considered valid; otherwise the authenticator |
| 969 | * verifies the password (usually by contacting the server). |
| 970 | * |
| 971 | * <p>This method may be called from any thread, but the returned |
| 972 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 973 | * |
| 974 | * <p>This method requires the caller to hold the permission |
| 975 | * {@link android.Manifest.permission#MANAGE_ACCOUNTS}. |
| 976 | * |
| 977 | * @param account The account to confirm password knowledge for |
| 978 | * @param options Authenticator-specific options for the request; |
| 979 | * if the {@link #KEY_PASSWORD} string field is present, the |
| 980 | * authenticator may use it directly rather than prompting the user; |
| 981 | * may be null or empty |
| 982 | * @param activity The {@link Activity} context to use for launching a new |
| 983 | * authenticator-defined sub-Activity to prompt the user to enter a |
| 984 | * password; used only to call startActivity(); if null, the prompt |
| 985 | * will not be launched directly, but the necessary {@link Intent} |
| 986 | * will be returned to the caller instead |
| 987 | * @param callback Callback to invoke when the request completes, |
| 988 | * null for no callback |
| 989 | * @param handler {@link Handler} identifying the callback thread, |
| 990 | * null for the main thread |
| 991 | * @return An {@link AccountManagerFuture} which resolves to a Bundle |
| 992 | * with these fields if activity or password was supplied and |
| 993 | * the account was successfully verified: |
| 994 | * <ul> |
| 995 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created |
| 996 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 997 | * <li> {@link #KEY_BOOLEAN_RESULT} - true to indicate success |
| 998 | * </ul> |
| 999 | * |
| 1000 | * If no activity or password was specified, the returned Bundle contains |
| 1001 | * only {@link #KEY_INTENT} with the {@link Intent} needed to launch the |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1002 | * password prompt. If an error occurred, |
| 1003 | * {@link AccountManagerFuture#getResult()} throws: |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1004 | * <ul> |
| 1005 | * <li> {@link AuthenticatorException} if the authenticator failed to respond |
| 1006 | * <li> {@link OperationCanceledException} if the operation was canceled for |
| 1007 | * any reason, including the user canceling the password prompt |
| 1008 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 1009 | * verifying the password, usually because of network trouble |
| 1010 | * </ul> |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1011 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1012 | public AccountManagerFuture<Bundle> confirmCredentials(final Account account, |
| 1013 | final Bundle options, |
| 1014 | final Activity activity, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1015 | final AccountManagerCallback<Bundle> callback, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1016 | final Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1017 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1018 | return new AmsTask(activity, handler, callback) { |
| 1019 | public void doWork() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1020 | mService.confirmCredentials(mResponse, account, options, activity != null); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1021 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1022 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1025 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1026 | * Asks the user to enter a new password for an account, updating the |
| 1027 | * saved credentials for the account. Normally this happens automatically |
| 1028 | * when the server rejects credentials during an auth token fetch, but this |
| 1029 | * can be invoked directly to ensure we have the correct credentials stored. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1030 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1031 | * <p>This method may be called from any thread, but the returned |
| 1032 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 1033 | * |
| 1034 | * <p>This method requires the caller to hold the permission |
| 1035 | * {@link android.Manifest.permission#MANAGE_ACCOUNTS}. |
| 1036 | * |
| 1037 | * @param account The account to update credentials for |
| 1038 | * @param authTokenType The credentials entered must allow an auth token |
| 1039 | * of this type to be created (but no actual auth token is returned); |
| 1040 | * may be null |
| 1041 | * @param options Authenticator-specific options for the request; |
| 1042 | * may be null or empty |
| 1043 | * @param activity The {@link Activity} context to use for launching a new |
| 1044 | * authenticator-defined sub-Activity to prompt the user to enter a |
| 1045 | * password; used only to call startActivity(); if null, the prompt |
| 1046 | * will not be launched directly, but the necessary {@link Intent} |
| 1047 | * will be returned to the caller instead |
| 1048 | * @param callback Callback to invoke when the request completes, |
| 1049 | * null for no callback |
| 1050 | * @param handler {@link Handler} identifying the callback thread, |
| 1051 | * null for the main thread |
| 1052 | * @return An {@link AccountManagerFuture} which resolves to a Bundle |
| 1053 | * with these fields if an activity was supplied and the account |
| 1054 | * credentials were successfully updated: |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1055 | * <ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1056 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account created |
| 1057 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1058 | * </ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1059 | * |
| 1060 | * If no activity was specified, the returned Bundle contains only |
| 1061 | * {@link #KEY_INTENT} with the {@link Intent} needed to launch the |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1062 | * password prompt. If an error occurred, |
| 1063 | * {@link AccountManagerFuture#getResult()} throws: |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1064 | * <ul> |
| 1065 | * <li> {@link AuthenticatorException} if the authenticator failed to respond |
| 1066 | * <li> {@link OperationCanceledException} if the operation was canceled for |
| 1067 | * any reason, including the user canceling the password prompt |
| 1068 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 1069 | * verifying the password, usually because of network trouble |
| 1070 | * </ul> |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1071 | */ |
| 1072 | public AccountManagerFuture<Bundle> updateCredentials(final Account account, |
| 1073 | final String authTokenType, |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1074 | final Bundle options, final Activity activity, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1075 | final AccountManagerCallback<Bundle> callback, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1076 | final Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1077 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1078 | return new AmsTask(activity, handler, callback) { |
| 1079 | public void doWork() throws RemoteException { |
| 1080 | mService.updateCredentials(mResponse, account, authTokenType, activity != null, |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1081 | options); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1082 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1083 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1086 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1087 | * Offers the user an opportunity to change an authenticator's settings. |
| 1088 | * These properties are for the authenticator in general, not a particular |
| 1089 | * account. Not all authenticators support this method. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1090 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1091 | * <p>This method may be called from any thread, but the returned |
| 1092 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 1093 | * |
| 1094 | * <p>This method requires the caller to hold the permission |
| 1095 | * {@link android.Manifest.permission#MANAGE_ACCOUNTS}. |
| 1096 | * |
| 1097 | * @param accountType The account type associated with the authenticator |
| 1098 | * to adjust |
| 1099 | * @param activity The {@link Activity} context to use for launching a new |
| 1100 | * authenticator-defined sub-Activity to adjust authenticator settings; |
| 1101 | * used only to call startActivity(); if null, the settings dialog will |
| 1102 | * not be launched directly, but the necessary {@link Intent} will be |
| 1103 | * returned to the caller instead |
| 1104 | * @param callback Callback to invoke when the request completes, |
| 1105 | * null for no callback |
| 1106 | * @param handler {@link Handler} identifying the callback thread, |
| 1107 | * null for the main thread |
| 1108 | * @return An {@link AccountManagerFuture} which resolves to a Bundle |
| 1109 | * which is empty if properties were edited successfully, or |
| 1110 | * if no activity was specified, contains only {@link #KEY_INTENT} |
| 1111 | * needed to launch the authenticator's settings dialog. |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1112 | * If an error occurred, {@link AccountManagerFuture#getResult()} |
| 1113 | * throws: |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1114 | * <ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1115 | * <li> {@link AuthenticatorException} if no authenticator was registered for |
| 1116 | * this account type or the authenticator failed to respond |
| 1117 | * <li> {@link OperationCanceledException} if the operation was canceled for |
| 1118 | * any reason, including the user canceling the settings dialog |
| 1119 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 1120 | * updating settings, usually because of network trouble |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1121 | * </ul> |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1122 | */ |
| 1123 | public AccountManagerFuture<Bundle> editProperties(final String accountType, |
| 1124 | final Activity activity, final AccountManagerCallback<Bundle> callback, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1125 | final Handler handler) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1126 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1127 | return new AmsTask(activity, handler, callback) { |
| 1128 | public void doWork() throws RemoteException { |
| 1129 | mService.editProperties(mResponse, accountType, activity != null); |
| 1130 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1131 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | private void ensureNotOnMainThread() { |
| 1135 | final Looper looper = Looper.myLooper(); |
| 1136 | if (looper != null && looper == mContext.getMainLooper()) { |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 1137 | final IllegalStateException exception = new IllegalStateException( |
| 1138 | "calling this from your main thread can lead to deadlock"); |
| 1139 | Log.e(TAG, "calling this from your main thread can lead to deadlock and/or ANRs", |
| 1140 | exception); |
Fred Quintana | 751fdc0 | 2010-02-09 14:13:18 -0800 | [diff] [blame] | 1141 | if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.FROYO) { |
| 1142 | throw exception; |
| 1143 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1147 | private void postToHandler(Handler handler, final AccountManagerCallback<Bundle> callback, |
| 1148 | final AccountManagerFuture<Bundle> future) { |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1149 | handler = handler == null ? mMainHandler : handler; |
| 1150 | handler.post(new Runnable() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1151 | public void run() { |
| 1152 | callback.run(future); |
| 1153 | } |
| 1154 | }); |
| 1155 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1156 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1157 | private void postToHandler(Handler handler, final OnAccountsUpdateListener listener, |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1158 | final Account[] accounts) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1159 | final Account[] accountsCopy = new Account[accounts.length]; |
| 1160 | // send a copy to make sure that one doesn't |
| 1161 | // change what another sees |
| 1162 | System.arraycopy(accounts, 0, accountsCopy, 0, accountsCopy.length); |
| 1163 | handler = (handler == null) ? mMainHandler : handler; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1164 | handler.post(new Runnable() { |
| 1165 | public void run() { |
Costin Manolache | b643724 | 2009-09-10 16:14:12 -0700 | [diff] [blame] | 1166 | try { |
| 1167 | listener.onAccountsUpdated(accountsCopy); |
| 1168 | } catch (SQLException e) { |
| 1169 | // Better luck next time. If the problem was disk-full, |
| 1170 | // the STORAGE_OK intent will re-trigger the update. |
| 1171 | Log.e(TAG, "Can't update accounts", e); |
| 1172 | } |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1173 | } |
| 1174 | }); |
| 1175 | } |
| 1176 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1177 | private abstract class AmsTask extends FutureTask<Bundle> implements AccountManagerFuture<Bundle> { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1178 | final IAccountManagerResponse mResponse; |
| 1179 | final Handler mHandler; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1180 | final AccountManagerCallback<Bundle> mCallback; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1181 | final Activity mActivity; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1182 | public AmsTask(Activity activity, Handler handler, AccountManagerCallback<Bundle> callback) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1183 | super(new Callable<Bundle>() { |
| 1184 | public Bundle call() throws Exception { |
| 1185 | throw new IllegalStateException("this should never be called"); |
| 1186 | } |
| 1187 | }); |
| 1188 | |
| 1189 | mHandler = handler; |
| 1190 | mCallback = callback; |
| 1191 | mActivity = activity; |
| 1192 | mResponse = new Response(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1195 | public final AccountManagerFuture<Bundle> start() { |
| 1196 | try { |
| 1197 | doWork(); |
| 1198 | } catch (RemoteException e) { |
| 1199 | setException(e); |
| 1200 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1201 | return this; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1202 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1203 | |
Fred Quintana | 96580e0 | 2010-03-04 13:42:42 -0800 | [diff] [blame] | 1204 | protected void set(Bundle bundle) { |
| 1205 | // TODO: somehow a null is being set as the result of the Future. Log this |
| 1206 | // case to help debug where this is occurring. When this bug is fixed this |
| 1207 | // condition statement should be removed. |
| 1208 | if (bundle == null) { |
| 1209 | Log.e(TAG, "the bundle must not be null", new Exception()); |
| 1210 | } |
| 1211 | super.set(bundle); |
| 1212 | } |
| 1213 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1214 | public abstract void doWork() throws RemoteException; |
| 1215 | |
| 1216 | private Bundle internalGetResult(Long timeout, TimeUnit unit) |
| 1217 | throws OperationCanceledException, IOException, AuthenticatorException { |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 1218 | if (!isDone()) { |
| 1219 | ensureNotOnMainThread(); |
| 1220 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1221 | try { |
| 1222 | if (timeout == null) { |
| 1223 | return get(); |
| 1224 | } else { |
| 1225 | return get(timeout, unit); |
| 1226 | } |
| 1227 | } catch (CancellationException e) { |
| 1228 | throw new OperationCanceledException(); |
| 1229 | } catch (TimeoutException e) { |
| 1230 | // fall through and cancel |
| 1231 | } catch (InterruptedException e) { |
| 1232 | // fall through and cancel |
| 1233 | } catch (ExecutionException e) { |
| 1234 | final Throwable cause = e.getCause(); |
| 1235 | if (cause instanceof IOException) { |
| 1236 | throw (IOException) cause; |
| 1237 | } else if (cause instanceof UnsupportedOperationException) { |
| 1238 | throw new AuthenticatorException(cause); |
| 1239 | } else if (cause instanceof AuthenticatorException) { |
| 1240 | throw (AuthenticatorException) cause; |
| 1241 | } else if (cause instanceof RuntimeException) { |
| 1242 | throw (RuntimeException) cause; |
| 1243 | } else if (cause instanceof Error) { |
| 1244 | throw (Error) cause; |
| 1245 | } else { |
| 1246 | throw new IllegalStateException(cause); |
| 1247 | } |
| 1248 | } finally { |
| 1249 | cancel(true /* interrupt if running */); |
| 1250 | } |
| 1251 | throw new OperationCanceledException(); |
| 1252 | } |
| 1253 | |
| 1254 | public Bundle getResult() |
| 1255 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 1256 | return internalGetResult(null, null); |
| 1257 | } |
| 1258 | |
| 1259 | public Bundle getResult(long timeout, TimeUnit unit) |
| 1260 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 1261 | return internalGetResult(timeout, unit); |
| 1262 | } |
| 1263 | |
| 1264 | protected void done() { |
| 1265 | if (mCallback != null) { |
| 1266 | postToHandler(mHandler, mCallback, this); |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | /** Handles the responses from the AccountManager */ |
| 1271 | private class Response extends IAccountManagerResponse.Stub { |
| 1272 | public void onResult(Bundle bundle) { |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 1273 | Intent intent = bundle.getParcelable(KEY_INTENT); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1274 | if (intent != null && mActivity != null) { |
| 1275 | // since the user provided an Activity we will silently start intents |
| 1276 | // that we see |
| 1277 | mActivity.startActivity(intent); |
| 1278 | // leave the Future running to wait for the real response to this request |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1279 | } else if (bundle.getBoolean("retry")) { |
| 1280 | try { |
| 1281 | doWork(); |
| 1282 | } catch (RemoteException e) { |
| 1283 | // this will only happen if the system process is dead, which means |
| 1284 | // we will be dying ourselves |
| 1285 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1286 | } else { |
| 1287 | set(bundle); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | public void onError(int code, String message) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1292 | if (code == ERROR_CODE_CANCELED) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1293 | // the authenticator indicated that this request was canceled, do so now |
| 1294 | cancel(true /* mayInterruptIfRunning */); |
| 1295 | return; |
| 1296 | } |
| 1297 | setException(convertErrorToException(code, message)); |
| 1298 | } |
| 1299 | } |
| 1300 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1303 | private abstract class BaseFutureTask<T> extends FutureTask<T> { |
| 1304 | final public IAccountManagerResponse mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1305 | final Handler mHandler; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1306 | |
| 1307 | public BaseFutureTask(Handler handler) { |
| 1308 | super(new Callable<T>() { |
| 1309 | public T call() throws Exception { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1310 | throw new IllegalStateException("this should never be called"); |
| 1311 | } |
| 1312 | }); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1313 | mHandler = handler; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1314 | mResponse = new Response(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1315 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1316 | |
| 1317 | public abstract void doWork() throws RemoteException; |
| 1318 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1319 | public abstract T bundleToResult(Bundle bundle) throws AuthenticatorException; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1320 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1321 | protected void postRunnableToHandler(Runnable runnable) { |
| 1322 | Handler handler = (mHandler == null) ? mMainHandler : mHandler; |
| 1323 | handler.post(runnable); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1326 | protected void startTask() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1327 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1328 | doWork(); |
| 1329 | } catch (RemoteException e) { |
| 1330 | setException(e); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1331 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1334 | protected class Response extends IAccountManagerResponse.Stub { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1335 | public void onResult(Bundle bundle) { |
| 1336 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1337 | T result = bundleToResult(bundle); |
| 1338 | if (result == null) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1339 | return; |
| 1340 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1341 | set(result); |
| 1342 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1343 | } catch (ClassCastException e) { |
| 1344 | // we will set the exception below |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1345 | } catch (AuthenticatorException e) { |
| 1346 | // we will set the exception below |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1347 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1348 | onError(ERROR_CODE_INVALID_RESPONSE, "no result in response"); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | public void onError(int code, String message) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1352 | if (code == ERROR_CODE_CANCELED) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1353 | cancel(true /* mayInterruptIfRunning */); |
| 1354 | return; |
| 1355 | } |
| 1356 | setException(convertErrorToException(code, message)); |
| 1357 | } |
| 1358 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | private abstract class Future2Task<T> |
| 1362 | extends BaseFutureTask<T> implements AccountManagerFuture<T> { |
| 1363 | final AccountManagerCallback<T> mCallback; |
| 1364 | public Future2Task(Handler handler, AccountManagerCallback<T> callback) { |
| 1365 | super(handler); |
| 1366 | mCallback = callback; |
| 1367 | } |
| 1368 | |
| 1369 | protected void done() { |
| 1370 | if (mCallback != null) { |
| 1371 | postRunnableToHandler(new Runnable() { |
| 1372 | public void run() { |
| 1373 | mCallback.run(Future2Task.this); |
| 1374 | } |
| 1375 | }); |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | public Future2Task<T> start() { |
| 1380 | startTask(); |
| 1381 | return this; |
| 1382 | } |
| 1383 | |
| 1384 | private T internalGetResult(Long timeout, TimeUnit unit) |
| 1385 | throws OperationCanceledException, IOException, AuthenticatorException { |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 1386 | if (!isDone()) { |
| 1387 | ensureNotOnMainThread(); |
| 1388 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1389 | try { |
| 1390 | if (timeout == null) { |
| 1391 | return get(); |
| 1392 | } else { |
| 1393 | return get(timeout, unit); |
| 1394 | } |
| 1395 | } catch (InterruptedException e) { |
| 1396 | // fall through and cancel |
| 1397 | } catch (TimeoutException e) { |
| 1398 | // fall through and cancel |
| 1399 | } catch (CancellationException e) { |
| 1400 | // fall through and cancel |
| 1401 | } catch (ExecutionException e) { |
| 1402 | final Throwable cause = e.getCause(); |
| 1403 | if (cause instanceof IOException) { |
| 1404 | throw (IOException) cause; |
| 1405 | } else if (cause instanceof UnsupportedOperationException) { |
| 1406 | throw new AuthenticatorException(cause); |
| 1407 | } else if (cause instanceof AuthenticatorException) { |
| 1408 | throw (AuthenticatorException) cause; |
| 1409 | } else if (cause instanceof RuntimeException) { |
| 1410 | throw (RuntimeException) cause; |
| 1411 | } else if (cause instanceof Error) { |
| 1412 | throw (Error) cause; |
| 1413 | } else { |
| 1414 | throw new IllegalStateException(cause); |
| 1415 | } |
| 1416 | } finally { |
| 1417 | cancel(true /* interrupt if running */); |
| 1418 | } |
| 1419 | throw new OperationCanceledException(); |
| 1420 | } |
| 1421 | |
| 1422 | public T getResult() |
| 1423 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 1424 | return internalGetResult(null, null); |
| 1425 | } |
| 1426 | |
| 1427 | public T getResult(long timeout, TimeUnit unit) |
| 1428 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 1429 | return internalGetResult(timeout, unit); |
| 1430 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1431 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1434 | private Exception convertErrorToException(int code, String message) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1435 | if (code == ERROR_CODE_NETWORK_ERROR) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1436 | return new IOException(message); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1437 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1438 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1439 | if (code == ERROR_CODE_UNSUPPORTED_OPERATION) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1440 | return new UnsupportedOperationException(message); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1441 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1442 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1443 | if (code == ERROR_CODE_INVALID_RESPONSE) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1444 | return new AuthenticatorException(message); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1447 | if (code == ERROR_CODE_BAD_ARGUMENTS) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1448 | return new IllegalArgumentException(message); |
| 1449 | } |
| 1450 | |
| 1451 | return new AuthenticatorException(message); |
| 1452 | } |
| 1453 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1454 | private class GetAuthTokenByTypeAndFeaturesTask |
| 1455 | extends AmsTask implements AccountManagerCallback<Bundle> { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1456 | GetAuthTokenByTypeAndFeaturesTask(final String accountType, final String authTokenType, |
| 1457 | final String[] features, Activity activityForPrompting, |
| 1458 | final Bundle addAccountOptions, final Bundle loginOptions, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1459 | AccountManagerCallback<Bundle> callback, Handler handler) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1460 | super(activityForPrompting, handler, callback); |
| 1461 | if (accountType == null) throw new IllegalArgumentException("account type is null"); |
| 1462 | mAccountType = accountType; |
| 1463 | mAuthTokenType = authTokenType; |
| 1464 | mFeatures = features; |
| 1465 | mAddAccountOptions = addAccountOptions; |
| 1466 | mLoginOptions = loginOptions; |
| 1467 | mMyCallback = this; |
| 1468 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1469 | volatile AccountManagerFuture<Bundle> mFuture = null; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1470 | final String mAccountType; |
| 1471 | final String mAuthTokenType; |
| 1472 | final String[] mFeatures; |
| 1473 | final Bundle mAddAccountOptions; |
| 1474 | final Bundle mLoginOptions; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1475 | final AccountManagerCallback<Bundle> mMyCallback; |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 1476 | private volatile int mNumAccounts = 0; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1477 | |
| 1478 | public void doWork() throws RemoteException { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1479 | getAccountsByTypeAndFeatures(mAccountType, mFeatures, |
| 1480 | new AccountManagerCallback<Account[]>() { |
| 1481 | public void run(AccountManagerFuture<Account[]> future) { |
| 1482 | Account[] accounts; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1483 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1484 | accounts = future.getResult(); |
| 1485 | } catch (OperationCanceledException e) { |
| 1486 | setException(e); |
| 1487 | return; |
| 1488 | } catch (IOException e) { |
| 1489 | setException(e); |
| 1490 | return; |
| 1491 | } catch (AuthenticatorException e) { |
| 1492 | setException(e); |
| 1493 | return; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1494 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1495 | |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 1496 | mNumAccounts = accounts.length; |
| 1497 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1498 | if (accounts.length == 0) { |
| 1499 | if (mActivity != null) { |
| 1500 | // no accounts, add one now. pretend that the user directly |
| 1501 | // made this request |
| 1502 | mFuture = addAccount(mAccountType, mAuthTokenType, mFeatures, |
| 1503 | mAddAccountOptions, mActivity, mMyCallback, mHandler); |
| 1504 | } else { |
| 1505 | // send result since we can't prompt to add an account |
| 1506 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1507 | result.putString(KEY_ACCOUNT_NAME, null); |
| 1508 | result.putString(KEY_ACCOUNT_TYPE, null); |
| 1509 | result.putString(KEY_AUTHTOKEN, null); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1510 | try { |
| 1511 | mResponse.onResult(result); |
| 1512 | } catch (RemoteException e) { |
| 1513 | // this will never happen |
| 1514 | } |
| 1515 | // we are done |
| 1516 | } |
| 1517 | } else if (accounts.length == 1) { |
| 1518 | // have a single account, return an authtoken for it |
| 1519 | if (mActivity == null) { |
| 1520 | mFuture = getAuthToken(accounts[0], mAuthTokenType, |
| 1521 | false /* notifyAuthFailure */, mMyCallback, mHandler); |
| 1522 | } else { |
| 1523 | mFuture = getAuthToken(accounts[0], |
| 1524 | mAuthTokenType, mLoginOptions, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1525 | mActivity, mMyCallback, mHandler); |
| 1526 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1527 | } else { |
| 1528 | if (mActivity != null) { |
| 1529 | IAccountManagerResponse chooseResponse = |
| 1530 | new IAccountManagerResponse.Stub() { |
| 1531 | public void onResult(Bundle value) throws RemoteException { |
| 1532 | Account account = new Account( |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1533 | value.getString(KEY_ACCOUNT_NAME), |
| 1534 | value.getString(KEY_ACCOUNT_TYPE)); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1535 | mFuture = getAuthToken(account, mAuthTokenType, mLoginOptions, |
| 1536 | mActivity, mMyCallback, mHandler); |
| 1537 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1538 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1539 | public void onError(int errorCode, String errorMessage) |
| 1540 | throws RemoteException { |
| 1541 | mResponse.onError(errorCode, errorMessage); |
| 1542 | } |
| 1543 | }; |
| 1544 | // have many accounts, launch the chooser |
| 1545 | Intent intent = new Intent(); |
| 1546 | intent.setClassName("android", |
| 1547 | "android.accounts.ChooseAccountActivity"); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1548 | intent.putExtra(KEY_ACCOUNTS, accounts); |
| 1549 | intent.putExtra(KEY_ACCOUNT_MANAGER_RESPONSE, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1550 | new AccountManagerResponse(chooseResponse)); |
| 1551 | mActivity.startActivity(intent); |
| 1552 | // the result will arrive via the IAccountManagerResponse |
| 1553 | } else { |
| 1554 | // send result since we can't prompt to select an account |
| 1555 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1556 | result.putString(KEY_ACCOUNTS, null); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1557 | try { |
| 1558 | mResponse.onResult(result); |
| 1559 | } catch (RemoteException e) { |
| 1560 | // this will never happen |
| 1561 | } |
| 1562 | // we are done |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1563 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1564 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1565 | }}, mHandler); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1568 | public void run(AccountManagerFuture<Bundle> future) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1569 | try { |
Fred Quintana | f0fd843 | 2010-03-08 12:48:05 -0800 | [diff] [blame] | 1570 | final Bundle result = future.getResult(); |
| 1571 | if (mNumAccounts == 0) { |
| 1572 | final String accountName = result.getString(KEY_ACCOUNT_NAME); |
| 1573 | final String accountType = result.getString(KEY_ACCOUNT_TYPE); |
| 1574 | if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) { |
| 1575 | setException(new AuthenticatorException("account not in result")); |
| 1576 | return; |
| 1577 | } |
| 1578 | final Account account = new Account(accountName, accountType); |
| 1579 | mNumAccounts = 1; |
| 1580 | getAuthToken(account, mAuthTokenType, null /* options */, mActivity, |
| 1581 | mMyCallback, mHandler); |
| 1582 | return; |
| 1583 | } |
| 1584 | set(result); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1585 | } catch (OperationCanceledException e) { |
| 1586 | cancel(true /* mayInterruptIfRUnning */); |
| 1587 | } catch (IOException e) { |
| 1588 | setException(e); |
| 1589 | } catch (AuthenticatorException e) { |
| 1590 | setException(e); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1595 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1596 | * This convenience helper combines the functionality of |
| 1597 | * {@link #getAccountsByTypeAndFeatures}, {@link #getAuthToken}, and |
| 1598 | * {@link #addAccount}. |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1599 | * |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1600 | * <p>This method gets a list of the accounts matching the |
| 1601 | * specified type and feature set; if there is exactly one, it is |
| 1602 | * used; if there are more than one, the user is prompted to pick one; |
| 1603 | * if there are none, the user is prompted to add one. Finally, |
| 1604 | * an auth token is acquired for the chosen account. |
| 1605 | * |
| 1606 | * <p>This method may be called from any thread, but the returned |
| 1607 | * {@link AccountManagerFuture} must not be used on the main thread. |
| 1608 | * |
| 1609 | * <p>This method requires the caller to hold the permission |
| 1610 | * {@link android.Manifest.permission#MANAGE_ACCOUNTS}. |
| 1611 | * |
| 1612 | * @param accountType The account type required |
Doug Zongker | ff592dc | 2010-02-23 12:26:33 -0800 | [diff] [blame] | 1613 | * (see {@link #getAccountsByType}), must not be null |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1614 | * @param authTokenType The desired auth token type |
| 1615 | * (see {@link #getAuthToken}), must not be null |
| 1616 | * @param features Required features for the account |
| 1617 | * (see {@link #getAccountsByTypeAndFeatures}), may be null or empty |
| 1618 | * @param activity The {@link Activity} context to use for launching new |
| 1619 | * sub-Activities to prompt to add an account, select an account, |
| 1620 | * and/or enter a password, as necessary; used only to call |
| 1621 | * startActivity(); should not be null |
| 1622 | * @param addAccountOptions Authenticator-specific options to use for |
| 1623 | * adding new accounts; may be null or empty |
| 1624 | * @param getAuthTokenOptions Authenticator-specific options to use for |
| 1625 | * getting auth tokens; may be null or empty |
| 1626 | * @param callback Callback to invoke when the request completes, |
| 1627 | * null for no callback |
| 1628 | * @param handler {@link Handler} identifying the callback thread, |
| 1629 | * null for the main thread |
| 1630 | * @return An {@link AccountManagerFuture} which resolves to a Bundle with |
| 1631 | * at least the following fields: |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1632 | * <ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1633 | * <li> {@link #KEY_ACCOUNT_NAME} - the name of the account |
| 1634 | * <li> {@link #KEY_ACCOUNT_TYPE} - the type of the account |
| 1635 | * <li> {@link #KEY_AUTHTOKEN} - the auth token you wanted |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1636 | * </ul> |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1637 | * |
Dan Egnor | 8e4378b | 2010-08-02 18:22:09 -0700 | [diff] [blame] | 1638 | * If an error occurred, {@link AccountManagerFuture#getResult()} throws: |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1639 | * <ul> |
| 1640 | * <li> {@link AuthenticatorException} if no authenticator was registered for |
| 1641 | * this account type or the authenticator failed to respond |
| 1642 | * <li> {@link OperationCanceledException} if the operation was canceled for |
| 1643 | * any reason, including the user canceling any operation |
| 1644 | * <li> {@link IOException} if the authenticator experienced an I/O problem |
| 1645 | * updating settings, usually because of network trouble |
| 1646 | * </ul> |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1647 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1648 | public AccountManagerFuture<Bundle> getAuthTokenByFeatures( |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1649 | final String accountType, final String authTokenType, final String[] features, |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1650 | final Activity activity, final Bundle addAccountOptions, |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1651 | final Bundle getAuthTokenOptions, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1652 | final AccountManagerCallback<Bundle> callback, final Handler handler) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1653 | if (accountType == null) throw new IllegalArgumentException("account type is null"); |
| 1654 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1655 | final GetAuthTokenByTypeAndFeaturesTask task = |
| 1656 | new GetAuthTokenByTypeAndFeaturesTask(accountType, authTokenType, features, |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1657 | activity, addAccountOptions, getAuthTokenOptions, callback, handler); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1658 | task.start(); |
| 1659 | return task; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1660 | } |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1661 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1662 | private final HashMap<OnAccountsUpdateListener, Handler> mAccountsUpdatedListeners = |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1663 | Maps.newHashMap(); |
| 1664 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1665 | /** |
| 1666 | * BroadcastReceiver that listens for the LOGIN_ACCOUNTS_CHANGED_ACTION intent |
| 1667 | * so that it can read the updated list of accounts and send them to the listener |
| 1668 | * in mAccountsUpdatedListeners. |
| 1669 | */ |
| 1670 | private final BroadcastReceiver mAccountsChangedBroadcastReceiver = new BroadcastReceiver() { |
| 1671 | public void onReceive(final Context context, final Intent intent) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1672 | final Account[] accounts = getAccounts(); |
| 1673 | // send the result to the listeners |
| 1674 | synchronized (mAccountsUpdatedListeners) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1675 | for (Map.Entry<OnAccountsUpdateListener, Handler> entry : |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1676 | mAccountsUpdatedListeners.entrySet()) { |
| 1677 | postToHandler(entry.getValue(), entry.getKey(), accounts); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1678 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1679 | } |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1680 | } |
| 1681 | }; |
| 1682 | |
| 1683 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1684 | * Adds an {@link OnAccountsUpdateListener} to this instance of the |
| 1685 | * {@link AccountManager}. This listener will be notified whenever the |
| 1686 | * list of accounts on the device changes. |
| 1687 | * |
| 1688 | * <p>As long as this listener is present, the AccountManager instance |
| 1689 | * will not be garbage-collected, and neither will the {@link Context} |
| 1690 | * used to retrieve it, which may be a large Activity instance. To avoid |
| 1691 | * memory leaks, you must remove this listener before then. Normally |
| 1692 | * listeners are added in an Activity or Service's {@link Activity#onCreate} |
| 1693 | * and removed in {@link Activity#onDestroy}. |
| 1694 | * |
| 1695 | * <p>It is safe to call this method from the main thread. |
| 1696 | * |
| 1697 | * <p>No permission is required to call this method. |
| 1698 | * |
| 1699 | * @param listener The listener to send notifications to |
| 1700 | * @param handler {@link Handler} identifying the thread to use |
| 1701 | * for notifications, null for the main thread |
| 1702 | * @param updateImmediately If true, the listener will be invoked |
| 1703 | * (on the handler thread) right away with the current account list |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1704 | * @throws IllegalArgumentException if listener is null |
| 1705 | * @throws IllegalStateException if listener was already added |
| 1706 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1707 | public void addOnAccountsUpdatedListener(final OnAccountsUpdateListener listener, |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1708 | Handler handler, boolean updateImmediately) { |
| 1709 | if (listener == null) { |
| 1710 | throw new IllegalArgumentException("the listener is null"); |
| 1711 | } |
| 1712 | synchronized (mAccountsUpdatedListeners) { |
| 1713 | if (mAccountsUpdatedListeners.containsKey(listener)) { |
| 1714 | throw new IllegalStateException("this listener is already added"); |
| 1715 | } |
| 1716 | final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty(); |
| 1717 | |
| 1718 | mAccountsUpdatedListeners.put(listener, handler); |
| 1719 | |
| 1720 | if (wasEmpty) { |
| 1721 | // Register a broadcast receiver to monitor account changes |
| 1722 | IntentFilter intentFilter = new IntentFilter(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1723 | intentFilter.addAction(LOGIN_ACCOUNTS_CHANGED_ACTION); |
Costin Manolache | b643724 | 2009-09-10 16:14:12 -0700 | [diff] [blame] | 1724 | // To recover from disk-full. |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1725 | intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1726 | mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter); |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | if (updateImmediately) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1731 | postToHandler(handler, listener, getAccounts()); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | /** |
Dan Egnor | 661f013 | 2010-02-19 11:23:00 -0800 | [diff] [blame] | 1736 | * Removes an {@link OnAccountsUpdateListener} previously registered with |
| 1737 | * {@link #addOnAccountsUpdatedListener}. The listener will no longer |
| 1738 | * receive notifications of account changes. |
| 1739 | * |
| 1740 | * <p>It is safe to call this method from the main thread. |
| 1741 | * |
| 1742 | * <p>No permission is required to call this method. |
| 1743 | * |
| 1744 | * @param listener The previously added listener to remove |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1745 | * @throws IllegalArgumentException if listener is null |
| 1746 | * @throws IllegalStateException if listener was not already added |
| 1747 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1748 | public void removeOnAccountsUpdatedListener(OnAccountsUpdateListener listener) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1749 | if (listener == null) throw new IllegalArgumentException("listener is null"); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1750 | synchronized (mAccountsUpdatedListeners) { |
Bryan Mawhinney | 5be61f5 | 2009-09-24 14:50:25 +0100 | [diff] [blame] | 1751 | if (!mAccountsUpdatedListeners.containsKey(listener)) { |
Costin Manolache | 88a211b | 2009-10-29 11:30:11 -0700 | [diff] [blame] | 1752 | Log.e(TAG, "Listener was not previously added"); |
| 1753 | return; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1754 | } |
Bryan Mawhinney | 5be61f5 | 2009-09-24 14:50:25 +0100 | [diff] [blame] | 1755 | mAccountsUpdatedListeners.remove(listener); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 1756 | if (mAccountsUpdatedListeners.isEmpty()) { |
| 1757 | mContext.unregisterReceiver(mAccountsChangedBroadcastReceiver); |
| 1758 | } |
| 1759 | } |
| 1760 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1761 | } |