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