RecentFolderList.java revision bc748acc02add75ccd04691931464547d998ec08
1/**
2 * Copyright (c) 2011, Google Inc.
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 com.android.mail.ui;
18
19import android.content.ContentValues;
20import android.content.Context;
21import android.database.Cursor;
22import android.net.Uri;
23import android.os.AsyncTask;
24
25import com.android.mail.providers.Account;
26import com.android.mail.providers.Folder;
27import com.android.mail.utils.LogUtils;
28import com.android.mail.utils.LruCache;
29
30import java.util.ArrayList;
31import java.util.Collections;
32import java.util.Comparator;
33import java.util.List;
34
35/**
36 * A self-updating list of folder canonical names for the N most recently touched folders, ordered
37 * from least-recently-touched to most-recently-touched. N is a fixed size determined upon
38 * creation.
39 *
40 * RecentFoldersCache returns lists of this type, and will keep them updated when observers are
41 * registered on them.
42 *
43 */
44public final class RecentFolderList {
45    private static final String TAG = "RecentFolderList";
46    /** The application context */
47    private final Context mContext;
48    /** The current account */
49    private Account mAccount = null;
50
51    private final LruCache<String, Folder> mFolderCache;
52    /**
53     *  We want to show five recent folders, and one space for the current folder (not displayed
54     *  to the user).
55     */
56    private final static int NUM_FOLDERS = 5 + 1;
57
58    /**
59     * Compare based on alphanumeric name of the folder, ignoring case.
60     */
61    private static final Comparator<Folder> ALPHABET_IGNORECASE = new Comparator<Folder>() {
62        @Override
63        public int compare(Folder lhs, Folder rhs) {
64            return lhs.name.compareToIgnoreCase(rhs.name);
65        }
66    };
67    /**
68     * Class to store the recent folder list asynchronously.
69     */
70    private class StoreRecent extends AsyncTask<Void, Void, Void> {
71        final Account mAccount;
72        final Folder mFolder;
73
74        public StoreRecent(Account account, Folder folder) {
75            mAccount = account;
76            mFolder = folder;
77        }
78
79        @Override
80        protected Void doInBackground(Void... v) {
81            Uri uri = mAccount.recentFolderListUri;
82            if (uri != null) {
83                ContentValues values = new ContentValues();
84                values.put(mFolder.uri.toString(), System.currentTimeMillis());
85                // TODO: Remove when well tested
86                LogUtils.i(TAG, "Save: " + mFolder.name);
87                mContext.getContentResolver().update(uri, values, null, null);
88            }
89            return null;
90        }
91    }
92
93    /**
94     * Create a Recent Folder List from the given account. This will query the UIProvider to
95     * retrieve the RecentFolderList from persistent storage (if any).
96     * @param account
97     */
98    public RecentFolderList(Context context) {
99        mFolderCache = new LruCache<String, Folder>(NUM_FOLDERS);
100        mContext = context;
101    }
102
103    /**
104     * Change the current account. This causes the recent label list to be written out to the
105     * provider. When a cursor over the recent folders for this account is available, the client
106     * <b>must</b> call {@link #loadFromUiProvider(Cursor)} with the updated cursor. Till then,
107     * the recent account list will be empty.
108     * @param account
109     */
110    public void setCurrentAccount(Account account) {
111        mAccount = account;
112        // At some point in the future, the load method will return and populate our cache with
113        // useful entries. But for now, the cache is invalid.
114        mFolderCache.clear();
115    }
116
117    /**
118     * Load the account information from the UI provider given the cursor over the recent folders.
119     * @param data a cursor over the recent folders.
120     */
121    public void loadFromUiProvider(Cursor data) {
122        if (mAccount == null || data == null) {
123            return;
124        }
125        int i = 0;
126        while (data.moveToNext()) {
127            Folder folder = new Folder(data);
128            String folderUriString = folder.uri.toString();
129            mFolderCache.putElement(folderUriString, folder);
130            // TODO: Remove when well tested
131            LogUtils.i(TAG, "Account " + mAccount.name + ", Recent: " + folder.name);
132            i++;
133            if (i >= NUM_FOLDERS)
134                break;
135        }
136    }
137
138    /**
139     * Marks the given folder as 'accessed' by the user interface, its entry is updated in the
140     * recent folder list, and the current time is written to the provider
141     * @param folder the folder we have changed to.
142     */
143    public void touchFolder(Folder folder) {
144        mFolderCache.putElement(folder.uri.toString(), folder);
145        new StoreRecent(mAccount, folder).execute();
146    }
147
148    /**
149     * Generate a sorted array of recent folders, excluding the specified folders.
150     * @param exclude the folder to be excluded.
151     */
152    public Folder[] getSortedArray(Folder exclude) {
153        // TODO: Need to exclude default inbox folder as well!
154        final int spaceForCurrentFolder =
155                (exclude != null && mFolderCache.getElement(exclude.uri.toString()) != null)
156                        ? 1 : 0;
157        final int numRecents = mFolderCache.size() - spaceForCurrentFolder;
158        final Folder[] folders = new Folder[numRecents];
159        int i = 0;
160        final List<Folder> recent = new ArrayList<Folder>(mFolderCache.values());
161        Collections.sort(recent, ALPHABET_IGNORECASE);
162        for (Folder f : recent) {
163            if (exclude == null || !f.uri.equals(exclude.uri)) {
164                folders[i++] = f;
165            }
166        }
167        return folders;
168    }
169}
170