Updating the top scrim to also use dynamicaly generated bitmap

Bug: 237818532
Test: Verified on device
Flag: N/A
Change-Id: I22ceb470677ab9df53e40fb78f0292b7fd5e2241
diff --git a/quickstep/src/com/android/quickstep/RecentsActivity.java b/quickstep/src/com/android/quickstep/RecentsActivity.java
index 39af7fd..8b9b4f3 100644
--- a/quickstep/src/com/android/quickstep/RecentsActivity.java
+++ b/quickstep/src/com/android/quickstep/RecentsActivity.java
@@ -22,7 +22,6 @@
 import static com.android.launcher3.QuickstepTransitionManager.RECENTS_LAUNCH_DURATION;
 import static com.android.launcher3.QuickstepTransitionManager.STATUS_BAR_TRANSITION_DURATION;
 import static com.android.launcher3.QuickstepTransitionManager.STATUS_BAR_TRANSITION_PRE_DELAY;
-import static com.android.launcher3.graphics.SysUiScrim.SYSUI_PROGRESS;
 import static com.android.launcher3.testing.shared.TestProtocol.OVERVIEW_STATE_ORDINAL;
 import static com.android.quickstep.OverviewComponentObserver.startHomeIntentSafely;
 import static com.android.quickstep.TaskUtils.taskIsATargetWithMode;
@@ -130,7 +129,7 @@
         mScrimView = findViewById(R.id.scrim_view);
         mFallbackRecentsView = findViewById(R.id.overview_panel);
         mActionsView = findViewById(R.id.overview_actions_view);
-        SYSUI_PROGRESS.set(getRootView().getSysUiScrim(), 0f);
+        getRootView().getSysUiScrim().getSysUIProgress().updateValue(0);
 
         SplitSelectStateController controller =
                 new SplitSelectStateController(this, mHandler, getStateManager(),
diff --git a/quickstep/src/com/android/quickstep/util/StaggeredWorkspaceAnim.java b/quickstep/src/com/android/quickstep/util/StaggeredWorkspaceAnim.java
index cd5edab..f354e78 100644
--- a/quickstep/src/com/android/quickstep/util/StaggeredWorkspaceAnim.java
+++ b/quickstep/src/com/android/quickstep/util/StaggeredWorkspaceAnim.java
@@ -177,8 +177,8 @@
 
         addDepthAnimationForState(launcher, NORMAL, duration);
 
-        mAnimators.play(launcher.getRootView().getSysUiScrim().createSysuiMultiplierAnim(0f, 1f)
-                .setDuration(duration));
+        mAnimators.play(launcher.getRootView().getSysUiScrim().getSysUIMultiplier()
+                .animateToValue(0f, 1f).setDuration(duration));
     }
 
     private void addAnimationForPage(CellLayout page, int totalRows, long duration) {
diff --git a/quickstep/src/com/android/quickstep/util/WorkspaceRevealAnim.java b/quickstep/src/com/android/quickstep/util/WorkspaceRevealAnim.java
index 34fa7f1..cde7050 100644
--- a/quickstep/src/com/android/quickstep/util/WorkspaceRevealAnim.java
+++ b/quickstep/src/com/android/quickstep/util/WorkspaceRevealAnim.java
@@ -92,7 +92,8 @@
         }
 
         // Add sysui scrim animation.
-        mAnimators.play(launcher.getRootView().getSysUiScrim().createSysuiMultiplierAnim(0f, 1f));
+        mAnimators.play(launcher.getRootView().getSysUiScrim()
+                .getSysUIMultiplier().animateToValue(0f, 1f));
 
         mAnimators.setDuration(DURATION_MS);
         mAnimators.setInterpolator(Interpolators.DECELERATED_EASE);
