Add alpha to bitmap, nine patch drawable, fix container hot spots

Also fixes primary text disabled state alpha.

BUG: 13818888
Change-Id: I9ae2e25216014177c2dac24f5c9095df87724a43
diff --git a/core/res/res/color/primary_text_quantum_dark.xml b/core/res/res/color/primary_text_quantum_dark.xml
new file mode 100644
index 0000000..1fcd0e3
--- /dev/null
+++ b/core/res/res/color/primary_text_quantum_dark.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2008 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+  
+          http://www.apache.org/licenses/LICENSE-2.0
+  
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:state_enabled="false" android:alpha="0.5" android:color="@color/primary_text_default_quantum_dark"/>
+    <item android:color="@color/primary_text_default_quantum_dark"/>
+</selector>
diff --git a/core/res/res/color/primary_text_quantum_light.xml b/core/res/res/color/primary_text_quantum_light.xml
new file mode 100644
index 0000000..1ec1634
--- /dev/null
+++ b/core/res/res/color/primary_text_quantum_light.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2008 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+  
+          http://www.apache.org/licenses/LICENSE-2.0
+  
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:state_enabled="false" android:alpha="0.5" android:color="@color/primary_text_default_quantum_light"/>
+    <item android:color="@color/primary_text_default_quantum_light"/>
+</selector>
diff --git a/core/res/res/drawable/btn_color_quantum.xml b/core/res/res/drawable/btn_color_quantum.xml
index e923003..2da9226 100644
--- a/core/res/res/drawable/btn_color_quantum.xml
+++ b/core/res/res/drawable/btn_color_quantum.xml
@@ -19,7 +19,8 @@
     <selector>
         <item android:state_enabled="false">
             <nine-patch android:src="@drawable/btn_qntm_alpha"
-                android:tint="?attr/colorButtonNormal" />
+                android:tint="?attr/colorButtonNormal"
+                android:alpha="?attr/disabledAlpha" />
         </item>
         <item>
             <nine-patch android:src="@drawable/btn_qntm_alpha"
diff --git a/core/res/res/drawable/btn_default_quantum.xml b/core/res/res/drawable/btn_default_quantum.xml
index 2919621..c6a3a33 100644
--- a/core/res/res/drawable/btn_default_quantum.xml
+++ b/core/res/res/drawable/btn_default_quantum.xml
@@ -17,7 +17,18 @@
 <touch-feedback xmlns:android="http://schemas.android.com/apk/res/android"
     android:tint="?attr/colorButtonPressed">
     <item>
-        <nine-patch android:src="@drawable/btn_qntm_alpha"
-            android:tint="?attr/colorButtonNormal" />
+        <selector>
+            <item android:state_enabled="false">
+                <nine-patch
+                    android:src="@drawable/btn_qntm_alpha"
+                    android:tint="?attr/colorButtonNormal"
+                    android:alpha="?attr/disabledAlpha" />
+            </item>
+            <item>
+                <nine-patch
+                    android:src="@drawable/btn_qntm_alpha"
+                    android:tint="?attr/colorButtonNormal" />
+            </item>
+        </selector>
     </item>
 </touch-feedback>
diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml
index 782066e..b7bffde 100644
--- a/core/res/res/values/attrs.xml
+++ b/core/res/res/values/attrs.xml
@@ -4504,6 +4504,8 @@
                  result to valid color values. Saturate(S + D) -->
             <enum name="add" value="16" />
         </attr>
+        <!-- Specifies the alpha multiplier to apply to the base drawable. -->
+        <attr name="alpha" />
     </declare-styleable>
 
     <!-- Drawable used to draw 9-patches. -->
@@ -4523,6 +4525,8 @@
         <!-- When a tint color is set, specifies its Porter-Duff blending mode. The
              default value is src_in, which treats the drawable as an alpha mask. -->
         <attr name="tintMode" />
+        <!-- Specifies the alpha multiplier to apply to the base drawable. -->
+        <attr name="alpha" />
     </declare-styleable>
 
     <!-- Drawable used to draw a single color. -->
diff --git a/core/res/res/values/colors_quantum.xml b/core/res/res/values/colors_quantum.xml
index ebe4a49..f8f192f 100644
--- a/core/res/res/values/colors_quantum.xml
+++ b/core/res/res/values/colors_quantum.xml
@@ -118,11 +118,11 @@
 
     <!-- Text & foreground colors -->
 
