1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.contacts.tests; 18 19 import android.app.IntentService; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.location.CountryDetector; 23 import android.telephony.PhoneNumberUtils; 24 import android.util.Log; 25 26 import com.android.i18n.phonenumbers.NumberParseException; 27 import com.android.i18n.phonenumbers.PhoneNumberUtil; 28 import com.android.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; 29 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber; 30 31 import java.util.LinkedHashSet; 32 import java.util.Set; 33 34 /** 35 * A service to test various phone number formatters. 36 * 37 Usage: 38 adb shell am startservice -e n PHONE_NUMBER \ 39 [-e c OPTIONAL COUNTRY CODE] \ 40 com.android.contacts.tests/.PhoneNumberTestService 41 42 Example: 43 44 adb shell am startservice -e n '6502530000' \ 45 com.android.contacts.tests/.PhoneNumberTestService 46 */ 47 public class PhoneNumberTestService extends IntentService { 48 private static final String TAG = "phonenumber"; 49 50 private static final String EXTRA_PHONE_NUMBER = "n"; 51 private static final String EXTRA_COUNTRY_CODE = "c"; 52 PhoneNumberTestService()53 public PhoneNumberTestService() { 54 super("PhoneNumberTestService"); 55 } 56 57 @Override onHandleIntent(Intent intent)58 protected void onHandleIntent(Intent intent) { 59 final String number = intent.getStringExtra(EXTRA_PHONE_NUMBER); 60 final String country = intent.getStringExtra(EXTRA_COUNTRY_CODE); 61 final String defaultCountry = getCurrentCountryCode(); 62 63 Log.i(TAG, "Input phone number: " + number); 64 Log.i(TAG, "Input country code: " + country); 65 Log.i(TAG, "Current country code: " + defaultCountry); 66 67 // Dump for the given country, the current country, US, GB and JP. 68 Set<String> countries = new LinkedHashSet<String>(); 69 if (country != null) countries.add(country); 70 countries.add(defaultCountry); 71 countries.add("US"); 72 countries.add("GB"); 73 countries.add("JP"); 74 75 for (String c : countries) { 76 dump(number, c); 77 } 78 } 79 dump(String number, String country)80 private void dump(String number, String country) { 81 Log.i(TAG, "Result for: " + number + " / " +country); 82 dump_PhoneNumberUtils_formatNumberToE164(number, country); 83 dump_PhoneNumberUtil_format(number, country, PhoneNumberFormat.E164); 84 dump_PhoneNumberUtil_format(number, country, PhoneNumberFormat.INTERNATIONAL); 85 dump_PhoneNumberUtil_format(number, country, PhoneNumberFormat.NATIONAL); 86 dump_PhoneNumberUtil_format(number, country, PhoneNumberFormat.RFC3966); 87 } 88 dump_PhoneNumberUtils_formatNumberToE164(String number, String country)89 private void dump_PhoneNumberUtils_formatNumberToE164(String number, String country) { 90 Log.i(TAG, " formatNumberToE164(" + number + ", " + country 91 + ") = " + PhoneNumberUtils.formatNumberToE164(number, country)); 92 } 93 dump_PhoneNumberUtil_format(String number, String country, PhoneNumberFormat format)94 private void dump_PhoneNumberUtil_format(String number, String country, 95 PhoneNumberFormat format) { 96 String formatted; 97 String truncated = ""; 98 boolean isValid = false; 99 try { 100 final PhoneNumberUtil util = PhoneNumberUtil.getInstance(); 101 final PhoneNumber pn = util.parse(number, country); 102 isValid = util.isValidNumber(pn); 103 formatted = util.format(pn, format); 104 util.truncateTooLongNumber(pn); 105 truncated = util.format(pn, format); 106 } catch (NumberParseException e) { 107 formatted = "Error: " + e.toString(); 108 } 109 Log.i(TAG, " PhoneNumberUtil.format(parse(" + number + ", " + country + "), " + format 110 + ") = " + formatted + " / truncated = " + truncated 111 + (isValid ? " (valid)" : " (invalid)")); 112 } 113 getCurrentCountryCode()114 private String getCurrentCountryCode() { 115 final CountryDetector countryDetector = 116 (CountryDetector) getSystemService(Context.COUNTRY_DETECTOR); 117 return countryDetector.detectCountry().getCountryIso(); 118 } 119 } 120 121