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 com.android.i18n.phonenumbers.AsYouTypeFormatter; 20import com.android.i18n.phonenumbers.PhoneNumberUtil; 21 22import android.telephony.PhoneNumberUtils; 23import android.text.Editable; 24import android.text.Selection; 25import android.text.TextWatcher; 26 27import java.util.Locale; 28 29/** 30 * Watches a {@link android.widget.TextView} and if a phone number is entered 31 * will format it. 32 * <p> 33 * Stop formatting when the user 34 * <ul> 35 * <li>Inputs non-dialable characters</li> 36 * <li>Removes the separator in the middle of string.</li> 37 * </ul> 38 * <p> 39 * The formatting will be restarted once the text is cleared. 40 */ 41public class PhoneNumberFormattingTextWatcher implements TextWatcher { 42 /** 43 * One or more characters were removed from the end. 44 */ 45 private final static int STATE_REMOVE_LAST = 0; 46 47 /** 48 * One or more characters were appended. 49 */ 50 private final static int STATE_APPEND = 1; 51 52 /** 53 * One or more digits were changed in the beginning or the middle of text. 54 */ 55 private final static int STATE_MODIFY_DIGITS = 2; 56 57 /** 58 * The changes other than the above. 59 */ 60 private final static int STATE_OTHER = 3; 61 62 /** 63 * The state of this change could be one value of the above 64 */ 65 private int mState; 66 67 /** 68 * Indicates the change was caused by ourselves. 69 */ 70 private boolean mSelfChange = false; 71 72 /** 73 * Indicates the formatting has been stopped. 74 */ 75 private boolean mStopFormatting; 76 77 private AsYouTypeFormatter mFormatter; 78 79 /** 80 * The formatting is based on the current system locale and future locale changes 81 * may not take effect on this instance. 82 */ 83 public PhoneNumberFormattingTextWatcher() { 84 this(Locale.getDefault().getCountry()); 85 } 86 87 /** 88 * The formatting is based on the given <code>countryCode</code>. 89 * 90 * @param countryCode the ISO 3166-1 two-letter country code that indicates the country/region 91 * where the phone number is being entered. 92 * 93 * @hide 94 */ 95 public PhoneNumberFormattingTextWatcher(String countryCode) { 96 if (countryCode == null) throw new IllegalArgumentException(); 97 mFormatter = PhoneNumberUtil.getInstance().getAsYouTypeFormatter(countryCode); 98 } 99 100 public void beforeTextChanged(CharSequence s, int start, int count, 101 int after) { 102 if (mSelfChange || mStopFormatting) { 103 return; 104 } 105 if (count == 0 && s.length() == start) { 106 // Append one or more new chars 107 mState = STATE_APPEND; 108 } else if (after == 0 && start + count == s.length() && count > 0) { 109 // Remove one or more chars from the end of string. 110 mState = STATE_REMOVE_LAST; 111 } else if (count > 0 && !hasSeparator(s, start, count)) { 112 // Remove the dialable chars in the begin or middle of text. 113 mState = STATE_MODIFY_DIGITS; 114 } else { 115 mState = STATE_OTHER; 116 } 117 } 118 119 public void onTextChanged(CharSequence s, int start, int before, int count) { 120 if (mSelfChange || mStopFormatting) { 121 return; 122 } 123 if (mState == STATE_OTHER) { 124 if (count > 0 && !hasSeparator(s, start, count)) { 125 // User inserted the dialable characters in the middle of text. 126 mState = STATE_MODIFY_DIGITS; 127 } 128 } 129 // Check whether we should stop formatting. 130 if (mState == STATE_APPEND && count > 0 && hasSeparator(s, start, count)) { 131 // User appended the non-dialable character, stop formatting. 132 stopFormatting(); 133 } else if (mState == STATE_OTHER) { 134 // User must insert or remove the non-dialable characters in the begin or middle of 135 // number, stop formatting. 136 stopFormatting(); 137 } 138 } 139 140 public synchronized void afterTextChanged(Editable s) { 141 if (mStopFormatting) { 142 // Restart the formatting when all texts were clear. 143 mStopFormatting = !(s.length() == 0); 144 return; 145 } 146 if (mSelfChange) { 147 // Ignore the change caused by s.replace(). 148 return; 149 } 150 String formatted = reformat(s, Selection.getSelectionEnd(s)); 151 if (formatted != null) { 152 int rememberedPos = mFormatter.getRememberedPosition(); 153 mSelfChange = true; 154 s.replace(0, s.length(), formatted, 0, formatted.length()); 155 // The text could be changed by other TextWatcher after we changed it. If we found the 156 // text is not the one we were expecting, just give up calling setSelection(). 157 if (formatted.equals(s.toString())) { 158 Selection.setSelection(s, rememberedPos); 159 } 160 mSelfChange = false; 161 } 162 } 163 164 /** 165 * Generate the formatted number by ignoring all non-dialable chars and stick the cursor to the 166 * nearest dialable char to the left. For instance, if the number is (650) 123-45678 and '4' is 167 * removed then the cursor should be behind '3' instead of '-'. 168 */ 169 private String reformat(CharSequence s, int cursor) { 170 // The index of char to the leftward of the cursor. 171 int curIndex = cursor - 1; 172 String formatted = null; 173 mFormatter.clear(); 174 char lastNonSeparator = 0; 175 boolean hasCursor = false; 176 int len = s.length(); 177 for (int i = 0; i < len; i++) { 178 char c = s.charAt(i); 179 if (PhoneNumberUtils.isNonSeparator(c)) { 180 if (lastNonSeparator != 0) { 181 formatted = getFormattedNumber(lastNonSeparator, hasCursor); 182 hasCursor = false; 183 } 184 lastNonSeparator = c; 185 } 186 if (i == curIndex) { 187 hasCursor = true; 188 } 189 } 190 if (lastNonSeparator != 0) { 191 formatted = getFormattedNumber(lastNonSeparator, hasCursor); 192 } 193 return formatted; 194 } 195 196 private String getFormattedNumber(char lastNonSeparator, boolean hasCursor) { 197 return hasCursor ? mFormatter.inputDigitAndRememberPosition(lastNonSeparator) 198 : mFormatter.inputDigit(lastNonSeparator); 199 } 200 201 private void stopFormatting() { 202 mStopFormatting = true; 203 mFormatter.clear(); 204 } 205 206 private boolean hasSeparator(final CharSequence s, final int start, final int count) { 207 for (int i = start; i < start + count; i++) { 208 char c = s.charAt(i); 209 if (!PhoneNumberUtils.isNonSeparator(c)) { 210 return true; 211 } 212 } 213 return false; 214 } 215} 216