Read device state config xml from bootconfig on boot

- create qemu-device-state vendor exec to write
  device_state_configuration.xml to /data/vendor/devicestate/
- init copies over it to /data/system/devicestate/
- only applies to foldable/rollable AVDs

Bug: 197562702

Test: run foldable/rollable AVDs

Change-Id: Ie831fb6e29a8768172b095d6a190afdf9a051f67
diff --git a/64bitonly/product/vendor.mk b/64bitonly/product/vendor.mk
index 3a34916..72cc594 100644
--- a/64bitonly/product/vendor.mk
+++ b/64bitonly/product/vendor.mk
@@ -42,6 +42,7 @@
     libcuttlefish-ril-2 \
     libgoldfish-rild \
     qemu-adb-keys \
+    qemu-device-state \
     qemu-props \
     stagefright \
     fingerprint.ranchu \
@@ -293,7 +294,6 @@
     device/generic/goldfish/input/virtio_input_multi_touch_11.idc:$(TARGET_COPY_OUT_VENDOR)/usr/idc/virtio_input_multi_touch_11.idc \
     device/generic/goldfish/display_settings_freeform.xml:$(TARGET_COPY_OUT_VENDOR)/etc/display_settings_freeform.xml \
     device/generic/goldfish/display_settings.xml:$(TARGET_COPY_OUT_VENDOR)/etc/display_settings.xml \
-    device/generic/goldfish/device_state_configuration.xml:$(TARGET_COPY_OUT_VENDOR)/etc/devicestate/device_state_configuration.xml \
     device/generic/goldfish/data/etc/config.ini:config.ini \
     device/generic/goldfish/wifi/simulated_hostapd.conf:$(TARGET_COPY_OUT_VENDOR)/etc/simulated_hostapd.conf \
     device/generic/goldfish/wifi/wpa_supplicant.conf:$(TARGET_COPY_OUT_VENDOR)/etc/wifi/wpa_supplicant.conf \
diff --git a/device_state_configuration.xml b/device_state_configuration.xml
deleted file mode 100644
index 17a09c4..0000000
--- a/device_state_configuration.xml
+++ /dev/null
@@ -1,18 +0,0 @@
-<device-state-config>
-  <device-state>
-    <identifier>0</identifier>
-    <conditions>
-      <lid-switch>
-        <open>false</open>
-      </lid-switch>
-    </conditions>
-  </device-state>
-  <device-state>
-    <identifier>1</identifier>
-    <conditions>
-      <lid-switch>
-        <open>true</open>
-      </lid-switch>
-    </conditions>
-  </device-state>
-</device-state-config>
diff --git a/init.ranchu.rc b/init.ranchu.rc
index 327ac57..d62215d 100644
--- a/init.ranchu.rc
+++ b/init.ranchu.rc
@@ -65,11 +65,13 @@
 on post-fs-data
     setprop vold.post_fs_data_done 1
     mkdir /data/vendor/adb 0755 root root
+    mkdir /data/vendor/devicestate 0755 root root
     mkdir /data/vendor/var 0755 root root
     mkdir /data/vendor/var/run 0755 root root
     mkdir /data/vendor/var/run/netns 0755 root root
 
     start qemu-adb-keys
+    start qemu-device-state
 
 on zygote-start
     # Create the directories used by the Wireless subsystem
@@ -200,6 +202,12 @@
     group root
     oneshot
 
+service qemu-device-state /vendor/bin/qemu-device-state
+    class core
+    user root
+    group root
+    oneshot
+
 service goldfish-logcat /system/bin/logcat -f /dev/hvc1 ${ro.boot.logcat}
     class main
     user logd
diff --git a/init.system_ext.rc b/init.system_ext.rc
index 5f8cb4e..5c2db83 100644
--- a/init.system_ext.rc
+++ b/init.system_ext.rc
@@ -5,3 +5,9 @@
     copy /data/vendor/adb/adb_keys /data/misc/adb/adb_keys
     chown system shell /data/misc/adb/adb_keys
     chmod 0640 /data/misc/adb/adb_keys
+
+on property:init.svc.qemu-device-state=stopped && property:ro.boot.qemu.device_state=*
+    mkdir /data/system/devicestate/ 0755 system system
+    copy /data/vendor/devicestate/device_state_configuration.xml /data/system/devicestate/device_state_configuration.xml
+    rm /data/vendor/devicestate/device_state_configuration.xml
+    chown system system /data/system/devicestate/device_state_configuration.xml
diff --git a/overlay/frameworks/base/core/res/res/values/config.xml b/overlay/frameworks/base/core/res/res/values/config.xml
index e652485..b7e54f5 100644
--- a/overlay/frameworks/base/core/res/res/values/config.xml
+++ b/overlay/frameworks/base/core/res/res/values/config.xml
@@ -57,7 +57,7 @@
     <!-- The device states (supplied by DeviceStateManager) that should be treated as folded by the
          display fold controller. -->
     <integer-array name="config_foldedDeviceStates" translatable="false">
-      <item>0</item> <!-- CLOSED -->
+      <item>1</item> <!-- CLOSED -->
     </integer-array>
 
     <!-- This device is able to support the microphone and camera global toggles. -->
