From 3aad3fcb8bb916c989fe67cfbf527eb0df6fc6d1 Mon Sep 17 00:00:00 2001 From: Martin Hibdon Date: Tue, 24 Sep 2013 15:57:59 -0700 Subject: [PATCH] Move calendar and contacts to the new account on upgrade b/10805685 Change-Id: I08de135f166ad10812c9318efe649694e5c448d0 --- .../email/service/EmailServiceUtils.java | 52 +++++++++++++++---- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/src/com/android/email/service/EmailServiceUtils.java b/src/com/android/email/service/EmailServiceUtils.java index 0f12c1303..c1200e606 100644 --- a/src/com/android/email/service/EmailServiceUtils.java +++ b/src/com/android/email/service/EmailServiceUtils.java @@ -36,11 +36,15 @@ import android.net.Uri; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; +import android.provider.BaseColumns; import android.provider.CalendarContract; import android.provider.CalendarContract.Calendars; import android.provider.CalendarContract.SyncState; import android.provider.ContactsContract; +import android.provider.ContactsContract.Contacts; +import android.provider.ContactsContract.RawContacts; import android.provider.SyncStateContract; +import android.text.TextUtils; import com.android.email.R; import com.android.emailcommon.Api; @@ -403,10 +407,12 @@ public class EmailServiceUtils { finishAccountManagerBlocker(amFuture); LogUtils.w(Logging.LOG_TAG, "Created new AccountManager account"); - - // XXX: - // It would be nice if we could copy or move all of the old account's data over - // to the new account here. b/10805685 + // TODO: Clean up how we determine the type. + final String accountType = protocolMap.get(hostAuth.mProtocol + "_type"); + // Move calendar and contacts data from the old account to the new one. + // We must do this before deleting the old account or the data is lost. + moveCalendarData(context.getContentResolver(), amName, oldType, accountType); + moveContactsData(context.getContentResolver(), amName, oldType, accountType); // Delete the AccountManager account amFuture = AccountManager.get(context) @@ -415,12 +421,7 @@ public class EmailServiceUtils { LogUtils.w(Logging.LOG_TAG, "Deleted old AccountManager account"); // Restore sync keys for contacts/calendar - // XXX See b/10211620 - // Disable this for now. We want to clear the sync keys so that the next - // sync will get everything. -/* - // TODO: Clean up how we determine the type. - final String accountType = protocolMap.get(hostAuth.mProtocol + "_type"); + if (accountType != null && calendarSyncKey != null && calendarSyncKey.length != 0) { client = context.getContentResolver() @@ -452,7 +453,6 @@ public class EmailServiceUtils { LogUtils.w(Logging.LOG_TAG, "Set contacts key FAILED"); } } -*/ // That's all folks! LogUtils.w(Logging.LOG_TAG, "Account update completed."); @@ -468,6 +468,36 @@ public class EmailServiceUtils { } } + private static void moveCalendarData(final ContentResolver resolver, final String name, + final String oldType, final String newType) { + final Uri oldCalendars = Calendars.CONTENT_URI.buildUpon() + .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true") + .appendQueryParameter(Calendars.ACCOUNT_NAME, name) + .appendQueryParameter(Calendars.ACCOUNT_TYPE, oldType) + .build(); + + // Update this calendar to have the new account type. + final ContentValues values = new ContentValues(); + values.put(CalendarContract.Calendars.ACCOUNT_TYPE, newType); + resolver.update(oldCalendars, values, + Calendars.ACCOUNT_NAME + "=? AND " + Calendars.ACCOUNT_TYPE + "=?", + new String[] {name, oldType}); + } + + private static void moveContactsData(final ContentResolver resolver, final String name, + final String oldType, final String newType) { + final Uri oldContacts = RawContacts.CONTENT_URI.buildUpon() + .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true") + .appendQueryParameter(RawContacts.ACCOUNT_NAME, name) + .appendQueryParameter(RawContacts.ACCOUNT_TYPE, oldType) + .build(); + + // Update this calendar to have the new account type. + final ContentValues values = new ContentValues(); + values.put(CalendarContract.Calendars.ACCOUNT_TYPE, newType); + resolver.update(oldContacts, values, null, null); + } + /** * Parse services.xml file to find our available email services */