Home | History | Annotate | Download | only in widget
      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 androidx.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.view.autofill.AutofillValue;
     26 import android.widget.EditText;
     27 import android.widget.TextView;
     28 
     29 /**
     30  * A custom EditText that satisfies the IME key monitoring requirements of GuidedStepFragment.
     31  */
     32 public class GuidedActionEditText extends EditText implements ImeKeyMonitor,
     33         GuidedActionAutofillSupport {
     34 
     35     /**
     36      * Workaround for b/26990627 forcing recompute the padding for the View when we turn on/off
     37      * the default background of EditText
     38      */
     39     static final class NoPaddingDrawable extends Drawable {
     40         @Override
     41         public boolean getPadding(Rect padding) {
     42             padding.set(0, 0, 0, 0);
     43             return true;
     44         }
     45 
     46         @Override
     47         public void draw(Canvas canvas) {
     48         }
     49 
     50         @Override
     51         public void setAlpha(int alpha) {
     52         }
     53 
     54         @Override
     55         public void setColorFilter(ColorFilter colorFilter) {
     56         }
     57 
     58         @Override
     59         public int getOpacity() {
     60             return PixelFormat.TRANSPARENT;
     61         }
     62     }
     63 
     64     private ImeKeyListener mKeyListener;
     65     private OnAutofillListener mAutofillListener;
     66     private final Drawable mSavedBackground;
     67     private final Drawable mNoPaddingDrawable;
     68 
     69     public GuidedActionEditText(Context ctx) {
     70         this(ctx, null);
     71     }
     72 
     73     public GuidedActionEditText(Context ctx, AttributeSet attrs) {
     74         this(ctx, attrs, android.R.attr.editTextStyle);
     75     }
     76 
     77     public GuidedActionEditText(Context ctx, AttributeSet attrs, int defStyleAttr) {
     78         super(ctx, attrs, defStyleAttr);
     79         mSavedBackground = getBackground();
     80         mNoPaddingDrawable = new NoPaddingDrawable();
     81         setBackground(mNoPaddingDrawable);
     82     }
     83 
     84     @Override
     85     public void setImeKeyListener(ImeKeyListener listener) {
     86         mKeyListener = listener;
     87     }
     88 
     89     @Override
     90     public boolean onKeyPreIme(int keyCode, KeyEvent event) {
     91         boolean result = false;
     92         if (mKeyListener != null) {
     93             result = mKeyListener.onKeyPreIme(this, keyCode, event);
     94         }
     95         if (!result) {
     96             result = super.onKeyPreIme(keyCode, event);
     97         }
     98         return result;
     99     }
    100 
    101     @Override
    102     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
    103         super.onInitializeAccessibilityNodeInfo(info);
    104         info.setClassName(isFocused() ? EditText.class.getName() : TextView.class.getName());
    105     }
    106 
    107     @Override
    108     protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
    109         super.onFocusChanged(focused, direction, previouslyFocusedRect);
    110         if (focused) {
    111             setBackground(mSavedBackground);
    112         } else {
    113             setBackground(mNoPaddingDrawable);
    114         }
    115         // Make the TextView focusable during editing, avoid the TextView gets accessibility focus
    116         // before editing started. see also GuidedActionAdapterGroup where setFocusable(true).
    117         if (!focused) {
    118             setFocusable(false);
    119         }
    120     }
    121 
    122     @Override
    123     public int getAutofillType() {
    124         // make it always autofillable as Guided fragment switches InputType when user clicks
    125         // on the field.
    126         return AUTOFILL_TYPE_TEXT;
    127     }
    128 
    129     @Override
    130     public void setOnAutofillListener(OnAutofillListener autofillListener) {
    131         mAutofillListener = autofillListener;
    132     }
    133 
    134     @Override
    135     public void autofill(AutofillValue values) {
    136         super.autofill(values);
    137         if (mAutofillListener != null) {
    138             mAutofillListener.onAutofill(this);
    139         }
    140     }
    141 }
    142