GuidedActionEditText.java revision 6626b899cb2565105f20e4ee2060a5104826d1dd
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.Rect;
18import android.util.AttributeSet;
19import android.widget.EditText;
20import android.view.KeyEvent;
21import android.view.accessibility.AccessibilityNodeInfo;
22
23/**
24 * A custom EditText that satisfies the IME key monitoring requirements of GuidedStepFragment.
25 */
26public class GuidedActionEditText extends EditText implements ImeKeyMonitor {
27
28    private ImeKeyListener mKeyListener;
29
30    public GuidedActionEditText(Context ctx) {
31        this(ctx, null);
32    }
33
34    public GuidedActionEditText(Context ctx, AttributeSet attrs) {
35        this(ctx, attrs, android.R.attr.editTextStyle);
36    }
37
38    public GuidedActionEditText(Context ctx, AttributeSet attrs, int defStyleAttr) {
39        super(ctx, attrs, defStyleAttr);
40    }
41
42    @Override
43    public void setImeKeyListener(ImeKeyListener listener) {
44        mKeyListener = listener;
45    }
46
47    @Override
48    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
49        boolean result = false;
50        if (mKeyListener != null) {
51            result = mKeyListener.onKeyPreIme(this, keyCode, event);
52        }
53        if (!result) {
54            result = super.onKeyPreIme(keyCode, event);
55        }
56        return result;
57    }
58
59    @Override
60    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
61        super.onInitializeAccessibilityNodeInfo(info);
62        // Dont let the TextView gets accessibility focus if it's not focused.
63        if (!isFocused()) {
64            info.setVisibleToUser(false);
65        }
66    }
67
68    @Override
69    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
70        super.onFocusChanged(focused, direction, previouslyFocusedRect);
71        // Make the TextView focusable during editing, avoid the TextView gets accessibility focus
72        // before editing started. see also GuidedActionAdapterGroup where setFocusable(true).
73        if (!focused) {
74            setFocusable(false);
75        }
76    }
77}
78