morristech
5/3/2019 - 8:06 PM

A simple bash script to enable demo mode on a Marshmallow+ device via ADB (based on http://bit.ly/295BHLx)

A simple bash script to enable demo mode on a Marshmallow+ device via ADB (based on http://bit.ly/295BHLx)

Usage

$ demo on|off [hhmm]

Enable or disable the demo mode on a connected Android device or emulator. You can also pass in a custom value for the system clock in the HHMM format (only used when you use the on command).

:warning: This script only works on *nix systems and has only been tested on macOS with Bash (but should be portable).

This script assumes you have adb on your path. If you don't, you can specify the path to adb by setting the $ADB environment variable. If you have multiple devices connected, this script will not work, but you can set $ANDROID_SERIAL to the target device's serial number (as displayed by adb devices).

Release notes

Version 2.2 (1 May 2019)

  • Fix bug when parsing some Android version numbers (which would cause the clock not to be properly set)
  • Allow turning on debug mode with the --debug flag

Version 2.1 (4 Feb 2019)

  • Add back missing licence header

Version 2.0 (3 Feb 2019)

  • Bring back support for hhmm parameter
    • By default, the script uses the device OS version as clock time (e.g., Android 8.1 -> 08:10)
    • If you specify a hhmm parameter, it will be used instead (without validation!)
    • If the script fails at determining the device OS version, it will use 10:10 as clock time
  • Improve script output and provide debugging log (requires modifying the script to enable)
  • Verify that adb command is available

Version 1.2 (12 Jan 2018)

  • Remove the adb root user request — it was not needed

Version 1.1 (12 Jan 2018)

  • Force a clock time (08:00) working around the lack of a default (this ignores the hhmm parameter you may have passed in)

Version 1.0 (29 Jun 2016)

#!/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
if [[ $* == *--debug* ]]; then
  DEBUG=true
fi

if [ "$DEBUG" = true ]; then
  set -x
fi

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"

  if [[ $VERSION =~ ^\d+(\.\d+)*$ ]]; then
    # 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
  else
    # Example VERSION: "9" — hopefully we won't get crazy other stuff here...
    MAJOR=$VERSION
    MINOR="0"
  fi
  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."