PopImapSyncAdapterService.java revision e6cc662abc0b5fffe223cda5e980b4f05a4e91dd
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 com.android.email.Controller; 20import com.android.email.provider.EmailContent; 21import com.android.email.provider.EmailContent.Mailbox; 22 23import android.accounts.Account; 24import android.accounts.OperationCanceledException; 25import android.app.Service; 26import android.content.AbstractThreadedSyncAdapter; 27import android.content.ContentProviderClient; 28import android.content.ContentResolver; 29import android.content.Context; 30import android.content.Intent; 31import android.content.SyncResult; 32import android.database.Cursor; 33import android.os.Bundle; 34import android.os.IBinder; 35import android.util.Log; 36 37public class PopImapSyncAdapterService extends Service { 38 private static final String TAG = "PopImapSyncAdapterService"; 39 private static SyncAdapterImpl sSyncAdapter = null; 40 private static final Object sSyncAdapterLock = new Object(); 41 42 public PopImapSyncAdapterService() { 43 super(); 44 } 45 46 private static class SyncAdapterImpl extends AbstractThreadedSyncAdapter { 47 private Context mContext; 48 49 public SyncAdapterImpl(Context context) { 50 super(context, true /* autoInitialize */); 51 mContext = context; 52 } 53 54 @Override 55 public void onPerformSync(Account account, Bundle extras, 56 String authority, ContentProviderClient provider, SyncResult syncResult) { 57 try { 58 PopImapSyncAdapterService.performSync(mContext, account, extras, 59 authority, provider, syncResult); 60 } catch (OperationCanceledException e) { 61 } 62 } 63 } 64 65 @Override 66 public void onCreate() { 67 super.onCreate(); 68 synchronized (sSyncAdapterLock) { 69 if (sSyncAdapter == null) { 70 sSyncAdapter = new SyncAdapterImpl(getApplicationContext()); 71 } 72 } 73 } 74 75 @Override 76 public IBinder onBind(Intent intent) { 77 return sSyncAdapter.getSyncAdapterBinder(); 78 } 79 80 /** 81 * Partial integration with system SyncManager; we initiate manual syncs upon request 82 */ 83 private static void performSync(Context context, Account account, Bundle extras, 84 String authority, ContentProviderClient provider, SyncResult syncResult) 85 throws OperationCanceledException { 86 if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false)) { 87 String emailAddress = account.name; 88 // Find an EmailProvider account with the Account's email address 89 Cursor c = context.getContentResolver().query(EmailContent.Account.CONTENT_URI, 90 EmailContent.ID_PROJECTION, EmailContent.Account.EMAIL_ADDRESS + "=?", 91 new String[] {emailAddress}, null); 92 if (c.moveToNext()) { 93 // If we have one, find the inbox and start it syncing 94 long accountId = c.getLong(EmailContent.Account.ID_PROJECTION_COLUMN); 95 long mailboxId = EmailContent.Mailbox.findMailboxOfType(context, accountId, 96 Mailbox.TYPE_INBOX); 97 if (mailboxId > 0) { 98 Log.d(TAG, "Starting manual sync for account " + emailAddress); 99 Controller.getInstance(context).updateMailbox(accountId, mailboxId); 100 } 101 } 102 } 103 } 104}