forked from IGPenguin/mobile-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_tools
468 lines (424 loc) · 10.7 KB
/
common_tools
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/bin/bash
SCRIPT_LOCATION=$(dirname "$0")
LAST_CHECK_DATE_FILE="$SCRIPT_LOCATION/../data/toolkit_last_check_date.txt"
TEMPORARY_FILE="/private/tmp/mobile-toolkit-cache"
##########################
### ANDROID SECTION
android_check_connected(){
android_get_devices
#No device connected
if [ ${#DEVICES[@]} -eq 0 ]
then
echo "❌ No Android devices detected"
yes_or_no "❓ Start an emulator?"; if [[ "$RESPONSE" == "y" || "$RESPONSE" == "Y" ]]; then $SCRIPT_LOCATION/aemulator "start"; fi
android_wait_for_device
fi
}
android_wait_for_device(){
echo "⏳ Waiting for Android device..."
adb wait-for-any-device
android_get_devices
}
android_get_devices(){
#Populate array with device ids
DEVICES=()
for LINE in `adb devices | grep -v "List" | awk '{print $1}'`
do
DEVICE=`echo $LINE | awk '{print $1}'`
DEVICES+=("$DEVICE")
done
}
android_device_info(){
MANUFACTURER=$(adb -s $1 shell getprop ro.product.manufacturer | tr -cd '[[:alnum:]]._-')
MODEL=$(adb -s $1 shell getprop ro.product.model | tr -cd '[[:alnum:]]._-')
VERSION=$(adb -s $1 shell getprop ro.build.version.release | tr -cd '[[:alnum:]]._-')
SDK=$(adb -s $1 shell getprop ro.build.version.sdk | tr -cd '[[:alnum:]]._-')
INFO=$(printf "%s)\n%s\n%s\n%s\n(API %s)\n-\n%s" "$NUMBER" "$MANUFACTURER" "$MODEL" "$VERSION" "$SDK" "$1")
}
android_choose_device() {
check_for_update
check_adb_dependency
android_check_connected
#Gather device info and choose device
if [ ${#DEVICES[@]} -gt 1 ]
then
NUMBER=1
echo "📱 Available devices:"
for ID in "${DEVICES[@]}"
do
android_device_info $ID
echo $INFO
((NUMBER++))
done
read -p "📝 Select a device: " CHOICE
while :;
do
if ((CHOICE <= 0 || CHOICE > ${#DEVICES[@]})); then
echo -en "\033[1A\033[2K" #deletes last echoed line in terminal
read -p "Invalid input, try again: " CHOICE
else
break
fi
done
SELECTED_DEVICE=${DEVICES[(($CHOICE-1))]}
else
SELECTED_DEVICE="${DEVICES[0]}"
fi
SELECTED_DEVICE_MODEL=$(adb -s $SELECTED_DEVICE shell getprop ro.product.model | tr -cd '[[:alnum:]]._-')
SELECTED_DEVICE_SDK=$(adb -s $SELECTED_DEVICE shell getprop ro.build.version.sdk | tr -cd '[[:alnum:]]._-')
}
check_screen_on(){
CURRENT_FOCUS=$(adb -s $1 shell dumpsys window windows | grep mCurrentFocus | cut -d'/' -f1 | rev | cut -d' ' -f1 | rev)
if [ "$CURRENT_FOCUS" == *"StatusBar"* ] || [ "$CURRENT_FOCUS" == *"mCurrentFocus=null"* ] || [ "$CURRENT_FOCUS" == *"Keyguard"* ]; then
return 1
else
return 0
fi
}
android_unlock_device(){
if [[ $(check_screen_on $1) -eq 1 ]]; then
echo "🛌 Waking up the device..."
adb -s $1 shell input keyevent KEYCODE_HOME
if [[ $(check_screen_on $1) -eq 1 ]]; then
adb -s $1 shell input keyevent KEYCODE_POWER
if [[ $(check_screen_on $1) -eq 1 ]]; then
adb -s $1 shell input keyevent 82
fi
fi
fi
}
android_get_foreground_package(){
SDK=$(adb -s $SELECTED_DEVICE shell getprop ro.build.version.sdk | tr -cd '[[:alnum:]]._-')
if (( $SDK < 21 )); then
android_get_foreground_package_sdk_low
else
android_get_foreground_package_sdk_21_plus
fi
}
android_get_foreground_package_sdk_21_plus(){
adb -s $SELECTED_DEVICE shell dumpsys activity recents | grep 'Recent #0' | cut -d= -f2 | sed 's| .*||' | cut -d '/' -f1
}
android_get_foreground_package_sdk_low(){
adb -s $SELECTED_DEVICE shell dumpsys window windows | grep mCurrentFocus | cut -d'/' -f1 | rev | cut -d' ' -f1 | rev
}
android_is_package_installed() {
adb -s $SELECTED_DEVICE shell pm list packages -f | sed -e 's/.*=//' | grep -xw $1 &> /dev/null
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo "🤷 Package \"$1\" is not installed."
exit
fi
}
detect_package_info(){
echo "🔍 Detecting package name..."
AAPPT_PATH=$(find ~/Library/Android/sdk -name 'aapt' | sort | tail -1)
PACKAGE_INFO=$($AAPPT_PATH dump badging "$1" > $TEMPORARY_FILE)
PACKAGE_NAME=$(cat $TEMPORARY_FILE | grep package:\ name);
PACKAGE_NAME=$(echo $PACKAGE_NAME | sed 's/^[^'\'']*'\''//');
PACKAGE_NAME=$(echo $PACKAGE_NAME | sed 's/'\''.*//');
APP_NAME=$(cat $TEMPORARY_FILE | grep application-label:)
APP_NAME=${APP_NAME#"application-label:"}
}
##########################
### IOS SECTION
prompt_xcode_launch(){
echo "❌ Developer image is not mounted and/or device screen is locked"
should_proceed "❓ Do you want to open Xcode to fix it?"
open -a Xcode
echo "⏳ Waiting for Xcode to launch..."
sleep 8
}
ios_wait_for_device(){
echo "⏳ Waiting for iOS device..."
ios_get_devices
while [ ${#IOS_USB_DEVICES[@]} -lt 1 ];
do
sleep 1
ios_get_devices
done
delete_lastline
}
ios_check_developer_image(){
IS_MOUNTED=$(ideviceimagemounter -u $1 -l) #Probably does not work on iOS 13
if [ ${#IS_MOUNTED} -eq 0 ] ; then
prompt_xcode_launch
fi
}
ios_device_info(){
ios_check_developer_image $1
MANUFACTURER="Apple"
MODEL=$(ios_translate_name $(ideviceinfo -u $1 | grep "HardwareModel" | cut -d\ -f2))
VERSION=$(ideviceinfo -u $1 | grep "ProductVersion" | cut -d\ -f2)
INFO=$(printf "%s)\n%s\n%s\n%s\n-\n%s" "$NUMBER" "$MANUFACTURER" "$MODEL" "$VERSION" "$ID")
}
ios_get_devices(){
ALL_IOS_DEVICES=( $(idevice_id -l | sort -u) )
IOS_USB_DEVICES=()
for ID in "${ALL_IOS_DEVICES[@]}"
do
ios_device_info $ID
if [ -z "$MODEL" ];then continue; fi; #Skip wireless (ghost) devices
IOS_USB_DEVICES+=($ID)
done
}
ios_choose_device(){
check_for_update
ios_get_devices
if [ ${#IOS_USB_DEVICES[@]} -eq 0 ] #No device connected
then
echo "❌ No iOS devices detected"
yes_or_no "❓ Start a simulator?"; if [[ "$RESPONSE" == "y" || "$RESPONSE" == "Y" ]]; then $SCRIPT_LOCATION/isimulator start; exit; fi
ios_wait_for_device
fi
if [ ${#IOS_USB_DEVICES[@]} -gt 1 ]
then
NUMBER=1
echo "📱 Available devices:"
for ID in "${IOS_USB_DEVICES[@]}"
do
ios_device_info $ID
echo $INFO
((NUMBER++))
done
read -p "📝 Select a device: " CHOICE
while :;
do
if ((CHOICE <= 0 || CHOICE > ${#IOS_USB_DEVICES[@]})); then
echo -en "\033[1A\033[2K" #deletes last echoed line in terminal
read -p "🤷 Invalid input, try again: " CHOICE
else
break
fi
done
SELECTED_DEVICE=${IOS_USB_DEVICES[(($CHOICE-1))]}
else
SELECTED_DEVICE="${IOS_USB_DEVICES[0]}"
fi
SELECTED_DEVICE_MODEL=$(ios_translate_name $(ideviceinfo -u $SELECTED_DEVICE | grep "HardwareModel" | cut -d\ -f2))
SELECTED_DEVICE_VERSION=$(ideviceinfo -u $SELECTED_DEVICE | grep "ProductVersion" | cut -d\ -f2)
}
ios_translate_name(){
# Translations here https://www.theiphonewiki.com/wiki/Models
NAME=$1
case $NAME in
"Purple"*|"purple"*)
NAME="iPhone"
;;
"M68"*)
NAME="iPhone"
;;
"N90"*|"N92"*)
NAME="iPhone4"
;;
"N94"*)
NAME="iPhone4S"
;;
"N88"*)
NAME="iPhone3GS"
;;
"N82"*)
NAME="iPhone3G"
;;
"N71"*)
NAME="iPhone6S"
;;
"N66"*)
NAME="iPhone6SPlus"
;;
"N61"*)
NAME="iPhone6"
;;
"N56"*)
NAME="iPhone6Plus"
;;
"N51"*|"N53"*)
NAME="iPhone5S"
;;
"N48"*)
NAME="iPhone5C"
;;
"N41"*|"N42"*)
NAME="iPhone5"
;;
"D10"*)
NAME="iPhone7"
;;
"D11"*)
NAME="iPhone7Plus"
;;
"D20"*)
NAME="iPhone8"
;;
"D21"*)
NAME="iPhone8Plus"
;;
"D22"*|"Ferrari"*|"ferrari"*)
NAME="iPhoneX"
;;
"D32"*)
NAME="iPhoneXS"
;;
"D33"*)
NAME="iPhoneXSMax"
;;
"N84"*)
NAME="iPhoneXR"
;;
"N69"*)
NAME="iPhoneSE"
;;
"J1"*)
NAME="iPad3gen"
;;
"J2"*)
NAME="iPad3gen"
;;
"J72"*)
NAME="iPadAir"
;;
"J82"*)
NAME="iPadAir2"
;;
"J85"*)
NAME="iPadMiniRetina"
;;
"J96"*)
NAME="iPadMini4"
;;
"J98"*|"J99"*|"J31"*)
NAME="iPadPro"
;;
"K48"*)
NAME="iPad"
;;
"K93"*|"K94"*|"K95"*)
NAME="iPad2"
;;
"P101"*|"P103"*)
NAME="iPad4gen"
;;
"P105"*|"P107"*)
NAME="iPadMini"
;;
esac
echo $NAME
}
#####
### COMMON SECTION
check_adb_dependency(){
if ! [ -x "$(command -v "adb")" ]; then
echo "🤷 Android Debug Bridge required!"
should_proceed "🔄 Install via homebrew? (this may take a while)"
brew cask install "android-platform-tools"
fi
}
check_dependency(){
if ! [ -x "$(command -v "$1")" ]; then
echo "⚠️ \"$1\" required!"
should_proceed "🔄 Install via homebrew? (this may take a while)"
if [ $2 == "-c" ];then
brew cask install $1
else
brew install $1
fi
fi
}
check_for_update(){
TODAY=$(date +%Y-%m-%d)
if [ -f "$LAST_CHECK_DATE_FILE" ]; then
LAST_CHECK_DATE=$(cat $LAST_CHECK_DATE_FILE)
else
echo $TODAY > $LAST_CHECK_DATE_FILE
fi
CURRENT_DIR="$PWD"
cd "$SCRIPT_LOCATION/.."
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$CURRENT_BRANCH" == "master" && "$LAST_CHECK_DATE" != "$TODAY" ]]; then
echo "🔄 Checking for Mobile Toolkit update..."
echo $TODAY > $LAST_CHECK_DATE_FILE
git fetch origin &> /dev/null
git status -uno | grep "up to date" &> /dev/null
if [ $? -ne 0 ]; then
yes_or_no "🆕 Update available, download now?";
if [[ "$RESPONSE" == "y" || "$RESPONSE" == "Y" ]]; then
echo "⏬ Updating..."
git pull &> /dev/null
echo "✨ New features:"
cat $SCRIPT_LOCATION/../changelog.txt
echo
echo "✅ Update complete"
fi
fi
fi
cd "$CURRENT_DIR"
}
delete_lastline(){
echo -en "\033[1A\033[2K"
}
yes_or_no(){
read -r -n 1 -p "$1 [y/n] " RESPONSE
case "$RESPONSE" in
[yY])
;;
[nN])
;;
*)
echo
echo "🤷 Invalid option."
yes_or_no $1
;;
esac
echo
}
should_proceed(){
read -r -n 1 -p "$1 [y/n] " RESPONSE
case "$RESPONSE" in
[yY])
;;
*)
exit
;;
esac
echo
}
choose_number(){
MAX=$1
if [ -n "$2" ]; then
read -p "$2" CHOICE
else
read -p "📝 Choose number: " CHOICE
fi
while :;
do
if [[ -z $CHOICE || $CHOICE -le 0 || $CHOICE -gt $MAX ]]; then
delete_lastline
read -p "🤷 Invalid choice, try again: " CHOICE
else
((++CHOICE))
break
fi
done
}
check_url(){
URL=$1
if [[ $URL == "" ]]; then
read -p "📝 Insert web url: " URL
check_url $URL
else
case $1 in
'http://'*)
;;
'https://'*)
;;
*'://'*)
;;
*)
URL='http://'$URL
;;
esac
fi
}
abort(){
echo $1
exit
}