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