-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_activities
executable file
·112 lines (90 loc) · 2.61 KB
/
list_activities
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
# Auto-completion function
_lista() {
# Array variable storing the possible completions.
COMPREPLY=()
# Pointer to current & previous completion word
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD - 1]}
if [[ ${cur} == -* ]]; then
COMPREPLY=($(compgen -W "-s -p -h" -- "$cur")) # Generate the completion matches and load them into $COMPREPLY array.
return 0
fi
# Use the compgen for specific CLI options
case "$prev" in
-s) COMPREPLY=($(compgen -W "$(adb devices | awk '{if (NR > 1) print $1}')" -- "$cur")) ;;
esac
return 0
}
usage() {
cat <<EOF
lista - CLI tool for activity listing
Description: Lists all activities for app by specified package
Usage: lista [options]
-s serial id - device serial ID. Should be used if more than one Android device connected to your machine.
-p package name (required unless -h is used). Example: -p "com.google.mail"
-h get help
Examples:
lista -p "com.google.mail"
lista -p "com.google.mail" -s 78f2ba2b
lista -h
EOF
}
lista() {
cmd="adb "
local SERIAL_ID=""
local PACKAGE_NAME=""
while getopts ":hs:p:" opt_char; do
case $opt_char in
s) SERIAL_ID=$OPTARG ;;
p) PACKAGE_NAME=$OPTARG ;; # check by regex com.nike.omega.debug - reproducible by lista -ps
h)
usage
return 1
;;
\?) # invalid option provided
echo "Invalid option provided\n"
echo "PACKAGE_NAME is required. Add -p argument"
usage
return 1
;;
*)
usage
return 1
;;
esac
done
shift $((OPTIND - 1))
_setSerialId $SERIAL_ID
if [ -z "$PACKAGE_NAME" ]; then
echo "PACKAGE_NAME is required. Add -p argument"
return 1
else
_setPackage $PACKAGE_NAME
fi
_showActivities $cmd
}
# Set serial id of device
_setSerialId() {
if [ ! -z "$1" ]; then
echo "Serial ID: $1"
cmd+="-s $1 "
fi
}
# Set package name to look for
_setPackage() {
echo "Package name: $PACKAGE_NAME\n"
cmd+="shell dumpsys activity activities | grep \"$PACKAGE_NAME\""
}
# Execute command and parse results
_showActivities() {
local result=$(eval "$@" | grep Hist | awk -F '[{}]' '{print $2}' | cut -d ' ' -f3)
if [ -z $result ]; then
echo "No activities found\nCheck if app is runnung on your device or package name is correct"
else
echo "Activity stack:"
echo "$result"
fi
}
# Assign the auto-completion function
complete -F _lista lista