blob: 41f8cda126d4abc539e81981b6b93d7065484ae6 [file] [log] [blame]
Igor Murashkin25f394d2018-09-11 16:37:18 -07001#!/bin/bash
Igor Murashkin7ab5c4d2019-04-17 12:28:19 -07002# Copyright 2018, The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
Igor Murashkin25f394d2018-09-11 16:37:18 -070015
16if [[ -z $ANDROID_BUILD_TOP ]]; then
17 echo "Please run source build/envsetup.sh first" >&2
18 exit 1
19fi
20
21source $ANDROID_BUILD_TOP/build/envsetup.sh
22
23verbose_print() {
24 if [[ "$verbose" == "y" ]]; then
25 echo "$@" >&2
26 fi
27}
Igor Murashkin973a2dc2018-09-14 16:27:53 -070028
29remote_pidof() {
30 local procname="$1"
31 adb shell ps | grep "$procname" | awk '{print $2;}'
32}
33
34remote_pkill() {
35 local procname="$1"
36 shift
37
38 local the_pids=$(remote_pidof "$procname")
39 local pid
40
41 for pid in $the_pids; do
42 verbose_print adb shell kill "$@" "$pid"
43 adb shell kill "$@" "$pid"
44 done
45}
46
47get_activity_name() {
48 local package="$1"
49 local action_key="android.intent.action.MAIN:"
50
51 # Example query-activities output being parsed:
52 #
53 # Activity #14:
54 # priority=0 preferredOrder=0 match=0x108000 specificIndex=-1 isDefault=true
55 # com.google.android.videos/com.google.android.youtube.videos.EntryPoint
56 # Activity #15:
57 # priority=0 preferredOrder=0 match=0x108000 specificIndex=-1 isDefault=true
58 # com.google.android.youtube/.app.honeycomb.Shell$HomeActivity
59
60 # Given package 'com.google.android.youtube' return '.app.honeycomb.Shell$HomeActivity'
61
62 local activity_line="$(adb shell cmd package query-activities --brief -a android.intent.action.MAIN -c android.intent.category.LAUNCHER | grep "$package/")"
63 IFS="/" read -a array <<< "$activity_line"
64 local activity_name="${array[1]}"
Igor Murashkin7ab5c4d2019-04-17 12:28:19 -070065
66 # Activities starting with '.' are shorthand for having their package name prefixed.
67 if [[ $activity_name == .* ]]; then
68 activity_name="${package}${activity_name}"
69 fi
Igor Murashkin973a2dc2018-09-14 16:27:53 -070070 echo "$activity_name"
71}
Igor Murashkin7ab5c4d2019-04-17 12:28:19 -070072
73# Use with logcat_from_timestamp to skip all past log-lines.
74logcat_save_timestamp() {
75 adb shell 'date -u +"%Y-%m-%d %H:%M:%S.%N"'
76}
77
78# Roll forward logcat to only show events
79# since the specified timestamp.
80#
81# i.e. don't look at historical logcat,
82# only look at FUTURE logcat.
83#
84# First use 'logcat_save_timestamp'
85# Then do whatever action you want.
86# Then us 'logcat_from_timestamp $timestamp'
87logcat_from_timestamp() {
88 local timestamp="$1"
89 shift # drop timestamp from args.
90 echo "DONT CALL THIS FUNCTION" >&2
91 exit 1
92
93 verbose_print adb logcat -T \"$timestamp\" \"$@\"
94 adb logcat -T "$timestamp" "$@"
95}
96
97logcat_from_timestamp_bg() {
98 local timestamp="$1"
99 shift # drop timestamp from args.
100 verbose_print adb logcat -T \"$timestamp\" \"$@\"
101 adb logcat -v UTC -T "$timestamp" "$@" &
102 logcat_from_timestamp_pid=$!
103}
104
105# Starting at timestamp $2, wait until we seen pattern $3
106# or until a timeout happens in $1 seconds.
107#
108# Set VERBOSE_LOGCAT=1 to debug every line of logcat it tries to parse.
109logcat_wait_for_pattern() {
110 local timeout="$1"
111 local timestamp="$2"
112 local pattern="$3"
113
114 local logcat_fd
115
116 coproc logcat_fd {
117 kill_children_quietly() {
118 kill "$logcat_pidd"
119 wait "$logcat_pidd" 2>/dev/null
120 }
121
122 trap 'kill_children_quietly' EXIT # kill logcat when this coproc is killed.
123
124 # run logcat in the background so it can be killed.
125 logcat_from_timestamp_bg "$timestamp"
126 logcat_pidd=$logcat_from_timestamp_pid
127 wait "$logcat_pidd"
128 }
129 local logcat_pid="$!"
130 verbose_print "[LOGCAT] Spawn pid $logcat_pid"
131
132 local timeout_ts="$(date -d "now + ${timeout} seconds" '+%s')"
133 local now_ts="0"
134
135 local return_code=1
136
137 verbose_print "logcat_wait_for_pattern begin"
138
139 while read -t "$timeout" -r -u "${logcat_fd[0]}" logcat_output; do
140 if (( $VERBOSE_LOGCAT )); then
141 verbose_print "LOGCAT: $logcat_output"
142 fi
143 if [[ "$logcat_output:" == *"$pattern"* ]]; then
144 verbose_print "LOGCAT: " "$logcat_output"
145 verbose_print "WE DID SEE PATTERN" '<<' "$pattern" '>>.'
146 return_code=0
147 break
148 fi
149 now_ts="$(date -d "now" '+%s')"
150 if (( now_ts >= timeout_ts )); then
151 verbose_print "DID TIMEOUT BEFORE SEEING ANYTHING (timeout=$timeout seconds) " '<<' "$pattern" '>>.'
152 break
153 fi
154 done
155
156 # Don't leave logcat lying around since it will keep going.
157 kill "$logcat_pid"
158 # Suppress annoying 'Terminated...' message.
159 wait "$logcat_pid" 2>/dev/null
160
161 verbose_print "[LOGCAT] $logcat_pid should be killed"
162
163 return $return_code
164}