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