#!/system/bin/sh

# see: https://www.xda-developers.com/broken-navigation-bar-lock-screen-ota-update/
update_fix () {
	settings put global device_provisioned 1 && \
	settings put secure user_setup_complete 1
}

boot_count () {
	settings get global boot_count
}

airplane_mode () {
	clear

	disabled_radios=""
	toggable_radios="wifi,cell,bluetooth,nfc,wimax,"

	echo "enter desired radions [first column] space separated

wifi		WiFi/WLAN
cell		cellular/mobile Network
bluetooth	Bluetooth
nfc		NFC
wimax		WiMAX"
	read -r user_input

	for field in ${user_input}; do
		case ${field} in
			wifi | cell | bluetooth | nfc | wimax )
				disabled_radios="${field},${disabled_radios}"
				toggable_radios=$(echo "${toggable_radios}" | sed "s/${field},//g")
			;;
			* )
				echo "${field} is unknown"
			;;
		esac
	done

	settings put global airplane_mode_radios ${disabled_radios}
	settings put global airplane_mode_toggleable_radios ${toggable_radios}
}

audio_focus () {
	cmd appops set ${1} TAKE_AUDIO_FOCUS ignore
}

read_clipboard () {
	cmd appops set ${1} READ_CLIPBOARD ignore
}

# by ale5000
reset_gms_data () {
	if [ -z ${1} ]; then
		find /data/data/*/shared_prefs -name com.google.android.gms.*.xml -delete
	else
		find /data/data/${1}/shared_prefs -name com.google.android.gms.*.xml -delete
	fi
}

error () {
	echo "!! ${@}"
	exit 1
}

[[ $(whoami) != "root" ]] && error "not running as root"

case ${1} in
	-f | --fix-update )
		update_fix
	;;

	-a | -P | --airplane-mode )
		airplane_mode
	;;

	-b | --boot-count )
		boot_count
	;;

	-F | --audio-focus )
		audio_focus "${2}"
	;;

	-b | --read-clipboard )
		read_clipboard "${2}"
	;;

	-m | --fix-microg )
		fix_microg
	;;

	-r | --reset-gms-data )
		reset_gms_data "${2}"
	;;

	-R | --reset-permissions )
		pm reset-permissions
	;;

	* )
	echo "nanodroid-util

Misc. Functions
-f | --fix-update			fix navbar, lockscreen, statusbar after OTA update
-a | --airplane-mode			change airplane mode settings
-F | --audio-focus			prevent an app from stealing audio focus
-b | --boot-count			read boot counts (Android 7+)
-B | --read-clipboard			prevent an app from reading the clipboard
-r | --reset-gms-data			reset GMS data for given app (or all if none)
-R | --reset-permissions		reset runtime permissions"
	;;
esac
