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