From 4a8c70c09be3914ded18031b4cca5a6d867de0d3 Mon Sep 17 00:00:00 2001 From: Andrew Stadler Date: Tue, 18 Aug 2009 12:14:15 -0700 Subject: [PATCH] Add "num new messages" field to Account * Reset DB version to "1" * Added field and support code (save, restore, & parcel) * Added forgotten mProtocolVersion to parcelable code * Added unit test for parceling Also, fixed race condition in SyncManager that was underlocking the wake locks during tests. --- .../android/email/provider/EmailContent.java | 13 +++++++++++- .../android/email/provider/EmailProvider.java | 16 ++++---------- src/com/android/exchange/SyncManager.java | 8 ++++--- .../email/provider/ProviderTestUtils.java | 3 +++ .../android/email/provider/ProviderTests.java | 21 +++++++++++++++++++ 5 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/com/android/email/provider/EmailContent.java b/src/com/android/email/provider/EmailContent.java index 431d72da6..b3f2d7532 100644 --- a/src/com/android/email/provider/EmailContent.java +++ b/src/com/android/email/provider/EmailContent.java @@ -790,6 +790,8 @@ public abstract class EmailContent { public static final String RINGTONE_URI = "ringtoneUri"; // Protocol version (arbitrary string, used by EAS currently) public static final String PROTOCOL_VERSION = "protocolVersion"; + // The number of new messages (reported by the sync/download engines + public static final String NEW_MESSAGE_COUNT = "newMessageCount"; } public static final class Account extends EmailContent implements AccountColumns, Parcelable { @@ -825,6 +827,7 @@ public abstract class EmailContent { public String mSenderName; public String mRingtoneUri; public String mProtocolVersion; + public int mNewMessageCount; // Convenience for creating an account public transient HostAuth mHostAuthRecv; @@ -844,6 +847,7 @@ public abstract class EmailContent { public static final int CONTENT_SENDER_NAME_COLUMN = 11; public static final int CONTENT_RINGTONE_URI_COLUMN = 12; public static final int CONTENT_PROTOCOL_VERSION_COLUMN = 13; + public static final int CONTENT_NEW_MESSAGE_COUNT_COLUMN = 14; public static final String[] CONTENT_PROJECTION = new String[] { RECORD_ID, AccountColumns.DISPLAY_NAME, @@ -851,7 +855,8 @@ public abstract class EmailContent { AccountColumns.SYNC_INTERVAL, AccountColumns.HOST_AUTH_KEY_RECV, AccountColumns.HOST_AUTH_KEY_SEND, AccountColumns.FLAGS, AccountColumns.IS_DEFAULT, AccountColumns.COMPATIBILITY_UUID, AccountColumns.SENDER_NAME, - AccountColumns.RINGTONE_URI, AccountColumns.PROTOCOL_VERSION + AccountColumns.RINGTONE_URI, AccountColumns.PROTOCOL_VERSION, + AccountColumns.NEW_MESSAGE_COUNT }; public static final int CONTENT_MAILBOX_TYPE_COLUMN = 1; @@ -941,6 +946,7 @@ public abstract class EmailContent { mSenderName = cursor.getString(CONTENT_SENDER_NAME_COLUMN); mRingtoneUri = cursor.getString(CONTENT_RINGTONE_URI_COLUMN); mProtocolVersion = cursor.getString(CONTENT_PROTOCOL_VERSION_COLUMN); + mNewMessageCount = cursor.getInt(CONTENT_NEW_MESSAGE_COUNT_COLUMN); return this; } @@ -1357,6 +1363,7 @@ public abstract class EmailContent { values.put(AccountColumns.SENDER_NAME, mSenderName); values.put(AccountColumns.RINGTONE_URI, mRingtoneUri); values.put(AccountColumns.PROTOCOL_VERSION, mProtocolVersion); + values.put(AccountColumns.NEW_MESSAGE_COUNT, mNewMessageCount); return values; } @@ -1428,6 +1435,8 @@ public abstract class EmailContent { dest.writeString(mCompatibilityUuid); dest.writeString(mSenderName); dest.writeString(mRingtoneUri); + dest.writeString(mProtocolVersion); + dest.writeInt(mNewMessageCount); if (mHostAuthRecv != null) { dest.writeByte((byte)1); @@ -1462,6 +1471,8 @@ public abstract class EmailContent { mCompatibilityUuid = in.readString(); mSenderName = in.readString(); mRingtoneUri = in.readString(); + mProtocolVersion = in.readString(); + mNewMessageCount = in.readInt(); mHostAuthRecv = null; if (in.readByte() == 1) { diff --git a/src/com/android/email/provider/EmailProvider.java b/src/com/android/email/provider/EmailProvider.java index ebdc1edad..2257358ff 100644 --- a/src/com/android/email/provider/EmailProvider.java +++ b/src/com/android/email/provider/EmailProvider.java @@ -55,18 +55,9 @@ public class EmailProvider extends ContentProvider { static final String DATABASE_NAME = "EmailProvider.db"; static final String BODY_DATABASE_NAME = "EmailProviderBody.db"; - // In these early versions, updating the database version will cause all tables to be deleted - // Obviously, we'll handle upgrades differently once things are a bit stable - // version 15: changed Address.pack() format. - // version 16: added protocolVersion column to Account - // version 17: prevent duplication of mailboxes with the same serverId - // version 18: renamed syncFrequency to syncInterval for Account and Mailbox - // version 19: added triggers to keep track of unreadCount by Mailbox - // version 20: changed type of EAS account mailbox, making old databases invalid for EAS - // version 21: fixed broken trigger linking account deletion to the deletion of its HostAuth's - // version 22: added syncStatus column to Mailbox + // Any changes to the database format *must* include update-in-place code. - public static final int DATABASE_VERSION = 22; + public static final int DATABASE_VERSION = 1; public static final int BODY_DATABASE_VERSION = 1; public static final String EMAIL_AUTHORITY = "com.android.email.provider"; @@ -383,7 +374,8 @@ public class EmailProvider extends ContentProvider { + AccountColumns.COMPATIBILITY_UUID + " text, " + AccountColumns.SENDER_NAME + " text, " + AccountColumns.RINGTONE_URI + " text, " - + AccountColumns.PROTOCOL_VERSION + " text" + + AccountColumns.PROTOCOL_VERSION + " text, " + + AccountColumns.NEW_MESSAGE_COUNT + " integer" + ");"; db.execSQL("create table " + Account.TABLE_NAME + s); // Deleting an account deletes associated Mailboxes and HostAuth's diff --git a/src/com/android/exchange/SyncManager.java b/src/com/android/exchange/SyncManager.java index 0ada2bc09..b4b713a15 100644 --- a/src/com/android/exchange/SyncManager.java +++ b/src/com/android/exchange/SyncManager.java @@ -665,9 +665,11 @@ public class SyncManager extends Service implements Runnable { clearAlarms(); // Release our wake lock, if we have one - if (mWakeLock != null) { - mWakeLock.release(); - mWakeLock = null; + synchronized (mWakeLocks) { + if (mWakeLock != null) { + mWakeLock.release(); + mWakeLock = null; + } } sPendingIntents.clear(); diff --git a/tests/src/com/android/email/provider/ProviderTestUtils.java b/tests/src/com/android/email/provider/ProviderTestUtils.java index 34726d370..f74681ce0 100644 --- a/tests/src/com/android/email/provider/ProviderTestUtils.java +++ b/tests/src/com/android/email/provider/ProviderTestUtils.java @@ -52,6 +52,7 @@ public class ProviderTestUtils extends Assert { account.mSenderName = name; account.mRingtoneUri = "content://ringtone-" + name; account.mProtocolVersion = "2.5" + name; + account.mNewMessageCount = 5 + name.length(); if (saveIt) { account.save(context); @@ -207,6 +208,8 @@ public class ProviderTestUtils extends Assert { assertEquals(caller + " mRingtoneUri", expect.mRingtoneUri, actual.mRingtoneUri); assertEquals(caller + " mProtocolVersion", expect.mProtocolVersion, actual.mProtocolVersion); + assertEquals(caller + " mNewMessageCount", expect.mNewMessageCount, + actual.mNewMessageCount); } /** diff --git a/tests/src/com/android/email/provider/ProviderTests.java b/tests/src/com/android/email/provider/ProviderTests.java index 290018f46..a4560795b 100644 --- a/tests/src/com/android/email/provider/ProviderTests.java +++ b/tests/src/com/android/email/provider/ProviderTests.java @@ -32,7 +32,9 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri; +import android.os.Bundle; import android.os.Environment; +import android.os.Parcel; import android.test.ProviderTestCase2; import java.io.File; @@ -77,6 +79,25 @@ public class ProviderTests extends ProviderTestCase2 { ProviderTestUtils.assertAccountEqual("testAccountSave", account1, account2); } + /** + * Simple test of account parceling. The rather tortuous path is to ensure that the + * account is really flattened all the way down to a parcel and back. + */ + public void testAccountParcel() { + Account account1 = ProviderTestUtils.setupAccount("parcel", false, mMockContext); + Bundle b = new Bundle(); + b.putParcelable("account", account1); + Parcel p = Parcel.obtain(); + b.writeToParcel(p, 0); + p.setDataPosition(0); // rewind it for reading + Bundle b2 = new Bundle(Account.class.getClassLoader()); + b2.readFromParcel(p); + Account account2 = (Account) b2.getParcelable("account"); + p.recycle(); + + ProviderTestUtils.assertAccountEqual("testAccountParcel", account1, account2); + } + private final static String[] MAILBOX_UNREAD_COUNT_PROJECTION = new String [] { MailboxColumns.UNREAD_COUNT };