1/*
2 * Copyright (C) 2013 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
18package com.android.mail.providers;
19
20import com.android.mail.ui.AccountController;
21import com.android.mail.utils.LogTag;
22import com.android.mail.utils.LogUtils;
23
24import android.database.DataSetObserver;
25
26/**
27 * A simple extension of {@link android.database.DataSetObserver} to provide all Accounts in
28 * {@link #onChanged(Account[])} when the list of Accounts changes. Initializing the object
29 * registers with the observer with the {@link com.android.mail.ui.AccountController} provided.
30 * The object will then begin to receive {@link #onChanged(Account[])} till {@link
31 * #unregisterAndDestroy()} is called. <p> To implement an {@link com.android.mail.providers
32 * .AllAccountObserver}, you need to implement the {@link #onChanged(Account[])} method.
33 */
34public abstract class AllAccountObserver extends DataSetObserver {
35    /**
36     * The AccountController that the observer is registered with.
37     */
38    private AccountController mController;
39
40    private static final String LOG_TAG = LogTag.getLogTag();
41
42    /**
43     * The no-argument constructor leaves the object unusable till
44     * {@link #initialize(com.android.mail.ui.AccountController)} is called.
45     */
46    public AllAccountObserver() {
47    }
48
49    /**
50     * Initializes an {@link com.android.mail.providers.AllAccountObserver} object that receives
51     * a call to {@link #onChanged(Account[])} when the controller changes the list of accounts.
52     *
53     * @param controller
54     */
55    public Account[] initialize(AccountController controller) {
56        if (controller == null) {
57            LogUtils.wtf(LOG_TAG, "AllAccountObserver initialized with null controller!");
58        }
59        mController = controller;
60        mController.registerAllAccountObserver(this);
61        return mController.getAllAccounts();
62    }
63
64    @Override
65    public final void onChanged() {
66        if (mController == null) {
67            return;
68        }
69        onChanged(mController.getAllAccounts());
70    }
71
72    /**
73     * Callback invoked when the list of Accounts changes.
74     * The updated list is passed as the argument.
75     * @param allAccounts the array of all accounts in the current application.
76     */
77    public abstract void onChanged(Account[] allAccounts);
78
79    /**
80     * Return the array of existing accounts.
81     * @return the array of existing accounts.
82     */
83    public final Account[] getAllAccounts() {
84        if (mController == null) {
85            return null;
86        }
87        return mController.getAllAccounts();
88    }
89
90    /**
91     * Unregisters for list of Account changes and makes the object unusable.
92     */
93    public void unregisterAndDestroy() {
94        if (mController == null) {
95            return;
96        }
97        mController.unregisterAllAccountObserver(this);
98    }
99}
100