Merge cherrypicks of [2454758, 2454859, 2454837, 2454965, 2454984, 2455001, 2454760, 2454860, 2454838, 2454899, 2454970, 2455005, 2455027, 2454973, 2455006, 2455061, 2455007, 2454914, 2454987, 2454974] into oc-release

Change-Id: I553bda5e86663208d37cf5dd37c25a82269f6577
diff --git a/data/etc/encryptionkey.img b/data/etc/encryptionkey.img
new file mode 100644
index 0000000..9e0f96a
--- /dev/null
+++ b/data/etc/encryptionkey.img
Binary files differ
diff --git a/data/etc/handheld_core_hardware.xml b/data/etc/handheld_core_hardware.xml
index cf60e88..b2aaf13 100644
--- a/data/etc/handheld_core_hardware.xml
+++ b/data/etc/handheld_core_hardware.xml
@@ -72,6 +72,9 @@
     <!-- Feature to specify if the device support managed users. -->
     <feature name="android.software.managed_users" />
 
+    <feature name="android.software.picture_in_picture" />
+
+    <feature name="android.software.cts" />
     <!-- devices with GPS must include android.hardware.location.gps.xml -->
     <!-- devices with an autofocus camera and/or flash must include either
          android.hardware.camera.autofocus.xml or
diff --git a/data/etc/permissions/privapp-permissions-goldfish.xml b/data/etc/permissions/privapp-permissions-goldfish.xml
new file mode 100644
index 0000000..7124fb8
--- /dev/null
+++ b/data/etc/permissions/privapp-permissions-goldfish.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2017 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 XML file declares which signature|privileged permissions should be granted to privileged
+applications on GMS or Google-branded devices.
+It allows additional grants on top of privapp-permissions-platform.xml
+-->
+
+<permissions>
+    <privapp-permissions package="com.android.sdksetup">
+        <permission name="android.permission.BACKUP"/>
+        <permission name="android.permission.WRITE_SECURE_SETTINGS"/>
+    </privapp-permissions>
+    <privapp-permissions package="com.android.dialer">
+        <permission name="android.permission.STATUS_BAR"/>
+    </privapp-permissions>
+</permissions>
\ No newline at end of file
diff --git a/fstab.ranchu b/fstab.ranchu
index f155c5a..b9eb754 100644
--- a/fstab.ranchu
+++ b/fstab.ranchu
@@ -4,5 +4,5 @@
 # specify MF_CHECK, and must come before any filesystems that do specify MF_CHECK
 /dev/block/vda                                          /system             ext4      ro                                                   wait
 /dev/block/vdb                                          /cache              ext4      noatime,nosuid,nodev,nomblk_io_submit,errors=panic   wait
-/dev/block/vdc                                          /data               ext4      noatime,nosuid,nodev,nomblk_io_submit,errors=panic   wait,check
-/devices/*/block/vdd                                    auto                auto      defaults                                             voldmanaged=sdcard:auto,encryptable=userdata
+/dev/block/vdc                                          /data               ext4      noatime,nosuid,nodev,nomblk_io_submit,errors=panic   wait,check,quota,forceencrypt=/dev/block/vdd
+/devices/*/block/vde                                    auto                auto      defaults                                             voldmanaged=sdcard:auto,encryptable=userdata
diff --git a/init.goldfish.rc b/init.goldfish.rc
index 784fee0..b1613a4 100644
--- a/init.goldfish.rc
+++ b/init.goldfish.rc
@@ -68,6 +68,12 @@
 on fs
         mount_all /fstab.goldfish
 
+#emulator is not much useful before boot complete
+#start it later
+on property:sys.boot_completed=1
+    setprop sys.usb.config adb
+    start adbd
+
 service goldfish-setup /system/etc/init.goldfish.sh
     user root
     group root
@@ -104,3 +110,10 @@
 service fingerprintd /system/bin/fingerprintd
     class late_start
     user system
