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.LayoutInflater;
24 import android.view.View;
25 import android.view.inputmethod.EditorInfo;
26 import android.view.inputmethod.InputConnection;
27 import android.widget.FrameLayout;
28 
29 import com.android.apps.inputmethod.simpleime.ims.InputMethodServiceWrapper;
30 
31 /** The {@link InputMethodService} implementation for SimpeTestIme app. */
32 public class SimpleInputMethodService extends InputMethodServiceWrapper {
33     private static final String TAG = "SimpleIMS";
34 
35     private FrameLayout mInputView;
36 
37     @Override
onCreateInputView()38     public View onCreateInputView() {
39         Log.i(TAG, "onCreateInputView()");
40         mInputView = (FrameLayout) LayoutInflater.from(this).inflate(R.layout.input_view, null);
41         return mInputView;
42     }
43 
44     @Override
onStartInputView(EditorInfo info, boolean restarting)45     public void onStartInputView(EditorInfo info, boolean restarting) {
46         super.onStartInputView(info, restarting);
47         mInputView.removeAllViews();
48         SimpleKeyboard keyboard = new SimpleKeyboard(this, R.layout.qwerty_10_9_9);
49         mInputView.addView(keyboard.inflateKeyboardView(LayoutInflater.from(this), mInputView));
50     }
51 
handle(String data, int keyboardState)52     void handle(String data, int keyboardState) {
53         InputConnection inputConnection = getCurrentInputConnection();
54         Integer keyCode = KeyCodeConstants.KEY_NAME_TO_CODE_MAP.get(data);
55         Log.v(TAG, "keyCode: " + keyCode);
56         if (keyCode != null) {
57             inputConnection.sendKeyEvent(
58                     new KeyEvent(
59                             SystemClock.uptimeMillis(),
60                             SystemClock.uptimeMillis(),
61                             KeyEvent.ACTION_DOWN,
62                             keyCode,
63                             0,
64                             KeyCodeConstants.isAlphaKeyCode(keyCode) ? keyboardState : 0));
65         }
66     }
67 }
68