2015-05-07 18:21:28 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
2015-08-13 15:18:25 -07:00
|
|
|
package com.android.launcher3;
|
2015-05-07 18:21:28 -07:00
|
|
|
|
2022-07-25 22:20:18 +00:00
|
|
|
import static com.android.launcher3.logging.KeyboardStateManager.KeyboardState.SHOW;
|
2021-05-06 12:11:44 -07:00
|
|
|
|
2015-05-07 18:21:28 -07:00
|
|
|
import android.content.Context;
|
2022-11-17 13:04:59 +00:00
|
|
|
import android.graphics.Rect;
|
2017-10-02 12:45:10 -07:00
|
|
|
import android.text.TextUtils;
|
2015-05-07 18:21:28 -07:00
|
|
|
import android.util.AttributeSet;
|
2023-08-04 13:18:16 +00:00
|
|
|
import android.util.Log;
|
2015-08-13 16:47:55 -07:00
|
|
|
import android.view.DragEvent;
|
2015-05-07 18:21:28 -07:00
|
|
|
import android.view.KeyEvent;
|
2016-09-09 15:02:20 -07:00
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2015-05-07 18:21:28 -07:00
|
|
|
import android.widget.EditText;
|
|
|
|
|
|
2021-05-06 12:11:44 -07:00
|
|
|
import com.android.launcher3.views.ActivityContext;
|
2017-10-02 12:45:10 -07:00
|
|
|
|
2022-11-17 13:04:59 +00:00
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
2015-05-07 18:21:28 -07:00
|
|
|
|
|
|
|
|
/**
|
2015-08-13 15:18:25 -07:00
|
|
|
* The edit text that reports back when the back key has been pressed.
|
2019-11-06 21:34:36 -08:00
|
|
|
* Note: AppCompatEditText doesn't fully support #displayCompletions and #onCommitCompletion
|
2015-05-07 18:21:28 -07:00
|
|
|
*/
|
2015-08-13 15:18:25 -07:00
|
|
|
public class ExtendedEditText extends EditText {
|
2023-08-04 13:18:16 +00:00
|
|
|
private static final String TAG = "ExtendedEditText";
|
|
|
|
|
|
2022-11-17 13:04:59 +00:00
|
|
|
private final Set<OnFocusChangeListener> mOnFocusChangeListeners = new HashSet<>();
|
|
|
|
|
|
2017-02-24 10:00:14 -08:00
|
|
|
private boolean mForceDisableSuggestions = false;
|
2016-09-09 15:02:20 -07:00
|
|
|
|
2015-05-07 18:21:28 -07:00
|
|
|
/**
|
|
|
|
|
* Implemented by listeners of the back key.
|
|
|
|
|
*/
|
|
|
|
|
public interface OnBackKeyListener {
|
2020-01-10 23:37:16 -08:00
|
|
|
boolean onBackKey();
|
2015-05-07 18:21:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OnBackKeyListener mBackKeyListener;
|
|
|
|
|
|
2015-08-13 15:18:25 -07:00
|
|
|
public ExtendedEditText(Context context) {
|
2016-09-20 12:37:41 -07:00
|
|
|
// ctor chaining breaks the touch handling
|
|
|
|
|
super(context);
|
2015-05-07 18:21:28 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-13 15:18:25 -07:00
|
|
|
public ExtendedEditText(Context context, AttributeSet attrs) {
|
2016-09-20 12:37:41 -07:00
|
|
|
// ctor chaining breaks the touch handling
|
|
|
|
|
super(context, attrs);
|
2015-05-07 18:21:28 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-13 15:18:25 -07:00
|
|
|
public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
|
2015-05-07 18:21:28 -07:00
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setOnBackKeyListener(OnBackKeyListener listener) {
|
|
|
|
|
mBackKeyListener = listener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
|
|
|
|
|
// If this is a back key, propagate the key back to the listener
|
2023-08-25 16:56:30 -07:00
|
|
|
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP
|
|
|
|
|
&& mBackKeyListener != null) {
|
|
|
|
|
return mBackKeyListener.onBackKey();
|
2015-05-07 18:21:28 -07:00
|
|
|
}
|
|
|
|
|
return super.onKeyPreIme(keyCode, event);
|
|
|
|
|
}
|
2015-08-13 16:47:55 -07:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onDragEvent(DragEvent event) {
|
|
|
|
|
// We don't want this view to interfere with Launcher own drag and drop.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-09-09 15:02:20 -07:00
|
|
|
|
2023-08-04 13:18:16 +00:00
|
|
|
/**
|
|
|
|
|
* Synchronously shows the soft input method.
|
|
|
|
|
*
|
|
|
|
|
* @param shouldFocus whether this EditText should also request focus.
|
|
|
|
|
* @return true if the keyboard is shown correctly and focus is given to this view (if
|
|
|
|
|
* applicable).
|
|
|
|
|
*/
|
|
|
|
|
public boolean showKeyboard(boolean shouldFocus) {
|
2022-07-25 22:20:18 +00:00
|
|
|
onKeyboardShown();
|
2023-08-04 13:18:16 +00:00
|
|
|
boolean focusResult = !shouldFocus || requestFocus();
|
|
|
|
|
return focusResult && showSoftInputInternal();
|
2016-09-09 15:02:20 -07:00
|
|
|
}
|
|
|
|
|
|
2018-06-05 16:22:35 -07:00
|
|
|
public void hideKeyboard() {
|
2023-10-25 10:27:40 +01:00
|
|
|
hideKeyboard(/* clearFocus= */ true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void hideKeyboard(boolean clearFocus) {
|
2022-09-09 15:54:10 -07:00
|
|
|
ActivityContext.lookupContext(getContext()).hideKeyboard();
|
2023-10-25 10:27:40 +01:00
|
|
|
if (clearFocus) {
|
|
|
|
|
clearFocus();
|
|
|
|
|
}
|
2018-06-05 16:22:35 -07:00
|
|
|
}
|
|
|
|
|
|
2022-07-25 22:20:18 +00:00
|
|
|
protected void onKeyboardShown() {
|
|
|
|
|
ActivityContext.lookupContext(getContext()).getStatsLogManager()
|
|
|
|
|
.keyboardStateManager().setKeyboardState(SHOW);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 13:18:16 +00:00
|
|
|
private boolean showSoftInputInternal() {
|
|
|
|
|
boolean result = false;
|
|
|
|
|
InputMethodManager imm = getContext().getSystemService(InputMethodManager.class);
|
|
|
|
|
if (imm != null) {
|
|
|
|
|
result = imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
|
|
|
|
|
} else {
|
|
|
|
|
Log.w(TAG, "Failed to retrieve InputMethodManager from the system.");
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2016-09-09 15:02:20 -07:00
|
|
|
}
|
2016-09-28 16:47:32 -07:00
|
|
|
|
|
|
|
|
public void dispatchBackKey() {
|
2018-06-05 16:22:35 -07:00
|
|
|
hideKeyboard();
|
2016-09-28 16:47:32 -07:00
|
|
|
if (mBackKeyListener != null) {
|
|
|
|
|
mBackKeyListener.onBackKey();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-24 10:00:14 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set to true when you want isSuggestionsEnabled to return false.
|
|
|
|
|
* Use this to disable the red underlines that appear under typos when suggestions is enabled.
|
|
|
|
|
*/
|
|
|
|
|
public void forceDisableSuggestions(boolean forceDisableSuggestions) {
|
|
|
|
|
mForceDisableSuggestions = forceDisableSuggestions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isSuggestionsEnabled() {
|
|
|
|
|
return !mForceDisableSuggestions && super.isSuggestionsEnabled();
|
|
|
|
|
}
|
2017-10-02 12:45:10 -07:00
|
|
|
|
|
|
|
|
public void reset() {
|
|
|
|
|
if (!TextUtils.isEmpty(getText())) {
|
|
|
|
|
setText("");
|
2021-03-10 10:18:03 -08:00
|
|
|
}
|
2017-10-02 12:45:10 -07:00
|
|
|
}
|
2022-11-17 13:04:59 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method should be preferred to {@link #setOnFocusChangeListener(OnFocusChangeListener)},
|
|
|
|
|
* as it allows for multiple listeners from different sources.
|
|
|
|
|
*/
|
|
|
|
|
public void addOnFocusChangeListener(OnFocusChangeListener listener) {
|
|
|
|
|
mOnFocusChangeListeners.add(listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes the given listener from the set of registered focus listeners, or does nothing if it
|
|
|
|
|
* wasn't registered in the first place.
|
|
|
|
|
*/
|
|
|
|
|
public void removeOnFocusChangeListener(OnFocusChangeListener listener) {
|
|
|
|
|
mOnFocusChangeListeners.remove(listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
|
|
|
|
|
super.onFocusChanged(focused, direction, previouslyFocusedRect);
|
|
|
|
|
for (OnFocusChangeListener listener : mOnFocusChangeListeners) {
|
|
|
|
|
listener.onFocusChange(this, focused);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-08 14:05:37 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save the input, suggestion, hint states when it's on focus, and set to unfocused states.
|
|
|
|
|
*/
|
|
|
|
|
public void saveFocusedStateAndUpdateToUnfocusedState() {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Restore to the previous saved focused state.
|
|
|
|
|
*/
|
|
|
|
|
public void restoreToFocusedState() {}
|
2015-05-07 18:21:28 -07:00
|
|
|
}
|