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;
|
2015-04-04 18:42:30 +00:00
|
|
|
import com.android.email.service.EmailServiceUtils.EmailServiceInfo;
|
2012-09-20 20:34:13 +00:00
|
|
|
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;
|
2014-04-11 21:42:28 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent.MessageColumns;
|
2012-09-20 20:34:13 +00:00
|
|
|
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-10-18 00:46:26 +00:00
|
|
|
import com.android.mail.providers.UIProvider;
|
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";
|
2015-05-01 19:35:23 +00:00
|
|
|
private AbstractThreadedSyncAdapter mSyncAdapter = null;
|
2012-09-20 20:34:13 +00:00
|
|
|
|
2015-04-04 18:42:30 +00:00
|
|
|
private static String sPop3Protocol;
|
|
|
|
private static String sLegacyImapProtocol;
|
|
|
|
|
2012-09-20 20:34:13 +00:00
|
|
|
public PopImapSyncAdapterService() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2015-05-01 19:35:23 +00:00
|
|
|
static class SyncAdapterImpl extends AbstractThreadedSyncAdapter {
|
2012-09-20 20:34:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 19:35:23 +00:00
|
|
|
public AbstractThreadedSyncAdapter getSyncAdapter() {
|
|
|
|
return new SyncAdapterImpl(getApplicationContext());
|
|
|
|
}
|
|
|
|
|
2012-09-20 20:34:13 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2015-05-01 19:35:23 +00:00
|
|
|
mSyncAdapter = getSyncAdapter();
|
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).
|
|
|
|
*/
|
2015-04-04 18:42:30 +00:00
|
|
|
private static boolean loadsFromServer(Context context, Mailbox m, Account acct) {
|
|
|
|
if (isLegacyImapProtocol(context, acct)) {
|
2012-09-20 20:34:13 +00:00
|
|
|
// 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;
|
|
|
|
|
2015-04-04 18:42:30 +00:00
|
|
|
} else if (isPop3Protocol(context, acct)) {
|
2012-09-20 20:34:13 +00:00
|
|
|
return Mailbox.TYPE_INBOX == m.mType;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-01 19:35:23 +00:00
|
|
|
private static boolean sync(final Context context, final long mailboxId,
|
2013-07-30 02:11:41 +00:00
|
|
|
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);
|
2015-05-01 19:35:23 +00:00
|
|
|
if (mailbox == null) return false;
|
2012-09-20 20:34:13 +00:00
|
|
|
Account account = Account.restoreAccountWithId(context, mailbox.mAccountKey);
|
2015-05-01 19:35:23 +00:00
|
|
|
if (account == null) return false;
|
2012-09-20 20:34:13 +00:00
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
if ((mailbox.mType != Mailbox.TYPE_OUTBOX) &&
|
2015-04-04 18:42:30 +00:00
|
|
|
!loadsFromServer(context, mailbox, account)) {
|
2012-09-20 20:34:13 +00:00
|
|
|
// This is an update to a message in a non-syncing mailbox; delete this from the
|
|
|
|
// updates table and return
|
2014-04-11 21:42:28 +00:00
|
|
|
resolver.delete(Message.UPDATED_CONTENT_URI, MessageColumns.MAILBOX_KEY + "=?",
|
2012-09-20 20:34:13 +00:00
|
|
|
new String[] {Long.toString(mailbox.mId)});
|
2015-05-01 19:35:23 +00:00
|
|
|
return true;
|
2012-09-20 20:34:13 +00:00
|
|
|
}
|
2013-10-16 23:12:11 +00:00
|
|
|
LogUtils.d(TAG, "About to sync 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
|
2014-09-30 00:51:27 +00:00
|
|
|
final int syncStatus = uiRefresh ? EmailContent.SYNC_STATUS_USER :
|
|
|
|
EmailContent.SYNC_STATUS_BACKGROUND;
|
|
|
|
values.put(Mailbox.UI_SYNC_STATUS, syncStatus);
|
2012-09-20 20:34:13 +00:00
|
|
|
resolver.update(mailboxUri, values, null, null);
|
|
|
|
try {
|
2014-09-30 00:51:27 +00:00
|
|
|
int lastSyncResult;
|
2012-09-20 20:34:13 +00:00
|
|
|
try {
|
|
|
|
if (mailbox.mType == Mailbox.TYPE_OUTBOX) {
|
|
|
|
EmailServiceStub.sendMailImpl(context, account.mId);
|
|
|
|
} else {
|
2014-09-30 00:51:27 +00:00
|
|
|
lastSyncResult = UIProvider.createSyncValue(syncStatus,
|
|
|
|
EmailContent.LAST_SYNC_RESULT_SUCCESS);
|
2013-07-30 02:11:41 +00:00
|
|
|
EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId,
|
2014-09-30 00:51:27 +00:00
|
|
|
EmailServiceStatus.IN_PROGRESS, 0, lastSyncResult);
|
2013-07-30 02:11:41 +00:00
|
|
|
final int status;
|
2015-04-04 18:42:30 +00:00
|
|
|
if (isLegacyImapProtocol(context, account)) {
|
2013-07-30 02:11:41 +00:00
|
|
|
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);
|
|
|
|
}
|
2013-10-18 00:46:26 +00:00
|
|
|
EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId, status, 0,
|
2014-09-30 00:51:27 +00:00
|
|
|
lastSyncResult);
|
2015-05-01 19:35:23 +00:00
|
|
|
return true;
|
2012-09-20 20:34:13 +00:00
|
|
|
}
|
|
|
|
} catch (MessagingException e) {
|
2014-01-30 20:00:03 +00:00
|
|
|
final int type = e.getExceptionType();
|
|
|
|
// type must be translated into the domain of values used by EmailServiceStatus
|
2014-09-30 00:51:27 +00:00
|
|
|
switch (type) {
|
2012-09-20 20:34:13 +00:00
|
|
|
case MessagingException.IOERROR:
|
2014-09-30 00:51:27 +00:00
|
|
|
lastSyncResult = UIProvider.createSyncValue(syncStatus,
|
|
|
|
EmailContent.LAST_SYNC_RESULT_CONNECTION_ERROR);
|
|
|
|
EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId,
|
|
|
|
EmailServiceStatus.FAILURE, 0, lastSyncResult);
|
2012-09-20 20:34:13 +00:00
|
|
|
syncResult.stats.numIoExceptions++;
|
|
|
|
break;
|
|
|
|
case MessagingException.AUTHENTICATION_FAILED:
|
2014-09-30 00:51:27 +00:00
|
|
|
lastSyncResult = UIProvider.createSyncValue(syncStatus,
|
|
|
|
EmailContent.LAST_SYNC_RESULT_AUTH_ERROR);
|
|
|
|
EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId,
|
|
|
|
EmailServiceStatus.FAILURE, 0, lastSyncResult);
|
2012-09-20 20:34:13 +00:00
|
|
|
syncResult.stats.numAuthExceptions++;
|
|
|
|
break;
|
2014-01-30 20:00:03 +00:00
|
|
|
case MessagingException.SERVER_ERROR:
|
2014-09-30 00:51:27 +00:00
|
|
|
lastSyncResult = UIProvider.createSyncValue(syncStatus,
|
|
|
|
EmailContent.LAST_SYNC_RESULT_SERVER_ERROR);
|
|
|
|
EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId,
|
|
|
|
EmailServiceStatus.FAILURE, 0, lastSyncResult);
|
2014-01-30 20:00:03 +00:00
|
|
|
break;
|
2013-10-18 00:46:26 +00:00
|
|
|
|
|
|
|
default:
|
2014-09-30 00:51:27 +00:00
|
|
|
lastSyncResult = UIProvider.createSyncValue(syncStatus,
|
|
|
|
EmailContent.LAST_SYNC_RESULT_INTERNAL_ERROR);
|
|
|
|
EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId,
|
|
|
|
EmailServiceStatus.FAILURE, 0, lastSyncResult);
|
2012-09-20 20:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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);
|
|
|
|
}
|
2015-05-01 19:35:23 +00:00
|
|
|
return false;
|
2012-09-20 20:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
2014-04-11 21:42:28 +00:00
|
|
|
new String[] {MessageColumns.MAILBOX_KEY},
|
|
|
|
MessageColumns.ACCOUNT_KEY + "=?",
|
2012-09-20 20:34:13 +00:00
|
|
|
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) {
|
2015-04-04 18:42:30 +00:00
|
|
|
EmailServiceInfo info = EmailServiceUtils.getServiceInfo(
|
|
|
|
context, acct.getProtocol(context));
|
|
|
|
// No mailbox specified, check if the protocol support auto-sync
|
|
|
|
// multiple mailboxes. In that case retrieve the mailboxes to sync
|
|
|
|
// from the account settings. Otherwise just sync the inbox.
|
|
|
|
if (info.offerLookback) {
|
|
|
|
mailboxIds = getLoopBackMailboxIdsForSync(context, acct);
|
2015-05-01 19:35:23 +00:00
|
|
|
}
|
2015-06-03 18:01:16 +00:00
|
|
|
if (mailboxIds == null || mailboxIds.length == 0) {
|
2015-04-04 18:42:30 +00:00
|
|
|
final long inboxId = Mailbox.findMailboxOfType(context, acct.mId,
|
|
|
|
Mailbox.TYPE_INBOX);
|
|
|
|
if (inboxId != Mailbox.NO_MAILBOX) {
|
|
|
|
mailboxIds = new long[1];
|
|
|
|
mailboxIds[0] = inboxId;
|
|
|
|
}
|
2013-10-09 18:03:40 +00:00
|
|
|
}
|
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);
|
2015-05-01 19:35:23 +00:00
|
|
|
boolean success = mailboxIds.length > 0;
|
2013-10-09 18:03:40 +00:00
|
|
|
for (long mailboxId : mailboxIds) {
|
2015-05-01 19:35:23 +00:00
|
|
|
boolean result = sync(context, mailboxId, extras, syncResult,
|
|
|
|
uiRefresh, deltaMessageCount);
|
|
|
|
if (!result) {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initial sync performed?
|
|
|
|
if (success) {
|
|
|
|
// All mailboxes (that need a sync) are now synced. Assume we
|
|
|
|
// have a valid sync key, in case this account has push support
|
|
|
|
markAsInitialSyncKey(context, acct.mId);
|
2013-10-09 18:03:40 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-20 20:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
if (c != null) {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-04 18:42:30 +00:00
|
|
|
|
2015-05-01 19:35:23 +00:00
|
|
|
private static void markAsInitialSyncKey(Context context, long accountId) {
|
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
Uri accountUri = ContentUris.withAppendedId(Account.CONTENT_URI, accountId);
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(AccountColumns.SYNC_KEY, "1");
|
|
|
|
resolver.update(accountUri, values, null, null);
|
|
|
|
}
|
|
|
|
|
2015-04-04 18:42:30 +00:00
|
|
|
private static boolean isLegacyImapProtocol(Context ctx, Account acct) {
|
|
|
|
if (sLegacyImapProtocol == null) {
|
|
|
|
sLegacyImapProtocol = ctx.getString(R.string.protocol_legacy_imap);
|
|
|
|
}
|
|
|
|
return acct.getProtocol(ctx).equals(sLegacyImapProtocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isPop3Protocol(Context ctx, Account acct) {
|
|
|
|
if (sPop3Protocol == null) {
|
|
|
|
sPop3Protocol = ctx.getString(R.string.protocol_pop3);
|
|
|
|
}
|
|
|
|
return acct.getProtocol(ctx).equals(sPop3Protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static long[] getLoopBackMailboxIdsForSync(Context ctx, Account acct) {
|
|
|
|
final Cursor c = Mailbox.getLoopBackMailboxIdsForSync(ctx.getContentResolver(), acct.mId);
|
|
|
|
if (c == null) {
|
|
|
|
// No mailboxes
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
long[] mailboxes = new long[c.getCount()];
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
mailboxes[i] = c.getLong(Mailbox.CONTENT_ID_COLUMN);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
return mailboxes;
|
|
|
|
}
|
2013-07-30 02:11:41 +00:00
|
|
|
}
|