1#!/bin/sh 2 3# Copyright 2015 Google Inc. 4# 5# Use of this source code is governed by a BSD-style license that can be 6# found in the LICENSE file. 7 8if [ -z "$1" ]; then 9 printf 'Usage:\n android_skp_capture.sh PACKAGE_NAME\n\n' 10 printf "Use \`adb shell 'pm list packages'\` to get a listing.\n\n" 11 exit 1 12fi 13if ! command -v adb > /dev/null 2>&1; then 14 if [ -x "${ANDROID_SDK_ROOT}/platform-tools/adb" ]; then 15 adb() { 16 "${ANDROID_SDK_ROOT}/platform-tools/adb" "$@" 17 } 18 else 19 echo 'adb missing' 20 exit 2 21 fi 22fi 23phase1_timeout_seconds=15 24phase2_timeout_seconds=60 25package="$1" 26filename="$(date '+%H%M%S').skp" 27remote_path="/data/data/${package}/cache/${filename}" 28local_path="$(date '+%Y-%m-%d_%H%M%S')_${package}.skp" 29key='debug.hwui.capture_frame_as_skp' 30adb shell "setprop '${key}' '${remote_path}'" 31spin() { 32 case "$spin" in 33 1) printf '\b|';; 34 2) printf '\b\\';; 35 3) printf '\b-';; 36 *) printf '\b/';; 37 esac 38 spin=$(( ( ${spin:-0} + 1 ) % 4 )) 39 sleep $1 40} 41 42banner() { 43 printf '\n=====================\n' 44 printf ' %s' "$*" 45 printf '\n=====================\n' 46} 47banner '...WAITING...' 48adb_test_exist() { 49 test '0' = "$(adb shell "test -e \"$1\"; echo \$?")"; 50} 51timeout=$(( $(date +%s) + $phase1_timeout_seconds)) 52while ! adb_test_exist "$remote_path"; do 53 spin 0.05 54 if [ $(date +%s) -gt $timeout ] ; then 55 printf '\bTimed out.\n' 56 adb shell "setprop '${key}' ''" 57 exit 3 58 fi 59done 60printf '\b' 61 62banner '...SAVING...' 63adb_test_file_nonzero() { 64 # grab first byte of `du` output 65 X="$(adb shell "du \"$1\" 2> /dev/null | dd bs=1 count=1 2> /dev/null")" 66 test "$X" && test "$X" -ne 0 67} 68#adb_filesize() { 69# adb shell "wc -c \"$1\"" 2> /dev/null | awk '{print $1}' 70#} 71timeout=$(( $(date +%s) + $phase2_timeout_seconds)) 72while ! adb_test_file_nonzero "$remote_path"; do 73 spin 0.05 74 if [ $(date +%s) -gt $timeout ] ; then 75 printf '\bTimed out.\n' 76 adb shell "setprop '${key}' ''" 77 exit 3 78 fi 79done 80printf '\b' 81 82adb shell "setprop '${key}' ''" 83 84i=0; while [ $i -lt 10 ]; do spin 0.10; i=$(($i + 1)); done; echo 85 86adb pull "$remote_path" "$local_path" 87if ! [ -f "$local_path" ] ; then 88 printf "something went wrong with `adb pull`." 89 exit 4 90fi 91adb shell rm "$remote_path" 92printf '\nSKP saved to file:\n %s\n\n' "$local_path" 93