1/*
2 * Copyright (C) 2012 Google Inc.
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mail.providers;
19
20import android.database.DataSetObserver;
21
22import com.android.mail.ui.RecentFolderController;
23import com.android.mail.ui.RecentFolderList;
24import com.android.mail.utils.LogTag;
25import com.android.mail.utils.LogUtils;
26
27/**
28 * A simple extension of {@link DataSetObserver} to provide the updated recent folders
29 * {@link #onChanged()} when they change. Initializing the object registers with
30 * the observer with the {@link RecentFolderObserver} provided. The object will then begin to
31 * receive {@link #onChanged()} till {@link #unregisterAndDestroy()} is called.
32 * The {@link RecentFolderList} returned in {@link #initialize(RecentFolderController)} is updated
33 * directly, no new object is created when the recent folder list is updated.
34 * <p>
35 * To implement an {@link RecentFolderObserver}, you need to implement the
36 * {@link #onChanged()} method.
37 */
38public abstract class RecentFolderObserver extends DataSetObserver {
39    /**
40     * The RecentFolderController that the observer is registered with.
41     */
42    private RecentFolderController mController;
43
44    private static final String LOG_TAG = LogTag.getLogTag();
45
46    /**
47     * The no-argument constructor leaves the object unusable till
48     * {@link #initialize(RecentFolderController)} is called.
49     */
50    public RecentFolderObserver () {
51    }
52
53    /**
54     * Initializes an {@link RecentFolderObserver} object that receives a call to
55     * {@link #onChanged()} when the controller changes the recent folder.
56     *
57     * @param controller
58     */
59    public RecentFolderList initialize(RecentFolderController controller) {
60        if (controller == null) {
61            LogUtils.wtf(LOG_TAG, "RecentFolderObserver initialized with null controller!");
62        }
63        mController = controller;
64        mController.registerRecentFolderObserver(this);
65        return mController.getRecentFolders();
66    }
67
68    @Override
69    public abstract void onChanged();
70
71    /**
72     * Return the most current recent folder.
73     * @return
74     */
75    public final RecentFolderList getRecentFolders() {
76        if (mController == null) {
77            return null;
78        }
79        return mController.getRecentFolders();
80    }
81
82    /**
83     * Unregisters for recent folder changes and makes the object unusable.
84     */
85    public void unregisterAndDestroy() {
86        if (mController == null) {
87            return;
88        }
89        mController.unregisterRecentFolderObserver(this);
90    }
91}