summaryrefslogtreecommitdiffstats
path: root/core/java/android/accounts/AccountManager.java
blob: 4fcaa884b1c6060055249f4da01189bac9ba4bca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.accounts;

import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.RemoteException;
import android.os.Parcelable;
import android.util.Config;
import android.util.Log;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;
import java.util.HashMap;
import java.util.Map;

import com.google.android.collect.Maps;

/**
 * A class that helps with interactions with the AccountManagerService. It provides
 * methods to allow for account, password, and authtoken management for all accounts on the
 * device. Some of these calls are implemented with the help of the corresponding
 * {@link IAccountAuthenticator} services. One accesses the {@link AccountManager} by calling:
 *    AccountManager accountManager = AccountManager.get(context);
 *
 * <p>
 * TODO(fredq) this interface is still in flux
 */
public class AccountManager {
    private static final String TAG = "AccountManager";

    private final Context mContext;
    private final IAccountManager mService;
    private final Handler mMainHandler;

    /**
     * @hide
     */
    public AccountManager(Context context, IAccountManager service) {
        mContext = context;
        mService = service;
        mMainHandler = new Handler(mContext.getMainLooper());
    }

    /**
     * @hide used for testing only
     */
    public AccountManager(Context context, IAccountManager service, Handler handler) {
        mContext = context;
        mService = service;
        mMainHandler = handler;
    }

    public static AccountManager get(Context context) {
        return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    }

    public String blockingGetPassword(Account account) {
        ensureNotOnMainThread();
        try {
            return mService.getPassword(account);
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
            throw new RuntimeException(e);
        }
    }

    public Future1<String> getPassword(final Future1Callback<String> callback,
            final Account account, final Handler handler) {
        return startAsFuture(callback, handler, new Callable<String>() {
            public String call() throws Exception {
                return blockingGetPassword(account);
            }
        });
    }

    public String blockingGetUserData(Account account, String key) {
        ensureNotOnMainThread();
        try {
            return mService.getUserData(account, key);
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
            throw new RuntimeException(e);
        }
    }

    public Future1<String> getUserData(Future1Callback<String> callback,
            final Account account, final String key, Handler handler) {
        return startAsFuture(callback, handler, new Callable<String>() {
            public String call() throws Exception {
                return blockingGetUserData(account, key);
            }
        });
    }

    public String[] blockingGetAuthenticatorTypes() {
        ensureNotOnMainThread();
        try {
            return mService.getAuthenticatorTypes();
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
            throw new RuntimeException(e);
        }
    }

    public Future1<String[]> getAuthenticatorTypes(Future1Callback<String[]> callback,
            Handler handler) {
        return startAsFuture(callback, handler, new Callable<String[]>() {
            public String[] call() throws Exception {
                return blockingGetAuthenticatorTypes();
            }
        });
    }

    public Account[] blockingGetAccounts() {
        ensureNotOnMainThread();
        try {
            return mService.getAccounts();
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
            throw new RuntimeException(e);
        }
    }

    public Account[] blockingGetAccountsByType(String accountType) {
        ensureNotOnMainThread();
        try {
            return mService.getAccountsByType(accountType);
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
            throw new RuntimeException(e);
        }
    }

    public Future1<Account[]> getAccounts(Future1Callback<Account[]> callback, Handler handler) {
        return startAsFuture(callback, handler, new Callable<Account[]>() {
            public Account[] call() throws Exception {
                return blockingGetAccounts();
            }
        });
    }

    public Future1<Account[]> getAccountsByType(Future1Callback<Account[]> callback,
            final String type, Handler handler) {
        return startAsFuture(callback, handler, new Callable<Account[]>() {
            public Account[] call() throws Exception {
                return blockingGetAccountsByType(type);
            }
        });
    }

    public boolean blockingAddAccountExplicitly(Account account, String password, Bundle extras) {
        ensureNotOnMainThread();
        try {
            return mService.addAccount(account, password, extras);
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
            throw new RuntimeException(e);
        }
    }

