blob: 38032c59ba661b2b592c816356b74f2cb8c25ec4 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
17package android.accounts;
18
19import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
Fred Quintana60307342009-03-24 22:48:12 -070023import android.os.*;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25/**
26 * A helper class that calls back on the provided
27 * AccountMonitorListener with the set of current accounts both when
28 * it gets created and whenever the set changes. It does this by
29 * binding to the AccountsService and registering to receive the
30 * intent broadcast when the set of accounts is changed. The
31 * connection to the accounts service is only made when it needs to
32 * fetch the current list of accounts (that is, when the
33 * AccountMonitor is first created, and when the intent is received).
34 */
Fred Quintana60307342009-03-24 22:48:12 -070035public class AccountMonitor extends BroadcastReceiver {
36 private static final String TAG = "AccountMonitor";
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 private final Context mContext;
39 private final AccountMonitorListener mListener;
40 private boolean mClosed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Fred Quintana60307342009-03-24 22:48:12 -070042 private volatile Looper mServiceLooper;
43 private volatile NotifierHandler mServiceHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45 /**
46 * Initializes the AccountMonitor and initiates a bind to the
47 * AccountsService to get the initial account list. For 1.0,
48 * the "list" is always a single account.
49 *
50 * @param context the context we are running in
51 * @param listener the user to notify when the account set changes
52 */
53 public AccountMonitor(Context context, AccountMonitorListener listener) {
54 if (listener == null) {
55 throw new IllegalArgumentException("listener is null");
56 }
57
58 mContext = context;
59 mListener = listener;
60
61 // Register a broadcast receiver to monitor account changes
62 IntentFilter intentFilter = new IntentFilter();
Fred Quintanaa698f422009-04-08 19:14:54 -070063 intentFilter.addAction(Constants.LOGIN_ACCOUNTS_CHANGED_ACTION);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK); // To recover from disk-full.
65 mContext.registerReceiver(this, intentFilter);
66
Fred Quintana60307342009-03-24 22:48:12 -070067 HandlerThread thread = new HandlerThread("AccountMonitorHandlerThread");
68 thread.start();
69 mServiceLooper = thread.getLooper();
70 mServiceHandler = new NotifierHandler(mServiceLooper);
71
72 mServiceHandler.sendEmptyMessage(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 }
74
75 @Override
76 public void onReceive(Context context, Intent intent) {
77 notifyListener();
78 }
79
Fred Quintanaa698f422009-04-08 19:14:54 -070080 private Future1Callback<Account[]> mGetAccountsCallback = new Future1Callback<Account[]>() {
81 public void run(Future1<Account[]> future) {
82 try {
83 Account[] accounts = future.getResult();
84 String[] accountNames = new String[accounts.length];
85 for (int i = 0; i < accounts.length; i++) {
86 accountNames[i] = accounts[i].mName;
87 }
88 mListener.onAccountsUpdated(accountNames);
89 } catch (OperationCanceledException e) {
90 // the request was canceled
91 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 }
Fred Quintanaa698f422009-04-08 19:14:54 -070093 };
94
95 private synchronized void notifyListener() {
96 AccountManager.get(mContext).getAccounts(mGetAccountsCallback, null /* handler */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
98
99 /**
100 * Unregisters the account receiver. Consecutive calls to this
101 * method are harmless, but also do nothing. Once this call is
102 * made no more notifications will occur.
103 */
104 public synchronized void close() {
105 if (!mClosed) {
106 mContext.unregisterReceiver(this);
107 mClosed = true;
108 }
109 }
Fred Quintana60307342009-03-24 22:48:12 -0700110
111 private final class NotifierHandler extends Handler {
112 public NotifierHandler(Looper looper) {
113 super(looper);
114 }
115
116 public void handleMessage(Message msg) {
117 notifyListener();
118 }
119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120}