OSDN Git Service

Detect unhandled keyboard focused state.
authorJiaquan He <hejq@google.com>
Thu, 2 Feb 2017 22:26:45 +0000 (14:26 -0800)
committerJiaquan He <hejq@google.com>
Mon, 10 Apr 2017 21:01:49 +0000 (21:01 +0000)
A View uses a Drawable as its background, which changes
its appearance according to the View's state. This commit
adds an algorithm to detect undefined state_focused in
state specs for those Drawables.

Test: cts-tradefed run singleCommand cts --skip-device-info
--skip-preconditions --abi armeabi-v7a -m CtsGraphicsTestCases
-t android.graphics.drawable.cts.DefaultFocusHighlightTest
Bug: 35096940

Change-Id: I5e9f07141679881fe88f9040aa116ea2f9d537c9

15 files changed:
core/java/android/content/res/ColorStateList.java
core/java/android/util/StateSet.java
graphics/java/android/graphics/drawable/AdaptiveIconDrawable.java
graphics/java/android/graphics/drawable/BitmapDrawable.java
graphics/java/android/graphics/drawable/ColorDrawable.java
graphics/java/android/graphics/drawable/Drawable.java
graphics/java/android/graphics/drawable/DrawableContainer.java
graphics/java/android/graphics/drawable/DrawableWrapper.java
graphics/java/android/graphics/drawable/GradientDrawable.java
graphics/java/android/graphics/drawable/LayerDrawable.java
graphics/java/android/graphics/drawable/NinePatchDrawable.java
graphics/java/android/graphics/drawable/RippleDrawable.java
graphics/java/android/graphics/drawable/ShapeDrawable.java
graphics/java/android/graphics/drawable/StateListDrawable.java
graphics/java/android/graphics/drawable/VectorDrawable.java

index a46db06..faf2381 100644 (file)
@@ -91,7 +91,7 @@ import java.util.Arrays;
  * file. An item with no state spec is considered to match any set of states and is generally
  * useful as a final item to be used as a default.
  * <p>
- * If an item with no state spec if placed before other items, those items
+ * If an item with no state spec is placed before other items, those items
  * will be ignored.
  *
  * <a name="ItemAttributes"></a>
