fedora-csb-system-manager
  1#!/usr/bin/env nix-shell
  2#! nix-shell -i bash -p android-tools curl
  3# shellcheck shell=bash
  4
  5# BOOX Tablet Debloat Script
  6# Disables safe BOOX bloatware packages to free up RAM
  7# Created: 2026-01-15
  8# ONLY runs on NoteAir4C devices for safety
  9
 10set -e
 11
 12# Colors for output
 13RED='\033[0;31m'
 14GREEN='\033[0;32m'
 15YELLOW='\033[1;33m'
 16BLUE='\033[0;34m'
 17NC='\033[0m' # No Color
 18
 19echo -e "${BLUE}========================================${NC}"
 20echo -e "${BLUE}BOOX Tablet Debloat Script${NC}"
 21echo -e "${BLUE}========================================${NC}"
 22echo
 23
 24# Check if device is connected
 25echo -e "${YELLOW}Checking for connected device...${NC}"
 26if ! adb devices | grep -q "device$"; then
 27	echo -e "${RED}ERROR: No device connected!${NC}"
 28	echo "Please ensure:"
 29	echo "  1. USB debugging is enabled on your BOOX tablet"
 30	echo "  2. Device is connected via USB"
 31	echo "  3. You've authorized this computer on the device"
 32	exit 1
 33fi
 34
 35DEVICE=$(adb devices | grep "device$" | awk '{print $1}')
 36echo -e "${GREEN}✓ Device connected: $DEVICE${NC}"
 37echo
 38
 39# Get device info
 40echo -e "${YELLOW}Verifying device model...${NC}"
 41MODEL=$(adb shell getprop ro.product.model)
 42ANDROID=$(adb shell getprop ro.build.version.release)
 43FIRMWARE=$(adb shell getprop ro.build.display.id)
 44
 45echo "  Model: $MODEL"
 46echo "  Android: $ANDROID"
 47echo "  Firmware: $FIRMWARE"
 48echo
 49
 50# Safety check: Only run on NoteAir4C
 51if [ "$MODEL" != "NoteAir4C" ]; then
 52	echo -e "${RED}ERROR: This script is configured to only run on NoteAir4C devices!${NC}"
 53	echo -e "${RED}Detected model: $MODEL${NC}"
 54	echo
 55	echo "This is a safety measure to prevent accidentally debloating the wrong device."
 56	echo
 57	echo "If you want to run this on a different BOOX model, edit the script and change"
 58	echo "the MODEL check to match your device."
 59	exit 1
 60fi
 61
 62echo -e "${GREEN}✓ Confirmed NoteAir4C - proceeding with safety checks${NC}"
 63echo
 64
 65# =============================================================================
 66# Safety Check: Ensure critical replacements are installed
 67# =============================================================================
 68
 69echo -e "${BLUE}========================================${NC}"
 70echo -e "${BLUE}Pre-flight Safety Checks${NC}"
 71echo -e "${BLUE}========================================${NC}"
 72echo
 73
 74SAFETY_ISSUES=0
 75
 76# Check for alternative launcher (if we plan to remove com.onyx launcher later)
 77echo -e "${YELLOW}Checking for alternative launcher...${NC}"
 78ALTERNATIVE_LAUNCHERS=(
 79	"ru.execbit.aiolauncher"        # AIO Launcher (recommended for e-ink)
 80	"ch.deletescape.lawnchair.plah" # Lawnchair (F-Droid)
 81	"com.teslacoilsw.launcher"      # Nova Launcher
 82)
 83
 84HAS_ALT_LAUNCHER=0
 85LAUNCHER_PACKAGE=""
 86for launcher in "${ALTERNATIVE_LAUNCHERS[@]}"; do
 87	if adb shell pm list packages | grep -q "^package:$launcher$"; then
 88		LAUNCHER_NAME=$(adb shell pm list packages | grep "$launcher" | cut -d: -f2)
 89		echo -e "${GREEN}✓ Found alternative launcher: $LAUNCHER_NAME${NC}"
 90		HAS_ALT_LAUNCHER=1
 91		LAUNCHER_PACKAGE=$launcher
 92
 93		# Set as default if it's AIO Launcher
 94		if [ "$launcher" = "ru.execbit.aiolauncher" ]; then
 95			echo -e "${YELLOW}  Setting AIO Launcher as default...${NC}"
 96			if adb shell cmd package set-home-activity ru.execbit.aiolauncher/.MainActivity 2>&1 | grep -q "Success"; then
 97				echo -e "${GREEN}  ✓ Set as default launcher${NC}"
 98			else
 99				echo -e "${YELLOW}  ⚠ Could not set as default automatically${NC}"
