GuidedActionEditText.java revision 0e7ad46e829a72558e9e256895fd6d46bbbb60b2
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 */
14package android.support.v17.leanback.widget;
15
16import android.content.Context;
17import android.graphics.Canvas;
18import android.graphics.ColorFilter;
19import android.graphics.PixelFormat;
20import android.graphics.Rect;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.widget.EditText;
24import android.view.KeyEvent;
25import android.view.accessibility.AccessibilityNodeInfo;
26
27/**
28 * A custom EditText that satisfies the IME key monitoring requirements of GuidedStepFragment.
29 */
30public class GuidedActionEditText extends EditText implements ImeKeyMonitor {
31
32    /**
33     * Workaround for b/26990627 forcing recompute the padding for the View when we turn on/off
34     * the default background of EditText
35     */
36    static final class NoPaddingDrawable extends Drawable {
37        @Override
38        public boolean getPadding(Rect padding) {
39            padding.set(0, 0, 0, 0);
40            return true;
41        }
42
43        @Override
44        public void draw(Canvas canvas) {
45        }
46
47        @Override
48        public void setAlpha(int alpha) {
49        }
50
51        @Override
52        public void setColorFilter(ColorFilter colorFilter) {
53        }
54
55        @Override
56        public int getOpacity() {
57            return PixelFormat.TRANSPARENT;
58        }
59    }
60
61    private ImeKeyListener mKeyListener;
62    private final Drawable mSavedBackground;
63    private final Drawable mNoPaddingDrawable;
64
65    public GuidedActionEditText(Context ctx) {
66        this(ctx, null);
67    }
68
69    public GuidedActionEditText(Context ctx, AttributeSet attrs) {
70        this(ctx, attrs, android.R.attr.editTextStyle);
71    }
72
73    public GuidedActionEditText(Context ctx, AttributeSet attrs, int defStyleAttr) {
74        super(ctx, attrs, defStyleAttr);
75        mSavedBackground = getBackground();
76        mNoPaddingDrawable = new NoPaddingDrawable();
77        setBackground(mNoPaddingDrawable);
78    }
79
80    @Override
81    public void setImeKeyListener(ImeKeyListener listener) {
82        mKeyListener = listener;
83    }
84
85    @Override
86    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
87        boolean result = false;
88        if (mKeyListener != null) {
89            result = mKeyListener.onKeyPreIme(this, keyCode, event);
90        }
91        if (!result) {
92            result = super.onKeyPreIme(keyCode, event);
93        }
94        return result;
95    }
96
97    @Override
98    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
99        super.onInitializeAccessibilityNodeInfo(info);
100        // Dont let the TextView gets accessibility focus if it's not focused.
101        if (!isFocused()) {
102            info.setVisibleToUser(false);
103        }
104    }
105
106    @Override
107    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
108        super.onFocusChanged(focused, direction, previouslyFocusedRect);
109        if (focused) {
110            setBackground(mSavedBackground);
111        } else {
112            setBackground(mNoPaddingDrawable);
113        }
114        // Make the TextView focusable during editing, avoid the TextView gets accessibility focus
115        // before editing started. see also GuidedActionAdapterGroup where setFocusable(true).
116        if (!focused) {
117            setFocusable(false);
118        }
119    }
120}
121