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