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.service; 18 19 import android.accounts.Account; 20 import android.content.AbstractThreadedSyncAdapter; 21 import android.content.ContentProviderClient; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.SyncResult; 25 import android.database.Cursor; 26 import android.os.Bundle; 27 import android.provider.CalendarContract.Events; 28 import android.util.Log; 29 30 import com.android.emailcommon.provider.EmailContent; 31 import com.android.emailcommon.provider.EmailContent.MailboxColumns; 32 import com.android.emailcommon.provider.Mailbox; 33 import com.android.exchange.Eas; 34 import com.android.mail.utils.LogUtils; 35 36 public class CalendarSyncAdapterService extends AbstractSyncAdapterService { 37 private static final String TAG = Eas.LOG_TAG; 38 private static final String ACCOUNT_AND_TYPE_CALENDAR = 39 MailboxColumns.ACCOUNT_KEY + "=? AND " + MailboxColumns.TYPE + '=' + Mailbox.TYPE_CALENDAR; 40 private static final String DIRTY_IN_ACCOUNT = 41 Events.DIRTY + "=1 AND " + Events.ACCOUNT_NAME + "=?"; 42 43 private static final Object sSyncAdapterLock = new Object(); 44 private static AbstractThreadedSyncAdapter sSyncAdapter = null; 45 CalendarSyncAdapterService()46 public CalendarSyncAdapterService() { 47 super(); 48 } 49 50 @Override getSyncAdapter()51 protected AbstractThreadedSyncAdapter getSyncAdapter() { 52 synchronized (sSyncAdapterLock) { 53 if (sSyncAdapter == null) { 54 sSyncAdapter = new SyncAdapterImpl(this); 55 } 56 return sSyncAdapter; 57 } 58 } 59 60 private static class SyncAdapterImpl extends AbstractThreadedSyncAdapter { SyncAdapterImpl(Context context)61 public SyncAdapterImpl(Context context) { 62 super(context, true /* autoInitialize */); 63 } 64 65 @Override onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)66 public void onPerformSync(Account account, Bundle extras, 67 String authority, ContentProviderClient provider, SyncResult syncResult) { 68 if (LogUtils.isLoggable(TAG, Log.DEBUG)) { 69 LogUtils.d(TAG, "onPerformSync Calendar starting %s, %s", account.toString(), 70 extras.toString()); 71 } else { 72 LogUtils.i(TAG, "onPerformSync Calendar starting %s", extras.toString()); 73 } 74 CalendarSyncAdapterService.performSync(getContext(), account, extras); 75 LogUtils.d(TAG, "onPerformSync Calendar finished"); 76 } 77 } 78 79 /** 80 * Partial integration with system SyncManager; we tell our EAS ExchangeService to start a 81 * calendar sync when we get the signal from SyncManager. 82 * The missing piece at this point is integration with the push/ping mechanism in EAS; this will 83 * be put in place at a later time. 84 */ performSync(Context context, Account account, Bundle extras)85 private static void performSync(Context context, Account account, Bundle extras) { 86 if (extras.getBoolean(Mailbox.SYNC_EXTRA_NOOP, false)) { 87 LogUtils.d(TAG, "No-op sync requested, done"); 88 return; 89 } 90 91 final ContentResolver cr = context.getContentResolver(); 92 final boolean logging = Eas.USER_LOG; 93 if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD)) { 94 final Cursor c = cr.query(Events.CONTENT_URI, 95 new String[] {Events._ID}, DIRTY_IN_ACCOUNT, new String[] {account.name}, null); 96 if (c == null) { 97 LogUtils.e(TAG, "Null changes cursor in CalendarSyncAdapterService"); 98 return; 99 } 100 try { 101 if (!c.moveToFirst()) { 102 if (logging) { 103 LogUtils.d(TAG, "No changes for " + account.name); 104 } 105 return; 106 } 107 } finally { 108 c.close(); 109 } 110 } 111 112 // Forward the sync request to the EmailSyncAdapterService. 113 final long [] mailboxIds = Mailbox.getMailboxIdsFromBundle(extras); 114 final Bundle mailExtras; 115 if (mailboxIds == null) { 116 // We weren't given any particular mailboxId, specify a sync for all calendars. 117 mailExtras = new Bundle(); 118 mailExtras.putInt(Mailbox.SYNC_EXTRA_MAILBOX_TYPE, Mailbox.TYPE_CALENDAR); 119 } else { 120 // Otherwise, add all of the mailboxes specified in the original sync extras. 121 mailExtras = Mailbox.createSyncBundle(mailboxIds); 122 } 123 mailExtras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); 124 mailExtras.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, true); 125 if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false)) { 126 mailExtras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true); 127 } 128 ContentResolver.requestSync(account, EmailContent.AUTHORITY, mailExtras); 129 LogUtils.d(TAG, "requestSync CalendarSyncAdapter %s", mailExtras.toString()); 130 } 131 } 132