From c35e1d67061fb172b87e8804b31c3f9da2852826 Mon Sep 17 00:00:00 2001 From: Max Overmeyer Date: Tue, 23 Feb 2021 15:08:14 +0000 Subject: [PATCH] Multi device recognition, neither beautiful nor totally working --- debloat_script.sh | 88 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 27 deletions(-) diff --git a/debloat_script.sh b/debloat_script.sh index b541c20..2bbe978 100644 --- a/debloat_script.sh +++ b/debloat_script.sh @@ -1,9 +1,43 @@ #!/usr/bin/env bash # BASH 4.4 or newer is needed! -if (( "${BASH_VERSINFO[0]}" < 5 )); then printf "\n%s\n\n" "Please upgrade to a bash version >= 4.4" && exit 1; fi +if (( "${BASH_VERSINFO[0]}" < 4 || ("${BASH_VERSINFO[0]}" == 4 && "${BASH_VERSINFO[1]}" < 4) )); then + printf "\n%s\n\n" "Please upgrade to a bash version >= 4.4" && exit 1; +fi + + + +#Multi-device detection +declare -a ADB_DEVICES=() +declare ADB_DEVICE +readarray -t ADB_DEVICES < <(adb devices -l | sed -n '/model:/p' | sed 's/\([^ ]*\).*/\1/') +# At least one device has to be available +if (( "${#ADB_DEVICES[@]}" == 0 )); then + printf "\n%s\n\n" "No connected devices recognized." && exit 1; +elif (( "${#ADB_DEVICES[@]}" > 1 )); then + declare -i i=0 + for adb_device in "${ADB_DEVICES[@]}"; do + printf "["$((i++))"] $adb_device\n" + done + + while true; do + read -r -p "Your selection: [0-"$((${#ADB_DEVICES[@]} - 1))"|X]" + + if [[ "$REPLY" =~ [Xx] ]]; then exit 0; + + elif [[ "$REPLY" == +([0-9]) && $REPLY < ${#ADB_DEVICES[@]} ]]; then + ADB_DEVICE=${ADB_DEVICES[$REPLY]} + break; + fi + done +else + ADB_DEVICE=${ADB_DEVICES[0]} +fi +#Check if the selected phone can be reached by ADB +if ! adb -s $ADB_DEVICE get-state &>/dev/null; then printf "\n%s\n\n" "Your phone is not detected correctly by ADB." && exit 1; fi + + -if ! adb get-state &>/dev/null; then printf "\n%s\n\n" "Your phone is not detected by ADB." && exit 1; fi set -Eeuo pipefail # Safer bash script @@ -39,11 +73,11 @@ main() { readonly BRAND="$(get_brand)" readonly BRAND_SUPPORTED=$( declare -p "$BRAND" &>/dev/null && echo "1" || echo "0") - readonly OLDER_THAN_ANDROID_8=$(( $(adb shell getprop ro.build.version.sdk | tr -d '\r') < 26 )) - readonly OLDER_THAN_ANDROID_5=$(( $(adb shell getprop ro.build.version.sdk | tr -d '\r') < 21 )) - readonly NEWER_THAN_ANDROID_9=$(( $(adb shell getprop ro.build.version.sdk | tr -d '\r') >= 29 )) + readonly OLDER_THAN_ANDROID_8=$(( $(adb -s $ADB_DEVICE shell getprop ro.build.version.sdk | tr -d '\r') < 26 )) + readonly OLDER_THAN_ANDROID_5=$(( $(adb -s $ADB_DEVICE shell getprop ro.build.version.sdk | tr -d '\r') < 21 )) + readonly NEWER_THAN_ANDROID_9=$(( $(adb -s $ADB_DEVICE shell getprop ro.build.version.sdk | tr -d '\r') >= 29 )) - readonly SYSTEM_MOUNT_POINT=$(adb shell getprop ro.build.system_root_image | grep "true" && echo "/system_root" || echo "/system") + readonly SYSTEM_MOUNT_POINT=$(adb -s $ADB_DEVICE shell getprop ro.build.system_root_image | grep "true" && echo "/system_root" || echo "/system") declare -a CUSTOM_LIST=() # Trimed APK/package list only containing packages/APK on the device (populated by generate_custom_list()) declare -a EXTERNAL_LIST=() # APK/package list provided by the user (populated by import_external_list()) @@ -66,7 +100,7 @@ main() { printf "${Bold}%s\n\n" "Please carefully read the FAQ before using this script!" - if [[ $(adb get-state) = "recovery" ]]; then uad_recovery_mode || exit; fi + if [[ $(adb -s $ADB_DEVICE get-state) = "recovery" ]]; then uad_recovery_mode || exit; fi printf "%s${nBold} " "Do you want to do an ADB backup ? [Y/N]" read -r @@ -84,7 +118,7 @@ main() { fi while true; do - adb shell 'pm list packages -s' | sed 's/package://g' > remaining_packages.txt & + adb -s $ADB_DEVICE shell 'pm list packages -s' | sed 's/package://g' > remaining_packages.txt & clear -x printf "\n${BORANGE}%s\n" "================== MAIN MENU ==================" @@ -164,7 +198,7 @@ main() { if [[ "$REPLY" =~ 8 ]]; then create_flashable_zip; fi if [[ "$REPLY" =~ 0 ]]; then debloat_or_restore pending; fi - elif [[ "$REPLY" =~ [Xx] ]]; then adb reboot && exit 0; + elif [[ "$REPLY" =~ [Xx] ]]; then adb -s $ADB_DEVICE reboot && exit 0; fi done @@ -198,7 +232,7 @@ import_external_list() { associate_path_with_package() { local -n ASSOCIATE="$1" declare -a PACKAGES=() - readarray -t PACKAGES < <(adb shell 'pm list packages -s -u -f' | sed -r 's/package://g' | sort -i) + readarray -t PACKAGES < <(adb -s $ADB_DEVICE shell 'pm list packages -s -u -f' | sed -r 's/package://g' | sort -i) for i in "${PACKAGES[@]}"; do ASSOCIATE["${i##*=}"]="${i%=*}" @@ -223,7 +257,7 @@ generate_custom_list() { for p in "${TEMP[@]}"; do - [[ RESTORE -ne 1 ]] && printf "${BBLUE}%s${NC}" "$p --> " && adb shell "pm clear $p" + [[ RESTORE -ne 1 ]] && printf "${BBLUE}%s${NC}" "$p --> " && adb -s $ADB_DEVICE shell "pm clear $p" CUSTOM_LIST+=("${PACKAGE_AND_PATH[$p]}") done @@ -264,7 +298,7 @@ debloat_or_restore() { fi # parse user list on the phone - readarray -t USERS < <(adb shell pm list users | grep -o "{[0-9]*" | grep -o "[0-9]*") + readarray -t USERS < <(adb -s $ADB_DEVICE shell pm list users | grep -o "{[0-9]*" | grep -o "[0-9]*") clear -x if [[ $# -gt 0 ]]; then @@ -312,7 +346,7 @@ debloat_or_restore() { do_package_action_and_log() { printf "${BBLUE}%s${BRED}%s${NC}%s" "[user $u_num] " "$package --> " - output="$(eval adb shell "$action")" && echo "$output" + output="$(eval adb -s $ADB_DEVICE shell "$action")" && echo "$output" if [[ ! "$output" =~ Failure|Error ]]; then if [[ $RESTORE -eq 0 ]]; then @@ -328,7 +362,7 @@ backup_apks() { local -n apks="$1" mkdir -p apks_backup for apk in "${apks[@]}"; do - if ! adb pull "$apk" apks_backup/; then + if ! adb -s $ADB_DEVICE pull "$apk" apks_backup/; then printf "\n${BRED}%s${NC}%s\n" "$apk" " cannot be found on the phone!" printf "\n${BRED}%s${NC}\n\n" "APKs backup failed" && exit 1 fi @@ -414,17 +448,17 @@ restore_apks() { return fi - adb shell "su -c \"mount -o rw,remount $SYSTEM_MOUNT_POINT\"" + adb -s $ADB_DEVICE shell "su -c \"mount -o rw,remount $SYSTEM_MOUNT_POINT\"" for p in "${CUSTOM_LIST[@]}"; do # $p = path/to/app.apk local apk=$(echo "$p" | sed -r 's/.*\///g') # app.apk local dir=$(dirname "$p") # path/to - if adb push "apks_backup/$apk" /sdcard && adb shell "su -c 'mkdir -p \"$dir\"; mv \"/sdcard/$apk\" \"$dir\"; chmod 644 \"$p\"'"; then + if adb -s $ADB_DEVICE push "apks_backup/$apk" /sdcard && adb -s $ADB_DEVICE shell "su -c 'mkdir -p \"$dir\"; mv \"/sdcard/$apk\" \"$dir\"; chmod 644 \"$p\"'"; then grep -Fqv "$p" deleted_apks.txt > temp.tmp || touch temp.tmp; mv temp.tmp deleted_apks.txt fi done - adb shell "su -c \"mount -f -o ro,remount $SYSTEM_MOUNT_POINT\"" + adb -s $ADB_DEVICE shell "su -c \"mount -f -o ro,remount $SYSTEM_MOUNT_POINT\"" printf "\n${BRED}%s${NC}%s\n" "Reboot your phone and Android will reinstall the app(s)" printf "\n\e[5m%s\033[0m" "Press any key to continue" read -n 1 -r -s @@ -463,7 +497,7 @@ root_debloat() { done commandes+="mount -f -o ro,remount $SYSTEM_MOUNT_POINT" printf "\n${BRED}%s${NC}\n\n" "Check your phone: Magisk Manager is probably asking you to grand root permissions for ADB" - adb shell "su -c '$commandes'" + adb -s $ADB_DEVICE shell "su -c '$commandes'" printf "\n${BRED}%s${NC}\n" "DONE" printf "\n%s${BRED}%s${NC}\n" "A list of deleted apks has been backed-up in " "deleted_apks.txt" printf "\n\e[5m%s\033[0m" "Press any key to continue" @@ -602,7 +636,7 @@ list_installed_packages() { echo - readarray -t packages < <(adb shell "pm list packages -f" | grep -i "$REPLY" | sed 's/package://g' | sort || true) + readarray -t packages < <(adb -s $ADB_DEVICE shell "pm list packages -f" | grep -i "$REPLY" | sed 's/package://g' | sort || true) for i in "${packages[@]}"; do printf "%s${BBLUE}%s${NC}\n" "${i##*=}" " ${i%=*}" @@ -617,7 +651,7 @@ restore_backup() { printf "${BRED}%s${NC}" "Enter the path of the backup to restore : " read -r - check_backup_integrity "$REPLY" && adb restore "$REPLY" || printf "\n${BRED}%s${NC}\n" "The backup is corrupted. Abort!" + check_backup_integrity "$REPLY" && adb -s $ADB_DEVICE restore "$REPLY" || printf "\n${BRED}%s${NC}\n" "The backup is corrupted. Abort!" echo printf "\e[5m%s\033[0m" "Press any key to continue" read -n 1 -r -s @@ -627,7 +661,7 @@ backup() { clear -x local backup=$(date +%Y-%m-%d-%H:%M:%S) - adb backup -apk -all -system -f "${PHONE:-phone}-${backup}.adb" # -noshare option is default + adb -s $ADB_DEVICE backup -apk -all -system -f "${PHONE:-phone}-${backup}.adb" # -noshare option is default echo printf "\n\e[5m%s\033[0m" "Press a key when the backup is done (your phone will tell you) " read -n 1 -r -s @@ -649,7 +683,7 @@ check_backup_integrity() { get_brand() { local brand - brand=$(adb shell getprop ro.product.brand | tr -d '\r' | awk '{print tolower($0)}') + brand=$(adb -s $ADB_DEVICE shell getprop ro.product.brand | tr -d '\r' | awk '{print tolower($0)}') # Support for alternative name case "$brand" in @@ -675,7 +709,7 @@ uad_recovery_mode() { case $REPLY in 1) { title="RESTORE"; RESTORE=1; };; 2) { title="DEBLOAT"; RESTORE=0; };; - [Xx]) { adb reboot && exit 0; };; + [Xx]) { adb -s $ADB_DEVICE reboot && exit 0; };; *) exit esac @@ -726,12 +760,12 @@ uad_recovery_mode() { local dir=$(dirname "$p") # path/to if [[ $RESTORE -eq 0 ]]; then - adb shell "rm -rf $p" && echo "rm -rf $p" + adb -s $ADB_DEVICE shell "rm -rf $p" && echo "rm -rf $p" grep -qxF "$p" deleted_apks.txt || echo "$p" >> "deleted_apks.txt" else - echo "adb push apks_backup/$apk /\"$p\"" - if adb shell "mkdir -p $dir" && adb push "apks_backup/$apk" "/$p"; then + echo "adb -s $ADB_DEVICE push apks_backup/$apk /\"$p\"" + if adb -s $ADB_DEVICE shell "mkdir -p $dir" && adb -s $ADB_DEVICE push "apks_backup/$apk" "/$p"; then grep -qFv "$p" deleted_apks.txt > temp.tmp || touch temp.tmp; mv temp.tmp deleted_apks.txt fi fi @@ -740,4 +774,4 @@ uad_recovery_mode() { done } -main "$@"; exit \ No newline at end of file +main "$@"; exit