From e7e1ca432e1aace5b7f11a3f1684a4abc503e6b2 Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Tue, 15 Sep 2009 19:27:05 -0700 Subject: [PATCH] Change AccountManager username for Exchange to the user's login credential * This is not a backward-compatible change (sorry) * Existing AccountManager EAS accounts and contacts are deleted * Existing EmailProvider data is deleted * Change works with new code (stadler) to avoid account duplication Change-Id: Ife09c51fa714d91054d017b497bce603add5375a --- .../email/mail/store/ExchangeStore.java | 2 +- .../android/email/provider/EmailProvider.java | 49 ++++++++++++------- src/com/android/exchange/EasSyncService.java | 3 +- src/com/android/exchange/SyncManager.java | 17 +++++-- .../exchange/adapter/ContactsSyncAdapter.java | 8 +-- 5 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/com/android/email/mail/store/ExchangeStore.java b/src/com/android/email/mail/store/ExchangeStore.java index c5d81745f..65106dee4 100644 --- a/src/com/android/email/mail/store/ExchangeStore.java +++ b/src/com/android/email/mail/store/ExchangeStore.java @@ -83,7 +83,7 @@ public class ExchangeStore extends Store { AccountManagerCallback callback) { // Create a description of the new account Bundle options = new Bundle(); - options.putString(EasAuthenticatorService.OPTIONS_USERNAME, acct.mEmailAddress); + options.putString(EasAuthenticatorService.OPTIONS_USERNAME, acct.mHostAuthRecv.mLogin); options.putString(EasAuthenticatorService.OPTIONS_PASSWORD, acct.mHostAuthRecv.mPassword); options.putBoolean(EasAuthenticatorService.OPTIONS_CONTACTS_SYNC_ENABLED, syncContacts); diff --git a/src/com/android/email/provider/EmailProvider.java b/src/com/android/email/provider/EmailProvider.java index 2679aa346..303fe6543 100644 --- a/src/com/android/email/provider/EmailProvider.java +++ b/src/com/android/email/provider/EmailProvider.java @@ -30,7 +30,9 @@ import com.android.email.provider.EmailContent.MailboxColumns; import com.android.email.provider.EmailContent.Message; import com.android.email.provider.EmailContent.MessageColumns; import com.android.email.provider.EmailContent.SyncColumns; +import com.android.exchange.Eas; +import android.accounts.AccountManager; import android.content.ContentProvider; import android.content.ContentProviderOperation; import android.content.ContentProviderResult; @@ -57,12 +59,14 @@ public class EmailProvider extends ContentProvider { // Any changes to the database format *must* include update-in-place code. // Original version: 3 - public static final int DATABASE_VERSION = 3; + // Version 4: Database wipe required; changing AccountManager interface w/Exchange + public static final int DATABASE_VERSION = 4; // Any changes to the database format *must* include update-in-place code. // Original version: 2 // Version 3: Add "sourceKey" column - public static final int BODY_DATABASE_VERSION = 3; + // Version 4: Database wipe required; changing AccountManager interface w/Exchange + public static final int BODY_DATABASE_VERSION = 4; public static final String EMAIL_AUTHORITY = "com.android.email.provider"; @@ -348,7 +352,7 @@ public class EmailProvider extends ContentProvider { "; end"); } - static void upgradeMessageTable(SQLiteDatabase db, int oldVersion, int newVersion) { + static void resetMessageTable(SQLiteDatabase db, int oldVersion, int newVersion) { try { db.execSQL("drop table " + Message.TABLE_NAME); db.execSQL("drop table " + Message.UPDATED_TABLE_NAME); @@ -387,7 +391,7 @@ public class EmailProvider extends ContentProvider { "; end"); } - static void upgradeAccountTable(SQLiteDatabase db, int oldVersion, int newVersion) { + static void resetAccountTable(SQLiteDatabase db, int oldVersion, int newVersion) { try { db.execSQL("drop table " + Account.TABLE_NAME); } catch (SQLException e) { @@ -409,7 +413,7 @@ public class EmailProvider extends ContentProvider { db.execSQL("create table " + HostAuth.TABLE_NAME + s); } - static void upgradeHostAuthTable(SQLiteDatabase db, int oldVersion, int newVersion) { + static void resetHostAuthTable(SQLiteDatabase db, int oldVersion, int newVersion) { try { db.execSQL("drop table " + HostAuth.TABLE_NAME); } catch (SQLException e) { @@ -447,7 +451,7 @@ public class EmailProvider extends ContentProvider { "; end"); } - static void upgradeMailboxTable(SQLiteDatabase db, int oldVersion, int newVersion) { + static void resetMailboxTable(SQLiteDatabase db, int oldVersion, int newVersion) { try { db.execSQL("drop table " + Mailbox.TABLE_NAME); } catch (SQLException e) { @@ -470,7 +474,7 @@ public class EmailProvider extends ContentProvider { db.execSQL(createIndex(Attachment.TABLE_NAME, AttachmentColumns.MESSAGE_KEY)); } - static void upgradeAttachmentTable(SQLiteDatabase db, int oldVersion, int newVersion) { + static void resetAttachmentTable(SQLiteDatabase db, int oldVersion, int newVersion) { try { db.execSQL("drop table " + Attachment.TABLE_NAME); } catch (SQLException e) { @@ -492,18 +496,13 @@ public class EmailProvider extends ContentProvider { } static void upgradeBodyTable(SQLiteDatabase db, int oldVersion, int newVersion) { - if (oldVersion < 2) { - // Versions earlier than 2 require a wipe of the database + if (oldVersion < 4) { try { db.execSQL("drop table " + Body.TABLE_NAME); createBodyTable(db); } catch (SQLException e) { } - } else if (oldVersion == 2) { - Log.d(TAG, "Upgrading Body from v2 to v3"); - db.execSQL("alter table " + Body.TABLE_NAME + - " add " + Body.SOURCE_MESSAGE_KEY + " integer"); - } + } } private final int mDatabaseVersion = DATABASE_VERSION; @@ -554,8 +553,11 @@ public class EmailProvider extends ContentProvider { } private class DatabaseHelper extends SQLiteOpenHelper { + Context mContext; + DatabaseHelper(Context context, String name) { super(context, name, null, mDatabaseVersion); + mContext = context; } @Override @@ -570,11 +572,20 @@ public class EmailProvider extends ContentProvider { @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - upgradeMessageTable(db, oldVersion, newVersion); - upgradeAttachmentTable(db, oldVersion, newVersion); - upgradeMailboxTable(db, oldVersion, newVersion); - upgradeHostAuthTable(db, oldVersion, newVersion); - upgradeAccountTable(db, oldVersion, newVersion); + // For versions prior to 4, delete all data + // Versions >= 4 require that data be preserved! + if (oldVersion < 4) { + android.accounts.Account[] accounts = + AccountManager.get(mContext).getAccountsByType(Eas.ACCOUNT_MANAGER_TYPE); + for (android.accounts.Account account: accounts) { + AccountManager.get(mContext).removeAccount(account, null, null); + } + resetMessageTable(db, oldVersion, newVersion); + resetAttachmentTable(db, oldVersion, newVersion); + resetMailboxTable(db, oldVersion, newVersion); + resetHostAuthTable(db, oldVersion, newVersion); + resetAccountTable(db, oldVersion, newVersion); + } } @Override diff --git a/src/com/android/exchange/EasSyncService.java b/src/com/android/exchange/EasSyncService.java index 667eeae1b..32c0c0f5a 100644 --- a/src/com/android/exchange/EasSyncService.java +++ b/src/com/android/exchange/EasSyncService.java @@ -133,6 +133,7 @@ public class EasSyncService extends AbstractSyncService { private boolean mSsl = true; private boolean mTrustSsl = false; public ContentResolver mContentResolver; + public String mHostLogin; private String[] mBindArguments = new String[2]; private ArrayList mPingChangeList; private HttpPost mPendingPost = null; @@ -147,6 +148,7 @@ public class EasSyncService extends AbstractSyncService { super(_context, _mailbox); mContentResolver = _context.getContentResolver(); HostAuth ha = HostAuth.restoreHostAuthWithId(_context, mAccount.mHostAuthKeyRecv); + mHostLogin = ha.mLogin; mSsl = (ha.mFlags & HostAuth.FLAG_SSL) != 0; mTrustSsl = (ha.mFlags & HostAuth.FLAG_TRUST_ALL_CERTIFICATES) != 0; } @@ -1039,7 +1041,6 @@ public class EasSyncService extends AbstractSyncService { runAccountMailbox(); } else { AbstractSyncAdapter target; - mAccount = Account.restoreAccountWithId(mContext, mAccount.mId); mProtocolVersion = mAccount.mProtocolVersion; mProtocolVersionDouble = Double.parseDouble(mProtocolVersion); if (mMailbox.mType == Mailbox.TYPE_CONTACTS) { diff --git a/src/com/android/exchange/SyncManager.java b/src/com/android/exchange/SyncManager.java index 2da2a1fb0..8b356794e 100644 --- a/src/com/android/exchange/SyncManager.java +++ b/src/com/android/exchange/SyncManager.java @@ -443,7 +443,7 @@ public class SyncManager extends Service implements Runnable { stopAccountSyncs(account.mId, true); // Delete this from AccountManager... android.accounts.Account acct = - new android.accounts.Account(account.mEmailAddress, + new android.accounts.Account(account.mHostAuthRecv.mLogin, Eas.ACCOUNT_MANAGER_TYPE); AccountManager.get(SyncManager.this).removeAccount(acct, null, null); mAccountKeyList = null; @@ -471,6 +471,10 @@ public class SyncManager extends Service implements Runnable { if (!mAccounts.contains(account.mId)) { // This is an addition; create our magic hidden mailbox... addAccountMailbox(account.mId); + // Don't forget to cache the HostAuth + HostAuth ha = + HostAuth.restoreHostAuthWithId(getContext(), account.mHostAuthKeyRecv); + account.mHostAuthRecv = ha; mAccounts.add(account); mAccountKeyList = null; } @@ -493,7 +497,10 @@ public class SyncManager extends Service implements Runnable { if (hostAuthId > 0) { HostAuth ha = HostAuth.restoreHostAuthWithId(context, hostAuthId); if (ha != null && ha.mProtocol.equals("eas")) { - accounts.add(new Account().restore(c)); + Account account = new Account().restore(c); + // Cache the HostAuth + account.mHostAuthRecv = ha; + accounts.add(account); } } } @@ -1030,7 +1037,7 @@ public class SyncManager extends Service implements Runnable { if (contactsId != Mailbox.NO_MAILBOX) { // Create an AccountManager style Account android.accounts.Account acct = - new android.accounts.Account(easAccount.mEmailAddress, + new android.accounts.Account(easAccount.mHostAuthRecv.mLogin, Eas.ACCOUNT_MANAGER_TYPE); // Get the Contacts mailbox; this happens rarely so it's ok to get it all Mailbox contacts = Mailbox.restoreMailboxWithId(this, contactsId); @@ -1069,7 +1076,7 @@ public class SyncManager extends Service implements Runnable { AccountManager.get(this).getAccountsByType(Eas.ACCOUNT_MANAGER_TYPE); List easAccounts = getAccountList(); for (Account easAccount: easAccounts) { - String accountName = easAccount.mEmailAddress; + String accountName = easAccount.mHostAuthRecv.mLogin; boolean found = false; for (android.accounts.Account acct: accts) { if (acct.name.equalsIgnoreCase(accountName)) { @@ -1400,7 +1407,7 @@ public class SyncManager extends Service implements Runnable { getAccountById(c.getInt(Mailbox.CONTENT_ACCOUNT_KEY_COLUMN)); if (account != null) { android.accounts.Account a = - new android.accounts.Account(account.mEmailAddress, + new android.accounts.Account(account.mHostAuthRecv.mLogin, Eas.ACCOUNT_MANAGER_TYPE); if (!ContentResolver.getSyncAutomatically(a, ContactsContract.AUTHORITY)) { diff --git a/src/com/android/exchange/adapter/ContactsSyncAdapter.java b/src/com/android/exchange/adapter/ContactsSyncAdapter.java index b4764e6e8..e815ee945 100644 --- a/src/com/android/exchange/adapter/ContactsSyncAdapter.java +++ b/src/com/android/exchange/adapter/ContactsSyncAdapter.java @@ -151,7 +151,7 @@ public class ContactsSyncAdapter extends AbstractSyncAdapter { setSyncKey("0", false); // Make sure ungrouped contacts for Exchange are defaultly visible ContentValues cv = new ContentValues(); - cv.put(Groups.ACCOUNT_NAME, mAccount.mEmailAddress); + cv.put(Groups.ACCOUNT_NAME, mService.mHostLogin); cv.put(Groups.ACCOUNT_TYPE, Eas.ACCOUNT_MANAGER_TYPE); cv.put(Settings.UNGROUPED_VISIBLE, true); client.insert(Settings.CONTENT_URI, cv); @@ -190,7 +190,7 @@ public class ContactsSyncAdapter extends AbstractSyncAdapter { public android.accounts.Account getAccountManagerAccount() { if (mAccountManagerAccount == null) { mAccountManagerAccount = - new android.accounts.Account(mAccount.mEmailAddress, Eas.ACCOUNT_MANAGER_TYPE); + new android.accounts.Account(mService.mHostLogin, Eas.ACCOUNT_MANAGER_TYPE); } return mAccountManagerAccount; } @@ -877,7 +877,7 @@ public class ContactsSyncAdapter extends AbstractSyncAdapter { private Uri uriWithAccountAndIsSyncAdapter(Uri uri) { return uri.buildUpon() - .appendQueryParameter(RawContacts.ACCOUNT_NAME, mAccount.mEmailAddress) + .appendQueryParameter(RawContacts.ACCOUNT_NAME, mService.mHostLogin) .appendQueryParameter(RawContacts.ACCOUNT_TYPE, Eas.ACCOUNT_MANAGER_TYPE) .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true") .build(); @@ -1713,7 +1713,7 @@ public class ContactsSyncAdapter extends AbstractSyncAdapter { // First, let's find Contacts that have changed. ContentResolver cr = mService.mContentResolver; Uri uri = RawContacts.CONTENT_URI.buildUpon() - .appendQueryParameter(RawContacts.ACCOUNT_NAME, mAccount.mEmailAddress) + .appendQueryParameter(RawContacts.ACCOUNT_NAME, mService.mHostLogin) .appendQueryParameter(RawContacts.ACCOUNT_TYPE, Eas.ACCOUNT_MANAGER_TYPE) .build();