Add phone number format tester to test apk
Usage:
adb shell am startservice -e n '6502530000' \
com.android.contacts.tests/.PhoneNumberTestService
This will output the following on logcat.
I/phonenumber(29086): Input phone number: 6502530000
I/phonenumber(29086): Input country code: null
I/phonenumber(29086): Current country code: US
I/phonenumber(29086): Result for: 6502530000 / US
I/phonenumber(29086): formatNumberToE164(6502530000, US) = +16502530000
I/phonenumber(29086): PhoneNumberUtil.format(parse(6502530000, US), E164) = +16502530000 (valid)
I/phonenumber(29086): PhoneNumberUtil.format(parse(6502530000, US), INTERNATIONAL) = +1 650-253-0000 (valid)
I/phonenumber(29086): PhoneNumberUtil.format(parse(6502530000, US), NATIONAL) = (650) 253-0000 (valid)
I/phonenumber(29086): PhoneNumberUtil.format(parse(6502530000, US), RFC3966) = +1-650-253-0000 (valid)
:
:
Also fixed the copyright header for QueryService.java.
Change-Id: If504a803e0001eeafd5928979caa1510ac989282
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 7e23c57..2806830 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -121,6 +121,7 @@
</service>
<service android:name=".QueryService" />
+ <service android:name=".PhoneNumberTestService" />
</application>
<instrumentation android:name="android.test.InstrumentationTestRunner"
diff --git a/tests/src/com/android/contacts/tests/PhoneNumberTestService.java b/tests/src/com/android/contacts/tests/PhoneNumberTestService.java
new file mode 100644
index 0000000..894e66e
--- /dev/null
+++ b/tests/src/com/android/contacts/tests/PhoneNumberTestService.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.tests;
+
+import com.android.i18n.phonenumbers.NumberParseException;
+import com.android.i18n.phonenumbers.PhoneNumberUtil;
+import com.android.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
+import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber;
+
+import android.app.IntentService;
+import android.content.Context;
+import android.content.Intent;
+import android.location.CountryDetector;
+import android.telephony.PhoneNumberUtils;
+import android.util.Log;
+
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+/**
+ * A service to test various phone number formatters.
+ *
+ Usage:
+ adb shell am startservice -e n PHONE_NUMBER \
+ [-e c OPTIONAL COUNTRY CODE] \
+ com.android.contacts.tests/.PhoneNumberTestService
+
+ Example:
+
+ adb shell am startservice -e n '6502530000' \
+ com.android.contacts.tests/.PhoneNumberTestService
+ */
+public class PhoneNumberTestService extends IntentService {
+ private static final String TAG = "phonenumber";
+
+ private static final String EXTRA_PHONE_NUMBER = "n";
+ private static final String EXTRA_COUNTRY_CODE = "c";
+
+ public PhoneNumberTestService() {
+ super("PhoneNumberTestService");
+ }
+
+ @Override
+ protected void onHandleIntent(Intent intent) {
+ final String number = intent.getStringExtra(EXTRA_PHONE_NUMBER);
+ final String country = intent.getStringExtra(EXTRA_COUNTRY_CODE);
+ final String defaultCountry = getCurrentCountryCode();
+
+ Log.i(TAG, "Input phone number: " + number);
+ Log.i(TAG, "Input country code: " + country);
+ Log.i(TAG, "Current country code: " + defaultCountry);
+
+ // Dump for the given country, the current country, US, GB and JP.
+ Set<String> countries = new LinkedHashSet<String>();
+ if (country != null) countries.add(country);
+ countries.add(defaultCountry);
+ countries.add("US");
+ countries.add("GB");
+ countries.add("JP");
+
+ for (String c : countries) {
+ dump(number, c);
+ }
+ }
+
+ private void dump(String number, String country) {
+ Log.i(TAG, "Result for: " + number + " / " +country);
+ dump_PhoneNumberUtils_formatNumberToE164(number, country);
+ dump_PhoneNumberUtil_format(number, country, PhoneNumberFormat.E164);
+ dump_PhoneNumberUtil_format(number, country, PhoneNumberFormat.INTERNATIONAL);
+ dump_PhoneNumberUtil_format(number, country, PhoneNumberFormat.NATIONAL);
+ dump_PhoneNumberUtil_format(number, country, PhoneNumberFormat.RFC3966);
+ }
+
+ private void dump_PhoneNumberUtils_formatNumberToE164(String number, String country) {
+ Log.i(TAG, " formatNumberToE164(" + number + ", " + country
+ + ") = " + PhoneNumberUtils.formatNumberToE164(number, country));
+ }
+
+ private void dump_PhoneNumberUtil_format(String number, String country,
+ PhoneNumberFormat format) {
+ String formatted;
+ boolean isValid = false;
+ try {
+ final PhoneNumberUtil util = PhoneNumberUtil.getInstance();
+ final PhoneNumber pn = util.parse(number, country);
+ isValid = util.isValidNumber(pn);
+ formatted = util.format(pn, format);
+ } catch (NumberParseException e) {
+ formatted = "Error: " + e.toString();
+ }
+ Log.i(TAG, " PhoneNumberUtil.format(parse(" + number + ", " + country + "), " + format
+ + ") = " + formatted + (isValid ? " (valid)" : " (invalid)"));
+ }
+
+ private String getCurrentCountryCode() {
+ final CountryDetector countryDetector =
+ (CountryDetector) getSystemService(Context.COUNTRY_DETECTOR);
+ return countryDetector.detectCountry().getCountryIso();
+ }
+}
+
diff --git a/tests/src/com/android/contacts/tests/QueryService.java b/tests/src/com/android/contacts/tests/QueryService.java
index 008988a..fd349cc 100644
--- a/tests/src/com/android/contacts/tests/QueryService.java
+++ b/tests/src/com/android/contacts/tests/QueryService.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010 The Android Open Source Project
+ * Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.