Amanuel Nega
Amanuel Nega

Reputation: 1977

Smart searching contacts in android

Following This Retrieving a List of Contacts Tutorial in the android developers site, I managed to implement contacts search functionality. Here is my code so far

private void retrieveContactRecord(String phoneNo) {
        try {
            Log.e("Info", "Input: " + phoneNo);
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(phoneNo));
            String[] projection = new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME};


            String sortOrder = ContactsContract.PhoneLookup.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
            ContentResolver cr = getContentResolver();
            if (cr != null) {
                Cursor resultCur = cr.query(uri, projection, null, null, sortOrder);
                if (resultCur != null) {
                    while (resultCur.moveToNext()) {
                        String contactId = resultCur.getString(resultCur.getColumnIndex(ContactsContract.PhoneLookup._ID));
                        String contactName = resultCur.getString(resultCur.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
                        Log.e("Info", "Contact Id : " + contactId);
                        Log.e("Info", "Contact Display Name : " + contactName);
                        break;
                    }
                    resultCur.close();
                }
            }
        } catch (Exception sfg) {
            Log.e("Error", "Error in loadContactRecord : " + sfg.toString());
        }
    }

Here is the catch, this code works pretty great, but I need to implement a smart search here. I want 26268 to match Amanu as well as 094 526 2684. I believe it is called T9 dictionary.

I tried looking at other projects for clue, but I couldn't find anything. Any pointers would be appreciated!

Upvotes: 5

Views: 2464

Answers (3)

Amanuel Nega
Amanuel Nega

Reputation: 1977

The ContentProvider for contacts doesn't support it. So what I did was to dump all of the contacts in a List then use a RegEx to match for the name.

public static String[] values = new String[]{" 0", "1", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "PQRS7", "TUV8", "WXYZ9"};

/**
 * Get the possible pattern
 * You'll get something like ["2ABC","4GHI"] for input "14"
 */
public static List<String> possibleValues(String in) {

    if (in.length() >= 1) {
        List<String> p = possibleValues(in.substring(1));
        String s = "" + in.charAt(0);
        if (s.matches("[0-9]")) {
            int n = Integer.parseInt(s);

            p.add(0, values[n]);
        } else {
            // It is a character, use it as it is
            p.add(s);
        }

        return p;
    }
    return new ArrayList<>();
}

.... Then compile the pattern. I used (?i) to make it case insensitive

List<String> values = Utils.possibleValues(query);
StringBuilder sb = new StringBuilder();
for (String value : values) {
    sb.append("[");
    sb.append(value);
    sb.append("]");
    if (values.get(values.size() - 1) != value) {
    sb.append("\\s*");
    }
}

Log.e("Utils", "Pattern = " + sb.toString());

Pattern queryPattern = Pattern.compile("(?i)(" + sb.toString() + ")");

You'll know what to do after this.

Upvotes: 0

Yaroslav
Yaroslav

Reputation: 4820

T9 search can be implemented using trie data structure. You can see an example here - Trie dict. After implementing something similar you will be able to convert your search input into its possible T9 decoded variant and compare if it matches with name.

Upvotes: 3

Ozgur
Ozgur

Reputation: 3766

Dump all contacts to a HashSet

Set<String> contacts = new HashSet<String>();

Then search:

List<List<String>> results = new ArrayList<List<String>>();
// start the search, pass empty stack to represent words found so far
search(input, dictionary, new Stack<String>(), results);

Search method (from @WhiteFang34)

public static void search(String input, Set<String> contacts,
    Stack<String> words, List<List<String>> results) {

    for (int i = 0; i < input.length(); i++) {
        // take the first i characters of the input and see if it is a word
        String substring = input.substring(0, i + 1);

        if (contacts.contains(substring)) {
            // the beginning of the input matches a word, store on stack
            words.push(substring);

            if (i == input.length() - 1) {
                // there's no input left, copy the words stack to results
                results.add(new ArrayList<String>(words));
            } else {
                // there's more input left, search the remaining part
                search(input.substring(i + 1), contacts, words, results);
            }

            // pop the matched word back off so we can move onto the next i
            words.pop();
        }
    }
}

Upvotes: 1

Related Questions