From 39056e5dc4b068745c2d9be69e5cd0c008bdc117 Mon Sep 17 00:00:00 2001 From: Maurice Lam Date: Thu, 2 Mar 2017 17:41:27 -0800 Subject: [PATCH] Make status bar white on GLIF pixel theme Add attributes suwBackgroundBaseColor and suwBackgroundPatterned to GlifLayout, which allows the GLIF Pixel theme to display a solid off-white status bar background instead of the pattern in regular GLIF. Test: ./gradlew connectedAndroidTest test Bug: 35446596 Change-Id: Icbf3608882367ce00188fd124df5d2ba0de00864 --- library/main/res/values/attrs.xml | 2 + library/main/res/values/styles.xml | 8 ++ .../android/setupwizardlib/GlifLayout.java | 71 +++++++++++++++- .../setupwizardlib/test/GlifLayoutTest.java | 7 ++ .../setupwizardlib/GlifLayoutTest.java | 80 ++++++++++++++++++- 5 files changed, 161 insertions(+), 7 deletions(-) diff --git a/library/main/res/values/attrs.xml b/library/main/res/values/attrs.xml index afeb16c..0102dc1 100644 --- a/library/main/res/values/attrs.xml +++ b/library/main/res/values/attrs.xml @@ -69,6 +69,8 @@ + + diff --git a/library/main/res/values/styles.xml b/library/main/res/values/styles.xml index 9a32c38..93720eb 100644 --- a/library/main/res/values/styles.xml +++ b/library/main/res/values/styles.xml @@ -20,10 +20,18 @@ diff --git a/library/main/src/com/android/setupwizardlib/GlifLayout.java b/library/main/src/com/android/setupwizardlib/GlifLayout.java index 2776024..037a148 100644 --- a/library/main/src/com/android/setupwizardlib/GlifLayout.java +++ b/library/main/src/com/android/setupwizardlib/GlifLayout.java @@ -20,11 +20,13 @@ import android.annotation.TargetApi; import android.content.Context; import android.content.res.ColorStateList; import android.content.res.TypedArray; +import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Build.VERSION_CODES; import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; @@ -64,6 +66,14 @@ public class GlifLayout extends TemplateLayout { private ColorStateList mPrimaryColor; + private boolean mBackgroundPatterned = true; + + /** + * The color of the background. If null, the color will inherit from mPrimaryColor. + */ + @Nullable + private ColorStateList mBackgroundBaseColor; + public GlifLayout(Context context) { this(context, 0, 0); } @@ -105,6 +115,14 @@ public class GlifLayout extends TemplateLayout { setPrimaryColor(primaryColor); } + ColorStateList backgroundColor = + a.getColorStateList(R.styleable.SuwGlifLayout_suwBackgroundBaseColor); + setBackgroundBaseColor(backgroundColor); + + boolean backgroundPatterned = + a.getBoolean(R.styleable.SuwGlifLayout_suwBackgroundPatterned, true); + setBackgroundPatterned(backgroundPatterned); + a.recycle(); } @@ -169,7 +187,7 @@ public class GlifLayout extends TemplateLayout { */ public void setPrimaryColor(@NonNull ColorStateList color) { mPrimaryColor = color; - setGlifPatternColor(color); + updateBackground(); getMixin(ProgressBarMixin.class).setColor(color); } @@ -177,11 +195,56 @@ public class GlifLayout extends TemplateLayout { return mPrimaryColor; } - private void setGlifPatternColor(@NonNull ColorStateList color) { + /** + * Sets the base color of the background view, which is the status bar for phones and the full- + * screen background for tablets. If {@link #isBackgroundPatterned()} is true, the pattern will + * be drawn with this color. + * + * @param color The color to use as the base color of the background. If {@code null}, + * {@link #getPrimaryColor()} will be used. + */ + public void setBackgroundBaseColor(@Nullable ColorStateList color) { + mBackgroundBaseColor = color; + updateBackground(); + } + + /** + * @return The base color of the background. {@code null} indicates the background will be drawn + * with {@link #getPrimaryColor()}. + */ + @Nullable + public ColorStateList getBackgroundBaseColor() { + return mBackgroundBaseColor; + } + + /** + * Sets whether the background should be {@link GlifPatternDrawable}. If {@code false}, the + * background will be a solid color. + */ + public void setBackgroundPatterned(boolean patterned) { + mBackgroundPatterned = patterned; + updateBackground(); + } + + /** + * @return True if this view uses {@link GlifPatternDrawable} as background. + */ + public boolean isBackgroundPatterned() { + return mBackgroundPatterned; + } + + private void updateBackground() { final View patternBg = findManagedViewById(R.id.suw_pattern_bg); if (patternBg != null) { - final GlifPatternDrawable background = - new GlifPatternDrawable(color.getDefaultColor()); + int backgroundColor = 0; + if (mBackgroundBaseColor != null) { + backgroundColor = mBackgroundBaseColor.getDefaultColor(); + } else if (mPrimaryColor != null) { + backgroundColor = mPrimaryColor.getDefaultColor(); + } + Drawable background = mBackgroundPatterned + ? new GlifPatternDrawable(backgroundColor) + : new ColorDrawable(backgroundColor); if (patternBg instanceof StatusBarBackgroundLayout) { ((StatusBarBackgroundLayout) patternBg).setStatusBarBackground(background); } else { diff --git a/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifLayoutTest.java b/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifLayoutTest.java index 78c53e8..ae3a0b9 100644 --- a/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifLayoutTest.java +++ b/library/test/instrumentation/src/com/android/setupwizardlib/test/GlifLayoutTest.java @@ -17,6 +17,7 @@ package com.android.setupwizardlib.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -99,6 +100,12 @@ public class GlifLayoutTest { assertEquals("Icon should be center aligned on GLIF Pixel theme", Gravity.CENTER_HORIZONTAL, parent.getGravity() & Gravity.CENTER_HORIZONTAL); } + + assertEquals("Status bar color should be white in GLIF Pixel theme", + "fffafafa", + Integer.toHexString(glifLayout.getBackgroundBaseColor().getDefaultColor())); + assertFalse("GLIF Pixel theme shuold not have patterned background", + glifLayout.isBackgroundPatterned()); } private void assertDefaultTemplateInflated(GlifLayout layout) { diff --git a/library/test/robotest/src/com/android/setupwizardlib/GlifLayoutTest.java b/library/test/robotest/src/com/android/setupwizardlib/GlifLayoutTest.java index a36ce76..6ab8259 100644 --- a/library/test/robotest/src/com/android/setupwizardlib/GlifLayoutTest.java +++ b/library/test/robotest/src/com/android/setupwizardlib/GlifLayoutTest.java @@ -16,16 +16,21 @@ package com.android.setupwizardlib; +import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.robolectric.RuntimeEnvironment.application; import android.content.Context; import android.content.res.ColorStateList; import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; import android.os.Build; import android.support.annotation.IdRes; import android.view.ContextThemeWrapper; @@ -39,6 +44,7 @@ import com.android.setupwizardlib.template.ColoredHeaderMixin; import com.android.setupwizardlib.template.HeaderMixin; import com.android.setupwizardlib.template.IconMixin; import com.android.setupwizardlib.template.ProgressBarMixin; +import com.android.setupwizardlib.view.StatusBarBackgroundLayout; import org.junit.Before; import org.junit.Test; @@ -123,9 +129,66 @@ public class GlifLayoutTest { ColorStateList.valueOf(Color.RED), progressBar.getProgressBackgroundTintList()); } - final View patternBg = layout.findManagedViewById(R.id.suw_pattern_bg); - final GlifPatternDrawable background = (GlifPatternDrawable) patternBg.getBackground(); - assertEquals(Color.RED, background.getColor()); + assertEquals(Color.RED, ((GlifPatternDrawable) getTabletBackground(layout)).getColor()); + } + + @Test + public void testSetBackgroundBaseColor() { + GlifLayout layout = new GlifLayout(mContext); + layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE)); + layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED)); + + assertEquals(Color.RED, ((GlifPatternDrawable) getPhoneBackground(layout)).getColor()); + assertEquals(Color.RED, layout.getBackgroundBaseColor().getDefaultColor()); + } + + @Config(qualifiers = "sw600dp") + @Test + public void testSetBackgroundBaseColorTablet() { + GlifLayout layout = new GlifLayout(mContext); + layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE)); + layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED)); + + assertEquals(Color.RED, ((GlifPatternDrawable) getTabletBackground(layout)).getColor()); + assertEquals(Color.RED, layout.getBackgroundBaseColor().getDefaultColor()); + } + + @Test + public void testSetBackgroundPatternedTrue() { + GlifLayout layout = new GlifLayout(mContext); + layout.setBackgroundPatterned(true); + + assertThat(getPhoneBackground(layout), instanceOf(GlifPatternDrawable.class)); + assertTrue("Background should be patterned", layout.isBackgroundPatterned()); + } + + @Test + public void testSetBackgroundPatternedFalse() { + GlifLayout layout = new GlifLayout(mContext); + layout.setBackgroundPatterned(false); + + assertThat(getPhoneBackground(layout), instanceOf(ColorDrawable.class)); + assertFalse("Background should not be patterned", layout.isBackgroundPatterned()); + } + + @Config(qualifiers = "sw600dp") + @Test + public void testSetBackgroundPatternedTrueTablet() { + GlifLayout layout = new GlifLayout(mContext); + layout.setBackgroundPatterned(true); + + assertThat(getTabletBackground(layout), instanceOf(GlifPatternDrawable.class)); + assertTrue("Background should be patterned", layout.isBackgroundPatterned()); + } + + @Config(qualifiers = "sw600dp") + @Test + public void testSetBackgroundPatternedFalseTablet() { + GlifLayout layout = new GlifLayout(mContext); + layout.setBackgroundPatterned(false); + + assertThat(getTabletBackground(layout), instanceOf(ColorDrawable.class)); + assertFalse("Background should not be patterned", layout.isBackgroundPatterned()); } @Test @@ -161,6 +224,17 @@ public class GlifLayoutTest { layout.getMixin(ProgressBarMixin.class)); } + private Drawable getPhoneBackground(GlifLayout layout) { + final StatusBarBackgroundLayout patternBg = + (StatusBarBackgroundLayout) layout.findManagedViewById(R.id.suw_pattern_bg); + return patternBg.getStatusBarBackground(); + } + + private Drawable getTabletBackground(GlifLayout layout) { + final View patternBg = layout.findManagedViewById(R.id.suw_pattern_bg); + return patternBg.getBackground(); + } + private void assertDefaultTemplateInflated(GlifLayout layout) { View title = layout.findViewById(R.id.suw_layout_title); assertNotNull("@id/suw_layout_title should not be null", title);