EmailWidgetLoader.java revision 44f5cd67c97da6a5c7e63a73b4dca7057b83cdbb
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.Message; 22import com.android.emailcommon.provider.EmailContent.MessageColumns; 23import com.android.emailcommon.provider.Mailbox; 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 */ 38class 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 private long mAccountId; 61 private long mMailboxId; 62 63 /** 64 * The actual data returned by this loader. 65 */ 66 static class CursorWithCounts extends CursorWrapper { 67 private final int mMessageCount; 68 69 public CursorWithCounts(Cursor cursor, int messageCount) { 70 super(cursor); 71 mMessageCount = messageCount; 72 } 73 74 /** 75 * @return The count that should be shown on the widget header. 76 * Note depending on the view, it may be the unread count, which is different from 77 * the record count (i.e. {@link #getCount()}}. 78 */ 79 public int getMessageCount() { 80 return mMessageCount; 81 } 82 } 83 84 private final Context mContext; 85 86 EmailWidgetLoader(Context context) { 87 super(context, Message.CONTENT_URI, WIDGET_PROJECTION, null, 88 null, SORT_TIMESTAMP_DESCENDING); 89 mContext = context; 90 } 91 92 @Override 93 public Cursor loadInBackground() { 94 final Cursor messagesCursor = super.loadInBackground(); 95 96 // Reset the notification Uri to our Message table notifier URI 97 messagesCursor.setNotificationUri(mContext.getContentResolver(), Message.NOTIFIER_URI); 98 99 final int messageCount; 100 if (mMailboxId != Mailbox.QUERY_ALL_FAVORITES) { 101 String selection = "(" + getSelection() + " ) AND " + MessageColumns.FLAG_READ + " = 0"; 102 messageCount = EmailContent.count(mContext, Message.CONTENT_URI, selection, 103 getSelectionArgs()); 104 } else { 105 // Just use the number of all messages shown. 106 messageCount = messagesCursor.getCount(); 107 } 108 109 return new CursorWithCounts(messagesCursor, 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 accountId 118 * @param mailboxId 119 */ 120 void load(long accountId, long mailboxId) { 121 reset(); 122 mAccountId = accountId; 123 mMailboxId = mailboxId; 124 setSelection(Message.PER_ACCOUNT_UNREAD_SELECTION); 125 setSelectionArgs(new String[]{ Long.toString(mAccountId) }); 126 startLoading(); 127 } 128} 129