Add support for multi-RRO generation

Bug: 140849291
Test: Manual build and deploy
Change-Id: I798f3948ab7e04efeb319953fcd9003a2ff29504
diff --git a/car-ui-lib/Android.mk b/car-ui-lib/Android.mk
index 6f280bb..28edddd 100644
--- a/car-ui-lib/Android.mk
+++ b/car-ui-lib/Android.mk
@@ -14,6 +14,27 @@
 # limitations under the License.
 #
 
+# Including generate_rros.mk utility.
+# Usage:
+#
+# LOCAL_PATH := $(call my-dir)
+#
+# CAR_UI_RRO_SET_NAME := sample
+# CAR_UI_RESOURCE_DIR := $(LOCAL_PATH)/res
+# CAR_UI_RRO_TARGETS := \
+#   com.your.package.name.1 \
+#   com.your.package.name.2 \
+#   com.your.package.name.3
+#
+# include $(CAR_UI_GENERATE_RRO_SET)
+#
+# Your AndroidManifest must use {{TARGET_PACKAGE_NAME}} and {{RRO_PACKAGE_NAME}}
+# tags, which will be replaced accordingly during build.
+
+CAR_UI_GENERATE_RRO_SET := $(call my-dir)/generate_rros.mk
+
+# Build car-ui library
+
 LOCAL_PATH:= $(call my-dir)
 
 include $(CLEAR_VARS)