diff --git a/res/drawable-hdpi/workspace_bg.9.png b/res/drawable-hdpi/workspace_bg.9.png
deleted file mode 100755
index 1d82fd4..0000000
--- a/res/drawable-hdpi/workspace_bg.9.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-mdpi/workspace_bg.9.png b/res/drawable-mdpi/workspace_bg.9.png
deleted file mode 100755
index 116ce44..0000000
--- a/res/drawable-mdpi/workspace_bg.9.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-xhdpi/workspace_bg.9.png b/res/drawable-xhdpi/workspace_bg.9.png
deleted file mode 100755
index b1b3b85..0000000
--- a/res/drawable-xhdpi/workspace_bg.9.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-xxhdpi/workspace_bg.9.png b/res/drawable-xxhdpi/workspace_bg.9.png
deleted file mode 100755
index d47f6b2..0000000
--- a/res/drawable-xxhdpi/workspace_bg.9.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-xxxhdpi/workspace_bg.9.png b/res/drawable-xxxhdpi/workspace_bg.9.png
deleted file mode 100755
index 3281548..0000000
--- a/res/drawable-xxxhdpi/workspace_bg.9.png
+++ /dev/null
Binary files differ
diff --git a/res/values/attrs.xml b/res/values/attrs.xml
index 9b1bcf5..915cdb4 100644
--- a/res/values/attrs.xml
+++ b/res/values/attrs.xml
@@ -37,7 +37,6 @@
     <attr name="workspaceShadowColor" format="color" />
     <attr name="workspaceAmbientShadowColor" format="color" />
     <attr name="workspaceKeyShadowColor" format="color" />
-    <attr name="workspaceStatusBarScrim" format="reference" />
     <attr name="widgetsTheme" format="reference" />
     <attr name="iconOnlyShortcutColor" format="color" />
     <attr name="eduHalfSheetBGColor" format="color" />
diff --git a/res/values/styles.xml b/res/values/styles.xml
index de29a95..bba4102 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -52,7 +52,6 @@
         <item name="workspaceShadowColor">#B0000000</item>
         <item name="workspaceAmbientShadowColor">#40000000</item>
         <item name="workspaceKeyShadowColor">#89000000</item>
-        <item name="workspaceStatusBarScrim">@drawable/workspace_bg</item>
         <item name="widgetsTheme">@style/WidgetContainerTheme</item>
         <item name="folderPaginationColor">@color/folder_pagination_color_light</item>
         <item name="folderPreviewColor">@color/folder_preview_light</item>
@@ -139,7 +138,6 @@
         <item name="workspaceAmbientShadowColor">@android:color/transparent</item>
         <item name="workspaceKeyShadowColor">@android:color/transparent</item>
         <item name="isWorkspaceDarkText">true</item>
-        <item name="workspaceStatusBarScrim">@null</item>
         <item name="workspaceAccentColor">@color/workspace_accent_color_dark</item>
         <item name="dropTargetHoverTextColor">@color/drop_target_hover_text_color_dark</item>
         <item name="dropTargetHoverButtonColor">@color/drop_target_hover_button_color_dark</item>
@@ -192,7 +190,6 @@
         <item name="workspaceAmbientShadowColor">@android:color/transparent</item>
         <item name="workspaceKeyShadowColor">@android:color/transparent</item>
         <item name="isWorkspaceDarkText">true</item>
-        <item name="workspaceStatusBarScrim">@null</item>
         <item name="workspaceAccentColor">@color/workspace_accent_color_dark</item>
         <item name="dropTargetHoverTextColor">@color/drop_target_hover_text_color_dark</item>
         <item name="dropTargetHoverButtonColor">@color/drop_target_hover_button_color_dark</item>
diff --git a/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java b/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java
index 565d7da..2e7177b 100644
--- a/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java
+++ b/src/com/android/launcher3/WorkspaceStateTransitionAnimation.java
@@ -35,7 +35,6 @@
 import static com.android.launcher3.anim.Interpolators.ZOOM_OUT;
 import static com.android.launcher3.anim.PropertySetter.NO_ANIM_PROPERTY_SETTER;
 import static com.android.launcher3.graphics.Scrim.SCRIM_PROGRESS;
