-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadbee.sh
executable file
·257 lines (220 loc) · 5.36 KB
/
adbee.sh
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/bin/bash
# This script wraps up some simple adb processes so that you can call it a little easier when
# integrating android device calls via ADB.
# the location of the adb util
source /etc/profile
adb_bin=$(which adb)
sniffer_bin=/opt/ha-bridge/bridge-sniffer/bridge-sniffer.sh
# debug log file location (default adb dir)
logfile=/var/log/openhab/adbee/debug.log
#############
# Functions #
#############
function enable_debug {
set -x
set -v
exec > >(tee -a $logfile)
exec 2> >(tee -a $logfile >&2)
echo "DEBUG: log will be stored in $logfile"
}
function connect_adb {
# Establish an ADB session and get the emulator name
$adb_bin connect $deviceip
connected=1
sleep 3
adb_id=$($adb_bin devices | grep -w $deviceip | grep -w "device" | awk '{print $1}')
adb_bin="$adb_bin -s $adb_id"
}
function disconnect_adb {
# Disconnect the ADB session
if [ -v connected ]; then
$adb_bin disconnect $deviceip
fi
}
function adb_reboot {
# issue reboot
$adb_bin shell reboot &
connected=1
sleep 3
}
function send_keyevent {
# send_keyevent <arg> (<arg> <arg>)
for var in "$@"
do
case "$var" in
up)
var=KEYCODE_DPAD_UP
shift 2;;
down)
var=KEYCODE_DPAD_DOWN
shift 2;;
left)
var=KEYCODE_DPAD_LEFT
shift 2;;
right)
var=KEYCODE_DPAD_RIGHT
shift 2;;
enter)
var=KEYCODE_ENTER
shift 2;;
back)
var=KEYCODE_BACK
shift 2;;
home)
var=KEYCODE_HOME
shift 2;;
play)
var=KEYCODE_MEDIA_PLAY_PAUSE
shift 2;;
pause)
var=KEYCODE_MEDIA_PLAY_PAUSE
shift 2;;
previous)
var=KEYCODE_MEDIA_PREVIOUS
shift 2;;
next)
var=KEYCODE_MEDIA_NEXT
shift 2;;
power)
var=KEYCODE_POWER
shift 2;;
settings)
longpress=1
var=KEYCODE_HOME
shift 2;;
--)
# There are no final arguments left to process, end
shift
break;;
esac
if [ -v longpress ]; then
$adb_bin shell input keyevent --longpress "$var"
sleep 1
unset longpress
else
$adb_bin shell input keyevent "$var"
fi
done
}
function start_app {
# start_app <com.package.name>
send_keyevent KEYCODE_WAKEUP
packageIntent=$($adb_bin shell pm dump $package | grep -A 1 "MAIN" | grep $package | awk '{print $2}' | grep $package)
$adb_bin shell am start -n ${packageIntent::-1}
}
function quick_state {
# send the device into another state, do not quote
# quick_state <state>
case "$state" in
wake)
send_keyevent KEYCODE_WAKEUP
shift 2;;
sleep)
if $adb_bin shell dumpsys power | grep -q "Display Power: state=ON" ; then
send_keyevent KEYCODE_POWER
fi
shift 2;;
mirror)
if $adb_bin shell dumpsys power | grep -q "Display Power: state=OFF" ; then
send_keyevent KEYCODE_WAKEUP
fi
send_keyevent settings right right enter
shift 2;;
settings)
send_keyevent settings right right enter
shift 2;;
reboot)
adb_reboot
disconnect_adb
shift 2;;
--)
# There are no final arguments left to process, end
shift
break;;
esac
}
#######################
# Argument Processing #
#######################
# Execute getopt on the arguments passed to this program, identified by the special character $@
args=`getopt -n "$0" -o "d:phga:k:s:" --long "deviceip:,app:,keys:,state:,ha:,debug,preserve,help" -- "$@"`
# Bad arguments, something has gone wrong with the getopt command.
if [ $? -ne 0 ];
then
exit 1
fi
# Make sure whitespace is preserved
eval set -- "$args"
# Test all the options and perform whatever needs to be done
while true;
do
case "$1" in
--ha)
if [ -n "$2" ]; then
deviceip=$($sniffer_bin -g -n "$2")
fi
echo "Bridge Sniffer: detected $deviceip as the request initiators closest neighbor"
connect_adb
shift 2;;
-d|--deviceip)
if [ -n "$2" ]; then
deviceip=$2
fi
connect_adb
shift 2;;
-k|--keys)
if [ -n "$2" ]; then
keys=$2
fi
send_keyevent $keys
shift 2;;
-s|--state)
if [ -n "$2" ]; then
state=$2
fi
quick_state $state
shift 2;;
-a|--app)
if [ -n "$2" ]; then
package=$2
fi
start_app
shift 2;;
-g|--debug)
enable_debug
shift 1;;
-p|--preserve)
connected=1
preserve=1
shift 1;;
-h|--help)
# Print help information
echo "Available options:"
echo "-d | --deviceip"
echo -e "\tThe IP Address for the device you are connecting to\n"
echo "--ha"
echo -e "\tDetermine the IP address of the system from your HA via bridge-sniffer (beta)\n"
echo "-a | --app"
echo -e "\tApplication to start in com.package.name format\n"
echo "-k | --keys"
echo -e "\tKey events to send to device (enclose multiple in double quotes)\n"
echo "-s | --state"
echo -e "\tQuick State, currently supported options are;\n"
echo -e "\t\twake, sleep, reboot\n"
echo "-p | --preserve"
echo -e "\tDo not disconnect adb after execution\n"
echo "-g | --debug"
echo -e "\tEnable bash debugger (-xv)\n"
echo "-h | --help"
echo -n "\tDisplay this help menu\n"
exit
shift 2;;
--)
# There are no final arguments left to process, end
shift
break;;
esac
done
if [ ! -v preserve ]; then
disconnect_adb
fi