+
+service bugreport /system/bin/dumpstate -d -p -B \
+        -o /data/data/com.android.shell/files/bugreports/bugreport
+    class main
+    disabled
+    oneshot
+    keycodes 114 115 116
diff --git a/init.ranchu-core.sh b/init.ranchu-core.sh
new file mode 100755
index 0000000..fd21a37
--- /dev/null
+++ b/init.ranchu-core.sh
@@ -0,0 +1,46 @@
+#!/system/bin/sh
+
+
+# ro.kernel.android.qemud is normally set when we
+# want the RIL (radio interface layer) to talk to
+# the emulated modem through qemud.
+#
+# However, this will be undefined in two cases:
+#
+# - When we want the RIL to talk directly to a guest
+#   serial device that is connected to a host serial
+#   device by the emulator.
+#
+# - We don't want to use the RIL but the VM-based
+#   modem emulation that runs inside the guest system
+#   instead.
+#
+# The following detects the latter case and sets up the
+# system for it.
+#
+qemud=`getprop ro.kernel.android.qemud`
+case "$qemud" in
+    "")
+    radio_ril=`getprop ro.kernel.android.ril`
+    case "$radio_ril" in
+        "")
+        # no need for the radio interface daemon
+        # telephony is entirely emulated in Java
+        setprop ro.radio.noril yes
+        stop ril-daemon
+        ;;
+    esac
+    ;;
+esac
+
+
+# disable boot animation for a faster boot sequence when needed
+boot_anim=`getprop ro.kernel.android.bootanim`
+case "$boot_anim" in
+    0)  setprop debug.sf.nobootanimation 1
+    ;;
+esac
+
+
+# take the wake lock
+echo "emulator_wake_lock" > /sys/power/wake_lock
diff --git a/init.ranchu-net.sh b/init.ranchu-net.sh
new file mode 100755
index 0000000..e4347d3
--- /dev/null
+++ b/init.ranchu-net.sh
@@ -0,0 +1,32 @@
+#!/system/bin/sh
+
+# Setup networking when boot starts
+ifconfig eth0 10.0.2.15 netmask 255.255.255.0 up
+route add default gw 10.0.2.2 dev eth0
+
+
+# Setup additionnal DNS servers if needed
+num_dns=`getprop ro.kernel.ndns`
+case "$num_dns" in
+    2) setprop net.eth0.dns2 10.0.2.4
+       ;;
+    3) setprop net.eth0.dns2 10.0.2.4
+       setprop net.eth0.dns3 10.0.2.5
+       ;;
+    4) setprop net.eth0.dns2 10.0.2.4
+       setprop net.eth0.dns3 10.0.2.5
+       setprop net.eth0.dns4 10.0.2.6
+       ;;
+esac
+
+
+# set up the second interface (for inter-emulator connections)
+# if required
+my_ip=`getprop net.shared_net_ip`
+case "$my_ip" in
+    "")
+    ;;
+    *) ifconfig eth1 "$my_ip" netmask 255.255.255.0 up
+    ;;
+esac
+
diff --git a/init.ranchu.rc b/init.ranchu.rc
index 6fb6e77..c9abe1d 100644
--- a/init.ranchu.rc
+++ b/init.ranchu.rc
@@ -9,6 +9,9 @@
     symlink /dev/goldfish_pipe /dev/android_pipe
     symlink /dev/goldfish_pipe /dev/qemu_pipe
 
+on post-fs-data
+    setprop vold.post_fs_data_done 1
+
 on boot
     setprop net.eth0.gw 10.0.2.2
     setprop net.eth0.dns1 10.0.2.3
@@ -17,6 +20,7 @@
     setprop ro.build.product generic
     setprop ro.product.device generic
     setprop ro.hardware.audio.primary goldfish
+    setprop ro.setupwizard.mode EMULATOR
 
 # fake some battery state
     setprop status.battery.state Slow
@@ -44,14 +48,37 @@
 # start essential services
 # These were written for the classic emulator, but are applicable to ranchu
     start goldfish-logcat
-    start goldfish-setup
+#    start goldfish-setup
 
 
 # enable Google-specific location features,
 # like NetworkLocationProvider and LocationCollector
     setprop ro.com.google.locationfeatures 1
 
-service goldfish-setup /system/etc/init.goldfish.sh
+#emulator is not much useful before boot complete
+#start it later
+on property:sys.boot_completed=1
+    setprop sys.usb.config adb
+    start adbd
+    start goldfish-logcat
+
+on property:qemu.adbd=start
+    setprop sys.usb.config adb
+    start adbd
+    start goldfish-logcat
+
+# allow goldfish-setup to take wake lock
+    chown root system /sys/power/wake_lock
+    chown root system /sys/power/wake_unlock
+
+service ranchu-setup /system/bin/init.ranchu-core.sh
+    class core
+    user root
+    group root
+    oneshot
+
+service ranchu-net /system/bin/init.ranchu-net.sh
+    class late_start
     user root
     group root wakelock
     oneshot
@@ -68,6 +95,9 @@
     group root
     oneshot
 
+on property:qemu.logcat=start
+    start goldfish-logcat
+
 # -Q is a special logcat option that forces the
 # program to check wether it runs on the emulator
 # if it does, it redirects its output to the device
@@ -83,3 +113,10 @@
 service fingerprintd /system/bin/fingerprintd
     class late_start
     user system
