Fixing dark tint for ShadowDrawable.
Updating the dark theme for empty all-apps search drawable.

Change-Id: Ieb2018924da337bdd97eaf3adf2d32c3e3bebc73
diff --git a/src/com/android/launcher3/ButtonDropTarget.java b/src/com/android/launcher3/ButtonDropTarget.java
index 8a477d8..e4a3226 100644
--- a/src/com/android/launcher3/ButtonDropTarget.java
+++ b/src/com/android/launcher3/ButtonDropTarget.java
@@ -102,8 +102,8 @@
     protected void setDrawable(int resId) {
         // We do not set the drawable in the xml as that inflates two drawables corresponding to
         // drawableLeft and drawableStart.
-        mDrawable = getResources().getDrawable(resId);
-        setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
+        setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0, 0, 0);
+        mDrawable = getCompoundDrawablesRelative()[0];
     }
 
     public void setDropTargetBar(DropTargetBar dropTargetBar) {
diff --git a/src/com/android/launcher3/allapps/AllAppsBackgroundDrawable.java b/src/com/android/launcher3/allapps/AllAppsBackgroundDrawable.java
index 54c5bd0..3830a93 100644
--- a/src/com/android/launcher3/allapps/AllAppsBackgroundDrawable.java
+++ b/src/com/android/launcher3/allapps/AllAppsBackgroundDrawable.java
@@ -23,10 +23,12 @@
 import android.graphics.PixelFormat;
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
+import android.view.ContextThemeWrapper;
 import android.view.Gravity;
 
 import com.android.launcher3.LauncherAnimUtils;
 import com.android.launcher3.R;
+import com.android.launcher3.util.Themes;
 
 /**
  * This is a custom composite drawable that has a fixed virtual size and dynamically lays out its
@@ -36,7 +38,7 @@
 public class AllAppsBackgroundDrawable extends Drawable {
 
     /**
-     * A helper class to positon and orient a drawable to be drawn.
+     * A helper class to position and orient a drawable to be drawn.
      */
     protected static class TransformedImageDrawable {
         private Drawable mImage;
@@ -49,9 +51,9 @@
          * @param gravity If one of the Gravity center values, the x and y offset will take the width
          *                and height of the image into account to center the image to the offset.
          */
-        public TransformedImageDrawable(Resources res, int resourceId, float xPct, float yPct,
+        public TransformedImageDrawable(Context context, int resourceId, float xPct, float yPct,
                 int gravity) {
-            mImage = res.getDrawable(resourceId);
+            mImage = context.getDrawable(resourceId);
             mXPercent = xPct;
             mYPercent = yPct;
             mGravity = gravity;
@@ -98,19 +100,24 @@
 
     public AllAppsBackgroundDrawable(Context context) {
         Resources res = context.getResources();
-        mHand = new TransformedImageDrawable(res, R.drawable.ic_all_apps_bg_hand,
-                0.575f, 0.f, Gravity.CENTER_HORIZONTAL);
-        mIcons = new TransformedImageDrawable[4];
-        mIcons[0] = new TransformedImageDrawable(res, R.drawable.ic_all_apps_bg_icon_1,
-                0.375f, 0, Gravity.CENTER_HORIZONTAL);
-        mIcons[1] = new TransformedImageDrawable(res, R.drawable.ic_all_apps_bg_icon_2,
-                0.3125f, 0.2f, Gravity.CENTER_HORIZONTAL);
-        mIcons[2] = new TransformedImageDrawable(res, R.drawable.ic_all_apps_bg_icon_3,
-                0.475f, 0.26f, Gravity.CENTER_HORIZONTAL);
-        mIcons[3] = new TransformedImageDrawable(res, R.drawable.ic_all_apps_bg_icon_4,
-                0.7f, 0.125f, Gravity.CENTER_HORIZONTAL);
         mWidth = res.getDimensionPixelSize(R.dimen.all_apps_background_canvas_width);
         mHeight = res.getDimensionPixelSize(R.dimen.all_apps_background_canvas_height);
+
+        context = new ContextThemeWrapper(context,
+                Themes.getAttrBoolean(context, R.attr.isMainColorDark)
+                        ? R.style.AllAppsEmptySearchBackground_Dark
+                        : R.style.AllAppsEmptySearchBackground);
+        mHand = new TransformedImageDrawable(context, R.drawable.ic_all_apps_bg_hand,
+                0.575f, 0.f, Gravity.CENTER_HORIZONTAL);
+        mIcons = new TransformedImageDrawable[4];
+        mIcons[0] = new TransformedImageDrawable(context, R.drawable.ic_all_apps_bg_icon_1,
+                0.375f, 0, Gravity.CENTER_HORIZONTAL);
+        mIcons[1] = new TransformedImageDrawable(context, R.drawable.ic_all_apps_bg_icon_2,
+                0.3125f, 0.2f, Gravity.CENTER_HORIZONTAL);
+        mIcons[2] = new TransformedImageDrawable(context, R.drawable.ic_all_apps_bg_icon_3,
+                0.475f, 0.26f, Gravity.CENTER_HORIZONTAL);
+        mIcons[3] = new TransformedImageDrawable(context, R.drawable.ic_all_apps_bg_icon_4,
+                0.7f, 0.125f, Gravity.CENTER_HORIZONTAL);
     }
 
     /**
diff --git a/src/com/android/launcher3/graphics/ShadowDrawable.java b/src/com/android/launcher3/graphics/ShadowDrawable.java
index 45c1b6a..ffcedb2 100644
--- a/src/com/android/launcher3/graphics/ShadowDrawable.java
+++ b/src/com/android/launcher3/graphics/ShadowDrawable.java
@@ -17,7 +17,6 @@
 package com.android.launcher3.graphics;
 
 import android.annotation.TargetApi;
-import android.content.res.ColorStateList;
 import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
@@ -28,7 +27,6 @@
 import android.graphics.Paint;
 import android.graphics.PixelFormat;
 import android.graphics.Rect;
-import android.graphics.drawable.ColorDrawable;
 import android.graphics.drawable.Drawable;
 import android.os.Build;
 import android.util.AttributeSet;
@@ -111,12 +109,11 @@
 
     @Override
     public void applyTheme(Resources.Theme t) {
-        if (mState.canApplyTheme()) {
-            // Workaround since ColorStateList does not expose applyTheme method
-            ColorDrawable cd = new ColorDrawable();
-            cd.setTintList(mState.mTintColor);
-            cd.applyTheme(t);
-
+        TypedArray ta = t.obtainStyledAttributes(new int[] {R.attr.isWorkspaceDarkText});
+        boolean isDark = ta.getBoolean(0, false);
+        ta.recycle();
+        if (mState.mIsDark != isDark) {
+            mState.mIsDark = isDark;
             mState.mLastDrawnBitmap = null;
             invalidateSelf();
         }
@@ -132,21 +129,22 @@
         d.setBounds(mState.mShadowSize, mState.mShadowSize,
                 mState.mIntrinsicWidth - mState.mShadowSize,
                 mState.mIntrinsicHeight - mState.mShadowSize);
-        if (mState.mTintColor != null) {
-            d.setTint(mState.mTintColor.getDefaultColor());
+        d.setTint(mState.mIsDark ? mState.mDarkTintColor : Color.WHITE);
+        d.draw(canvas);
+
+        // Do not draw shadow on dark theme
+        if (!mState.mIsDark) {
+            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
+            paint.setMaskFilter(new BlurMaskFilter(mState.mShadowSize, BlurMaskFilter.Blur.NORMAL));
+            int[] offset = new int[2];
+            Bitmap shadow = bitmap.extractAlpha(paint, offset);
+
+            paint.setMaskFilter(null);
+            paint.setColor(mState.mShadowColor);
+            bitmap.eraseColor(Color.TRANSPARENT);
+            canvas.drawBitmap(shadow, offset[0], offset[1], paint);
+            d.draw(canvas);
         }
-        d.draw(canvas);
-
-        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
-        paint.setMaskFilter(new BlurMaskFilter(mState.mShadowSize, BlurMaskFilter.Blur.NORMAL));
-        int[] offset = new int[2];
-        Bitmap shadow = bitmap.extractAlpha(paint, offset);
-
-        paint.setMaskFilter(null);
-        paint.setColor(mState.mShadowColor);
-        bitmap.eraseColor(Color.TRANSPARENT);
-        canvas.drawBitmap(shadow, offset[0], offset[1], paint);
-        d.draw(canvas);
 
         if (Utilities.isAtLeastO()) {
             bitmap = bitmap.copy(Bitmap.Config.HARDWARE, false);
@@ -162,7 +160,6 @@
         final TypedArray a = theme == null
                 ? r.obtainAttributes(attrs, R.styleable.ShadowDrawable)
                 : theme.obtainStyledAttributes(attrs, R.styleable.ShadowDrawable, 0, 0);
-
         try {
             Drawable d = a.getDrawable(R.styleable.ShadowDrawable_android_src);
             if (d == null) {
@@ -172,7 +169,8 @@
                     R.styleable.ShadowDrawable_android_shadowColor, Color.BLACK);
             mState.mShadowSize = a.getDimensionPixelSize(
                     R.styleable.ShadowDrawable_android_elevation, 0);
-            mState.mTintColor = a.getColorStateList(R.styleable.ShadowDrawable_android_tint);
+            mState.mDarkTintColor = a.getColor(
+                    R.styleable.ShadowDrawable_darkTintColor, Color.BLACK);
 
             mState.mIntrinsicHeight = d.getIntrinsicHeight() + 2 * mState.mShadowSize;
             mState.mIntrinsicWidth = d.getIntrinsicWidth() + 2 * mState.mShadowSize;
@@ -192,8 +190,9 @@
 
         int mShadowColor;
         int mShadowSize;
-        ColorStateList mTintColor;
+        int mDarkTintColor;
 
+        boolean mIsDark;
         Bitmap mLastDrawnBitmap;
         ConstantState mChildState;
 
@@ -209,7 +208,7 @@
 
         @Override
         public boolean canApplyTheme() {
-            return mTintColor != null;
+            return true;
         }
     }
 }