RecentMailboxManager.java revision f04c832f23b14aa26bf584eb9a82f9bc06279524
1/*
2 * Copyright (C) 2011 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 com.android.email.activity;
18
19import com.google.common.annotations.VisibleForTesting;
20
21import android.content.ContentValues;
22import android.content.Context;
23import android.database.Cursor;
24
25import com.android.email.Clock;
26import com.android.emailcommon.provider.EmailContent;
27import com.android.emailcommon.provider.Mailbox;
28import com.android.emailcommon.provider.EmailContent.MailboxColumns;
29import com.android.emailcommon.utility.EmailAsyncTask;
30
31import java.util.ArrayList;
32
33/**
34 * Manages recent data for mailboxes.
35 */
36public class RecentMailboxManager {
37    @VisibleForTesting
38    static Clock sClock = Clock.INSTANCE;
39
40    /** The maximum number of results to retrieve */
41    private static final int LIMIT_RESULTS = 5;
42    /** Query to find the top most recent mailboxes */
43    private static final String RECENT_SELECTION =
44            MailboxColumns.ID + " IN " +
45            "( SELECT " + MailboxColumns.ID
46            + " FROM " + Mailbox.TABLE_NAME
47            + " WHERE ( " + MailboxColumns.ACCOUNT_KEY + "=? "
48            +     " AND " + MailboxColumns.LAST_TOUCHED_TIME + ">0 )"
49            + " ORDER BY " + MailboxColumns.LAST_TOUCHED_TIME + " DESC"
50            + " LIMIT ? )";
51    /** Similar query to {@link #RECENT_SELECTION}, except, exclude all but user mailboxes */
52    private static final String RECENT_SELECTION_WITH_EXCLUSIONS =
53            RECENT_SELECTION + " AND " + MailboxColumns.TYPE + "=" + Mailbox.TYPE_MAIL;
54    private final Context mContext;
55    private static RecentMailboxManager sInstance;
56
57    public static synchronized RecentMailboxManager getInstance(Context context) {
58        if (sInstance == null) {
59            sInstance = new RecentMailboxManager(context);
60        }
61        return sInstance;
62    }
63
64    /** Hide constructor */
65    private RecentMailboxManager(Context context) {
66        mContext = context;
67    }
68
69    /** Updates the specified mailbox's touch time. Returns an async task for test only. */
70    public EmailAsyncTask<Void, Void, Void> touch(long mailboxId) {
71        return fireAndForget(mailboxId, sClock.getTime());
72    }
73
74    /**
75     * Gets the most recently touched mailboxes for the specified account.
76     * <p><em>WARNING</em>: This method blocks on the database.
77     * @param accountId The ID of the account to load the recent list.
78     * @param withExclusions If {@code false}, all mailboxes are eligible for the recent list.
79     *          Otherwise, only user defined mailboxes are eligible for the recent list.
80     */
81    public ArrayList<Long> getMostRecent(long accountId, boolean withExclusions) {
82        String selection = withExclusions ? RECENT_SELECTION_WITH_EXCLUSIONS : RECENT_SELECTION;
83        ArrayList<Long> returnList = new ArrayList<Long>();
84        Cursor cursor = mContext.getContentResolver().query(Mailbox.CONTENT_URI,
85            EmailContent.ID_PROJECTION,
86            selection,
87            new String[] { Long.toString(accountId), Integer.toString(LIMIT_RESULTS) },
88            MailboxColumns.DISPLAY_NAME);
89        try {
90            while (cursor.moveToNext()) {
91                returnList.add(cursor.getLong(EmailContent.ID_PROJECTION_COLUMN));
92            }
93        } finally {
94            cursor.close();
95        }
96        return returnList;
97    }
98
99    /** Updates the last touched time for the mailbox in the background */
100    private EmailAsyncTask<Void, Void, Void> fireAndForget(final long mailboxId, final long time) {
101        return EmailAsyncTask.runAsyncParallel(new Runnable() {
102            @Override
103            public void run() {
104                ContentValues values = new ContentValues();
105                values.put(MailboxColumns.LAST_TOUCHED_TIME, time);
106                mContext.getContentResolver().update(Mailbox.CONTENT_URI, values,
107                    EmailContent.ID_SELECTION,
108                    new String[] { Long.toString(mailboxId) });
109            }
110        });
111    }
112}
113