    public Future1<Boolean> addAccountExplicitly(final Future1Callback<Boolean> callback,
            final Account account, final String password, final Bundle extras,
            final Handler handler) {
        return startAsFuture(callback, handler, new Callable<Boolean>() {
            public Boolean call() throws Exception {
                return blockingAddAccountExplicitly(account, password, extras);
            }
        });
    }

    public void blockingRemoveAccount(Account account) {
        ensureNotOnMainThread();
        try {
            mService.removeAccount(account);
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
        }
    }

    public Future1<Void> removeAccount(Future1Callback<Void> callback, final Account account,
            final Handler handler) {
        return startAsFuture(callback, handler, new Callable<Void>() {
            public Void call() throws Exception {
                blockingRemoveAccount(account);
                return null;
            }
        });
    }

    public void blockingInvalidateAuthToken(String accountType, String authToken) {
        ensureNotOnMainThread();
        try {
            mService.invalidateAuthToken(accountType, authToken);
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
        }
    }

    public Future1<Void> invalidateAuthToken(Future1Callback<Void> callback,
            final String accountType, final String authToken, final Handler handler) {
        return startAsFuture(callback, handler, new Callable<Void>() {
            public Void call() throws Exception {
                blockingInvalidateAuthToken(accountType, authToken);
                return null;
            }
        });
    }

    public String blockingPeekAuthToken(Account account, String authTokenType) {
        ensureNotOnMainThread();
        try {
            return mService.peekAuthToken(account, authTokenType);
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
            throw new RuntimeException(e);
        }
    }

    public Future1<String> peekAuthToken(Future1Callback<String> callback,
            final Account account, final String authTokenType, final Handler handler) {
        return startAsFuture(callback, handler, new Callable<String>() {
            public String call() throws Exception {
                return blockingPeekAuthToken(account, authTokenType);
            }
        });
    }

    public void blockingSetPassword(Account account, String password) {
        ensureNotOnMainThread();
        try {
            mService.setPassword(account, password);
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
        }
    }

    public Future1<Void> setPassword(Future1Callback<Void> callback,
            final Account account, final String password, final Handler handler) {
        return startAsFuture(callback, handler, new Callable<Void>() {
            public Void call() throws Exception {
                blockingSetPassword(account, password);
                return null;
            }
        });
    }

    public void blockingClearPassword(Account account) {
        ensureNotOnMainThread();
        try {
            mService.clearPassword(account);
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
        }
    }

    public Future1<Void> clearPassword(final Future1Callback<Void> callback, final Account account,
            final Handler handler) {
        return startAsFuture(callback, handler, new Callable<Void>() {
            public Void call() throws Exception {
                blockingClearPassword(account);
                return null;
            }
        });
    }

    public void blockingSetUserData(Account account, String key, String value) {
        ensureNotOnMainThread();
        try {
            mService.setUserData(account, key, value);
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
        }
    }

    public Future1<Void> setUserData(Future1Callback<Void> callback,
            final Account account, final String key, final String value, final Handler handler) {
        return startAsFuture(callback, handler, new Callable<Void>() {
            public Void call() throws Exception {
                blockingSetUserData(account, key, value);
                return null;
            }
        });
    }

    public void blockingSetAuthToken(Account account, String authTokenType, String authToken) {
        ensureNotOnMainThread();
        try {
            mService.setAuthToken(account, authTokenType, authToken);
        } catch (RemoteException e) {
            // if this happens the entire runtime will restart
        }
    }

    public Future1<Void> setAuthToken(Future1Callback<Void> callback,
            final Account account, final String authTokenType, final String authToken,
            final Handler handler) {
        return startAsFuture(callback, handler, new Callable<Void>() {
            public Void call() throws Exception {
                blockingSetAuthToken(account, authTokenType, authToken);
                return null;
            }
        });
    }

    public String blockingGetAuthToken(Account account, String authTokenType,
            boolean notifyAuthFailure)
            throws OperationCanceledException, IOException, AuthenticatorException {
        ensureNotOnMainThread();
        Bundle bundle = getAuthToken(account, authTokenType, notifyAuthFailure, null /* callback */,
                null /* handler */).getResult();
        return bundle.getString(Constants.AUTHTOKEN_KEY);
    }