-    <color name="primary_text_quantum_light">#de000000</color>
+    <color name="primary_text_default_quantum_light">#de000000</color>
     <color name="secondary_text_quantum_light">#8a000000</color>
     <color name="tertiary_text_quantum_light">#4d000000</color>
 
-    <color name="primary_text_quantum_dark">#deffffff</color>
+    <color name="primary_text_default_quantum_dark">#deffffff</color>
     <color name="secondary_text_quantum_dark">#8affffff</color>
     <color name="tertiary_text_quantum_dark">#4dffffff</color>
 </resources>
diff --git a/graphics/java/android/graphics/drawable/BitmapDrawable.java b/graphics/java/android/graphics/drawable/BitmapDrawable.java
index 66a88a2..60b4615 100644
--- a/graphics/java/android/graphics/drawable/BitmapDrawable.java
+++ b/graphics/java/android/graphics/drawable/BitmapDrawable.java
@@ -484,6 +484,15 @@
             copyBounds(mDstRect);
         }
 
+        final int restoreAlpha;
+        if (state.mBaseAlpha != 1.0f) {
+            final Paint p = getPaint();
+            restoreAlpha = p.getAlpha();
+            p.setAlpha((int) (restoreAlpha * state.mBaseAlpha + 0.5f));
+        } else {
+            restoreAlpha = -1;
+        }
+
         final boolean clearColorFilter;
         if (mTintFilter != null && paint.getColorFilter() == null) {
             paint.setColorFilter(mTintFilter);
@@ -537,6 +546,10 @@
         if (clearColorFilter) {
             paint.setColorFilter(null);
         }
+
+        if (restoreAlpha >= 0) {
+            paint.setAlpha(restoreAlpha);
+        }
     }
 
     @Override
@@ -762,6 +775,10 @@
             paint.setDither(dither);
         }
 
+        if (themeAttrs == null || themeAttrs[R.styleable.BitmapDrawable_alpha] == 0) {
+            state.mBaseAlpha = a.getFloat(R.styleable.BitmapDrawable_alpha, 1.0f);
+        }
+
         if (themeAttrs == null || themeAttrs[R.styleable.BitmapDrawable_gravity] == 0) {
             final int gravity = a.getInt(
                     R.styleable.BitmapDrawable_gravity, Gravity.FILL);
@@ -818,6 +835,10 @@
             paint.setDither(dither);
         }
 
+        if (a.hasValue(R.styleable.BitmapDrawable_alpha)) {
+            state.mBaseAlpha = a.getFloat(R.styleable.BitmapDrawable_alpha, state.mBaseAlpha);
+        }
+
         if (a.hasValue(R.styleable.BitmapDrawable_gravity)) {
             final int gravity = a.getInt(
                     R.styleable.BitmapDrawable_gravity, Gravity.FILL);
@@ -933,6 +954,7 @@
         int[] mThemeAttrs;
         int mChangingConfigurations;
         int mGravity = Gravity.FILL;
+        float mBaseAlpha = 1.0f;
         Paint mPaint = new Paint(DEFAULT_PAINT_FLAGS);
         Shader.TileMode mTileModeX = null;
         Shader.TileMode mTileModeY = null;
@@ -954,6 +976,7 @@
             mTileModeX = bitmapState.mTileModeX;
             mTileModeY = bitmapState.mTileModeY;
             mTargetDensity = bitmapState.mTargetDensity;
+            mBaseAlpha = bitmapState.mBaseAlpha;
             mPaint = new Paint(bitmapState.mPaint);
             mRebuildShader = bitmapState.mRebuildShader;
             mAutoMirrored = bitmapState.mAutoMirrored;
diff --git a/graphics/java/android/graphics/drawable/DrawableContainer.java b/graphics/java/android/graphics/drawable/DrawableContainer.java
index 05df3bc..1f8b51d 100644
--- a/graphics/java/android/graphics/drawable/DrawableContainer.java
+++ b/graphics/java/android/graphics/drawable/DrawableContainer.java
@@ -238,6 +238,35 @@
     }
 
     @Override
