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.view.KeyEvent; 24import android.view.accessibility.AccessibilityNodeInfo; 25import android.widget.EditText; 26import android.widget.TextView; 27 28/** 29 * A custom EditText that satisfies the IME key monitoring requirements of GuidedStepFragment. 30 */ 31public 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 39 public boolean getPadding(Rect padding) { 40 padding.set(0, 0, 0, 0); 41 return true; 42 } 43 44 @Override 45 public void draw(Canvas canvas) { 46 } 47 48 @Override 49 public void setAlpha(int alpha) { 50 } 51 52 @Override 53 public void setColorFilter(ColorFilter colorFilter) { 54 } 55 56 @Override 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 66 public GuidedActionEditText(Context ctx) { 67 this(ctx, null); 68 } 69 70 public GuidedActionEditText(Context ctx, AttributeSet attrs) { 71 this(ctx, attrs, android.R.attr.editTextStyle); 72 } 73 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 82 public void setImeKeyListener(ImeKeyListener listener) { 83 mKeyListener = listener; 84 } 85 86 @Override 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 99 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 100 super.onInitializeAccessibilityNodeInfo(info); 101 info.setClassName(isFocused() ? EditText.class.getName() : TextView.class.getName()); 102 } 103 104 @Override 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