2012-06-28 19:16:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 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.email.service;
|
|
|
|
|
|
|
|
import android.app.Service;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.TrafficStats;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.os.RemoteException;
|
|
|
|
|
2014-08-28 18:00:08 +00:00
|
|
|
import com.android.email.DebugUtils;
|
2012-06-28 19:16:59 +00:00
|
|
|
import com.android.email.NotificationController;
|
2014-09-07 20:36:33 +00:00
|
|
|
import com.android.email.NotificationControllerCreatorHolder;
|
2012-06-28 19:16:59 +00:00
|
|
|
import com.android.email.mail.Store;
|
2012-08-02 17:53:40 +00:00
|
|
|
import com.android.email.mail.store.Pop3Store;
|
|
|
|
import com.android.email.mail.store.Pop3Store.Pop3Folder;
|
|
|
|
import com.android.email.mail.store.Pop3Store.Pop3Message;
|
2012-06-28 19:16:59 +00:00
|
|
|
import com.android.email.provider.Utilities;
|
|
|
|
import com.android.emailcommon.Logging;
|
|
|
|
import com.android.emailcommon.TrafficFlags;
|
|
|
|
import com.android.emailcommon.mail.AuthenticationFailedException;
|
|
|
|
import com.android.emailcommon.mail.Folder.OpenMode;
|
|
|
|
import com.android.emailcommon.mail.MessagingException;
|
|
|
|
import com.android.emailcommon.provider.Account;
|
|
|
|
import com.android.emailcommon.provider.EmailContent;
|
2012-08-02 17:53:40 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent.Attachment;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.Message;
|
2012-06-28 19:16:59 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent.MessageColumns;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.SyncColumns;
|
|
|
|
import com.android.emailcommon.provider.Mailbox;
|
|
|
|
import com.android.emailcommon.service.EmailServiceStatus;
|
|
|
|
import com.android.emailcommon.service.IEmailServiceCallback;
|
2013-09-12 21:33:07 +00:00
|
|
|
import com.android.emailcommon.utility.AttachmentUtilities;
|
2012-08-02 17:53:40 +00:00
|
|
|
import com.android.mail.providers.UIProvider;
|
|
|
|
import com.android.mail.providers.UIProvider.AttachmentState;
|
2013-05-26 04:32:32 +00:00
|
|
|
import com.android.mail.utils.LogUtils;
|
2012-06-28 19:16:59 +00:00
|
|
|
|
2012-08-03 23:02:55 +00:00
|
|
|
import org.apache.james.mime4j.EOLConvertingInputStream;
|
|
|
|
|
2012-08-02 17:53:40 +00:00
|
|
|
import java.io.IOException;
|
2012-06-28 19:16:59 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
2013-09-12 21:33:07 +00:00
|
|
|
import java.util.HashSet;
|
2014-08-05 09:43:02 +00:00
|
|
|
import android.util.Log;
|
2012-06-28 19:16:59 +00:00
|
|
|
|
|
|
|
public class Pop3Service extends Service {
|
|
|
|
private static final String TAG = "Pop3Service";
|
2013-07-29 18:52:43 +00:00
|
|
|
private static final int DEFAULT_SYNC_COUNT = 100;
|
2014-08-05 09:43:02 +00:00
|
|
|
private static final String ACTION_CHECK_MAIL =
|
|
|
|
"org.codeaurora.email.intent.action.MAIL_SERVICE_WAKEUP";
|
|
|
|
private static final String EXTRA_ACCOUNT = "org.codeaurora.email.intent.extra.ACCOUNT";
|
2012-06-28 19:16:59 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
2014-08-05 09:43:02 +00:00
|
|
|
Log.d(TAG,"Inside onStartCommand");
|
|
|
|
final String action = intent.getAction();
|
|
|
|
Log.d(TAG,"action is " + action);
|
|
|
|
Context context = getApplicationContext();
|
|
|
|
if (ACTION_CHECK_MAIL.equals(action)) {
|
|
|
|
final long accountId = intent.getLongExtra(EXTRA_ACCOUNT, -1);
|
|
|
|
Log.d(TAG,"accountId is " + accountId);
|
|
|
|
final long inboxId = Mailbox.findMailboxOfType(context, accountId,
|
|
|
|
Mailbox.TYPE_INBOX);
|
|
|
|
Log.d(TAG,"inboxId is " + inboxId);
|
|
|
|
mBinder.init(context);
|
|
|
|
mBinder.requestSync(inboxId, true, 0);
|
|
|
|
}
|
2012-06-28 19:16:59 +00:00
|
|
|
return Service.START_STICKY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create our EmailService implementation here.
|
|
|
|
*/
|
|
|
|
private final EmailServiceStub mBinder = new EmailServiceStub() {
|
2012-08-02 17:53:40 +00:00
|
|
|
@Override
|
2014-02-26 16:57:53 +00:00
|
|
|
public void loadAttachment(final IEmailServiceCallback callback, final long accountId,
|
|
|
|
final long attachmentId, final boolean background) throws RemoteException {
|
2012-08-02 17:53:40 +00:00
|
|
|
Attachment att = Attachment.restoreAttachmentWithId(mContext, attachmentId);
|
|
|
|
if (att == null || att.mUiState != AttachmentState.DOWNLOADING) return;
|
|
|
|
long inboxId = Mailbox.findMailboxOfType(mContext, att.mAccountKey, Mailbox.TYPE_INBOX);
|
|
|
|
if (inboxId == Mailbox.NO_MAILBOX) return;
|
|
|
|
// We load attachments during a sync
|
2014-01-29 23:24:06 +00:00
|
|
|
requestSync(inboxId, true, 0);
|
2012-08-23 05:25:42 +00:00
|
|
|
}
|
2012-06-28 19:16:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
2013-07-30 02:11:41 +00:00
|
|
|
mBinder.init(this);
|
2012-06-28 19:16:59 +00:00
|
|
|
return mBinder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-02 17:53:40 +00:00
|
|
|
* Start foreground synchronization of the specified folder. This is called
|
|
|
|
* by synchronizeMailbox or checkMail. TODO this should use ID's instead of
|
|
|
|
* fully-restored objects
|
|
|
|
*
|
2012-06-28 19:16:59 +00:00
|
|
|
* @param account
|
|
|
|
* @param folder
|
2013-04-05 01:30:39 +00:00
|
|
|
* @param deltaMessageCount the requested change in number of messages to sync.
|
2013-07-30 02:11:41 +00:00
|
|
|
* @return The status code for whether this operation succeeded.
|
2012-06-28 19:16:59 +00:00
|
|
|
* @throws MessagingException
|
|
|
|
*/
|
2013-07-30 02:11:41 +00:00
|
|
|
public static int synchronizeMailboxSynchronous(Context context, final Account account,
|
2013-04-05 01:30:39 +00:00
|
|
|
final Mailbox folder, final int deltaMessageCount) throws MessagingException {
|
2012-06-28 19:16:59 +00:00
|
|
|
TrafficStats.setThreadStatsTag(TrafficFlags.getSyncFlags(context, account));
|
2014-09-07 20:36:33 +00:00
|
|
|
final NotificationController nc =
|
|
|
|
NotificationControllerCreatorHolder.getInstance(context);
|
2012-06-28 19:16:59 +00:00
|
|
|
try {
|
2013-04-05 01:30:39 +00:00
|
|
|
synchronizePop3Mailbox(context, account, folder, deltaMessageCount);
|
2012-06-28 19:16:59 +00:00
|
|
|
// Clear authentication notification for this account
|
2014-09-07 20:36:33 +00:00
|
|
|
if (nc != null) {
|
|
|
|
nc.cancelLoginFailedNotification(account.mId);
|
|
|
|
}
|
2012-06-28 19:16:59 +00:00
|
|
|
} catch (MessagingException e) {
|
|
|
|
if (Logging.LOGD) {
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.v(Logging.LOG_TAG, "synchronizeMailbox", e);
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
2014-09-07 20:36:33 +00:00
|
|
|
if (e instanceof AuthenticationFailedException && nc != null) {
|
2012-06-28 19:16:59 +00:00
|
|
|
// Generate authentication notification
|
2014-07-11 17:08:20 +00:00
|
|
|
nc.showLoginFailedNotificationSynchronous(account.mId, true /* incoming */);
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2013-07-30 02:11:41 +00:00
|
|
|
// TODO: Rather than use exceptions as logic aobve, return the status and handle it
|
|
|
|
// correctly in caller.
|
|
|
|
return EmailServiceStatus.SUCCESS;
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-02 17:53:40 +00:00
|
|
|
* Lightweight record for the first pass of message sync, where I'm just
|
|
|
|
* seeing if the local message requires sync. Later (for messages that need
|
|
|
|
* syncing) we'll do a full readout from the DB.
|
2012-06-28 19:16:59 +00:00
|
|
|
*/
|
|
|
|
private static class LocalMessageInfo {
|
|
|
|
private static final int COLUMN_ID = 0;
|
2012-08-02 17:53:40 +00:00
|
|
|
private static final int COLUMN_FLAG_LOADED = 1;
|
|
|
|
private static final int COLUMN_SERVER_ID = 2;
|
2012-06-28 19:16:59 +00:00
|
|
|
private static final String[] PROJECTION = new String[] {
|
2012-08-02 17:53:40 +00:00
|
|
|
EmailContent.RECORD_ID, MessageColumns.FLAG_LOADED, SyncColumns.SERVER_ID
|
2012-06-28 19:16:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
final long mId;
|
|
|
|
final int mFlagLoaded;
|
|
|
|
final String mServerId;
|
|
|
|
|
|
|
|
public LocalMessageInfo(Cursor c) {
|
|
|
|
mId = c.getLong(COLUMN_ID);
|
|
|
|
mFlagLoaded = c.getInt(COLUMN_FLAG_LOADED);
|
|
|
|
mServerId = c.getString(COLUMN_SERVER_ID);
|
2012-08-02 17:53:40 +00:00
|
|
|
// Note: mailbox key and account key not needed - they are projected
|
|
|
|
// for the SELECT
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the structure and body of messages not yet synced
|
2012-08-02 17:53:40 +00:00
|
|
|
*
|
2012-06-28 19:16:59 +00:00
|
|
|
* @param account the account we're syncing
|
|
|
|
* @param remoteFolder the (open) Folder we're working on
|
|
|
|
* @param unsyncedMessages an array of Message's we've got headers for
|
|
|
|
* @param toMailbox the destination mailbox we're syncing
|
|
|
|
* @throws MessagingException
|
|
|
|
*/
|
|
|
|
static void loadUnsyncedMessages(final Context context, final Account account,
|
2012-08-02 17:53:40 +00:00
|
|
|
Pop3Folder remoteFolder, ArrayList<Pop3Message> unsyncedMessages,
|
|
|
|
final Mailbox toMailbox) throws MessagingException {
|
2013-07-29 18:52:43 +00:00
|
|
|
|
2014-08-28 18:00:08 +00:00
|
|
|
if (DebugUtils.DEBUG) {
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.d(TAG, "Loading " + unsyncedMessages.size() + " unsynced messages");
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
2013-07-29 18:52:43 +00:00
|
|
|
|
2012-08-02 17:53:40 +00:00
|
|
|
try {
|
|
|
|
int cnt = unsyncedMessages.size();
|
2013-07-29 18:52:43 +00:00
|
|
|
// They are in most recent to least recent order, process them that way.
|
|
|
|
for (int i = 0; i < cnt; i++) {
|
2013-07-29 17:40:14 +00:00
|
|
|
final Pop3Message message = unsyncedMessages.get(i);
|
|
|
|
remoteFolder.fetchBody(message, Pop3Store.FETCH_BODY_SANE_SUGGESTED_SIZE / 76,
|
|
|
|
null);
|
2012-08-02 17:53:40 +00:00
|
|
|
int flag = EmailContent.Message.FLAG_LOADED_COMPLETE;
|
|
|
|
if (!message.isComplete()) {
|
2013-07-29 17:40:14 +00:00
|
|
|
// TODO: when the message is not complete, this should mark the message as
|
|
|
|
// partial. When that change is made, we need to make sure that:
|
|
|
|
// 1) Partial messages are shown in the conversation list
|
|
|
|
// 2) We are able to download the rest of the message/attachment when the
|
|
|
|
// user requests it.
|
2013-07-30 00:02:27 +00:00
|
|
|
flag = EmailContent.Message.FLAG_LOADED_PARTIAL;
|
2012-08-02 17:53:40 +00:00
|
|
|
}
|
2014-08-28 18:00:08 +00:00
|
|
|
if (DebugUtils.DEBUG) {
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.d(TAG, "Message is " + (message.isComplete() ? "" : "NOT ")
|
|
|
|
+ "complete");
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
2012-08-02 17:53:40 +00:00
|
|
|
// If message is incomplete, create a "fake" attachment
|
|
|
|
Utilities.copyOneMessageToProvider(context, message, account, toMailbox, flag);
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
2012-08-02 17:53:40 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new MessagingException(MessagingException.IOERROR);
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-03 23:02:55 +00:00
|
|
|
private static class FetchCallback implements EOLConvertingInputStream.Callback {
|
|
|
|
private final ContentResolver mResolver;
|
|
|
|
private final Uri mAttachmentUri;
|
|
|
|
private final ContentValues mContentValues = new ContentValues();
|
|
|
|
|
|
|
|
FetchCallback(ContentResolver resolver, Uri attachmentUri) {
|
|
|
|
mResolver = resolver;
|
|
|
|
mAttachmentUri = attachmentUri;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void report(int bytesRead) {
|
|
|
|
mContentValues.put(AttachmentColumns.UI_DOWNLOADED_SIZE, bytesRead);
|
|
|
|
mResolver.update(mAttachmentUri, mContentValues, null, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-28 19:16:59 +00:00
|
|
|
/**
|
2012-08-02 17:53:40 +00:00
|
|
|
* Synchronizer
|
2012-06-28 19:16:59 +00:00
|
|
|
*
|
|
|
|
* @param account the account to sync
|
|
|
|
* @param mailbox the mailbox to sync
|
2013-04-05 01:30:39 +00:00
|
|
|
* @param deltaMessageCount the requested change to number of messages to sync
|
2012-06-28 19:16:59 +00:00
|
|
|
* @throws MessagingException
|
|
|
|
*/
|
2013-08-28 22:07:57 +00:00
|
|
|
private synchronized static void synchronizePop3Mailbox(final Context context, final Account account,
|
2013-04-05 01:30:39 +00:00
|
|
|
final Mailbox mailbox, final int deltaMessageCount) throws MessagingException {
|
2012-08-03 01:09:56 +00:00
|
|
|
// TODO Break this into smaller pieces
|
2012-06-28 19:16:59 +00:00
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
|
2012-08-02 17:53:40 +00:00
|
|
|
// We only sync Inbox
|
|
|
|
if (mailbox.mType != Mailbox.TYPE_INBOX) {
|
2012-06-28 19:16:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-02 17:53:40 +00:00
|
|
|
// Get the message list from EmailProvider and create an index of the uids
|
2012-06-28 19:16:59 +00:00
|
|
|
|
|
|
|
Cursor localUidCursor = null;
|
|
|
|
HashMap<String, LocalMessageInfo> localMessageMap = new HashMap<String, LocalMessageInfo>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
localUidCursor = resolver.query(
|
|
|
|
EmailContent.Message.CONTENT_URI,
|
|
|
|
LocalMessageInfo.PROJECTION,
|
2013-04-15 20:27:07 +00:00
|
|
|
MessageColumns.MAILBOX_KEY + "=?",
|
2012-06-28 19:16:59 +00:00
|
|
|
new String[] {
|
|
|
|
String.valueOf(mailbox.mId)
|
|
|
|
},
|
|
|
|
null);
|
|
|
|
while (localUidCursor.moveToNext()) {
|
|
|
|
LocalMessageInfo info = new LocalMessageInfo(localUidCursor);
|
|
|
|
localMessageMap.put(info.mServerId, info);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (localUidCursor != null) {
|
|
|
|
localUidCursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 17:53:40 +00:00
|
|
|
// Open the remote folder and create the remote folder if necessary
|
2012-06-28 19:16:59 +00:00
|
|
|
|
2012-08-02 17:53:40 +00:00
|
|
|
Pop3Store remoteStore = (Pop3Store)Store.getInstance(account, context);
|
2012-06-28 19:16:59 +00:00
|
|
|
// The account might have been deleted
|
2012-08-02 17:53:40 +00:00
|
|
|
if (remoteStore == null)
|
|
|
|
return;
|
|
|
|
Pop3Folder remoteFolder = (Pop3Folder)remoteStore.getFolder(mailbox.mServerId);
|
2012-06-28 19:16:59 +00:00
|
|
|
|
2012-08-02 17:53:40 +00:00
|
|
|
// Open the remote folder. This pre-loads certain metadata like message
|
|
|
|
// count.
|
2012-06-28 19:16:59 +00:00
|
|
|
remoteFolder.open(OpenMode.READ_WRITE);
|
|
|
|
|
2012-08-03 01:09:56 +00:00
|
|
|
String[] accountIdArgs = new String[] { Long.toString(account.mId) };
|
|
|
|
long trashMailboxId = Mailbox.findMailboxOfType(context, account.mId, Mailbox.TYPE_TRASH);
|
|
|
|
Cursor updates = resolver.query(
|
|
|
|
EmailContent.Message.UPDATED_CONTENT_URI,
|
|
|
|
EmailContent.Message.ID_COLUMN_PROJECTION,
|
|
|
|
EmailContent.MessageColumns.ACCOUNT_KEY + "=?", accountIdArgs,
|
|
|
|
null);
|
|
|
|
try {
|
|
|
|
// loop through messages marked as deleted
|
|
|
|
while (updates.moveToNext()) {
|
|
|
|
long id = updates.getLong(Message.ID_COLUMNS_ID_COLUMN);
|
|
|
|
EmailContent.Message currentMsg =
|
|
|
|
EmailContent.Message.restoreMessageWithId(context, id);
|
|
|
|
if (currentMsg.mMailboxKey == trashMailboxId) {
|
|
|
|
// Delete this on the server
|
|
|
|
Pop3Message popMessage =
|
|
|
|
(Pop3Message)remoteFolder.getMessage(currentMsg.mServerId);
|
|
|
|
if (popMessage != null) {
|
|
|
|
remoteFolder.deleteMessage(popMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Finally, delete the update
|
|
|
|
Uri uri = ContentUris.withAppendedId(EmailContent.Message.UPDATED_CONTENT_URI, id);
|
|
|
|
context.getContentResolver().delete(uri, null, null);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
updates.close();
|
|
|
|
}
|
|
|
|
|
2013-04-05 01:30:39 +00:00
|
|
|
// Get the remote message count.
|
|
|
|
final int remoteMessageCount = remoteFolder.getMessageCount();
|
|
|
|
|
2013-07-29 18:52:43 +00:00
|
|
|
// Save the folder message count.
|
|
|
|
mailbox.updateMessageCount(context, remoteMessageCount);
|
2012-06-28 19:16:59 +00:00
|
|
|
|
2012-08-02 17:53:40 +00:00
|
|
|
// Create a list of messages to download
|
|
|
|
Pop3Message[] remoteMessages = new Pop3Message[0];
|
|
|
|
final ArrayList<Pop3Message> unsyncedMessages = new ArrayList<Pop3Message>();
|
|
|
|
HashMap<String, Pop3Message> remoteUidMap = new HashMap<String, Pop3Message>();
|
2012-06-28 19:16:59 +00:00
|
|
|
|
|
|
|
if (remoteMessageCount > 0) {
|
|
|
|
/*
|
2013-07-29 18:52:43 +00:00
|
|
|
* Get all messageIds in the mailbox.
|
|
|
|
* We don't necessarily need to sync all of them.
|
2012-06-28 19:16:59 +00:00
|
|
|
*/
|
2013-07-29 18:52:43 +00:00
|
|
|
remoteMessages = remoteFolder.getMessages(remoteMessageCount, remoteMessageCount);
|
|
|
|
LogUtils.d(Logging.LOG_TAG, "remoteMessageCount " + remoteMessageCount);
|
2012-06-28 19:16:59 +00:00
|
|
|
|
|
|
|
/*
|
2013-07-29 18:52:43 +00:00
|
|
|
* TODO: It would be nicer if the default sync window were time based rather than
|
|
|
|
* count based, but POP3 does not support time based queries, and the UIDL command
|
|
|
|
* does not report timestamps. To handle this, we would need to load a block of
|
|
|
|
* Ids, sync those messages to get the timestamps, and then load more Ids until we
|
|
|
|
* have filled out our window.
|
2012-06-28 19:16:59 +00:00
|
|
|
*/
|
2013-07-29 18:52:43 +00:00
|
|
|
int count = 0;
|
|
|
|
int countNeeded = DEFAULT_SYNC_COUNT;
|
|
|
|
for (final Pop3Message message : remoteMessages) {
|
|
|
|
final String uid = message.getUid();
|
2012-08-02 17:53:40 +00:00
|
|
|
remoteUidMap.put(uid, message);
|
2013-09-11 23:59:55 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 21:33:07 +00:00
|
|
|
/*
|
|
|
|
* Figure out which messages we need to sync. Start at the most recent ones, and keep
|
|
|
|
* going until we hit one of four end conditions:
|
|
|
|
* 1. We currently have zero local messages. In this case, we will sync the most recent
|
|
|
|
* DEFAULT_SYNC_COUNT, then stop.
|
|
|
|
* 2. We have some local messages, and after encountering them, we find some older
|
|
|
|
* messages that do not yet exist locally. In this case, we will load whichever came
|
|
|
|
* before the ones we already had locally, and also deltaMessageCount additional
|
|
|
|
* older messages.
|
|
|
|
* 3. We have some local messages, but after examining the most recent
|
|
|
|
* DEFAULT_SYNC_COUNT remote messages, we still have not encountered any that exist
|
|
|
|
* locally. In this case, we'll stop adding new messages to sync, leaving a gap between
|
|
|
|
* the ones we've just loaded and the ones we already had.
|
|
|
|
* 4. We examine all of the remote messages before running into any of our count
|
|
|
|
* limitations.
|
|
|
|
*/
|
2013-09-11 23:59:55 +00:00
|
|
|
for (final Pop3Message message : remoteMessages) {
|
|
|
|
final String uid = message.getUid();
|
2013-07-29 18:52:43 +00:00
|
|
|
final LocalMessageInfo localMessage = localMessageMap.get(uid);
|
|
|
|
if (localMessage == null) {
|
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
// We have found a message that already exists locally. We may or may not
|
|
|
|
// need to keep looking, depending on what deltaMessageCount is.
|
|
|
|
LogUtils.d(Logging.LOG_TAG, "found a local message, need " +
|
|
|
|
deltaMessageCount + " more remote messages");
|
|
|
|
countNeeded = deltaMessageCount;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
2012-06-28 19:16:59 +00:00
|
|
|
// localMessage == null -> message has never been created (not even headers)
|
2013-04-15 20:27:07 +00:00
|
|
|
// mFlagLoaded != FLAG_LOADED_COMPLETE -> message failed to sync completely
|
2012-06-28 19:16:59 +00:00
|
|
|
if (localMessage == null ||
|
2013-07-30 00:02:27 +00:00
|
|
|
(localMessage.mFlagLoaded != EmailContent.Message.FLAG_LOADED_COMPLETE &&
|
|
|
|
localMessage.mFlagLoaded != Message.FLAG_LOADED_PARTIAL)) {
|
2013-07-29 18:52:43 +00:00
|
|
|
LogUtils.d(Logging.LOG_TAG, "need to sync " + uid);
|
2012-06-28 19:16:59 +00:00
|
|
|
unsyncedMessages.add(message);
|
2013-07-29 18:52:43 +00:00
|
|
|
} else {
|
|
|
|
LogUtils.d(Logging.LOG_TAG, "don't need to sync " + uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count >= countNeeded) {
|
|
|
|
LogUtils.d(Logging.LOG_TAG, "loaded " + count + " messages, stopping");
|
|
|
|
break;
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 01:09:56 +00:00
|
|
|
} else {
|
2014-08-28 18:00:08 +00:00
|
|
|
if (DebugUtils.DEBUG) {
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.d(TAG, "*** Message count is zero??");
|
2012-08-03 01:09:56 +00:00
|
|
|
}
|
2013-04-15 20:27:07 +00:00
|
|
|
remoteFolder.close(false);
|
2012-08-03 01:09:56 +00:00
|
|
|
return;
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 17:53:40 +00:00
|
|
|
// Get "attachments" to be loaded
|
|
|
|
Cursor c = resolver.query(Attachment.CONTENT_URI, Attachment.CONTENT_PROJECTION,
|
|
|
|
AttachmentColumns.ACCOUNT_KEY + "=? AND " +
|
|
|
|
AttachmentColumns.UI_STATE + "=" + AttachmentState.DOWNLOADING,
|
|
|
|
new String[] {Long.toString(account.mId)}, null);
|
|
|
|
try {
|
2013-04-05 01:30:39 +00:00
|
|
|
final ContentValues values = new ContentValues();
|
2012-08-02 17:53:40 +00:00
|
|
|
while (c.moveToNext()) {
|
|
|
|
values.put(AttachmentColumns.UI_STATE, UIProvider.AttachmentState.SAVED);
|
|
|
|
Attachment att = new Attachment();
|
|
|
|
att.restore(c);
|
|
|
|
Message msg = Message.restoreMessageWithId(context, att.mMessageKey);
|
|
|
|
if (msg == null || (msg.mFlagLoaded == Message.FLAG_LOADED_COMPLETE)) {
|
|
|
|
values.put(AttachmentColumns.UI_DOWNLOADED_SIZE, att.mSize);
|
|
|
|
resolver.update(ContentUris.withAppendedId(Attachment.CONTENT_URI, att.mId),
|
|
|
|
values, null, null);
|
2012-06-28 19:16:59 +00:00
|
|
|
continue;
|
2012-08-02 17:53:40 +00:00
|
|
|
} else {
|
|
|
|
String uid = msg.mServerId;
|
|
|
|
Pop3Message popMessage = remoteUidMap.get(uid);
|
|
|
|
if (popMessage != null) {
|
2012-08-03 23:02:55 +00:00
|
|
|
Uri attUri = ContentUris.withAppendedId(Attachment.CONTENT_URI, att.mId);
|
2012-08-02 17:53:40 +00:00
|
|
|
try {
|
2012-08-03 23:02:55 +00:00
|
|
|
remoteFolder.fetchBody(popMessage, -1,
|
|
|
|
new FetchCallback(resolver, attUri));
|
2012-08-02 17:53:40 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new MessagingException(MessagingException.IOERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Say we've downloaded the attachment
|
|
|
|
values.put(AttachmentColumns.UI_STATE, AttachmentState.SAVED);
|
|
|
|
resolver.update(attUri, values, null, null);
|
|
|
|
|
|
|
|
int flag = EmailContent.Message.FLAG_LOADED_COMPLETE;
|
|
|
|
if (!popMessage.isComplete()) {
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.e(TAG, "How is this possible?");
|
2012-08-02 17:53:40 +00:00
|
|
|
}
|
|
|
|
Utilities.copyOneMessageToProvider(
|
|
|
|
context, popMessage, account, mailbox, flag);
|
|
|
|
// Get rid of the temporary attachment
|
|
|
|
resolver.delete(attUri, null, null);
|
|
|
|
|
2013-09-12 23:04:37 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Should we mark this attachment as failed so we don't
|
|
|
|
// keep trying to download?
|
|
|
|
LogUtils.e(TAG, "Could not find message for attachment " + uid);
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 17:53:40 +00:00
|
|
|
} finally {
|
|
|
|
c.close();
|
2012-06-28 19:16:59 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 17:53:40 +00:00
|
|
|
// Remove any messages that are in the local store but no longer on the remote store.
|
2012-06-28 19:16:59 +00:00
|
|
|
HashSet<String> localUidsToDelete = new HashSet<String>(localMessageMap.keySet());
|
|
|
|
localUidsToDelete.removeAll(remoteUidMap.keySet());
|
|
|
|
for (String uidToDelete : localUidsToDelete) {
|
2013-07-29 18:52:43 +00:00
|
|
|
LogUtils.d(Logging.LOG_TAG, "need to delete " + uidToDelete);
|
2012-06-28 19:16:59 +00:00
|
|
|
LocalMessageInfo infoToDelete = localMessageMap.get(uidToDelete);
|
|
|
|
|
|
|
|
// Delete associated data (attachment files)
|
2012-08-02 17:53:40 +00:00
|
|
|
// Attachment & Body records are auto-deleted when we delete the
|
|
|
|
// Message record
|
2012-06-28 19:16:59 +00:00
|
|
|
AttachmentUtilities.deleteAllAttachmentFiles(context, account.mId,
|
|
|
|
infoToDelete.mId);
|
|
|
|
|
|
|
|
// Delete the message itself
|
|
|
|
Uri uriToDelete = ContentUris.withAppendedId(
|
|
|
|
EmailContent.Message.CONTENT_URI, infoToDelete.mId);
|
|
|
|
resolver.delete(uriToDelete, null, null);
|
|
|
|
|
|
|
|
// Delete extra rows (e.g. synced or deleted)
|
2012-08-02 17:53:40 +00:00
|
|
|
Uri updateRowToDelete = ContentUris.withAppendedId(
|
2012-06-28 19:16:59 +00:00
|
|
|
EmailContent.Message.UPDATED_CONTENT_URI, infoToDelete.mId);
|
2012-08-02 17:53:40 +00:00
|
|
|
resolver.delete(updateRowToDelete, null, null);
|
|
|
|
Uri deleteRowToDelete = ContentUris.withAppendedId(
|
|
|
|
EmailContent.Message.DELETED_CONTENT_URI, infoToDelete.mId);
|
|
|
|
resolver.delete(deleteRowToDelete, null, null);
|
2013-09-12 21:33:07 +00:00
|
|
|
}
|
2012-06-28 19:16:59 +00:00
|
|
|
|
2013-07-29 18:52:43 +00:00
|
|
|
LogUtils.d(TAG, "loadUnsynchedMessages " + unsyncedMessages.size());
|
2012-08-02 17:53:40 +00:00
|
|
|
// Load messages we need to sync
|
2012-06-28 19:16:59 +00:00
|
|
|
loadUnsyncedMessages(context, account, remoteFolder, unsyncedMessages, mailbox);
|
|
|
|
|
2012-08-02 17:53:40 +00:00
|
|
|
// Clean up and report results
|
2012-06-28 19:16:59 +00:00
|
|
|
remoteFolder.close(false);
|
|
|
|
}
|
2012-08-02 17:53:40 +00:00
|
|
|
}
|