    /**
     * Request the auth token for this account/authTokenType. If this succeeds then the
     * auth token will then be passed to the activity. If this results in an authentication
     * failure then a login intent will be returned that can be invoked to prompt the user to
     * update their credentials. This login activity will return the auth token to the calling
     * activity. If activity is null then the login intent will not be invoked.
     *
     * @param account the account whose auth token should be retrieved
     * @param authTokenType the auth token type that should be retrieved
     * @param loginOptions
     * @param activity the activity to launch the login intent, if necessary, and to which
     */
    public Future2 getAuthToken(
            final Account account, final String authTokenType, final Bundle loginOptions,
            final Activity activity, Future2Callback callback, Handler handler) {
        if (activity == null) throw new IllegalArgumentException("activity is null");
        if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
        return new AmsTask(activity, handler, callback) {
            public void doWork() throws RemoteException {
                mService.getAuthToken(mResponse, account, authTokenType,
                        false /* notifyOnAuthFailure */, true /* expectActivityLaunch */,
                        loginOptions);
            }
        }.start();
    }

    public Future2 getAuthToken(
            final Account account, final String authTokenType, final boolean notifyAuthFailure,
            Future2Callback callback, Handler handler) {
        if (account == null) throw new IllegalArgumentException("account is null");
        if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
        return new AmsTask(null, handler, callback) {
            public void doWork() throws RemoteException {
                mService.getAuthToken(mResponse, account, authTokenType,
                        notifyAuthFailure, false /* expectActivityLaunch */, null /* options */);
            }
        }.start();
    }

    public Future2 addAccount(final String accountType,
            final String authTokenType, final String[] requiredFeatures,
            final Bundle addAccountOptions,
            final Activity activity, Future2Callback callback, Handler handler) {
        return new AmsTask(activity, handler, callback) {
            public void doWork() throws RemoteException {
                mService.addAcount(mResponse, accountType, authTokenType,
                        requiredFeatures, activity != null, addAccountOptions);
            }
        }.start();
    }

    /** @deprecated use {@link #confirmCredentials} instead */
    public Future1<Boolean> confirmPassword(final Account account, final String password,
            Future1Callback<Boolean> callback, Handler handler) {
        return new AMSTaskBoolean(handler, callback) {
            public void doWork() throws RemoteException {
                mService.confirmPassword(response, account, password);
            }
        };
    }

    public Account[] blockingGetAccountsWithTypeAndFeatures(String type, String[] features)
            throws AuthenticatorException, IOException, OperationCanceledException {
        Future2 future = getAccountsWithTypeAndFeatures(type, features,
                null /* callback */, null /* handler */);
        Bundle result = future.getResult();
        Parcelable[] accountsTemp = result.getParcelableArray(Constants.ACCOUNTS_KEY);
        if (accountsTemp == null) {
            throw new AuthenticatorException("accounts should not be null");
        }
        Account[] accounts = new Account[accountsTemp.length];
        for (int i = 0; i < accountsTemp.length; i++) {
            accounts[i] = (Account) accountsTemp[i];
        }
        return accounts;
    }

    public Future2 getAccountsWithTypeAndFeatures(
            final String type, final String[] features,
            Future2Callback callback, Handler handler) {
        if (type == null) throw new IllegalArgumentException("type is null");
        return new AmsTask(null /* activity */, handler, callback) {
            public void doWork() throws RemoteException {
                mService.getAccountsByTypeAndFeatures(mResponse, type, features);
            }
        }.start();
    }

    public Future2 confirmCredentials(final Account account, final Activity activity,
            final Future2Callback callback,
            final Handler handler) {
        return new AmsTask(activity, handler, callback) {
            public void doWork() throws RemoteException {
                mService.confirmCredentials(mResponse, account, activity != null);
            }
        }.start();
    }

    public Future2 updateCredentials(final Account account, final String authTokenType,
            final Bundle loginOptions, final Activity activity,
            final Future2Callback callback,
            final Handler handler) {
        return new AmsTask(activity, handler, callback) {
            public void doWork() throws RemoteException {
                mService.updateCredentials(mResponse, account, authTokenType, activity != null,
                        loginOptions);
            }
        }.start();
    }