100				echo -e "${YELLOW}  Please set manually: Home button → AIO Launcher → Always${NC}"
101			fi
102		fi
103		break
104	fi
105done
106
107if [ $HAS_ALT_LAUNCHER -eq 0 ]; then
108	echo -e "${YELLOW}⚠ No alternative launcher found${NC}"
109	echo "  Note: Not removing BOOX launcher (com.onyx) - it's safe for now."
110	echo "  If you want to remove it later, install an alternative launcher first."
111fi
112echo
113
114# Check for alternative keyboard (if we plan to remove ONYX keyboards)
115echo -e "${YELLOW}Checking for alternative keyboard...${NC}"
116ALTERNATIVE_KEYBOARDS=(
117	"com.google.android.inputmethod.latin" # Gboard (already installed)
118	"org.pocketworkstation.pckeyboard"     # Hacker's Keyboard
119	"com.menny.android.anysoftkeyboard"    # AnySoftKeyboard
120	"rkr.simplekeyboard.inputmethod"       # Simple Keyboard
121	"fr.bepo.clavierexterne"               # BÉPO (already installed)
122)
123
124HAS_ALT_KEYBOARD=0
125KEYBOARD_NAMES=""
126for keyboard in "${ALTERNATIVE_KEYBOARDS[@]}"; do
127	if adb shell pm list packages | grep -q "^package:$keyboard$"; then
128		KEYBOARD_NAME=$(adb shell pm list packages | grep "$keyboard" | cut -d: -f2)
129		KEYBOARD_NAMES="$KEYBOARD_NAMES\n    - $KEYBOARD_NAME"
130		HAS_ALT_KEYBOARD=1
131	fi
132done
133
134if [ $HAS_ALT_KEYBOARD -eq 1 ]; then
135	echo -e "${GREEN}✓ Found alternative keyboard(s):${NC}"
136	echo -e "$KEYBOARD_NAMES"
137else
138	echo -e "${RED}✗ No alternative keyboard found!${NC}"
139	echo "  Installing Google Keyboard or another alternative is recommended"
140	echo "  if you plan to disable ONYX keyboards."
141	((SAFETY_ISSUES++))
142fi
143echo
144
145# Check for F-Droid (useful for installing alternatives)
146echo -e "${YELLOW}Checking for F-Droid...${NC}"
147if adb shell pm list packages | grep -q "^package:org.fdroid.fdroid$"; then
148	echo -e "${GREEN}✓ F-Droid is installed${NC}"
149
150	# Check if it's disabled
151	if adb shell pm list packages -d | grep -q "^package:org.fdroid.fdroid$"; then
152		echo -e "${YELLOW}  ⚠ F-Droid is currently disabled - enabling it...${NC}"
153		adb shell pm enable org.fdroid.fdroid
154		echo -e "${GREEN}  ✓ F-Droid enabled${NC}"
155	fi
156else
157	echo -e "${YELLOW}⚠ F-Droid not installed${NC}"
158	echo "  F-Droid is useful for installing alternative apps, but not required."
159	echo "  AIO Launcher can be installed from Google Play Store instead."
160fi
161echo
162
163# Offer to install AIO Launcher if not present
164if [ $HAS_ALT_LAUNCHER -eq 0 ]; then
165	echo -e "${YELLOW}Recommendation: Install AIO Launcher${NC}"
166	echo "  AIO Launcher is a minimalist launcher designed for simplicity and speed."
167	echo "  It's particularly well-suited for e-ink displays."
168	echo "  Features:"
169	echo "    - Text-based minimal interface (perfect for e-ink)"
170	echo "    - Very low memory footprint"
171	echo "    - Fast, no animations"
172	echo "    - Highly customizable"
173	echo
174	echo "  This script can automatically download and install AIO Launcher for you."
175	echo
176	read -p "  Download and install AIO Launcher now? [Y/n] " -n 1 -r
177	echo
178	if [[ ! $REPLY =~ ^[Nn]$ ]]; then
179		echo -e "${YELLOW}Downloading AIO Launcher...${NC}"
180
181		# Create temp directory
182		TEMP_DIR=$(mktemp -d)
183		APK_PATH="$TEMP_DIR/aio-launcher.apk"
184
185		# Download from Uptodown (reliable APK mirror)
186		# Note: This is the free version from official Uptodown mirror
187		echo "  Source: Uptodown (reliable APK mirror)"
188
189		# Get latest version download URL from Uptodown
190		# Uptodown provides direct APK downloads for Android apps
191		DOWNLOAD_URL="https://dw.uptodown.com/dwn/VgGOk7_tR_2XAHTeDUlQwXdxn6_o9rN3WT-ypxdA-t7MyzTWcWoPgJ1sqPXxRWO2TBu0ZcLCY3r60hSmX-5hG-b8RWqXlBQqOSRx6m-q1mCYOCApnLd9O0HnMPqbhevU/DudI_KHcChA0M2TvhAxE3TjXSC7SDRZdJAoVm_9MuJKCREMOBVYN8Uig7-f3fWQXbqLmOjgMcuQtSFWaEwJ0SX90J4_PcP1C1A5dXXbKTIFT0I8y-6VVp9-zFCYiPKhv/Xhf4XPFxpyQs0WqjhzPJuw==/aio-launcher.apk"
192
193		if curl -L -o "$APK_PATH" "$DOWNLOAD_URL" 2>/dev/null; then
194			echo -e "${GREEN}  ✓ Downloaded successfully${NC}"
195
196			echo -e "${YELLOW}Installing AIO Launcher...${NC}"
197			if adb install "$APK_PATH" 2>&1 | grep -q "Success"; then
198				echo -e "${GREEN}  ✓ AIO Launcher installed successfully${NC}"
199				HAS_ALT_LAUNCHER=1
200				LAUNCHER_PACKAGE="ru.execbit.aiolauncher"
201
202				# Set as default launcher
203				echo -e "${YELLOW}Setting AIO Launcher as default launcher...${NC}"
204
205				# Set AIO Launcher as default home activity
206				if adb shell cmd package set-home-activity ru.execbit.aiolauncher/.MainActivity 2>&1 | grep -q "Success"; then
207					echo -e "${GREEN}  ✓ AIO Launcher set as default launcher${NC}"
208				else
209					echo -e "${YELLOW}  ⚠ Could not automatically set default launcher${NC}"
210					echo "  Please set manually: Home button → AIO Launcher → Always"
211				fi
212			else
213				echo -e "${RED}  ✗ Failed to install AIO Launcher${NC}"
214				echo "  You can install manually from Google Play Store instead."
215			fi
216
217			# Cleanup
218			rm -f "$APK_PATH"
219			rmdir "$TEMP_DIR"
220		else
221			echo -e "${RED}  ✗ Failed to download AIO Launcher${NC}"
222			echo "  You can install manually from Google Play Store instead."
223			rm -rf "$TEMP_DIR"
224		fi
225	else
226		echo -e "${YELLOW}  Install AIO Launcher manually from Google Play Store:${NC}"
227		echo "    1. Open Play Store on your BOOX"
228		echo "    2. Search for 'AIO Launcher'"
229		echo "    3. Install it"
230		echo "    4. Set as default launcher (Home button → AIO Launcher → Always)"
231		echo
232		read -p "  Continue without alternative launcher? [y/N] " -n 1 -r
233		echo
234		if [[ ! $REPLY =~ ^[Yy]$ ]]; then
235			echo -e "${YELLOW}Exiting. Install AIO Launcher and run this script again.${NC}"
236			exit 0
237		fi
238	fi
239	echo
240fi
241
242# Summary of safety checks
243if [ $SAFETY_ISSUES -gt 0 ]; then
244	echo -e "${YELLOW}⚠ Warning: $SAFETY_ISSUES safety issue(s) detected${NC}"
245	echo "  Review the warnings above before proceeding."
246	echo
247fi
248
249# =============================================================================
250# Memory stats before debloating
251# =============================================================================
252
253echo -e "${BLUE}========================================${NC}"
254echo -e "${BLUE}Memory Analysis${NC}"
255echo -e "${BLUE}========================================${NC}"
256echo
257
258echo -e "${YELLOW}Memory Before Debloating:${NC}"
259MEM_TOTAL=$(adb shell cat /proc/meminfo | grep MemTotal | awk '{print $2}')
260MEM_FREE_BEFORE=$(adb shell cat /proc/meminfo | grep MemFree | awk '{print $2}')
261MEM_AVAILABLE_BEFORE=$(adb shell cat /proc/meminfo | grep MemAvailable | awk '{print $2}')
262echo "  Total RAM: $((MEM_TOTAL / 1024)) MB"
263echo "  Free RAM: $((MEM_FREE_BEFORE / 1024)) MB"
264echo "  Available RAM: $((MEM_AVAILABLE_BEFORE / 1024)) MB"
265echo
266
267# =============================================================================
268# Package removal/disabling - Interactive Selection
269# =============================================================================
270
271echo -e "${BLUE}========================================${NC}"
272echo -e "${BLUE}Package Selection${NC}"
273echo -e "${BLUE}========================================${NC}"
274echo
275
276# Define all packages with safety levels
277# Format: "package_name|safety_level|description|memory_mb"
278
279declare -A PACKAGE_INFO
280
281# ===== SAFE - No dependencies, no critical functionality =====
282PACKAGE_INFO["com.onyx.mail"]="SAFE|Email client - use alternatives like K-9 Mail|198"
283PACKAGE_INFO["com.onyx.gallery"]="SAFE|Gallery app - use alternatives like Immich or Simple Gallery|161"
284PACKAGE_INFO["com.onyx.easytransfer"]="SAFE|File transfer utility - not needed|146"
285PACKAGE_INFO["com.onyx.aiassistant"]="SAFE|AI Assistant - not essential|139"
286PACKAGE_INFO["com.onyx.android.ksync"]="SAFE|Cloud sync service - use alternatives like Syncthing|201"
287PACKAGE_INFO["com.onyx.calculator"]="SAFE|Calculator app - Android has built-in|0"
288PACKAGE_INFO["com.onyx.musicplayer"]="SAFE|Music player - not ideal for e-ink|0"
289PACKAGE_INFO["com.onyx.voicerecorder"]="SAFE|Voice recorder - rarely used on e-ink|0"
290PACKAGE_INFO["com.onyx.dict"]="SAFE|Dictionary app - use online alternatives|0"
291PACKAGE_INFO["com.onyx.igetshop"]="SAFE|Shopping app - bloatware|0"
292PACKAGE_INFO["com.onyx.clock"]="SAFE|Clock app - Android has built-in|128"
293PACKAGE_INFO["com.onyx.android.production.test"]="SAFE|Test suite - not needed for users|0"
294
295# ===== CONDITIONAL - Safe if alternatives installed =====
296PACKAGE_INFO["com.onyx.kime"]="CONDITIONAL|ONYX Keyboard 1 - need alternative keyboard first|0"
297PACKAGE_INFO["com.onyx.latinime"]="CONDITIONAL|ONYX Keyboard 2 - need alternative keyboard first|0"
298PACKAGE_INFO["com.onyx"]="CONDITIONAL|BOOX Launcher - MUST have alternative launcher installed first!|477"
299PACKAGE_INFO["com.onyx.appmarket"]="CONDITIONAL|ONYX App Store - can use Play Store/F-Droid instead|0"
300PACKAGE_INFO["com.onyx.kreader"]="CONDITIONAL|BOOX Reader - if using KOReader or other alternatives|0"
301
302# ===== RISKY - Can cause issues, careful consideration needed =====
303PACKAGE_INFO["com.android.vending"]="RISKY|Google Play Store - need F-Droid or sideload apps|216"
304PACKAGE_INFO["com.google.android.gms"]="RISKY|Google Play Services - some apps depend on it|621"
305PACKAGE_INFO["org.chromium.chrome"]="RISKY|Chrome browser - need alternative browser|118"
306PACKAGE_INFO["com.google.android.tts"]="RISKY|Google Text-to-Speech - needed for TTS features|154"
307PACKAGE_INFO["com.google.android.syncadapters.calendar"]="RISKY|Google Calendar Sync - if using Google Calendar|161"
308PACKAGE_INFO["com.google.android.apps.restore"]="RISKY|Google Restore - needed for app backups|141"
309
310# ===== DANGEROUS - DO NOT REMOVE unless you know what you're doing =====
311PACKAGE_INFO["com.android.systemui"]="DANGEROUS|System UI - CRITICAL, will break device|0"
312PACKAGE_INFO["com.android.settings"]="DANGEROUS|Android Settings - CRITICAL, will break device|0"
313PACKAGE_INFO["com.onyx.android.onyxotaservice"]="DANGEROUS|OTA Update Service - prevents firmware updates if removed|0"
314
315# Default safe list (pre-selected)
316PACKAGES_TO_DISABLE=(
317	"com.onyx.mail"
318	"com.onyx.gallery"
319	"com.onyx.easytransfer"
320	"com.onyx.aiassistant"
321	"com.onyx.android.ksync"
322	"com.onyx.calculator"
323	"com.onyx.musicplayer"
324	"com.onyx.voicerecorder"
325	"com.onyx.dict"
326	"com.onyx.igetshop"
327	"com.onyx.clock"
328	"com.onyx.android.production.test"
329)
330
331echo -e "${GREEN}Package Categories:${NC}"
332echo -e "  ${GREEN}[SAFE]${NC}        - No dependencies, safe to remove"
333echo -e "  ${YELLOW}[CONDITIONAL]${NC} - Safe only if alternatives installed"
334echo -e "  ${RED}[RISKY]${NC}       - May break functionality, careful!"
335echo -e "  ${RED}[DANGEROUS]${NC}   - DO NOT REMOVE"
336echo
337
338# Show what's currently selected (safe packages by default)
339echo -e "${YELLOW}Currently selected packages (${#PACKAGES_TO_DISABLE[@]} packages):${NC}"
340for pkg in "${PACKAGES_TO_DISABLE[@]}"; do
341	IFS='|' read -r level desc mem <<< "${PACKAGE_INFO[$pkg]}"
342	COLOR="${GREEN}"
343	echo -e "  ${COLOR}[${level}]${NC} $pkg - $desc"
344done
345echo
346
347# Ask if user wants to customize selection
348read -p "Customize package selection? [y/N] " -n 1 -r
349echo
350if [[ $REPLY =~ ^[Yy]$ ]]; then
351	echo
352	echo -e "${BLUE}========================================${NC}"
353	echo -e "${BLUE}Available Packages${NC}"
354	echo -e "${BLUE}========================================${NC}"
355	echo
356
357	# Show all packages by category
358	echo -e "${GREEN}=== SAFE Packages ===${NC}"
359	for pkg in "${!PACKAGE_INFO[@]}"; do
360		IFS='|' read -r level desc mem <<< "${PACKAGE_INFO[$pkg]}"
361		if [ "$level" == "SAFE" ]; then
362			# Check if package exists on device
363			if adb shell pm list packages | grep -q "^package:$pkg$"; then
364				echo "  $pkg - $desc"
365			fi
366		fi
367	done
368	echo
369
370	echo -e "${YELLOW}=== CONDITIONAL Packages (need alternatives) ===${NC}"
371	for pkg in "${!PACKAGE_INFO[@]}"; do
372		IFS='|' read -r level desc mem <<< "${PACKAGE_INFO[$pkg]}"
373		if [ "$level" == "CONDITIONAL" ]; then
374			if adb shell pm list packages | grep -q "^package:$pkg$"; then
375				echo "  $pkg - $desc"
376			fi
377		fi
378	done
379	echo
380
381	echo -e "${RED}=== RISKY Packages (careful!) ===${NC}"
382	for pkg in "${!PACKAGE_INFO[@]}"; do
383		IFS='|' read -r level desc mem <<< "${PACKAGE_INFO[$pkg]}"
384		if [ "$level" == "RISKY" ]; then
385			if adb shell pm list packages | grep -q "^package:$pkg$"; then
386				echo "  $pkg - $desc"
387			fi
388		fi
389	done
390	echo
391
392	echo -e "${RED}=== DANGEROUS Packages (DO NOT REMOVE) ===${NC}"
393	for pkg in "${!PACKAGE_INFO[@]}"; do
394		IFS='|' read -r level desc mem <<< "${PACKAGE_INFO[$pkg]}"
395		if [ "$level" == "DANGEROUS" ]; then
396			if adb shell pm list packages | grep -q "^package:$pkg$"; then
397				echo "  $pkg - $desc"
398			fi
399		fi
400	done
401	echo
402
403	echo -e "${YELLOW}Choose a preset:${NC}"
404	echo "  1) Safe only (default, recommended)"
405	echo "  2) Safe + Keyboards (if you have alternative keyboard)"
406	echo "  3) Safe + Google services (degoogle - requires F-Droid)"
407	echo "  4) Safe + Launcher (ONLY if AIO Launcher is installed and set as default)"
408	echo "  5) Custom selection (advanced)"
409	echo
410	read -p "Select preset [1-5]: " -n 1 -r PRESET
411	echo
412	echo
413
414	case $PRESET in
415	2)
416		echo -e "${YELLOW}Selected: Safe + Keyboards${NC}"
417		# Check for alternative keyboard
418		if [ $HAS_ALT_KEYBOARD -eq 0 ]; then
419			echo -e "${RED}WARNING: No alternative keyboard detected!${NC}"
420			echo "Install Gboard, AnySoftKeyboard, or Hacker's Keyboard first."
421			read -p "Continue anyway? [y/N] " -n 1 -r
422			echo
423			if [[ ! $REPLY =~ ^[Yy]$ ]]; then
424				echo "Falling back to safe packages only."
425			else
426				PACKAGES_TO_DISABLE+=("com.onyx.kime" "com.onyx.latinime")
427			fi
428		else
429			PACKAGES_TO_DISABLE+=("com.onyx.kime" "com.onyx.latinime")
430			echo -e "${GREEN}Alternative keyboard detected - safe to remove ONYX keyboards${NC}"
431		fi
432		;;
433	3)
434		echo -e "${YELLOW}Selected: Safe + Google services (degoogle)${NC}"
435		echo -e "${RED}WARNING: This will remove Google Play Store!${NC}"
436		echo "Make sure F-Droid is installed for app management."
437		read -p "Continue with degoogle? [y/N] " -n 1 -r
438		echo
439		if [[ $REPLY =~ ^[Yy]$ ]]; then
440			PACKAGES_TO_DISABLE+=(
441				"com.android.vending"
442				"com.google.android.tts"
443				"com.google.android.syncadapters.calendar"
444				"com.google.android.apps.restore"
445				"org.chromium.chrome"
446			)
447			echo -e "${YELLOW}Added Google services to removal list${NC}"
448		fi
449		;;
450	4)
451		echo -e "${YELLOW}Selected: Safe + Launcher${NC}"
452		if [ $HAS_ALT_LAUNCHER -eq 0 ]; then
453			echo -e "${RED}ERROR: No alternative launcher detected!${NC}"
454			echo "Install AIO Launcher or another launcher first."
455			echo "Falling back to safe packages only."
456		else
457			echo -e "${GREEN}Alternative launcher detected: $LAUNCHER_PACKAGE${NC}"
458			read -p "Remove BOOX Launcher? [y/N] " -n 1 -r
459			echo
460			if [[ $REPLY =~ ^[Yy]$ ]]; then
461				PACKAGES_TO_DISABLE+=("com.onyx")
462				echo -e "${YELLOW}Added BOOX Launcher to removal list${NC}"
463			fi
464		fi
465		;;
466	5)
467		echo -e "${YELLOW}Custom selection${NC}"
468		echo "Enter package names to add (comma-separated), or press Enter to skip:"
469		read -r CUSTOM_PACKAGES
470		if [ -n "$CUSTOM_PACKAGES" ]; then
471			IFS=',' read -ra PKGS <<< "$CUSTOM_PACKAGES"
472			for pkg in "${PKGS[@]}"; do
473				pkg=$(echo "$pkg" | xargs) # trim whitespace
474				if [ -n "${PACKAGE_INFO[$pkg]}" ]; then
475					PACKAGES_TO_DISABLE+=("$pkg")
476					echo "  Added: $pkg"
477				else
478					echo "  Unknown package (skipped): $pkg"
479				fi
480			done
481		fi
482		;;
483	*)
484		echo -e "${GREEN}Using default safe packages${NC}"
485		;;
486	esac
487	echo
488fi
489
490# Final confirmation - show what will be disabled
491echo -e "${BLUE}========================================${NC}"
492echo -e "${BLUE}Final Package List${NC}"
493echo -e "${BLUE}========================================${NC}"
494echo
495echo -e "${YELLOW}Packages to uninstall (${#PACKAGES_TO_DISABLE[@]} total):${NC}"
496
497TOTAL_MEM_RECOVERY=0
498for pkg in "${PACKAGES_TO_DISABLE[@]}"; do
499	IFS='|' read -r level desc mem <<< "${PACKAGE_INFO[$pkg]}"
500
501	# Color based on safety level
502	case $level in
503		SAFE)        COLOR="${GREEN}" ;;
504		CONDITIONAL) COLOR="${YELLOW}" ;;
505		RISKY)       COLOR="${RED}" ;;
506		DANGEROUS)   COLOR="${RED}" ;;
507		*)           COLOR="${NC}" ;;
508	esac
509
510	if [ "$mem" -gt 0 ]; then
511		echo -e "  ${COLOR}[${level}]${NC} $pkg - $desc (${mem} MB)"
512		TOTAL_MEM_RECOVERY=$((TOTAL_MEM_RECOVERY + mem))
513	else
514		echo -e "  ${COLOR}[${level}]${NC} $pkg - $desc"
515	fi
516done
517
518if [ $TOTAL_MEM_RECOVERY -gt 0 ]; then
519	echo
520	echo -e "${GREEN}Estimated memory recovery: ~${TOTAL_MEM_RECOVERY} MB${NC}"
521fi
522echo
523
524# Final confirmation
525read -p "Continue with uninstalling these packages? [y/N] " -n 1 -r
526echo
527if [[ ! $REPLY =~ ^[Yy]$ ]]; then
528	echo -e "${YELLOW}Aborted by user.${NC}"
529	exit 0
530fi
531
532echo
533
534# Uninstall packages
535UNINSTALLED_COUNT=0
536SKIPPED_COUNT=0
537FAILED_COUNT=0
538
539# Cache package lists to avoid repeated ADB calls (much faster)
540echo -e "${YELLOW}Getting package lists from device...${NC}"
541ALL_PACKAGES=$(adb shell pm list packages)
542echo -e "${GREEN}✓ Package list cached${NC}"
543echo
544
545for pkg in "${PACKAGES_TO_DISABLE[@]}"; do
546	# Check if package exists (using cached list)
547	if ! echo "$ALL_PACKAGES" | grep -q "^package:$pkg$"; then
548		echo -e "${YELLOW}⊘ Skipping $pkg (not installed)${NC}"
549		SKIPPED_COUNT=$((SKIPPED_COUNT + 1))
550		continue
551	fi
552
553	# Uninstall package for user 0
554	echo -e "${YELLOW}Uninstalling: $pkg${NC}"
555	if adb shell pm uninstall --user 0 "$pkg" 2>&1 | grep -q "Success"; then
556		echo -e "${GREEN}✓ Successfully uninstalled: $pkg${NC}"
557		UNINSTALLED_COUNT=$((UNINSTALLED_COUNT + 1))
558	else
559		echo -e "${RED}✗ Failed to uninstall: $pkg${NC}"
560		FAILED_COUNT=$((FAILED_COUNT + 1))
561	fi
562done
563
564echo
565echo -e "${BLUE}========================================${NC}"
566echo -e "${BLUE}Summary${NC}"
567echo -e "${BLUE}========================================${NC}"
568echo "  Uninstalled: $UNINSTALLED_COUNT"
569echo "  Skipped: $SKIPPED_COUNT"
570echo "  Failed: $FAILED_COUNT"
571echo
572
573# Get memory stats after
574if [ $UNINSTALLED_COUNT -gt 0 ]; then
575	echo -e "${YELLOW}Memory After Debloating:${NC}"
576	MEM_FREE_AFTER=$(adb shell cat /proc/meminfo | grep MemFree | awk '{print $2}')
577	MEM_AVAILABLE_AFTER=$(adb shell cat /proc/meminfo | grep MemAvailable | awk '{print $2}')
578	echo "  Free RAM: $((MEM_FREE_AFTER / 1024)) MB (was $((MEM_FREE_BEFORE / 1024)) MB)"
579	echo "  Available RAM: $((MEM_AVAILABLE_AFTER / 1024)) MB (was $((MEM_AVAILABLE_BEFORE / 1024)) MB)"
580
581	# Calculate improvement
582	FREE_DIFF=$(((MEM_FREE_AFTER - MEM_FREE_BEFORE) / 1024))
583	AVAILABLE_DIFF=$(((MEM_AVAILABLE_AFTER - MEM_AVAILABLE_BEFORE) / 1024))
584
585	echo
586	if [ $FREE_DIFF -gt 0 ]; then
587		echo -e "${GREEN}Immediate improvement: +${FREE_DIFF} MB free${NC}"
588	fi
589	if [ $AVAILABLE_DIFF -gt 0 ]; then
590		echo -e "${GREEN}Available RAM improved: +${AVAILABLE_DIFF} MB${NC}"
591	fi
592	echo
593
594	# Suggest reboot
595	echo -e "${YELLOW}Recommendation:${NC}"
596	echo "  Reboot device to fully free up memory from disabled apps."
597	echo
598	read -p "Reboot device now? [y/N] " -n 1 -r
599	echo
600	if [[ $REPLY =~ ^[Yy]$ ]]; then
601		echo -e "${YELLOW}Rebooting device...${NC}"
602		adb reboot
603		echo -e "${GREEN}✓ Device rebooting. Wait ~30 seconds then check memory usage.${NC}"
604	else
605		echo -e "${YELLOW}Remember to reboot later to see full memory recovery.${NC}"
606	fi
607else
608	echo -e "${YELLOW}No packages were uninstalled. Nothing to do.${NC}"
609fi
610
611# =============================================================================
612# BOOX-Specific Optimizations
613# =============================================================================
614
615echo
616echo -e "${BLUE}========================================${NC}"
617echo -e "${BLUE}BOOX E-ink Optimizations${NC}"
618echo -e "${BLUE}========================================${NC}"
619echo
620
621# Apps that should NOT be frozen (need background activity)
622APPS_TO_UNFREEZE=(
623	"org.mozilla.thunderbird"              # Thunderbird (email)
624	"com.fsck.k9"                          # K-9 Mail
625	"com.google.android.gm"                # Gmail
626	"org.thoughtcrime.securesms"           # Signal (messaging)
627	"org.telegram.messenger"               # Telegram
628	"com.whatsapp"                         # WhatsApp
629	"io.homeassistant.companion.android"   # Home Assistant
630	"com.termux"                           # Termux (terminal)
631	"org.koreader.launcher.fdroid"         # KOReader (reading)
632	"org.koreader.launcher"                # KOReader (Play Store version)
633	"ru.execbit.aiolauncher"               # AIO Launcher
634	"app.organicmaps"                      # Organic Maps
635	"org.mozilla.firefox"                  # Firefox
636	"com.readwise.reader"                  # Readwise Reader
637	"com.amazon.kindle"                    # Kindle
638)
639
640echo -e "${YELLOW}Configuring app permissions for e-ink optimization...${NC}"
641echo
642
643CONFIGURED_COUNT=0
644for app in "${APPS_TO_UNFREEZE[@]}"; do
645	# Check if app is installed
646	if ! echo "$ALL_PACKAGES" | grep -q "^package:$app$"; then
647		continue
648	fi
649
650	APP_NAME=$(echo "$app" | rev | cut -d. -f1 | rev)
651	echo -e "${YELLOW}Configuring: $APP_NAME ($app)${NC}"
652
653	# Allow background activity (prevents BOOX from killing the app)
654	if adb shell cmd appops set "$app" RUN_IN_BACKGROUND allow 2>&1 | grep -qv "No operations"; then
655		echo -e "${GREEN}  ✓ Allowed background activity${NC}"
656	fi
657
658	# Disable battery optimization (Android standard)
659	# Note: BOOX doesn't fully support this, but we try anyway
660	adb shell dumpsys deviceidle whitelist +"$app" 2>/dev/null || true
661
662	CONFIGURED_COUNT=$((CONFIGURED_COUNT + 1))
663done
664
665if [ $CONFIGURED_COUNT -gt 0 ]; then
666	echo
667	echo -e "${GREEN}✓ Configured $CONFIGURED_COUNT apps for background activity${NC}"
668else
669	echo -e "${YELLOW}No apps found that need configuration${NC}"
670fi
671
672echo
673echo -e "${BLUE}========================================${NC}"
674echo -e "${BLUE}Manual E-ink Settings (Recommended)${NC}"
675echo -e "${BLUE}========================================${NC}"
676echo
677echo -e "${YELLOW}The following settings should be configured manually on your BOOX:${NC}"
678echo
679echo "1. App Freeze Settings (Critical!):"
680echo "   Settings → Apps → App Management Settings"
681echo "   For each important app (email, messaging, etc.):"
682echo "     - Tap the app"
683echo "     - Disable 'Freeze' or enable 'Stay active in background'"
684echo
685echo "2. E-ink Optimization Settings:"
686echo "   Settings → Apps → App Optimization"
687echo "   For each app, configure:"
688echo "     - Screen Refresh Mode:"
689echo "       • Speed Mode: For interactive apps (browsers, email, terminals)"
690echo "       • Balanced Mode: For general use"
691echo "       • Quality/HD Mode: For reading (Kindle, KOReader)"
692echo "     - Animation Optimization: Enable (reduces ghosting)"
693echo
694echo "3. Recommended App Settings:"
695echo "   - Kindle/Reading apps: Quality mode, no animation"
696echo "   - Email/Messaging: Speed mode, enable background activity"
697echo "   - Termux/Terminal: Speed mode, enable background activity"
698echo "   - KOReader: Speed mode, unfreeze, unrestricted battery"
699echo "   - Browsers: Speed mode"
700echo "   - Home Assistant: Speed mode, enable background activity"
701echo
702
703echo
704echo -e "${BLUE}========================================${NC}"
705echo -e "${GREEN}Done!${NC}"
706echo -e "${BLUE}========================================${NC}"
707echo
708echo "Next steps:"
709echo "  1. If you installed AIO Launcher, set it as default:"
710echo "     Home button → AIO Launcher → Always"
711echo
712echo "  2. To reinstall a package:"
713echo "     adb shell cmd package install-existing <package_name>"
714echo
715echo "  3. To see all installed packages:"
716echo "     adb shell pm list packages"
717echo
718echo "  4. To add more packages to debloat:"
719echo "     Edit ~/src/home/tools/boox-debloat.sh"
720echo "     Add packages to PACKAGES_TO_DISABLE array"
721echo