# Android Debug Bridge
https://source.android.com/setup/build/adb
https://developer.android.com/studio/command-line/adb

# Enabling debug mode
Settings > About Phone > Software information >
Scroll down to 'Build number' and tap that box 7 times

# Enabling debug mode (Android version 11)
Settings > About Phone > Version >
Scroll down to 'Build number' and tap that box 7 times >
Enter PIN to confirm

# Enabling usb debugging
Setting > Search > Type 'developer options' > Select the 'Developer options'
under 'Developer options' (and not the one under 'Screen reader') > Scroll
down to 'USB debugging' directly under the 'Debugging  header' and slide the
switch from Off (left) to On (right) > Press 'OK' to confirm you want to enable
USB debugging.

# Connecting via ADB (USB mode)
$ adb start-server
$ adb connect <ip address>                  # skip if connect to usb
$ adb devices
$ adb shell
$ ls
$ pm list packages
$ pm list packages | grep <search-term>
$ pm list packages -s | grep <search-term>  # system apps
$ pm uninstall -k --user 0 <pkgname>        # keep app data intact
$ pm uninstall --user 0 <pkgname>           # removing bloated app
$ pm path <pkgname>                         # path to the apk file
$ exit
$ adb kill-server

pm list packages -f             # see their associated file
pm list packages -d             # filter to only show disabled packages
pm list packages -e             # filter to only show enabled packages
pm list packages -i             # see the installer for the packages
pm list packages -u             # also include uninstalled packages

Note: The command pm list packages -f will often fill up several screens. That
is not useful for forensic examination. So, the first step is to exit the shell
using exit. Then, with any command, you can export the output to a text file.
When executing shell commands from outside the shell, you should precede the
command with adb shell.

adh shell pm list packages -f > package.txt

# list of all connected devices
adb devices -l

# list all the installed packages
adb shell pm list packages

# list the package names as well as the path to the installed APK files
adb shell pm list packages -f

# list only the system packages
adb shell pm list packages -s

# list system package names with the installed package location
adb shell pm list packages -f -s

# list only 3rd party (non-system) packages
adb shell pm list packages -3

# list 3rd party package names with the installed package location
adb shell pm list packages -f -3

# list all the disabled package names
adb shell pm list packages -d

# list all the enabled package names
adb shell pm list packages -e

# information
adb shell getprop ro.product.model
adb shell getprop ro.build.version.release
adb shell getprop ro.serialno
adb shell getprop ro.board.platform
adb shell getprop ro.build.description
adb shell getprop ro.product.locale.language
adb shell getprop ro.product.locale.region
adb shell getprop ro.board.platform
adb shell getprop ro.build.version.codename

# command parameters format to backup data without unlock/root
adb backup [-f <file>] [-apk | -noapk] [-shared | -noshared] [-all]
 [-system | -nosystem] [<packages...>]

# backup system and app data but not the apps themselves
adb backup -all
(by default it saves device data to the current directory as backup.ab)

# backup system and app data to a file but not the apps themselves
adb backup -all -f /home/user/android_backup/backup_file.ab

# backup system and app data including the apps themselves
adb backup -all -apk

# backup system and app data alone and does not backup apps (default)
adb backup -all -noapk

# backup data on the SD card (enable backup of the device's shared storage contents)
adb backup -all -shared

# does not backup data on the SD card (default)
adb backup -all -noshared

# -system sets whether or not the -all flag also includes system applications or not
adb backup -all -system     # include system applications

# -nosystem (default)
adb backup -all -nosystem   # no system applications

# backup a specific application package
adb backup <packages...>

adb backup -noapk com.android.vending -f backup_google_play_store.ab

The above command indicates that we want ot backup "com.android.vending" package to
"backup_google_play_store.ab" file as indicated with the -f flag. The .ab says it is
an Android Backup file. The -noapk flag indicates that we don't want the .apk file to
be backed up. Now we should see a confirmation dialog prompt to start the backup. The
backup will not start until we press "Back up my data".

# example backup command
adb backup -apk -shared -all -f /home/user/android_backup/backup_file.ab

# restore the backup data
adb restore /home/user/android_backup/backup_file.ab

# restore a specific application package
adb restore backup_google_play_store.ab

# files
$ adb shell
$ cd storage/self/primary/
$ cd sdcard/                [same directory as above]

Asus - 49.42 GB used of 64 GB [77% used]