    public Future2 editProperties(final String accountType, final Activity activity,
            final Future2Callback callback,
            final Handler handler) {
        return new AmsTask(activity, handler, callback) {
            public void doWork() throws RemoteException {
                mService.editProperties(mResponse, accountType, activity != null);
            }
        }.start();
    }

    private void ensureNotOnMainThread() {
        final Looper looper = Looper.myLooper();
        if (looper != null && looper == mContext.getMainLooper()) {
            // We really want to throw an exception here, but GTalkService exercises this
            // path quite a bit and needs some serious rewrite in order to work properly.
            //noinspection ThrowableInstanceNeverThrow
//            Log.e(TAG, "calling this from your main thread can lead to deadlock and/or ANRs",
//                    new Exception());
            // TODO(fredq) remove the log and throw this exception when the callers are fixed
//            throw new IllegalStateException(
//                    "calling this from your main thread can lead to deadlock");
        }
    }

    private void postToHandler(Handler handler, final Future2Callback callback,
            final Future2 future) {
        handler = handler == null ? mMainHandler : handler;
        handler.post(new Runnable() {
            public void run() {
                callback.run(future);
            }
        });
    }

    private void postToHandler(Handler handler, final OnAccountsUpdatedListener listener,
            final Account[] accounts) {
        handler = handler == null ? mMainHandler : handler;
        handler.post(new Runnable() {
            public void run() {
                listener.onAccountsUpdated(accounts);
            }
        });
    }

    private <V> void postToHandler(Handler handler, final Future1Callback<V> callback,
            final Future1<V> future) {
        handler = handler == null ? mMainHandler : handler;
        handler.post(new Runnable() {
            public void run() {
                callback.run(future);
            }
        });
    }

    private <V> Future1<V> startAsFuture(Future1Callback<V> callback, Handler handler,
            Callable<V> callable) {
        final FutureTaskWithCallback<V> task =
                new FutureTaskWithCallback<V>(callback, callable, handler);
        new Thread(task).start();
        return task;
    }

    private class FutureTaskWithCallback<V> extends FutureTask<V> implements Future1<V> {
        final Future1Callback<V> mCallback;
        final Handler mHandler;

        public FutureTaskWithCallback(Future1Callback<V> callback, Callable<V> callable,
                Handler handler) {
            super(callable);
            mCallback = callback;
            mHandler = handler;
        }

        protected void done() {
            if (mCallback != null) {
                postToHandler(mHandler, mCallback, this);
            }
        }

        public V internalGetResult(Long timeout, TimeUnit unit) throws OperationCanceledException {
            try {
                if (timeout == null) {
                    return get();
                } else {
                    return get(timeout, unit);
                }
            } catch (InterruptedException e) {
                // we will cancel the task below
            } catch (CancellationException e) {
                // we will cancel the task below
            } catch (TimeoutException e) {
                // we will cancel the task below
            } catch (ExecutionException e) {
                // this should never happen
                throw new IllegalStateException(e.getCause());
            } finally {
                cancel(true /* interruptIfRunning */);
            }
            throw new OperationCanceledException();
        }

        public V getResult() throws OperationCanceledException {
            return internalGetResult(null, null);
        }

        public V getResult(long timeout, TimeUnit unit) throws OperationCanceledException {
            return internalGetResult(null, null);
        }
    }

    private abstract class AmsTask extends FutureTask<Bundle> implements Future2 {
        final IAccountManagerResponse mResponse;
        final Handler mHandler;
        final Future2Callback mCallback;
        final Activity mActivity;
        final Thread mThread;
        public AmsTask(Activity activity, Handler handler, Future2Callback callback) {
            super(new Callable<Bundle>() {
                public Bundle call() throws Exception {
                    throw new IllegalStateException("this should never be called");
                }
            });

            mHandler = handler;
            mCallback = callback;
            mActivity = activity;
            mResponse = new Response();
            mThread = new Thread(new Runnable() {
                public void run() {
                    try {
                        doWork();
                    } catch (RemoteException e) {
                        // never happens
                    }
                }
            }, "AmsTask");
        }

        public final Future2 start() {
            mThread.start();
            return this;
        }

        public abstract void doWork() throws RemoteException;

