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 /** 44 * Indicates the change was caused by ourselves. 45 */ 46 private boolean mSelfChange = false; 47 48 /** 49 * Indicates the formatting has been stopped. 50 */ 51 private boolean mStopFormatting; 52 53 private AsYouTypeFormatter mFormatter; 54 55 /** 56 * The formatting is based on the current system locale and future locale changes 57 * may not take effect on this instance. 58 */ 59 public PhoneNumberFormattingTextWatcher() { 60 this(Locale.getDefault().getCountry()); 61 } 62 63 /** 64 * The formatting is based on the given <code>countryCode</code>. 65 * 66 * @param countryCode the ISO 3166-1 two-letter country code that indicates the country/region 67 * where the phone number is being entered. 68 * 69 * @hide 70 */ 71 public PhoneNumberFormattingTextWatcher(String countryCode) { 72 if (countryCode == null) throw new IllegalArgumentException(); 73 mFormatter = PhoneNumberUtil.getInstance().getAsYouTypeFormatter(countryCode); 74 } 75 76 @Override 77 public void beforeTextChanged(CharSequence s, int start, int count, 78 int after) { 79 if (mSelfChange || mStopFormatting) { 80 return; 81 } 82 // If the user manually deleted any non-dialable characters, stop formatting 83 if (count > 0 && hasSeparator(s, start, count)) { 84 stopFormatting(); 85 } 86 } 87 88 @Override 89 public void onTextChanged(CharSequence s, int start, int before, int count) { 90 if (mSelfChange || mStopFormatting) { 91 return; 92 } 93 // If the user inserted any non-dialable characters, stop formatting 94 if (count > 0 && hasSeparator(s, start, count)) { 95 stopFormatting(); 96 } 97 } 98 99 @Override 100 public synchronized void afterTextChanged(Editable s) { 101 if (mStopFormatting) { 102 // Restart the formatting when all texts were clear. 103 mStopFormatting = !(s.length() == 0); 104 return; 105 } 106 if (mSelfChange) { 107 // Ignore the change caused by s.replace(). 108 return; 109 } 110 String formatted = reformat(s, Selection.getSelectionEnd(s)); 111 if (formatted != null) { 112 int rememberedPos = mFormatter.getRememberedPosition(); 113 mSelfChange = true; 114 s.replace(0, s.length(), formatted, 0, formatted.length()); 115 // The text could be changed by other TextWatcher after we changed it. If we found the 116 // text is not the one we were expecting, just give up calling setSelection(). 117 if (formatted.equals(s.toString())) { 118 Selection.setSelection(s, rememberedPos); 119 } 120 mSelfChange = false; 121 } 122 } 123 124 /** 125 * Generate the formatted number by ignoring all non-dialable chars and stick the cursor to the 126 * nearest dialable char to the left. For instance, if the number is (650) 123-45678 and '4' is 127 * removed then the cursor should be behind '3' instead of '-'. 128 */ 129 private String reformat(CharSequence s, int cursor) { 130 // The index of char to the leftward of the cursor. 131 int curIndex = cursor - 1; 132 String formatted = null; 133 mFormatter.clear(); 134 char lastNonSeparator = 0; 135 boolean hasCursor = false; 136 int len = s.length(); 137 for (int i = 0; i < len; i++) { 138 char c = s.charAt(i); 139 if (PhoneNumberUtils.isNonSeparator(c)) { 140 if (lastNonSeparator != 0) { 141 formatted = getFormattedNumber(lastNonSeparator, hasCursor); 142 hasCursor = false; 143 } 144 lastNonSeparator = c; 145 } 146 if (i == curIndex) { 147 hasCursor = true; 148 } 149 } 150 if (lastNonSeparator != 0) { 151 formatted = getFormattedNumber(lastNonSeparator, hasCursor); 152 } 153 return formatted; 154 } 155 156 private String getFormattedNumber(char lastNonSeparator, boolean hasCursor) { 157 return hasCursor ? mFormatter.inputDigitAndRememberPosition(lastNonSeparator) 158 : mFormatter.inputDigit(lastNonSeparator); 159 } 160 161 private void stopFormatting() { 162 mStopFormatting = true; 163 mFormatter.clear(); 164 } 165 166 private boolean hasSeparator(final CharSequence s, final int start, final int count) { 167 for (int i = start; i < start + count; i++) { 168 char c = s.charAt(i); 169 if (!PhoneNumberUtils.isNonSeparator(c)) { 170 return true; 171 } 172 } 173 return false; 174 } 175} 176