Example usage for android.telephony PhoneNumberUtils convertKeypadLettersToDigits

List of usage examples for android.telephony PhoneNumberUtils convertKeypadLettersToDigits

Introduction

In this page you can find the example usage for android.telephony PhoneNumberUtils convertKeypadLettersToDigits.

Prototype

public static String convertKeypadLettersToDigits(String input) 

Source Link

Document

Translates any alphabetic letters (i.e.

Usage

From source file:Main.java

/**
 * Normalize a phone number by removing the characters other than digits. If
 * the given number has keypad letters, the letters will be converted to
 * digits first.//from  w  w w . j a v a  2  s.  c o m
 *
 * @param phoneNumber The number to be normalized.
 * @return The normalized number.
 *
 * TODO: Remove if PhoneNumberUtils.normalizeNumber(String phoneNumber) is made public.
 */
public static String normalizeNumber(String phoneNumber) {
    StringBuilder sb = new StringBuilder();
    int len = phoneNumber.length();
    for (int i = 0; i < len; i++) {
        char c = phoneNumber.charAt(i);
        // Character.digit() supports ASCII and Unicode digits (fullwidth, Arabic-Indic, etc.)
        int digit = Character.digit(c, 10);
        if (digit != -1) {
            sb.append(digit);
        } else if (i == 0 && c == '+') {
            sb.append(c);
        } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            return normalizeNumber(PhoneNumberUtils.convertKeypadLettersToDigits(phoneNumber));
        }
    }
    return sb.toString();
}

From source file:org.thoughtcrime.securesms.contacts.ContactAccessorNewApi.java

@Override
public Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver) {
    String phone = "";
    String cons = null;//from  w w w  . j ava  2 s.  co  m
    if (constraint != null) {
        cons = constraint.toString();

        if (RecipientsAdapter.usefulAsDigits(cons)) {
            phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons);
            if (phone.equals(cons)) {
                phone = "";
            } else {
                phone = phone.trim();
            }
        }
    }

    Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(cons));
    String selection = String.format("%s=%s OR %s=%s OR %s=%s", Phone.TYPE, Phone.TYPE_MOBILE, Phone.TYPE,
            Phone.TYPE_WORK_MOBILE, Phone.TYPE, Phone.TYPE_MMS);

    Cursor phoneCursor = mContentResolver.query(uri, PROJECTION_PHONE, null, null, SORT_ORDER);

    if (phone.length() > 0) {
        ArrayList result = new ArrayList();
        result.add(Integer.valueOf(-1)); // ID
        result.add(Long.valueOf(-1)); // CONTACT_ID
        result.add(Integer.valueOf(Phone.TYPE_CUSTOM)); // TYPE
        result.add(phone); // NUMBER

        /*
         * The "\u00A0" keeps Phone.getDisplayLabel() from deciding
         * to display the default label ("Home") next to the transformation
         * of the letters into numbers.
         */
        result.add("\u00A0"); // LABEL
        result.add(cons); // NAME

        ArrayList<ArrayList> wrap = new ArrayList<ArrayList>();
        wrap.add(result);

        ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap);

        return new MergeCursor(new Cursor[] { translated, phoneCursor });
    } else {
        return phoneCursor;
    }

}

From source file:org.fdroid.enigtext.contacts.ContactAccessor.java

/***
 * If the code below looks shitty to you, that's because it was taken
 * directly from the Android source, where shitty code is all you get.
 *///from   w w w.  j  a  v a  2s  .co m

public Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver) {
    final String SORT_ORDER = Contacts.TIMES_CONTACTED + " DESC," + Contacts.DISPLAY_NAME + "," + Phone.TYPE;

    final String[] PROJECTION_PHONE = { Phone._ID, // 0
            Phone.CONTACT_ID, // 1
            Phone.TYPE, // 2
            Phone.NUMBER, // 3
            Phone.LABEL, // 4
            Phone.DISPLAY_NAME, // 5
    };

    String phone = "";
    String cons = null;

    if (constraint != null) {
        cons = constraint.toString();

        if (RecipientsAdapter.usefulAsDigits(cons)) {
            phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons);
            if (phone.equals(cons)) {
                phone = "";
            } else {
                phone = phone.trim();
            }
        }
    }

    Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(cons));
    String selection = String.format("%s=%s OR %s=%s OR %s=%s", Phone.TYPE, Phone.TYPE_MOBILE, Phone.TYPE,
            Phone.TYPE_WORK_MOBILE, Phone.TYPE, Phone.TYPE_MMS);

    Cursor phoneCursor = mContentResolver.query(uri, PROJECTION_PHONE, null, null, SORT_ORDER);

    if (phone.length() > 0) {
        ArrayList result = new ArrayList();
        result.add(Integer.valueOf(-1)); // ID
        result.add(Long.valueOf(-1)); // CONTACT_ID
        result.add(Integer.valueOf(Phone.TYPE_CUSTOM)); // TYPE
        result.add(phone); // NUMBER

        /*
        * The "\u00A0" keeps Phone.getDisplayLabel() from deciding
        * to display the default label ("Home") next to the transformation
        * of the letters into numbers.
        */
        result.add("\u00A0"); // LABEL
        result.add(cons); // NAME

        ArrayList<ArrayList> wrap = new ArrayList<ArrayList>();
        wrap.add(result);

        ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap);

        return new MergeCursor(new Cursor[] { translated, phoneCursor });
    } else {
        return phoneCursor;
    }
}

