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.testing; 18 19 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; 20 import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; 21 22 import android.app.Activity; 23 import android.app.Instrumentation; 24 import android.content.Intent; 25 import android.os.Bundle; 26 import android.util.Log; 27 import android.view.WindowInsets; 28 import android.view.WindowInsetsController; 29 import android.view.WindowManager; 30 import android.view.inputmethod.InputMethodManager; 31 import android.widget.EditText; 32 import android.widget.LinearLayout; 33 34 import androidx.annotation.Nullable; 35 36 import java.lang.ref.WeakReference; 37 38 /** 39 * A special activity for testing purpose. 40 * 41 * <p>This is used when the instruments package is SimpleTestIme, as the Intent needs to be started 42 * in the instruments package. More details see {@link 43 * Instrumentation#startActivitySync(Intent)}.</> 44 */ 45 public class TestActivity extends Activity { 46 private static final String TAG = "TestActivity"; 47 private static WeakReference<TestActivity> sLastCreatedInstance = 48 new WeakReference<>(null); 49 50 /** 51 * Start a new test activity with an editor and wait for it to begin running before returning. 52 * 53 * @param instrumentation application instrumentation 54 * @return the newly started activity 55 */ start(Instrumentation instrumentation)56 public static TestActivity start(Instrumentation instrumentation) { 57 Intent intent = 58 new Intent() 59 .setAction(Intent.ACTION_MAIN) 60 .setClass(instrumentation.getTargetContext(), TestActivity.class) 61 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 62 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 63 return (TestActivity) instrumentation.startActivitySync(intent); 64 } 65 66 private EditText mEditText; 67 getEditText()68 public EditText getEditText() { 69 return mEditText; 70 } 71 72 @Override onCreate(Bundle savedInstanceState)73 protected void onCreate(Bundle savedInstanceState) { 74 super.onCreate(savedInstanceState); 75 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 76 LinearLayout rootView = new LinearLayout(this); 77 mEditText = new EditText(this); 78 mEditText.setContentDescription("Input box"); 79 rootView.addView(mEditText, new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)); 80 rootView.setFitsSystemWindows(true); 81 setContentView(rootView); 82 mEditText.requestFocus(); 83 sLastCreatedInstance = new WeakReference<>(this); 84 } 85 86 /** Get the last created TestActivity instance. */ getLastCreatedInstance()87 public static @Nullable TestActivity getLastCreatedInstance() { 88 return sLastCreatedInstance.get(); 89 } 90 91 /** Shows soft keyboard via InputMethodManager. */ showImeWithInputMethodManager(int flags)92 public boolean showImeWithInputMethodManager(int flags) { 93 InputMethodManager imm = getSystemService(InputMethodManager.class); 94 boolean result = imm.showSoftInput(mEditText, flags); 95 Log.i(TAG, "showIme() via InputMethodManager, result=" + result); 96 return result; 97 } 98 99 /** Shows soft keyboard via WindowInsetsController. */ showImeWithWindowInsetsController()100 public boolean showImeWithWindowInsetsController() { 101 WindowInsetsController windowInsetsController = mEditText.getWindowInsetsController(); 102 windowInsetsController.show(WindowInsets.Type.ime()); 103 Log.i(TAG, "showIme() via WindowInsetsController"); 104 return true; 105 } 106 107 /** Hides soft keyboard via InputMethodManager. */ hideImeWithInputMethodManager(int flags)108 public boolean hideImeWithInputMethodManager(int flags) { 109 InputMethodManager imm = getSystemService(InputMethodManager.class); 110 boolean result = imm.hideSoftInputFromWindow(mEditText.getWindowToken(), flags); 111 Log.i(TAG, "hideIme() via InputMethodManager, result=" + result); 112 return result; 113 } 114 115 /** Hides soft keyboard via WindowInsetsController. */ hideImeWithWindowInsetsController()116 public boolean hideImeWithWindowInsetsController() { 117 WindowInsetsController windowInsetsController = mEditText.getWindowInsetsController(); 118 windowInsetsController.hide(WindowInsets.Type.ime()); 119 Log.i(TAG, "hideIme() via WindowInsetsController"); 120 return true; 121 } 122 } 123