Fix rotation button background not showing after first boot.

KeyButtonView#setDarkIntensity() does not initialize mHasOvalBg and
mOvalBgPaint until the drawable is attached to the view, which happens
after KeyButtonView#draw() is called in the first run.

Test: Reboot, make sure background of the rotation button shows at first
run
Fixes: 134070810

Change-Id: Ib38660cde691f3d36f502023ffe082f0becd95eb
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/FloatingRotationButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/FloatingRotationButton.java
index f5016da..6bbeffa 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/FloatingRotationButton.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/FloatingRotationButton.java
@@ -16,8 +16,10 @@
 
 package com.android.systemui.statusbar.phone;
 
+import android.annotation.ColorInt;
 import android.content.Context;
 import android.content.res.Resources;
+import android.graphics.Color;
 import android.graphics.PixelFormat;
 import android.view.ContextThemeWrapper;
 import android.view.Gravity;
@@ -26,6 +28,7 @@
 import android.view.View;
 import android.view.WindowManager;
 
+import com.android.settingslib.Utils;
 import com.android.systemui.R;
 import com.android.systemui.statusbar.policy.KeyButtonDrawable;
 import com.android.systemui.statusbar.policy.KeyButtonView;
@@ -33,6 +36,8 @@
 /** Containing logic for the rotation button on the physical left bottom corner of the screen. */
 public class FloatingRotationButton implements RotationButton {
 
+    private static final float BACKGROUND_ALPHA = 0.92f;
+
     private final Context mContext;
     private final WindowManager mWindowManager;
     private final KeyButtonView mKeyButtonView;
@@ -151,8 +156,18 @@
     public KeyButtonDrawable getImageDrawable() {
         Context context = new ContextThemeWrapper(mContext.getApplicationContext(),
                 mRotationButtonController.getStyleRes());
-        return KeyButtonDrawable.create(context, R.drawable.ic_sysbar_rotate_button,
-                false /* shadow */, true /* hasOvalBg */);
+        final int dualToneDarkTheme = Utils.getThemeAttr(context, R.attr.darkIconTheme);
+        final int dualToneLightTheme = Utils.getThemeAttr(context, R.attr.lightIconTheme);
+        Context lightContext = new ContextThemeWrapper(context, dualToneLightTheme);
+        Context darkContext = new ContextThemeWrapper(context, dualToneDarkTheme);
+        @ColorInt int darkColor = Utils.getColorAttrDefaultColor(darkContext,
+                R.attr.singleToneColor);
+        Color ovalBackgroundColor = Color.valueOf(Color.red(darkColor), Color.green(darkColor),
+                Color.blue(darkColor), BACKGROUND_ALPHA);
+
+        return KeyButtonDrawable.create(lightContext,
+                Utils.getColorAttrDefaultColor(lightContext, R.attr.singleToneColor), darkColor,
+                R.drawable.ic_sysbar_rotate_button, false /* shadow */, ovalBackgroundColor);
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/RotationContextButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/RotationContextButton.java
index b117dec..24e7336 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/RotationContextButton.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/RotationContextButton.java
@@ -24,7 +24,6 @@
 import android.view.ContextThemeWrapper;
 import android.view.View;
 
-import com.android.systemui.shared.system.QuickStepContract;
 import com.android.systemui.statusbar.policy.KeyButtonDrawable;
 
 /** Containing logic for the rotation button in nav bar. */
@@ -61,7 +60,7 @@
         Context context = new ContextThemeWrapper(getContext().getApplicationContext(),
                 mRotationButtonController.getStyleRes());
         return KeyButtonDrawable.create(context, mIconResId, false /* shadow */,
-                QuickStepContract.isGesturalMode(mNavBarMode));
+                null /* ovalBackgroundColor */);
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java
index 568de63..8fcaa67 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java
@@ -82,9 +82,9 @@
     private AnimatedVectorDrawable mAnimatedDrawable;
 
     public KeyButtonDrawable(Drawable d, @ColorInt int lightColor, @ColorInt int darkColor,
-            boolean horizontalFlip, boolean hasOvalBg) {
+            boolean horizontalFlip, Color ovalBackgroundColor) {
         this(d, new ShadowDrawableState(lightColor, darkColor,
-                d instanceof AnimatedVectorDrawable, horizontalFlip, hasOvalBg));
+                d instanceof AnimatedVectorDrawable, horizontalFlip, ovalBackgroundColor));
     }
 
     private KeyButtonDrawable(Drawable d, ShadowDrawableState state) {
@@ -166,7 +166,7 @@
     public void setColorFilter(ColorFilter colorFilter) {
         mIconPaint.setColorFilter(colorFilter);
         if (mAnimatedDrawable != null) {
-            if (mState.mHasOvalBg) {
+            if (hasOvalBg()) {
                 mAnimatedDrawable.setColorFilter(
                         new PorterDuffColorFilter(mState.mLightColor, PorterDuff.Mode.SRC_IN));
             } else {
@@ -212,15 +212,6 @@
         return mState.mBaseWidth + (mState.mShadowSize + Math.abs(mState.mShadowOffsetX)) * 2;
     }
 
-    /** Return if the drawable has oval background. */
-    public boolean hasOvalBg() {
-        return mState.mHasOvalBg;
-    }
-
-    public int getDarkColor() {
-        return mState.mDarkColor;
-    }
-
     public boolean canAnimate() {
         return mState.mSupportsAnimation;
     }
@@ -290,6 +281,14 @@
         return mState.canApplyTheme();
     }
 
+    @ColorInt int getDrawableBackgroundColor() {
+        return mState.mOvalBackgroundColor.toArgb();
+    }
+
+    boolean hasOvalBg() {
+        return mState.mOvalBackgroundColor != null;
+    }
+
     private void regenerateBitmapIconCache() {
         final int width = getIntrinsicWidth();
         final int height = getIntrinsicHeight();
@@ -394,16 +393,16 @@
         final int mLightColor;
         final int mDarkColor;
         final boolean mSupportsAnimation;
-        final boolean mHasOvalBg;
+        final Color mOvalBackgroundColor;
 
         public ShadowDrawableState(@ColorInt int lightColor, @ColorInt int darkColor,
-                boolean animated, boolean horizontalFlip, boolean hasOvalBg) {
+                boolean animated, boolean horizontalFlip, Color ovalBackgroundColor) {
             mLightColor = lightColor;
             mDarkColor = darkColor;
             mSupportsAnimation = animated;
             mAlpha = 255;
             mHorizontalFlip = horizontalFlip;
-            mHasOvalBg = hasOvalBg;
+            mOvalBackgroundColor = ovalBackgroundColor;
         }
 
         @Override
@@ -428,16 +427,17 @@
      * @param ctx Context to get the drawable and determine the dark and light theme
      * @param icon the icon resource id
      * @param hasShadow if a shadow will appear with the drawable
-     * @param hasOvalBg if an oval bg will be drawn
+     * @param ovalBackgroundColor the color of the oval bg that will be drawn
      * @return KeyButtonDrawable
      */
     public static KeyButtonDrawable create(@NonNull Context ctx, @DrawableRes int icon,
-            boolean hasShadow, boolean hasOvalBg) {
+            boolean hasShadow, Color ovalBackgroundColor) {
         final int dualToneDarkTheme = Utils.getThemeAttr(ctx, R.attr.darkIconTheme);
         final int dualToneLightTheme = Utils.getThemeAttr(ctx, R.attr.lightIconTheme);
         Context lightContext = new ContextThemeWrapper(ctx, dualToneLightTheme);
         Context darkContext = new ContextThemeWrapper(ctx, dualToneDarkTheme);
-        return KeyButtonDrawable.create(lightContext, darkContext, icon, hasShadow, hasOvalBg);
+        return KeyButtonDrawable.create(lightContext, darkContext, icon, hasShadow,
+                ovalBackgroundColor);
     }
 
     /**
@@ -446,7 +446,7 @@
      */
     public static KeyButtonDrawable create(@NonNull Context ctx, @DrawableRes int icon,
             boolean hasShadow) {
-        return create(ctx, icon, hasShadow, false /* hasOvalBg */);
+        return create(ctx, icon, hasShadow, null /* ovalBackgroundColor */);
     }
 
     /**
@@ -454,11 +454,11 @@
      * {@link #create(Context, int, boolean, boolean)}.
      */
     public static KeyButtonDrawable create(Context lightContext, Context darkContext,
-            @DrawableRes int iconResId, boolean hasShadow, boolean hasOvalBg) {
+            @DrawableRes int iconResId, boolean hasShadow, Color ovalBackgroundColor) {
         return create(lightContext,
             Utils.getColorAttrDefaultColor(lightContext, R.attr.singleToneColor),
             Utils.getColorAttrDefaultColor(darkContext, R.attr.singleToneColor),
-            iconResId, hasShadow, hasOvalBg);
+            iconResId, hasShadow, ovalBackgroundColor);
     }
 
     /**
@@ -467,12 +467,12 @@
      */
     public static KeyButtonDrawable create(Context context, @ColorInt int lightColor,
             @ColorInt int darkColor, @DrawableRes int iconResId, boolean hasShadow,
-            boolean hasOvalBg) {
+            Color ovalBackgroundColor) {
         final Resources res = context.getResources();
         boolean isRtl = res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
         Drawable d = context.getDrawable(iconResId);
         final KeyButtonDrawable drawable = new KeyButtonDrawable(d, lightColor, darkColor,
-                isRtl && d.isAutoMirrored(), hasOvalBg);
+                isRtl && d.isAutoMirrored(), ovalBackgroundColor);
         if (hasShadow) {
             int offsetX = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_x);
             int offsetY = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_y);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java
index c9579fd..64b2842 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java
@@ -79,6 +79,7 @@
     private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
     private final InputManager mInputManager;
     private final Paint mOvalBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
+    private float mDarkIntensity;
     private boolean mHasOvalBg = false;
 
     private final Runnable mCheckLongPress = new Runnable() {
@@ -304,6 +305,23 @@
         return true;
     }
 
+    @Override
+    public void setImageDrawable(Drawable drawable) {
+        super.setImageDrawable(drawable);
+
+        if (drawable == null) {
+            return;
+        }
+        KeyButtonDrawable keyButtonDrawable = (KeyButtonDrawable) drawable;
+        keyButtonDrawable.setDarkIntensity(mDarkIntensity);
+        mHasOvalBg = keyButtonDrawable.hasOvalBg();
+        if (mHasOvalBg) {
+            mOvalBgPaint.setColor(keyButtonDrawable.getDrawableBackgroundColor());
+        }
+        mRipple.setType(keyButtonDrawable.hasOvalBg() ? KeyButtonRipple.Type.OVAL
+                : KeyButtonRipple.Type.ROUNDED_RECT);
+    }
+
     public void playSoundEffect(int soundConstant) {
         if (!mPlaySounds) return;
         mAudioManager.playSoundEffect(soundConstant, ActivityManager.getCurrentUser());
@@ -360,17 +378,11 @@
 
     @Override
     public void setDarkIntensity(float darkIntensity) {
+        mDarkIntensity = darkIntensity;
+
         Drawable drawable = getDrawable();
         if (drawable != null) {
-            KeyButtonDrawable keyButtonDrawable = (KeyButtonDrawable) drawable;
-            keyButtonDrawable.setDarkIntensity(darkIntensity);
-            mHasOvalBg = keyButtonDrawable.hasOvalBg();
-            if (mHasOvalBg) {
-                mOvalBgPaint.setColor(keyButtonDrawable.getDarkColor());
-            }
-            mRipple.setType(keyButtonDrawable.hasOvalBg() ? KeyButtonRipple.Type.OVAL
-                    : KeyButtonRipple.Type.ROUNDED_RECT);
-
+            ((KeyButtonDrawable) drawable).setDarkIntensity(darkIntensity);
             // Since we reuse the same drawable for multiple views, we need to invalidate the view
             // manually.
             invalidate();
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarContextTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarContextTest.java
index cb70a1f..be69f5f 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarContextTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarContextTest.java
@@ -180,9 +180,9 @@
         final Drawable d = mock(Drawable.class);
         final ContextualButton button = spy(mBtn0);
         final KeyButtonDrawable kbd1 = spy(new KeyButtonDrawable(d, unusedColor, unusedColor,
-                false /* horizontalFlip */, false /* hasOvalBg */));
+                false /* horizontalFlip */, null /* ovalBackgroundColor */));
         final KeyButtonDrawable kbd2 = spy(new KeyButtonDrawable(d, unusedColor, unusedColor,
-                false /* horizontalFlip */, false /* hasOvalBg */));
+                false /* horizontalFlip */, null /* ovalBackgroundColor */));
         kbd1.setDarkIntensity(TEST_DARK_INTENSITY);
         kbd2.setDarkIntensity(0f);