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
17package com.android.contacts.tests;
18
19import android.app.IntentService;
20import android.content.Intent;
21import android.telephony.PhoneNumberUtils;
22import android.util.Log;
23
24import com.android.contacts.GeoUtil;
25
26import com.google.i18n.phonenumbers.NumberParseException;
27import com.google.i18n.phonenumbers.PhoneNumberUtil;
28import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
29import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
30
31import java.util.LinkedHashSet;
32import 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 */
47public 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
53    public PhoneNumberTestService() {
54        super("PhoneNumberTestService");
55    }
56
57    @Override
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
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
89    private void dump_PhoneNumberUtils_formatNumberToE164(String number, String country) {
90        Log.i(TAG, "  formatNumberToE164(" + number + ", " + country
91                + ") = " + PhoneNumberUtils.formatNumberToE164(number, country));
92    }
93
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
114    private String getCurrentCountryCode() {
115        return GeoUtil.getCurrentCountryIso(getApplicationContext());
116    }
117}
118