• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package android.support.v17.leanback.widget;
15 
16 import android.content.Context;
17 import android.graphics.Canvas;
18 import android.graphics.ColorFilter;
19 import android.graphics.PixelFormat;
20 import android.graphics.Rect;
21 import android.graphics.drawable.Drawable;
22 import android.util.AttributeSet;
23 import android.view.KeyEvent;
24 import android.view.accessibility.AccessibilityNodeInfo;
25 import android.widget.EditText;
26 import android.widget.TextView;
27 
28 /**
29  * A custom EditText that satisfies the IME key monitoring requirements of GuidedStepFragment.
30  */
31 public class GuidedActionEditText extends EditText implements ImeKeyMonitor {
32 
33     /**
34      * Workaround for b/26990627 forcing recompute the padding for the View when we turn on/off
35      * the default background of EditText
36      */
37     static final class NoPaddingDrawable extends Drawable {
38         @Override
getPadding(Rect padding)39         public boolean getPadding(Rect padding) {
40             padding.set(0, 0, 0, 0);
41             return true;
42         }
43 
44         @Override
draw(Canvas canvas)45         public void draw(Canvas canvas) {
46         }
47 
48         @Override
setAlpha(int alpha)49         public void setAlpha(int alpha) {
50         }
51 
52         @Override
setColorFilter(ColorFilter colorFilter)53         public void setColorFilter(ColorFilter colorFilter) {
54         }
55 
56         @Override
getOpacity()57         public int getOpacity() {
58             return PixelFormat.TRANSPARENT;
59         }
60     }
61 
62     private ImeKeyListener mKeyListener;
63     private final Drawable mSavedBackground;
64     private final Drawable mNoPaddingDrawable;
65 
GuidedActionEditText(Context ctx)66     public GuidedActionEditText(Context ctx) {
67         this(ctx, null);
68     }
69 
GuidedActionEditText(Context ctx, AttributeSet attrs)70     public GuidedActionEditText(Context ctx, AttributeSet attrs) {
71         this(ctx, attrs, android.R.attr.editTextStyle);
72     }
73 
GuidedActionEditText(Context ctx, AttributeSet attrs, int defStyleAttr)74     public GuidedActionEditText(Context ctx, AttributeSet attrs, int defStyleAttr) {
75         super(ctx, attrs, defStyleAttr);
76         mSavedBackground = getBackground();
77         mNoPaddingDrawable = new NoPaddingDrawable();
78         setBackground(mNoPaddingDrawable);
79     }
80 
81     @Override
setImeKeyListener(ImeKeyListener listener)82     public void setImeKeyListener(ImeKeyListener listener) {
83         mKeyListener = listener;
84     }
85 
86     @Override
onKeyPreIme(int keyCode, KeyEvent event)87     public boolean onKeyPreIme(int keyCode, KeyEvent event) {
88         boolean result = false;
89         if (mKeyListener != null) {
90             result = mKeyListener.onKeyPreIme(this, keyCode, event);
91         }
92         if (!result) {
93             result = super.onKeyPreIme(keyCode, event);
94         }
95         return result;
96     }
97 
98     @Override
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)99     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
100         super.onInitializeAccessibilityNodeInfo(info);
101         info.setClassName(isFocused() ? EditText.class.getName() : TextView.class.getName());
102     }
103 
104     @Override
onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)105     protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
106         super.onFocusChanged(focused, direction, previouslyFocusedRect);
107         if (focused) {
108             setBackground(mSavedBackground);
109         } else {
110             setBackground(mNoPaddingDrawable);
111         }
112         // Make the TextView focusable during editing, avoid the TextView gets accessibility focus
113         // before editing started. see also GuidedActionAdapterGroup where setFocusable(true).
114         if (!focused) {
115             setFocusable(false);
116         }
117     }
118 }
119