Debian Codegraph @

tests/HandwritingIme/src/com/google/android/test/handwritingime/HandwritingIme.java

raw symbols SCIP
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 package com.google.android.test.handwritingime;
17
18 import android.annotation.Nullable;
19 import android.inputmethodservice.InputMethodService;
20 import android.util.Log;
21 import android.view.Gravity;
22 import android.view.MotionEvent;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.view.Window;
26 import android.widget.FrameLayout;
27 import android.widget.TextView;
28 import android.widget.Toast;
29
30 import java.util.Random;
31
32 public class HandwritingIme extends InputMethodService {
33
34 public static final int HEIGHT_DP = 100;
35
36 private Window mInkWindow;
37 private InkView mInk;
38
39 static final String TAG = "HandwritingIme";
40
41 interface HandwritingFinisher {
42 void finish();
43 }
44
45 interface StylusListener {
46 void onStylusEvent(MotionEvent me);
47 }
48
49 final class StylusConsumer implements StylusListener {
50 @Override
51 public void onStylusEvent(MotionEvent me) {
52 HandwritingIme.this.onStylusEvent(me);
53 }
54 }
55
56 final class HandwritingFinisherImpl implements HandwritingFinisher {
57
58 HandwritingFinisherImpl() {}
59
60 @Override
61 public void finish() {
62 finishStylusHandwriting();
63 Log.d(TAG, "HandwritingIme called finishStylusHandwriting() ");
64 }
65 }
66
67 private void onStylusEvent(@Nullable MotionEvent event) {
68 // TODO Hookup recognizer here
69 if (event.getAction() == MotionEvent.ACTION_UP) {
70 sendKeyChar((char) (56 + new Random().nextInt(66)));
71 }
72 }
73
74 @Override
75 public View onCreateInputView() {
76 Log.d(TAG, "onCreateInputView");
77 final ViewGroup view = new FrameLayout(this);
78 final View inner = new View(this);
79 final float density = getResources().getDisplayMetrics().density;
80 final int height = (int) (HEIGHT_DP * density);
81 view.setPadding(0, 0, 0, 0);
82 view.addView(inner, new FrameLayout.LayoutParams(
83 FrameLayout.LayoutParams.MATCH_PARENT, height));
84 TextView text = new TextView(this);
85 text.setText("Handwriting IME");
86 text.setTextSize(13f);
87 text.setTextColor(getColor(android.R.color.white));
88 text.setGravity(Gravity.CENTER);
89 text.setLayoutParams(new FrameLayout.LayoutParams(
90 FrameLayout.LayoutParams.MATCH_PARENT, height));
91 view.addView(text);
92 inner.setBackgroundColor(0xff0110fe); // blue
93
94 return view;
95 }
96
97 public void onPrepareStylusHandwriting() {
98 Log.d(TAG, "onPrepareStylusHandwriting ");
99 if (mInk == null) {
100 mInk = new InkView(this, new HandwritingFinisherImpl(), new StylusConsumer());
101 }
102 }
103
104 @Override
105 public boolean onStartStylusHandwriting() {
106 Log.d(TAG, "onStartStylusHandwriting ");
107 Toast.makeText(this, "START HW", Toast.LENGTH_SHORT).show();
108 mInkWindow = getStylusHandwritingWindow();
109 mInkWindow.setContentView(mInk, mInk.getLayoutParams());
110 return true;
111 }
112
113 @Override
114 public void onFinishStylusHandwriting() {
115 Log.d(TAG, "onFinishStylusHandwriting ");
116 Toast.makeText(this, "Finish HW", Toast.LENGTH_SHORT).show();
117 // Free-up
118 ((ViewGroup) mInk.getParent()).removeView(mInk);
119 mInk = null;
120 }
121 }