1 /*
2  * Copyright 2024 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 androidx.pdf.widget;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.graphics.Rect;
22 import android.util.AttributeSet;
23 import android.view.inputmethod.InputMethodManager;
24 
25 import androidx.annotation.RestrictTo;
26 import androidx.appcompat.widget.AppCompatEditText;
27 import androidx.core.view.WindowCompat;
28 import androidx.core.view.WindowInsetsCompat;
29 
30 import org.jspecify.annotations.NonNull;
31 
32 /**
33  * EditText for search queries which shows/hides the keyboard on focus change.
34  */
35 @RestrictTo(RestrictTo.Scope.LIBRARY)
36 public class SearchEditText extends AppCompatEditText {
37     private static final String TAG = SearchEditText.class.getSimpleName();
38 
39     /**
40      * Runnable to show the keyboard asynchronously in case the UI is not fully initialized.
41      * Based on
42      * the platform SearchView.
43      */
44     private final Runnable mShowImeRunnable =
45             () -> {
46                 WindowCompat.getInsetsController(((Activity) getContext()).getWindow(), this)
47                         .show(WindowInsetsCompat.Type.ime());
48             };
49 
SearchEditText(@onNull Context context)50     public SearchEditText(@NonNull Context context) {
51         super(context);
52     }
53 
SearchEditText(@onNull Context context, @NonNull AttributeSet attrs)54     public SearchEditText(@NonNull Context context, @NonNull AttributeSet attrs) {
55         super(context, attrs);
56     }
57 
SearchEditText(@onNull Context context, @NonNull AttributeSet attrs, int defStyle)58     public SearchEditText(@NonNull Context context, @NonNull AttributeSet attrs, int defStyle) {
59         super(context, attrs, defStyle);
60     }
61 
62     @Override
onFocusChanged(boolean focused, int direction, @NonNull Rect previouslyFocusedRect)63     protected void onFocusChanged(boolean focused, int direction,
64             @NonNull Rect previouslyFocusedRect) {
65         super.onFocusChanged(focused, direction, previouslyFocusedRect);
66         if (focused) {
67             post(mShowImeRunnable);
68         } else {
69             InputMethodManager imm =
70                     (InputMethodManager) getContext().getSystemService(
71                             Context.INPUT_METHOD_SERVICE);
72             if (imm != null) {
73                 imm.hideSoftInputFromWindow(getWindowToken(), 0);
74             }
75         }
76     }
77 }
78