@@ -521,6 +521,15 @@ public class ColorStateList extends ComplexColor implements Parcelable {
     }
 
     /**
+     * Return whether the state spec list has at least one item explicitly specifying
+     * {@link android.R.attr#state_focused}.
+     * @hide
+     */
+    public boolean hasFocusStateSpecified() {
+        return StateSet.containsAttribute(mStateSpecs, R.attr.state_focused);
+    }
+
+    /**
      * Indicates whether this color state list is opaque, which means that every
      * color returned from {@link #getColorForState(int[], int)} has an alpha
      * value of 255.
index 051de8a..4bbc0f8 100644 (file)
@@ -228,6 +228,29 @@ public class StateSet {
         return true;
     }
 
+    /**
+     * Check whether a list of state specs has an attribute specified.
+     * @param stateSpecs a list of state specs we're checking.
+     * @param attr an attribute we're looking for.
+     * @return {@code true} if the attribute is contained in the state specs.
+     * @hide
+     */
+    public static boolean containsAttribute(int[][] stateSpecs, int attr) {
+        if (stateSpecs != null) {
+            for (int[] spec : stateSpecs) {
+                if (spec == null) {
+                    break;
+                }
+                for (int specAttr : spec) {
+                    if (specAttr == attr || -specAttr == attr) {
+                        return true;
+                    }
+                }
+            }
+        }
+        return false;
+    }
+
     public static int[] trimStateSet(int[] states, int newSize) {
         if (states.length == newSize) {
             return states;
index 643c0da..6dfe03d 100644 (file)
@@ -728,6 +728,12 @@ public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback
         return mLayerState.isStateful();
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        return mLayerState.hasFocusStateSpecified();
+    }
+
     @Override
     protected boolean onStateChange(int[] state) {
         boolean changed = false;
@@ -1035,6 +1041,17 @@ public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback
             return isStateful;
         }
 
+        public final boolean hasFocusStateSpecified() {
+            final ChildDrawable[] array = mChildren;
+            for (int i = 0; i < N_CHILDREN; i++) {
+                final Drawable dr = array[i].mDrawable;
+                if (dr != null && dr.hasFocusStateSpecified()) {
+                    return true;
+                }
+            }
+            return false;
+        }
+
         public final boolean canConstantState() {
             final ChildDrawable[] array = mChildren;
             for (int i = 0; i < N_CHILDREN; i++) {
index 6deeb0d..5004667 100644 (file)
@@ -737,6 +737,12 @@ public class BitmapDrawable extends Drawable {
                 || super.isStateful();
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        return mBitmapState.mTint != null && mBitmapState.mTint.hasFocusStateSpecified();
+    }
+
     @Override
     public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme)
             throws XmlPullParserException, IOException {
index 7524cac..559e3d3 100644 (file)
@@ -207,6 +207,12 @@ public class ColorDrawable extends Drawable {
         return mColorState.mTint != null && mColorState.mTint.isStateful();
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        return mColorState.mTint != null && mColorState.mTint.hasFocusStateSpecified();
+    }
+
     @Override
     public int getOpacity() {
         if (mTintFilter != null || mPaint.getColorFilter() != null) {
index 850f40e..44fb1c7 100644 (file)
@@ -26,6 +26,7 @@ import android.annotation.ColorInt;
 import android.annotation.IntRange;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.TestApi;
 import android.content.pm.ActivityInfo.Config;
 import android.content.res.ColorStateList;
 import android.content.res.Resources;
@@ -713,6 +714,25 @@ public abstract class Drawable {
     }
 
     /**
+     * Indicates whether this drawable has at least one state spec explicitly
+     * specifying {@link android.R.attr#state_focused}.
+     *
+     * <p>Note: A View uses a {@link Drawable} instance as its background and it
+     * changes its appearance based on a state. On keyboard devices, it should
+     * specify its {@link android.R.attr#state_focused} to make sure the user
+     * knows which view is holding the focus.</p>
+     *
+     * @return {@code true} if {@link android.R.attr#state_focused} is specified
+     * for this drawable.
+     *
+     * @hide
+     */
+    @TestApi
+    public boolean hasFocusStateSpecified() {
+        return false;
+    }
+
+    /**
      * Specify a set of states for the drawable. These are use-case specific,
      * so see the relevant documentation. As an example, the background for
      * widgets like Button understand the following states:
index a491d7e..aa4cd9c 100644 (file)
@@ -242,6 +242,18 @@ public class DrawableContainer extends Drawable implements Drawable.Callback {
         return mDrawableContainerState.isStateful();
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        if (mCurrDrawable != null) {
+            return mCurrDrawable.hasFocusStateSpecified();
+        }
+        if (mLastDrawable != null) {
+            return mLastDrawable.hasFocusStateSpecified();
+        }
+        return false;
+    }
+
     @Override
     public void setAutoMirrored(boolean mirrored) {
         if (mDrawableContainerState.mAutoMirrored != mirrored) {
index 5887939..431b63b 100644 (file)
@@ -323,6 +323,12 @@ public abstract class DrawableWrapper extends Drawable implements Drawable.Callb
         return mDrawable != null && mDrawable.isStateful();
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        return mDrawable != null && mDrawable.hasFocusStateSpecified();
+    }
+
     @Override
     protected boolean onStateChange(int[] state) {
         if (mDrawable != null && mDrawable.isStateful()) {
index 7aad7df..6c3aea2 100644 (file)
@@ -987,6 +987,15 @@ public class GradientDrawable extends Drawable {
                 || (s.mTint != null && s.mTint.isStateful());
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        final GradientState s = mGradientState;
+        return (s.mSolidColors != null && s.mSolidColors.hasFocusStateSpecified())
+                || (s.mStrokeColors != null && s.mStrokeColors.hasFocusStateSpecified())
+                || (s.mTint != null && s.mTint.hasFocusStateSpecified());
+    }
+
     @Override
     public @Config int getChangingConfigurations() {
         return super.getChangingConfigurations() | mGradientState.getChangingConfigurations();
index b159f0f..4725c2c 100644 (file)
@@ -1476,6 +1476,12 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
         return mLayerState.isStateful();
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        return mLayerState.hasFocusStateSpecified();
+    }
+
     @Override
     protected boolean onStateChange(int[] state) {
         boolean changed = false;
@@ -2117,6 +2123,18 @@ public class LayerDrawable extends Drawable implements Drawable.Callback {
             return isStateful;
         }
 
+        public final boolean hasFocusStateSpecified() {
+            final int N = mNumChildren;
+            final ChildDrawable[] array = mChildren;
+            for (int i = 0; i < N; i++) {
+                final Drawable dr = array[i].mDrawable;
+                if (dr != null && dr.hasFocusStateSpecified()) {
+                    return true;
+                }
+            }
+            return false;
+        }
+
         public final boolean canConstantState() {
             final ChildDrawable[] array = mChildren;
             final int N = mNumChildren;
index c7183d9..1790020 100644 (file)
@@ -574,6 +574,12 @@ public class NinePatchDrawable extends Drawable {
         return super.isStateful() || (s.mTint != null && s.mTint.isStateful());
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        return mNinePatchState.mTint != null && mNinePatchState.mTint.hasFocusStateSpecified();
+    }
+
     final static class NinePatchState extends ConstantState {
         @Config int mChangingConfigurations;
 
index f83c160..bfd0604 100644 (file)
@@ -377,6 +377,12 @@ public class RippleDrawable extends LayerDrawable {
         return true;
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        return true;
+    }
+
     /**
      * Sets the ripple color.
      *
index 6758607..c43899b 100644 (file)
@@ -352,6 +352,12 @@ public class ShapeDrawable extends Drawable {
         return super.isStateful() || (s.mTint != null && s.mTint.isStateful());
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        return mShapeState.mTint != null && mShapeState.mTint.hasFocusStateSpecified();
+    }
+
     /**
      * Subclasses override this to parse custom subelements. If you handle it,
      * return true, else return <em>super.inflateTag(...)</em>.
index 64a9eb5..c98f160 100644 (file)
@@ -90,6 +90,12 @@ public class StateListDrawable extends DrawableContainer {
         return true;
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        return mStateListState.hasFocusStateSpecified();
+    }
+
     @Override
     protected boolean onStateChange(int[] stateSet) {
         final boolean changed = super.onStateChange(stateSet);
@@ -342,6 +348,10 @@ public class StateListDrawable extends DrawableContainer {
             return -1;
         }
 
+        boolean hasFocusStateSpecified() {
+            return StateSet.containsAttribute(mStateSets, R.attr.state_focused);
+        }
+
         @Override
         public Drawable newDrawable() {
             return new StateListDrawable(this, null);
index a1539b8..41e5af1 100644 (file)
@@ -413,6 +413,12 @@ public class VectorDrawable extends Drawable {
         return super.isStateful() || (mVectorState != null && mVectorState.isStateful());
     }
 
+    /** @hide */
+    @Override
+    public boolean hasFocusStateSpecified() {
+        return mVectorState != null && mVectorState.hasFocusStateSpecified();
+    }
+
     @Override
     protected boolean onStateChange(int[] stateSet) {
         boolean changed = false;
@@ -976,6 +982,11 @@ public class VectorDrawable extends Drawable {
                     || (mRootGroup != null && mRootGroup.isStateful());
         }
 
+        public boolean hasFocusStateSpecified() {
+            return mTint != null && mTint.hasFocusStateSpecified()
+                    || (mRootGroup != null && mRootGroup.hasFocusStateSpecified());
+        }
+
         void setViewportSize(float viewportWidth, float viewportHeight) {
             mViewportWidth = viewportWidth;
             mViewportHeight = viewportHeight;
@@ -1326,6 +1337,21 @@ public class VectorDrawable extends Drawable {
         }
 
         @Override
+        public boolean hasFocusStateSpecified() {
+            boolean result = false;
+
+            final ArrayList<VObject> children = mChildren;
+            for (int i = 0, count = children.size(); i < count; i++) {
+                final VObject child = children.get(i);
+                if (child.isStateful()) {
+                    result |= child.hasFocusStateSpecified();
+                }
+            }
+
+            return result;
+        }
+
+        @Override
         int getNativeSize() {
             // Return the native allocation needed for the subtree.
             int size = NATIVE_ALLOCATION_SIZE;
@@ -1569,6 +1595,11 @@ public class VectorDrawable extends Drawable {
         }
 
         @Override
+        public boolean hasFocusStateSpecified() {
+            return false;
+        }
+
+        @Override
         int getNativeSize() {
             return NATIVE_ALLOCATION_SIZE;
         }
@@ -1819,6 +1850,14 @@ public class VectorDrawable extends Drawable {
         }
 
         @Override
+        public boolean hasFocusStateSpecified() {
+            return (mStrokeColors != null && mStrokeColors instanceof ColorStateList &&
+                    ((ColorStateList) mStrokeColors).hasFocusStateSpecified()) &&
+                    (mFillColors != null && mFillColors instanceof ColorStateList &&
+                    ((ColorStateList) mFillColors).hasFocusStateSpecified());
+        }
+
+        @Override
         int getNativeSize() {
             return NATIVE_ALLOCATION_SIZE;
         }
@@ -2116,6 +2155,7 @@ public class VectorDrawable extends Drawable {
         abstract void applyTheme(Theme t);
         abstract boolean onStateChange(int[] state);
         abstract boolean isStateful();
+        abstract boolean hasFocusStateSpecified();
         abstract int getNativeSize();
         abstract Property getProperty(String propertyName);
     }