1 /* 2 * Copyright (C) 2020 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 android.widget; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.View; 22 import android.view.inputmethod.EditorInfo; 23 import android.view.inputmethod.InputConnection; 24 import android.view.inputmethod.InputMethodManager; 25 26 /** 27 * EditText that keeps track of the IME state, specifically its input connection. This is useful 28 * for clients who request the IME before the system has established a connection. 29 * @hide 30 */ 31 public class ImeAwareEditText extends EditText { 32 private boolean mHasPendingShowSoftInputRequest; 33 final Runnable mRunShowSoftInputIfNecessary = () -> showSoftInputIfNecessary(); 34 ImeAwareEditText(Context context)35 public ImeAwareEditText(Context context) { 36 super(context, null); 37 } 38 ImeAwareEditText(Context context, AttributeSet attrs)39 public ImeAwareEditText(Context context, AttributeSet attrs) { 40 super(context, attrs); 41 } 42 ImeAwareEditText(Context context, AttributeSet attrs, int defStyleAttr)43 public ImeAwareEditText(Context context, AttributeSet attrs, int defStyleAttr) { 44 super(context, attrs, defStyleAttr); 45 } 46 ImeAwareEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)47 public ImeAwareEditText(Context context, AttributeSet attrs, int defStyleAttr, 48 int defStyleRes) { 49 super(context, attrs, defStyleAttr, defStyleRes); 50 } 51 52 /** 53 * This method is called back by the system when the system is about to establish a connection 54 * to the current input method. 55 * 56 * <p>This is a good and reliable signal to schedule a pending task to call 57 * {@link InputMethodManager#showSoftInput(View, int)}.</p> 58 * 59 * @param editorInfo context about the text input field. 60 * @return {@link InputConnection} to be passed to the input method. 61 */ 62 @Override onCreateInputConnection(EditorInfo editorInfo)63 public InputConnection onCreateInputConnection(EditorInfo editorInfo) { 64 final InputConnection ic = super.onCreateInputConnection(editorInfo); 65 if (mHasPendingShowSoftInputRequest) { 66 removeCallbacks(mRunShowSoftInputIfNecessary); 67 post(mRunShowSoftInputIfNecessary); 68 } 69 return ic; 70 } 71 showSoftInputIfNecessary()72 private void showSoftInputIfNecessary() { 73 if (mHasPendingShowSoftInputRequest) { 74 final InputMethodManager imm = 75 getContext().getSystemService(InputMethodManager.class); 76 imm.showSoftInput(this, 0); 77 mHasPendingShowSoftInputRequest = false; 78 } 79 } 80 scheduleShowSoftInput()81 public void scheduleShowSoftInput() { 82 final InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); 83 if (imm.isActive(this)) { 84 // This means that ImeAwareEditText is already connected to the IME. 85 // InputMethodManager#showSoftInput() is guaranteed to pass client-side focus check. 86 mHasPendingShowSoftInputRequest = false; 87 removeCallbacks(mRunShowSoftInputIfNecessary); 88 imm.showSoftInput(this, 0); 89 return; 90 } 91 92 // Otherwise, InputMethodManager#showSoftInput() should be deferred after 93 // onCreateInputConnection(). 94 mHasPendingShowSoftInputRequest = true; 95 } 96 } 97