EmailWidgetLoader.java revision 897a0ea81c8cddcb142d6ac7f7c47801858c8537
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.widget;
18
19import com.android.email.data.ThrottlingCursorLoader;
20import com.android.emailcommon.provider.EmailContent;
21import com.android.emailcommon.provider.EmailContent.Account;
22import com.android.emailcommon.provider.EmailContent.Message;
23import com.android.emailcommon.provider.EmailContent.MessageColumns;
24
25import android.content.Context;
26import android.database.Cursor;
27import android.database.CursorWrapper;
28
29/**
30 * Loader for {@link EmailWidget}.
31 *
32 * This loader not only loads the messages, but also:
33 * - The number of accounts.
34 * - The message count shown in the widget header.
35 *   It's currently just the same as the message count, but this will be updated to the unread
36 *   counts for inboxes.
37 */
38/* package */ class EmailWidgetLoader extends ThrottlingCursorLoader {
39    private static final String SORT_TIMESTAMP_DESCENDING = MessageColumns.TIMESTAMP + " DESC";
40
41    // The projection to be used by the WidgetLoader
42    private static final String[] WIDGET_PROJECTION = new String[] {
43            EmailContent.RECORD_ID, MessageColumns.DISPLAY_NAME, MessageColumns.TIMESTAMP,
44            MessageColumns.SUBJECT, MessageColumns.FLAG_READ, MessageColumns.FLAG_FAVORITE,
45            MessageColumns.FLAG_ATTACHMENT, MessageColumns.MAILBOX_KEY, MessageColumns.SNIPPET,
46            MessageColumns.ACCOUNT_KEY, MessageColumns.FLAGS
47            };
48    public static final int WIDGET_COLUMN_ID = 0;
49    public static final int WIDGET_COLUMN_DISPLAY_NAME = 1;
50    public static final int WIDGET_COLUMN_TIMESTAMP = 2;
51    public static final int WIDGET_COLUMN_SUBJECT = 3;
52    public static final int WIDGET_COLUMN_FLAG_READ = 4;
53    public static final int WIDGET_COLUMN_FLAG_FAVORITE = 5;
54    public static final int WIDGET_COLUMN_FLAG_ATTACHMENT = 6;
55    public static final int WIDGET_COLUMN_MAILBOX_KEY = 7;
56    public static final int WIDGET_COLUMN_SNIPPET = 8;
57    public static final int WIDGET_COLUMN_ACCOUNT_KEY = 9;
58    public static final int WIDGET_COLUMN_FLAGS = 10;
59
60    /**
61     * The actual data returned by this loader.
62     */
63    public static class CursorWithCounts extends CursorWrapper {
64        private final int mAccountCount;
65        private final int mMessageCount;
66
67        public CursorWithCounts(Cursor cursor, int accountCount, int messageCount) {
68            super(cursor);
69            mAccountCount = accountCount;
70            mMessageCount = messageCount;
71        }
72
73        public int getAccountCount() {
74            return mAccountCount;
75        }
76
77        /**
78         * @return The count that should be shown on the widget header.
79         *     Note depending on the view, it may be the unread count, which is different from
80         *     the record count (i.e. {@link #getCount()}}.
81         */
82        public int getMessageCount() {
83            return mMessageCount;
84        }
85    }
86
87    private final Context mContext;
88
89    private WidgetView mLoadingWidgetView;
90
91    public EmailWidgetLoader(Context context) {
92        super(context, Message.CONTENT_URI, WIDGET_PROJECTION, null,
93                null, SORT_TIMESTAMP_DESCENDING);
94        mContext = context;
95    }
96
97    @Override
98    public Cursor loadInBackground() {
99        final Cursor messagesCursor = super.loadInBackground();
100
101        // Reset the notification Uri to our Message table notifier URI
102        messagesCursor.setNotificationUri(mContext.getContentResolver(), Message.NOTIFIER_URI);
103
104        final int accountCount = EmailContent.count(mContext, Account.CONTENT_URI);
105
106        // TODO Use correct count -- e.g. unread count for inboxes, not total count.
107        final int messageCount = messagesCursor.getCount();
108
109        return new CursorWithCounts(messagesCursor, accountCount, messageCount);
110    }
111
112    /**
113     * Stop any pending load, reset selection parameters, and start loading.
114     *
115     * Must be called from the UI thread
116     *
117     * @param view the current ViewType
118     */
119    public void load(WidgetView view) {
120        reset();
121        mLoadingWidgetView = view;
122        setSelection(view.getSelection(mContext));
123        setSelectionArgs(view.getSelectionArgs());
124        startLoading();
125    }
126
127    /**
128     * @return the {@link WidgetView} that is (being) loaded.
129     *
130     * Must be called from the UI thread
131     */
132    public WidgetView getLoadingWidgetView() {
133        return mLoadingWidgetView;
134    }
135}
136