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