PhoneNumberFormattingTextWatcher.java revision 767a662ecde33c3979bf02b793d392aca0403162
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;
20import android.text.Selection;
21import android.text.TextWatcher;
22import android.widget.TextView;
23
24import java.util.Locale;
25
26/**
27 * Watches a {@link TextView} and if a phone number is entered will format it using
28 * {@link PhoneNumberUtils#formatNumber(Editable, int)}. The formatting is based on
29 * the current system locale when this object is created and future locale changes
30 * may not take effect on this instance.
31 */
32public class PhoneNumberFormattingTextWatcher implements TextWatcher {
33
34    static private int sFormatType;
35    static private Locale sCachedLocale;
36    private boolean mFormatting;
37    private boolean mDeletingHyphen;
38    private int mHyphenStart;
39    private boolean mDeletingBackward;
40
41    public PhoneNumberFormattingTextWatcher() {
42        if (sCachedLocale == null || sCachedLocale != Locale.getDefault()) {
43            sCachedLocale = Locale.getDefault();
44            sFormatType = PhoneNumberUtils.getFormatTypeForLocale(sCachedLocale);
45        }
46    }
47
48    public synchronized void afterTextChanged(Editable text) {
49        // Make sure to ignore calls to afterTextChanged caused by the work done below
50        if (!mFormatting) {
51            mFormatting = true;
52
53            // If deleting the hyphen, also delete the char before or after that
54            if (mDeletingHyphen && mHyphenStart > 0) {
55                if (mDeletingBackward) {
56                    if (mHyphenStart - 1 < text.length()) {
57                        text.delete(mHyphenStart - 1, mHyphenStart);
58                    }
59                } else if (mHyphenStart < text.length()) {
60                    text.delete(mHyphenStart, mHyphenStart + 1);
61                }
62            }
63
64            PhoneNumberUtils.formatNumber(text, sFormatType);
65
66            mFormatting = false;
67        }
68    }
69
70    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
71        // Check if the user is deleting a hyphen
72        if (!mFormatting) {
73            // Make sure user is deleting one char, without a selection
74            final int selStart = Selection.getSelectionStart(s);
75            final int selEnd = Selection.getSelectionEnd(s);
76            if (s.length() > 1 // Can delete another character
77                    && count == 1 // Deleting only one character
78                    && after == 0 // Deleting
79                    && s.charAt(start) == '-' // a hyphen
80                    && selStart == selEnd) { // no selection
81                mDeletingHyphen = true;
82                mHyphenStart = start;
83                // Check if the user is deleting forward or backward
84                if (selStart == start + 1) {
85                    mDeletingBackward = true;
86                } else {
87                    mDeletingBackward = false;
88                }
89            } else {
90                mDeletingHyphen = false;
91            }
92        }
93    }
94
95    public void onTextChanged(CharSequence s, int start, int before, int count) {
96        // Does nothing
97    }
98}
99