        private Bundle internalGetResult(Long timeout, TimeUnit unit)
                throws OperationCanceledException, IOException, AuthenticatorException {
            try {
                if (timeout == null) {
                    return get();
                } else {
                    return get(timeout, unit);
                }
            } catch (CancellationException e) {
                throw new OperationCanceledException();
            } catch (TimeoutException e) {
                // fall through and cancel
            } catch (InterruptedException e) {
                // fall through and cancel
            } catch (ExecutionException e) {
                final Throwable cause = e.getCause();
                if (cause instanceof IOException) {
                    throw (IOException) cause;
                } else if (cause instanceof UnsupportedOperationException) {
                    throw new AuthenticatorException(cause);
                } else if (cause instanceof AuthenticatorException) {
                    throw (AuthenticatorException) cause;
                } else if (cause instanceof RuntimeException) {
                    throw (RuntimeException) cause;
                } else if (cause instanceof Error) {
                    throw (Error) cause;
                } else {
                    throw new IllegalStateException(cause);
                }
            } finally {
                cancel(true /* interrupt if running */);
            }
            throw new OperationCanceledException();
        }

        public Bundle getResult()
                throws OperationCanceledException, IOException, AuthenticatorException {
            return internalGetResult(null, null);
        }

        public Bundle getResult(long timeout, TimeUnit unit)
                throws OperationCanceledException, IOException, AuthenticatorException {
            return internalGetResult(timeout, unit);
        }

        protected void done() {
            if (mCallback != null) {
                postToHandler(mHandler, mCallback, this);
            }
        }

        /** Handles the responses from the AccountManager */
        private class Response extends IAccountManagerResponse.Stub {
            public void onResult(Bundle bundle) {
                Intent intent = bundle.getParcelable("intent");
                if (intent != null && mActivity != null) {
                    // since the user provided an Activity we will silently start intents
                    // that we see
                    mActivity.startActivity(intent);
                    // leave the Future running to wait for the real response to this request
                } else {
                    set(bundle);
                }
            }

            public void onError(int code, String message) {
                if (code == Constants.ERROR_CODE_CANCELED) {
                    // the authenticator indicated that this request was canceled, do so now
                    cancel(true /* mayInterruptIfRunning */);
                    return;
                }
                setException(convertErrorToException(code, message));
            }
        }

    }

    private abstract class AMSTaskBoolean extends FutureTask<Boolean> implements Future1<Boolean> {
        final IAccountManagerResponse response;
        final Handler mHandler;
        final Future1Callback<Boolean> mCallback;
        public AMSTaskBoolean(Handler handler, Future1Callback<Boolean> callback) {
            super(new Callable<Boolean>() {
                public Boolean call() throws Exception {
                    throw new IllegalStateException("this should never be called");
                }
            });

            mHandler = handler;
            mCallback = callback;
            response = new Response();

            new Thread(new Runnable() {
                public void run() {
                    try {
                        doWork();
                    } catch (RemoteException e) {
                        // never happens
                    }
                }
            }).start();
        }

        public abstract void doWork() throws RemoteException;


        protected void done() {
            if (mCallback != null) {
                postToHandler(mHandler, mCallback, this);
            }
        }

        private Boolean internalGetResult(Long timeout, TimeUnit unit) {
            try {
                if (timeout == null) {
                    return get();
                } else {
                    return get(timeout, unit);
                }
            } catch (InterruptedException e) {
                // fall through and cancel
            } catch (TimeoutException e) {
                // fall through and cancel
            } catch (CancellationException e) {
                return false;
            } catch (ExecutionException e) {
                final Throwable cause = e.getCause();
                if (cause instanceof IOException) {
                    return false;
                } else if (cause instanceof UnsupportedOperationException) {
                    return false;
                } else if (cause instanceof AuthenticatorException) {
                    return false;
                } else if (cause instanceof RuntimeException) {
                    throw (RuntimeException) cause;
                } else if (cause instanceof Error) {
                    throw (Error) cause;
                } else {
                    throw new IllegalStateException(cause);
                }
            } finally {
                cancel(true /* interrupt if running */);
            }
            return false;
        }

