1/*
2 * Copyright (C) 2008 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 android.telephony;
18
19import android.text.Editable;
20
21/*
22 * Japanese Phone number formatting rule is a bit complicated.
23 * Here are some valid examples:
24 *
25 * 022-229-1234 0223-23-1234 022-301-9876 015-482-7849 0154-91-3478
26 * 01547-5-4534 090-1234-1234 080-0123-6789
27 * 0800-000-9999 0570-000-000 0276-00-0000
28 *
29 * As you can see, there is no straight-forward rule here.
30 * In order to handle this, a big array is prepared.
31 */
32/* package */ class JapanesePhoneNumberFormatter {
33    private static short FORMAT_MAP[] = {
34    -100, 10, 220, -15, 410, 530, -15, 670, 780, 1060,
35    -100, -25, 20, 40, 70, 100, 150, 190, 200, 210,
36    -36, -100, -100, -35, -35, -35, 30, -100, -100, -100,
37    -35, -35, -35, -35, -35, -35, -35, -45, -35, -35,
38    -100, -100, -100, -35, -35, -35, -35, 50, -35, 60,
39    -35, -35, -45, -35, -45, -35, -35, -45, -35, -35,
40    -35, -35, -45, -35, -35, -35, -35, -45, -45, -35,
41    -100, -100, -35, -35, -35, 80, 90, -100, -100, -100,
42    -35, -35, -35, -35, -35, -35, -45, -45, -35, -35,
43    -35, -35, -35, -35, -35, -35, -45, -35, -35, -35,
44    -25, -25, -35, -35, 110, 120, 130, -35, 140, -25,
45    -35, -25, -35, -35, -35, -35, -35, -45, -25, -35,
46    -35, -25, -35, -35, -35, -35, -35, -25, -45, -35,
47    -35, -35, -35, -35, -45, -35, -35, -35, -35, -35,
48    -35, -35, -35, -35, -35, -35, -45, -45, -35, -35,
49    -100, -100, -35, 160, 170, 180, -35, -35, -100, -100,
50    -35, -35, -45, -35, -45, -45, -35, -35, -35, -35,
51    -35, -35, -35, -35, -35, -35, -35, -35, -45, -35,
52    -35, -35, -35, -35, -45, -45, -45, -35, -45, -35,
53    -25, -25, -35, -35, -35, -35, -35, -25, -35, -35,
54    -25, -25, -35, -35, -35, -35, -35, -35, -25, -25,
55    -25, -35, -35, -35, -35, -35, -25, -35, -35, -25,
56    -100, -100, 230, 250, 260, 270, 320, 340, 360, 390,
57    -35, -25, -25, 240, -35, -35, -35, -25, -35, -35,
58    -25, -35, -35, -35, -25, -25, -25, -25, -25, -25,
59    -25, -25, -25, -35, -35, -35, -25, -35, -35, -25,
60    -35, -35, -35, -35, -35, -25, -35, -35, -35, -25,
61    -35, -25, -25, -25, -35, 280, 290, 300, 310, -35,
62    -25, -25, -25, -25, -25, -25, -25, -35, -35, -25,
63    -25, -35, -35, -35, -35, -35, -35, -35, -35, -35,
64    -25, -25, -35, -35, -35, -25, -25, -25, -25, -25,
65    -25, -35, -35, -35, -35, -35, -35, -35, -35, -35,
66    -35, -35, -25, -35, 330, -35, -35, -35, -35, -35,
67    -25, -35, -35, -35, -35, -35, -25, -25, -25, -25,
68    -35, -25, -25, -25, -35, -25, -35, -35, 350, -35,
69    -25, -35, -35, -35, -35, -35, -35, -35, -25, -25,
70    -35, -25, -35, 370, -35, -35, -25, -35, -35, 380,
71    -25, -35, -35, -25, -25, -35, -35, -35, -35, -35,
72    -25, -35, -25, -25, -25, -25, -35, -35, -35, -35,
73    -25, -35, -25, 400, -35, -35, -35, -35, -25, -35,
74    -25, -35, -35, -35, -35, -25, -25, -25, -25, -25,
75    -15, -15, 420, 460, -25, -25, 470, 480, 500, 510,
76    -15, -25, 430, -25, -25, -25, -25, -25, 440, 450,
77    -25, -35, -35, -35, -35, -35, -35, -35, -35, -35,
78    -25, -25, -35, -35, -25, -25, -25, -35, -35, -35,
79    -15, -25, -15, -15, -15, -15, -15, -25, -25, -15,
80    -25, -25, -25, -25, -25, -25, -35, -25, -35, -35,
81    -35, -25, -25, -35, -25, -35, -35, -35, -25, -25,
82    490, -15, -25, -25, -25, -35, -35, -25, -35, -35,
83    -15, -35, -35, -35, -35, -35, -35, -35, -35, -15,
84    -35, -25, -25, -25, -25, -25, -25, -25, -25, -25,
85    -25, -25, -25, -35, -35, -35, -25, -25, -25, 520,
86    -100, -100, -45, -100, -45, -100, -45, -100, -45, -100,
87    -25, -100, -25, 540, 580, 590, 600, 610, 630, 640,
88    -25, -35, -35, -35, -25, -25, -35, -35, -35, 550,
89    -35, -35, -25, -25, -25, -25, 560, 570, -25, -35,
90    -35, -35, -35, -35, -25, -25, -25, -25, -25, -25,
91    -25, -25, -25, -25, -35, -25, -25, -35, -25, -25,
92    -25, -25, -25, -25, -35, -35, -25, -35, -35, -25,
93    -35, -35, -25, -35, -35, -35, -35, -35, -35, -25,
94    -100, -35, -35, -35, -35, -35, -35, -35, -35, -35,
95    -36, -100, -35, -35, -35, -35, 620, -35, -35, -100,
96    -35, -35, -35, -35, -35, -35, -35, -35, -35, -45,
97    -25, -35, -25, -25, -35, -35, -35, -35, -25, -25,
98    -25, -25, -25, -25, -35, -35, -35, 650, -35, 660,
99    -35, -35, -35, -35, -45, -35, -35, -35, -35, -45,
100    -35, -35, -35, -35, -35, -35, -35, -35, -35, -25,
101    -26, -100, 680, 690, 700, -25, 720, 730, -25, 740,
102    -25, -35, -25, -25, -25, -35, -25, -25, -25, -25,
103    -25, -25, -25, -25, -25, -35, -35, -35, -35, -35,
104    -35, -100, -35, -35, -35, -35, 710, -35, -35, -35,
105    -35, -35, -35, -35, -35, -35, -35, -35, -45, -35,
106    -25, -35, -25, -35, -25, -35, -35, -35, -35, -25,
107    -35, -35, -35, -35, -35, -25, -35, -25, -35, -35,
108    -35, -35, -25, -25, 750, 760, 770, -35, -35, -35,
109    -25, -35, -25, -25, -25, -25, -35, -35, -35, -25,
110    -25, -35, -35, -35, -35, -25, -25, -35, -35, -25,
111    -25, -35, -35, -35, -35, -35, -25, -25, -35, -35,
112    790, -100, 800, 850, 900, 920, 940, 1030, 1040, 1050,
113    -36, -26, -26, -26, -26, -26, -26, -26, -26, -26,
114    -35, -25, -25, -35, 810, -25, -35, -35, -25, 820,
115    -25, -35, -25, -25, -35, -35, -35, -35, -35, -25,
116    -25, -35, 830, -35, 840, -35, -25, -35, -35, -25,
117    -35, -25, -25, -25, -25, -25, -25, -25, -25, -25,
118    -100, -25, -25, -25, -100, -100, -100, -100, -100, -100,
119    -25, -25, -35, -35, -35, -35, 860, -35, 870, 880,
120    -25, -35, -35, -35, -35, -35, -35, -35, -35, -35,
121    -35, -35, -35, -35, -35, -35, -35, -45, -45, -35,
122    -100, -100, -100, -100, -100, -100, 890, -100, -100, -100,
123    -25, -45, -45, -25, -45, -45, -25, -45, -45, -45,
124    -25, -25, -25, -25, -25, -35, -35, 910, -35, -25,
125    -35, -35, -35, -35, -35, -35, -35, -45, -35, -35,
126    -100, 930, -35, -35, -35, -35, -35, -35, -35, -35,
127    -100, -100, -45, -100, -45, -100, -100, -100, -100, -100,
128    -25, -25, -25, 950, -25, 970, 990, -35, 1000, 1010,
129    -35, -35, -35, -35, -35, -35, 960, -35, -35, -35,
130    -45, -45, -45, -45, -45, -45, -35, -45, -45, -45,
131    -35, -35, -25, -35, -35, 980, -35, -35, -35, -35,
132    -100, -100, -25, -25, -100, -100, -100, -100, -100, -100,
133    -25, -35, -35, -35, -35, -35, -35, -35, -35, -35,
134    -25, -35, -35, -35, -35, -35, -35, -35, -35, -25,
135    -25, -35, -35, -35, -25, -25, -35, -35, -35, 1020,
136    -45, -45, -35, -35, -45, -45, -45, -45, -45, -45,
137    -25, -25, -25, -25, -25, -35, -25, -35, -25, -35,
138    -35, -25, -25, -35, -35, -35, -25, -35, -25, -35,
139    -25, -25, -35, -35, -35, -35, -35, -35, -35, -25,
140    -26, -100, 1070, 1080, 1090, 1110, 1120, 1130, 1140, 1160,
141    -35, -25, -25, -25, -25, -25, -25, -25, -25, -25,
142    -35, -25, -25, -25, -25, -25, -25, -25, -25, -25,
143    -35, -100, -35, -35, -35, -100, -35, -35, -35, 1100,
144    -35, -35, -35, -35, -35, -35, -45, -35, -35, -35,
145    -35, -25, -35, -25, -35, -35, -35, -35, -25, -35,
146    -25, -25, -25, -25, -35, -35, -35, -35, -35, -35,
147    -25, -25, -35, -35, -35, -25, -25, -35, -35, -35,
148    1150, -25, -35, -35, -35, -35, -35, -35, -25, -25,
149    -35, -35, -45, -35, -35, -35, -35, -35, -35, -35,
150    -35, 1170, -25, -35, 1180, -35, 1190, -35, -25, -25,
151    -100, -100, -45, -45, -100, -100, -100, -100, -100, -100,
152    -25, -35, -35, -35, -35, -35, -35, -25, -25, -35,
153    -35, -35, -35, -35, -35, -35, -35, -35, -35, -45};
154
155    public static void format(Editable text) {
156        // Here, "root" means the position of "'":
157        // 0'3, 0'90, and +81'-90
158        // (dash will be deleted soon, so it is actually +81'90).
159        int rootIndex = 1;
160        int length = text.length();
161        if (length > 3
162                && text.subSequence(0, 3).toString().equals("+81")) {
163            rootIndex = 3;
164        } else if (length < 1 || text.charAt(0) != '0') {
165            return;
166        }
167
168        CharSequence saved = text.subSequence(0, length);
169
170        // Strip the dashes first, as we're going to add them back
171        int i = 0;
172        while (i < text.length()) {
173            if (text.charAt(i) == '-') {
174                text.delete(i, i + 1);
175            } else {
176                i++;
177            }
178        }
179
180        length = text.length();
181        int dashposition;
182
183        i = rootIndex;
184        int base = 0;
185        while (i < length) {
186            char ch = text.charAt(i);
187            if (!Character.isDigit(ch)) {
188                text.replace(0, length, saved);
189                return;
190            }
191            short value = FORMAT_MAP[base + ch - '0'];
192            if (value < 0) {
193                if (value <= -100) {
194                    text.replace(0, length, saved);
195                    return;
196                }
197                int dashPos2 = rootIndex + (Math.abs(value) % 10);
198                if (length > dashPos2) {
199                    text.insert(dashPos2, "-");
200                }
201                int dashPos1 = rootIndex + (Math.abs(value) / 10);
202                if (length > dashPos1) {
203                    text.insert(dashPos1, "-");
204                }
205                break;
206            } else {
207                base = value;
208                i++;
209            }
210        }
211
212        if (length > 3 && rootIndex == 3) {
213            text.insert(rootIndex, "-");
214        }
215    }
216}