+
+service bugreport /system/bin/dumpstate -d -p -B \
+        -o /data/data/com.android.shell/files/bugreports/bugreport
+    class main
+    disabled
+    oneshot
+    keycodes 114 115 116
diff --git a/manifest.xml b/manifest.xml
new file mode 100644
index 0000000..7d359a0
--- /dev/null
+++ b/manifest.xml
@@ -0,0 +1,35 @@
+<manifest version="1.0" type="device">
+    <hal format="hidl">
+        <name>android.hardware.drm</name>
+        <transport arch="32">passthrough</transport>
+        <impl level="generic"></impl>
+        <version>1.0</version>
+        <!-- TODO(b/36371166): change to default -->
+        <interface>
+            <name>ICryptoFactory</name>
+            <instance>crypto</instance>
+        </interface>
+        <interface>
+            <name>IDrmFactory</name>
+            <instance>drm</instance>
+        </interface>
+    </hal>
+    <hal format="hidl">
+        <name>android.hardware.graphics.allocator</name>
+        <transport>hwbinder</transport>
+        <version>2.0</version>
+        <interface>
+            <name>IAllocator</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
+    <hal format="hidl">
+        <name>android.hardware.power</name>
+        <transport>hwbinder</transport>
+        <version>1.0</version>
+        <interface>
+            <name>IPower</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
+</manifest>
diff --git a/overlay/frameworks/base/packages/SettingsProvider/res/values/defaults.xml b/overlay/frameworks/base/packages/SettingsProvider/res/values/defaults.xml
index 6602627..56fbf89 100644
--- a/overlay/frameworks/base/packages/SettingsProvider/res/values/defaults.xml
+++ b/overlay/frameworks/base/packages/SettingsProvider/res/values/defaults.xml
@@ -18,7 +18,9 @@
 -->
 
 <resources>
+    <!-- cannot set this to true because O-CTS is testing the keyguard b/37465076
     <bool name="def_lockscreen_disabled">true</bool>
+    -->
 
     <!-- Allow users to use both the on-screen keyboard, as well as a real
          keyboard -->
diff --git a/power/Android.mk b/power/Android.mk
index ffbc457..3839088 100644
--- a/power/Android.mk
+++ b/power/Android.mk
@@ -27,3 +27,14 @@
 LOCAL_MODULE := power.goldfish
 LOCAL_MODULE_TAGS := optional
 include $(BUILD_SHARED_LIBRARY)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_CFLAGS += -DQEMU_HARDWARE
+LOCAL_SHARED_LIBRARIES := liblog libcutils
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/../include
+LOCAL_SRC_FILES := power_qemu.c
+LOCAL_MODULE := power.ranchu
+LOCAL_MODULE_TAGS := optional
+include $(BUILD_SHARED_LIBRARY)
diff --git a/qemu-props/qemu-props.c b/qemu-props/qemu-props.c
index a5dc819..c80863a 100644
--- a/qemu-props/qemu-props.c
+++ b/qemu-props/qemu-props.c
@@ -109,6 +109,20 @@
     }
 
 
+    /* HACK start adbd periodically every minute, if adbd is already running, this is a no-op */
+    for(;;) {
+        usleep(60000000);
+        char  temp[BUFF_SIZE];
+        property_get("sys.boot_completed", temp, "");
+        int is_boot_completed = (strncmp(temp, "1", 1) == 0) ? 1 : 0;
+        if (is_boot_completed) {
+            DD("start adbd ...");
+            property_set("qemu.adbd", "start");
+        } else {
+            DD("skip starting adbd ...");
+        }
+    }
+
     /* finally, close the channel and exit */
     close(qemud_fd);
     DD("exiting (%d properties set).", count);
diff --git a/sensors/sensors_qemu.c b/sensors/sensors_qemu.c
index 6c41a9c..9eee746 100644
--- a/sensors/sensors_qemu.c
+++ b/sensors/sensors_qemu.c
@@ -355,7 +355,7 @@
             events[ID_TEMPERATURE].type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
             continue;
         }
- 
+
         /* "proximity:<value>" */
         if (sscanf(buff, "proximity:%g", params+0) == 1) {
             new_sensors |= SENSORS_PROXIMITY;
@@ -742,7 +742,7 @@
           .fifoMaxEventCount =   0,
           .stringType =         0,
           .requiredPermission = 0,
-          .flags = SENSOR_FLAG_WAKE_UP | SENSOR_FLAG_ON_CHANGE_MODE,
+          .flags = SENSOR_FLAG_ON_CHANGE_MODE,
           .reserved   = {}
         },