From 67e1cd9d3b582d655e8494b4fc2f1dd634f326c4 Mon Sep 17 00:00:00 2001 From: Markil 3 Date: Tue, 25 Aug 2026 12:18:46 -0600 Subject: [PATCH] Creates the scripts. --- android-lock | 157 +++++++++++++++++++++++++++++++++++++++++++ android-watch-lock | 11 +++ android-watch-scrcpy | 91 +++++++++++++++++++++++++ 3 files changed, 259 insertions(+) create mode 100755 android-lock create mode 100755 android-watch-lock create mode 100755 android-watch-scrcpy diff --git a/android-lock b/android-lock new file mode 100755 index 0000000..de78101 --- /dev/null +++ b/android-lock @@ -0,0 +1,157 @@ +#!/bin/bash +lookup_password_gnome() { + pwd="$(secret-tool lookup "$1" Passwords)" + if [ $? = 0 ] + then + echo "$pwd" + return 0 + else + echo "Could not find password \"$1\" in Gnome keyring." >&2 + echo "" + return 1 + fi +} + +lookup_password_kde() { + pwd="$(kwallet-query -r "$1" kdewallet)" + if [ $? = 0 ] + then + echo "$pwd" + return 0 + else + echo "Could not find password \"$1\" in KDE wallet." >&2 + echo "" + return 1 + fi +} + +lookup_password() { + password="$1" + which kwallet-query >/dev/null + has_kde=$? + which secret-tool >/dev/null + has_gnome=$? + if [ $has_kde = 0 ] + then + pwd="$(lookup_password_kde "$password")" + elif [ $has_gnome = 0 ] + then + pwd="$(lookup_password_gnome "$password")" + else + echo "System does not use either KDE wallet or Gnome keyring" >&2 + return 2; + fi + + if [ $? = 0 ] + then + echo "$pwd"; + return 0; + else + echo "" + return 1; + fi +} + +is_screen_on() { + [ "$(adb shell dumpsys power | grep mHoldingDisplaySuspendBlocker= | grep -oE '(true|false)')" == true ] && return + false +} + +is_unlocked() { + [ "$(adb shell dumpsys window| awk '/mDreamingLockscreen=/{print $2; exit}' | grep -oE '(true|false)')" == false ] && return + false +} + +unlock() { + if ! is_screen_on + then + adb shell input keyevent 26 # wakeup + fi + adb shell input keyevent 82 # get the keyboard ready + adb shell input keyevent 66; + adb shell input keyevent 66; + password_name="android-${ANDROID_SERIAL}" + sleep 1; # wait a bit to ensure all the inputs have fully processes. + password="$(lookup_password "$password_name")" + if [ -z "$password" ] + then + echo "No password found. Please submit one under the \"${password_name}\" entry in the OS keyring." >&2 + exit 1 + else + adb shell input keyboard text "$password" + adb shell input keyboard keyevent 66; + echo "OK, should be on now." >&2 + fi +} + +lock() { + adb shell input keyevent 26; +} + +list_devices() { + adb devices | awk '/(^[[:alnum:]]+[[:blank:]]+device$)/ {print $1}' +} + +first_device() { + devices="$(adb devices | awk '/(^[[:alnum:]]+[[:blank:]]+device$)/ {print $1}')" + if [ -z "$devices" ] + then + echo "" + return + else + set -- "$devices" + echo $1 + fi +} + +has_devices() { + [ ! -z "$(list_devices)" ] && return + false +} + +while getopts 'uls:' flag +do + case "${flag}" in + u) set_unlock=true ;; + l) set_lock=true ;; + s) export ANDROID_SERIAL="${OPTARG}" ;; + *) print_usage; exit 1 ;; + esac +done + +which adb >/dev/null 2>/dev/null +if [ $? != 0 ] +then + echo "ADB not installed or not located in $PATH" >&2 + exit 4 +fi + +if ! has_devices +then + echo "No Android devices found" >&2 + exit 3 +fi + +if [ -z "$ANDROID_SERIAL" ] +then + export ANDROID_SERIAL="$(first_device)" +fi + +if is_unlocked +then + if [ "$set_unlock" != true ] + then + echo "Locking screen." >&2 + lock + else + echo "Screen is already unlocked." >&2 + fi +else + if [ "$set_lock" != true ] + then + echo "Screen is off. Turning on." >&2 + unlock + else + echo "Screen is already locked." >&2 + fi +fi diff --git a/android-watch-lock b/android-watch-lock new file mode 100755 index 0000000..fa183fd --- /dev/null +++ b/android-watch-lock @@ -0,0 +1,11 @@ +#!/bin/bash +# ADB watch for login and unlock a phone + +dbus-monitor --session "type=signal,interface=org.freedesktop.ScreenSaver" | while read MSG; do + LOCK_STAT=`echo $MSG | grep boolean | awk '{print $2}'` + if [[ "$LOCK_STAT" == "true" ]]; then + android-lock -l + elif [[ "$LOCK_STAT" == "false" ]]; then + android-lock -u + fi +done diff --git a/android-watch-scrcpy b/android-watch-scrcpy new file mode 100755 index 0000000..78b4190 --- /dev/null +++ b/android-watch-scrcpy @@ -0,0 +1,91 @@ +#!/bin/bash + +list_devices() { + adb devices | awk '/(^[[:alnum:]]+[[:blank:]]+device$)/ {print $1}' +} + +first_device() { + devices="$(adb devices | awk '/(^[[:alnum:]]+[[:blank:]]+device$)/ {print $1}')" + if [ -z "$devices" ] + then + echo "" + return + else + set -- "$devices" + echo $1 + fi +} + +has_devices() { + [ ! -z "$(list_devices)" ] && return + false +} + +which adb >/dev/null 2>/dev/null +if [ $? != 0 ] +then + echo "ADB not installed or not located in $PATH" >&2 + exit 4 +fi + +which scrcpy >/dev/null 2>/dev/null +if [ $? != 0 ] +then + echo "scrcpy not installed or not located in $PATH" >&2 + exit 4 +fi + +declare -A serial_map +declare -A bluetooth_map + +while true +do + devices="$(list_devices)" + while read serial; + do + if [ -z "$serial" ] + then + continue + fi + if [ -z "${serial_map[$serial]}" ] + then + echo "New device $serial connected" >&2 + scrcpy -s "$serial" & + serial_map[$serial]=$! + android-lock -u -s "$serial" + # Link the phone to bluetooth as well. + bluetooth_mac="$(adb -s "$serial" shell settings get secure bluetooth_address)" + bluetoothctl devices | grep "$bluetooth_mac" >/dev/null 2>/dev/null + if [ $? != 0 ] + then + # Pair the devices + bluetoothctl scan on + adb -s "$serial" shell am start -a android.bluetooth.adapter.action.REQUEST_DISCOVERABLE + sleep 1 + bluetoothctl pair "$bluetooth_mac" + bluetoothctl scan off + else + adb -s "$serial" shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE + fi + bluetoothctl connect "$bluetooth_mac" + bluetooth_map[$serial]="$bluetooth_mac" + fi + done <<< "$devices" + + # Check for devices that have disconnected. + for serial in "${!serial_map[@]}" + do + adb -s "$serial" get-state >/dev/null 2>/dev/null + if [ $? != 0 ] + then + # The device has since been disconnected, and we should remove it. + echo "Device $serial disconnected" + kill "${serial_map[$serial]}" + unset serial_map[$serial] + echo "Disconnecting bluetooth ${bluetooth_map[$serial]}" + bluetoothctl disconnect "${bluetooth_map[$serial]}" + echo "Bluetooth disconnected" + unset bluetooth_map[$serial] + fi + done +done