Skip to content

Commit

Permalink
Add contact phone highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
saveman71 committed May 20, 2018
1 parent 8c1156e commit c13adc4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import android.database.ContentObserver;
import android.provider.ContactsContract;
import android.util.Pair;

import java.util.List;

import fr.neamar.kiss.forwarder.Permission;
import fr.neamar.kiss.loader.LoadContactsPojos;
Expand Down Expand Up @@ -74,7 +71,7 @@ public void requestResults(String query, Searcher searcher) {

if (!match && queryNormalized.length() > 2) {
// search for the phone number
matchInfo = fuzzyScore.match(pojo.phoneSimplified);
matchInfo = fuzzyScore.match(pojo.normalizedPhone.codePoints);
match = matchInfo.match;
pojo.relevance = matchInfo.score;
}
Expand All @@ -99,10 +96,10 @@ public void requestResults(String query, Searcher searcher) {
* @return a contactpojo, or null.
*/
public ContactsPojo findByPhone(String phoneNumber) {
String simplifiedPhoneNumber = PhoneNormalizer.simplifyPhoneNumber(phoneNumber);
StringNormalizer.Result simplifiedPhoneNumber = PhoneNormalizer.simplifyPhoneNumber(phoneNumber);

for (ContactsPojo pojo : pojos) {
if (pojo.phoneSimplified.equals(simplifiedPhoneNumber)) {
if (pojo.normalizedPhone.equals(simplifiedPhoneNumber)) {
return pojo;
}
}
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/fr/neamar/kiss/loader/LoadContactsPojos.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import fr.neamar.kiss.forwarder.Permission;
import fr.neamar.kiss.normalizer.PhoneNormalizer;
Expand Down Expand Up @@ -50,10 +52,8 @@ protected ArrayList<ContactsPojo> doInBackground(Void... params) {
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID}, null, null, ContactsContract.CommonDataKinds.Phone.TIMES_CONTACTED + " DESC");

// Prevent duplicates by keeping in memory encountered phones.
// The string key is "phone" + "|" + "name" (so if two contacts
// with distinct name share same number, they both get displayed)
Map<String, ArrayList<ContactsPojo>> mapContacts = new HashMap<>();
// Prevent duplicates by keeping in memory encountered contacts.
Map<String, Set<ContactsPojo>> mapContacts = new HashMap<>();

if (cur != null) {
if (cur.getCount() > 0) {
Expand All @@ -76,7 +76,7 @@ protected ArrayList<ContactsPojo> doInBackground(Void... params) {
if (contact.phone == null) {
contact.phone = "";
}
contact.phoneSimplified = PhoneNormalizer.simplifyPhoneNumber(contact.phone);
contact.normalizedPhone = PhoneNormalizer.simplifyPhoneNumber(contact.phone);
contact.starred = cur.getInt(starredIndex) != 0;
contact.primary = cur.getInt(isPrimaryIndex) != 0;
String photoId = cur.getString(photoIdIndex);
Expand All @@ -94,7 +94,7 @@ protected ArrayList<ContactsPojo> doInBackground(Void... params) {
if (mapContacts.containsKey(contact.lookupKey))
mapContacts.get(contact.lookupKey).add(contact);
else {
ArrayList<ContactsPojo> phones = new ArrayList<>();
Set<ContactsPojo> phones = new HashSet<>();
phones.add(contact);
mapContacts.put(contact.lookupKey, phones);
}
Expand Down Expand Up @@ -132,7 +132,7 @@ protected ArrayList<ContactsPojo> doInBackground(Void... params) {
nickCursor.close();
}

for (List<ContactsPojo> phones : mapContacts.values()) {
for (Set<ContactsPojo> phones : mapContacts.values()) {
// Find primary phone and add this one.
Boolean hasPrimary = false;
for (ContactsPojo contact : phones) {
Expand All @@ -147,8 +147,8 @@ protected ArrayList<ContactsPojo> doInBackground(Void... params) {
if (!hasPrimary) {
HashSet<String> added = new HashSet<>(phones.size());
for (ContactsPojo contact : phones) {
if (!added.contains(contact.phoneSimplified)) {
added.add(contact.phoneSimplified);
if (!added.contains(contact.normalizedPhone.toString())) {
added.add(contact.normalizedPhone.toString());
contacts.add(contact);
}
}
Expand Down
26 changes: 16 additions & 10 deletions app/src/main/java/fr/neamar/kiss/normalizer/PhoneNormalizer.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package fr.neamar.kiss.normalizer;

public class PhoneNormalizer {
public static String simplifyPhoneNumber(String phoneNumber) {
public static StringNormalizer.Result simplifyPhoneNumber(String phoneNumber) {
// This is done manually for performance reason,
// But the algorithm is just a regexp replacement of "[-.():/ ]" with ""
StringBuilder sb = new StringBuilder();
int phoneLength = phoneNumber.length();
for(int i = 0; i < phoneLength; i++)
{
char c = phoneNumber.charAt(i);
if(c == ' ' || c == '-' || c == '.' || c == '(' || c == ')' || c == ':' || c == '/') {
continue;

int numCodePoints = Character.codePointCount(phoneNumber, 0, phoneNumber.length());
IntSequenceBuilder codePoints = new IntSequenceBuilder(numCodePoints);
IntSequenceBuilder resultMap = new IntSequenceBuilder(numCodePoints);

int i = 0;
for (int iterCodePoint = 0; iterCodePoint < numCodePoints; iterCodePoint += 1) {
int c = Character.codePointAt(phoneNumber, i);

if (c != ' ' && c != '-' && c != '.' && c != '(' && c != ')' && c != ':' && c != '/') {
codePoints.add(c);
resultMap.add(i);
}
sb.append(c);
i += Character.charCount(c);
}
return sb.toString();

return new StringNormalizer.Result(phoneNumber.length(), codePoints.toArray(), resultMap.toArray());
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/fr/neamar/kiss/pojo/ContactsPojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ContactsPojo extends Pojo {

public String phone = "";
//phone without special characters
public String phoneSimplified = "";
public StringNormalizer.Result normalizedPhone = null;
public Uri icon = null;

// Is this a primary phone?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public View display(Context context, int position, View convertView, FuzzyScore

// Contact phone
TextView contactPhone = view.findViewById(R.id.item_contact_phone);
contactPhone.setText(contactPojo.phone);
displayHighlighted(contactPojo.normalizedPhone, contactPojo.phone, fuzzyScore, contactPhone, context);

// Contact nickname
TextView contactNickname = view.findViewById(R.id.item_contact_nickname);
Expand Down

0 comments on commit c13adc4

Please sign in to comment.