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