diff --git a/car-ui-lib/generate_rros.mk b/car-ui-lib/generate_rros.mk
new file mode 100644
index 0000000..4e7931a
--- /dev/null
+++ b/car-ui-lib/generate_rros.mk
@@ -0,0 +1,49 @@
+#
+# Copyright (C) 2019 The Android Open-Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# Generates one RRO for a given package
+# $(1) target package name
+# $(2) name of the RRO set (e.g. "base")
+# $(3) resources folder
+define generate-rro
+  include $$(CLEAR_VARS)
+
+  rro_package_name := $(2)-$(subst .,-,$(1))
+  LOCAL_RESOURCE_DIR := $(3)
+  LOCAL_PACKAGE_NAME := $$(rro_package_name)
+  LOCAL_PRODUCT_MODULE := true
+  LOCAL_CERTIFICATE := platform
+  LOCAL_SDK_VERSION := current
+
+  gen := $$(call intermediates-dir-for,ETC,$$(rro_package_name))/AndroidManifest.xml
+  $$(gen): $(LOCAL_PATH)/AndroidManifest.xml
+	@echo Generate $$@
+	$$(hide) mkdir -p $$(dir $$@)
+	$$(hide) sed -e "s/{{TARGET_PACKAGE_NAME}}/$(1)/" \
+	             -e "s/{{RRO_PACKAGE_NAME}}/$(1).$(2).rro/" $$< > $$@
+  LOCAL_FULL_MANIFEST_FILE := $$(gen)
+
+  include $$(BUILD_RRO_PACKAGE)
+endef
+
+$(foreach t,\
+  $(CAR_UI_RRO_TARGETS),\
+  $(eval $(call generate-rro,$(t),$(CAR_UI_RRO_SET_NAME),$(CAR_UI_RESOURCE_DIR))))
+
+# Clear variables
+CAR_UI_RRO_SET_NAME :=
+CAR_UI_RESOURCE_DIR :=
+CAR_UI_RRO_TARGETS :=
diff --git a/car-ui-lib/tests/rro-base/Android.mk b/car-ui-lib/tests/rro-base/Android.mk
new file mode 100644
index 0000000..2d6730c
--- /dev/null
+++ b/car-ui-lib/tests/rro-base/Android.mk
@@ -0,0 +1,26 @@
+#
+# Copyright (C) 2019 The Android Open-Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+CAR_UI_RRO_SET_NAME := base
+CAR_UI_RESOURCE_DIR := $(LOCAL_PATH)/res
+CAR_UI_RRO_TARGETS := \
+  com.android.car.ui.paintbooth \
+  com.android.car.media \
+  com.android.car.dialer
+
+include $(CAR_UI_GENERATE_RRO_SET)
diff --git a/car-ui-lib/tests/rro-base/AndroidManifest.xml b/car-ui-lib/tests/rro-base/AndroidManifest.xml
new file mode 100644
index 0000000..b6b6ffb
--- /dev/null
+++ b/car-ui-lib/tests/rro-base/AndroidManifest.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+    Copyright 2019 The Android Open Source Project
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+
+<!--
+    Example to use Runtime Resource Overlay(RRO).
+
+    Step 1: Create a new project with AndroidManifest.xml file as shown below.
+        "{{TARGET_PACKAGE_NAME}}" will point to the package which will be overridden by new values.
+        "{{RRO_PACKAGE_NAME}}" will be the current package name of this apk.
+    Step 2: Create new values in the current package that will override the values in the target
+        apk. Path and resource name should be same as the target apk in order to override.
+        Look at car-ui-lib/res to see a list of resources available for customization.
+    Step 3: Update Android.mk variables as needed (see details at generate-rros.mk):
+        CAR_UI_RRO_SET_NAME: general name of this overlay, e.g: base.
+        CAR_UI_RESOURCE_DIR: location of the resources folder, e.g.: $(LOCAL_PATH)/res
+        CAR_UI_RRO_TARGETS: list of package names to overlay
+    Step 4: Build and generate the apk package for this project. Resulting RROs will be located at
+        $OUT/vendor/overlay. A full flashing of a device will install all of them, but they can be
+        installed individually (see below).
+    Step 5: Push the package to "/vendor/overlay/" and reboot. Follow the commands below.
+        # adb root;
+        # adb remount;
+        # adb push <path-to-apk> /vendor/overlay/;
+        Alternatively, to side-load a change, just install the APK as normal (note: the apk
+        will end up at /data/app instead of /vendor/overlay).
+        # adb install -r <path-to-apk>
+    Step 6: Apply by overlay command
+        # "adb shell cmd overlay list;" Output of the same will be as shown below.
+           com.android.car.ui.paintbooth
+           [ ] com.android.car.ui.paintbooth.base.rro
+        # adb shell cmd overlay enable (double-hyphen)user 0 com.android.car.ui.paintbooth.rro
+           to enable the RRO
+        # adb shell cmd overlay disable (double-hyphen)user 0 com.android.car.ui.paintbooth.rro
+           to disable RRO
+        (Be careful to use the right user id, depending on the targeted app)
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+        package="{{RRO_PACKAGE_NAME}}">
+    <application android:hasCode="false"/>
+    <overlay android:priority="10"
+             android:targetPackage="{{TARGET_PACKAGE_NAME}}"/>
+</manifest>
diff --git a/car-ui-lib/tests/rro-base/res/values/colors.xml b/car-ui-lib/tests/rro-base/res/values/colors.xml
new file mode 100644
index 0000000..3341ad8
--- /dev/null
+++ b/car-ui-lib/tests/rro-base/res/values/colors.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright 2019 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<resources>
+  <color name="dialog_activity_background_color">#ff000f</color>
+</resources>
diff --git a/car-ui-lib/tests/rro-base/res/values/styles.xml b/car-ui-lib/tests/rro-base/res/values/styles.xml
new file mode 100644
index 0000000..b5d20db
--- /dev/null
+++ b/car-ui-lib/tests/rro-base/res/values/styles.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<resources xmlns:android="http://schemas.android.com/apk/res/android">
+    <style name="TextAppearance.CarUi.Widget.Toolbar.Title" parent="android:TextAppearance.DeviceDefault">
+        <item name="android:singleLine">true</item>
+        <item name="android:textSize">40sp</item>
+        <item name="android:textColor">#FF00F0</item>
+    </style>
+</resources>