2009-07-27 17:24:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.exchange;
|
|
|
|
|
|
|
|
import com.android.email.provider.EmailContent;
|
|
|
|
import com.android.email.provider.EmailContent.AccountColumns;
|
|
|
|
import com.android.email.provider.EmailContent.Mailbox;
|
|
|
|
import com.android.email.provider.EmailContent.MailboxColumns;
|
|
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
|
import android.accounts.OperationCanceledException;
|
|
|
|
import android.app.Service;
|
|
|
|
import android.content.AbstractThreadedSyncAdapter;
|
|
|
|
import android.content.ContentProviderClient;
|
|
|
|
import android.content.ContentResolver;
|
2009-08-28 17:25:20 +00:00
|
|
|
import android.content.Context;
|
2009-07-27 17:24:58 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SyncResult;
|
|
|
|
import android.database.Cursor;
|
2009-08-29 19:55:55 +00:00
|
|
|
import android.net.Uri;
|
2009-07-27 17:24:58 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.IBinder;
|
2009-08-29 19:55:55 +00:00
|
|
|
import android.provider.ContactsContract.RawContacts;
|
2009-07-27 17:24:58 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
public class ContactsSyncAdapterService extends Service {
|
2009-08-27 23:31:13 +00:00
|
|
|
private static final String TAG = "EAS ContactsSyncAdapterService";
|
|
|
|
private static SyncAdapterImpl sSyncAdapter = null;
|
|
|
|
private static final Object sSyncAdapterLock = new Object();
|
2009-07-27 17:24:58 +00:00
|
|
|
|
|
|
|
private static final String[] ID_PROJECTION = new String[] {EmailContent.RECORD_ID};
|
|
|
|
private static final String ACCOUNT_AND_TYPE_CONTACTS =
|
|
|
|
MailboxColumns.ACCOUNT_KEY + "=? AND " + MailboxColumns.TYPE + '=' + Mailbox.TYPE_CONTACTS;
|
|
|
|
|
|
|
|
public ContactsSyncAdapterService() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2009-08-27 23:31:13 +00:00
|
|
|
private static class SyncAdapterImpl extends AbstractThreadedSyncAdapter {
|
2009-08-28 17:25:20 +00:00
|
|
|
private Context mContext;
|
|
|
|
|
2009-08-27 23:31:13 +00:00
|
|
|
public SyncAdapterImpl(Context context) {
|
|
|
|
super(context, true /* autoInitialize */);
|
2009-08-28 17:25:20 +00:00
|
|
|
mContext = context;
|
2009-07-27 17:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void performSync(Account account, Bundle extras,
|
|
|
|
String authority, ContentProviderClient provider, SyncResult syncResult) {
|
|
|
|
try {
|
2009-08-28 17:25:20 +00:00
|
|
|
ContactsSyncAdapterService.performSync(mContext, account, extras,
|
2009-07-27 17:24:58 +00:00
|
|
|
authority, provider, syncResult);
|
|
|
|
} catch (OperationCanceledException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-28 17:25:20 +00:00
|
|
|
@Override
|
2009-08-27 23:31:13 +00:00
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
synchronized (sSyncAdapterLock) {
|
|
|
|
if (sSyncAdapter == null) {
|
|
|
|
sSyncAdapter = new SyncAdapterImpl(getApplicationContext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-27 17:24:58 +00:00
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
2009-08-27 23:31:13 +00:00
|
|
|
return sSyncAdapter.getISyncAdapter().asBinder();
|
2009-07-27 17:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Partial integration with system SyncManager; we tell our EAS SyncManager to start a contacts
|
|
|
|
* sync when we get the signal from the system SyncManager.
|
|
|
|
* The missing piece at this point is integration with the push/ping mechanism in EAS; this will
|
|
|
|
* be put in place at a later time.
|
|
|
|
*/
|
2009-08-29 19:55:55 +00:00
|
|
|
private static void performSync(Context context, Account account, Bundle extras,
|
|
|
|
String authority, ContentProviderClient provider, SyncResult syncResult)
|
2009-07-27 17:24:58 +00:00
|
|
|
throws OperationCanceledException {
|
2009-08-27 23:31:13 +00:00
|
|
|
ContentResolver cr = context.getContentResolver();
|
2009-08-29 19:55:55 +00:00
|
|
|
Log.i(TAG, "performSync");
|
|
|
|
if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD)) {
|
|
|
|
Uri uri = RawContacts.CONTENT_URI.buildUpon()
|
|
|
|
.appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name)
|
|
|
|
.appendQueryParameter(RawContacts.ACCOUNT_TYPE, Eas.ACCOUNT_MANAGER_TYPE)
|
|
|
|
.build();
|
|
|
|
Cursor c = cr.query(uri,
|
|
|
|
new String[] {RawContacts._ID}, RawContacts.DIRTY + "=1", null, null);
|
|
|
|
try {
|
|
|
|
if (!c.moveToFirst()) {
|
|
|
|
Log.i(TAG, "Upload sync; no changes");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-27 17:24:58 +00:00
|
|
|
// Find the (EmailProvider) account associated with this email address
|
|
|
|
Cursor accountCursor =
|
|
|
|
cr.query(com.android.email.provider.EmailContent.Account.CONTENT_URI, ID_PROJECTION,
|
2009-08-16 04:50:48 +00:00
|
|
|
AccountColumns.EMAIL_ADDRESS + "=?", new String[] {account.name}, null);
|
2009-07-27 17:24:58 +00:00
|
|
|
try {
|
|
|
|
if (accountCursor.moveToFirst()) {
|
|
|
|
long accountId = accountCursor.getLong(0);
|
|
|
|
// Now, find the contacts mailbox associated with the account
|
|
|
|
Cursor mailboxCursor = cr.query(Mailbox.CONTENT_URI, ID_PROJECTION,
|
|
|
|
ACCOUNT_AND_TYPE_CONTACTS, new String[] {Long.toString(accountId)}, null);
|
|
|
|
try {
|
2009-08-29 19:55:55 +00:00
|
|
|
if (mailboxCursor.moveToFirst()) {
|
2009-08-16 04:50:48 +00:00
|
|
|
Log.i(TAG, "Contact sync requested for " + account.name);
|
2009-07-27 17:24:58 +00:00
|
|
|
// Ask for a sync from our sync manager
|
2009-08-05 15:41:16 +00:00
|
|
|
SyncManager.serviceRequest(mailboxCursor.getLong(0),
|
|
|
|
SyncManager.SYNC_UPSYNC);
|
2009-07-27 17:24:58 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
mailboxCursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
accountCursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|