mirror of
https://github.com/ntninja/kiss-launcher.git
synced 2026-03-13 09:50:25 +08:00
Cleanup
* switched to production (use our phone number normalization as backup only) * removed libphonenumber submodule & added hint to generate-mobile-phone-regex.py * pattern for ignored characters in phonenumbers is compiled once and reused.
This commit is contained in:
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +0,0 @@
|
||||
[submodule "utils/libphonenumber"]
|
||||
path = utils/libphonenumber
|
||||
url = https://github.com/googlei18n/libphonenumber.git
|
||||
|
||||
@@ -24,6 +24,7 @@ import fr.neamar.kiss.pojo.ContactsPojo;
|
||||
|
||||
public class LoadContactsPojos extends LoadPojos<ContactsPojo> {
|
||||
private static Pattern mobileNumberPattern;
|
||||
private static Pattern ignorePattern = Pattern.compile("[-.():/ ]");
|
||||
|
||||
public LoadContactsPojos(Context context) {
|
||||
super(context, "contact://");
|
||||
@@ -90,7 +91,7 @@ public class LoadContactsPojos extends LoadPojos<ContactsPojo> {
|
||||
if (contact.phone == null) {
|
||||
contact.phone = "";
|
||||
}
|
||||
contact.phoneSimplified = contact.phone.replaceAll("[-.():/ ]", "");
|
||||
contact.phoneSimplified = ignorePattern.matcher(contact.phone).replaceAll("");
|
||||
contact.homeNumber = mobileNumberPattern == null ||
|
||||
!mobileNumberPattern.matcher(contact.phoneSimplified).lookingAt();
|
||||
contact.starred = cur.getInt(cur
|
||||
|
||||
@@ -11,21 +11,18 @@ import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import fr.neamar.kiss.R;
|
||||
|
||||
public class PhoneNormalizer {
|
||||
static PhoneNormalizer instance;
|
||||
private final Map<String, PrefixEntry> prefix_data;
|
||||
private final PrefixEntry entry;
|
||||
private final String countryIso;
|
||||
|
||||
private PhoneNormalizer(Context context) {
|
||||
countryIso = determineCountryIso(context);
|
||||
// Do we really need all of the prefix data? we're just using one entry (for countryIso)..
|
||||
prefix_data = loadPrefixData(context);
|
||||
entry = loadPrefixEntry(context, countryIso);
|
||||
}
|
||||
|
||||
public static void initialize(Context context) {
|
||||
@@ -36,28 +33,19 @@ public class PhoneNormalizer {
|
||||
public static String normalizePhone(String phoneNumber) {
|
||||
if (phoneNumber == null) return "";
|
||||
|
||||
// actual code to be used in production
|
||||
if (false) {
|
||||
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
return PhoneNumberUtils.formatNumberToE164(phoneNumber, instance.countryIso);
|
||||
} else {
|
||||
//noinspection deprecation
|
||||
return PhoneNumberUtils.formatNumber(instance.toE164(phoneNumber));
|
||||
}
|
||||
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
return PhoneNumberUtils.formatNumberToE164(phoneNumber, instance.countryIso);
|
||||
} else {
|
||||
//noinspection deprecation
|
||||
return PhoneNumberUtils.formatNumber(instance.toE164(phoneNumber));
|
||||
}
|
||||
|
||||
// for testing: skip API check, debug output.
|
||||
String normalized = instance.toE164(phoneNumber);
|
||||
Log.d("NORM", phoneNumber + " => " + normalized);
|
||||
return PhoneNumberUtils.formatNumber(normalized);
|
||||
}
|
||||
|
||||
private String toE164(String input) {
|
||||
if (input.startsWith("+"))
|
||||
return input;
|
||||
|
||||
if (prefix_data != null && prefix_data.containsKey(countryIso)) {
|
||||
PrefixEntry entry = prefix_data.get(countryIso);
|
||||
if (entry != null) {
|
||||
if (entry.international_prefix_pattern.length() > 0 &&
|
||||
input.matches(entry.international_prefix_pattern)) {
|
||||
return "+" + input.replaceFirst(entry.international_prefix_pattern, "");
|
||||
@@ -71,27 +59,24 @@ public class PhoneNormalizer {
|
||||
return input;
|
||||
}
|
||||
|
||||
private Map<String, PrefixEntry> loadPrefixData(Context context) {
|
||||
private PrefixEntry loadPrefixEntry(Context context, String countryIso) {
|
||||
InputStream inputStream = context.getResources().openRawResource(R.raw.phone_number_prefixes);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
|
||||
Map<String, PrefixEntry> data = new HashMap<>();
|
||||
try {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
String[] parts = line.split(",", -1);
|
||||
if (parts.length == 4) {
|
||||
data.put(parts[0],
|
||||
new PrefixEntry(parts[0], parts[1], parts[2], parts[3]));
|
||||
if (parts.length == 4 && parts[0].equals(countryIso)) {
|
||||
return new PrefixEntry(parts[0], parts[1], parts[2], parts[3]);
|
||||
} else {
|
||||
Log.w("PREFIX", "invalid csv record! " + line);
|
||||
}
|
||||
}
|
||||
} catch (IOException ioex) {
|
||||
ioex.printStackTrace();
|
||||
data = null;
|
||||
}
|
||||
return data;
|
||||
return null;
|
||||
}
|
||||
|
||||
private String determineCountryIso(Context context) {
|
||||
@@ -99,7 +84,7 @@ public class PhoneNormalizer {
|
||||
if (manager != null) {
|
||||
try {
|
||||
String simCountryIso = manager.getSimCountryIso();
|
||||
Log.d("COUNTRY", "SIM: " + simCountryIso);
|
||||
//Log.d("COUNTRY", "SIM: " + simCountryIso);
|
||||
if (!TextUtils.isEmpty(simCountryIso))
|
||||
return simCountryIso;
|
||||
} catch (Exception ex) {
|
||||
@@ -107,14 +92,14 @@ public class PhoneNormalizer {
|
||||
}
|
||||
try {
|
||||
String networkCountryIso = manager.getNetworkCountryIso();
|
||||
Log.d("COUNTRY", "NET: " + networkCountryIso);
|
||||
//Log.d("COUNTRY", "NET: " + networkCountryIso);
|
||||
if (!TextUtils.isEmpty(networkCountryIso))
|
||||
return networkCountryIso;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
Log.d("COUNTRY", "LOCALE: " + Locale.getDefault().getCountry());
|
||||
//Log.d("COUNTRY", "LOCALE: " + Locale.getDefault().getCountry());
|
||||
return Locale.getDefault().getCountry();
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -15,7 +15,12 @@ Options:
|
||||
international phone number prefixes (for normalizing numbers)
|
||||
[default: ../app/src/main/res/raw/phone_number_prefixes.csv]
|
||||
-d --debug Debug output (instead of concatenating the final regular expression)
|
||||
-h --help
|
||||
-h --help Show this screen
|
||||
|
||||
Remarks:
|
||||
This script uses data from googles libphonenumber. Please download
|
||||
PhoneNumberMetadata.xml and pass its path to the --input parameter.
|
||||
See: https://github.com/googlei18n/libphonenumber
|
||||
"""
|
||||
|
||||
import csv
|
||||
|
||||
Submodule utils/libphonenumber deleted from c70f1e784a
Reference in New Issue
Block a user