# Apps to be remove
Calculator - com.asus.calculator                        172 KB   (rm)
Calendar - com.google.android.calendar                  14.97 MB (rm)
Chrome - com.android.chrome                             85.77 MB (rm)
Drive - com.google.android.apps.docs                    16.76 MB (rm)
Duo - com.google.android.apps.tachyon                   10.85 MB (rm)
facebook - com.facebook.katana                          119 KB   (rm)
Facebook App Installer - com.facebook.system                     (rm)
Facebook App Manager - com.facebook.appmanager                   (rm)
Gboard - com.google.android.inputmethod.latin           9.74 MB  (rm)
Gmail - com.google.android.gm                           18.83 MB (rm)
Google - com.google.android.googlequicksearchbox        62.82 MB (rm)
Google Play Movies & TV - com.google.android.videos     22.34 MB (rm)
Google Play Music - com.google.android.music            18.65 MB (rm)
Google Play Store - com.android.vending                 3.34 MB  (rm)
Google Text-to-speech Engine - com.google.android.tts            (rm)
Instagram - com.instagram.android                       119 KB   (rm)
Maps - com.google.android.apps.maps                     41.68 MB (rm)
Messenger - com.facebook.orca                           119 KB   (rm)
Photos - com.google.android.apps.photos                 25.83 MB (rm)
Youtube - com.google.android.youtube                    24.89 MB (rm)


# fdroidcl [https://github.com/mvdan/fdroidcl]
fdroidcl update                     # fetch and update the fdroid index
fdroidcl search                     # show all available apps
fdroidcl search <search_term>       # search for an app in the index
fdroidcl show <appid_term>          # display info about an app
fdroidcl download <appid_term>      # download an APK file
fdroidcl install <appid_term>       # install an app
fdroidcl uninstall <appid_term>     # uninstall an app
fdroidcl search -u                  # show all available updates
fdroidcl install -u                 # install all available updates

Commands:
update                              Update the index
search [<regexp...>]                Search available apps
show <appid...>                     Show detailed info about an app
install [<appid...>]                Install or upgrade apps
uninstall <appid...>                Uninstall an app
download <appid...>                 Download an app
devices                             List connected devices
list (categories)                   List all known values of a kind
defaults                            Reset to the default settings
version                             Print version information

An appid is just an app's unique package name. A specific version of an app
can be selected by following the appid with a colon and the version code. The
'search' and 'show' commands can be used to find these strings.

For example:
$ fdroidcl search redreader
$ fdroidcl show org.quantumbadger.redreader
$ fdroidcl install org.quantumbadger.redreader:85

List of search options:
$ fdroidcl search [option]
 -q   print package names only
 -i   filter installed apps
 -u   filter apps with updates
 -d   select apps last updated in the last <n> days; a negative value drops them
 -c   filter apps by category
 -o   sort order (added, updated)

List all installed apps:
To list all installed apps on a device we can use the following command:
$ fdroidcl search -i -q


# adb version
$ adb version

# get state
$ adb shell settings get global airplane_mode_on    [0/1 - disabled/enabled]
$ adb shell settings get global mobile_data         [0/1 - disabled/enabled]
$ adb shell settings get global wifi_on             [0/1 - disabled/enabled]
$ adb shell settings get global bluetooth_on        [0/1 - disabled/enabled]
$ adb shell settings get global cell_on             [0/1 - disabled/enabled]

# enable/disable wifi
$ adb shell svc wifi enable
$ adb shell svc wifi disable

# enable/disable data
$ adb shell svc data enable
$ adb shell svc data disable

# enable/disable airplane
# enable:
$ adb shell settings put global airplane_mode_on 1
$ adb shell am broadcast -a android.intent.action.AIRPLANE_MODE
# disable:
$ adb shell settings put global airplane_mode_on 0
$ adb shell am broadcast -a android.intent.action.AIRPLANE_MODE

# enable/disable bluetooth
# enable:
$ adb shell settings put global bluetooth_disabled_profiles 1
# disable:
$ adb shell settings put global bluetooth_disabled_profiles 0

# bash script that checks state and then switch on/off:
#!/bin/bash
function toggle_wifi {
    if [[ $(adb shell settings get global wifi_on) == "1" ]]; then
        adb shell svc wifi disable
    else
        adb shell svc wifi enable; fi
}

