Snap for 5704982 from 9af37fef61c0bb42b8d1bd39452ba904f021f566 to qt-aml-release

Change-Id: I99bb2a624eadc3862d3f1636e3864ad3232cd38f
diff --git a/device-common.mk b/device-common.mk
index 61c01af..72fc8f2 100644
--- a/device-common.mk
+++ b/device-common.mk
@@ -658,3 +658,6 @@
 # a_sns_test for sensor testing
 PRODUCT_PACKAGES_DEBUG += a_sns_test
 
+# Write flags to the vendor space in /misc partition.
+PRODUCT_PACKAGES += \
+    misc_writer
diff --git a/init.common.rc b/init.common.rc
index ec47091..46f697b 100644
--- a/init.common.rc
+++ b/init.common.rc
@@ -886,3 +886,27 @@
 
 on property:sys.vdso=32
    write /sys/module/vdso/parameters/enable_64 0
+
+# Write the dark theme magic (`theme-dark`, or 0x7468656d652d6461726b in hex string) to /misc
+# partition. Offset 10814 in vendor space is effectively offset 12862 in /misc partition.
+service vendor.darkboot /vendor/bin/misc_writer --vendor-space-offset 10814 --hex-string 0x7468656d652d6461726b
+    disabled
+    oneshot
+
+# Clear the 10-byte dark theme magic in /misc partition. Offset 10814 in vendor space is effectively
+# offset 12862 in /misc partition.
+service vendor.darkboot_clear /vendor/bin/misc_writer --vendor-space-offset 10814 --hex-string 0x00000000000000000000
+    disabled
+    oneshot
+
+# Set the dark boot flag on dark mode (UiModeManager.MODE_NIGHT_YES == 2).
+on property:persist.sys.theme=2
+    start vendor.darkboot
+
+# Clear the dark theme flag on light mode (UiModeManager.MODE_NIGHT_NO == 1) or auto mode
+# (UiModeManager.MODE_NIGHT_AUTO == 0).
+on property:persist.sys.theme=1
+    start vendor.darkboot_clear
+
+on property:persist.sys.theme=0
+    start vendor.darkboot_clear
diff --git a/marlin/BoardConfig.mk b/marlin/BoardConfig.mk
index 776b851..eb1e364 100644
--- a/marlin/BoardConfig.mk
+++ b/marlin/BoardConfig.mk
@@ -168,7 +168,8 @@
 
 #Add NON-HLOS files for ota upgrade
 ADD_RADIO_FILES := true
-TARGET_RECOVERY_UI_LIB := librecovery_ui_nanohub
+
+TARGET_RECOVERY_UI_LIB := librecovery_ui_nanohub libbootloader_message libfstab
 
 #Add support for firmare upgrade on 8996
 HAVE_SYNAPTICS_DSX_FW_UPGRADE := true
diff --git a/recovery/nanohub/Android.mk b/recovery/nanohub/Android.mk
index 70ccb37..29bf171 100644
--- a/recovery/nanohub/Android.mk
+++ b/recovery/nanohub/Android.mk
@@ -14,7 +14,7 @@
 # limitations under the License.
 #
 
-ifeq ($(TARGET_RECOVERY_UI_LIB),librecovery_ui_nanohub)
+ifneq (,$(filter librecovery_ui_nanohub,$(TARGET_RECOVERY_UI_LIB)))
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
 
@@ -24,7 +24,9 @@
 
 LOCAL_SRC_FILES := nanohub_recovery_ui.cpp
 
-LOCAL_SHARED_LIBRARIES := librecovery_ui
+LOCAL_SHARED_LIBRARIES := libbase librecovery_ui
+
+LOCAL_STATIC_LIBRARIES := libbootloader_message
 
 include $(BUILD_STATIC_LIBRARY)
 endif
diff --git a/recovery/nanohub/nanohub_recovery_ui.cpp b/recovery/nanohub/nanohub_recovery_ui.cpp
index 9298c05..6f17530 100644
--- a/recovery/nanohub/nanohub_recovery_ui.cpp
+++ b/recovery/nanohub/nanohub_recovery_ui.cpp
@@ -20,38 +20,54 @@
 #include <unistd.h>
 #include <errno.h>
 
-#include "recovery_ui/device.h"
-#include "recovery_ui/screen_ui.h"
+#include <string>
 
-class Nanohub_Device : public Device
-{
-public:
+#include <android-base/logging.h>
+#include <bootloader_message/bootloader_message.h>
+#include <recovery_ui/device.h>
+#include <recovery_ui/screen_ui.h>
+
+// Wipes the dark theme flag as part of data wipe.
+static bool WipeDarkThemeFlag() {
+    // Must be consistent with the one in init.hardware.rc (10-byte `theme-dark`). The magic is at
+    // 10814 in vendor space, or (2048 + 10814) since the start of /misc.
+    const std::string wipe_str(10, '\x00');
+    constexpr size_t kDarkThemeFlagOffsetInVendorSpace = 10814;
+    if (std::string err; !WriteMiscPartitionVendorSpace(
+            wipe_str.data(), wipe_str.size(), kDarkThemeFlagOffsetInVendorSpace, &err)) {
+        LOG(ERROR) << "Failed to write wipe string: " << err;
+        return false;
+    }
+    LOG(INFO) << "Dark theme flag wiped successfully";
+    return true;
+}
+
+class Nanohub_Device : public Device {
+  public:
     Nanohub_Device(ScreenRecoveryUI* ui) : Device(ui) {}
     bool PostWipeData();
 };
 
-bool Nanohub_Device::PostWipeData()
-{
-    int fd;
-
-    fd = open("/sys/class/nanohub/nanohub/erase_shared", O_WRONLY);
+bool Nanohub_Device::PostWipeData() {
+    int fd = open("/sys/class/nanohub/nanohub/erase_shared", O_WRONLY);
     if (fd < 0) {
-        printf("error: open erase_shared failed: %s\n", strerror(errno));
+        PLOG(ERROR) << "open erase_shared failed";
     } else {
         if (write(fd, "1\n", 2) != 2) {
-            printf("error: write to erase_shared failed: %s\n", strerror(errno));
+            PLOG(ERROR) << "write to erase_shared failed";
         } else {
-            printf("Successfully erased nanoapps.\n");
+            LOG(INFO) << "Successfully erased nanoapps";
         }
         close(fd);
     }
 
+    WipeDarkThemeFlag();
+
     // open/write failure caused by permissions issues would persist across
     // reboots, so always return true to prevent a factory reset failure loop.
     return true;
 }
 
-Device *make_device()
-{
+Device* make_device() {
     return new Nanohub_Device(new ScreenRecoveryUI);
 }
diff --git a/sailfish/BoardConfig.mk b/sailfish/BoardConfig.mk
index 3c90f23..ed8ea87 100644
--- a/sailfish/BoardConfig.mk
+++ b/sailfish/BoardConfig.mk
@@ -167,7 +167,8 @@
 
 #Add NON-HLOS files for ota upgrade
 ADD_RADIO_FILES := true
-TARGET_RECOVERY_UI_LIB := librecovery_ui_nanohub
+
+TARGET_RECOVERY_UI_LIB := librecovery_ui_nanohub libbootloader_message libfstab
 
 #Add support for firmare upgrade on 8996
 HAVE_SYNAPTICS_DSX_FW_UPGRADE := true