        public Boolean getResult() throws OperationCanceledException {
            return internalGetResult(null, null);
        }

        public Boolean getResult(long timeout, TimeUnit unit) throws OperationCanceledException {
            return internalGetResult(timeout, unit);
        }

        private class Response extends IAccountManagerResponse.Stub {
            public void onResult(Bundle bundle) {
                try {
                    if (bundle.containsKey(Constants.BOOLEAN_RESULT_KEY)) {
                        set(bundle.getBoolean(Constants.BOOLEAN_RESULT_KEY));
                        return;
                    }
                } catch (ClassCastException e) {
                    // we will set the exception below
                }
                onError(Constants.ERROR_CODE_INVALID_RESPONSE, "no result in response");
            }

            public void onError(int code, String message) {
                if (code == Constants.ERROR_CODE_CANCELED) {
                    cancel(true /* mayInterruptIfRunning */);
                    return;
                }
                setException(convertErrorToException(code, message));
            }
        }

    }

    private Exception convertErrorToException(int code, String message) {
        if (code == Constants.ERROR_CODE_NETWORK_ERROR) {
            return new IOException(message);
        }

        if (code == Constants.ERROR_CODE_UNSUPPORTED_OPERATION) {
            return new UnsupportedOperationException(message);
        }

        if (code == Constants.ERROR_CODE_INVALID_RESPONSE) {
            return new AuthenticatorException(message);
        }

        if (code == Constants.ERROR_CODE_BAD_ARGUMENTS) {
            return new IllegalArgumentException(message);
        }

        return new AuthenticatorException(message);
    }

    private class GetAuthTokenByTypeAndFeaturesTask extends AmsTask implements Future2Callback {
        GetAuthTokenByTypeAndFeaturesTask(final String accountType, final String authTokenType,
                final String[] features, Activity activityForPrompting,
                final Bundle addAccountOptions, final Bundle loginOptions,
                Future2Callback callback, Handler handler) {
            super(activityForPrompting, handler, callback);
            if (accountType == null) throw new IllegalArgumentException("account type is null");
            mAccountType = accountType;
            mAuthTokenType = authTokenType;
            mFeatures = features;
            mAddAccountOptions = addAccountOptions;
            mLoginOptions = loginOptions;
            mMyCallback = this;
        }
        volatile Future2 mFuture = null;
        final String mAccountType;
        final String mAuthTokenType;
        final String[] mFeatures;
        final Bundle mAddAccountOptions;
        final Bundle mLoginOptions;
        final Future2Callback mMyCallback;

