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