Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.accounts; |
| 18 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.content.Intent; |
| 21 | import android.content.Context; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 22 | import android.content.IntentFilter; |
| 23 | import android.content.BroadcastReceiver; |
Costin Manolache | b643724 | 2009-09-10 16:14:12 -0700 | [diff] [blame] | 24 | import android.database.SQLException; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.os.Handler; |
| 27 | import android.os.Looper; |
| 28 | import android.os.RemoteException; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 29 | import android.os.Parcelable; |
Costin Manolache | b643724 | 2009-09-10 16:14:12 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 31 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 32 | import java.io.IOException; |
| 33 | import java.util.concurrent.Callable; |
| 34 | import java.util.concurrent.CancellationException; |
| 35 | import java.util.concurrent.ExecutionException; |
| 36 | import java.util.concurrent.FutureTask; |
| 37 | import java.util.concurrent.TimeoutException; |
| 38 | import java.util.concurrent.TimeUnit; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 39 | import java.util.HashMap; |
| 40 | import java.util.Map; |
| 41 | |
| 42 | import com.google.android.collect.Maps; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 45 | * A class that helps with interactions with the AccountManagerService. It provides |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 46 | * methods to allow for account, password, and authtoken management for all accounts on the |
| 47 | * device. Some of these calls are implemented with the help of the corresponding |
| 48 | * {@link IAccountAuthenticator} services. One accesses the {@link AccountManager} by calling: |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 49 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 50 | * |
| 51 | * <p> |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 52 | * TODO(fredq) this interface is still in flux |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 53 | */ |
| 54 | public class AccountManager { |
| 55 | private static final String TAG = "AccountManager"; |
| 56 | |
| 57 | private final Context mContext; |
| 58 | private final IAccountManager mService; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 59 | private final Handler mMainHandler; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 60 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 61 | /** |
| 62 | * @hide |
| 63 | */ |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 64 | public AccountManager(Context context, IAccountManager service) { |
| 65 | mContext = context; |
| 66 | mService = service; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 67 | mMainHandler = new Handler(mContext.getMainLooper()); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Fred Quintana | 0eabf02 | 2009-04-27 15:08:17 -0700 | [diff] [blame] | 70 | /** |
| 71 | * @hide used for testing only |
| 72 | */ |
| 73 | public AccountManager(Context context, IAccountManager service, Handler handler) { |
| 74 | mContext = context; |
| 75 | mService = service; |
| 76 | mMainHandler = handler; |
| 77 | } |
| 78 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 79 | public static AccountManager get(Context context) { |
| 80 | return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE); |
| 81 | } |
| 82 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 83 | public String getPassword(final Account account) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 84 | try { |
| 85 | return mService.getPassword(account); |
| 86 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 87 | // will never happen |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 88 | throw new RuntimeException(e); |
| 89 | } |
| 90 | } |
| 91 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 92 | public String getUserData(final Account account, final String key) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 93 | try { |
| 94 | return mService.getUserData(account, key); |
| 95 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 96 | // will never happen |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 97 | throw new RuntimeException(e); |
| 98 | } |
| 99 | } |
| 100 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 101 | public AuthenticatorDescription[] getAuthenticatorTypes() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 102 | try { |
| 103 | return mService.getAuthenticatorTypes(); |
| 104 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 105 | // will never happen |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 106 | throw new RuntimeException(e); |
| 107 | } |
| 108 | } |
| 109 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 110 | public Account[] getAccounts() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 111 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 112 | return mService.getAccounts(null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 113 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 114 | // won't ever happen |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 115 | throw new RuntimeException(e); |
| 116 | } |
| 117 | } |
| 118 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 119 | public Account[] getAccountsByType(String type) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 120 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 121 | return mService.getAccounts(type); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 122 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 123 | // won't ever happen |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 124 | throw new RuntimeException(e); |
| 125 | } |
| 126 | } |
| 127 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 128 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 129 | try { |
| 130 | return mService.addAccount(account, password, extras); |
| 131 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 132 | // won't ever happen |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 133 | throw new RuntimeException(e); |
| 134 | } |
| 135 | } |
| 136 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 137 | public AccountManagerFuture<Boolean> removeAccount(final Account account, |
| 138 | AccountManagerCallback<Boolean> callback, Handler handler) { |
| 139 | return new Future2Task<Boolean>(handler, callback) { |
| 140 | public void doWork() throws RemoteException { |
| 141 | mService.removeAccount(mResponse, account); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 142 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 143 | public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 144 | if (!bundle.containsKey(Constants.BOOLEAN_RESULT_KEY)) { |
| 145 | throw new AuthenticatorException("no result in response"); |
| 146 | } |
| 147 | return bundle.getBoolean(Constants.BOOLEAN_RESULT_KEY); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 148 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 149 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 152 | public void invalidateAuthToken(final String accountType, final String authToken) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 153 | try { |
| 154 | mService.invalidateAuthToken(accountType, authToken); |
| 155 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 156 | // won't ever happen |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 157 | throw new RuntimeException(e); |
| 158 | } |
| 159 | } |
| 160 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 161 | public String peekAuthToken(final Account account, final String authTokenType) { |
| 162 | try { |
| 163 | return mService.peekAuthToken(account, authTokenType); |
| 164 | } catch (RemoteException e) { |
| 165 | // won't ever happen |
| 166 | throw new RuntimeException(e); |
| 167 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 170 | public void setPassword(final Account account, final String password) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 171 | try { |
| 172 | mService.setPassword(account, password); |
| 173 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 174 | // won't ever happen |
| 175 | throw new RuntimeException(e); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 179 | public void clearPassword(final Account account) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 180 | try { |
| 181 | mService.clearPassword(account); |
| 182 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 183 | // won't ever happen |
| 184 | throw new RuntimeException(e); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 188 | public void setUserData(final Account account, final String key, final String value) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 189 | try { |
| 190 | mService.setUserData(account, key, value); |
| 191 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 192 | // won't ever happen |
| 193 | throw new RuntimeException(e); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 197 | public void setAuthToken(Account account, final String authTokenType, final String authToken) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 198 | try { |
| 199 | mService.setAuthToken(account, authTokenType, authToken); |
| 200 | } catch (RemoteException e) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 201 | // won't ever happen |
| 202 | throw new RuntimeException(e); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 206 | public String blockingGetAuthToken(Account account, String authTokenType, |
| 207 | boolean notifyAuthFailure) |
| 208 | throws OperationCanceledException, IOException, AuthenticatorException { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 209 | Bundle bundle = getAuthToken(account, authTokenType, notifyAuthFailure, null /* callback */, |
| 210 | null /* handler */).getResult(); |
| 211 | return bundle.getString(Constants.AUTHTOKEN_KEY); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Request the auth token for this account/authTokenType. If this succeeds then the |
| 216 | * auth token will then be passed to the activity. If this results in an authentication |
| 217 | * failure then a login intent will be returned that can be invoked to prompt the user to |
| 218 | * update their credentials. This login activity will return the auth token to the calling |
| 219 | * activity. If activity is null then the login intent will not be invoked. |
| 220 | * |
| 221 | * @param account the account whose auth token should be retrieved |
| 222 | * @param authTokenType the auth token type that should be retrieved |
| 223 | * @param loginOptions |
| 224 | * @param activity the activity to launch the login intent, if necessary, and to which |
| 225 | */ |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 226 | public AccountManagerFuture<Bundle> getAuthToken( |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 227 | final Account account, final String authTokenType, final Bundle loginOptions, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 228 | final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 229 | if (activity == null) throw new IllegalArgumentException("activity is null"); |
| 230 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 231 | return new AmsTask(activity, handler, callback) { |
| 232 | public void doWork() throws RemoteException { |
| 233 | mService.getAuthToken(mResponse, account, authTokenType, |
| 234 | false /* notifyOnAuthFailure */, true /* expectActivityLaunch */, |
| 235 | loginOptions); |
| 236 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 237 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 240 | public AccountManagerFuture<Bundle> getAuthToken( |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 241 | final Account account, final String authTokenType, final boolean notifyAuthFailure, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 242 | AccountManagerCallback<Bundle> callback, Handler handler) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 243 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 244 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 245 | return new AmsTask(null, handler, callback) { |
| 246 | public void doWork() throws RemoteException { |
| 247 | mService.getAuthToken(mResponse, account, authTokenType, |
| 248 | notifyAuthFailure, false /* expectActivityLaunch */, null /* options */); |
| 249 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 250 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 253 | public AccountManagerFuture<Bundle> addAccount(final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 254 | final String authTokenType, final String[] requiredFeatures, |
| 255 | final Bundle addAccountOptions, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 256 | final Activity activity, AccountManagerCallback<Bundle> callback, Handler handler) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 257 | return new AmsTask(activity, handler, callback) { |
| 258 | public void doWork() throws RemoteException { |
| 259 | mService.addAcount(mResponse, accountType, authTokenType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 260 | requiredFeatures, activity != null, addAccountOptions); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 261 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 262 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** @deprecated use {@link #confirmCredentials} instead */ |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 266 | @Deprecated |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 267 | public AccountManagerFuture<Boolean> confirmPassword(final Account account, final String password, |
| 268 | AccountManagerCallback<Boolean> callback, Handler handler) { |
| 269 | return new Future2Task<Boolean>(handler, callback) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 270 | public void doWork() throws RemoteException { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 271 | mService.confirmPassword(mResponse, account, password); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 272 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 273 | public Boolean bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 274 | if (!bundle.containsKey(Constants.BOOLEAN_RESULT_KEY)) { |
| 275 | throw new AuthenticatorException("no result in response"); |
| 276 | } |
| 277 | return bundle.getBoolean(Constants.BOOLEAN_RESULT_KEY); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 278 | } |
| 279 | }.start(); |
| 280 | } |
| 281 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 282 | public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures( |
| 283 | final String type, final String[] features, |
| 284 | AccountManagerCallback<Account[]> callback, Handler handler) { |
| 285 | if (type == null) throw new IllegalArgumentException("type is null"); |
| 286 | return new Future2Task<Account[]>(handler, callback) { |
| 287 | public void doWork() throws RemoteException { |
| 288 | mService.getAccountsByFeatures(mResponse, type, features); |
| 289 | } |
| 290 | public Account[] bundleToResult(Bundle bundle) throws AuthenticatorException { |
| 291 | if (!bundle.containsKey(Constants.ACCOUNTS_KEY)) { |
| 292 | throw new AuthenticatorException("no result in response"); |
| 293 | } |
| 294 | final Parcelable[] parcelables = bundle.getParcelableArray(Constants.ACCOUNTS_KEY); |
| 295 | Account[] descs = new Account[parcelables.length]; |
| 296 | for (int i = 0; i < parcelables.length; i++) { |
| 297 | descs[i] = (Account) parcelables[i]; |
| 298 | } |
| 299 | return descs; |
| 300 | } |
| 301 | }.start(); |
| 302 | } |
| 303 | |
| 304 | public AccountManagerFuture<Bundle> confirmCredentials(final Account account, final Activity activity, |
| 305 | final AccountManagerCallback<Bundle> callback, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 306 | final Handler handler) { |
| 307 | return new AmsTask(activity, handler, callback) { |
| 308 | public void doWork() throws RemoteException { |
| 309 | mService.confirmCredentials(mResponse, account, activity != null); |
| 310 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 311 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 314 | public AccountManagerFuture<Bundle> updateCredentials(final Account account, final String authTokenType, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 315 | final Bundle loginOptions, final Activity activity, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 316 | final AccountManagerCallback<Bundle> callback, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 317 | final Handler handler) { |
| 318 | return new AmsTask(activity, handler, callback) { |
| 319 | public void doWork() throws RemoteException { |
| 320 | mService.updateCredentials(mResponse, account, authTokenType, activity != null, |
| 321 | loginOptions); |
| 322 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 323 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 326 | public AccountManagerFuture<Bundle> editProperties(final String accountType, final Activity activity, |
| 327 | final AccountManagerCallback<Bundle> callback, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 328 | final Handler handler) { |
| 329 | return new AmsTask(activity, handler, callback) { |
| 330 | public void doWork() throws RemoteException { |
| 331 | mService.editProperties(mResponse, accountType, activity != null); |
| 332 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 333 | }.start(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | private void ensureNotOnMainThread() { |
| 337 | final Looper looper = Looper.myLooper(); |
| 338 | if (looper != null && looper == mContext.getMainLooper()) { |
| 339 | // We really want to throw an exception here, but GTalkService exercises this |
| 340 | // path quite a bit and needs some serious rewrite in order to work properly. |
| 341 | //noinspection ThrowableInstanceNeverThrow |
| 342 | // Log.e(TAG, "calling this from your main thread can lead to deadlock and/or ANRs", |
| 343 | // new Exception()); |
| 344 | // TODO(fredq) remove the log and throw this exception when the callers are fixed |
| 345 | // throw new IllegalStateException( |
| 346 | // "calling this from your main thread can lead to deadlock"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 350 | private void postToHandler(Handler handler, final AccountManagerCallback<Bundle> callback, |
| 351 | final AccountManagerFuture<Bundle> future) { |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 352 | handler = handler == null ? mMainHandler : handler; |
| 353 | handler.post(new Runnable() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 354 | public void run() { |
| 355 | callback.run(future); |
| 356 | } |
| 357 | }); |
| 358 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 359 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 360 | private void postToHandler(Handler handler, final OnAccountsUpdatedListener listener, |
| 361 | final Account[] accounts) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 362 | final Account[] accountsCopy = new Account[accounts.length]; |
| 363 | // send a copy to make sure that one doesn't |
| 364 | // change what another sees |
| 365 | System.arraycopy(accounts, 0, accountsCopy, 0, accountsCopy.length); |
| 366 | handler = (handler == null) ? mMainHandler : handler; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 367 | handler.post(new Runnable() { |
| 368 | public void run() { |
Costin Manolache | b643724 | 2009-09-10 16:14:12 -0700 | [diff] [blame] | 369 | try { |
| 370 | listener.onAccountsUpdated(accountsCopy); |
| 371 | } catch (SQLException e) { |
| 372 | // Better luck next time. If the problem was disk-full, |
| 373 | // the STORAGE_OK intent will re-trigger the update. |
| 374 | Log.e(TAG, "Can't update accounts", e); |
| 375 | } |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 376 | } |
| 377 | }); |
| 378 | } |
| 379 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 380 | private abstract class AmsTask extends FutureTask<Bundle> implements AccountManagerFuture<Bundle> { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 381 | final IAccountManagerResponse mResponse; |
| 382 | final Handler mHandler; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 383 | final AccountManagerCallback<Bundle> mCallback; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 384 | final Activity mActivity; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 385 | public AmsTask(Activity activity, Handler handler, AccountManagerCallback<Bundle> callback) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 386 | super(new Callable<Bundle>() { |
| 387 | public Bundle call() throws Exception { |
| 388 | throw new IllegalStateException("this should never be called"); |
| 389 | } |
| 390 | }); |
| 391 | |
| 392 | mHandler = handler; |
| 393 | mCallback = callback; |
| 394 | mActivity = activity; |
| 395 | mResponse = new Response(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 398 | public final AccountManagerFuture<Bundle> start() { |
| 399 | try { |
| 400 | doWork(); |
| 401 | } catch (RemoteException e) { |
| 402 | setException(e); |
| 403 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 404 | return this; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 405 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 406 | |
| 407 | public abstract void doWork() throws RemoteException; |
| 408 | |
| 409 | private Bundle internalGetResult(Long timeout, TimeUnit unit) |
| 410 | throws OperationCanceledException, IOException, AuthenticatorException { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 411 | ensureNotOnMainThread(); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 412 | try { |
| 413 | if (timeout == null) { |
| 414 | return get(); |
| 415 | } else { |
| 416 | return get(timeout, unit); |
| 417 | } |
| 418 | } catch (CancellationException e) { |
| 419 | throw new OperationCanceledException(); |
| 420 | } catch (TimeoutException e) { |
| 421 | // fall through and cancel |
| 422 | } catch (InterruptedException e) { |
| 423 | // fall through and cancel |
| 424 | } catch (ExecutionException e) { |
| 425 | final Throwable cause = e.getCause(); |
| 426 | if (cause instanceof IOException) { |
| 427 | throw (IOException) cause; |
| 428 | } else if (cause instanceof UnsupportedOperationException) { |
| 429 | throw new AuthenticatorException(cause); |
| 430 | } else if (cause instanceof AuthenticatorException) { |
| 431 | throw (AuthenticatorException) cause; |
| 432 | } else if (cause instanceof RuntimeException) { |
| 433 | throw (RuntimeException) cause; |
| 434 | } else if (cause instanceof Error) { |
| 435 | throw (Error) cause; |
| 436 | } else { |
| 437 | throw new IllegalStateException(cause); |
| 438 | } |
| 439 | } finally { |
| 440 | cancel(true /* interrupt if running */); |
| 441 | } |
| 442 | throw new OperationCanceledException(); |
| 443 | } |
| 444 | |
| 445 | public Bundle getResult() |
| 446 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 447 | return internalGetResult(null, null); |
| 448 | } |
| 449 | |
| 450 | public Bundle getResult(long timeout, TimeUnit unit) |
| 451 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 452 | return internalGetResult(timeout, unit); |
| 453 | } |
| 454 | |
| 455 | protected void done() { |
| 456 | if (mCallback != null) { |
| 457 | postToHandler(mHandler, mCallback, this); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /** Handles the responses from the AccountManager */ |
| 462 | private class Response extends IAccountManagerResponse.Stub { |
| 463 | public void onResult(Bundle bundle) { |
| 464 | Intent intent = bundle.getParcelable("intent"); |
| 465 | if (intent != null && mActivity != null) { |
| 466 | // since the user provided an Activity we will silently start intents |
| 467 | // that we see |
| 468 | mActivity.startActivity(intent); |
| 469 | // 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] | 470 | } else if (bundle.getBoolean("retry")) { |
| 471 | try { |
| 472 | doWork(); |
| 473 | } catch (RemoteException e) { |
| 474 | // this will only happen if the system process is dead, which means |
| 475 | // we will be dying ourselves |
| 476 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 477 | } else { |
| 478 | set(bundle); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | public void onError(int code, String message) { |
| 483 | if (code == Constants.ERROR_CODE_CANCELED) { |
| 484 | // the authenticator indicated that this request was canceled, do so now |
| 485 | cancel(true /* mayInterruptIfRunning */); |
| 486 | return; |
| 487 | } |
| 488 | setException(convertErrorToException(code, message)); |
| 489 | } |
| 490 | } |
| 491 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 494 | private abstract class BaseFutureTask<T> extends FutureTask<T> { |
| 495 | final public IAccountManagerResponse mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 496 | final Handler mHandler; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 497 | |
| 498 | public BaseFutureTask(Handler handler) { |
| 499 | super(new Callable<T>() { |
| 500 | public T call() throws Exception { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 501 | throw new IllegalStateException("this should never be called"); |
| 502 | } |
| 503 | }); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 504 | mHandler = handler; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 505 | mResponse = new Response(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 506 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 507 | |
| 508 | public abstract void doWork() throws RemoteException; |
| 509 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 510 | public abstract T bundleToResult(Bundle bundle) throws AuthenticatorException; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 511 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 512 | protected void postRunnableToHandler(Runnable runnable) { |
| 513 | Handler handler = (mHandler == null) ? mMainHandler : mHandler; |
| 514 | handler.post(runnable); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 517 | protected void startTask() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 518 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 519 | doWork(); |
| 520 | } catch (RemoteException e) { |
| 521 | setException(e); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 522 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 525 | protected class Response extends IAccountManagerResponse.Stub { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 526 | public void onResult(Bundle bundle) { |
| 527 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 528 | T result = bundleToResult(bundle); |
| 529 | if (result == null) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 530 | return; |
| 531 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 532 | set(result); |
| 533 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 534 | } catch (ClassCastException e) { |
| 535 | // we will set the exception below |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 536 | } catch (AuthenticatorException e) { |
| 537 | // we will set the exception below |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 538 | } |
| 539 | onError(Constants.ERROR_CODE_INVALID_RESPONSE, "no result in response"); |
| 540 | } |
| 541 | |
| 542 | public void onError(int code, String message) { |
| 543 | if (code == Constants.ERROR_CODE_CANCELED) { |
| 544 | cancel(true /* mayInterruptIfRunning */); |
| 545 | return; |
| 546 | } |
| 547 | setException(convertErrorToException(code, message)); |
| 548 | } |
| 549 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | private abstract class Future2Task<T> |
| 553 | extends BaseFutureTask<T> implements AccountManagerFuture<T> { |
| 554 | final AccountManagerCallback<T> mCallback; |
| 555 | public Future2Task(Handler handler, AccountManagerCallback<T> callback) { |
| 556 | super(handler); |
| 557 | mCallback = callback; |
| 558 | } |
| 559 | |
| 560 | protected void done() { |
| 561 | if (mCallback != null) { |
| 562 | postRunnableToHandler(new Runnable() { |
| 563 | public void run() { |
| 564 | mCallback.run(Future2Task.this); |
| 565 | } |
| 566 | }); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | public Future2Task<T> start() { |
| 571 | startTask(); |
| 572 | return this; |
| 573 | } |
| 574 | |
| 575 | private T internalGetResult(Long timeout, TimeUnit unit) |
| 576 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 577 | ensureNotOnMainThread(); |
| 578 | try { |
| 579 | if (timeout == null) { |
| 580 | return get(); |
| 581 | } else { |
| 582 | return get(timeout, unit); |
| 583 | } |
| 584 | } catch (InterruptedException e) { |
| 585 | // fall through and cancel |
| 586 | } catch (TimeoutException e) { |
| 587 | // fall through and cancel |
| 588 | } catch (CancellationException e) { |
| 589 | // fall through and cancel |
| 590 | } catch (ExecutionException e) { |
| 591 | final Throwable cause = e.getCause(); |
| 592 | if (cause instanceof IOException) { |
| 593 | throw (IOException) cause; |
| 594 | } else if (cause instanceof UnsupportedOperationException) { |
| 595 | throw new AuthenticatorException(cause); |
| 596 | } else if (cause instanceof AuthenticatorException) { |
| 597 | throw (AuthenticatorException) cause; |
| 598 | } else if (cause instanceof RuntimeException) { |
| 599 | throw (RuntimeException) cause; |
| 600 | } else if (cause instanceof Error) { |
| 601 | throw (Error) cause; |
| 602 | } else { |
| 603 | throw new IllegalStateException(cause); |
| 604 | } |
| 605 | } finally { |
| 606 | cancel(true /* interrupt if running */); |
| 607 | } |
| 608 | throw new OperationCanceledException(); |
| 609 | } |
| 610 | |
| 611 | public T getResult() |
| 612 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 613 | return internalGetResult(null, null); |
| 614 | } |
| 615 | |
| 616 | public T getResult(long timeout, TimeUnit unit) |
| 617 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 618 | return internalGetResult(timeout, unit); |
| 619 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 620 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 623 | private Exception convertErrorToException(int code, String message) { |
| 624 | if (code == Constants.ERROR_CODE_NETWORK_ERROR) { |
| 625 | return new IOException(message); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 626 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 627 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 628 | if (code == Constants.ERROR_CODE_UNSUPPORTED_OPERATION) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 629 | return new UnsupportedOperationException(message); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 630 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 631 | |
| 632 | if (code == Constants.ERROR_CODE_INVALID_RESPONSE) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 633 | return new AuthenticatorException(message); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 636 | if (code == Constants.ERROR_CODE_BAD_ARGUMENTS) { |
| 637 | return new IllegalArgumentException(message); |
| 638 | } |
| 639 | |
| 640 | return new AuthenticatorException(message); |
| 641 | } |
| 642 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 643 | private class GetAuthTokenByTypeAndFeaturesTask |
| 644 | extends AmsTask implements AccountManagerCallback<Bundle> { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 645 | GetAuthTokenByTypeAndFeaturesTask(final String accountType, final String authTokenType, |
| 646 | final String[] features, Activity activityForPrompting, |
| 647 | final Bundle addAccountOptions, final Bundle loginOptions, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 648 | AccountManagerCallback<Bundle> callback, Handler handler) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 649 | super(activityForPrompting, handler, callback); |
| 650 | if (accountType == null) throw new IllegalArgumentException("account type is null"); |
| 651 | mAccountType = accountType; |
| 652 | mAuthTokenType = authTokenType; |
| 653 | mFeatures = features; |
| 654 | mAddAccountOptions = addAccountOptions; |
| 655 | mLoginOptions = loginOptions; |
| 656 | mMyCallback = this; |
| 657 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 658 | volatile AccountManagerFuture<Bundle> mFuture = null; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 659 | final String mAccountType; |
| 660 | final String mAuthTokenType; |
| 661 | final String[] mFeatures; |
| 662 | final Bundle mAddAccountOptions; |
| 663 | final Bundle mLoginOptions; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 664 | final AccountManagerCallback<Bundle> mMyCallback; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 665 | |
| 666 | public void doWork() throws RemoteException { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 667 | getAccountsByTypeAndFeatures(mAccountType, mFeatures, |
| 668 | new AccountManagerCallback<Account[]>() { |
| 669 | public void run(AccountManagerFuture<Account[]> future) { |
| 670 | Account[] accounts; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 671 | try { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 672 | accounts = future.getResult(); |
| 673 | } catch (OperationCanceledException e) { |
| 674 | setException(e); |
| 675 | return; |
| 676 | } catch (IOException e) { |
| 677 | setException(e); |
| 678 | return; |
| 679 | } catch (AuthenticatorException e) { |
| 680 | setException(e); |
| 681 | return; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 682 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 683 | |
| 684 | if (accounts.length == 0) { |
| 685 | if (mActivity != null) { |
| 686 | // no accounts, add one now. pretend that the user directly |
| 687 | // made this request |
| 688 | mFuture = addAccount(mAccountType, mAuthTokenType, mFeatures, |
| 689 | mAddAccountOptions, mActivity, mMyCallback, mHandler); |
| 690 | } else { |
| 691 | // send result since we can't prompt to add an account |
| 692 | Bundle result = new Bundle(); |
| 693 | result.putString(Constants.ACCOUNT_NAME_KEY, null); |
| 694 | result.putString(Constants.ACCOUNT_TYPE_KEY, null); |
| 695 | result.putString(Constants.AUTHTOKEN_KEY, null); |
| 696 | try { |
| 697 | mResponse.onResult(result); |
| 698 | } catch (RemoteException e) { |
| 699 | // this will never happen |
| 700 | } |
| 701 | // we are done |
| 702 | } |
| 703 | } else if (accounts.length == 1) { |
| 704 | // have a single account, return an authtoken for it |
| 705 | if (mActivity == null) { |
| 706 | mFuture = getAuthToken(accounts[0], mAuthTokenType, |
| 707 | false /* notifyAuthFailure */, mMyCallback, mHandler); |
| 708 | } else { |
| 709 | mFuture = getAuthToken(accounts[0], |
| 710 | mAuthTokenType, mLoginOptions, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 711 | mActivity, mMyCallback, mHandler); |
| 712 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 713 | } else { |
| 714 | if (mActivity != null) { |
| 715 | IAccountManagerResponse chooseResponse = |
| 716 | new IAccountManagerResponse.Stub() { |
| 717 | public void onResult(Bundle value) throws RemoteException { |
| 718 | Account account = new Account( |
| 719 | value.getString(Constants.ACCOUNT_NAME_KEY), |
| 720 | value.getString(Constants.ACCOUNT_TYPE_KEY)); |
| 721 | mFuture = getAuthToken(account, mAuthTokenType, mLoginOptions, |
| 722 | mActivity, mMyCallback, mHandler); |
| 723 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 724 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 725 | public void onError(int errorCode, String errorMessage) |
| 726 | throws RemoteException { |
| 727 | mResponse.onError(errorCode, errorMessage); |
| 728 | } |
| 729 | }; |
| 730 | // have many accounts, launch the chooser |
| 731 | Intent intent = new Intent(); |
| 732 | intent.setClassName("android", |
| 733 | "android.accounts.ChooseAccountActivity"); |
| 734 | intent.putExtra(Constants.ACCOUNTS_KEY, accounts); |
| 735 | intent.putExtra(Constants.ACCOUNT_MANAGER_RESPONSE_KEY, |
| 736 | new AccountManagerResponse(chooseResponse)); |
| 737 | mActivity.startActivity(intent); |
| 738 | // the result will arrive via the IAccountManagerResponse |
| 739 | } else { |
| 740 | // send result since we can't prompt to select an account |
| 741 | Bundle result = new Bundle(); |
| 742 | result.putString(Constants.ACCOUNTS_KEY, null); |
| 743 | try { |
| 744 | mResponse.onResult(result); |
| 745 | } catch (RemoteException e) { |
| 746 | // this will never happen |
| 747 | } |
| 748 | // we are done |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 749 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 750 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 751 | }}, mHandler); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | |
| 755 | |
| 756 | // TODO(fredq) pass through the calls to our implemention of Future2 to the underlying |
| 757 | // future that we create. We need to do things like have cancel cancel the mFuture, if set |
| 758 | // or to cause this to be canceled if mFuture isn't set. |
| 759 | // Once this is done then getAuthTokenByFeatures can be changed to return a Future2. |
| 760 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 761 | public void run(AccountManagerFuture<Bundle> future) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 762 | try { |
| 763 | set(future.get()); |
| 764 | } catch (InterruptedException e) { |
| 765 | cancel(true); |
| 766 | } catch (CancellationException e) { |
| 767 | cancel(true); |
| 768 | } catch (ExecutionException e) { |
| 769 | setException(e.getCause()); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | public void getAuthTokenByFeatures( |
| 775 | final String accountType, final String authTokenType, final String[] features, |
| 776 | final Activity activityForPrompting, final Bundle addAccountOptions, |
| 777 | final Bundle loginOptions, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 778 | final AccountManagerCallback<Bundle> callback, final Handler handler) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 779 | if (accountType == null) throw new IllegalArgumentException("account type is null"); |
| 780 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 781 | new GetAuthTokenByTypeAndFeaturesTask(accountType, authTokenType, features, |
| 782 | activityForPrompting, addAccountOptions, loginOptions, callback, handler).start(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 783 | } |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 784 | |
| 785 | private final HashMap<OnAccountsUpdatedListener, Handler> mAccountsUpdatedListeners = |
| 786 | Maps.newHashMap(); |
| 787 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 788 | /** |
| 789 | * BroadcastReceiver that listens for the LOGIN_ACCOUNTS_CHANGED_ACTION intent |
| 790 | * so that it can read the updated list of accounts and send them to the listener |
| 791 | * in mAccountsUpdatedListeners. |
| 792 | */ |
| 793 | private final BroadcastReceiver mAccountsChangedBroadcastReceiver = new BroadcastReceiver() { |
| 794 | public void onReceive(final Context context, final Intent intent) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 795 | final Account[] accounts = getAccounts(); |
| 796 | // send the result to the listeners |
| 797 | synchronized (mAccountsUpdatedListeners) { |
| 798 | for (Map.Entry<OnAccountsUpdatedListener, Handler> entry : |
| 799 | mAccountsUpdatedListeners.entrySet()) { |
| 800 | postToHandler(entry.getValue(), entry.getKey(), accounts); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 801 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 802 | } |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 803 | } |
| 804 | }; |
| 805 | |
| 806 | /** |
| 807 | * Add a {@link OnAccountsUpdatedListener} to this instance of the {@link AccountManager}. |
| 808 | * The listener is guaranteed to be invoked on the thread of the Handler that is passed |
| 809 | * in or the main thread's Handler if handler is null. |
| 810 | * @param listener the listener to add |
| 811 | * @param handler the Handler whose thread will be used to invoke the listener. If null |
| 812 | * the AccountManager context's main thread will be used. |
| 813 | * @param updateImmediately if true then the listener will be invoked as a result of this |
| 814 | * call. |
| 815 | * @throws IllegalArgumentException if listener is null |
| 816 | * @throws IllegalStateException if listener was already added |
| 817 | */ |
| 818 | public void addOnAccountsUpdatedListener(final OnAccountsUpdatedListener listener, |
| 819 | Handler handler, boolean updateImmediately) { |
| 820 | if (listener == null) { |
| 821 | throw new IllegalArgumentException("the listener is null"); |
| 822 | } |
| 823 | synchronized (mAccountsUpdatedListeners) { |
| 824 | if (mAccountsUpdatedListeners.containsKey(listener)) { |
| 825 | throw new IllegalStateException("this listener is already added"); |
| 826 | } |
| 827 | final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty(); |
| 828 | |
| 829 | mAccountsUpdatedListeners.put(listener, handler); |
| 830 | |
| 831 | if (wasEmpty) { |
| 832 | // Register a broadcast receiver to monitor account changes |
| 833 | IntentFilter intentFilter = new IntentFilter(); |
| 834 | intentFilter.addAction(Constants.LOGIN_ACCOUNTS_CHANGED_ACTION); |
Costin Manolache | b643724 | 2009-09-10 16:14:12 -0700 | [diff] [blame] | 835 | // To recover from disk-full. |
| 836 | intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 837 | mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | if (updateImmediately) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 842 | postToHandler(handler, listener, getAccounts()); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * Remove an {@link OnAccountsUpdatedListener} that was previously registered with |
| 848 | * {@link #addOnAccountsUpdatedListener}. |
| 849 | * @param listener the listener to remove |
| 850 | * @throws IllegalArgumentException if listener is null |
| 851 | * @throws IllegalStateException if listener was not already added |
| 852 | */ |
| 853 | public void removeOnAccountsUpdatedListener(OnAccountsUpdatedListener listener) { |
| 854 | if (listener == null) { |
| 855 | throw new IllegalArgumentException("the listener is null"); |
| 856 | } |
| 857 | synchronized (mAccountsUpdatedListeners) { |
| 858 | if (mAccountsUpdatedListeners.remove(listener) == null) { |
| 859 | throw new IllegalStateException("this listener was not previously added"); |
| 860 | } |
| 861 | if (mAccountsUpdatedListeners.isEmpty()) { |
| 862 | mContext.unregisterReceiver(mAccountsChangedBroadcastReceiver); |
| 863 | } |
| 864 | } |
| 865 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 866 | } |