1 /*
2  * Copyright (C) 2018 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 com.android.tv.settings.connectivity.util;
18 
19 import android.content.Context;
20 import android.graphics.Rect;
21 import android.util.AttributeSet;
22 import android.view.KeyEvent;
23 import android.view.accessibility.AccessibilityNodeInfo;
24 import android.widget.EditText;
25 import android.widget.TextView;
26 
27 import androidx.leanback.widget.ImeKeyMonitor;
28 
29 /**
30  * Mostly copied from {@GuidedActionEditText}, remove some code to satisfies TvSettings need.
31  */
32 public class SettingsGuidedActionEditText extends EditText implements ImeKeyMonitor {
33     private ImeKeyMonitor.ImeKeyListener mKeyListener;
34 
SettingsGuidedActionEditText(Context context)35     public SettingsGuidedActionEditText(Context context) {
36         super(context);
37     }
38 
SettingsGuidedActionEditText(Context context, AttributeSet attrs)39     public SettingsGuidedActionEditText(Context context, AttributeSet attrs) {
40         super(context, attrs);
41     }
42 
SettingsGuidedActionEditText(Context context, AttributeSet attrs, int defStyle)43     public SettingsGuidedActionEditText(Context context, AttributeSet attrs, int defStyle) {
44         super(context, attrs, defStyle);
45     }
46 
47     @Override
setImeKeyListener(ImeKeyMonitor.ImeKeyListener listener)48     public void setImeKeyListener(ImeKeyMonitor.ImeKeyListener listener) {
49         mKeyListener = listener;
50     }
51 
52     @Override
onKeyPreIme(int keyCode, KeyEvent event)53     public boolean onKeyPreIme(int keyCode, KeyEvent event) {
54         boolean result = false;
55         if (mKeyListener != null) {
56             result = mKeyListener.onKeyPreIme(this, keyCode, event);
57         }
58         if (!result) {
59             result = super.onKeyPreIme(keyCode, event);
60         }
61         return result;
62     }
63 
64     @Override
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)65     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
66         super.onInitializeAccessibilityNodeInfo(info);
67         info.setClassName(isFocused() ? EditText.class.getName() : TextView.class.getName());
68     }
69 
70     @Override
onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)71     protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
72         super.onFocusChanged(focused, direction, previouslyFocusedRect);
73         // Make the TextView focusable during editing, avoid the TextView gets accessibility focus
74         // before editing started. see also GuidedActionAdapterGroup where setFocusable(true).
75         if (!focused) {
76             setFocusable(false);
77         }
78     }
79 }
80