2012-09-20 20:34:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 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.AbstractThreadedSyncAdapter;
|
|
|
|
import android.content.ContentProviderClient;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SyncResult;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.IBinder;
|
|
|
|
|
|
|
|
import com.android.email.R;
|
|
|
|
import com.android.emailcommon.TempDirectory;
|
|
|
|
import com.android.emailcommon.mail.MessagingException;
|
|
|
|
import com.android.emailcommon.provider.Account;
|
|
|
|
import com.android.emailcommon.provider.EmailContent;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.AccountColumns;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.Message;
|
|
|
|
import com.android.emailcommon.provider.Mailbox;
|
|
|
|
import com.android.emailcommon.service.EmailServiceProxy;
|
2013-07-30 02:11:41 +00:00
|
|
|
import com.android.emailcommon.service.EmailServiceStatus;
|
2013-05-26 04:32:32 +00:00
|
|
|
import com.android.mail.utils.LogUtils;
|
2012-09-20 20:34:13 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
public class PopImapSyncAdapterService extends Service {
|
2013-04-02 18:36:11 +00:00
|
|
|
private static final String TAG = "PopImapSyncService";
|
2013-04-30 21:18:50 +00:00
|
|
|
private SyncAdapterImpl mSyncAdapter = null;
|
2012-09-20 20:34:13 +00:00
|
|
|
|
|
|
|
public PopImapSyncAdapterService() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class SyncAdapterImpl extends AbstractThreadedSyncAdapter {
|
|
|
|
public SyncAdapterImpl(Context context) {
|
|
|
|
super(context, true /* autoInitialize */);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPerformSync(android.accounts.Account account, Bundle extras,
|
|
|
|
String authority, ContentProviderClient provider, SyncResult syncResult) {
|
2013-06-25 22:15:02 +00:00
|
|
|
PopImapSyncAdapterService.performSync(getContext(), account, extras, provider,
|
|
|
|
syncResult);
|
2012-09-20 20:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2013-04-30 21:18:50 +00:00
|
|
|
mSyncAdapter = new SyncAdapterImpl(getApplicationContext());
|
2012-09-20 20:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
2013-04-30 21:18:50 +00:00
|
|
|
return mSyncAdapter.getSyncAdapterBinder();
|
2012-09-20 20:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return whether or not this mailbox retrieves its data from the server (as opposed to just
|
|
|
|
* a local mailbox that is never synced).
|
|
|
|
*/
|
|
|
|
private static boolean loadsFromServer(Context context, Mailbox m, String protocol) {
|
|
|
|
String legacyImapProtocol = context.getString(R.string.protocol_legacy_imap);
|
2012-09-22 00:49:26 +00:00
|
|
|
String pop3Protocol = context.getString(R.string.protocol_pop3);
|
2012-09-20 20:34:13 +00:00
|
|
|
if (legacyImapProtocol.equals(protocol)) {
|
|
|
|
// TODO: actually use a sync flag when creating the mailboxes. Right now we use an
|
|
|
|
// approximation for IMAP.
|
|
|
|
return m.mType != Mailbox.TYPE_DRAFTS
|
|
|
|
&& m.mType != Mailbox.TYPE_OUTBOX
|
|
|
|
&& m.mType != Mailbox.TYPE_SEARCH;
|
|
|
|
|
2012-09-22 00:49:26 +00:00
|
|
|
} else if (pop3Protocol.equals(protocol)) {
|
2012-09-20 20:34:13 +00:00
|
|
|
return Mailbox.TYPE_INBOX == m.mType;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-30 02:11:41 +00:00
|
|
|
private static void sync(final Context context, final long mailboxId,
|
|
|
|
final Bundle extras, final SyncResult syncResult, final boolean uiRefresh,
|
|
|
|
final int deltaMessageCount) {
|
2012-09-20 20:34:13 +00:00
|
|
|
TempDirectory.setTempDirectory(context);
|
|
|
|
Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
|
|
|
|
if (mailbox == null) return;
|
|
|
|
Account account = Account.restoreAccountWithId(context, mailbox.mAccountKey);
|
|
|
|
if (account == null) return;
|
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
String protocol = account.getProtocol(context);
|
|
|
|
if ((mailbox.mType != Mailbox.TYPE_OUTBOX) &&
|
|
|
|
!loadsFromServer(context, mailbox, protocol)) {
|
|
|
|
// This is an update to a message in a non-syncing mailbox; delete this from the
|
|
|
|
// updates table and return
|
|
|
|
resolver.delete(Message.UPDATED_CONTENT_URI, Message.MAILBOX_KEY + "=?",
|
|
|
|
new String[] {Long.toString(mailbox.mId)});
|
|
|
|
return;
|
|
|
|
}
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.d(TAG, "Mailbox: " + mailbox.mDisplayName);
|
2012-09-20 20:34:13 +00:00
|
|
|
|
|
|
|
Uri mailboxUri = ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId);
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
// Set mailbox sync state
|
|
|
|
values.put(Mailbox.UI_SYNC_STATUS,
|
|
|
|
uiRefresh ? EmailContent.SYNC_STATUS_USER : EmailContent.SYNC_STATUS_BACKGROUND);
|
|
|
|
resolver.update(mailboxUri, values, null, null);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String legacyImapProtocol = context.getString(R.string.protocol_legacy_imap);
|
|
|
|
if (mailbox.mType == Mailbox.TYPE_OUTBOX) {
|
|
|
|
EmailServiceStub.sendMailImpl(context, account.mId);
|
|
|
|
} else {
|
2013-07-30 02:11:41 +00:00
|
|
|
EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId,
|
|
|
|
EmailServiceStatus.IN_PROGRESS, 0);
|
|
|
|
final int status;
|
|
|
|
if (protocol.equals(legacyImapProtocol)) {
|
|
|
|
status = ImapService.synchronizeMailboxSynchronous(context, account,
|
2013-08-06 23:50:11 +00:00
|
|
|
mailbox, deltaMessageCount != 0, uiRefresh);
|
2013-07-30 02:11:41 +00:00
|
|
|
} else {
|
|
|
|
status = Pop3Service.synchronizeMailboxSynchronous(context, account,
|
|
|
|
mailbox, deltaMessageCount);
|
|
|
|
}
|
|
|
|
EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId, status, 0);
|
2012-09-20 20:34:13 +00:00
|
|
|
}
|
|
|
|
} catch (MessagingException e) {
|
|
|
|
int cause = e.getExceptionType();
|
2013-10-01 23:11:54 +00:00
|
|
|
// XXX It's no good to put the MessagingException.cause here, that's not the
|
|
|
|
// same set of values that we use in EmailServiceStatus.
|
2013-07-30 02:11:41 +00:00
|
|
|
EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId, cause, 0);
|
2012-09-20 20:34:13 +00:00
|
|
|
switch(cause) {
|
|
|
|
case MessagingException.IOERROR:
|
|
|
|
syncResult.stats.numIoExceptions++;
|
|
|
|
break;
|
|
|
|
case MessagingException.AUTHENTICATION_FAILED:
|
|
|
|
syncResult.stats.numAuthExceptions++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
2013-04-05 01:30:39 +00:00
|
|
|
// Always clear our sync state and update sync time.
|
2012-09-20 20:34:13 +00:00
|
|
|
values.put(Mailbox.UI_SYNC_STATUS, EmailContent.SYNC_STATUS_NONE);
|
2013-04-05 01:30:39 +00:00
|
|
|
values.put(Mailbox.SYNC_TIME, System.currentTimeMillis());
|
2012-09-20 20:34:13 +00:00
|
|
|
resolver.update(mailboxUri, values, null, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Partial integration with system SyncManager; we initiate manual syncs upon request
|
|
|
|
*/
|
|
|
|
private static void performSync(Context context, android.accounts.Account account,
|
2013-06-25 22:15:02 +00:00
|
|
|
Bundle extras, ContentProviderClient provider, SyncResult syncResult) {
|
2012-09-20 20:34:13 +00:00
|
|
|
// Find an EmailProvider account with the Account's email address
|
|
|
|
Cursor c = null;
|
|
|
|
try {
|
|
|
|
c = provider.query(com.android.emailcommon.provider.Account.CONTENT_URI,
|
|
|
|
Account.CONTENT_PROJECTION, AccountColumns.EMAIL_ADDRESS + "=?",
|
|
|
|
new String[] {account.name}, null);
|
|
|
|
if (c != null && c.moveToNext()) {
|
|
|
|
Account acct = new Account();
|
|
|
|
acct.restore(c);
|
|
|
|
if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD)) {
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.d(TAG, "Upload sync request for " + acct.mDisplayName);
|
2012-09-20 20:34:13 +00:00
|
|
|
// See if any boxes have mail...
|
|
|
|
ArrayList<Long> mailboxesToUpdate;
|
|
|
|
Cursor updatesCursor = provider.query(Message.UPDATED_CONTENT_URI,
|
|
|
|
new String[] {Message.MAILBOX_KEY},
|
|
|
|
Message.ACCOUNT_KEY + "=?",
|
|
|
|
new String[] {Long.toString(acct.mId)},
|
|
|
|
null);
|
|
|
|
try {
|
|
|
|
if ((updatesCursor == null) || (updatesCursor.getCount() == 0)) return;
|
|
|
|
mailboxesToUpdate = new ArrayList<Long>();
|
|
|
|
while (updatesCursor.moveToNext()) {
|
|
|
|
Long mailboxId = updatesCursor.getLong(0);
|
|
|
|
if (!mailboxesToUpdate.contains(mailboxId)) {
|
|
|
|
mailboxesToUpdate.add(mailboxId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (updatesCursor != null) {
|
|
|
|
updatesCursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (long mailboxId: mailboxesToUpdate) {
|
2013-07-30 02:11:41 +00:00
|
|
|
sync(context, mailboxId, extras, syncResult, false, 0);
|
2012-09-20 20:34:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-05-26 04:32:32 +00:00
|
|
|
LogUtils.d(TAG, "Sync request for " + acct.mDisplayName);
|
|
|
|
LogUtils.d(TAG, extras.toString());
|
2013-09-13 20:18:56 +00:00
|
|
|
|
|
|
|
// We update our folder structure on every sync.
|
|
|
|
final EmailServiceProxy service =
|
|
|
|
EmailServiceUtils.getServiceForAccount(context, acct.mId);
|
|
|
|
service.updateFolderList(acct.mId);
|
|
|
|
|
2013-09-14 00:49:12 +00:00
|
|
|
// Get the id for the mailbox we want to sync.
|
2013-10-09 18:03:40 +00:00
|
|
|
long [] mailboxIds = Mailbox.getMailboxIdsFromBundle(extras);
|
|
|
|
if (mailboxIds == null || mailboxIds.length == 0) {
|
|
|
|
// No mailbox specified, just sync the inbox.
|
2013-09-14 00:49:12 +00:00
|
|
|
// TODO: IMAP may eventually want to allow multiple auto-sync mailboxes.
|
2013-10-09 18:03:40 +00:00
|
|
|
final long inboxId = Mailbox.findMailboxOfType(context, acct.mId,
|
2013-09-14 00:49:12 +00:00
|
|
|
Mailbox.TYPE_INBOX);
|
2013-10-09 18:03:40 +00:00
|
|
|
if (inboxId != Mailbox.NO_MAILBOX) {
|
|
|
|
mailboxIds = new long[1];
|
|
|
|
mailboxIds[0] = inboxId;
|
|
|
|
}
|
2013-09-14 00:49:12 +00:00
|
|
|
}
|
2013-10-09 18:03:40 +00:00
|
|
|
|
|
|
|
if (mailboxIds != null) {
|
|
|
|
boolean uiRefresh =
|
2012-09-20 20:34:13 +00:00
|
|
|
extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
|
2013-10-09 18:03:40 +00:00
|
|
|
int deltaMessageCount =
|
|
|
|
extras.getInt(Mailbox.SYNC_EXTRA_DELTA_MESSAGE_COUNT, 0);
|
|
|
|
for (long mailboxId : mailboxIds) {
|
|
|
|
sync(context, mailboxId, extras, syncResult, uiRefresh, deltaMessageCount);
|
|
|
|
}
|
|
|
|
}
|
2012-09-20 20:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
if (c != null) {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-30 02:11:41 +00:00
|
|
|
}
|