diff --git a/qemu-device-state/Android.bp b/qemu-device-state/Android.bp
new file mode 100644
index 0000000..ca186e0
--- /dev/null
+++ b/qemu-device-state/Android.bp
@@ -0,0 +1,44 @@
+// Copyright (C) 2021 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.
+
+// this file is used to build emulator-specific program tools
+// that should only run in the emulator.
+//
+
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "device_generic_goldfish_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["device_generic_goldfish_license"],
+}
+
+cc_binary {
+    name: "qemu-device-state",
+    vendor: true,
+    srcs: [
+        "qemu-device-state.cpp",
+    ],
+    shared_libs: [
+        "libbase",
+        "liblog",
+    ],
+    static_libs: [
+        "libfstab",
+    ],
+    cflags: [
+        "-DLOG_TAG=\"qemu-device-state\"",
+    ],
+}
diff --git a/qemu-device-state/qemu-device-state.cpp b/qemu-device-state/qemu-device-state.cpp
new file mode 100644
index 0000000..f85f54f
--- /dev/null
+++ b/qemu-device-state/qemu-device-state.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2021 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.
+ */
+
+/* this program is used to read a set of system properties and their values
+ * from the emulator program and set them in the currently-running emulated
+ * system. It does so by connecting to the 'boot-properties' qemud service.
+ *
+ * This program should be run as root and called from
+ * /system/etc/init.ranchu.rc exclusively.
+ */
+
+#define LOG_TAG  "qemu-device-state"
+
+#define DEBUG  0
+//#define LOG_NDEBUG 0
+
+#include <fstream>
+#include <iostream>
+#include <string.h>
+
+#include <log/log.h>
+
+#if DEBUG
+#  define  DD(...)    ALOGD(__VA_ARGS__)
+#else
+#  define  DD(...)    ((void)0)
+#endif
+
+#define DEVIE_STATE_PROP "qemu.device_state"
+// init will copy over this file to
+// /data/system/devicestate/device_state_configuration.xml
+#define DEVICE_STATE_FILE "/data/vendor/devicestate/device_state_configuration.xml"
+
+extern bool fs_mgr_get_boot_config(const std::string& key, std::string* out_val);
+
+int main(void) {
+    std::string deviceState;
+    if (!fs_mgr_get_boot_config(DEVIE_STATE_PROP, &deviceState)) {
+        ALOGV("%s bootconfig prop not set", DEVIE_STATE_PROP);
+        return 0;
+    }
+
+    std::ofstream f;
+    f.open(DEVICE_STATE_FILE);
+    if (!f.is_open()) {
+        ALOGE("Failed to open %s\n", DEVICE_STATE_FILE);
+        exit(1);
+    }
+
+    ALOGI("write to %s", DEVICE_STATE_FILE);
+    f << deviceState;
+    f.close();
+    return 0;
+}
diff --git a/sepolicy/common/file_contexts b/sepolicy/common/file_contexts
index aeeecbb..e259c3b 100644
--- a/sepolicy/common/file_contexts
+++ b/sepolicy/common/file_contexts
@@ -31,6 +31,7 @@
 /vendor/bin/init\.ranchu-net\.sh u:object_r:goldfish_setup_exec:s0
 /vendor/bin/init\.wifi\.sh   u:object_r:goldfish_setup_exec:s0
 /vendor/bin/qemu-adb-keys    u:object_r:qemu_adb_keys_exec:s0
+/vendor/bin/qemu-device-state u:object_r:qemu_device_state_exec:s0
 /vendor/bin/qemu-props       u:object_r:qemu_props_exec:s0
 /vendor/bin/mac80211_create_radios u:object_r:mac80211_create_radios_exec:s0
 /vendor/bin/createns         u:object_r:createns_exec:s0
diff --git a/sepolicy/common/qemu_device_state.te b/sepolicy/common/qemu_device_state.te
new file mode 100644
index 0000000..2295236
--- /dev/null
+++ b/sepolicy/common/qemu_device_state.te
@@ -0,0 +1,13 @@
+# qemu-device-state service:  Writes host adb key for adbd on boot.
+type qemu_device_state, domain;
+type qemu_device_state_exec, vendor_file_type, exec_type, file_type;
+
+init_daemon_domain(qemu_device_state)
+
+# Allow qemu_device_state to read /proc/bootconfig
+allow qemu_device_state proc_cmdline:file r_file_perms;
+allow qemu_device_state proc_bootconfig:file r_file_perms;
+
+# Permission to write to /data/vendor/system/devicestate.
+allow qemu_device_state vendor_data_file:dir create_dir_perms;
+allow qemu_device_state vendor_data_file:file create_file_perms;
diff --git a/vendor.mk b/vendor.mk
index f0119f0..316b905 100644
--- a/vendor.mk
+++ b/vendor.mk
@@ -42,6 +42,7 @@
     libcuttlefish-ril-2 \
     libgoldfish-rild \
     qemu-adb-keys \
+    qemu-device-state \
     qemu-props \
     stagefright \
     fingerprint.ranchu \
@@ -297,7 +298,6 @@
     device/generic/goldfish/input/virtio_input_multi_touch_11.idc:$(TARGET_COPY_OUT_VENDOR)/usr/idc/virtio_input_multi_touch_11.idc \
     device/generic/goldfish/display_settings_freeform.xml:$(TARGET_COPY_OUT_VENDOR)/etc/display_settings_freeform.xml \
     device/generic/goldfish/display_settings.xml:$(TARGET_COPY_OUT_VENDOR)/etc/display_settings.xml \
-    device/generic/goldfish/device_state_configuration.xml:$(TARGET_COPY_OUT_VENDOR)/etc/devicestate/device_state_configuration.xml \
     device/generic/goldfish/data/etc/config.ini:config.ini \
     device/generic/goldfish/wifi/simulated_hostapd.conf:$(TARGET_COPY_OUT_VENDOR)/etc/simulated_hostapd.conf \
     device/generic/goldfish/wifi/wpa_supplicant.conf:$(TARGET_COPY_OUT_VENDOR)/etc/wifi/wpa_supplicant.conf \