2011-05-14 00:26:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
package com.android.emailcommon.provider;
|
|
|
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2011-05-14 01:35:08 +00:00
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
2011-05-14 00:26:27 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2011-06-13 21:57:17 +00:00
|
|
|
import com.android.emailcommon.Logging;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.MailboxColumns;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.SyncColumns;
|
|
|
|
import com.android.emailcommon.utility.Utility;
|
|
|
|
|
2011-05-14 01:35:08 +00:00
|
|
|
public class Mailbox extends EmailContent implements SyncColumns, MailboxColumns, Parcelable {
|
2011-05-14 00:26:27 +00:00
|
|
|
public static final String TABLE_NAME = "Mailbox";
|
2012-08-23 05:25:42 +00:00
|
|
|
|
|
|
|
public static Uri CONTENT_URI;
|
|
|
|
public static Uri ADD_TO_FIELD_URI;
|
|
|
|
public static Uri FROM_ACCOUNT_AND_TYPE_URI;
|
|
|
|
|
|
|
|
public static void initMailbox() {
|
|
|
|
CONTENT_URI = Uri.parse(EmailContent.CONTENT_URI + "/mailbox");
|
|
|
|
ADD_TO_FIELD_URI = Uri.parse(EmailContent.CONTENT_URI + "/mailboxIdAddToField");
|
|
|
|
FROM_ACCOUNT_AND_TYPE_URI = Uri.parse(EmailContent.CONTENT_URI +
|
|
|
|
"/mailboxIdFromAccountAndType");
|
|
|
|
}
|
2011-05-14 00:26:27 +00:00
|
|
|
|
|
|
|
public String mDisplayName;
|
|
|
|
public String mServerId;
|
|
|
|
public String mParentServerId;
|
|
|
|
public long mParentKey;
|
|
|
|
public long mAccountKey;
|
|
|
|
public int mType;
|
|
|
|
public int mDelimiter;
|
|
|
|
public String mSyncKey;
|
|
|
|
public int mSyncLookback;
|
|
|
|
public int mSyncInterval;
|
|
|
|
public long mSyncTime;
|
|
|
|
public boolean mFlagVisible = true;
|
|
|
|
public int mFlags;
|
|
|
|
public int mVisibleLimit;
|
|
|
|
public String mSyncStatus;
|
2011-06-03 15:51:25 +00:00
|
|
|
public long mLastTouchedTime;
|
2012-06-28 17:40:46 +00:00
|
|
|
public int mUiSyncStatus;
|
|
|
|
public int mUiLastSyncResult;
|
|
|
|
public int mTotalCount;
|
|
|
|
public String mHierarchicalName;
|
2011-05-14 00:26:27 +00:00
|
|
|
|
|
|
|
public static final int CONTENT_ID_COLUMN = 0;
|
|
|
|
public static final int CONTENT_DISPLAY_NAME_COLUMN = 1;
|
|
|
|
public static final int CONTENT_SERVER_ID_COLUMN = 2;
|
|
|
|
public static final int CONTENT_PARENT_SERVER_ID_COLUMN = 3;
|
|
|
|
public static final int CONTENT_ACCOUNT_KEY_COLUMN = 4;
|
|
|
|
public static final int CONTENT_TYPE_COLUMN = 5;
|
|
|
|
public static final int CONTENT_DELIMITER_COLUMN = 6;
|
|
|
|
public static final int CONTENT_SYNC_KEY_COLUMN = 7;
|
|
|
|
public static final int CONTENT_SYNC_LOOKBACK_COLUMN = 8;
|
|
|
|
public static final int CONTENT_SYNC_INTERVAL_COLUMN = 9;
|
|
|
|
public static final int CONTENT_SYNC_TIME_COLUMN = 10;
|
|
|
|
public static final int CONTENT_FLAG_VISIBLE_COLUMN = 11;
|
|
|
|
public static final int CONTENT_FLAGS_COLUMN = 12;
|
|
|
|
public static final int CONTENT_VISIBLE_LIMIT_COLUMN = 13;
|
|
|
|
public static final int CONTENT_SYNC_STATUS_COLUMN = 14;
|
|
|
|
public static final int CONTENT_PARENT_KEY_COLUMN = 15;
|
2012-06-28 17:40:46 +00:00
|
|
|
public static final int CONTENT_LAST_TOUCHED_TIME_COLUMN = 16;
|
|
|
|
public static final int CONTENT_UI_SYNC_STATUS_COLUMN = 17;
|
|
|
|
public static final int CONTENT_UI_LAST_SYNC_RESULT_COLUMN = 18;
|
2012-12-11 18:37:35 +00:00
|
|
|
public static final int CONTENT_TOTAL_COUNT_COLUMN = 19;
|
|
|
|
public static final int CONTENT_HIERARCHICAL_NAME_COLUMN = 20;
|
2011-05-14 00:26:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <em>NOTE</em>: If fields are added or removed, the method {@link #getHashes()}
|
|
|
|
* MUST be updated.
|
|
|
|
*/
|
|
|
|
public static final String[] CONTENT_PROJECTION = new String[] {
|
|
|
|
RECORD_ID, MailboxColumns.DISPLAY_NAME, MailboxColumns.SERVER_ID,
|
|
|
|
MailboxColumns.PARENT_SERVER_ID, MailboxColumns.ACCOUNT_KEY, MailboxColumns.TYPE,
|
|
|
|
MailboxColumns.DELIMITER, MailboxColumns.SYNC_KEY, MailboxColumns.SYNC_LOOKBACK,
|
|
|
|
MailboxColumns.SYNC_INTERVAL, MailboxColumns.SYNC_TIME,
|
|
|
|
MailboxColumns.FLAG_VISIBLE, MailboxColumns.FLAGS, MailboxColumns.VISIBLE_LIMIT,
|
2012-06-28 17:40:46 +00:00
|
|
|
MailboxColumns.SYNC_STATUS, MailboxColumns.PARENT_KEY, MailboxColumns.LAST_TOUCHED_TIME,
|
|
|
|
MailboxColumns.UI_SYNC_STATUS, MailboxColumns.UI_LAST_SYNC_RESULT,
|
|
|
|
MailboxColumns.TOTAL_COUNT, MailboxColumns.HIERARCHICAL_NAME
|
2011-05-14 00:26:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private static final String ACCOUNT_AND_MAILBOX_TYPE_SELECTION =
|
|
|
|
MailboxColumns.ACCOUNT_KEY + " =? AND " +
|
|
|
|
MailboxColumns.TYPE + " =?";
|
|
|
|
private static final String MAILBOX_TYPE_SELECTION =
|
|
|
|
MailboxColumns.TYPE + " =?";
|
|
|
|
/** Selection by server pathname for a given account */
|
2011-06-23 17:52:21 +00:00
|
|
|
public static final String PATH_AND_ACCOUNT_SELECTION =
|
2011-05-14 00:26:27 +00:00
|
|
|
MailboxColumns.SERVER_ID + "=? and " + MailboxColumns.ACCOUNT_KEY + "=?";
|
|
|
|
|
|
|
|
private static final String[] MAILBOX_SUM_OF_UNREAD_COUNT_PROJECTION = new String [] {
|
|
|
|
"sum(" + MailboxColumns.UNREAD_COUNT + ")"
|
|
|
|
};
|
|
|
|
private static final int UNREAD_COUNT_COUNT_COLUMN = 0;
|
|
|
|
private static final String[] MAILBOX_SUM_OF_MESSAGE_COUNT_PROJECTION = new String [] {
|
|
|
|
"sum(" + MailboxColumns.MESSAGE_COUNT + ")"
|
|
|
|
};
|
|
|
|
private static final int MESSAGE_COUNT_COUNT_COLUMN = 0;
|
|
|
|
|
|
|
|
private static final String[] MAILBOX_TYPE_PROJECTION = new String [] {
|
|
|
|
MailboxColumns.TYPE
|
|
|
|
};
|
|
|
|
private static final int MAILBOX_TYPE_TYPE_COLUMN = 0;
|
|
|
|
|
|
|
|
private static final String[] MAILBOX_DISPLAY_NAME_PROJECTION = new String [] {
|
|
|
|
MailboxColumns.DISPLAY_NAME
|
|
|
|
};
|
|
|
|
private static final int MAILBOX_DISPLAY_NAME_COLUMN = 0;
|
|
|
|
|
|
|
|
public static final long NO_MAILBOX = -1;
|
|
|
|
|
|
|
|
// Sentinel values for the mSyncInterval field of both Mailbox records
|
|
|
|
public static final int CHECK_INTERVAL_NEVER = -1;
|
|
|
|
public static final int CHECK_INTERVAL_PUSH = -2;
|
|
|
|
// The following two sentinel values are used by EAS
|
|
|
|
// Ping indicates that the EAS mailbox is synced based on a "ping" from the server
|
|
|
|
public static final int CHECK_INTERVAL_PING = -3;
|
|
|
|
// Push-Hold indicates an EAS push or ping Mailbox shouldn't sync just yet
|
|
|
|
public static final int CHECK_INTERVAL_PUSH_HOLD = -4;
|
|
|
|
|
2011-05-17 17:50:30 +00:00
|
|
|
// Sentinel for PARENT_KEY. Use NO_MAILBOX for toplevel mailboxes (i.e. no parents).
|
2011-05-14 00:26:27 +00:00
|
|
|
public static final long PARENT_KEY_UNINITIALIZED = 0L;
|
|
|
|
|
|
|
|
private static final String WHERE_TYPE_AND_ACCOUNT_KEY =
|
|
|
|
MailboxColumns.TYPE + "=? and " + MailboxColumns.ACCOUNT_KEY + "=?";
|
|
|
|
|
|
|
|
public static final Integer[] INVALID_DROP_TARGETS = new Integer[] {Mailbox.TYPE_DRAFTS,
|
|
|
|
Mailbox.TYPE_OUTBOX, Mailbox.TYPE_SENT};
|
|
|
|
|
|
|
|
public static final String USER_VISIBLE_MAILBOX_SELECTION =
|
|
|
|
MailboxColumns.TYPE + "<" + Mailbox.TYPE_NOT_EMAIL +
|
|
|
|
" AND " + MailboxColumns.FLAG_VISIBLE + "=1";
|
|
|
|
|
|
|
|
// Types of mailboxes. The list is ordered to match a typical UI presentation, e.g.
|
|
|
|
// placing the inbox at the top.
|
|
|
|
// Arrays of "special_mailbox_display_names" and "special_mailbox_icons" are depends on
|
|
|
|
// types Id of mailboxes.
|
|
|
|
/** No type specified */
|
|
|
|
public static final int TYPE_NONE = -1;
|
|
|
|
/** The "main" mailbox for the account, almost always referred to as "Inbox" */
|
|
|
|
public static final int TYPE_INBOX = 0;
|
|
|
|
// Types of mailboxes
|
|
|
|
/** Generic mailbox that holds mail */
|
|
|
|
public static final int TYPE_MAIL = 1;
|
|
|
|
/** Parent-only mailbox; does not hold any mail */
|
|
|
|
public static final int TYPE_PARENT = 2;
|
|
|
|
/** Drafts mailbox */
|
|
|
|
public static final int TYPE_DRAFTS = 3;
|
|
|
|
/** Local mailbox associated with the account's outgoing mail */
|
|
|
|
public static final int TYPE_OUTBOX = 4;
|
|
|
|
/** Sent mail; mail that was sent from the account */
|
|
|
|
public static final int TYPE_SENT = 5;
|
|
|
|
/** Deleted mail */
|
|
|
|
public static final int TYPE_TRASH = 6;
|
|
|
|
/** Junk mail */
|
|
|
|
public static final int TYPE_JUNK = 7;
|
|
|
|
/** Search results */
|
|
|
|
public static final int TYPE_SEARCH = 8;
|
2012-06-28 17:40:46 +00:00
|
|
|
/** Starred (virtual */
|
|
|
|
public static final int TYPE_STARRED = 9;
|
2011-05-14 00:26:27 +00:00
|
|
|
|
|
|
|
// Types after this are used for non-mail mailboxes (as in EAS)
|
|
|
|
public static final int TYPE_NOT_EMAIL = 0x40;
|
|
|
|
public static final int TYPE_CALENDAR = 0x41;
|
|
|
|
public static final int TYPE_CONTACTS = 0x42;
|
|
|
|
public static final int TYPE_TASKS = 0x43;
|
|
|
|
public static final int TYPE_EAS_ACCOUNT_MAILBOX = 0x44;
|
|
|
|
public static final int TYPE_UNKNOWN = 0x45;
|
|
|
|
|
|
|
|
public static final int TYPE_NOT_SYNCABLE = 0x100;
|
|
|
|
// A mailbox that holds Messages that are attachments
|
|
|
|
public static final int TYPE_ATTACHMENT = 0x101;
|
|
|
|
|
2012-06-28 17:40:46 +00:00
|
|
|
// Default "touch" time for system mailboxes
|
|
|
|
public static final int DRAFTS_DEFAULT_TOUCH_TIME = 2;
|
|
|
|
public static final int SENT_DEFAULT_TOUCH_TIME = 1;
|
|
|
|
|
2011-05-14 00:26:27 +00:00
|
|
|
// Bit field flags; each is defined below
|
|
|
|
// Warning: Do not read these flags until POP/IMAP/EAS all populate them
|
|
|
|
/** No flags set */
|
|
|
|
public static final int FLAG_NONE = 0;
|
|
|
|
/** Has children in the mailbox hierarchy */
|
|
|
|
public static final int FLAG_HAS_CHILDREN = 1<<0;
|
|
|
|
/** Children are visible in the UI */
|
|
|
|
public static final int FLAG_CHILDREN_VISIBLE = 1<<1;
|
|
|
|
/** cannot receive "pushed" mail */
|
|
|
|
public static final int FLAG_CANT_PUSH = 1<<2;
|
|
|
|
/** can hold emails (i.e. some parent mailboxes cannot themselves contain mail) */
|
|
|
|
public static final int FLAG_HOLDS_MAIL = 1<<3;
|
|
|
|
/** can be used as a target for moving messages within the account */
|
|
|
|
public static final int FLAG_ACCEPTS_MOVED_MAIL = 1<<4;
|
|
|
|
/** can be used as a target for appending messages */
|
|
|
|
public static final int FLAG_ACCEPTS_APPENDED_MAIL = 1<<5;
|
2012-06-28 17:40:46 +00:00
|
|
|
/** has user settings (sync lookback, etc.) */
|
|
|
|
public static final int FLAG_SUPPORTS_SETTINGS = 1<<6;
|
2011-05-14 00:26:27 +00:00
|
|
|
|
|
|
|
// Magic mailbox ID's
|
|
|
|
// NOTE: This is a quick solution for merged mailboxes. I would rather implement this
|
|
|
|
// with a more generic way of packaging and sharing queries between activities
|
|
|
|
public static final long QUERY_ALL_INBOXES = -2;
|
|
|
|
public static final long QUERY_ALL_UNREAD = -3;
|
|
|
|
public static final long QUERY_ALL_FAVORITES = -4;
|
|
|
|
public static final long QUERY_ALL_DRAFTS = -5;
|
|
|
|
public static final long QUERY_ALL_OUTBOX = -6;
|
|
|
|
|
|
|
|
public Mailbox() {
|
|
|
|
mBaseUri = CONTENT_URI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore a Mailbox from the database, given its unique id
|
|
|
|
* @param context
|
|
|
|
* @param id
|
|
|
|
* @return the instantiated Mailbox
|
|
|
|
*/
|
|
|
|
public static Mailbox restoreMailboxWithId(Context context, long id) {
|
|
|
|
return EmailContent.restoreContentWithId(context, Mailbox.class,
|
|
|
|
Mailbox.CONTENT_URI, Mailbox.CONTENT_PROJECTION, id);
|
|
|
|
}
|
|
|
|
|
2011-07-15 19:49:13 +00:00
|
|
|
/**
|
|
|
|
* Builds a new mailbox with "typical" settings for a system mailbox, such as a local "Drafts"
|
|
|
|
* mailbox. This is useful for protocols like POP3 or IMAP who don't have certain local
|
|
|
|
* system mailboxes synced with the server.
|
|
|
|
* Note: the mailbox is not persisted - clients must call {@link #save} themselves.
|
|
|
|
*/
|
|
|
|
public static Mailbox newSystemMailbox(long accountId, int mailboxType, String name) {
|
|
|
|
if (mailboxType == Mailbox.TYPE_MAIL) {
|
|
|
|
throw new IllegalArgumentException("Cannot specify TYPE_MAIL for a system mailbox");
|
|
|
|
}
|
|
|
|
Mailbox box = new Mailbox();
|
|
|
|
box.mAccountKey = accountId;
|
|
|
|
box.mType = mailboxType;
|
|
|
|
box.mSyncInterval = Account.CHECK_INTERVAL_NEVER;
|
|
|
|
box.mFlagVisible = true;
|
|
|
|
box.mServerId = box.mDisplayName = name;
|
|
|
|
box.mParentKey = Mailbox.NO_MAILBOX;
|
|
|
|
box.mFlags = Mailbox.FLAG_HOLDS_MAIL;
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
2011-05-14 00:26:27 +00:00
|
|
|
/**
|
|
|
|
* Returns a Mailbox from the database, given its pathname and account id. All mailbox
|
|
|
|
* paths for a particular account must be unique. Paths are stored in the column
|
|
|
|
* {@link MailboxColumns#SERVER_ID} for want of yet another column in the table.
|
|
|
|
* @param context
|
|
|
|
* @param accountId the ID of the account
|
|
|
|
* @param path the fully qualified, remote pathname
|
|
|
|
*/
|
|
|
|
public static Mailbox restoreMailboxForPath(Context context, long accountId, String path) {
|
|
|
|
Cursor c = context.getContentResolver().query(
|
|
|
|
Mailbox.CONTENT_URI,
|
|
|
|
Mailbox.CONTENT_PROJECTION,
|
|
|
|
Mailbox.PATH_AND_ACCOUNT_SELECTION,
|
|
|
|
new String[] { path, Long.toString(accountId) },
|
|
|
|
null);
|
|
|
|
if (c == null) throw new ProviderUnavailableException();
|
|
|
|
try {
|
|
|
|
Mailbox mailbox = null;
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
mailbox = getContent(c, Mailbox.class);
|
|
|
|
if (c.moveToNext()) {
|
|
|
|
Log.w(Logging.LOG_TAG, "Multiple mailboxes named \"" + path + "\"");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Log.i(Logging.LOG_TAG, "Could not find mailbox at \"" + path + "\"");
|
|
|
|
}
|
|
|
|
return mailbox;
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 21:47:21 +00:00
|
|
|
/**
|
|
|
|
* Returns a {@link Mailbox} for the given path. If the path is not in the database, a new
|
|
|
|
* mailbox will be created.
|
|
|
|
*/
|
|
|
|
public static Mailbox getMailboxForPath(Context context, long accountId, String path) {
|
|
|
|
Mailbox mailbox = restoreMailboxForPath(context, accountId, path);
|
|
|
|
if (mailbox == null) {
|
|
|
|
mailbox = new Mailbox();
|
|
|
|
}
|
|
|
|
return mailbox;
|
|
|
|
}
|
|
|
|
|
2011-05-14 00:26:27 +00:00
|
|
|
@Override
|
|
|
|
public void restore(Cursor cursor) {
|
|
|
|
mBaseUri = CONTENT_URI;
|
|
|
|
mId = cursor.getLong(CONTENT_ID_COLUMN);
|
|
|
|
mDisplayName = cursor.getString(CONTENT_DISPLAY_NAME_COLUMN);
|
|
|
|
mServerId = cursor.getString(CONTENT_SERVER_ID_COLUMN);
|
|
|
|
mParentServerId = cursor.getString(CONTENT_PARENT_SERVER_ID_COLUMN);
|
|
|
|
mParentKey = cursor.getLong(CONTENT_PARENT_KEY_COLUMN);
|
|
|
|
mAccountKey = cursor.getLong(CONTENT_ACCOUNT_KEY_COLUMN);
|
|
|
|
mType = cursor.getInt(CONTENT_TYPE_COLUMN);
|
|
|
|
mDelimiter = cursor.getInt(CONTENT_DELIMITER_COLUMN);
|
|
|
|
mSyncKey = cursor.getString(CONTENT_SYNC_KEY_COLUMN);
|
|
|
|
mSyncLookback = cursor.getInt(CONTENT_SYNC_LOOKBACK_COLUMN);
|
|
|
|
mSyncInterval = cursor.getInt(CONTENT_SYNC_INTERVAL_COLUMN);
|
|
|
|
mSyncTime = cursor.getLong(CONTENT_SYNC_TIME_COLUMN);
|
|
|
|
mFlagVisible = cursor.getInt(CONTENT_FLAG_VISIBLE_COLUMN) == 1;
|
|
|
|
mFlags = cursor.getInt(CONTENT_FLAGS_COLUMN);
|
|
|
|
mVisibleLimit = cursor.getInt(CONTENT_VISIBLE_LIMIT_COLUMN);
|
|
|
|
mSyncStatus = cursor.getString(CONTENT_SYNC_STATUS_COLUMN);
|
2011-06-03 15:51:25 +00:00
|
|
|
mLastTouchedTime = cursor.getLong(CONTENT_LAST_TOUCHED_TIME_COLUMN);
|
2012-06-28 17:40:46 +00:00
|
|
|
mUiSyncStatus = cursor.getInt(CONTENT_UI_SYNC_STATUS_COLUMN);
|
|
|
|
mUiLastSyncResult = cursor.getInt(CONTENT_UI_LAST_SYNC_RESULT_COLUMN);
|
|
|
|
mTotalCount = cursor.getInt(CONTENT_TOTAL_COUNT_COLUMN);
|
|
|
|
mHierarchicalName = cursor.getString(CONTENT_HIERARCHICAL_NAME_COLUMN);
|
2011-05-14 00:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ContentValues toContentValues() {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(MailboxColumns.DISPLAY_NAME, mDisplayName);
|
|
|
|
values.put(MailboxColumns.SERVER_ID, mServerId);
|
|
|
|
values.put(MailboxColumns.PARENT_SERVER_ID, mParentServerId);
|
|
|
|
values.put(MailboxColumns.PARENT_KEY, mParentKey);
|
|
|
|
values.put(MailboxColumns.ACCOUNT_KEY, mAccountKey);
|
|
|
|
values.put(MailboxColumns.TYPE, mType);
|
|
|
|
values.put(MailboxColumns.DELIMITER, mDelimiter);
|
|
|
|
values.put(MailboxColumns.SYNC_KEY, mSyncKey);
|
|
|
|
values.put(MailboxColumns.SYNC_LOOKBACK, mSyncLookback);
|
|
|
|
values.put(MailboxColumns.SYNC_INTERVAL, mSyncInterval);
|
|
|
|
values.put(MailboxColumns.SYNC_TIME, mSyncTime);
|
|
|
|
values.put(MailboxColumns.FLAG_VISIBLE, mFlagVisible);
|
|
|
|
values.put(MailboxColumns.FLAGS, mFlags);
|
|
|
|
values.put(MailboxColumns.VISIBLE_LIMIT, mVisibleLimit);
|
|
|
|
values.put(MailboxColumns.SYNC_STATUS, mSyncStatus);
|
2011-06-03 15:51:25 +00:00
|
|
|
values.put(MailboxColumns.LAST_TOUCHED_TIME, mLastTouchedTime);
|
2012-06-28 17:40:46 +00:00
|
|
|
values.put(MailboxColumns.UI_SYNC_STATUS, mUiSyncStatus);
|
|
|
|
values.put(MailboxColumns.UI_LAST_SYNC_RESULT, mUiLastSyncResult);
|
|
|
|
values.put(MailboxColumns.TOTAL_COUNT, mTotalCount);
|
|
|
|
values.put(MailboxColumns.HIERARCHICAL_NAME, mHierarchicalName);
|
2011-05-14 00:26:27 +00:00
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
Improve EmailContent caching...
* Guarantee that up to 16 Account (with HostAuths), and Policy rows
are always cached. Also, 6 commonly used Mailboxes per Account
(Inbox, Outbox, Drafts, Sent, Trash, and Search)
* Precache these rows when EmailProvider starts up
* Ensure that newly added, precachable rows are cached when created
* Clean up some inefficient/wrong caching code
* Fix a commonly called method in NotificationManager in which we
load a single Mailbox row using selection vs withAppendedId
* Confirm that we don't read from the database in typical use and
heavy message loading
* Add a special URI for finding mailbox by type (using the cache)
* Add special-case code for EmailContent.count(Account.CONTENT_URI)
which is used in a number of places (including on the UI thread)
and whose value is easily determined
* Add a special URI to get the default account id
* Confirm that all unit tests work
The goal here is to be able to load all Account, HostAuth, Policy,
and Mailbox objects (by id) without worrying about disk access.
There will still be a single disk read for uncommon Mailbox reads,
but this should be considered acceptable.
Change-Id: Ibc9aa7acc73185e360b0b6f3053b90a985e97210
TODO: Unit tests
2011-06-19 01:03:11 +00:00
|
|
|
* Convenience method to return the id of a given type of Mailbox for a given Account; the
|
|
|
|
* common Mailbox types (Inbox, Outbox, Sent, Drafts, Trash, and Search) are all cached by
|
|
|
|
* EmailProvider; therefore, we warn if the mailbox is not found in the cache
|
|
|
|
*
|
2011-05-14 00:26:27 +00:00
|
|
|
* @param context the caller's context, used to get a ContentResolver
|
|
|
|
* @param accountId the id of the account to be queried
|
|
|
|
* @param type the mailbox type, as defined above
|
|
|
|
* @return the id of the mailbox, or -1 if not found
|
|
|
|
*/
|
|
|
|
public static long findMailboxOfType(Context context, long accountId, int type) {
|
Improve EmailContent caching...
* Guarantee that up to 16 Account (with HostAuths), and Policy rows
are always cached. Also, 6 commonly used Mailboxes per Account
(Inbox, Outbox, Drafts, Sent, Trash, and Search)
* Precache these rows when EmailProvider starts up
* Ensure that newly added, precachable rows are cached when created
* Clean up some inefficient/wrong caching code
* Fix a commonly called method in NotificationManager in which we
load a single Mailbox row using selection vs withAppendedId
* Confirm that we don't read from the database in typical use and
heavy message loading
* Add a special URI for finding mailbox by type (using the cache)
* Add special-case code for EmailContent.count(Account.CONTENT_URI)
which is used in a number of places (including on the UI thread)
and whose value is easily determined
* Add a special URI to get the default account id
* Confirm that all unit tests work
The goal here is to be able to load all Account, HostAuth, Policy,
and Mailbox objects (by id) without worrying about disk access.
There will still be a single disk read for uncommon Mailbox reads,
but this should be considered acceptable.
Change-Id: Ibc9aa7acc73185e360b0b6f3053b90a985e97210
TODO: Unit tests
2011-06-19 01:03:11 +00:00
|
|
|
// First use special URI
|
|
|
|
Uri uri = FROM_ACCOUNT_AND_TYPE_URI.buildUpon().appendPath(Long.toString(accountId))
|
|
|
|
.appendPath(Integer.toString(type)).build();
|
|
|
|
Cursor c = context.getContentResolver().query(uri, ID_PROJECTION, null, null, null);
|
2011-06-22 17:59:06 +00:00
|
|
|
if (c != null) {
|
|
|
|
try {
|
|
|
|
c.moveToFirst();
|
|
|
|
Long mailboxId = c.getLong(ID_PROJECTION_COLUMN);
|
2011-07-25 23:04:07 +00:00
|
|
|
if (mailboxId != null
|
|
|
|
&& mailboxId != 0L
|
|
|
|
&& mailboxId != NO_MAILBOX) {
|
2011-06-22 17:59:06 +00:00
|
|
|
return mailboxId;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
Improve EmailContent caching...
* Guarantee that up to 16 Account (with HostAuths), and Policy rows
are always cached. Also, 6 commonly used Mailboxes per Account
(Inbox, Outbox, Drafts, Sent, Trash, and Search)
* Precache these rows when EmailProvider starts up
* Ensure that newly added, precachable rows are cached when created
* Clean up some inefficient/wrong caching code
* Fix a commonly called method in NotificationManager in which we
load a single Mailbox row using selection vs withAppendedId
* Confirm that we don't read from the database in typical use and
heavy message loading
* Add a special URI for finding mailbox by type (using the cache)
* Add special-case code for EmailContent.count(Account.CONTENT_URI)
which is used in a number of places (including on the UI thread)
and whose value is easily determined
* Add a special URI to get the default account id
* Confirm that all unit tests work
The goal here is to be able to load all Account, HostAuth, Policy,
and Mailbox objects (by id) without worrying about disk access.
There will still be a single disk read for uncommon Mailbox reads,
but this should be considered acceptable.
Change-Id: Ibc9aa7acc73185e360b0b6f3053b90a985e97210
TODO: Unit tests
2011-06-19 01:03:11 +00:00
|
|
|
}
|
2011-07-25 23:04:07 +00:00
|
|
|
// Fallback to querying the database directly.
|
2011-05-14 00:26:27 +00:00
|
|
|
String[] bindArguments = new String[] {Long.toString(type), Long.toString(accountId)};
|
|
|
|
return Utility.getFirstRowLong(context, Mailbox.CONTENT_URI,
|
|
|
|
ID_PROJECTION, WHERE_TYPE_AND_ACCOUNT_KEY, bindArguments, null,
|
|
|
|
ID_PROJECTION_COLUMN, NO_MAILBOX);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method that returns the mailbox found using the method above
|
|
|
|
*/
|
|
|
|
public static Mailbox restoreMailboxOfType(Context context, long accountId, int type) {
|
|
|
|
long mailboxId = findMailboxOfType(context, accountId, type);
|
|
|
|
if (mailboxId != Mailbox.NO_MAILBOX) {
|
|
|
|
return Mailbox.restoreMailboxWithId(context, mailboxId);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getUnreadCountByAccountAndMailboxType(Context context, long accountId,
|
|
|
|
int type) {
|
|
|
|
return Utility.getFirstRowInt(context, Mailbox.CONTENT_URI,
|
|
|
|
MAILBOX_SUM_OF_UNREAD_COUNT_PROJECTION,
|
|
|
|
ACCOUNT_AND_MAILBOX_TYPE_SELECTION,
|
|
|
|
new String[] { String.valueOf(accountId), String.valueOf(type) },
|
|
|
|
null, UNREAD_COUNT_COUNT_COLUMN, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getUnreadCountByMailboxType(Context context, int type) {
|
|
|
|
return Utility.getFirstRowInt(context, Mailbox.CONTENT_URI,
|
|
|
|
MAILBOX_SUM_OF_UNREAD_COUNT_PROJECTION,
|
|
|
|
MAILBOX_TYPE_SELECTION,
|
|
|
|
new String[] { String.valueOf(type) }, null, UNREAD_COUNT_COUNT_COLUMN, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getMessageCountByMailboxType(Context context, int type) {
|
|
|
|
return Utility.getFirstRowInt(context, Mailbox.CONTENT_URI,
|
|
|
|
MAILBOX_SUM_OF_MESSAGE_COUNT_PROJECTION,
|
|
|
|
MAILBOX_TYPE_SELECTION,
|
|
|
|
new String[] { String.valueOf(type) }, null, MESSAGE_COUNT_COUNT_COLUMN, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the mailbox for a message with a given id
|
|
|
|
* @param context the caller's context
|
|
|
|
* @param messageId the id of the message
|
|
|
|
* @return the mailbox, or null if the mailbox doesn't exist
|
|
|
|
*/
|
|
|
|
public static Mailbox getMailboxForMessageId(Context context, long messageId) {
|
|
|
|
long mailboxId = Message.getKeyColumnLong(context, messageId,
|
|
|
|
MessageColumns.MAILBOX_KEY);
|
|
|
|
if (mailboxId != -1) {
|
|
|
|
return Mailbox.restoreMailboxWithId(context, mailboxId);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mailbox type, or -1 if mailbox not found.
|
|
|
|
*/
|
|
|
|
public static int getMailboxType(Context context, long mailboxId) {
|
|
|
|
Uri url = ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId);
|
|
|
|
return Utility.getFirstRowInt(context, url, MAILBOX_TYPE_PROJECTION,
|
|
|
|
null, null, null, MAILBOX_TYPE_TYPE_COLUMN, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mailbox display name, or null if mailbox not found.
|
|
|
|
*/
|
|
|
|
public static String getDisplayName(Context context, long mailboxId) {
|
|
|
|
Uri url = ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId);
|
|
|
|
return Utility.getFirstRowString(context, url, MAILBOX_DISPLAY_NAME_PROJECTION,
|
|
|
|
null, null, null, MAILBOX_DISPLAY_NAME_COLUMN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mailboxId ID of a mailbox. This method accepts magic mailbox IDs, such as
|
|
|
|
* {@link #QUERY_ALL_INBOXES}. (They're all non-refreshable.)
|
|
|
|
* @return true if a mailbox is refreshable.
|
|
|
|
*/
|
|
|
|
public static boolean isRefreshable(Context context, long mailboxId) {
|
|
|
|
if (mailboxId < 0) {
|
|
|
|
return false; // magic mailboxes
|
|
|
|
}
|
|
|
|
switch (getMailboxType(context, mailboxId)) {
|
|
|
|
case -1: // not found
|
|
|
|
case TYPE_DRAFTS:
|
|
|
|
case TYPE_OUTBOX:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-20 20:03:54 +00:00
|
|
|
* @return whether or not this mailbox supports moving messages out of it
|
2011-05-14 00:26:27 +00:00
|
|
|
*/
|
2011-07-20 20:03:54 +00:00
|
|
|
public boolean canHaveMessagesMoved() {
|
|
|
|
switch (mType) {
|
2011-05-14 00:26:27 +00:00
|
|
|
case TYPE_INBOX:
|
|
|
|
case TYPE_MAIL:
|
|
|
|
case TYPE_TRASH:
|
|
|
|
case TYPE_JUNK:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false; // TYPE_DRAFTS, TYPE_OUTBOX, TYPE_SENT, etc
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if messages in a mailbox of a type can be replied/forwarded.
|
|
|
|
*/
|
|
|
|
public static boolean isMailboxTypeReplyAndForwardable(int type) {
|
|
|
|
return (type != TYPE_TRASH) && (type != TYPE_DRAFTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a set of hashes that can identify this mailbox. These can be used to
|
|
|
|
* determine if any of the fields have been modified.
|
|
|
|
*/
|
|
|
|
public Object[] getHashes() {
|
|
|
|
Object[] hash = new Object[CONTENT_PROJECTION.length];
|
|
|
|
|
|
|
|
hash[CONTENT_ID_COLUMN]
|
|
|
|
= mId;
|
|
|
|
hash[CONTENT_DISPLAY_NAME_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mDisplayName;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_SERVER_ID_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mServerId;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_PARENT_SERVER_ID_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mParentServerId;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_ACCOUNT_KEY_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mAccountKey;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_TYPE_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mType;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_DELIMITER_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mDelimiter;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_SYNC_KEY_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mSyncKey;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_SYNC_LOOKBACK_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mSyncLookback;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_SYNC_INTERVAL_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mSyncInterval;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_SYNC_TIME_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mSyncTime;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_FLAG_VISIBLE_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mFlagVisible;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_FLAGS_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mFlags;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_VISIBLE_LIMIT_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mVisibleLimit;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_SYNC_STATUS_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mSyncStatus;
|
2011-05-14 00:26:27 +00:00
|
|
|
hash[CONTENT_PARENT_KEY_COLUMN]
|
2011-06-03 15:51:25 +00:00
|
|
|
= mParentKey;
|
|
|
|
hash[CONTENT_LAST_TOUCHED_TIME_COLUMN]
|
|
|
|
= mLastTouchedTime;
|
2012-06-28 17:40:46 +00:00
|
|
|
hash[CONTENT_UI_SYNC_STATUS_COLUMN]
|
|
|
|
= mUiSyncStatus;
|
|
|
|
hash[CONTENT_UI_LAST_SYNC_RESULT_COLUMN]
|
|
|
|
= mUiLastSyncResult;
|
|
|
|
hash[CONTENT_TOTAL_COUNT_COLUMN]
|
|
|
|
= mTotalCount;
|
|
|
|
hash[CONTENT_HIERARCHICAL_NAME_COLUMN]
|
|
|
|
= mHierarchicalName;
|
2011-05-14 00:26:27 +00:00
|
|
|
return hash;
|
|
|
|
}
|
2011-05-14 01:35:08 +00:00
|
|
|
|
|
|
|
// Parcelable
|
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parcelable
|
|
|
|
@Override
|
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
dest.writeParcelable(mBaseUri, flags);
|
|
|
|
dest.writeLong(mId);
|
|
|
|
dest.writeString(mDisplayName);
|
|
|
|
dest.writeString(mServerId);
|
|
|
|
dest.writeString(mParentServerId);
|
|
|
|
dest.writeLong(mParentKey);
|
|
|
|
dest.writeLong(mAccountKey);
|
|
|
|
dest.writeInt(mType);
|
|
|
|
dest.writeInt(mDelimiter);
|
|
|
|
dest.writeString(mSyncKey);
|
|
|
|
dest.writeInt(mSyncLookback);
|
|
|
|
dest.writeInt(mSyncInterval);
|
|
|
|
dest.writeLong(mSyncTime);
|
|
|
|
dest.writeInt(mFlagVisible ? 1 : 0);
|
|
|
|
dest.writeInt(mFlags);
|
|
|
|
dest.writeInt(mVisibleLimit);
|
|
|
|
dest.writeString(mSyncStatus);
|
2011-06-03 15:51:25 +00:00
|
|
|
dest.writeLong(mLastTouchedTime);
|
2012-06-28 17:40:46 +00:00
|
|
|
dest.writeInt(mUiSyncStatus);
|
|
|
|
dest.writeInt(mUiLastSyncResult);
|
|
|
|
dest.writeInt(mTotalCount);
|
|
|
|
dest.writeString(mHierarchicalName);
|
2011-05-14 01:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Mailbox(Parcel in) {
|
|
|
|
mBaseUri = in.readParcelable(null);
|
|
|
|
mId = in.readLong();
|
|
|
|
mDisplayName = in.readString();
|
|
|
|
mServerId = in.readString();
|
|
|
|
mParentServerId = in.readString();
|
|
|
|
mParentKey = in.readLong();
|
|
|
|
mAccountKey = in.readLong();
|
|
|
|
mType = in.readInt();
|
|
|
|
mDelimiter = in.readInt();
|
|
|
|
mSyncKey = in.readString();
|
|
|
|
mSyncLookback = in.readInt();
|
|
|
|
mSyncInterval = in.readInt();
|
|
|
|
mSyncTime = in.readLong();
|
|
|
|
mFlagVisible = in.readInt() == 1;
|
|
|
|
mFlags = in.readInt();
|
|
|
|
mVisibleLimit = in.readInt();
|
|
|
|
mSyncStatus = in.readString();
|
2011-06-03 15:51:25 +00:00
|
|
|
mLastTouchedTime = in.readLong();
|
2012-06-28 17:40:46 +00:00
|
|
|
mUiSyncStatus = in.readInt();
|
|
|
|
mUiLastSyncResult = in.readInt();
|
|
|
|
mTotalCount = in.readInt();
|
|
|
|
mHierarchicalName = in.readString();
|
2011-05-14 01:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static final Parcelable.Creator<Mailbox> CREATOR = new Parcelable.Creator<Mailbox>() {
|
|
|
|
@Override
|
|
|
|
public Mailbox createFromParcel(Parcel source) {
|
|
|
|
return new Mailbox(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mailbox[] newArray(int size) {
|
|
|
|
return new Mailbox[size];
|
|
|
|
}
|
|
|
|
};
|
2012-06-28 17:40:46 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "[Mailbox " + mId + ": " + mDisplayName + "]";
|
|
|
|
}
|
2011-06-01 17:31:21 +00:00
|
|
|
}
|