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 17 package com.android.exchange; 18 19 import com.android.emailcommon.provider.EmailContent; 20 import com.android.emailcommon.provider.EmailContent.AccountColumns; 21 import com.android.emailcommon.provider.EmailContent.MailboxColumns; 22 import com.android.emailcommon.provider.Mailbox; 23 24 import android.accounts.Account; 25 import android.accounts.OperationCanceledException; 26 import android.app.Service; 27 import android.content.AbstractThreadedSyncAdapter; 28 import android.content.ContentProviderClient; 29 import android.content.ContentResolver; 30 import android.content.Context; 31 import android.content.Intent; 32 import android.content.SyncResult; 33 import android.database.Cursor; 34 import android.os.Bundle; 35 import android.os.IBinder; 36 import android.util.Log; 37 38 public class EmailSyncAdapterService extends Service { 39 private static final String TAG = "EAS EmailSyncAdapterService"; 40 private static SyncAdapterImpl sSyncAdapter = null; 41 private static final Object sSyncAdapterLock = new Object(); 42 43 private static final String[] ID_PROJECTION = new String[] {EmailContent.RECORD_ID}; 44 private static final String ACCOUNT_AND_TYPE_INBOX = 45 MailboxColumns.ACCOUNT_KEY + "=? AND " + MailboxColumns.TYPE + '=' + Mailbox.TYPE_INBOX; 46 EmailSyncAdapterService()47 public EmailSyncAdapterService() { 48 super(); 49 } 50 51 private static class SyncAdapterImpl extends AbstractThreadedSyncAdapter { 52 private Context mContext; 53 SyncAdapterImpl(Context context)54 public SyncAdapterImpl(Context context) { 55 super(context, true /* autoInitialize */); 56 mContext = context; 57 } 58 59 @Override onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)60 public void onPerformSync(Account account, Bundle extras, 61 String authority, ContentProviderClient provider, SyncResult syncResult) { 62 try { 63 EmailSyncAdapterService.performSync(mContext, account, extras, 64 authority, provider, syncResult); 65 } catch (OperationCanceledException e) { 66 } 67 } 68 } 69 70 @Override onCreate()71 public void onCreate() { 72 super.onCreate(); 73 synchronized (sSyncAdapterLock) { 74 if (sSyncAdapter == null) { 75 sSyncAdapter = new SyncAdapterImpl(getApplicationContext()); 76 } 77 } 78 } 79 80 @Override onBind(Intent intent)81 public IBinder onBind(Intent intent) { 82 return sSyncAdapter.getSyncAdapterBinder(); 83 } 84 85 /** 86 * Partial integration with system SyncManager; we tell our EAS ExchangeService to start an 87 * inbox sync when we get the signal from the system SyncManager. 88 */ performSync(Context context, Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)89 private static void performSync(Context context, Account account, Bundle extras, 90 String authority, ContentProviderClient provider, SyncResult syncResult) 91 throws OperationCanceledException { 92 ContentResolver cr = context.getContentResolver(); 93 Log.i(TAG, "performSync"); 94 95 // Find the (EmailProvider) account associated with this email address 96 Cursor accountCursor = 97 cr.query(com.android.emailcommon.provider.Account.CONTENT_URI, 98 ID_PROJECTION, AccountColumns.EMAIL_ADDRESS + "=?", new String[] {account.name}, 99 null); 100 try { 101 if (accountCursor.moveToFirst()) { 102 long accountId = accountCursor.getLong(0); 103 // Now, find the inbox associated with the account 104 Cursor mailboxCursor = cr.query(Mailbox.CONTENT_URI, ID_PROJECTION, 105 ACCOUNT_AND_TYPE_INBOX, new String[] {Long.toString(accountId)}, null); 106 try { 107 if (mailboxCursor.moveToFirst()) { 108 Log.i(TAG, "Mail sync requested for " + account.name); 109 // Ask for a sync from our sync manager 110 ExchangeService.serviceRequest(mailboxCursor.getLong(0), 111 ExchangeService.SYNC_KICK); 112 } 113 } finally { 114 mailboxCursor.close(); 115 } 116 } 117 } finally { 118 accountCursor.close(); 119 } 120 } 121 }