Home | History | Annotate | Download | only in service
      1 /*
      2  * Copyright (C) 2009 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.net.Uri;
     27 import android.os.Bundle;
     28 import android.provider.ContactsContract.Groups;
     29 import android.provider.ContactsContract.RawContacts;
     30 import android.util.Log;
     31 
     32 import com.android.emailcommon.provider.EmailContent;
     33 import com.android.emailcommon.provider.EmailContent.MailboxColumns;
     34 import com.android.emailcommon.provider.Mailbox;
     35 import com.android.exchange.Eas;
     36 import com.android.mail.utils.LogUtils;
     37 
     38 public class ContactsSyncAdapterService extends AbstractSyncAdapterService {
     39     private static final String TAG = Eas.LOG_TAG;
     40     private static final String ACCOUNT_AND_TYPE_CONTACTS =
     41         MailboxColumns.ACCOUNT_KEY + "=? AND " + MailboxColumns.TYPE + '=' + Mailbox.TYPE_CONTACTS;
     42 
     43     private static final Object sSyncAdapterLock = new Object();
     44     private static AbstractThreadedSyncAdapter sSyncAdapter = null;
     45 
     46     public ContactsSyncAdapterService() {
     47         super();
     48     }
     49 
     50     @Override
     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 {
     61         public SyncAdapterImpl(Context context) {
     62             super(context, true /* autoInitialize */);
     63         }
     64 
     65         @Override
     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 Contacts starting %s, %s", account.toString(),
     70                         extras.toString());
     71             } else {
     72                 LogUtils.i(TAG, "onPerformSync Contacts starting %s", extras.toString());
     73             }
     74             ContactsSyncAdapterService.performSync(getContext(), account, extras);
     75             LogUtils.d(TAG, "onPerformSync Contacts finished");
     76         }
     77     }
     78 
     79     private static boolean hasDirtyRows(ContentResolver resolver, Uri uri, String dirtyColumn) {
     80         Cursor c = resolver.query(uri, EmailContent.ID_PROJECTION, dirtyColumn + "=1", null, null);
     81         try {
     82             return c.getCount() > 0;
     83         } finally {
     84             c.close();
     85         }
     86     }
     87 
     88     /**
     89      * Partial integration with system SyncManager; we tell our EAS ExchangeService to start a
     90      * contacts sync when we get the signal from SyncManager.
     91      * The missing piece at this point is integration with the push/ping mechanism in EAS; this will
     92      * be put in place at a later time.
     93      */
     94     private static void performSync(Context context, Account account, Bundle extras) {
     95         if (extras.getBoolean(Mailbox.SYNC_EXTRA_NOOP, false)) {
     96             LogUtils.d(TAG, "No-op sync requested, done");
     97             return;
     98         }
     99         ContentResolver cr = context.getContentResolver();
    100         // If we've been asked to do an upload, make sure we've got work to do
    101         if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD)) {
    102             Uri uri = RawContacts.CONTENT_URI.buildUpon()
    103                 .appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name)
    104                 .appendQueryParameter(RawContacts.ACCOUNT_TYPE, Eas.EXCHANGE_ACCOUNT_MANAGER_TYPE)
    105                 .build();
    106             // See if we've got dirty contacts or dirty groups containing our contacts
    107             boolean changed = hasDirtyRows(cr, uri, RawContacts.DIRTY);
    108             if (!changed) {
    109                 uri = Groups.CONTENT_URI.buildUpon()
    110                     .appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name)
    111                     .appendQueryParameter(RawContacts.ACCOUNT_TYPE,
    112                             Eas.EXCHANGE_ACCOUNT_MANAGER_TYPE)
    113                     .build();
    114                 changed = hasDirtyRows(cr, uri, Groups.DIRTY);
    115             }
    116             if (!changed) {
    117                 LogUtils.d(TAG, "Upload sync; no changes");
    118                 return;
    119             }
    120         }
    121 
    122         // Forward the sync request to the EmailSyncAdapterService.
    123         long [] mailboxIds = Mailbox.getMailboxIdsFromBundle(extras);
    124         final Bundle mailExtras;
    125         if (mailboxIds == null) {
    126             // We weren't given any particular mailboxId, specify a sync for all contacts.
    127             mailExtras = new Bundle();
    128             mailExtras.putInt(Mailbox.SYNC_EXTRA_MAILBOX_TYPE, Mailbox.TYPE_CALENDAR);
    129         } else {
    130             // Otherwise, add all of the mailboxes specified in the original sync extras.
    131             mailExtras = Mailbox.createSyncBundle(mailboxIds);
    132         }
    133         mailExtras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    134         mailExtras.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, true);
    135         if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false)) {
    136             mailExtras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
    137         }
    138         ContentResolver.requestSync(account, EmailContent.AUTHORITY, mailExtras);
    139         LogUtils.d(TAG, "requestSync ContactsSyncAdapter %s", mailExtras.toString());
    140     }
    141 }
    142