summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/com/android/providers/contacts/ContactsDatabaseHelper.java28
-rw-r--r--tests/src/com/android/providers/contacts/ContactsProvider2Test.java52
2 files changed, 79 insertions, 1 deletions
diff --git a/src/com/android/providers/contacts/ContactsDatabaseHelper.java b/src/com/android/providers/contacts/ContactsDatabaseHelper.java
index 76cbeb1..bda9826 100644
--- a/src/com/android/providers/contacts/ContactsDatabaseHelper.java
+++ b/src/com/android/providers/contacts/ContactsDatabaseHelper.java
@@ -82,7 +82,9 @@ import com.android.providers.contacts.database.ContactsTableUtil;
import com.android.providers.contacts.database.DeletedContactsTableUtil;
import com.android.providers.contacts.database.MoreDatabaseUtils;
import com.android.providers.contacts.util.NeededForTesting;
+
import com.google.android.collect.Sets;
+import com.google.common.annotations.VisibleForTesting;
import java.util.HashMap;
import java.util.Locale;
@@ -114,7 +116,7 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
* 900-999 L
* </pre>
*/
- static final int DATABASE_VERSION = 905;
+ static final int DATABASE_VERSION = 906;
public interface Tables {
public static final String CONTACTS = "contacts";
@@ -2769,6 +2771,11 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
oldVersion = 905;
}
+ if (oldVersion < 906) {
+ upgradeToVersion906(db);
+ oldVersion = 906;
+ }
+
if (upgradeViewsAndTriggers) {
createContactsViews(db);
createGroupsView(db);
@@ -4147,6 +4154,25 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE calls ADD transcription TEXT;");
}
+ /**
+ * Upgrades the database with the new value for {@link PinnedPositions#UNPINNED}. In this
+ * database version upgrade, the value is changed from 2147483647 (Integer.MAX_VALUE) to 0.
+ *
+ * The first pinned contact now starts from position 1.
+ */
+ @VisibleForTesting
+ public void upgradeToVersion906(SQLiteDatabase db) {
+ db.execSQL("UPDATE contacts SET pinned = pinned + 1"
+ + " WHERE pinned >= 0 AND pinned < 2147483647;");
+ db.execSQL("UPDATE raw_contacts SET pinned = pinned + 1"
+ + " WHERE pinned >= 0 AND pinned < 2147483647;");
+
+ db.execSQL("UPDATE contacts SET pinned = 0"
+ + " WHERE pinned = 2147483647;");
+ db.execSQL("UPDATE raw_contacts SET pinned = 0"
+ + " WHERE pinned = 2147483647;");
+ }
+
public String extractHandleFromEmailAddress(String email) {
Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(email);
if (tokens.length == 0) {
diff --git a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
index 31f8917..25e803e 100644
--- a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
+++ b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
@@ -28,6 +28,7 @@ import android.content.Entity;
import android.content.EntityIterator;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.ContactsContract;
@@ -85,6 +86,7 @@ import com.android.providers.contacts.testutil.DeletedContactUtil;
import com.android.providers.contacts.testutil.RawContactUtil;
import com.android.providers.contacts.testutil.TestUtil;
import com.android.providers.contacts.tests.R;
+
import com.google.android.collect.Lists;
import com.google.android.collect.Sets;
@@ -8176,6 +8178,56 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test {
);
}
+ /**
+ * Verifies that any existing pinned contacts have their pinned positions incremented by one
+ * after the upgrade step
+ */
+ public void testPinnedPositionsUpgradeTo906_PinnedContactsIncrementedByOne() {
+ final DatabaseAsserts.ContactIdPair i1 = DatabaseAsserts.assertAndCreateContact(mResolver);
+ final DatabaseAsserts.ContactIdPair i2 = DatabaseAsserts.assertAndCreateContact(mResolver);
+ final DatabaseAsserts.ContactIdPair i3 = DatabaseAsserts.assertAndCreateContact(mResolver);
+ final ArrayList<ContentProviderOperation> operations =
+ new ArrayList<ContentProviderOperation>();
+ operations.add(newPinningOperation(i1.mContactId, 0, true));
+ operations.add(newPinningOperation(i2.mContactId, 5, true));
+ operations.add(newPinningOperation(i3.mContactId, Integer.MAX_VALUE - 2, true));
+ CommonDatabaseUtils.applyBatch(mResolver, operations);
+
+ final ContactsDatabaseHelper helper =
+ ((ContactsDatabaseHelper) ((ContactsProvider2) getProvider()).getDatabaseHelper());
+ SQLiteDatabase db = helper.getWritableDatabase();
+ helper.upgradeToVersion906(db);
+ assertStoredValuesWithProjection(Contacts.CONTENT_URI,
+ cv(Contacts._ID, i1.mContactId, Contacts.PINNED, 1),
+ cv(Contacts._ID, i2.mContactId, Contacts.PINNED, 6),
+ cv(Contacts._ID, i3.mContactId, Contacts.PINNED, Integer.MAX_VALUE - 1)
+ );
+ }
+
+ /**
+ * Verifies that any unpinned contacts (or those with pinned position Integer.MAX_VALUE - 1)
+ * have their pinned positions correctly set to 0 after the upgrade step.
+ */
+ public void testPinnedPositionsUpgradeTo906_UnpinnedValueCorrectlyUpdated() {
+ final DatabaseAsserts.ContactIdPair i1 = DatabaseAsserts.assertAndCreateContact(mResolver);
+ final DatabaseAsserts.ContactIdPair i2 = DatabaseAsserts.assertAndCreateContact(mResolver);
+ final ArrayList<ContentProviderOperation> operations =
+ new ArrayList<ContentProviderOperation>();
+ operations.add(newPinningOperation(i1.mContactId, Integer.MAX_VALUE -1 , true));
+ operations.add(newPinningOperation(i2.mContactId, Integer.MAX_VALUE, true));
+ CommonDatabaseUtils.applyBatch(mResolver, operations);
+
+ final ContactsDatabaseHelper helper =
+ ((ContactsDatabaseHelper) ((ContactsProvider2) getProvider()).getDatabaseHelper());
+ SQLiteDatabase db = helper.getWritableDatabase();
+ helper.upgradeToVersion906(db);
+
+ assertStoredValuesWithProjection(Contacts.CONTENT_URI,
+ cv(Contacts._ID, i1.mContactId, Contacts.PINNED, 0),
+ cv(Contacts._ID, i2.mContactId, Contacts.PINNED, 0)
+ );
+ }
+
private ContentProviderOperation newPinningOperation(long id, int pinned, boolean star) {
final Uri uri = Uri.withAppendedPath(Contacts.CONTENT_URI, String.valueOf(id));
final ContentValues values = new ContentValues();