FileDocCategorySizeDatePackage
AccountMonitor.javaAPI DocAndroid 1.5 API6312Wed May 06 22:41:54 BST 2009android.accounts

AccountMonitor

public class AccountMonitor extends android.content.BroadcastReceiver implements android.content.ServiceConnection
A helper class that calls back on the provided AccountMonitorListener with the set of current accounts both when it gets created and whenever the set changes. It does this by binding to the AccountsService and registering to receive the intent broadcast when the set of accounts is changed. The connection to the accounts service is only made when it needs to fetch the current list of accounts (that is, when the AccountMonitor is first created, and when the intent is received).

Fields Summary
private final android.content.Context
mContext
private final AccountMonitorListener
mListener
private boolean
mClosed
private int
pending
Constructors Summary
public AccountMonitor(android.content.Context context, AccountMonitorListener listener)
Initializes the AccountMonitor and initiates a bind to the AccountsService to get the initial account list. For 1.0, the "list" is always a single account.

param
context the context we are running in
param
listener the user to notify when the account set changes

        if (listener == null) {
            throw new IllegalArgumentException("listener is null");
        }

        mContext = context;
        mListener = listener;

        // Register a broadcast receiver to monitor account changes
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(AccountsServiceConstants.LOGIN_ACCOUNTS_CHANGED_ACTION);
        intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);  // To recover from disk-full.
        mContext.registerReceiver(this, intentFilter);

        // Send the listener the initial state now.
        notifyListener();
    
Methods Summary
public synchronized voidclose()
Unregisters the account receiver. Consecutive calls to this method are harmless, but also do nothing. Once this call is made no more notifications will occur.

        if (!mClosed) {
            mContext.unregisterReceiver(this);
            mClosed = true;
        }
    
protected voidfinalize()
calls close()

throws
Throwable

        close();
        super.finalize();
    
private synchronized voidnotifyListener()

        if (pending == 0) {
            // initiate the bind
            if (!mContext.bindService(AccountsServiceConstants.SERVICE_INTENT,
                                      this, Context.BIND_AUTO_CREATE)) {
                // This is normal if GLS isn't part of this build.
                Log.w("AccountMonitor",
                      "Couldn't connect to "  +
                      AccountsServiceConstants.SERVICE_INTENT +
                      " (Missing service?)");
            }
        } else {
            // already bound.  bindService will not trigger another
            // call to onServiceConnected, so instead we make sure
            // that the existing background thread will call
            // getAccounts() after this function returns, by
            // incrementing pending.
            //
            // Yes, this else clause contains only a comment.
        }
        ++pending;
    
public voidonReceive(android.content.Context context, android.content.Intent intent)

        notifyListener();
    
public voidonServiceConnected(android.content.ComponentName className, android.os.IBinder service)

        // Create a background thread to update the accounts.
        new AccountUpdater(service).start();
    
public voidonServiceDisconnected(android.content.ComponentName className)