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.KeyCharacterMap.KeyData;
21import android.text.InputType;
22import android.text.Spannable;
23
24/**
25 * For dialing-only text entry
26 * <p></p>
27 * As for all implementations of {@link KeyListener}, this class is only concerned
28 * with hardware keyboards.  Software input methods have no obligation to trigger
29 * the methods in this class.
30 */
31public class DialerKeyListener extends NumberKeyListener
32{
33    @Override
34    protected char[] getAcceptedChars()
35    {
36        return CHARACTERS;
37    }
38
39    public static DialerKeyListener getInstance() {
40        if (sInstance != null)
41            return sInstance;
42
43        sInstance = new DialerKeyListener();
44        return sInstance;
45    }
46
47    public int getInputType() {
48        return InputType.TYPE_CLASS_PHONE;
49    }
50
51    /**
52     * Overrides the superclass's lookup method to prefer the number field
53     * from the KeyEvent.
54     */
55    protected int lookup(KeyEvent event, Spannable content) {
56        int meta = getMetaState(content, event);
57        int number = event.getNumber();
58
59        /*
60         * Prefer number if no meta key is active, or if it produces something
61         * valid and the meta lookup does not.
62         */
63        if ((meta & (MetaKeyKeyListener.META_ALT_ON | MetaKeyKeyListener.META_SHIFT_ON)) == 0) {
64            if (number != 0) {
65                return number;
66            }
67        }
68
69        int match = super.lookup(event, content);
70
71        if (match != 0) {
72            return match;
73        } else {
74            /*
75             * If a meta key is active but the lookup with the meta key
76             * did not produce anything, try some other meta keys, because
77             * the user might have pressed SHIFT when they meant ALT,
78             * or vice versa.
79             */
80
81            if (meta != 0) {
82                KeyData kd = new KeyData();
83                char[] accepted = getAcceptedChars();
84
85                if (event.getKeyData(kd)) {
86                    for (int i = 1; i < kd.meta.length; i++) {
87                        if (ok(accepted, kd.meta[i])) {
88                            return kd.meta[i];
89                        }
90                    }
91                }
92            }
93
94            /*
95             * Otherwise, use the number associated with the key, since
96             * whatever they wanted to do with the meta key does not
97             * seem to be valid here.
98             */
99
100            return number;
101        }
102    }
103
104
105    /**
106     * The characters that are used.
107     *
108     * @see KeyEvent#getMatch
109     * @see #getAcceptedChars
110     */
111    public static final char[] CHARACTERS = new char[] {
112            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '#', '*',
113            '+', '-', '(', ')', ',', '/', 'N', '.', ' ', ';'
114        };
115
116    private static DialerKeyListener sInstance;
117}
118