blob: 05751d62e5f762f2096c963278a7c683a8282284 [file] [log] [blame]
Yan Zhu285718d2019-05-29 15:40:40 -07001/*
2 * Copyright (C) 2019 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
17package com.google.android.car.multidisplaytest.ime;
18
19import android.content.Context;
20import android.os.Bundle;
21import android.util.Log;
22import android.view.LayoutInflater;
23import android.view.MotionEvent;
24import android.view.View;
25import android.view.ViewGroup;
26import android.view.inputmethod.InputConnection;
27import android.view.inputmethod.InputMethodManager;
28import android.widget.Button;
29import android.widget.EditText;
30import android.widget.TextView;
31
32import androidx.fragment.app.Fragment;
33
34import com.google.android.car.multidisplaytest.R;
35
36/**
37 * Modified from GarageModeTestApp;
38 * Including coping Watchdog.java and Logger.java
39 */
40public class InputTestFragment extends Fragment {
41 private static final String TAG = InputTestFragment.class.getSimpleName();
42
43 private Button mClearButton;
44 private EditText mTestEditText;
45 private InputMethodManager mInputManager;
46 private InputConnection mInputConnection;
47 private TextView mWatchdogTextView;
48 private ViewGroup mInputViewGroup;
49 private Watchdog mWatchdog;
50
51 @Override
52 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
53 View view = inflater.inflate(R.layout.input_type_test, container, false);
54 setViewsFromFragment(view);
55 setListners();
56
57 return view;
58 }
59
60 @Override
61 public void onResume() {
62 super.onResume();
63
64 Log.d(TAG, "Resuming watchdog");
65
66 mWatchdog = new Watchdog(mWatchdogTextView);
67 mWatchdog.start();
68 }
69
70 @Override
71 public void onPause() {
72 super.onPause();
73
74 Log.d(TAG, "Pausing watchdog");
75
76 if (mWatchdog != null) {
77 mWatchdog.stop();
78 mWatchdog = null;
79 }
80 }
81
82 private void setViewsFromFragment(View view) {
83 mWatchdogTextView = view.findViewById(R.id.ime_watchdog);
84 mInputManager = (InputMethodManager) getActivity()
85 .getSystemService(Context.INPUT_METHOD_SERVICE);
86 mInputViewGroup = view.findViewById(R.id.inputViewGroup);
87 mClearButton = view.findViewById(R.id.clearButton);
88 // Log this EditText view's input focus to test for input connection with IME
89 mTestEditText = view.findViewById(R.id.testEditText);
90 }
91
92 private void setListners() {
93 mClearButton.setOnClickListener(view -> onClearButtonClick());
94 mInputViewGroup.setOnTouchListener((view, event) -> {
95 if (event.getActionMasked() == MotionEvent.ACTION_UP) {
96 if (mWatchdog != null) {
97 boolean activeState = mInputManager.isActive();
98 boolean acceptingState = mInputManager.isAcceptingText();
99 String logMessage = String.format("IME states: Active - %b, AcceptingText - %b",
100 activeState, acceptingState);
101 mWatchdog.logEvent(logMessage);
102 }
103 }
104 return true;
105 });
106
107 mTestEditText.setOnFocusChangeListener((view, hasFocus) -> {
108 if (mWatchdog != null) {
109 if (hasFocus) {
110 mWatchdog.logEvent("EditText view has input connection with IME");
111 } else {
112 mWatchdog.logEvent("EditText view doesn't have input connection with IME");
113 }
114 }
115 });
116 }
117
118 private void onClearButtonClick() {
119 if (mWatchdog != null) {
120 mWatchdog.logEvent("Clear botton test...");
121 mWatchdog.start();
122 }
123 }
124}