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 setContentView(rootView); 81 mEditText.requestFocus(); 82 sLastCreatedInstance = new WeakReference<>(this); 83 } 84 85 /** Get the last created TestActivity instance. */ getLastCreatedInstance()86 public static @Nullable TestActivity getLastCreatedInstance() { 87 return sLastCreatedInstance.get(); 88 } 89 90 /** Shows soft keyboard via InputMethodManager. */ showImeWithInputMethodManager(int flags)91 public boolean showImeWithInputMethodManager(int flags) { 92 InputMethodManager imm = getSystemService(InputMethodManager.class); 93 boolean result = imm.showSoftInput(mEditText, flags); 94 Log.i(TAG, "showIme() via InputMethodManager, result=" + result); 95 return result; 96 } 97 98 /** Shows soft keyboard via WindowInsetsController. */ showImeWithWindowInsetsController()99 public boolean showImeWithWindowInsetsController() { 100 WindowInsetsController windowInsetsController = mEditText.getWindowInsetsController(); 101 windowInsetsController.show(WindowInsets.Type.ime()); 102 Log.i(TAG, "showIme() via WindowInsetsController"); 103 return true; 104 } 105 106 /** Hides soft keyboard via InputMethodManager. */ hideImeWithInputMethodManager(int flags)107 public boolean hideImeWithInputMethodManager(int flags) { 108 InputMethodManager imm = getSystemService(InputMethodManager.class); 109 boolean result = imm.hideSoftInputFromWindow(mEditText.getWindowToken(), flags); 110 Log.i(TAG, "hideIme() via InputMethodManager, result=" + result); 111 return result; 112 } 113 114 /** Hides soft keyboard via WindowInsetsController. */ hideImeWithWindowInsetsController()115 public boolean hideImeWithWindowInsetsController() { 116 WindowInsetsController windowInsetsController = mEditText.getWindowInsetsController(); 117 windowInsetsController.hide(WindowInsets.Type.ime()); 118 Log.i(TAG, "hideIme() via WindowInsetsController"); 119 return true; 120 } 121 } 122