1# Script to install SystemUI apk in system partition
2APK_FILE="$1"
3if [ -n "$2" ]; then
4  export ANDROID_SERIAL="$2"
5fi
6
7# TODO(b/234033515): Device list info does not yet contain adb server port
8# You might need to manually set this environment variable if you changed the adb server port in
9# the Android Studio settings:
10#export ANDROID_ADB_SERVER_PORT=
11
12if [ -z "$APK_FILE" ]; then
13    echo "Apk file not specified. Using default SystemUI-google-debug.apk"
14    SCRIPT_DIR="$(cd $(dirname $0) && pwd)"
15    BUILD_DIR="$SCRIPT_DIR/../../../../../../out/gradle/build/SysUIGradleProject/SystemUI/build"
16    APK_FILE="$BUILD_DIR/intermediates/apk/google/debug/SystemUI-google-debug.apk"
17fi
18
19echo "ANDROID_SERIAL=$ANDROID_SERIAL"
20echo "APK_FILE=$APK_FILE"
21
22if [ ! -f "$APK_FILE" ]; then
23    echo "Compiled APK not found $APK_FILE" > /dev/stderr
24    exit 1
25fi
26
27adb root || exit 1
28adb wait-for-device
29
30VERITY_ENABLED="$(adb shell getprop | grep 'partition.*verified')"
31if [ -n "$VERITY_ENABLED" ]; then
32    echo "Disabling verity and rebooting"
33    adb disable-verity
34    adb reboot
35
36    echo "Waiting for device"
37    adb wait-for-device root
38    adb wait-for-device
39fi
40
41adb remount
42
43TARGET_PATH="$(adb shell pm path com.android.systemui | cut -d ':' -f 2)"
44if [ -z "$TARGET_PATH" ]; then
45    echo "Unable to get apk path: $TARGET_PATH]" > /dev/stderr
46    exit 1
47fi
48
49echo "Pushing apk to device at $TARGET_PATH"
50adb push "$APK_FILE" "$TARGET_PATH"
51adb shell fsync "$TARGET_PATH"
52
53# Restart the system, then wait up to 60 seconds for 'adb shell dumpsys package' to become available
54echo "Restarting the system..."
55adb shell 'stop ; start'
56sleep 2
57MAX_TRIES=29
58N=0
59while [[ "$N" -lt "$MAX_TRIES" && -z "$(adb shell dumpsys package com.android.systemui 2>&1 | grep versionName)" ]]; do
60    sleep 2
61    N="$((N+1))"
62done
63
64if [[ "$N" -ge "$MAX_TRIES" ]]; then
65    echo "Timed out waiting for package service. Failed to run 'adb shell dumpsys package'."
66    exit 1
67fi
68
69VERSION="$(adb shell dumpsys package com.android.systemui 2>&1 | grep versionName)"
70if [[ "$VERSION" == *"BuildFromAndroidStudio"* ]]; then
71    echo "Install complete"
72else
73    echo "Installation verification failed. Package versionName does not contain \"BuildFromAndroidStudio\" as expected."
74    exit 1
75fi
76