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    skp-capture.sh PACKAGE_NAME OPTIONAL_FRAME_COUNT\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_prefix="$(date '+%Y-%m-%d_%H%M%S')_${package}"
29local_path="${local_path_prefix}.skp"
30enable_capture_key='debug.hwui.capture_skp_enabled'
31enable_capture_value=$(adb shell "getprop '${enable_capture_key}'")
32#printf 'captureflag=' "$enable_capture_value" '\n'
33if [ -z "$enable_capture_value" ]; then
34    printf 'Capture SKP property need to be enabled first. Please use\n'
35    printf "\"adb shell setprop debug.hwui.capture_skp_enabled true\" and then restart\n"
36    printf "the process.\n\n"
37    exit 1
38fi
39if [ ! -z "$2" ]; then
40    adb shell "setprop 'debug.hwui.capture_skp_frames' $2"
41fi
42filename_key='debug.hwui.skp_filename'
43adb shell "setprop '${filename_key}' '${remote_path}'"
44spin() {
45    case "$spin" in
46         1) printf '\b|';;
47         2) printf '\b\\';;
48         3) printf '\b-';;
49         *) printf '\b/';;
50    esac
51    spin=$(( ( ${spin:-0} + 1 ) % 4 ))
52    sleep $1
53}
54
55banner() {
56    printf '\n=====================\n'
57    printf '   %s' "$*"
58    printf '\n=====================\n'
59}
60banner '...WAITING...'
61adb_test_exist() {
62    test '0' = "$(adb shell "test -e \"$1\"; echo \$?")";
63}
64timeout=$(( $(date +%s) + $phase1_timeout_seconds))
65while ! adb_test_exist "$remote_path"; do
66    spin 0.05
67    if [ $(date +%s) -gt $timeout ] ; then
68        printf '\bTimed out.\n'
69        adb shell "setprop '${filename_key}' ''"
70        exit 3
71    fi
72done
73printf '\b'
74
75#read -n1 -r -p "Press any key to continue..." key
76
77banner '...SAVING...'
78adb_test_file_nonzero() {
79    # grab first byte of `du` output
80    X="$(adb shell "du \"$1\" 2> /dev/null | dd bs=1 count=1 2> /dev/null")"
81    test "$X" && test "$X" -ne 0
82}
83#adb_filesize() {
84#    adb shell "wc -c \"$1\"" 2> /dev/null | awk '{print $1}'
85#}
86timeout=$(( $(date +%s) + $phase2_timeout_seconds))
87while ! adb_test_file_nonzero "$remote_path"; do
88    spin 0.05
89    if [ $(date +%s) -gt $timeout ] ; then
90        printf '\bTimed out.\n'
91        adb shell "setprop '${filename_key}' ''"
92        exit 3
93    fi
94done
95printf '\b'
96
97adb shell "setprop '${filename_key}' ''"
98
99i=0; while [ $i -lt 10 ]; do spin 0.10; i=$(($i + 1)); done; echo
100
101adb pull "$remote_path" "$local_path"
102if ! [ -f "$local_path" ] ; then
103    printf "something went wrong with `adb pull`."
104    exit 4
105fi
106adb shell rm "$remote_path"
107printf '\nSKP saved to file:\n    %s\n\n'  "$local_path"
108if [ ! -z "$2" ]; then
109    bridge="_"
110    adb shell "setprop 'debug.hwui.capture_skp_frames' ''"
111    for i in $(seq 2 $2); do
112        adb pull "${remote_path}_${i}" "${local_path_prefix}_${i}.skp"
113        adb shell rm "${remote_path}_${i}"
114    done
115fi
116
117