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