        public void doWork() throws RemoteException {
            getAccountsWithTypeAndFeatures(mAccountType, mFeatures, new Future2Callback() {
                public void run(Future2 future) {
                    Bundle getAccountsResult;
                    try {
                        getAccountsResult = future.getResult();
                    } catch (OperationCanceledException e) {
                        setException(e);
                        return;
                    } catch (IOException e) {
                        setException(e);
                        return;
                    } catch (AuthenticatorException e) {
                        setException(e);
                        return;
                    }

                    Parcelable[] accounts =
                            getAccountsResult.getParcelableArray(Constants.ACCOUNTS_KEY);
                    if (accounts.length == 0) {
                        if (mActivity != null) {
                            // no accounts, add one now. pretend that the user directly
                            // made this request
                            mFuture = addAccount(mAccountType, mAuthTokenType, mFeatures,
                                    mAddAccountOptions, mActivity, mMyCallback, mHandler);
                        } else {
                            // send result since we can't prompt to add an account
                            Bundle result = new Bundle();
                            result.putString(Constants.ACCOUNT_NAME_KEY, null);
                            result.putString(Constants.ACCOUNT_TYPE_KEY, null);
                            result.putString(Constants.AUTHTOKEN_KEY, null);
                            try {
                                mResponse.onResult(result);
                            } catch (RemoteException e) {
                                // this will never happen
                            }
                            // we are done
                        }
                    } else if (accounts.length == 1) {
                        // have a single account, return an authtoken for it
                        if (mActivity == null) {
                            mFuture = getAuthToken((Account) accounts[0], mAuthTokenType,
                                    false /* notifyAuthFailure */, mMyCallback, mHandler);
                        } else {
                            mFuture = getAuthToken((Account) accounts[0],
                                    mAuthTokenType, mLoginOptions,
                                    mActivity, mMyCallback, mHandler);
                        }
                    } else {
                        if (mActivity != null) {
                            IAccountManagerResponse chooseResponse =
                                    new IAccountManagerResponse.Stub() {
                                public void onResult(Bundle value) throws RemoteException {
                                    Account account = new Account(
                                            value.getString(Constants.ACCOUNT_NAME_KEY),
                                            value.getString(Constants.ACCOUNT_TYPE_KEY));
                                    mFuture = getAuthToken(account, mAuthTokenType, mLoginOptions,
                                            mActivity, mMyCallback, mHandler);
                                }

                                public void onError(int errorCode, String errorMessage)
                                        throws RemoteException {
                                    mResponse.onError(errorCode, errorMessage);
                                }
                            };
                            // have many accounts, launch the chooser
                            Intent intent = new Intent();
                            intent.setClassName("android",
                                    "android.accounts.ChooseAccountActivity");
                            intent.putExtra(Constants.ACCOUNTS_KEY, accounts);
                            intent.putExtra(Constants.ACCOUNT_MANAGER_RESPONSE_KEY,
                                    new AccountManagerResponse(chooseResponse));
                            mActivity.startActivity(intent);
                            // the result will arrive via the IAccountManagerResponse
                        } else {
                            // send result since we can't prompt to select an account
                            Bundle result = new Bundle();
                            result.putString(Constants.ACCOUNTS_KEY, null);
                            try {
                                mResponse.onResult(result);
                            } catch (RemoteException e) {
                                // this will never happen
                            }
                            // we are done
                        }
                    }
                }}, mHandler);
        }



        // TODO(fredq) pass through the calls to our implemention of Future2 to the underlying
        // future that we create. We need to do things like have cancel cancel the mFuture, if set
        // or to cause this to be canceled if mFuture isn't set.
        // Once this is done then getAuthTokenByFeatures can be changed to return a Future2.

        public void run(Future2 future) {
            try {
                set(future.get());
            } catch (InterruptedException e) {
                cancel(true);
            } catch (CancellationException e) {
                cancel(true);
            } catch (ExecutionException e) {
                setException(e.getCause());
            }
        }
    }

    public void getAuthTokenByFeatures(
            final String accountType, final String authTokenType, final String[] features,
            final Activity activityForPrompting, final Bundle addAccountOptions,
            final Bundle loginOptions,
            final Future2Callback callback, final Handler handler) {
        if (accountType == null) throw new IllegalArgumentException("account type is null");
        if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null");
        new GetAuthTokenByTypeAndFeaturesTask(accountType, authTokenType,  features,
                activityForPrompting, addAccountOptions, loginOptions, callback, handler).start();
    }

    private final HashMap<OnAccountsUpdatedListener, Handler> mAccountsUpdatedListeners =
            Maps.newHashMap();

    // These variable are only used from the LOGIN_ACCOUNTS_CHANGED_ACTION BroadcastReceiver
    // and its getAccounts() callback which are both invoked only on the main thread. As a
    // result we don't need to protect against concurrent accesses and any changes are guaranteed
    // to be visible when used. Basically, these two variables are thread-confined.
    private Future1<Account[]> mAccountsLookupFuture = null;
    private boolean mAccountLookupPending = false;

