summaryrefslogtreecommitdiffstats
path: root/provider_src/com/android/email/service/PopImapSyncAdapterService.java
blob: 5e19478dde2e27746d974f3b4ced0863e50b47e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * 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.email.service.EmailServiceUtils.EmailServiceInfo;
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.EmailContent.MessageColumns;
import com.android.emailcommon.provider.Mailbox;
import com.android.emailcommon.service.EmailServiceProxy;
import com.android.emailcommon.service.EmailServiceStatus;
import com.android.mail.providers.UIProvider;
import com.android.mail.utils.LogUtils;

import java.util.ArrayList;

public class PopImapSyncAdapterService extends Service {
    private static final String TAG = "PopImapSyncService";
    private AbstractThreadedSyncAdapter mSyncAdapter = null;

    private static String sPop3Protocol;
    private static String sLegacyImapProtocol;

    public PopImapSyncAdapterService() {
        super();
    }

    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) {
            PopImapSyncAdapterService.performSync(getContext(), account, extras, provider,
                    syncResult);
        }
    }

    public AbstractThreadedSyncAdapter getSyncAdapter() {
        return new SyncAdapterImpl(getApplicationContext());
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mSyncAdapter = getSyncAdapter();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mSyncAdapter.getSyncAdapterBinder();
    }

    /**
     * @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, Account acct) {
        if (isLegacyImapProtocol(context, acct)) {
            // 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;

        } else if (isPop3Protocol(context, acct)) {
            return Mailbox.TYPE_INBOX == m.mType;
        }

        return false;
    }

    private static boolean sync(final Context context, final long mailboxId,
            final Bundle extras, final SyncResult syncResult, final boolean uiRefresh,
            final int deltaMessageCount) {
        TempDirectory.setTempDirectory(context);
        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
        if (mailbox == null) return false;
        Account account = Account.restoreAccountWithId(context, mailbox.mAccountKey);
        if (account == null) return false;
        ContentResolver resolver = context.getContentResolver();
        if ((mailbox.mType != Mailbox.TYPE_OUTBOX) &&
                !loadsFromServer(context, mailbox, account)) {
            // 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, MessageColumns.MAILBOX_KEY + "=?",
                    new String[] {Long.toString(mailbox.mId)});
            return true;
        }
        LogUtils.d(TAG, "About to sync mailbox: " + mailbox.mDisplayName);

        Uri mailboxUri = ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId);
        ContentValues values = new ContentValues();
        // Set mailbox sync state
        final int syncStatus = uiRefresh ? EmailContent.SYNC_STATUS_USER :
                EmailContent.SYNC_STATUS_BACKGROUND;
        values.put(Mailbox.UI_SYNC_STATUS, syncStatus);
        resolver.update(mailboxUri, values, null, null);
        try {
            int lastSyncResult;
            try {
                if (mailbox.mType == Mailbox.TYPE_OUTBOX) {
                    EmailServiceStub.sendMailImpl(context, account.mId);
                } else {
                    lastSyncResult = UIProvider.createSyncValue(syncStatus,
                            EmailContent.LAST_SYNC_RESULT_SUCCESS);
                    EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId,
                            EmailServiceStatus.IN_PROGRESS, 0, lastSyncResult);
                    final int status;
                    if (isLegacyImapProtocol(context, account)) {
                        status = ImapService.synchronizeMailboxSynchronous(context, account,
                                mailbox, deltaMessageCount != 0, uiRefresh);
                    } else {
                        status = Pop3Service.synchronizeMailboxSynchronous(context, account,
                                mailbox, deltaMessageCount);
                    }
                    EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId, status, 0,
                            lastSyncResult);
                    return true;
                }
            } catch (MessagingException e) {
                final int type = e.getExceptionType();
                // type must be translated into the domain of values used by EmailServiceStatus
                switch (type) {
                    case MessagingException.IOERROR:
                        lastSyncResult = UIProvider.createSyncValue(syncStatus,
                                EmailContent.LAST_SYNC_RESULT_CONNECTION_ERROR);
                        EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId,
                                EmailServiceStatus.FAILURE, 0, lastSyncResult);
                        syncResult.stats.numIoExceptions++;
                        break;
                    case MessagingException.AUTHENTICATION_FAILED:
                        lastSyncResult = UIProvider.createSyncValue(syncStatus,
                                EmailContent.LAST_SYNC_RESULT_AUTH_ERROR);
                        EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId,
                                EmailServiceStatus.FAILURE, 0, lastSyncResult);
                        syncResult.stats.numAuthExceptions++;
                        break;
                    case MessagingException.SERVER_ERROR:
                        lastSyncResult = UIProvider.createSyncValue(syncStatus,
                                EmailContent.LAST_SYNC_RESULT_SERVER_ERROR);
                        EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId,
                                EmailServiceStatus.FAILURE, 0, lastSyncResult);
                        break;

                    default:
                        lastSyncResult = UIProvider.createSyncValue(syncStatus,
                                EmailContent.LAST_SYNC_RESULT_INTERNAL_ERROR);
                        EmailServiceStatus.syncMailboxStatus(resolver, extras, mailboxId,
                                EmailServiceStatus.FAILURE, 0, lastSyncResult);
                }
            }
        } finally {
            // Always clear our sync state and update sync time.
            values.put(Mailbox.UI_SYNC_STATUS, EmailContent.SYNC_STATUS_NONE);
            values.put(Mailbox.SYNC_TIME, System.currentTimeMillis());
            resolver.update(mailboxUri, values, null, null);
        }
        return false;
    }

    /**
     * Partial integration with system SyncManager; we initiate manual syncs upon request
     */
    private static void performSync(Context context, android.accounts.Account account,
            Bundle extras, ContentProviderClient provider, SyncResult syncResult) {
        // 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)) {
                    LogUtils.d(TAG, "Upload sync request for " + acct.mDisplayName);
                    // See if any boxes have mail...
                    ArrayList<Long> mailboxesToUpdate;
                    Cursor updatesCursor = provider.query(Message.UPDATED_CONTENT_URI,
                            new String[] {MessageColumns.MAILBOX_KEY},
                            MessageColumns.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) {
                        sync(context, mailboxId, extras, syncResult, false, 0);
                    }
                } else {
                    LogUtils.d(TAG, "Sync request for " + acct.mDisplayName);
                    LogUtils.d(TAG, extras.toString());

                    // We update our folder structure on every sync.
                    final EmailServiceProxy service =
                            EmailServiceUtils.getServiceForAccount(context, acct.mId);
                    service.updateFolderList(acct.mId);

                    // Get the id for the mailbox we want to sync.
                    long [] mailboxIds = Mailbox.getMailboxIdsFromBundle(extras);
                    if (mailboxIds == null || mailboxIds.length == 0) {
                        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);
                        }
                        if (mailboxIds.length == 0) {
                            final long inboxId = Mailbox.findMailboxOfType(context, acct.mId,
                                    Mailbox.TYPE_INBOX);
                            if (inboxId != Mailbox.NO_MAILBOX) {
                                mailboxIds = new long[1];
                                mailboxIds[0] = inboxId;
                            }
                        }
                    }

                    if (mailboxIds != null) {
                        boolean uiRefresh =
                            extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
                        int deltaMessageCount =
                                extras.getInt(Mailbox.SYNC_EXTRA_DELTA_MESSAGE_COUNT, 0);
                        boolean success = mailboxIds.length > 0;
                        for (long mailboxId : mailboxIds) {
                            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);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (c != null) {
                c.close();
            }
        }
    }

    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);
    }

    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;
    }
}