NumberKeyListener.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2006 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.text.method;
18
19import android.view.KeyEvent;
20import android.view.View;
21import android.text.Editable;
22import android.text.InputFilter;
23import android.text.Selection;
24import android.text.Spannable;
25import android.text.SpannableStringBuilder;
26import android.text.Spanned;
27import android.util.SparseIntArray;
28
29/**
30 * For numeric text entry
31 */
32public abstract class NumberKeyListener extends BaseKeyListener
33    implements InputFilter
34{
35    /**
36     * You can say which characters you can accept.
37     */
38    protected abstract char[] getAcceptedChars();
39
40    protected int lookup(KeyEvent event, Spannable content) {
41        return event.getMatch(getAcceptedChars(), getMetaState(content));
42    }
43
44    public CharSequence filter(CharSequence source, int start, int end,
45                               Spanned dest, int dstart, int dend) {
46        char[] accept = getAcceptedChars();
47        boolean filter = false;
48
49        int i;
50        for (i = start; i < end; i++) {
51            if (!ok(accept, source.charAt(i))) {
52                break;
53            }
54        }
55
56        if (i == end) {
57            // It was all OK.
58            return null;
59        }
60
61        if (end - start == 1) {
62            // It was not OK, and there is only one char, so nothing remains.
63            return "";
64        }
65
66        SpannableStringBuilder filtered =
67            new SpannableStringBuilder(source, start, end);
68        i -= start;
69        end -= start;
70
71        int len = end - start;
72        // Only count down to i because the chars before that were all OK.
73        for (int j = end - 1; j >= i; j--) {
74            if (!ok(accept, source.charAt(j))) {
75                filtered.delete(j, j + 1);
76            }
77        }
78
79        return filtered;
80    }
81
82    protected static boolean ok(char[] accept, char c) {
83        for (int i = accept.length - 1; i >= 0; i--) {
84            if (accept[i] == c) {
85                return true;
86            }
87        }
88
89        return false;
90    }
91
92    @Override
93    public boolean onKeyDown(View view, Editable content,
94                             int keyCode, KeyEvent event) {
95        int selStart, selEnd;
96
97        {
98            int a = Selection.getSelectionStart(content);
99            int b = Selection.getSelectionEnd(content);
100
101            selStart = Math.min(a, b);
102            selEnd = Math.max(a, b);
103        }
104
105        int i = event != null ? lookup(event, content) : 0;
106        int repeatCount = event != null ? event.getRepeatCount() : 0;
107        if (repeatCount == 0) {
108            if (i != 0) {
109                if (selStart != selEnd) {
110                    Selection.setSelection(content, selEnd);
111                }
112
113                content.replace(selStart, selEnd, String.valueOf((char) i));
114
115                adjustMetaAfterKeypress(content);
116                return true;
117            }
118        } else if (i == '0' && repeatCount == 1) {
119            // Pretty hackish, it replaces the 0 with the +
120
121            if (selStart == selEnd && selEnd > 0 &&
122                    content.charAt(selStart - 1) == '0') {
123                content.replace(selStart - 1, selEnd, String.valueOf('+'));
124                adjustMetaAfterKeypress(content);
125                return true;
126            }
127        }
128
129        adjustMetaAfterKeypress(content);
130        return super.onKeyDown(view, content, keyCode, event);
131    }
132}
133