Skip to content

Instantly share code, notes, and snippets.

@sinergy
Forked from rock3r/README.md
Created February 5, 2019 14:49
Show Gist options
  • Save sinergy/5e6ce174b5c961865dcffed2b33023a9 to your computer and use it in GitHub Desktop.
Save sinergy/5e6ce174b5c961865dcffed2b33023a9 to your computer and use it in GitHub Desktop.
A simple bash script to enable demo mode on a Marshmallow+ device via ADB (based on http://bit.ly/295BHLx)
#!/bin/sh
# License for any modification to the original (linked below):
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# Sebastiano Poggi wrote this file. As long as you retain
# this notice you can do whatever you want with this stuff. If we meet some day,
# and you think this stuff is worth it, you can buy us a beer in return.
#
# Based on http://bit.ly/295BHLx
DEBUG=false
function log {
if [ "$DEBUG" = true ]; then
echo "🐞 [DEBUG] $1"
fi
}
function checkAdb {
if [[ $ADB == "" ]]; then
ADB=adb
fi
command -v $ADB >/dev/null 2>&1 || { echo >&2 "❌ This command requires adb but it’s not on the PATH.\nYou can specify adb's path with the ADB variable."; exit 1; }
log "Using ADB with command: '$ADB'"
}
function silent {
$1 &>/dev/null
}
function readClockFromDeviceOsVersion {
VERSION=$($ADB shell getprop ro.build.version.release)
log "Read version: $VERSION"
# Example VERSION: "8.1.0"
MAJOR=${VERSION%%.*} # Drop VERSION from first '.' onwards
MINOR=${VERSION#*.} # Drop VERSION before first '.' (included)
MINOR=${MINOR%%.*} # Drop MINOR from first '.' onwards
log "Parsed: major='$MAJOR', minor='$MINOR'"
if [[ $MINOR == "" ]]; then
MINOR="0"
fi
if [[ $MAJOR != "" ]]; then
echo "ℹ️ Using device Android version for clock time. Detected version: $MAJOR.$MINOR"
MAJOR=$(printf %02d $MAJOR) # Left-pad with zeroes to 2 digits
if [[ $MINOR -lt 10 ]]; then
# Right-pad minor version (e.g., '1' -> '10')
MINOR="${MINOR}0"
fi
HHMM="$MAJOR$MINOR"
log "Clock value: $HHMM"
else
HHMM="1010"
echo "❌ parsing clock value, using default: $HHMM"
fi
}
function setupDemoMode {
silent "$ADB shell am broadcast -a com.android.systemui.demo -e command enter || exit"
silent "$ADB shell am broadcast -a com.android.systemui.demo -e command clock -e hhmm ${HHMM}"
silent "$ADB shell am broadcast -a com.android.systemui.demo -e command battery -e plugged false -e level 100"
silent "$ADB shell am broadcast -a com.android.systemui.demo -e command network -e nosim hide"
silent "$ADB shell am broadcast -a com.android.systemui.demo -e command network -e wifi show -e level 4"
silent "$ADB shell am broadcast -a com.android.systemui.demo -e command network -e mobile show -e datatype 4g -e level 4 -e fully true"
silent "$ADB shell am broadcast -a com.android.systemui.demo -e command notifications -e visible false"
}
function enableDemoMode {
adb shell settings put global sysui_demo_allowed 1
}
############################################## SCRIPT BEGINS HERE #########################################
CMD=$1
checkAdb
if [[ $CMD != "on" && $CMD != "off" ]]; then
echo "⚠️ Usage: $0 on|off [hhmm]" >&2
exit
fi
echo "🔍 Finding device..."
silent "$ADB wait-for-device"
if [ $CMD == "on" ]; then
if [[ "$2" != "" ]]; then
HHMM="$2"
else
readClockFromDeviceOsVersion
fi
echo "ℹ️ Enabling demo mode..."
enableDemoMode
setupDemoMode
elif [ $CMD == "off" ]; then
echo "ℹ️ Disabling demo mode..."
silent "$ADB shell am broadcast -a com.android.systemui.demo -e command exit"
fi
echo "✅ Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment