PopImapSyncAdapterService.java revision f5418f1f93b02e7fab9f15eb201800b65510998e
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.service;
18
19import android.accounts.Account;
20import android.accounts.OperationCanceledException;
21import android.app.Service;
22import android.content.AbstractThreadedSyncAdapter;
23import android.content.ContentProviderClient;
24import android.content.ContentResolver;
25import android.content.Context;
26import android.content.Intent;
27import android.content.SyncResult;
28import android.database.Cursor;
29import android.os.Bundle;
30import android.os.IBinder;
31import android.util.Log;
32
33import com.android.email.Controller;
34import com.android.emailcommon.provider.EmailContent;
35import com.android.emailcommon.provider.EmailContent.AccountColumns;
36import com.android.emailcommon.provider.Mailbox;
37
38public class PopImapSyncAdapterService extends Service {
39    private static final String TAG = "PopImapSyncAdapterService";
40    private static SyncAdapterImpl sSyncAdapter = null;
41    private static final Object sSyncAdapterLock = new Object();
42
43    public PopImapSyncAdapterService() {
44        super();
45    }
46
47    private static class SyncAdapterImpl extends AbstractThreadedSyncAdapter {
48        private Context mContext;
49
50        public SyncAdapterImpl(Context context) {
51            super(context, true /* autoInitialize */);
52            mContext = context;
53        }
54
55        @Override
56        public void onPerformSync(Account account, Bundle extras,
57                String authority, ContentProviderClient provider, SyncResult syncResult) {
58            try {
59                PopImapSyncAdapterService.performSync(mContext, account, extras,
60                        authority, provider, syncResult);
61            } catch (OperationCanceledException e) {
62            }
63        }
64    }
65
66    @Override
67    public void onCreate() {
68        super.onCreate();
69        synchronized (sSyncAdapterLock) {
70            if (sSyncAdapter == null) {
71                sSyncAdapter = new SyncAdapterImpl(getApplicationContext());
72            }
73        }
74    }
75
76    @Override
77    public IBinder onBind(Intent intent) {
78        return sSyncAdapter.getSyncAdapterBinder();
79    }
80
81    /**
82     * Partial integration with system SyncManager; we initiate manual syncs upon request
83     */
84    private static void performSync(Context context, Account account, Bundle extras,
85            String authority, ContentProviderClient provider, SyncResult syncResult)
86            throws OperationCanceledException {
87        if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false)) {
88            String emailAddress = account.name;
89            // Find an EmailProvider account with the Account's email address
90            Cursor c = context.getContentResolver().query(
91                    com.android.emailcommon.provider.Account.CONTENT_URI,
92                    EmailContent.ID_PROJECTION, AccountColumns.EMAIL_ADDRESS + "=?",
93                    new String[] {emailAddress}, null);
94            if (c.moveToNext()) {
95                // If we have one, find the inbox and start it syncing
96                long accountId = c.getLong(EmailContent.ID_PROJECTION_COLUMN);
97                long mailboxId = Mailbox.findMailboxOfType(context, accountId,
98                        Mailbox.TYPE_INBOX);
99                if (mailboxId > 0) {
100                    Log.d(TAG, "Starting manual sync for account " + emailAddress);
101                    Controller.getInstance(context).updateMailbox(accountId, mailboxId, false);
102                }
103            }
104        }
105    }
106}