Home | History | Annotate | Download | only in softkeyboard
      1 /*
      2  * Copyright (C) 2008-2009 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 com.example.android.softkeyboard;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.content.res.XmlResourceParser;
     22 import android.graphics.drawable.Drawable;
     23 import android.inputmethodservice.Keyboard;
     24 import android.view.inputmethod.EditorInfo;
     25 
     26 public class LatinKeyboard extends Keyboard {
     27 
     28     private Key mEnterKey;
     29     private Key mSpaceKey;
     30 
     31     public LatinKeyboard(Context context, int xmlLayoutResId) {
     32         super(context, xmlLayoutResId);
     33     }
     34 
     35     public LatinKeyboard(Context context, int layoutTemplateResId,
     36             CharSequence characters, int columns, int horizontalPadding) {
     37         super(context, layoutTemplateResId, characters, columns, horizontalPadding);
     38     }
     39 
     40     @Override
     41     protected Key createKeyFromXml(Resources res, Row parent, int x, int y,
     42             XmlResourceParser parser) {
     43         Key key = new LatinKey(res, parent, x, y, parser);
     44         if (key.codes[0] == 10) {
     45             mEnterKey = key;
     46         } else if (key.codes[0] == ' ') {
     47             mSpaceKey = key;
     48         }
     49         return key;
     50     }
     51 
     52     /**
     53      * This looks at the ime options given by the current editor, to set the
     54      * appropriate label on the keyboard's enter key (if it has one).
     55      */
     56     void setImeOptions(Resources res, int options) {
     57         if (mEnterKey == null) {
     58             return;
     59         }
     60 
     61         switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
     62             case EditorInfo.IME_ACTION_GO:
     63                 mEnterKey.iconPreview = null;
     64                 mEnterKey.icon = null;
     65                 mEnterKey.label = res.getText(R.string.label_go_key);
     66                 break;
     67             case EditorInfo.IME_ACTION_NEXT:
     68                 mEnterKey.iconPreview = null;
     69                 mEnterKey.icon = null;
     70                 mEnterKey.label = res.getText(R.string.label_next_key);
     71                 break;
     72             case EditorInfo.IME_ACTION_SEARCH:
     73                 mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
     74                 mEnterKey.label = null;
     75                 break;
     76             case EditorInfo.IME_ACTION_SEND:
     77                 mEnterKey.iconPreview = null;
     78                 mEnterKey.icon = null;
     79                 mEnterKey.label = res.getText(R.string.label_send_key);
     80                 break;
     81             default:
     82                 mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_return);
     83                 mEnterKey.label = null;
     84                 break;
     85         }
     86     }
     87 
     88     void setSpaceIcon(final Drawable icon) {
     89         if (mSpaceKey != null) {
     90             mSpaceKey.icon = icon;
     91         }
     92     }
     93 
     94     static class LatinKey extends Keyboard.Key {
     95 
     96         public LatinKey(Resources res, Keyboard.Row parent, int x, int y, XmlResourceParser parser) {
     97             super(res, parent, x, y, parser);
     98         }
     99 
    100         /**
    101          * Overriding this method so that we can reduce the target area for the key that
    102          * closes the keyboard.
    103          */
    104         @Override
    105         public boolean isInside(int x, int y) {
    106             return super.isInside(x, codes[0] == KEYCODE_CANCEL ? y - 10 : y);
    107         }
    108     }
    109 
    110 }
    111