summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZheng Fu <zhengfu@google.com>2015-06-11 18:27:51 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2015-06-11 18:27:51 +0000
commit007375d04b398a825e935f5ca4b86ff4853e6941 (patch)
tree1c755dc8f102cdae7fce8ef792a7f2b8552aba03
parentcf6adcbba1739c8ac88aeca3411f7fa4853b9005 (diff)
parent3ad4f2e72b677e3f7936af17f831bf6cdd95aa69 (diff)
downloadpackages_providers_ContactsProvider-007375d04b398a825e935f5ca4b86ff4853e6941.zip
packages_providers_ContactsProvider-007375d04b398a825e935f5ca4b86ff4853e6941.tar.gz
packages_providers_ContactsProvider-007375d04b398a825e935f5ca4b86ff4853e6941.tar.bz2
am 3ad4f2e7: Merge "Revert metadata_sync table and related code from M" into mnc-dev
* commit '3ad4f2e72b677e3f7936af17f831bf6cdd95aa69': Revert metadata_sync table and related code from M
-rw-r--r--src/com/android/providers/contacts/ContactsDatabaseHelper.java93
-rw-r--r--src/com/android/providers/contacts/ContactsProvider2.java223
-rw-r--r--src/com/android/providers/contacts/MetadataEntryParser.java274
-rw-r--r--tests/assets/test1/testFileDeviceContactMetadataJSON.txt69
-rw-r--r--tests/src/com/android/providers/contacts/ContactsProvider2Test.java109
-rw-r--r--tests/src/com/android/providers/contacts/MetadataEntryParserTest.java305
6 files changed, 10 insertions, 1063 deletions
diff --git a/src/com/android/providers/contacts/ContactsDatabaseHelper.java b/src/com/android/providers/contacts/ContactsDatabaseHelper.java
index 2bccea1..d20358a 100644
--- a/src/com/android/providers/contacts/ContactsDatabaseHelper.java
+++ b/src/com/android/providers/contacts/ContactsDatabaseHelper.java
@@ -63,7 +63,6 @@ import android.provider.ContactsContract.DisplayNameSources;
import android.provider.ContactsContract.DisplayPhoto;
import android.provider.ContactsContract.FullNameStyle;
import android.provider.ContactsContract.Groups;
-import android.provider.ContactsContract.MetadataSync;
import android.provider.ContactsContract.PhoneticNameStyle;
import android.provider.ContactsContract.PhotoFiles;
import android.provider.ContactsContract.PinnedPositions;
@@ -151,7 +150,6 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
public static final String DEFAULT_DIRECTORY = "default_directory";
public static final String SEARCH_INDEX = "search_index";
public static final String VOICEMAIL_STATUS = "voicemail_status";
- public static final String METADATA_SYNC = "metadata_sync";
public static final String PRE_AUTHORIZED_URIS = "pre_authorized_uris";
// This list of tables contains auto-incremented sequences.
@@ -306,7 +304,6 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
public static final String GROUPS = "view_groups";
public static final String DATA_USAGE_STAT = "view_data_usage_stat";
public static final String STREAM_ITEMS = "view_stream_items";
- public static final String METADATA_SYNC = "view_metadata_sync";
}
public interface Projections {
@@ -732,12 +729,6 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
public static final int USAGE_TYPE_INT_SHORT_TEXT = 2;
}
- public interface MetadataSyncColumns {
- static final String CONCRETE_ID = Tables.METADATA_SYNC + "._id";
- static final String ACCOUNT_ID = "account_id";
- static final String CONCRETE_ACCOUNT_ID = Tables.METADATA_SYNC + "." + ACCOUNT_ID;
- }
-
private interface EmailQuery {
public static final String TABLE = Tables.DATA;
@@ -1590,19 +1581,6 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
DataUsageStatColumns.USAGE_TYPE_INT +
");");
- db.execSQL("CREATE TABLE IF NOT EXISTS "
- + Tables.METADATA_SYNC + " (" +
- MetadataSync._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
- MetadataSync.RAW_CONTACT_BACKUP_ID + " TEXT NOT NULL," +
- MetadataSyncColumns.ACCOUNT_ID + " INTEGER NOT NULL," +
- MetadataSync.DATA + " TEXT," +
- MetadataSync.DELETED + " INTEGER NOT NULL DEFAULT 0);");
-
- db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS metadata_sync_index ON " +
- Tables.METADATA_SYNC + " (" +
- MetadataSync.RAW_CONTACT_BACKUP_ID + ", " +
- MetadataSyncColumns.ACCOUNT_ID +");");
-
db.execSQL("CREATE TABLE " + Tables.PRE_AUTHORIZED_URIS + " ("+
PreAuthorizedUris._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
PreAuthorizedUris.URI + " STRING NOT NULL, " +
@@ -1846,7 +1824,6 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
db.execSQL("DROP VIEW IF EXISTS " + Views.ENTITIES + ";");
db.execSQL("DROP VIEW IF EXISTS " + Views.DATA_USAGE_STAT + ";");
db.execSQL("DROP VIEW IF EXISTS " + Views.STREAM_ITEMS + ";");
- db.execSQL("DROP VIEW IF EXISTS " + Views.METADATA_SYNC + ";");
String dataColumns =
Data.IS_PRIMARY + ", "
@@ -2174,21 +2151,6 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
+ RawContactsColumns.CONCRETE_CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID + ")";
db.execSQL("CREATE VIEW " + Views.STREAM_ITEMS + " AS " + streamItemSelect);
-
- String metadataSyncSelect = "SELECT " +
- MetadataSyncColumns.CONCRETE_ID + ", " +
- MetadataSync.RAW_CONTACT_BACKUP_ID + ", " +
- AccountsColumns.ACCOUNT_NAME + ", " +
- AccountsColumns.ACCOUNT_TYPE + ", " +
- AccountsColumns.DATA_SET + ", " +
- MetadataSync.DATA + ", " +
- MetadataSync.DELETED +
- " FROM " + Tables.METADATA_SYNC
- + " JOIN " + Tables.ACCOUNTS + " ON ("
- + MetadataSyncColumns.CONCRETE_ACCOUNT_ID + "=" + AccountsColumns.CONCRETE_ID
- + ")";
-
- db.execSQL("CREATE VIEW " + Views.METADATA_SYNC + " AS " + metadataSyncSelect);
}
private static String buildDisplayPhotoUriAlias(String contactIdColumn, String alias) {
@@ -2890,11 +2852,6 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
upgradeViewsAndTriggers = true;
oldVersion = 1000;
}
- if (oldVersion < 1001) {
- upgradeToVersion1001(db);
- rebuildSqliteStats = true;
- oldVersion = 1001;
- }
if (oldVersion < 1002) {
rebuildSqliteStats = true;
@@ -2927,18 +2884,18 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
oldVersion = 1007;
}
- if (oldVersion < 1008) {
- upgradeToVersion1008(db);
- upgradeViewsAndTriggers = true;
- oldVersion = 1008;
- }
-
if (oldVersion < 1009) {
upgradeToVersion1009(db);
oldVersion = 1009;
}
if (oldVersion < 1010) {
+ upgradeToVersion1010(db);
+ rebuildSqliteStats = true;
+ oldVersion = 1010;
+ }
+
+ if (oldVersion < 1010) {
upgradeViewsAndTriggers = true;
oldVersion = 1010;
}
@@ -4381,21 +4338,6 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
db.execSQL("CREATE INDEX IF NOT EXISTS data_hash_id_index ON data (hash_id);");
}
- /**
- * Add new metadata_sync table to cache the meta data on raw contacts level from server before
- * they are merged into other CP2 tables. The data column is the blob column containing all
- * the backed up metadata for this raw_contact. This table should only be used by metadata
- * sync adapter.
- */
- private void upgradeToVersion1001(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE metadata_sync (" +
- "_id INTEGER PRIMARY KEY AUTOINCREMENT, raw_contact_backup_id TEXT NOT NULL, " +
- "account_id INTEGER NOT NULL, data TEXT, " +
- "FOREIGN KEY(account_id) REFERENCES accounts(_id));");
- db.execSQL("CREATE UNIQUE INDEX metadata_sync_index ON metadata_sync (" +
- "raw_contact_backup_id, account_id);");
- }
-
@VisibleForTesting
public void upgradeToVersion1002(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS pre_authorized_uris;");
@@ -4467,28 +4409,16 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
// Log verbose because this should be the majority case.
Log.v(TAG, "Version 1007: Columns already exist, skipping upgrade steps.");
}
- }
-
- /**
- * The metadata_sync APIs were not in-use by anyone in the time
- * between their initial creation (in v1001) and this update. So we're just dropping
- * and re-creating it to get appropriate columns. The delta is as follows:
- * - drop foreign key constraint on account_id
- * - add deleted column
- */
- public void upgradeToVersion1008(SQLiteDatabase db) {
- db.execSQL("DROP TABLE IF EXISTS metadata_sync");
- db.execSQL("CREATE TABLE metadata_sync (" +
- "_id INTEGER PRIMARY KEY AUTOINCREMENT, raw_contact_backup_id TEXT NOT NULL, " +
- "account_id INTEGER NOT NULL, data TEXT, deleted INTEGER NOT NULL DEFAULT 0);");
- db.execSQL("CREATE UNIQUE INDEX metadata_sync_index ON metadata_sync (" +
- "raw_contact_backup_id, account_id);");
}
public void upgradeToVersion1009(SQLiteDatabase db) {
db.execSQL("ALTER TABLE data ADD carrier_presence INTEGER NOT NULL DEFAULT 0");
}
+ public void upgradeToVersion1010(SQLiteDatabase db) {
+ db.execSQL("DROP TABLE IF EXISTS metadata_sync");
+ }
+
public String extractHandleFromEmailAddress(String email) {
Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(email);
if (tokens.length == 0) {
@@ -4677,9 +4607,6 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper {
updateIndexStats(db, Tables.DATA_USAGE_STAT,
"data_usage_stat_index", "20 2 1");
- updateIndexStats(db, Tables.METADATA_SYNC,
- "metadata_sync_index", "10000 1 1");
-
// Tiny tables
updateIndexStats(db, Tables.AGGREGATION_EXCEPTIONS,
null, "10");
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index c881c65..886bc94 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -153,11 +153,6 @@ import com.android.providers.contacts.aggregation.util.CommonNicknameCache;
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.MetadataEntryParser.AggregationData;
-import com.android.providers.contacts.MetadataEntryParser.FieldData;
-import com.android.providers.contacts.MetadataEntryParser.MetadataEntry;
-import com.android.providers.contacts.MetadataEntryParser.RawContactInfo;
-import com.android.providers.contacts.MetadataEntryParser.UsageStats;
import com.android.providers.contacts.util.Clock;
import com.android.providers.contacts.util.ContactsPermissions;
import com.android.providers.contacts.util.DbQueryUtils;
@@ -4705,162 +4700,6 @@ public class ContactsProvider2 extends AbstractContactsProvider
scheduleBackgroundTask(BACKGROUND_TASK_UPDATE_ACCOUNTS);
}
- interface RawContactsBackupQuery {
- String TABLE = Tables.RAW_CONTACTS;
- String[] COLUMNS = new String[] {
- RawContacts._ID,
- };
- int RAW_CONTACT_ID = 0;
- String SELECTION = RawContacts.DELETED + "=0 AND " +
- RawContacts.BACKUP_ID + "=? AND " +
- RawContactsColumns.ACCOUNT_ID + "=?";
- }
-
- /**
- * Fetch rawContactId related to the given backupId.
- * Return 0 if there's no such rawContact or it's deleted.
- */
- private long queryRawContactId(SQLiteDatabase db, String backupId, long accountId) {
- if (TextUtils.isEmpty(backupId)) {
- return 0;
- }
- mSelectionArgs2[0] = backupId;
- mSelectionArgs2[1] = String.valueOf(accountId);
- long rawContactId = 0;
- final Cursor cursor = db.query(RawContactsBackupQuery.TABLE,
- RawContactsBackupQuery.COLUMNS, RawContactsBackupQuery.SELECTION,
- mSelectionArgs2, null, null, null);
- try {
- if (cursor.moveToFirst()) {
- rawContactId = cursor.getLong(RawContactsBackupQuery.RAW_CONTACT_ID);
- }
- } finally {
- cursor.close();
- }
- return rawContactId;
- }
-
- interface DataHashQuery {
- String TABLE = Tables.DATA;
- String[] COLUMNS = new String[] {
- Data._ID,
- };
- int DATA_ID = 0;
- String SELECTION = Data.HASH_ID + "=?";
- }
-
- /**
- * Fetch a list of dataId related to the given hashId.
- * Return empty list if there's no such data.
- */
- private ArrayList<Long> queryDataId(SQLiteDatabase db, String hashId) {
- if (TextUtils.isEmpty(hashId)) {
- return new ArrayList<>();
- }
- mSelectionArgs1[0] = hashId;
- ArrayList<Long> result = new ArrayList<>();
- long dataId = 0;
- final Cursor c = db.query(DataHashQuery.TABLE, DataHashQuery.COLUMNS,
- DataHashQuery.SELECTION, mSelectionArgs1, null, null, null);
- try {
- while (c.moveToNext()) {
- dataId = c.getLong(DataHashQuery.DATA_ID);
- result.add(dataId);
- }
- } finally {
- c.close();
- }
- return result;
- }
-
- private long searchRawContactIdForRawContactInfo(SQLiteDatabase db,
- RawContactInfo rawContactInfo) {
- if (rawContactInfo == null) {
- return 0;
- }
- final String backupId = rawContactInfo.mBackupId;
- final String accountType = rawContactInfo.mAccountType;
- final String accountName = rawContactInfo.mAccountName;
- final String dataSet = rawContactInfo.mDataSet;
- ContentValues values = new ContentValues();
- values.put(AccountsColumns.ACCOUNT_TYPE, accountType);
- values.put(AccountsColumns.ACCOUNT_NAME, accountName);
- if (dataSet != null) {
- values.put(AccountsColumns.DATA_SET, dataSet);
- }
-
- final long accountId = replaceAccountInfoByAccountId(RawContacts.CONTENT_URI, values);
- final long rawContactId = queryRawContactId(db, backupId, accountId);
- return rawContactId;
- }
-
- /**
- * Update RawContact, Data, DataUsageStats, AggregationException tables from MetadataEntry.
- */
- @NeededForTesting
- void updateFromMetaDataEntry(SQLiteDatabase db, MetadataEntry metadataEntry) {
- final RawContactInfo rawContactInfo = metadataEntry.mRawContactInfo;
- final long rawContactId = searchRawContactIdForRawContactInfo(db, rawContactInfo);
- if (rawContactId == 0) {
- return;
- }
-
- ContentValues rawContactValues = new ContentValues();
- rawContactValues.put(RawContacts.SEND_TO_VOICEMAIL, metadataEntry.mSendToVoicemail);
- rawContactValues.put(RawContacts.STARRED, metadataEntry.mStarred);
- rawContactValues.put(RawContacts.PINNED, metadataEntry.mPinned);
- updateRawContact(db, rawContactId, rawContactValues, true);
-
- // Update Data and DataUsageStats table.
- for (int i = 0; i < metadataEntry.mFieldDatas.size(); i++) {
- final FieldData fieldData = metadataEntry.mFieldDatas.get(i);
- final String dataHashId = fieldData.mDataHashId;
- final ArrayList<Long> dataIds = queryDataId(db, dataHashId);
-
- for (long dataId : dataIds) {
- // Update is_primary and is_super_primary.
- ContentValues dataValues = new ContentValues();
- dataValues.put(Data.IS_PRIMARY, fieldData.mIsPrimary);
- dataValues.put(Data.IS_SUPER_PRIMARY, fieldData.mIsSuperPrimary);
- updateData(ContentUris.withAppendedId(Data.CONTENT_URI, dataId),
- dataValues, null, null, true);
-
- // Update UsageStats.
- for (int j = 0; j < fieldData.mUsageStatsList.size(); j++) {
- final UsageStats usageStats = fieldData.mUsageStatsList.get(j);
- final String usageType = usageStats.mUsageType;
- final int typeInt = getDataUsageFeedbackType(usageType.toLowerCase(), null);
- final long lastTimeUsed = usageStats.mLastTimeUsed;
- final int timesUsed = usageStats.mTimesUsed;
- ContentValues usageStatsValues = new ContentValues();
- usageStatsValues.put(DataUsageStatColumns.DATA_ID, dataId);
- usageStatsValues.put(DataUsageStatColumns.USAGE_TYPE_INT, typeInt);
- usageStatsValues.put(DataUsageStatColumns.LAST_TIME_USED, lastTimeUsed);
- usageStatsValues.put(DataUsageStatColumns.TIMES_USED, timesUsed);
- updateDataUsageStats(db, usageStatsValues);
- }
- }
- }
-
- // Update AggregationException table.
- for (int i = 0; i < metadataEntry.mAggregationDatas.size(); i++) {
- final AggregationData aggregationData = metadataEntry.mAggregationDatas.get(i);
- final int typeInt = getAggregationType(aggregationData.mType, null);
- final RawContactInfo aggregationContact1 = aggregationData.mRawContactInfo1;
- final RawContactInfo aggregationContact2 = aggregationData.mRawContactInfo2;
- final long rawContactId1 = searchRawContactIdForRawContactInfo(db, aggregationContact1);
- final long rawContactId2 = searchRawContactIdForRawContactInfo(db, aggregationContact2);
- if (rawContactId1 == 0 || rawContactId2 == 0) {
- continue;
- }
- ContentValues values = new ContentValues();
- values.put(AggregationExceptions.RAW_CONTACT_ID1, rawContactId1);
- values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2);
- values.put(AggregationExceptions.TYPE, typeInt);
- updateAggregationException(db, values);
- }
- }
-
/** return serialized version of {@code accounts} */
@VisibleForTesting
static String accountsToString(Set<Account> accounts) {
@@ -9261,52 +9100,6 @@ public class ContactsProvider2 extends AbstractContactsProvider
}
/**
- * Update {@link Tables#DATA_USAGE_STAT}.
- * Update or insert usageType, lastTimeUsed, and timesUsed for specific dataId.
- */
- private void updateDataUsageStats(SQLiteDatabase db, ContentValues values) {
- final String dataId = values.getAsString(DataUsageStatColumns.DATA_ID);
- final String type = values.getAsString(DataUsageStatColumns.USAGE_TYPE_INT);
- final String lastTimeUsed = values.getAsString(DataUsageStatColumns.LAST_TIME_USED);
- final String timesUsed = values.getAsString(DataUsageStatColumns.TIMES_USED);
-
- mSelectionArgs2[0] = dataId;
- mSelectionArgs2[1] = type;
- final Cursor cursor = db.query(DataUsageStatQuery.TABLE,
- DataUsageStatQuery.COLUMNS, DataUsageStatQuery.SELECTION,
- mSelectionArgs2, null, null, null);
-
- try {
- if (cursor.moveToFirst()) {
- final long id = cursor.getLong(DataUsageStatQuery.ID);
-
- mSelectionArgs3[0] = lastTimeUsed;
- mSelectionArgs3[1] = timesUsed;
- mSelectionArgs3[2] = String.valueOf(id);
- db.execSQL("UPDATE " + Tables.DATA_USAGE_STAT +
- " SET " + DataUsageStatColumns.LAST_TIME_USED + "=?" +
- "," + DataUsageStatColumns.TIMES_USED + "=?" +
- " WHERE " + DataUsageStatColumns._ID + "=?",
- mSelectionArgs3);
- } else {
- mSelectionArgs4[0] = dataId;
- mSelectionArgs4[1] = type;
- mSelectionArgs4[2] = timesUsed;
- mSelectionArgs4[3] = lastTimeUsed;
- db.execSQL("INSERT INTO " + Tables.DATA_USAGE_STAT +
- "(" + DataUsageStatColumns.DATA_ID +
- "," + DataUsageStatColumns.USAGE_TYPE_INT +
- "," + DataUsageStatColumns.TIMES_USED +
- "," + DataUsageStatColumns.LAST_TIME_USED +
- ") VALUES (?,?,?,?)",
- mSelectionArgs4);
- }
- } finally {
- cursor.close();
- }
- }
-
- /**
* Returns a sort order String for promoting data rows (email addresses, phone numbers, etc.)
* associated with a primary account. The primary account should be supplied from applications
* with {@link ContactsContract#PRIMARY_ACCOUNT_NAME} and
@@ -9480,22 +9273,6 @@ public class ContactsProvider2 extends AbstractContactsProvider
throw new IllegalArgumentException("Invalid usage type " + type);
}
- private static final int getAggregationType(String type, Integer defaultType) {
- if ("TOGETHER".equalsIgnoreCase(type)) {
- return AggregationExceptions.TYPE_KEEP_TOGETHER; // 1
- }
- if ("SEPARATE".equalsIgnoreCase(type)) {
- return AggregationExceptions.TYPE_KEEP_SEPARATE; // 2
- }
- if ("UNSET".equalsIgnoreCase(type)) {
- return AggregationExceptions.TYPE_AUTOMATIC; // 0
- }
- if (defaultType != null) {
- return defaultType;
- }
- throw new IllegalArgumentException("Invalid aggregation type " + type);
- }
-
/** Use only for debug logging */
@Override
public String toString() {
diff --git a/src/com/android/providers/contacts/MetadataEntryParser.java b/src/com/android/providers/contacts/MetadataEntryParser.java
deleted file mode 100644
index b90f938..0000000
--- a/src/com/android/providers/contacts/MetadataEntryParser.java
+++ /dev/null
@@ -1,274 +0,0 @@
-/*
- * Copyright (C) 2015 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.providers.contacts;
-
-import android.text.TextUtils;
-import com.android.providers.contacts.util.NeededForTesting;
-
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-import java.util.ArrayList;
-
-@NeededForTesting
-public class MetadataEntryParser {
-
- private final static String UNIQUE_CONTACT_ID = "unique_contact_id";
- private final static String ACCOUNT_TYPE = "account_type";
- private final static String CUSTOM_ACCOUNT_TYPE = "custom_account_type";
- private final static String ENUM_VALUE_FOR_GOOGLE_ACCOUNT = "GOOGLE_ACCOUNT";
- private final static String GOOGLE_ACCOUNT_TYPE = "com.google";
- private final static String ENUM_VALUE_FOR_CUSTOM_ACCOUNT = "CUSTOM_ACCOUNT";
- private final static String ACCOUNT_NAME = "account_name";
- private final static String DATA_SET = "data_set";
- private final static String ENUM_FOR_PLUS_DATA_SET = "GOOGLE_PLUS";
- private final static String ENUM_FOR_CUSTOM_DATA_SET = "CUSTOM";
- private final static String PLUS_DATA_SET_TYPE = "plus";
- private final static String CUSTOM_DATA_SET = "custom_data_set";
- private final static String CONTACT_ID = "contact_id";
- private final static String CONTACT_PREFS = "contact_prefs";
- private final static String SEND_TO_VOICEMAIL = "send_to_voicemail";
- private final static String STARRED = "starred";
- private final static String PINNED = "pinned";
- private final static String AGGREGATION_DATA = "aggregation_data";
- private final static String CONTACT_IDS = "contact_ids";
- private final static String TYPE = "type";
- private final static String FIELD_DATA = "field_data";
- private final static String FIELD_DATA_ID = "field_data_id";
- private final static String FIELD_DATA_PREFS = "field_data_prefs";
- private final static String IS_PRIMARY = "is_primary";
- private final static String IS_SUPER_PRIMARY = "is_super_primary";
- private final static String USAGE_STATS = "usage_stats";
- private final static String USAGE_TYPE = "usage_type";
- private final static String LAST_TIME_USED = "last_time_used";
- private final static String USAGE_COUNT = "usage_count";
-
- @NeededForTesting
- public static class UsageStats {
- final String mUsageType;
- final long mLastTimeUsed;
- final int mTimesUsed;
-
- @NeededForTesting
- public UsageStats(String usageType, long lastTimeUsed, int timesUsed) {
- this.mUsageType = usageType;
- this.mLastTimeUsed = lastTimeUsed;
- this.mTimesUsed = timesUsed;
- }
- }
-
- @NeededForTesting
- public static class FieldData {
- final String mDataHashId;
- final boolean mIsPrimary;
- final boolean mIsSuperPrimary;
- final ArrayList<UsageStats> mUsageStatsList;
-
- @NeededForTesting
- public FieldData(String dataHashId, boolean isPrimary, boolean isSuperPrimary,
- ArrayList<UsageStats> usageStatsList) {
- this.mDataHashId = dataHashId;
- this.mIsPrimary = isPrimary;
- this.mIsSuperPrimary = isSuperPrimary;
- this.mUsageStatsList = usageStatsList;
- }
- }
-
- @NeededForTesting
- public static class RawContactInfo {
- final String mBackupId;
- final String mAccountType;
- final String mAccountName;
- final String mDataSet;
-
- @NeededForTesting
- public RawContactInfo(String backupId, String accountType, String accountName,
- String dataSet) {
- this.mBackupId = backupId;
- this.mAccountType = accountType;
- this.mAccountName = accountName;
- mDataSet = dataSet;
- }
- }
-
- @NeededForTesting
- public static class AggregationData {
- final RawContactInfo mRawContactInfo1;
- final RawContactInfo mRawContactInfo2;
- final String mType;
-
- @NeededForTesting
- public AggregationData(RawContactInfo rawContactInfo1, RawContactInfo rawContactInfo2,
- String type) {
- this.mRawContactInfo1 = rawContactInfo1;
- this.mRawContactInfo2 = rawContactInfo2;
- this.mType = type;
- }
- }
-
- @NeededForTesting
- public static class MetadataEntry {
- final RawContactInfo mRawContactInfo;
- final int mSendToVoicemail;
- final int mStarred;
- final int mPinned;
- final ArrayList<FieldData> mFieldDatas;
- final ArrayList<AggregationData> mAggregationDatas;
-
- @NeededForTesting
- public MetadataEntry(RawContactInfo rawContactInfo,
- int sendToVoicemail, int starred, int pinned,
- ArrayList<FieldData> fieldDatas,
- ArrayList<AggregationData> aggregationDatas) {
- this.mRawContactInfo = rawContactInfo;
- this.mSendToVoicemail = sendToVoicemail;
- this.mStarred = starred;
- this.mPinned = pinned;
- this.mFieldDatas = fieldDatas;
- this.mAggregationDatas = aggregationDatas;
- }
- }
-
- @NeededForTesting
- static MetadataEntry parseDataToMetaDataEntry(String inputData) {
- if (TextUtils.isEmpty(inputData)) {
- throw new IllegalArgumentException("Input cannot be empty.");
- }
-
- try {
- final JSONObject root = new JSONObject(inputData);
- // Parse to get rawContactId and account info.
- final JSONObject uniqueContactJSON = root.getJSONObject(UNIQUE_CONTACT_ID);
- final RawContactInfo rawContactInfo = parseUniqueContact(uniqueContactJSON);
-
- // Parse contactPrefs to get sendToVoicemail, starred, pinned.
- final JSONObject contactPrefs = root.getJSONObject(CONTACT_PREFS);
- final boolean sendToVoicemail = contactPrefs.getBoolean(SEND_TO_VOICEMAIL);
- final boolean starred = contactPrefs.getBoolean(STARRED);
- final int pinned = contactPrefs.getInt(PINNED);
-
- // Parse aggregationDatas
- final ArrayList<AggregationData> aggregationsList = new ArrayList<AggregationData>();
- if (root.has(AGGREGATION_DATA)) {
- final JSONArray aggregationDatas = root.getJSONArray(AGGREGATION_DATA);
-
- for (int i = 0; i < aggregationDatas.length(); i++) {
- final JSONObject aggregationData = aggregationDatas.getJSONObject(i);
- final JSONArray contacts = aggregationData.getJSONArray(CONTACT_IDS);
-
- if (contacts.length() != 2) {
- throw new IllegalArgumentException(
- "There should be two contacts for each aggregation.");
- }
- final JSONObject rawContact1 = contacts.getJSONObject(0);
- final RawContactInfo aggregationContact1 = parseUniqueContact(rawContact1);
- final JSONObject rawContact2 = contacts.getJSONObject(1);
- final RawContactInfo aggregationContact2 = parseUniqueContact(rawContact2);
- final String type = aggregationData.getString(TYPE);
- if (TextUtils.isEmpty(type)) {
- throw new IllegalArgumentException("Aggregation type cannot be empty.");
- }
-
- final AggregationData aggregation = new AggregationData(
- aggregationContact1, aggregationContact2, type);
- aggregationsList.add(aggregation);
- }
- }
-
- // Parse fieldDatas
- final ArrayList<FieldData> fieldDatasList = new ArrayList<FieldData>();
- if (root.has(FIELD_DATA)) {
- final JSONArray fieldDatas = root.getJSONArray(FIELD_DATA);
-
- for (int i = 0; i < fieldDatas.length(); i++) {
- final JSONObject fieldData = fieldDatas.getJSONObject(i);
- final String dataHashId = fieldData.getString(FIELD_DATA_ID);
- if (TextUtils.isEmpty(dataHashId)) {
- throw new IllegalArgumentException("Field data hash id cannot be empty.");
- }
- final JSONObject fieldDataPrefs = fieldData.getJSONObject(FIELD_DATA_PREFS);
- final boolean isPrimary = fieldDataPrefs.getBoolean(IS_PRIMARY);
- final boolean isSuperPrimary = fieldDataPrefs.getBoolean(IS_SUPER_PRIMARY);
-
- final ArrayList<UsageStats> usageStatsList = new ArrayList<UsageStats>();
- if (fieldData.has(USAGE_STATS)) {
- final JSONArray usageStats = fieldData.getJSONArray(USAGE_STATS);
- for (int j = 0; j < usageStats.length(); j++) {
- final JSONObject usageStat = usageStats.getJSONObject(j);
- final String usageType = usageStat.getString(USAGE_TYPE);
- if (TextUtils.isEmpty(usageType)) {
- throw new IllegalArgumentException("Usage type cannot be empty.");
- }
- final long lastTimeUsed = usageStat.getLong(LAST_TIME_USED);
- final int usageCount = usageStat.getInt(USAGE_COUNT);
-
- final UsageStats usageStatsParsed = new UsageStats(
- usageType, lastTimeUsed, usageCount);
- usageStatsList.add(usageStatsParsed);
- }
- }
-
- final FieldData fieldDataParse = new FieldData(dataHashId, isPrimary,
- isSuperPrimary, usageStatsList);
- fieldDatasList.add(fieldDataParse);
- }
- }
- final MetadataEntry metaDataEntry = new MetadataEntry(rawContactInfo,
- sendToVoicemail ? 1 : 0, starred ? 1 : 0, pinned,
- fieldDatasList, aggregationsList);
- return metaDataEntry;
- } catch (JSONException e) {
- throw new IllegalArgumentException("JSON Exception.", e);
- }
- }
-
- private static RawContactInfo parseUniqueContact(JSONObject uniqueContactJSON) {
- try {
- final String backupId = uniqueContactJSON.getString(CONTACT_ID);
- final String accountName = uniqueContactJSON.getString(ACCOUNT_NAME);
- String accountType = uniqueContactJSON.getString(ACCOUNT_TYPE);
- if (ENUM_VALUE_FOR_GOOGLE_ACCOUNT.equals(accountType)) {
- accountType = GOOGLE_ACCOUNT_TYPE;
- } else if (ENUM_VALUE_FOR_CUSTOM_ACCOUNT.equals(accountType)) {
- accountType = uniqueContactJSON.getString(CUSTOM_ACCOUNT_TYPE);
- } else {
- throw new IllegalArgumentException("Unknown account type.");
- }
-
- String dataSet = null;
- switch (uniqueContactJSON.getString(DATA_SET)) {
- case ENUM_FOR_PLUS_DATA_SET:
- dataSet = PLUS_DATA_SET_TYPE;
- break;
- case ENUM_FOR_CUSTOM_DATA_SET:
- dataSet = uniqueContactJSON.getString(CUSTOM_DATA_SET);
- break;
- }
- if (TextUtils.isEmpty(backupId) || TextUtils.isEmpty(accountType)
- || TextUtils.isEmpty(accountName)) {
- throw new IllegalArgumentException(
- "Contact backup id, account type, account name cannot be empty.");
- }
- final RawContactInfo rawContactInfo = new RawContactInfo(
- backupId, accountType, accountName, dataSet);
- return rawContactInfo;
- } catch (JSONException e) {
- throw new IllegalArgumentException("JSON Exception.", e);
- }
- }
-}
diff --git a/tests/assets/test1/testFileDeviceContactMetadataJSON.txt b/tests/assets/test1/testFileDeviceContactMetadataJSON.txt
deleted file mode 100644
index 65e624d..0000000
--- a/tests/assets/test1/testFileDeviceContactMetadataJSON.txt
+++ /dev/null
@@ -1,69 +0,0 @@
-{
- "unique_contact_id": {
- "account_type": "CUSTOM_ACCOUNT",
- "custom_account_type": "facebook",
- "account_name": "android-test",
- "contact_id": "1111111",
- "data_set": "FOCUS"
- },
- "contact_prefs": {
- "send_to_voicemail": true,
- "starred": false,
- "pinned": 2
- },
- "aggregation_data": [
- {
- "type": "TOGETHER",
- "contact_ids": [
- {
- "account_type": "GOOGLE_ACCOUNT",
- "account_name": "android-test2",
- "contact_id": "2222222",
- "data_set": "GOOGLE_PLUS"
- },
- {
- "account_type": "GOOGLE_ACCOUNT",
- "account_name": "android-test3",
- "contact_id": "3333333",
- "data_set": "CUSTOM",
- "custom_data_set": "custom type"
- }
- ]
- }
- ],
- "field_data": [
- {
- "field_data_id": "1001",
- "field_data_prefs": {
- "is_primary": true,
- "is_super_primary": true
- },
- "usage_stats": [
- {
- "usage_type": "CALL",
- "last_time_used": 10000001,
- "usage_count": 10
- },
- {
- "usage_type": "SHORT_TEXT",
- "last_time_used": 20000002,
- "usage_count": 20
- }
- ]
- },
- {
- "field_data_id": "1002",
- "field_data_prefs": {
- "is_primary": false,
- "is_super_primary": false
- },
- "usage_stats": [
- {
- "usage_type": "LONG_TEXT",
- "last_time_used": 30000003,
- "usage_count": 30
- }
- ]
- }
- ]
-} \ No newline at end of file
diff --git a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
index 3c21d21..4f05f88 100644
--- a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
+++ b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
@@ -84,11 +84,6 @@ import com.android.providers.contacts.ContactsDatabaseHelper.DbProperties;
import com.android.providers.contacts.ContactsDatabaseHelper.PresenceColumns;
import com.android.providers.contacts.ContactsDatabaseHelper.RawContactsColumns;
import com.android.providers.contacts.ContactsDatabaseHelper.Tables;
-import com.android.providers.contacts.MetadataEntryParser.AggregationData;
-import com.android.providers.contacts.MetadataEntryParser.FieldData;
-import com.android.providers.contacts.MetadataEntryParser.MetadataEntry;
-import com.android.providers.contacts.MetadataEntryParser.RawContactInfo;
-import com.android.providers.contacts.MetadataEntryParser.UsageStats;
import com.android.providers.contacts.testutil.CommonDatabaseUtils;
import com.android.providers.contacts.testutil.ContactUtil;
import com.android.providers.contacts.testutil.DataUtil;
@@ -2738,110 +2733,6 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test {
assertStoredValuesOrderly(filterUri1, new ContentValues[] { v2, v1, v4, v3 });
}
- public void testUpdateFromMetadataEntry() {
- String accountType1 = "accountType1";
- String accountName1 = "accountName1";
- String dataSet1 = "plus";
- Account account1 = new Account(accountName1, accountType1);
- long rawContactId = RawContactUtil.createRawContactWithName(mResolver, account1);
- Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
- // Add backup_id for the raw contact.
- String backupId = "backupId100001";
- ContentValues values = new ContentValues();
- values.put(RawContacts.BACKUP_ID, backupId);
- assertEquals(1, mResolver.update(rawContactUri, values, null, null));
-
- String emailAddress = "address@email.com";
- Uri dataUri = insertEmail(rawContactId, emailAddress);
- String hashId = "hashId100002";
- ContentValues dataValues = new ContentValues();
- dataValues.put(Data.HASH_ID, hashId);
- assertEquals(1, mResolver.update(dataUri, dataValues, null, null));
-
- // Another data that should not be updated.
- String phoneNumber = "111-111-1111";
- Uri dataUri2 = insertPhoneNumber(rawContactId, phoneNumber);
- String hashId2 = "hashId100004";
- ContentValues dataValues2 = new ContentValues();
- dataValues.put(Data.HASH_ID, hashId2);
- mResolver.update(dataUri2, dataValues2, null, null);
-
- String accountType2 = "accountType2";
- String accountName2 = "accountName2";
- Account account2 = new Account(accountName2, accountType2);
- long rawContactId2 = RawContactUtil.createRawContactWithName(mResolver, account2);
- Uri rawContactUri2 = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId2);
- String backupId2 = "backupId100003";
- ContentValues values2 = new ContentValues();
- values2.put(RawContacts.BACKUP_ID, backupId2);
- assertEquals(1, mResolver.update(rawContactUri2, values2, null, null));
-
- String usageTypeString = "CALL";
- int lastTimeUsed = 1111111;
- int timesUsed = 5;
- String aggregationTypeString = "SEPARATE";
- int aggregationType = AggregationExceptions.TYPE_KEEP_SEPARATE;
-
- RawContactInfo rawContactInfo = new RawContactInfo(
- backupId, accountType1, accountName1, null);
- UsageStats usageStats = new UsageStats(usageTypeString, lastTimeUsed, timesUsed);
- ArrayList<UsageStats> usageStatsList = new ArrayList<>();
- usageStatsList.add(usageStats);
- FieldData fieldData = new FieldData(hashId, true, true, usageStatsList);
- ArrayList<FieldData> fieldDataList = new ArrayList<>();
- fieldDataList.add(fieldData);
- ArrayList<AggregationData> aggregationDataList = new ArrayList<>();
- MetadataEntry metadataEntry = new MetadataEntry(rawContactInfo,
- 1, 1, 1, fieldDataList, aggregationDataList);
-
- ContactsProvider2 provider = (ContactsProvider2) getProvider();
- final ContactsDatabaseHelper helper =
- ((ContactsDatabaseHelper) provider.getDatabaseHelper());
- SQLiteDatabase db = helper.getWritableDatabase();
-
- // Before updating tables from MetadataEntry.
- assertStoredValue(rawContactUri, RawContacts.ACCOUNT_TYPE, accountType1);
- assertStoredValue(rawContactUri, RawContacts.ACCOUNT_NAME, accountName1);
- assertStoredValue(rawContactUri, RawContacts.SEND_TO_VOICEMAIL, "0");
- assertStoredValue(rawContactUri, RawContacts.STARRED, "0");
- assertStoredValue(rawContactUri, RawContacts.PINNED, "0");
- assertStoredValue(dataUri, Data.IS_PRIMARY, 0);
- assertStoredValue(dataUri, Data.IS_SUPER_PRIMARY, 0);
-
- // Update tables without aggregation first, since aggregator will affect pinned value.
- provider.updateFromMetaDataEntry(db, metadataEntry);
-
- // After updating tables from MetadataEntry.
- assertStoredValue(rawContactUri, RawContacts.ACCOUNT_TYPE, accountType1);
- assertStoredValue(rawContactUri, RawContacts.ACCOUNT_NAME, accountName1);
- assertStoredValue(rawContactUri, RawContacts.SEND_TO_VOICEMAIL, "1");
- assertStoredValue(rawContactUri, RawContacts.STARRED, "1");
- assertStoredValue(rawContactUri, RawContacts.PINNED, "1");
- assertStoredValue(dataUri, Data.IS_PRIMARY, 1);
- assertStoredValue(dataUri, Data.IS_SUPER_PRIMARY, 1);
- final Uri dataUriWithUsageType = Data.CONTENT_URI.buildUpon().appendQueryParameter(
- DataUsageFeedback.USAGE_TYPE, usageTypeString).build();
- assertDataUsageCursorContains(dataUriWithUsageType, emailAddress, timesUsed, lastTimeUsed);
-
- // Update AggregationException table.
- RawContactInfo aggregationContact = new RawContactInfo(
- backupId2, accountType2, accountName2, null);
- AggregationData aggregationData = new AggregationData(
- rawContactInfo, aggregationContact, aggregationTypeString);
- aggregationDataList.add(aggregationData);
- metadataEntry = new MetadataEntry(rawContactInfo,
- 1, 1, 1, fieldDataList, aggregationDataList);
- provider.updateFromMetaDataEntry(db, metadataEntry);
-
- // Check if AggregationException table is updated.
- assertStoredValue(AggregationExceptions.CONTENT_URI, AggregationExceptions.RAW_CONTACT_ID1,
- rawContactId);
- assertStoredValue(AggregationExceptions.CONTENT_URI, AggregationExceptions.RAW_CONTACT_ID2,
- rawContactId2);
- assertStoredValue(AggregationExceptions.CONTENT_URI, AggregationExceptions.TYPE,
- aggregationType);
- }
-
public void testPostalsQuery() {
long rawContactId = RawContactUtil.createRawContactWithName(mResolver, "Alice", "Nextore");
Uri dataUri = insertPostalAddress(rawContactId, "1600 Amphiteatre Ave, Mountain View");
diff --git a/tests/src/com/android/providers/contacts/MetadataEntryParserTest.java b/tests/src/com/android/providers/contacts/MetadataEntryParserTest.java
deleted file mode 100644
index be4df57..0000000
--- a/tests/src/com/android/providers/contacts/MetadataEntryParserTest.java
+++ /dev/null
@@ -1,305 +0,0 @@
-/*
- * Copyright (C) 2015 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.providers.contacts;
-
-import android.content.Context;
-import android.test.AndroidTestCase;
-import android.test.suitebuilder.annotation.SmallTest;
-import com.android.providers.contacts.MetadataEntryParser.AggregationData;
-import com.android.providers.contacts.MetadataEntryParser.FieldData;
-import com.android.providers.contacts.MetadataEntryParser.MetadataEntry;
-import com.android.providers.contacts.MetadataEntryParser.RawContactInfo;
-import com.android.providers.contacts.MetadataEntryParser.UsageStats;
-import org.json.JSONException;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-
-/**
- * Unit tests for {@link MetadataEntryParser}.
- *
- * Run the test like this:
- * <code>
- adb shell am instrument -e class com.android.providers.contacts.MetadataEntryParserTest -w \
- com.android.providers.contacts.tests/android.test.InstrumentationTestRunner
- * </code>
- */
-@SmallTest
-public class MetadataEntryParserTest extends AndroidTestCase {
-
- public void testErrorForEmptyInput() {
- try {
- MetadataEntryParser.parseDataToMetaDataEntry("");
- } catch (IllegalArgumentException e) {
- // Expected.
- }
- }
-
- public void testParseDataToMetadataEntry() throws IOException {
- String contactBackupId = "1111111";
- String accountType = "facebook";
- String accountName = "android-test";
- String dataSet = null;
- int sendToVoicemail = 1;
- int starred = 0;
- int pinned = 2;
- String dataHashId1 = "1001";
- String usageType1_1 = "CALL";
- long lastTimeUsed1_1 = 10000001;
- int timesUsed1_1 = 10;
- String usageType1_2 = "SHORT_TEXT";
- long lastTimeUsed1_2 = 20000002;
- int timesUsed1_2 = 20;
- String dataHashId2 = "1002";
- String usageType2 = "LONG_TEXT";
- long lastTimeUsed2 = 30000003;
- int timesUsed2 = 30;
- String aggregationContactBackupId1 = "2222222";
- String aggregationAccountType1 = "com.google";
- String aggregationAccountName1 = "android-test2";
- String aggregationDataSet1 = "plus";
- String aggregationContactBackupId2 = "3333333";
- String aggregationAccountType2 = "com.google";
- String aggregationAccountName2 = "android-test3";
- String aggregationDataSet2 = "custom type";
- String type = "TOGETHER";
- String inputFile = "test1/testFileDeviceContactMetadataJSON.txt";
-
- RawContactInfo rawContactInfo = new RawContactInfo(
- contactBackupId, accountType, accountName, dataSet);
- RawContactInfo aggregationContact1 = new RawContactInfo(aggregationContactBackupId1,
- aggregationAccountType1, aggregationAccountName1, aggregationDataSet1);
- RawContactInfo aggregationContact2 = new RawContactInfo(aggregationContactBackupId2,
- aggregationAccountType2, aggregationAccountName2, aggregationDataSet2);
- AggregationData aggregationData = new AggregationData(
- aggregationContact1, aggregationContact2, type);
- ArrayList<AggregationData> aggregationDataList = new ArrayList<>();
- aggregationDataList.add(aggregationData);
-
- UsageStats usageStats1_1 = new UsageStats(usageType1_1, lastTimeUsed1_1, timesUsed1_1);
- UsageStats usageStats1_2 = new UsageStats(usageType1_2, lastTimeUsed1_2, timesUsed1_2);
- UsageStats usageStats2 = new UsageStats(usageType2, lastTimeUsed2, timesUsed2);
-
- ArrayList<UsageStats> usageStats1List = new ArrayList<>();
- usageStats1List.add(usageStats1_1);
- usageStats1List.add(usageStats1_2);
- FieldData fieldData1 = new FieldData(dataHashId1, true, true, usageStats1List);
-
- ArrayList<UsageStats> usageStats2List = new ArrayList<>();
- usageStats2List.add(usageStats2);
- FieldData fieldData2 = new FieldData(dataHashId2, false, false, usageStats2List);
-
- ArrayList<FieldData> fieldDataList = new ArrayList<>();
- fieldDataList.add(fieldData1);
- fieldDataList.add(fieldData2);
-
- MetadataEntry expectedResult = new MetadataEntry(rawContactInfo,
- sendToVoicemail, starred, pinned, fieldDataList, aggregationDataList);
-
- String inputJson = readAssetAsString(inputFile);
- MetadataEntry metadataEntry = MetadataEntryParser.parseDataToMetaDataEntry(
- inputJson.toString());
- assertMetaDataEntry(expectedResult, metadataEntry);
- }
-
- public void testErrorForMissingContactId() {
- String input = "{\"unique_contact_id\": {\n" +
- " \"account_type\": \"CUSTOM_ACCOUNT\",\n" +
- " \"custom_account_type\": \"facebook\",\n" +
- " \"account_name\": \"android-test\"\n" +
- " }}";
- try {
- MetadataEntryParser.parseDataToMetaDataEntry(input);
- } catch (IllegalArgumentException e) {
- // Expected.
- }
- }
-
- public void testErrorForNullContactId() throws JSONException {
- String input = "{\"unique_contact_id\": {\n" +
- " \"account_type\": \"CUSTOM_ACCOUNT\",\n" +
- " \"custom_account_type\": \"facebook\",\n" +
- " \"account_name\": \"android-test\",\n" +
- " \"contact_id\": \"\"\n" +
- " }}";
- try {
- MetadataEntryParser.parseDataToMetaDataEntry(input);
- } catch (IllegalArgumentException e) {
- // Expected.
- }
- }
-
- public void testErrorForNullAccountType() throws JSONException {
- String input = "{\"unique_contact_id\": {\n" +
- " \"account_type\": \"\",\n" +
- " \"custom_account_type\": \"facebook\",\n" +
- " \"account_name\": \"android-test\",\n" +
- " \"contact_id\": \"\"\n" +
- " }}";
- try {
- MetadataEntryParser.parseDataToMetaDataEntry(input);
- } catch (IllegalArgumentException e) {
- // Expected.
- }
- }
-
- public void testErrorForNullAccountName() throws JSONException {
- String input = "{\"unique_contact_id\": {\n" +
- " \"account_type\": \"CUSTOM_ACCOUNT\",\n" +
- " \"custom_account_type\": \"facebook\",\n" +
- " \"account_name\": \"\",\n" +
- " \"contact_id\": \"1111111\"\n" +
- " }}";
- try {
- MetadataEntryParser.parseDataToMetaDataEntry(input);
- } catch (IllegalArgumentException e) {
- // Expected.
- }
- }
-
- public void testErrorForNullFieldDataId() throws JSONException {
- String input = "{\"unique_contact_id\": {\n" +
- " \"account_type\": \"CUSTOM_ACCOUNT\",\n" +
- " \"custom_account_type\": \"facebook\",\n" +
- " \"account_name\": \"android-test\",\n" +
- " \"contact_id\": \"1111111\"\n" +
- " },\n" +
- " \"contact_prefs\": {\n" +
- " \"send_to_voicemail\": true,\n" +
- " \"starred\": false,\n" +
- " \"pinned\": 2\n" +
- " }," +
- " \"field_data\": [{\n" +
- " \"field_data_id\": \"\"}]" +
- "}";
- try {
- MetadataEntryParser.parseDataToMetaDataEntry(input);
- } catch (IllegalArgumentException e) {
- // Expected.
- }
- }
-
- public void testErrorForNullAggregationType() throws JSONException {
- String input = "{\n" +
- " \"unique_contact_id\": {\n" +
- " \"account_type\": \"CUSTOM_ACCOUNT\",\n" +
- " \"custom_account_type\": \"facebook\",\n" +
- " \"account_name\": \"android-test\",\n" +
- " \"contact_id\": \"1111111\"\n" +
- " },\n" +
- " \"contact_prefs\": {\n" +
- " \"send_to_voicemail\": true,\n" +
- " \"starred\": false,\n" +
- " \"pinned\": 2\n" +
- " },\n" +
- " \"aggregation_data\": [\n" +
- " {\n" +
- " \"type\": \"\",\n" +
- " \"contact_ids\": [\n" +
- " {\n" +
- " \"contact_id\": \"2222222\"\n" +
- " },\n" +
- " {\n" +
- " \"contact_id\": \"3333333\"\n" +
- " }\n" +
- " ]\n" +
- " }\n" +
- " ]}";
- try {
- MetadataEntryParser.parseDataToMetaDataEntry(input);
- } catch (IllegalArgumentException e) {
- // Expected.
- }
- }
-
- private String readAssetAsString(String fileName) throws IOException {
- Context context = getTestContext();
- InputStream input = context.getAssets().open(fileName);
- ByteArrayOutputStream contents = new ByteArrayOutputStream();
- int len;
- byte[] data = new byte[1024];
- do {
- len = input.read(data);
- if (len > 0) contents.write(data, 0, len);
- } while (len == data.length);
- return contents.toString();
- }
-
- private void assertMetaDataEntry(MetadataEntry entry1, MetadataEntry entry2) {
- assertRawContactInfoEquals(entry1.mRawContactInfo, entry2.mRawContactInfo);
- assertEquals(entry1.mSendToVoicemail, entry2.mSendToVoicemail);
- assertEquals(entry1.mStarred, entry2.mStarred);
- assertEquals(entry1.mPinned, entry2.mPinned);
- assertAggregationDataListEquals(entry1.mAggregationDatas, entry2.mAggregationDatas);
- assertFieldDataListEquals(entry1.mFieldDatas, entry2.mFieldDatas);
- }
-
- private void assertRawContactInfoEquals(RawContactInfo contact1, RawContactInfo contact2) {
- assertEquals(contact1.mBackupId, contact2.mBackupId);
- assertEquals(contact1.mAccountType, contact2.mAccountType);
- assertEquals(contact1.mAccountName, contact2.mAccountName);
- assertEquals(contact1.mDataSet, contact2.mDataSet);
- }
-
- private void assertAggregationDataListEquals(ArrayList<AggregationData> aggregationList1,
- ArrayList<AggregationData> aggregationList2) {
- assertEquals(aggregationList1.size(), aggregationList2.size());
- for (int i = 0; i < aggregationList1.size(); i++) {
- assertAggregationDataEquals(aggregationList1.get(i), aggregationList2.get(i));
- }
- }
-
- private void assertAggregationDataEquals(AggregationData aggregationData1,
- AggregationData aggregationData2) {
- assertRawContactInfoEquals(aggregationData1.mRawContactInfo1,
- aggregationData2.mRawContactInfo1);
- assertRawContactInfoEquals(aggregationData1.mRawContactInfo2,
- aggregationData2.mRawContactInfo2);
- assertEquals(aggregationData1.mType, aggregationData2.mType);
- }
-
- private void assertFieldDataListEquals(ArrayList<FieldData> fieldDataList1,
- ArrayList<FieldData> fieldDataList2) {
- assertEquals(fieldDataList1.size(), fieldDataList2.size());
- for (int i = 0; i < fieldDataList1.size(); i++) {
- assertFieldDataEquals(fieldDataList1.get(i), fieldDataList2.get(i));
- }
- }
-
- private void assertFieldDataEquals(FieldData fieldData1, FieldData fieldData2) {
- assertEquals(fieldData1.mDataHashId, fieldData2.mDataHashId);
- assertEquals(fieldData1.mIsPrimary, fieldData2.mIsPrimary);
- assertEquals(fieldData1.mIsSuperPrimary, fieldData2.mIsSuperPrimary);
- assertUsageStatsListEquals(fieldData1.mUsageStatsList, fieldData2.mUsageStatsList);
- }
-
- private void assertUsageStatsListEquals(ArrayList<UsageStats> usageStatsList1,
- ArrayList<UsageStats> usageStatsList2) {
- assertEquals(usageStatsList1.size(), usageStatsList2.size());
- for (int i = 0; i < usageStatsList1.size(); i++) {
- assertUsageStatsEquals(usageStatsList1.get(i), usageStatsList2.get(i));
- }
- }
-
- private void assertUsageStatsEquals(UsageStats usageStats1, UsageStats usageStats2) {
- assertEquals(usageStats1.mUsageType, usageStats2.mUsageType);
- assertEquals(usageStats1.mLastTimeUsed, usageStats2.mLastTimeUsed);
- assertEquals(usageStats1.mTimesUsed, usageStats2.mTimesUsed);
- }
-}