+    public void setHotspot(int key, float x, float y) {
+        if (mCurrDrawable != null) {
+            mCurrDrawable.setHotspot(key, x, y);
+        }
+    }
+
+    @Override
+    public void removeHotspot(int key) {
+        if (mCurrDrawable != null) {
+            mCurrDrawable.removeHotspot(key);
+        }
+    }
+
+    @Override
+    public void clearHotspots() {
+        if (mCurrDrawable != null) {
+            mCurrDrawable.clearHotspots();
+        }
+    }
+
+    @Override
+    public boolean supportsHotspots() {
+        if (mCurrDrawable != null) {
+            return mCurrDrawable.supportsHotspots();
+        }
+        return false;
+    }
+
+    @Override
     protected boolean onStateChange(int[] state) {
         if (mLastDrawable != null) {
             return mLastDrawable.setState(state);
diff --git a/graphics/java/android/graphics/drawable/NinePatchDrawable.java b/graphics/java/android/graphics/drawable/NinePatchDrawable.java
index 3e9ca0a..21992ce 100644
--- a/graphics/java/android/graphics/drawable/NinePatchDrawable.java
+++ b/graphics/java/android/graphics/drawable/NinePatchDrawable.java
@@ -241,6 +241,15 @@
             canvas.scale(-1.0f, 1.0f);
         }
 
+        final int restoreAlpha;
+        if (mNinePatchState.mBaseAlpha != 1.0f) {
+            final Paint p = getPaint();
+            restoreAlpha = p.getAlpha();
+            p.setAlpha((int) (restoreAlpha * mNinePatchState.mBaseAlpha + 0.5f));
+        } else {
+            restoreAlpha = -1;
+        }
+
         mNinePatch.draw(canvas, bounds, mPaint);
 
         if (needsMirroring) {
@@ -250,6 +259,10 @@
         if (clearColorFilter) {
             mPaint.setColorFilter(null);
         }
+
+        if (restoreAlpha >= 0) {
+            mPaint.setAlpha(restoreAlpha);
+        }
     }
 
     @Override
@@ -491,6 +504,10 @@
             }
         }
 
+        if (themeAttrs == null || themeAttrs[R.styleable.NinePatchDrawable_alpha] == 0) {
+            ninePatchState.mBaseAlpha = a.getFloat(R.styleable.NinePatchDrawable_alpha, 1.0f);
+        }
+
         // Apply the constant state to the paint.
         initializeWithState(ninePatchState, r);
 
@@ -584,6 +601,10 @@
             }
         }
 
+        if (a.hasValue(R.styleable.NinePatchDrawable_alpha)) {
+            state.mBaseAlpha = a.getFloat(R.styleable.NinePatchDrawable_alpha, 1.0f);
+        }
+
         // Apply the constant state to the paint.
         initializeWithState(state, r);
 
@@ -689,12 +710,13 @@
         Mode mTintMode = Mode.SRC_IN;
         Rect mPadding;
         Insets mOpticalInsets;
+        float mBaseAlpha = 1.0f;
         boolean mDither;
         int[] mThemeAttrs;
         int mChangingConfigurations;
         int mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
         boolean mAutoMirrored;
-        
+
         NinePatchState() {
             // Empty constructor.
         }
@@ -726,6 +748,7 @@
             mThemeAttrs = state.mThemeAttrs;
             mPadding = state.mPadding;
             mOpticalInsets = state.mOpticalInsets;
+            mBaseAlpha = state.mBaseAlpha;
             mDither = state.mDither;
             mChangingConfigurations = state.mChangingConfigurations;
             mTargetDensity = state.mTargetDensity;
diff --git a/graphics/java/android/graphics/drawable/TouchFeedbackDrawable.java b/graphics/java/android/graphics/drawable/TouchFeedbackDrawable.java
index 5f59467..cf08031 100644
--- a/graphics/java/android/graphics/drawable/TouchFeedbackDrawable.java
+++ b/graphics/java/android/graphics/drawable/TouchFeedbackDrawable.java
@@ -39,6 +39,7 @@
 import org.xmlpull.v1.XmlPullParserException;
 
 import java.io.IOException;
+import java.util.Arrays;
 
 /**
  * Documentation pending.
@@ -110,6 +111,24 @@
         return false;
     }
 
+    @Override
+    public boolean setVisible(boolean visible, boolean restart) {
+        if (!visible) {
+            if (mTouchedRipples != null) {
+                mTouchedRipples.clear();
+            }
+
+            if (mActiveRipplesCount > 0) {
+                Arrays.fill(mActiveRipples, null);
+                mActiveRipplesCount = 0;
+                mAnimating = false;
+                unscheduleSelf(mAnimationRunnable);
+            }
+        }
+
+        return super.setVisible(visible, restart);
+    }
+
     /**
      * @hide
      */