From source file:org.thoughtcrime.securesms.contacts.ContactAccessor.java

/***
 * If the code below looks shitty to you, that's because it was taken
 * directly from the Android source, where shitty code is all you get.
 *//*from   w  ww . ja v a2 s  .c  o  m*/

public Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver) {
    final String SORT_ORDER = Contacts.TIMES_CONTACTED + " DESC," + Contacts.DISPLAY_NAME + "," + Phone.TYPE;

    final String[] PROJECTION_PHONE = { Phone._ID, // 0
            Phone.CONTACT_ID, // 1
            Phone.TYPE, // 2
            Phone.NUMBER, // 3
            Phone.LABEL, // 4
            Phone.DISPLAY_NAME, // 5
    };

    String phone = "";
    String cons = null;

    if (constraint != null) {
        cons = constraint.toString();

        if (RecipientsAdapter.usefulAsDigits(cons)) {
            phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons);
            if (phone.equals(cons) && !PhoneNumberUtils.isWellFormedSmsAddress(phone)) {
                phone = "";
            } else {
                phone = phone.trim();
            }
        }
    }
    Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(cons));
    String selection = String.format("%s=%s OR %s=%s OR %s=%s", Phone.TYPE, Phone.TYPE_MOBILE, Phone.TYPE,
            Phone.TYPE_WORK_MOBILE, Phone.TYPE, Phone.TYPE_MMS);

    Cursor phoneCursor = mContentResolver.query(uri, PROJECTION_PHONE, null, null, SORT_ORDER);

    if (phone.length() > 0) {
        ArrayList result = new ArrayList();
        result.add(Integer.valueOf(-1)); // ID
        result.add(Long.valueOf(-1)); // CONTACT_ID
        result.add(Integer.valueOf(Phone.TYPE_CUSTOM)); // TYPE
        result.add(phone); // NUMBER

        /*
        * The "\u00A0" keeps Phone.getDisplayLabel() from deciding
        * to display the default label ("Home") next to the transformation
        * of the letters into numbers.
        */
        result.add("\u00A0"); // LABEL
        result.add(cons); // NAME

        ArrayList<ArrayList> wrap = new ArrayList<ArrayList>();
        wrap.add(result);

        ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap);

        return new MergeCursor(new Cursor[] { translated, phoneCursor });
    } else {
        return phoneCursor;
    }
}

From source file:com.securecomcode.text.contacts.ContactAccessor.java

/***
 * If the code below looks shitty to you, that's because it was taken
 * directly from the Android source, where shitty code is all you get.
 *///from  ww  w  .ja v  a  2 s .com

public Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver) {
    final String SORT_ORDER = Contacts.TIMES_CONTACTED + " DESC," + Contacts.DISPLAY_NAME + ","
            + Contacts.Data.IS_SUPER_PRIMARY + " DESC," + Phone.TYPE;

    final String[] PROJECTION_PHONE = { Phone._ID, // 0
            Phone.CONTACT_ID, // 1
            Phone.TYPE, // 2
            Phone.NUMBER, // 3
            Phone.LABEL, // 4
            Phone.DISPLAY_NAME, // 5
    };

    String phone = "";
    String cons = null;

    if (constraint != null) {
        cons = constraint.toString();

        if (RecipientsAdapter.usefulAsDigits(cons)) {
            phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons);
            if (phone.equals(cons) && !PhoneNumberUtils.isWellFormedSmsAddress(phone)) {
                phone = "";
            } else {
                phone = phone.trim();
            }
        }
    }
    Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(cons));
    String selection = String.format("%s=%s OR %s=%s OR %s=%s", Phone.TYPE, Phone.TYPE_MOBILE, Phone.TYPE,
            Phone.TYPE_WORK_MOBILE, Phone.TYPE, Phone.TYPE_MMS);

    Cursor phoneCursor = mContentResolver.query(uri, PROJECTION_PHONE, null, null, SORT_ORDER);

    if (phone.length() > 0) {
        ArrayList result = new ArrayList();
        result.add(Integer.valueOf(-1)); // ID
        result.add(Long.valueOf(-1)); // CONTACT_ID
        result.add(Integer.valueOf(Phone.TYPE_CUSTOM)); // TYPE
        result.add(phone); // NUMBER

        /*
        * The "\u00A0" keeps Phone.getDisplayLabel() from deciding
        * to display the default label ("Home") next to the transformation
        * of the letters into numbers.
        */
        result.add("\u00A0"); // LABEL
        result.add(cons); // NAME

        ArrayList<ArrayList> wrap = new ArrayList<ArrayList>();
        wrap.add(result);

        ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap);

        return new MergeCursor(new Cursor[] { translated, phoneCursor });
    } else {
        return phoneCursor;
    }
}