blob: d05673cccfb7197e34d89fcff808467a3b8a48fb [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
18import android.content.Context;
19import android.util.AttributeSet;
Winson4e7a1012015-08-13 16:47:55 -070020import android.view.DragEvent;
Winson Chungde34aa42015-05-07 18:21:28 -070021import android.view.KeyEvent;
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070022import android.view.inputmethod.InputMethodManager;
Winson Chungde34aa42015-05-07 18:21:28 -070023import android.widget.EditText;
24
25
26/**
Winson97b0d082015-08-13 15:18:25 -070027 * The edit text that reports back when the back key has been pressed.
Winson Chungde34aa42015-05-07 18:21:28 -070028 */
Winson97b0d082015-08-13 15:18:25 -070029public class ExtendedEditText extends EditText {
Winson Chungde34aa42015-05-07 18:21:28 -070030
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070031 private boolean mShowImeAfterFirstLayout;
32
Winson Chungde34aa42015-05-07 18:21:28 -070033 /**
34 * Implemented by listeners of the back key.
35 */
36 public interface OnBackKeyListener {
Winson97b0d082015-08-13 15:18:25 -070037 public boolean onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070038 }
39
40 private OnBackKeyListener mBackKeyListener;
41
Winson97b0d082015-08-13 15:18:25 -070042 public ExtendedEditText(Context context) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070043 // ctor chaining breaks the touch handling
44 super(context);
Winson Chungde34aa42015-05-07 18:21:28 -070045 }
46
Winson97b0d082015-08-13 15:18:25 -070047 public ExtendedEditText(Context context, AttributeSet attrs) {
Hyunyoung Song3f9d6472016-09-20 12:37:41 -070048 // ctor chaining breaks the touch handling
49 super(context, attrs);
Winson Chungde34aa42015-05-07 18:21:28 -070050 }
51
Winson97b0d082015-08-13 15:18:25 -070052 public ExtendedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chungde34aa42015-05-07 18:21:28 -070053 super(context, attrs, defStyleAttr);
54 }
55
56 public void setOnBackKeyListener(OnBackKeyListener listener) {
57 mBackKeyListener = listener;
58 }
59
60 @Override
61 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
62 // If this is a back key, propagate the key back to the listener
63 if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
64 if (mBackKeyListener != null) {
Winson97b0d082015-08-13 15:18:25 -070065 return mBackKeyListener.onBackKey();
Winson Chungde34aa42015-05-07 18:21:28 -070066 }
67 return false;
68 }
69 return super.onKeyPreIme(keyCode, event);
70 }
Winson4e7a1012015-08-13 16:47:55 -070071
72 @Override
73 public boolean onDragEvent(DragEvent event) {
74 // We don't want this view to interfere with Launcher own drag and drop.
75 return false;
76 }
Hyunyoung Songc2fe1142016-09-09 15:02:20 -070077
78 @Override
79 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
80 super.onLayout(changed, left, top, right, bottom);
81 if (mShowImeAfterFirstLayout) {
82 // soft input only shows one frame after the layout of the EditText happens,
83 post(new Runnable() {
84 @Override
85 public void run() {
86 showSoftInput();
87 mShowImeAfterFirstLayout = false;
88 }
89 });
90 }
91 }
92
93 public void showKeyboard() {
94 mShowImeAfterFirstLayout = !showSoftInput();
95 }
96
97 private boolean showSoftInput() {
98 return requestFocus() &&
99 ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
Hyunyoung Song7e83ab92016-09-21 17:03:56 -0700100 .showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
Hyunyoung Songc2fe1142016-09-09 15:02:20 -0700101 }
Sunny Goyal740ac7f2016-09-28 16:47:32 -0700102
103 public void dispatchBackKey() {
104 ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
105 .hideSoftInputFromWindow(getWindowToken(), 0);
106 if (mBackKeyListener != null) {
107 mBackKeyListener.onBackKey();
108 }
109 }
Winson Chungde34aa42015-05-07 18:21:28 -0700110}