Prevent recoloring toolbar background
We don't want each app customizing the look of the toolbar, but they
are allowed to make the background transparent.
Fixes: 140063106
Test: Manually
Change-Id: I38e1e738b193e6f3b607d8b8a85424c76d82b927
diff --git a/car-chassis-lib/res/values/attrs.xml b/car-chassis-lib/res/values/attrs.xml
index 3491748..58c6fd0 100644
--- a/car-chassis-lib/res/values/attrs.xml
+++ b/car-chassis-lib/res/values/attrs.xml
@@ -32,6 +32,8 @@
<enum name="subpage_custom" value="2"/>
<enum name="search" value="3"/>
</attr>
+ <!-- Whether or not the toolbar should have a background. Default true. -->
+ <attr name="showBackground" format="boolean"/>
</declare-styleable>
<!-- Theme attribute to specifying a default style for all chassisToolbars -->
diff --git a/car-chassis-lib/res/values/styles.xml b/car-chassis-lib/res/values/styles.xml
index 2ba1a4c..da3be41 100644
--- a/car-chassis-lib/res/values/styles.xml
+++ b/car-chassis-lib/res/values/styles.xml
@@ -30,6 +30,7 @@
<item name="android:layout_height">@dimen/chassis_tab_icon_height</item>
<item name="android:scaleType">fitCenter</item>
<item name="android:tint">@color/chassis_tab_item_selector</item>
+ <item name="android:tintMode">src_in</item>
</style>
<style name="ChassisTabItemBackground">
diff --git a/car-chassis-lib/src/com/android/car/chassis/Toolbar.java b/car-chassis-lib/src/com/android/car/chassis/Toolbar.java
index ae51e92..0cb2373 100644
--- a/car-chassis-lib/src/com/android/car/chassis/Toolbar.java
+++ b/car-chassis-lib/src/com/android/car/chassis/Toolbar.java
@@ -17,6 +17,7 @@
import android.content.Context;
import android.content.res.TypedArray;
+import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
@@ -130,7 +131,7 @@
mTitle.setText(a.getString(R.styleable.ChassisToolbar_title));
setLogo(a.getResourceId(R.styleable.ChassisToolbar_logo, 0));
setButtons(a.getResourceId(R.styleable.ChassisToolbar_buttons, 0));
- setBackground(context.getDrawable(R.color.chassis_toolbar_background_color));
+ setBackgroundShown(a.getBoolean(R.styleable.ChassisToolbar_showBackground, true));
mShowButtonsWhileSearching = a.getBoolean(
R.styleable.ChassisToolbar_showButtonsWhileSearching, false);
String searchHint = a.getString(R.styleable.ChassisToolbar_searchHint);
@@ -246,6 +247,27 @@
}
/**
+ * setBackground is disallowed, to prevent apps from deviating from the intended style too much.
+ */
+ @Override
+ public void setBackground(Drawable d) {
+ throw new UnsupportedOperationException(
+ "You can not change the background of a chassis toolbar, use "
+ + "setBackgroundShown(boolean) or an RRO instead.");
+ }
+
+ /**
+ * Show/hide the background. When hidden, the toolbar is completely transparent.
+ */
+ public void setBackgroundShown(boolean shown) {
+ if (shown) {
+ super.setBackground(getContext().getDrawable(R.color.chassis_toolbar_background_color));
+ } else {
+ super.setBackground(null);
+ }
+ }
+
+ /**
* Sets the buttons to be shown. Click events for these buttons will be received in
* {@link Listener#onCustomButtonPressed(View)}.
*