-import static com.android.launcher3.graphics.SysUiScrim.SYSUI_PROGRESS;
 import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_FADE;
 import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_SCALE;
 import static com.android.launcher3.states.StateAnimationConfig.ANIM_HOTSEAT_TRANSLATE;
@@ -54,6 +53,7 @@
 import com.android.launcher3.LauncherState.PageAlphaProvider;
 import com.android.launcher3.LauncherState.PageTranslationProvider;
 import com.android.launcher3.LauncherState.ScaleAndTranslation;
+import com.android.launcher3.anim.AnimatedFloat;
 import com.android.launcher3.anim.PendingAnimation;
 import com.android.launcher3.anim.PropertySetter;
 import com.android.launcher3.anim.SpringAnimationBuilder;
@@ -196,7 +196,7 @@
                 state.getWorkspaceBackgroundAlpha(mLauncher), LINEAR);
 
         SysUiScrim sysUiScrim = mLauncher.getRootView().getSysUiScrim();
-        propertySetter.setFloat(sysUiScrim, SYSUI_PROGRESS,
+        propertySetter.setFloat(sysUiScrim.getSysUIProgress(), AnimatedFloat.VALUE,
                 state.hasFlag(FLAG_HAS_SYS_UI_SCRIM) ? 1 : 0, LINEAR);
 
         propertySetter.setViewBackgroundColor(mLauncher.getScrimView(),
diff --git a/src/com/android/launcher3/graphics/SysUiScrim.java b/src/com/android/launcher3/graphics/SysUiScrim.java
index 21ebc98..a572a60 100644
--- a/src/com/android/launcher3/graphics/SysUiScrim.java
+++ b/src/com/android/launcher3/graphics/SysUiScrim.java
@@ -13,29 +13,30 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package com.android.launcher3.graphics;
 
+import static android.graphics.Paint.DITHER_FLAG;
+import static android.graphics.Paint.FILTER_BITMAP_FLAG;
+
 import static com.android.launcher3.config.FeatureFlags.KEYGUARD_ANIMATION;
-import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
 
 import android.animation.ObjectAnimator;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
-import android.graphics.Color;
 import android.graphics.LinearGradient;
 import android.graphics.Paint;
 import android.graphics.Rect;
 import android.graphics.RectF;
 import android.graphics.Shader;
-import android.graphics.drawable.Drawable;
 import android.util.DisplayMetrics;
-import android.util.FloatProperty;
 import android.view.View;
 
+import androidx.annotation.ColorInt;
+
 import com.android.launcher3.BaseDraggingActivity;
 import com.android.launcher3.DeviceProfile;
 import com.android.launcher3.R;
+import com.android.launcher3.anim.AnimatedFloat;
 import com.android.launcher3.testing.shared.ResourceUtils;
 import com.android.launcher3.util.ScreenOnTracker;
 import com.android.launcher3.util.ScreenOnTracker.ScreenOnListener;
@@ -46,33 +47,6 @@
  */
 public class SysUiScrim implements View.OnAttachStateChangeListener {
 
-    public static final FloatProperty<SysUiScrim> SYSUI_PROGRESS =
-            new FloatProperty<SysUiScrim>("sysUiProgress") {
-                @Override
-                public Float get(SysUiScrim scrim) {
-                    return scrim.mSysUiProgress;
-                }
-
-                @Override
-                public void setValue(SysUiScrim scrim, float value) {
-                    scrim.setSysUiProgress(value);
-                }
-            };
-
-    private static final FloatProperty<SysUiScrim> SYSUI_ANIM_MULTIPLIER =
-            new FloatProperty<SysUiScrim>("sysUiAnimMultiplier") {
-                @Override
-                public Float get(SysUiScrim scrim) {
-                    return scrim.mSysUiAnimMultiplier;
-                }
-
-                @Override
-                public void setValue(SysUiScrim scrim, float value) {
-                    scrim.mSysUiAnimMultiplier = value;
-                    scrim.reapplySysUiAlpha();
-                }
-            };
-
     /**
      * Receiver used to get a signal that the user unlocked their device.
      */
@@ -92,44 +66,52 @@
         }
     };
 
-    private static final int MAX_HOTSEAT_SCRIM_ALPHA = 100;
-    private static final int ALPHA_MASK_HEIGHT_DP = 500;
-    private static final int ALPHA_MASK_BITMAP_DP = 200;
-    private static final int ALPHA_MASK_WIDTH_DP = 2;
+    private static final int MAX_SYSUI_SCRIM_ALPHA = 255;
+    private static final int ALPHA_MASK_BITMAP_WIDTH_DP = 2;
+
+    private static final int BOTTOM_MASK_HEIGHT_DP = 200;
+    private static final int TOP_MASK_HEIGHT_DP = 100;
 
     private boolean mDrawTopScrim, mDrawBottomScrim;
 
-    private final RectF mFinalMaskRect = new RectF();
-    private final Paint mBottomMaskPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
-    private final Bitmap mBottomMask;
-    private final int mMaskHeight;
+    private final RectF mTopMaskRect = new RectF();
+    private final Paint mTopMaskPaint = new Paint(FILTER_BITMAP_FLAG | DITHER_FLAG);
+    private final Bitmap mTopMaskBitmap;
+    private final int mTopMaskHeight;
+
+    private final RectF mBottomMaskRect = new RectF();
+    private final Paint mBottomMaskPaint = new Paint(FILTER_BITMAP_FLAG | DITHER_FLAG);
+    private final Bitmap mBottomMaskBitmap;
+    private final int mBottomMaskHeight;
 
     private final View mRoot;
     private final BaseDraggingActivity mActivity;
-    private final Drawable mTopScrim;
-
-    private float mSysUiProgress = 1;
-    private boolean mHideSysUiScrim;
+    private final boolean mHideSysUiScrim;
 
     private boolean mAnimateScrimOnNextDraw = false;
-    private float mSysUiAnimMultiplier = 1;
+    private final AnimatedFloat mSysUiAnimMultiplier = new AnimatedFloat(this::reapplySysUiAlpha);
+    private final AnimatedFloat mSysUiProgress = new AnimatedFloat(this::reapplySysUiAlpha);
 
     public SysUiScrim(View view) {
         mRoot = view;
         mActivity = BaseDraggingActivity.fromContext(view.getContext());
-        mMaskHeight = ResourceUtils.pxFromDp(ALPHA_MASK_BITMAP_DP,
-                view.getResources().getDisplayMetrics());
-        mTopScrim = Themes.getAttrDrawable(view.getContext(), R.attr.workspaceStatusBarScrim);
-        if (mTopScrim != null) {
-            mTopScrim.setDither(true);
-            mBottomMask = createDitheredAlphaMask();
-            mHideSysUiScrim = false;
-        } else {
-            mBottomMask = null;
-            mHideSysUiScrim = true;
-        }
+        DisplayMetrics dm = mActivity.getResources().getDisplayMetrics();
 
-        view.addOnAttachStateChangeListener(this);
+        mTopMaskHeight = ResourceUtils.pxFromDp(TOP_MASK_HEIGHT_DP, dm);
+        mBottomMaskHeight = ResourceUtils.pxFromDp(BOTTOM_MASK_HEIGHT_DP, dm);
+        mHideSysUiScrim = Themes.getAttrBoolean(view.getContext(), R.attr.isWorkspaceDarkText);
+
+        mTopMaskBitmap = mHideSysUiScrim ? null : createDitheredAlphaMask(mTopMaskHeight,
+                new int[]{0x50FFFFFF, 0x0AFFFFFF, 0x00FFFFFF},
+                new float[]{0f, 0.7f, 1f});
+        mTopMaskPaint.setColor(0xFF222222);
+        mBottomMaskBitmap = mHideSysUiScrim ? null : createDitheredAlphaMask(mBottomMaskHeight,
+                new int[]{0x00FFFFFF, 0x2FFFFFFF},
+                new float[]{0f, 1f});
+
+        if (!KEYGUARD_ANIMATION.get() && !mHideSysUiScrim) {
+            view.addOnAttachStateChangeListener(this);
+        }
     }
 
     /**
@@ -137,16 +119,16 @@
      */
     public void draw(Canvas canvas) {
         if (!mHideSysUiScrim) {
-            if (mSysUiProgress <= 0) {
+            if (mSysUiProgress.value <= 0) {
                 mAnimateScrimOnNextDraw = false;
                 return;
             }
 
             if (mAnimateScrimOnNextDraw) {
-                mSysUiAnimMultiplier = 0;
+                mSysUiAnimMultiplier.value = 0;
                 reapplySysUiAlphaNoInvalidate();
 
-                ObjectAnimator oa = createSysuiMultiplierAnim(1);
+                ObjectAnimator oa = mSysUiAnimMultiplier.animateToValue(1);
                 oa.setDuration(600);
                 oa.setStartDelay(mActivity.getWindow().getTransitionBackgroundFadeDuration());
                 oa.start();
@@ -154,21 +136,26 @@
             }
 
             if (mDrawTopScrim) {
-                mTopScrim.draw(canvas);
+                canvas.drawBitmap(mTopMaskBitmap, null, mTopMaskRect, mTopMaskPaint);
             }
             if (mDrawBottomScrim) {
-                canvas.drawBitmap(mBottomMask, null, mFinalMaskRect, mBottomMaskPaint);
+                canvas.drawBitmap(mBottomMaskBitmap, null, mBottomMaskRect, mBottomMaskPaint);
             }
         }
     }
 
     /**
-     * @return an ObjectAnimator that controls the fade in/out of the sys ui scrim.
+     * Returns the sysui multiplier property for controlling fade in/out of the scrim
      */
-    public ObjectAnimator createSysuiMultiplierAnim(float... values) {
-        ObjectAnimator anim = ObjectAnimator.ofFloat(this, SYSUI_ANIM_MULTIPLIER, values);
-        anim.setAutoCancel(true);
-        return anim;
+    public AnimatedFloat getSysUIMultiplier() {
+        return mSysUiAnimMultiplier;
+    }
+
+    /**
+     * Returns the sysui progress property for controlling fade in/out of the scrim
+     */
+    public AnimatedFloat getSysUIProgress() {
+        return mSysUiProgress;
     }
 
     /**
@@ -180,44 +167,26 @@
      */
     public void onInsetsChanged(Rect insets) {
         DeviceProfile dp = mActivity.getDeviceProfile();
-        mDrawTopScrim = mTopScrim != null && insets.top > 0;
-        mDrawBottomScrim = mBottomMask != null
-                && !dp.isVerticalBarLayout()
-                && !dp.isGestureMode
-                && !dp.isTaskbarPresent;
+        mDrawTopScrim = insets.top > 0;
+        mDrawBottomScrim = !dp.isVerticalBarLayout() && !dp.isGestureMode && !dp.isTaskbarPresent;
     }
 
     @Override
     public void onViewAttachedToWindow(View view) {
-        if (!KEYGUARD_ANIMATION.get() && mTopScrim != null) {
-            ScreenOnTracker.INSTANCE.get(mActivity).addListener(mScreenOnListener);
-        }
+        ScreenOnTracker.INSTANCE.get(mActivity).addListener(mScreenOnListener);
     }
 
     @Override
     public void onViewDetachedFromWindow(View view) {
-        if (!KEYGUARD_ANIMATION.get() && mTopScrim != null) {
-            ScreenOnTracker.INSTANCE.get(mActivity).removeListener(mScreenOnListener);
-        }
+        ScreenOnTracker.INSTANCE.get(mActivity).removeListener(mScreenOnListener);
     }
 
     /**
      * Set the width and height of the view being scrimmed
-     * @param w
-     * @param h
      */
     public void setSize(int w, int h) {
-        if (mTopScrim != null) {
-            mTopScrim.setBounds(0, 0, w, h);
-            mFinalMaskRect.set(0, h - mMaskHeight, w, h);
-        }
-    }
-
-    private void setSysUiProgress(float progress) {
-        if (progress != mSysUiProgress) {
-            mSysUiProgress = progress;
-            reapplySysUiAlpha();
-        }
+        mTopMaskRect.set(0, 0, w, mTopMaskHeight);
+        mBottomMaskRect.set(0, h - mBottomMaskHeight, w, h);
     }
 
     private void reapplySysUiAlpha() {
@@ -228,29 +197,21 @@
     }
 
     private void reapplySysUiAlphaNoInvalidate() {
-        float factor = mSysUiProgress * mSysUiAnimMultiplier;
-        mBottomMaskPaint.setAlpha(Math.round(MAX_HOTSEAT_SCRIM_ALPHA * factor));
-        if (mTopScrim != null) {
-            mTopScrim.setAlpha(Math.round(255 * factor));
-        }
+        float factor = mSysUiProgress.value * mSysUiAnimMultiplier.value;
+        mBottomMaskPaint.setAlpha(Math.round(MAX_SYSUI_SCRIM_ALPHA * factor));
+        mTopMaskPaint.setAlpha(Math.round(MAX_SYSUI_SCRIM_ALPHA * factor));
     }
 
-    private Bitmap createDitheredAlphaMask() {
+    private Bitmap createDitheredAlphaMask(int height, @ColorInt int[] colors, float[] positions) {
         DisplayMetrics dm = mActivity.getResources().getDisplayMetrics();
-        int width = ResourceUtils.pxFromDp(ALPHA_MASK_WIDTH_DP, dm);
-        int gradientHeight = ResourceUtils.pxFromDp(ALPHA_MASK_HEIGHT_DP, dm);
-        Bitmap dst = Bitmap.createBitmap(width, mMaskHeight, Bitmap.Config.ALPHA_8);
+        int width = ResourceUtils.pxFromDp(ALPHA_MASK_BITMAP_WIDTH_DP, dm);
+        Bitmap dst = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
         Canvas c = new Canvas(dst);
-        Paint paint = new Paint(Paint.DITHER_FLAG);
-        LinearGradient lg = new LinearGradient(0, 0, 0, gradientHeight,
-                new int[]{
-                        0x00FFFFFF,
-                        setColorAlphaBound(Color.WHITE, (int) (0xFF * 0.95)),
-                        0xFFFFFFFF},
-                new float[]{0f, 0.8f, 1f},
-                Shader.TileMode.CLAMP);
+        Paint paint = new Paint(DITHER_FLAG);
+        LinearGradient lg = new LinearGradient(0, 0, 0, height,
+                colors, positions, Shader.TileMode.CLAMP);
         paint.setShader(lg);
-        c.drawRect(0, 0, width, gradientHeight, paint);
+        c.drawPaint(paint);
         return dst;
     }
 }
diff --git a/src/com/android/launcher3/touch/AbstractStateChangeTouchController.java b/src/com/android/launcher3/touch/AbstractStateChangeTouchController.java
index c499e35..512ad80 100644
--- a/src/com/android/launcher3/touch/AbstractStateChangeTouchController.java
+++ b/src/com/android/launcher3/touch/AbstractStateChangeTouchController.java
@@ -384,8 +384,8 @@
         } else {
             logReachedState(targetState);
         }
-        mLauncher.getRootView().getSysUiScrim().createSysuiMultiplierAnim(
-                1f).setDuration(0).start();
+        mLauncher.getRootView().getSysUiScrim().getSysUIMultiplier().animateToValue(1f)
+                .setDuration(0).start();
     }
 
     private void logReachedState(LauncherState targetState) {