• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008-2009 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * 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.inputmethodservice.Keyboard;
23 import android.inputmethodservice.Keyboard.Key;
24 import android.inputmethodservice.Keyboard.Row;
25 import android.view.inputmethod.EditorInfo;
26 
27 public class LatinKeyboard extends Keyboard {
28 
29     private Key mEnterKey;
30 
LatinKeyboard(Context context, int xmlLayoutResId)31     public LatinKeyboard(Context context, int xmlLayoutResId) {
32         super(context, xmlLayoutResId);
33     }
34 
LatinKeyboard(Context context, int layoutTemplateResId, CharSequence characters, int columns, int horizontalPadding)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
createKeyFromXml(Resources res, Row parent, int x, int y, XmlResourceParser parser)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         }
47         return key;
48     }
49 
50     /**
51      * This looks at the ime options given by the current editor, to set the
52      * appropriate label on the keyboard's enter key (if it has one).
53      */
setImeOptions(Resources res, int options)54     void setImeOptions(Resources res, int options) {
55         if (mEnterKey == null) {
56             return;
57         }
58 
59         switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
60             case EditorInfo.IME_ACTION_GO:
61                 mEnterKey.iconPreview = null;
62                 mEnterKey.icon = null;
63                 mEnterKey.label = res.getText(R.string.label_go_key);
64                 break;
65             case EditorInfo.IME_ACTION_NEXT:
66                 mEnterKey.iconPreview = null;
67                 mEnterKey.icon = null;
68                 mEnterKey.label = res.getText(R.string.label_next_key);
69                 break;
70             case EditorInfo.IME_ACTION_SEARCH:
71                 mEnterKey.icon = res.getDrawable(
72                         R.drawable.sym_keyboard_search);
73                 mEnterKey.label = null;
74                 break;
75             case EditorInfo.IME_ACTION_SEND:
76                 mEnterKey.iconPreview = null;
77                 mEnterKey.icon = null;
78                 mEnterKey.label = res.getText(R.string.label_send_key);
79                 break;
80             default:
81                 mEnterKey.icon = res.getDrawable(
82                         R.drawable.sym_keyboard_return);
83                 mEnterKey.label = null;
84                 break;
85         }
86     }
87 
88     static class LatinKey extends Keyboard.Key {
89 
LatinKey(Resources res, Keyboard.Row parent, int x, int y, XmlResourceParser parser)90         public LatinKey(Resources res, Keyboard.Row parent, int x, int y, XmlResourceParser parser) {
91             super(res, parent, x, y, parser);
92         }
93 
94         /**
95          * Overriding this method so that we can reduce the target area for the key that
96          * closes the keyboard.
97          */
98         @Override
isInside(int x, int y)99         public boolean isInside(int x, int y) {
100             return super.isInside(x, codes[0] == KEYCODE_CANCEL ? y - 10 : y);
101         }
102     }
103 
104 }
105