From 44a175732dc4b872515f978b986ef7b357fe2f00 Mon Sep 17 00:00:00 2001 From: Tom Ouyang Date: Mon, 13 Apr 2015 17:49:17 -0700 Subject: Add affinity model for contact names. This allows us to: 1. Rank contacts and only add the top N names to the keyboard LM. 2. Avoid adding duplicate names. Note: The affinity calcualuation is limited by the fact that some apps currently do not update the TIMES_CONTACTED counter. To better handle this case, the new measure also takes into account whether or not a name is in the visible contacts group. Bug: 20053274 Change-Id: I2741cb8958667d4a294aba8c437a45cec4b42dc7 --- .../inputmethod/latin/ContactsManagerTest.java | 60 ++++++++++++++++++---- 1 file changed, 50 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/src/com/android/inputmethod/latin/ContactsManagerTest.java b/tests/src/com/android/inputmethod/latin/ContactsManagerTest.java index 6326b3b0f..f987e0c05 100644 --- a/tests/src/com/android/inputmethod/latin/ContactsManagerTest.java +++ b/tests/src/com/android/inputmethod/latin/ContactsManagerTest.java @@ -29,11 +29,15 @@ import android.test.mock.MockContentProvider; import android.test.mock.MockContentResolver; import android.test.suitebuilder.annotation.SmallTest; +import com.android.inputmethod.latin.ContactsDictionaryConstants; +import com.android.inputmethod.latin.ContactsManager; + import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.HashMap; +import java.util.concurrent.TimeUnit; /** * Tests for {@link ContactsManager} @@ -63,12 +67,13 @@ public class ContactsManagerTest extends AndroidTestCase { @Test public void testGetValidNames() { - final String contactName1 = "firstname lastname"; + final String contactName1 = "firstname last-name"; final String contactName2 = "larry"; - mMatrixCursor.addRow(new Object[] { 1, contactName1 }); - mMatrixCursor.addRow(new Object[] { 2, null /* null name */ }); - mMatrixCursor.addRow(new Object[] { 3, contactName2 }); - mMatrixCursor.addRow(new Object[] { 4, "floopy@example.com" /* invalid name */ }); + mMatrixCursor.addRow(new Object[] { 1, contactName1, 0, 0, 0 }); + mMatrixCursor.addRow(new Object[] { 2, null /* null name */, 0, 0, 0 }); + mMatrixCursor.addRow(new Object[] { 3, contactName2, 0, 0, 0 }); + mMatrixCursor.addRow(new Object[] { 4, "floopy@example.com" /* invalid name */, 0, 0, 0 }); + mMatrixCursor.addRow(new Object[] { 5, "news-group" /* invalid name */, 0, 0, 0 }); mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor); final ArrayList validNames = mManager.getValidNames(Contacts.CONTENT_URI); @@ -78,13 +83,48 @@ public class ContactsManagerTest extends AndroidTestCase { } @Test - public void testGetCount() { - mMatrixCursor.addRow(new Object[] { 1, "firstname" }); - mMatrixCursor.addRow(new Object[] { 2, null /* null name */ }); - mMatrixCursor.addRow(new Object[] { 3, "larry" }); - mMatrixCursor.addRow(new Object[] { 4, "floopy@example.com" /* invalid name */ }); + public void testGetValidNamesAffinity() { + final long now = System.currentTimeMillis(); + final long month_ago = now - TimeUnit.MILLISECONDS.convert(31, TimeUnit.DAYS); + for (int i = 0; i < ContactsManager.MAX_CONTACT_NAMES + 10; ++i) { + mMatrixCursor.addRow(new Object[] { i, "name" + i, i, now, 1 }); + } + mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor); + + final ArrayList validNames = mManager.getValidNames(Contacts.CONTENT_URI); + assertEquals(ContactsManager.MAX_CONTACT_NAMES, validNames.size()); + for (int i = 0; i < 10; ++i) { + assertFalse(validNames.contains("name" + i)); + } + for (int i = 10; i < ContactsManager.MAX_CONTACT_NAMES + 10; ++i) { + assertTrue(validNames.contains("name" + i)); + } + } + + @Test + public void testComputeAffinity() { + final long now = System.currentTimeMillis(); + final long month_ago = now - TimeUnit.MILLISECONDS.convert(31, TimeUnit.DAYS); + mMatrixCursor.addRow(new Object[] { 1, "name", 1, month_ago, 1 }); mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor); + Cursor cursor = mFakeContactsContentProvider.query(Contacts.CONTENT_URI, + ContactsDictionaryConstants.PROJECTION_ID_ONLY, null, null, null); + cursor.moveToFirst(); + ContactsManager.RankedContact contact = new ContactsManager.RankedContact(cursor); + contact.computeAffinity(1, month_ago); + assertEquals(contact.getAffinity(), 1.0f); + contact.computeAffinity(2, now); + assertEquals(contact.getAffinity(), (2.0f/3.0f + (float)Math.pow(0.5, 3) + 1.0f) / 3); + } + + @Test + public void testGetCount() { + mMatrixCursor.addRow(new Object[] { 1, "firstname", 0, 0, 0 }); + mMatrixCursor.addRow(new Object[] { 2, null /* null name */, 0, 0, 0 }); + mMatrixCursor.addRow(new Object[] { 3, "larry", 0, 0, 0 }); + mMatrixCursor.addRow(new Object[] { 4, "floopy@example.com" /* invalid name */, 0, 0, 0 }); + mFakeContactsContentProvider.addQueryResult(Contacts.CONTENT_URI, mMatrixCursor); assertEquals(4, mManager.getContactCount()); } -- cgit v1.2.3