• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
16 package com.android.launcher3;
17 
18 import android.content.Context;
19 import android.text.TextUtils;
20 import android.util.AttributeSet;
21 import android.view.DragEvent;
22 import android.view.KeyEvent;
23 import android.view.View;
24 import android.view.inputmethod.InputMethodManager;
25 import android.widget.EditText;
26 
27 import com.android.launcher3.util.UiThreadHelper;
28 
29 
30 /**
31  * The edit text that reports back when the back key has been pressed.
32  */
33 public class ExtendedEditText extends EditText {
34 
35     private boolean mShowImeAfterFirstLayout;
36     private boolean mForceDisableSuggestions = false;
37 
38     /**
39      * Implemented by listeners of the back key.
40      */
41     public interface OnBackKeyListener {
onBackKey()42         public boolean onBackKey();
43     }
44 
45     private OnBackKeyListener mBackKeyListener;
46 
ExtendedEditText(Context context)47     public ExtendedEditText(Context context) {
48         // ctor chaining breaks the touch handling
49         super(context);
50     }
51 
ExtendedEditText(Context context, AttributeSet attrs)52     public ExtendedEditText(Context context, AttributeSet attrs) {
53         // ctor chaining breaks the touch handling
54         super(context, attrs);
55     }
56 
ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr)57     public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
58         super(context, attrs, defStyleAttr);
59     }
60 
setOnBackKeyListener(OnBackKeyListener listener)61     public void setOnBackKeyListener(OnBackKeyListener listener) {
62         mBackKeyListener = listener;
63     }
64 
65     @Override
onKeyPreIme(int keyCode, KeyEvent event)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) {
69             if (mBackKeyListener != null) {
70                 return mBackKeyListener.onBackKey();
71             }
72             return false;
73         }
74         return super.onKeyPreIme(keyCode, event);
75     }
76 
77     @Override
onDragEvent(DragEvent event)78     public boolean onDragEvent(DragEvent event) {
79         // We don't want this view to interfere with Launcher own drag and drop.
80         return false;
81     }
82 
83     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)84     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
85         super.onLayout(changed, left, top, right, bottom);
86         if (mShowImeAfterFirstLayout) {
87             // soft input only shows one frame after the layout of the EditText happens,
88             post(new Runnable() {
89                 @Override
90                 public void run() {
91                     showSoftInput();
92                     mShowImeAfterFirstLayout = false;
93                 }
94             });
95         }
96     }
97 
showKeyboard()98     public void showKeyboard() {
99         mShowImeAfterFirstLayout = !showSoftInput();
100     }
101 
hideKeyboard()102     public void hideKeyboard() {
103         UiThreadHelper.hideKeyboardAsync(getContext(), getWindowToken());
104     }
105 
showSoftInput()106     private boolean showSoftInput() {
107         return requestFocus() &&
108                 ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
109                     .showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
110     }
111 
dispatchBackKey()112     public void dispatchBackKey() {
113         hideKeyboard();
114         if (mBackKeyListener != null) {
115             mBackKeyListener.onBackKey();
116         }
117     }
118 
119     /**
120      * Set to true when you want isSuggestionsEnabled to return false.
121      * Use this to disable the red underlines that appear under typos when suggestions is enabled.
122      */
forceDisableSuggestions(boolean forceDisableSuggestions)123     public void forceDisableSuggestions(boolean forceDisableSuggestions) {
124         mForceDisableSuggestions = forceDisableSuggestions;
125     }
126 
127     @Override
isSuggestionsEnabled()128     public boolean isSuggestionsEnabled() {
129         return !mForceDisableSuggestions && super.isSuggestionsEnabled();
130     }
131 
reset()132     public void reset() {
133         if (!TextUtils.isEmpty(getText())) {
134             setText("");
135         }
136         if (isFocused()) {
137             View nextFocus = focusSearch(View.FOCUS_DOWN);
138             if (nextFocus != null) {
139                 nextFocus.requestFocus();
140             }
141         }
142         hideKeyboard();
143     }
144 }
145