summaryrefslogtreecommitdiffstats
path: root/src/com/android/providers/contacts/aggregation/util
diff options
context:
space:
mode:
authorZheng Fu <zhengfu@google.com>2015-04-15 15:39:16 -0700
committerZheng Fu <zhengfu@google.com>2015-04-15 17:12:45 -0700
commitaa18c233fdec3359c5231d4a5f61188446bf5d6f (patch)
tree48a60f768e641cc1922de5c2b30b1da6f8d14ef2 /src/com/android/providers/contacts/aggregation/util
parentb94bfa502569ce869d443353c174d02754d42a82 (diff)
downloadpackages_providers_ContactsProvider-aa18c233fdec3359c5231d4a5f61188446bf5d6f.zip
packages_providers_ContactsProvider-aa18c233fdec3359c5231d4a5f61188446bf5d6f.tar.gz
packages_providers_ContactsProvider-aa18c233fdec3359c5231d4a5f61188446bf5d6f.tar.bz2
use new setting flag to switch between current and new aggregator.
In order for the switch, add AbstractContactAggregator for ContactAggregator and ContactAggregator2. Bug:20055573 Change-Id: I6f27d4df8017938b226f5c6371b15ba41fd18acd
Diffstat (limited to 'src/com/android/providers/contacts/aggregation/util')
-rw-r--r--src/com/android/providers/contacts/aggregation/util/ContactMatcher.java114
-rw-r--r--src/com/android/providers/contacts/aggregation/util/MatchScore.java149
-rw-r--r--src/com/android/providers/contacts/aggregation/util/RawContactMatcher.java99
-rw-r--r--src/com/android/providers/contacts/aggregation/util/RawContactMatchingCandidates.java8
4 files changed, 167 insertions, 203 deletions
diff --git a/src/com/android/providers/contacts/aggregation/util/ContactMatcher.java b/src/com/android/providers/contacts/aggregation/util/ContactMatcher.java
index 2e552e9..9b71651 100644
--- a/src/com/android/providers/contacts/aggregation/util/ContactMatcher.java
+++ b/src/com/android/providers/contacts/aggregation/util/ContactMatcher.java
@@ -31,9 +31,6 @@ import java.util.List;
public class ContactMatcher {
private static final String TAG = "ContactMatcher";
- // Best possible match score
- public static final int MAX_SCORE = 100;
-
// Suggest to aggregate contacts if their match score is equal or greater than this threshold
public static final int SCORE_THRESHOLD_SUGGEST = 50;
@@ -59,9 +56,6 @@ public class ContactMatcher {
// Maximum number of characters in a name to be considered by the matching algorithm.
private static final int MAX_MATCHED_NAME_LENGTH = 30;
- // Scores a multiplied by this number to allow room for "fractional" scores
- private static final int SCORE_SCALE = 1000;
-
public static final int MATCHING_ALGORITHM_EXACT = 0;
public static final int MATCHING_ALGORITHM_CONSERVATIVE = 1;
public static final int MATCHING_ALGORITHM_APPROXIMATE = 2;
@@ -159,88 +153,6 @@ public class ContactMatcher {
return sMaxScore[index];
}
- /**
- * Captures the max score and match count for a specific contact. Used in an
- * contactId - MatchScore map.
- */
- public static class MatchScore implements Comparable<MatchScore> {
- private long mContactId;
- private boolean mKeepIn;
- private boolean mKeepOut;
- private int mPrimaryScore;
- private int mSecondaryScore;
- private int mMatchCount;
-
- public MatchScore(long contactId) {
- this.mContactId = contactId;
- }
-
- public void reset(long contactId) {
- this.mContactId = contactId;
- mKeepIn = false;
- mKeepOut = false;
- mPrimaryScore = 0;
- mSecondaryScore = 0;
- mMatchCount = 0;
- }
-
- public long getContactId() {
- return mContactId;
- }
-
- public void updatePrimaryScore(int score) {
- if (score > mPrimaryScore) {
- mPrimaryScore = score;
- }
- mMatchCount++;
- }
-
- public void updateSecondaryScore(int score) {
- if (score > mSecondaryScore) {
- mSecondaryScore = score;
- }
- mMatchCount++;
- }
-
- public void keepIn() {
- mKeepIn = true;
- }
-
- public void keepOut() {
- mKeepOut = true;
- }
-
- public int getScore() {
- if (mKeepOut) {
- return 0;
- }
-
- if (mKeepIn) {
- return MAX_SCORE;
- }
-
- int score = (mPrimaryScore > mSecondaryScore ? mPrimaryScore : mSecondaryScore);
-
- // Ensure that of two contacts with the same match score the one with more matching
- // data elements wins.
- return score * SCORE_SCALE + mMatchCount;
- }
-
- /**
- * Descending order of match score.
- */
- @Override
- public int compareTo(MatchScore another) {
- return another.getScore() - getScore();
- }
-
- @Override
- public String toString() {
- return mContactId + ": " + mPrimaryScore + "/" + mSecondaryScore + "(" + mMatchCount
- + ")";
- }
- }
-
private final HashMap<Long, MatchScore> mScores = new HashMap<Long, MatchScore>();
private final ArrayList<MatchScore> mScoreList = new ArrayList<MatchScore>();
private int mScoreCount = 0;
@@ -268,7 +180,7 @@ public class ContactMatcher {
* Marks the contact as a full match, because we found an Identity match
*/
public void matchIdentity(long contactId) {
- updatePrimaryScore(contactId, MAX_SCORE);
+ updatePrimaryScore(contactId, MatchScore.MAX_SCORE);
}
/**
@@ -374,18 +286,18 @@ public class ContactMatcher {
for (int i = 0; i < mScoreCount; i++) {
MatchScore score = mScoreList.get(i);
- if (score.mKeepOut) {
+ if (score.isKeepOut()) {
continue;
}
- int s = score.mSecondaryScore;
+ int s = score.getSecondaryScore();
if (s >= threshold) {
if (contactIds == null) {
contactIds = new ArrayList<Long>();
}
- contactIds.add(score.mContactId);
+ contactIds.add(score.getContactId());
}
- score.mPrimaryScore = NO_DATA_SCORE;
+ score.setPrimaryScore(NO_DATA_SCORE);
}
return contactIds;
}
@@ -401,17 +313,17 @@ public class ContactMatcher {
int maxScore = 0;
for (int i = 0; i < mScoreCount; i++) {
MatchScore score = mScoreList.get(i);
- if (score.mKeepOut) {
+ if (score.isKeepOut()) {
continue;
}
- if (score.mKeepIn) {
- return score.mContactId;
+ if (score.isKeepIn()) {
+ return score.getContactId();
}
- int s = score.mPrimaryScore;
+ int s = score.getPrimaryScore();
if (s == NO_DATA_SCORE) {
- s = score.mSecondaryScore;
+ s = score.getSecondaryScore();
}
if (s >= threshold) {
@@ -420,8 +332,8 @@ public class ContactMatcher {
}
// In order to make it stable, let's jut pick the one with the lowest ID
// if multiple candidates are found.
- if ((s > maxScore) || ((s == maxScore) && (contactId > score.mContactId))) {
- contactId = score.mContactId;
+ if ((s > maxScore) || ((s == maxScore) && (contactId > score.getContactId()))) {
+ contactId = score.getContactId();
maxScore = s;
}
}
@@ -433,7 +345,7 @@ public class ContactMatcher {
* Returns matches in the order of descending score.
*/
public List<MatchScore> pickBestMatches(int threshold) {
- int scaledThreshold = threshold * SCORE_SCALE;
+ int scaledThreshold = threshold * MatchScore.SCORE_SCALE;
List<MatchScore> matches = mScoreList.subList(0, mScoreCount);
Collections.sort(matches);
int count = 0;
diff --git a/src/com/android/providers/contacts/aggregation/util/MatchScore.java b/src/com/android/providers/contacts/aggregation/util/MatchScore.java
new file mode 100644
index 0000000..f87731c
--- /dev/null
+++ b/src/com/android/providers/contacts/aggregation/util/MatchScore.java
@@ -0,0 +1,149 @@
+/*
+ * 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.aggregation.util;
+
+/**
+ * Captures the max score and match count for a specific raw contact or contact.
+ */
+public class MatchScore implements Comparable<MatchScore> {
+ // Scores a multiplied by this number to allow room for "fractional" scores
+ public static final int SCORE_SCALE = 1000;
+ // Best possible match score
+ public static final int MAX_SCORE = 100;
+
+ private long mRawContactId;
+ private long mContactId;
+ private long mAccountId;
+
+ private boolean mKeepIn;
+ private boolean mKeepOut;
+
+ private int mPrimaryScore;
+ private int mSecondaryScore;
+ private int mMatchCount;
+
+ public MatchScore(long rawContactId, long contactId, long accountId) {
+ this.mRawContactId = rawContactId;
+ this.mContactId = contactId;
+ this.mAccountId = accountId;
+ }
+
+ public MatchScore(long contactId) {
+ this.mRawContactId = 0;
+ this.mContactId = contactId;
+ this.mAccountId = 0;
+ }
+
+ public void reset(long rawContactId, long contactId, long accountId) {
+ this.mRawContactId = rawContactId;
+ this.mContactId = contactId;
+ this.mAccountId = accountId;
+ mKeepIn = false;
+ mKeepOut = false;
+ mPrimaryScore = 0;
+ mSecondaryScore = 0;
+ mMatchCount = 0;
+ }
+
+ public void reset(long contactId) {
+ this.reset(0l, contactId, 0l);
+ }
+
+
+ public long getRawContactId() {
+ return mRawContactId;
+ }
+
+ public long getContactId() {
+ return mContactId;
+ }
+
+ public long getAccountId() {
+ return mAccountId;
+ }
+
+ public void updatePrimaryScore(int score) {
+ if (score > mPrimaryScore) {
+ mPrimaryScore = score;
+ }
+ mMatchCount++;
+ }
+
+ public void updateSecondaryScore(int score) {
+ if (score > mSecondaryScore) {
+ mSecondaryScore = score;
+ }
+ mMatchCount++;
+ }
+
+ public void keepIn() {
+ mKeepIn = true;
+ }
+
+ public void keepOut() {
+ mKeepOut = true;
+ }
+
+ public int getScore() {
+ if (mKeepOut) {
+ return 0;
+ }
+
+ if (mKeepIn) {
+ return MAX_SCORE;
+ }
+
+ int score = (mPrimaryScore > mSecondaryScore ? mPrimaryScore : mSecondaryScore);
+
+ // Ensure that of two contacts with the same match score the one with more matching
+ // data elements wins.
+ return score * SCORE_SCALE + mMatchCount;
+ }
+
+ public boolean isKeepIn() {
+ return mKeepIn;
+ }
+
+ public boolean isKeepOut() {
+ return mKeepOut;
+ }
+
+ public int getPrimaryScore() {
+ return mPrimaryScore;
+ }
+
+ public int getSecondaryScore() {
+ return mSecondaryScore;
+ }
+
+ public void setPrimaryScore(int mPrimaryScore) {
+ this.mPrimaryScore = mPrimaryScore;
+ }
+
+ /**
+ * Descending order of match score.
+ */
+ @Override
+ public int compareTo(MatchScore another) {
+ return another.getScore() - getScore();
+ }
+
+ @Override
+ public String toString() {
+ return mRawContactId + "/" + mContactId + "/" + mAccountId + ": " + mPrimaryScore +
+ "/" + mSecondaryScore + "(" + mMatchCount + ")";
+ }
+}
diff --git a/src/com/android/providers/contacts/aggregation/util/RawContactMatcher.java b/src/com/android/providers/contacts/aggregation/util/RawContactMatcher.java
index 3b0150c..1abcfa1 100644
--- a/src/com/android/providers/contacts/aggregation/util/RawContactMatcher.java
+++ b/src/com/android/providers/contacts/aggregation/util/RawContactMatcher.java
@@ -15,11 +15,10 @@
*/
package com.android.providers.contacts.aggregation.util;
+import android.util.Log;
import com.android.providers.contacts.ContactsDatabaseHelper.NameLookupType;
import com.android.providers.contacts.util.Hex;
-import android.util.Log;
-
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -160,102 +159,6 @@ public class RawContactMatcher {
return sMaxScore[index];
}
- /**
- * Captures the max score and match count for a specific raw contact. Used in an
- * rawContactId - MatchScore map.
- */
- public static class MatchScore implements Comparable<MatchScore> {
- private long mRawContactId;
- private long mContactId;
- private long mAccountId;
- private boolean mKeepIn;
- private boolean mKeepOut;
- private int mPrimaryScore;
- private int mSecondaryScore;
- private int mMatchCount;
-
- public MatchScore(long rawContactId, long contactId, long accountId) {
- this.mRawContactId = rawContactId;
- this.mContactId = contactId;
- this.mAccountId = accountId;
- }
-
- public void reset(long rawContactId, long contactId, long accountId) {
- this.mRawContactId = rawContactId;
- this.mContactId = contactId;
- this.mAccountId = accountId;
- mKeepIn = false;
- mKeepOut = false;
- mPrimaryScore = 0;
- mSecondaryScore = 0;
- mMatchCount = 0;
- }
-
- public long getRawContactId() {
- return mRawContactId;
- }
-
- public long getContactId() {
- return mContactId;
- }
-
- public long getAccountId() {
- return mAccountId;
- }
-
- public void updatePrimaryScore(int score) {
- if (score > mPrimaryScore) {
- mPrimaryScore = score;
- }
- mMatchCount++;
- }
-
- public void updateSecondaryScore(int score) {
- if (score > mSecondaryScore) {
- mSecondaryScore = score;
- }
- mMatchCount++;
- }
-
- public void keepIn() {
- mKeepIn = true;
- }
-
- public void keepOut() {
- mKeepOut = true;
- }
-
- public int getScore() {
- if (mKeepOut) {
- return 0;
- }
-
- if (mKeepIn) {
- return MAX_SCORE;
- }
-
- int score = (mPrimaryScore > mSecondaryScore ? mPrimaryScore : mSecondaryScore);
-
- // Ensure that of two contacts with the same match score the one with more matching
- // data elements wins.
- return score * SCORE_SCALE + mMatchCount;
- }
-
- /**
- * Descending order of match score.
- */
- @Override
- public int compareTo(MatchScore another) {
- return another.getScore() - getScore();
- }
-
- @Override
- public String toString() {
- return mRawContactId + "/" + mContactId + "/" + mAccountId + ": " + mPrimaryScore +
- "/" + mSecondaryScore + "(" + mMatchCount + ")";
- }
- }
-
private final HashMap<Long, MatchScore> mScores = new HashMap<Long, MatchScore>();
private final ArrayList<MatchScore> mScoreList = new ArrayList<MatchScore>();
private int mScoreCount = 0;
diff --git a/src/com/android/providers/contacts/aggregation/util/RawContactMatchingCandidates.java b/src/com/android/providers/contacts/aggregation/util/RawContactMatchingCandidates.java
index 39125b4..917c810 100644
--- a/src/com/android/providers/contacts/aggregation/util/RawContactMatchingCandidates.java
+++ b/src/com/android/providers/contacts/aggregation/util/RawContactMatchingCandidates.java
@@ -29,25 +29,25 @@ import static com.android.internal.util.Preconditions.checkNotNull;
* Matching candidates for a raw contact, used in the contact aggregator.
*/
public class RawContactMatchingCandidates {
- private List<RawContactMatcher.MatchScore> mBestMatches;
+ private List<MatchScore> mBestMatches;
private Set<Long> mRawContactIds = null;
private Map<Long, Long> mRawContactToContact = null;
private Map<Long, Long> mRawContactToAccount = null;
- public RawContactMatchingCandidates(List<RawContactMatcher.MatchScore> mBestMatches) {
+ public RawContactMatchingCandidates(List<MatchScore> mBestMatches) {
checkNotNull(mBestMatches);
this.mBestMatches = mBestMatches;
}
public RawContactMatchingCandidates() {
- mBestMatches = new ArrayList<RawContactMatcher.MatchScore>();
+ mBestMatches = new ArrayList<MatchScore>();
}
public int getCount() {
return mBestMatches.size();
}
- public void add(RawContactMatcher.MatchScore score) {
+ public void add(MatchScore score) {
mBestMatches.add(score);
if (mRawContactIds != null) {
mRawContactIds.add(score.getRawContactId());