blob: 11f2020054ffc9d3672dd0c40e75b808f981f023 [file] [log] [blame]
Winson Chungde34aa42015-05-07 18:21:28 -07001/*
2 * Copyright (C) 2015 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 */
Winson97b0d082015-08-13 15:18:25 -070016package com.android.launcher3;
Winson Chungde34aa42015-05-07 18:21:28 -070017
Anushree Ganjama4616602022-07-25 22:20:18 +000018import static com.android.launcher3.logging.KeyboardStateManager.KeyboardState.SHOW;
Sunny Goyal59969372021-05-06 12:11:44 -070019
Winson Chungde34aa42015-05-07 18:21:28 -070020import android.content.Context;
Sunny Goyal326403e2017-10-02 12:45:10 -070021import android.text.TextUtils;
Winson Chungde34aa42015-05-07 18:21:28 -070022import android.util.AttributeSet;
Winson4e7a1012015-08-13 16:47:55 -070023import android.view.DragEvent;
Winson Chungde34aa42015-05-07 18:21:28 -070024import android.view.KeyEvent;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070025import android.view.inputmethod.InputMethodManager;
Winson Chungde34aa42015-05-07 18:21:28 -070026import android.widget.EditText;
27
Sunny Goyal59969372021-05-06 12:11:44 -070028import com.android.launcher3.views.ActivityContext;
Sunny Goyal326403e2017-10-02 12:45:10 -070029
Winson Chungde34aa42015-05-07 18:21:28 -070030
31/**
Winson97b0d082015-08-13 15:18:25 -070032 * The edit text that reports back when the back key has been pressed.
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -080033 * Note: AppCompatEditText doesn't fully support #displayCompletions and #onCommitCompletion
Winson Chungde34aa42015-05-07 18:21:28 -070034 */
Winson97b0d082015-08-13 15:18:25 -070035public class ExtendedEditText extends EditText {
Jon Miranda54d4e642017-02-24 10:00:14 -080036 private boolean mForceDisableSuggestions = false;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070037
Winson Chungde34aa42015-05-07 18:21:28 -070038 /**
39 * Implemented by listeners of the back key.
40 */
41 public interface OnBackKeyListener {
Hyunyoung Song54d1f882020-01-10 23:37:16 -080042 boolean onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070043 }
44
45 private OnBackKeyListener mBackKeyListener;
46
Winson97b0d082015-08-13 15:18:25 -070047 public ExtendedEditText(Context context) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070048 // ctor chaining breaks the touch handling
49 super(context);
Winson Chungde34aa42015-05-07 18:21:28 -070050 }
51
Winson97b0d082015-08-13 15:18:25 -070052 public ExtendedEditText(Context context, AttributeSet attrs) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070053 // ctor chaining breaks the touch handling
54 super(context, attrs);
Winson Chungde34aa42015-05-07 18:21:28 -070055 }
56
Winson97b0d082015-08-13 15:18:25 -070057 public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chungde34aa42015-05-07 18:21:28 -070058 super(context, attrs, defStyleAttr);
59 }
60
61 public void setOnBackKeyListener(OnBackKeyListener listener) {
62 mBackKeyListener = listener;
63 }
64
65 @Override
66 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
67 // If this is a back key, propagate the key back to the listener
68 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
Hyunyoung Song011f4382021-06-25 01:18:34 -070069 if (TextUtils.isEmpty(getText())) {
70 hideKeyboard();
71 }
Winson Chungde34aa42015-05-07 18:21:28 -070072 if (mBackKeyListener != null) {
Winson97b0d082015-08-13 15:18:25 -070073 return mBackKeyListener.onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070074 }
75 return false;
76 }
77 return super.onKeyPreIme(keyCode, event);
78 }
Winson4e7a1012015-08-13 16:47:55 -070079
80 @Override
81 public boolean onDragEvent(DragEvent event) {
82 // We don't want this view to interfere with Launcher own drag and drop.
83 return false;
84 }
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070085
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070086 public void showKeyboard() {
Anushree Ganjama4616602022-07-25 22:20:18 +000087 onKeyboardShown();
88 showSoftInput();
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070089 }
90
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -070091 public void hideKeyboard() {
Sunny Goyal8958a702022-09-09 15:54:10 -070092 ActivityContext.lookupContext(getContext()).hideKeyboard();
hyunyoungsd7a02ec2022-06-03 00:47:04 -070093 clearFocus();
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -070094 }
95
Anushree Ganjama4616602022-07-25 22:20:18 +000096 protected void onKeyboardShown() {
97 ActivityContext.lookupContext(getContext()).getStatsLogManager()
98 .keyboardStateManager().setKeyboardState(SHOW);
99 }
100
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700101 private boolean showSoftInput() {
102 return requestFocus() &&
Hyunyoung Song5fcd8f92019-11-06 21:34:36 -0800103 getContext().getSystemService(InputMethodManager.class)
Hyunyoung Song7e83ab92016-09-21 17:03:56 -0700104 .showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700105 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700106
107 public void dispatchBackKey() {
Mehdi Alizadeh25b4dfe2018-06-05 16:22:35 -0700108 hideKeyboard();
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700109 if (mBackKeyListener != null) {
110 mBackKeyListener.onBackKey();
111 }
112 }
Jon Miranda54d4e642017-02-24 10:00:14 -0800113
114 /**
115 * Set to true when you want isSuggestionsEnabled to return false.
116 * Use this to disable the red underlines that appear under typos when suggestions is enabled.
117 */
118 public void forceDisableSuggestions(boolean forceDisableSuggestions) {
119 mForceDisableSuggestions = forceDisableSuggestions;
120 }
121
122 @Override
123 public boolean isSuggestionsEnabled() {
124 return !mForceDisableSuggestions && super.isSuggestionsEnabled();
125 }
Sunny Goyal326403e2017-10-02 12:45:10 -0700126
127 public void reset() {
128 if (!TextUtils.isEmpty(getText())) {
129 setText("");
Hyunyoung Songa907a992021-03-10 10:18:03 -0800130 }
Sunny Goyal326403e2017-10-02 12:45:10 -0700131 }
Winson Chungde34aa42015-05-07 18:21:28 -0700132}