function toggle_data {
    if [[ $(adb shell settings get global mobile_data) == "1" ]]; then
        adb shell svc data disable
    else
        adb shell svc data enable; fi
}

toggle_wifi
toggle_data
exit 0;

Reference:
https://stackoverflow.com/questions/23528824/is-there-an-adb-command-to-enable-
disable-mobile-datarooted-device
https://android.stackexchange.com/questions/228469/how-to-switchnot-only-on-or-
off-the-mobile-data-and-wifi-using-adb

# delete contact via adb
# to query contact
$ adb shell content query --uri content://com.android.contacts/contacts
# to add contact (where's id = 8)
$ adb shell content edit --uri content://com.android.contacts/contacts/8
# to remove contact (where's id = 1)
$ adb shell content delete --uri content://com.android.contacts/contacts/1
# to remove all contacts at once
$ adb shell pm clear com.android.providers.contacts

# take screenshots and recordings
Let's start recordingg a video of our device screen by running the following
command:
$ adb shell screenrecord /sdcard/Movies/video.mp4
This will record the device screen and save the video file to the path
/sdcard/Movies/video.mp4. To stop recording press Ctrl+C to finish the command.
In order to take a screenshot of some screen of our app we just need to navigate
to the desired screen, and then run the command:
$ adb shell screencap -p /sdcard/Pictures/screenshot.png
The command above will take a screenshot of the current screen and save it at
the path /sdcard/Pictures/screenshot.png. If you tried running the command, you
may have noticed that no output text is displayed, so how can we check if the
screenshot was really captured? Fortunately, we can use ADB to several commands
that are well known by users who are familiar with Unix commands, like the ls
which we will use to list the files from the Pictures directory:
$ adb shell ls /sdcard/Pictures/
screenshot.png

$ adb shell mkdir /sdcard/Pictures/Screenshots
$ adb shell ls /sdcard/Pictures/
Screenshots
screenshot.png

# list file
$ adb shell ls /sdcard/Download/*.pdf
# move file
$ adb shell mv /sdcard/Pictures/screenshot.png /sdcard/Pictures/Screenshots/
# copy file
$ adb shell cp /sdcard/Pictures/screenshot.png /sdcard/Pictures/Screenshots/
# delete file
$ adb shell rm /sdcard/Pictures/screenshot.png
# delete directory
$ adb shell rm -r /sdcard/Pictures/Screenshots
# print log to console output (press ctrl+c to stop it)
$ adb logcat

# transfer files from android to device:
adb pull <remote file> <local location>
mkdir android
cd android
adb pull "/sdcard/DCIM/Camera/20210329_134800.jpg"
adb pull "/sdcard/Download/"
adb pull "/sdcard/Videos/"
ls
20210329_134800.jpg Download/ Videos/

# transfer files  from device to android:
adb push <local file> <remote location>

# 11 cool adb tricks to try on your android phone
https://www.smartprix.com/bytes/cool-adb-commands-tips-tricks/
# How to remove bloatware from Realme, Xiaomi, Samsung and other phones
https://www.smartprix.com/bytes/how-to-remove-bloatware-from-realme-xiaomi-and-
samsung-phones/
# Useful adb commands for android testing
https://blog.testproject.io/2021/08/10/useful-adb-commands-for-android-testing/

# adb netstat command
adb shell netstat

# ls command
ls -a                   # show hidden files
ls -lh                  # show sizes in a human readable format
ls -R                   # recursively show sub directories
ls -ltr                 # show last modification date
ls -n                   # see UID or GID of files
ls -t                   # sort by time and date

# ps command
ps -A                   # show active processes
ps -E                   # show active processes
ps -U root -u root      # see processes running with root privileges
ps -e --forest          # see the process tree
ps -f --forest -c sshd  # see the process tree for a specific process (sshd)
ps -C sshd              # see child processes for a specific process
ps -r                   # see all running processes

$ adb shell
$ cd system/app/Facebook/

# get the name/details of the current running activity
adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'

# start an application
adb shell am start -n com.packge.name/com.package.name.ActivityName

# scroll screen
adb shell input swipe 300 300 500 1000  # scroll screen up
adb shell input swipe 500 1000 300 300  # scroll screen down

# send text using virtual keyboard
adb shell input text "text to be send"

# send keyevent as physical keyboard
adb shell input keyevent 66     # 66 is key_code for "Enter"
