• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.android.apps.inputmethod.simpleime;
18 
19 import android.inputmethodservice.InputMethodService;
20 import android.os.SystemClock;
21 import android.util.Log;
22 import android.view.KeyEvent;
23 import android.view.View;
24 
25 import androidx.annotation.NonNull;
26 
27 import com.android.apps.inputmethod.simpleime.ims.InputMethodServiceWrapper;
28 
29 /** A simple implementation of an {@link InputMethodService}. */
30 public final class SimpleInputMethodService extends InputMethodServiceWrapper {
31 
32     private static final String TAG = "SimpleIMS";
33 
34     @Override
onCreateInputView()35     public View onCreateInputView() {
36         Log.i(TAG, "onCreateInputView()");
37         final var simpleKeyboard = new SimpleKeyboardView(this);
38         simpleKeyboard.setKeyPressListener(this::onKeyPress);
39         return simpleKeyboard;
40     }
41 
42     /**
43      * Called when a key is pressed.
44      *
45      * @param keyCodeName the keycode of the key, as a string.
46      * @param metaState   the flags indicating which meta keys are currently pressed.
47      */
onKeyPress(@onNull String keyCodeName, int metaState)48     private void onKeyPress(@NonNull String keyCodeName, int metaState) {
49         final int keyCode = KeyCodeConstants.getKeyCode(keyCodeName);
50         Log.v(TAG, "onKeyPress: " + keyCode);
51         if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
52             final var ic = getCurrentInputConnection();
53             if (ic != null) {
54                 final var downTime = SystemClock.uptimeMillis();
55                 ic.sendKeyEvent(new KeyEvent(downTime, downTime, KeyEvent.ACTION_DOWN, keyCode,
56                         0 /* repeat */, KeyCodeConstants.isAlphaKeyCode(keyCode) ? metaState : 0));
57             }
58         }
59     }
60 }
61