Skip to content

Commit

Permalink
Add some custom keypad letter mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
naveensingh committed Jan 13, 2025
1 parent 30b338e commit 4f91781
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ fun String.formatPhoneNumber(minimumLength: Int = 4): String {

fun String.highlightTextFromNumbers(textToHighlight: String, primaryColor: Int): SpannableString {
val spannableString = SpannableString(this)
val digits = PhoneNumberUtils.convertKeypadLettersToDigits(this)
val digits = KeypadHelper.convertKeypadLettersToDigits(this)
if (digits.contains(textToHighlight)) {
val startIndex = digits.indexOf(textToHighlight, 0, true)
val endIndex = Math.min(startIndex + textToHighlight.length, length)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.fossify.commons.helpers

import android.telephony.PhoneNumberUtils

/**
* Mostly depends on [android.telephony.PhoneNumberUtils].
*/
object KeypadHelper {
// this should probably be split into language-specific mappings
private val KEYPAD_MAP = mutableMapOf<Char, Int>().apply {
put('ı', 4)
put('İ', 4)

put('ł', 5)
put('Ł', 5)
}

/**
* Translates any alphabetic letters (i.e. [A-Za-z]) in the
* specified phone number into the equivalent numeric digits,
* according to the phone keypad letter mapping described in
* ITU E.161 and ISO/IEC 9995-8.
*
* @return the input string, with alpha letters converted to numeric
* digits using the phone keypad letter mapping. For example,
* an input of "1-800-FOSS-411" will return "1-800-3667-411".
*/
fun convertKeypadLettersToDigits(input: String): String {
val digits = PhoneNumberUtils.convertKeypadLettersToDigits(input)
val result = StringBuilder(digits.length)
for (c in digits) {
val digit = KEYPAD_MAP[c] ?: c
result.append(digit)
}

return result.toString()
}
}

0 comments on commit 4f91781

Please sign in to comment.