    /**
     * BroadcastReceiver that listens for the LOGIN_ACCOUNTS_CHANGED_ACTION intent
     * so that it can read the updated list of accounts and send them to the listener
     * in mAccountsUpdatedListeners.
     */
    private final BroadcastReceiver mAccountsChangedBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(final Context context, final Intent intent) {
            if (mAccountsLookupFuture != null) {
                // an accounts lookup is already in progress,
                // don't bother starting another request
                mAccountLookupPending = true;
                return;
            }
            // initiate a read of the accounts
            mAccountsLookupFuture = getAccounts(new Future1Callback<Account[]>() {
                public void run(Future1<Account[]> future) {
                    // clear the future so that future receives will try the lookup again
                    mAccountsLookupFuture = null;

                    // get the accounts array
                    Account[] accounts;
                    try {
                        accounts = future.getResult();
                    } catch (OperationCanceledException e) {
                        // this should never happen, but if it does pretend we got another
                        // accounts changed broadcast
                        if (Config.LOGD) {
                            Log.d(TAG, "the accounts lookup for listener notifications was "
                                    + "canceled, try again by simulating the receipt of "
                                    + "a LOGIN_ACCOUNTS_CHANGED_ACTION broadcast");
                        }
                        onReceive(context, intent);
                        return;
                    }

                    // send the result to the listeners
                    synchronized (mAccountsUpdatedListeners) {
                        for (Map.Entry<OnAccountsUpdatedListener, Handler> entry :
                                mAccountsUpdatedListeners.entrySet()) {
                            Account[] accountsCopy = new Account[accounts.length];
                            // send the listeners a copy to make sure that one doesn't
                            // change what another sees
                            System.arraycopy(accounts, 0, accountsCopy, 0, accountsCopy.length);
                            postToHandler(entry.getValue(), entry.getKey(), accountsCopy);
                        }
                    }

                    // If mAccountLookupPending was set when the account lookup finished it
                    // means that we had previously ignored a LOGIN_ACCOUNTS_CHANGED_ACTION
                    // intent because a lookup was already in progress. Now that we are done
                    // with this lookup and notification pretend that another intent
                    // was received by calling onReceive() directly.
                    if (mAccountLookupPending) {
                        mAccountLookupPending = false;
                        onReceive(context, intent);
                        return;
                    }
                }
            }, mMainHandler);
        }
    };

    /**
     * Add a {@link OnAccountsUpdatedListener} to this instance of the {@link AccountManager}.
     * The listener is guaranteed to be invoked on the thread of the Handler that is passed
     * in or the main thread's Handler if handler is null.
     * @param listener the listener to add
     * @param handler the Handler whose thread will be used to invoke the listener. If null
     * the AccountManager context's main thread will be used.
     * @param updateImmediately if true then the listener will be invoked as a result of this
     * call.
     * @throws IllegalArgumentException if listener is null
     * @throws IllegalStateException if listener was already added
     */
    public void addOnAccountsUpdatedListener(final OnAccountsUpdatedListener listener,
            Handler handler, boolean updateImmediately) {
        if (listener == null) {
            throw new IllegalArgumentException("the listener is null");
        }
        synchronized (mAccountsUpdatedListeners) {
            if (mAccountsUpdatedListeners.containsKey(listener)) {
                throw new IllegalStateException("this listener is already added");
            }
            final boolean wasEmpty = mAccountsUpdatedListeners.isEmpty();

            mAccountsUpdatedListeners.put(listener, handler);

            if (wasEmpty) {
                // Register a broadcast receiver to monitor account changes
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction(Constants.LOGIN_ACCOUNTS_CHANGED_ACTION);
                mContext.registerReceiver(mAccountsChangedBroadcastReceiver, intentFilter);
            }
        }

        if (updateImmediately) {
            getAccounts(new Future1Callback<Account[]>() {
                public void run(Future1<Account[]> future) {
                    try {
                        listener.onAccountsUpdated(future.getResult());
                    } catch (OperationCanceledException e) {
                        // ignore
                    }
                }
            }, handler);
        }
    }

    /**
     * Remove an {@link OnAccountsUpdatedListener} that was previously registered with
     * {@link #addOnAccountsUpdatedListener}.
     * @param listener the listener to remove
     * @throws IllegalArgumentException if listener is null
     * @throws IllegalStateException if listener was not already added
     */
    public void removeOnAccountsUpdatedListener(OnAccountsUpdatedListener listener) {
        if (listener == null) {
            throw new IllegalArgumentException("the listener is null");
        }
        synchronized (mAccountsUpdatedListeners) {
            if (mAccountsUpdatedListeners.remove(listener) == null) {
                throw new IllegalStateException("this listener was not previously added");
            }
            if (mAccountsUpdatedListeners.isEmpty()) {
                mContext.unregisterReceiver(mAccountsChangedBroadcastReceiver);
            }
        }
    }
}