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.WindowManager; 29 import android.view.inputmethod.InputMethodManager; 30 import android.widget.EditText; 31 import android.widget.LinearLayout; 32 33 import androidx.annotation.NonNull; 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 final class TestActivity extends Activity { 46 47 private static final String TAG = "TestActivity"; 48 49 /** Last created instance of this activity. */ 50 @NonNull 51 private static WeakReference<TestActivity> sInstance = new WeakReference<>(null); 52 53 private EditText mEditText; 54 55 @Override onCreate(@ullable Bundle savedInstanceState)56 protected void onCreate(@Nullable Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 59 final var rootView = new LinearLayout(this); 60 mEditText = new EditText(this); 61 mEditText.setHint("editText"); 62 rootView.addView(mEditText, new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)); 63 rootView.setFitsSystemWindows(true); 64 setContentView(rootView); 65 mEditText.requestFocus(); 66 sInstance = new WeakReference<>(this); 67 } 68 getEditText()69 public EditText getEditText() { 70 return mEditText; 71 } 72 73 /** Shows soft keyboard via InputMethodManager. */ showImeWithInputMethodManager(int flags)74 public boolean showImeWithInputMethodManager(int flags) { 75 final var imm = getSystemService(InputMethodManager.class); 76 final boolean result = imm.showSoftInput(mEditText, flags); 77 Log.i(TAG, "showIme() via InputMethodManager, result=" + result); 78 return result; 79 } 80 81 /** Shows soft keyboard via WindowInsetsController. */ showImeWithWindowInsetsController()82 public void showImeWithWindowInsetsController() { 83 final var controller = mEditText.getWindowInsetsController(); 84 controller.show(WindowInsets.Type.ime()); 85 Log.i(TAG, "showIme() via WindowInsetsController"); 86 } 87 88 /** Hides soft keyboard via InputMethodManager. */ hideImeWithInputMethodManager(int flags)89 public boolean hideImeWithInputMethodManager(int flags) { 90 final var imm = getSystemService(InputMethodManager.class); 91 final boolean result = imm.hideSoftInputFromWindow(mEditText.getWindowToken(), flags); 92 Log.i(TAG, "hideIme() via InputMethodManager, result=" + result); 93 return result; 94 } 95 96 /** Hides soft keyboard via WindowInsetsController. */ hideImeWithWindowInsetsController()97 public void hideImeWithWindowInsetsController() { 98 final var controller = mEditText.getWindowInsetsController(); 99 controller.hide(WindowInsets.Type.ime()); 100 Log.i(TAG, "hideIme() via WindowInsetsController"); 101 } 102 103 /** Gets the last created instance of this activity, if available. */ 104 @Nullable getInstance()105 public static TestActivity getInstance() { 106 return sInstance.get(); 107 } 108 109 /** 110 * Start a new test activity with an editor and wait for it to begin running before returning. 111 * 112 * @param instrumentation application instrumentation. 113 * @return the newly started activity. 114 */ 115 @NonNull startSync(@onNull Instrumentation instrumentation)116 public static TestActivity startSync(@NonNull Instrumentation instrumentation) { 117 final var intent = new Intent() 118 .setAction(Intent.ACTION_MAIN) 119 .setClass(instrumentation.getTargetContext(), TestActivity.class) 120 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 121 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 122 return (TestActivity) instrumentation.startActivitySync(intent); 123 } 124 } 125