Re: PSA: How to archive your EXACT homescreen(s) apps to Windows for perfect cross-platform backup/restore sans the net
Maria Sophia <[email protected]>
| Newsgroups | comp.mobile.android,alt.comp.microsoft.windows,alt.comp.os.windows-10 |
|---|---|
| Organization | BWH Usenet Archive (https://usenet.blueworldhosting.com) |
| Message-ID | <[email protected]> |
Well, a few tests later, where each test uncovers a different bug, here's
the current version which seems to be working to back up the entire phone
to a duplicate hierarchy on the desktop.
I'm only testing on the following:
Android 13, Samsung Galaxy A32-5G, non rooted, Windows 10
Note every folder has a "base.apk" inside plus splits (if included).
I'm not sure yet how to rename the base.apk to the application name.
Also, it would be nice to figure out how to get the version number.
# -------------------------------------------------------------------
# apkhome.py
# Parse nova.db, build homescreen folder/app skeleton and pull APKs
# python apkhome.py
# -------------------------------------------------------------------
# v1p6 20260819 Added optional cleanup prompt for existing ./apkhome directory
# v1p5 20260819 Added custom fallback path for scrcpy's bundled adb.exe
# v1p4 20260819 Added log file output (apkhome.log) alongside console
# v1p3 20260819 Added Ctrl+C (KeyboardInterrupt) handling & cleanup
# v1p2 20260819 Added interactive debug mode toggle for dry runs
# v1p1 20260819 Added automated ADB split/monolithic APK pulling
# v1p0 20260819 Combined SQL parsing and direct directory tree generation
# -------------------------------------------------------------------
# Reads nova.db, re-creates the homescreen folder and package hierarchy,
# checks for a connected ADB device, runs adb shell pm path <package>
# to dynamically locate and pull component files (base.apk plus splits)
# feeding results into the newly created matching homescreen hierarchy.
# -------------------------------------------------------------------
# WIP:
# Need to consider how to rename the base.apk to the package name
# Need to consider how to add the version number to the package name
# Need to consider how to zip up splits
# -------------------------------------------------------------------
import os
import re
import shutil
import sqlite3
import subprocess
db_file = "nova.db"
base_dir = "./apkhome"
log_filename = "apkhome.log"
# Explicit fallback path for ADB based on a typical scrcpy setup
CUSTOM_ADB_PATH = r"C:\app\editor\android\scrcpy\adb.exe"
if not os.path.exists(db_file):
print(f"Error: {db_file} not found. Please place it in the working directory.")
exit(1)
# Open log file for writing
log_file = open(log_filename, "w", encoding="utf-8")
def log_print(message):
"""Prints to console and writes to the log file simultaneously."""
print(message)
log_file.write(message + "\n")
log_file.flush()
# Helper to find the correct adb executable command/path
def get_adb_binary():
# 1. Try global 'adb' command first
try:
subprocess.run(["adb", "version"], capture_output=True, text=True, check=True)
return "adb"
except (subprocess.SubprocessError, FileNotFoundError):
pass
# 2. Try the custom scrcpy path fallback
if os.path.exists(CUSTOM_ADB_PATH):
return CUSTOM_ADB_PATH
return None
# --- SETUP & MODES PROMPT ---
log_print("--- Setup Mode ---")
mode_choice = input("Run in [D]ebug mode (mock files, no phone needed) or [L]ive mode (real ADB pull)? [d/l]: ").strip().lower()
log_file.write(f"User selected mode choice: {mode_choice}\n")
DEBUG_MODE = mode_choice.startswith('d')
# --- EXISTING DIRECTORY HANDLING ---
if os.path.exists(base_dir):
clean_choice = input(f"Existing '{base_dir}' folder detected. [C]lean/wipe it completely or [K]eep existing files? [c/k]: ").strip().lower()
log_file.write(f"User selected directory cleanup choice: {clean_choice}\n")
if clean_choice.startswith('c'):
try:
shutil.rmtree(base_dir)
log_print(f"[i] Successfully wiped existing '{base_dir}' directory.")
except Exception as e:
log_print(f"[!] Warning: Failed to fully remove '{base_dir}': {e}")
else:
log_print(f"[i] Keeping existing '{base_dir}' directory (merging/updating content).")
adb_binary = None
adb_active = False
if DEBUG_MODE:
log_print("\n[!] Running in DEBUG mode. ADB will be bypassed, and dummy APKs will be created.")
else:
log_print("\n[i] Running in LIVE mode. Locating ADB binary...")
adb_binary = get_adb_binary()
if not adb_binary:
log_print(f"Warning: 'adb' not found in PATH or at custom path ({CUSTOM_ADB_PATH}). Skipping APK pulling.")
adb_active = False
else:
log_print(f"Using ADB binary at: {adb_binary}")
try:
adb_check = subprocess.run([adb_binary, "devices"], capture_output=True, text=True, check=True)
devices = [line for line in adb_check.stdout.splitlines() if "\tdevice" in line]
if not devices:
log_print("Warning: No active ADB devices found. Directory skeleton will be built, but APK pulling will be skipped.")
adb_active = False
else:
log_print(f"ADB device detected: {devices[0].split()[0]}")
adb_active = True
except (subprocess.SubprocessError, FileNotFoundError):
log_print("Warning: Failed to execute ADB. Skipping APK pulling.")
adb_active = False
log_print(f"\nConnecting to {db_file} and processing layout...")
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
try:
# 1. Fetch distinct screen indices associated with desktop items
cursor.execute(
"SELECT DISTINCT screen FROM favorites WHERE container = -100 ORDER BY screen ASC;"
)
screens = cursor.fetchall()
for (screen_num,) in screens:
current_panel = f"panel_{screen_num}"
log_print(f"\n--- Processing: {current_panel} ---")
# 2. Find folders located directly on this specific screen (ignoring blank titles)
cursor.execute(
"SELECT _id, title FROM favorites WHERE container = -100 AND screen = ?"
" AND intent IS NULL AND title IS NOT NULL AND TRIM(title) != '';",
(screen_num,),
)
folders = cursor.fetchall()
for folder_id, folder_name in folders:
clean_folder_name = re.sub(r'[\\/*?:"<>|]', "", folder_name).strip()
if not clean_folder_name:
clean_folder_name = "unnamed_folder"
cursor.execute(
"SELECT title, intent FROM favorites WHERE container = ?;",
(folder_id,),
)
items = cursor.fetchall()
if items:
for app_title, intent in items:
pkg = "Unknown"
if intent and "component=" in intent:
try:
part = intent.split("component=")[1]
pkg = part.split("/")[0]
except IndexError:
pkg = intent
target_path = os.path.join(
base_dir, current_panel, clean_folder_name, pkg
)
os.makedirs(target_path, exist_ok=True)
log_print(f" [Folder] {clean_folder_name} -> App: {pkg}")
# Pull APK or generate mock file depending on mode
if DEBUG_MODE and pkg != "Unknown":
dummy_apk_path = os.path.join(target_path, "base.apk")
with open(dummy_apk_path, "w") as f:
f.write("mock apk content")
log_print(f" [Mock ADB] Created test file at {dummy_apk_path}")
elif adb_active and pkg != "Unknown":
path_cmd = subprocess.run(
[adb_binary, "shell", "pm", "path", pkg],
capture_output=True, text=True
)
pulled_any = False
for line in path_cmd.stdout.splitlines():
if line.startswith("package:"):
remote_path = line.replace("package:", "").strip()
if remote_path:
pulled_any = True
subprocess.run(
[adb_binary, "pull", remote_path, target_path],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
if not pulled_any:
log_print(f" [ADB] Warning: Could not find path for package (may be uninstalled): {pkg}")
else:
target_path = os.path.join(base_dir, current_panel, clean_folder_name)
os.makedirs(target_path, exist_ok=True)
# 3. Find loose/top-level apps sitting directly on this screen
cursor.execute(
"SELECT title, intent FROM favorites WHERE container = -100 AND screen = ?"
" AND intent IS NOT NULL;",
(screen_num,),
)
loose_apps = cursor.fetchall()
if loose_apps:
for app_title, intent in loose_apps:
pkg = "Unknown"
if intent and "component=" in intent:
try:
part = intent.split("component=")[1]
pkg = part.split("/")[0]
except IndexError:
pkg = intent
target_path = os.path.join(
base_dir, current_panel, "loose_apps", pkg
)
os.makedirs(target_path, exist_ok=True)
log_print(f" [Loose App] {pkg}")
if DEBUG_MODE and pkg != "Unknown":
dummy_apk_path = os.path.join(target_path, "base.apk")
with open(dummy_apk_path, "w") as f:
f.write("mock apk content")
log_print(f" [Mock ADB] Created test file at {dummy_apk_path}")
elif adb_active and pkg != "Unknown":
path_cmd = subprocess.run(
[adb_binary, "shell", "pm", "path", pkg],
capture_output=True, text=True
)
pulled_any = False
for line in path_cmd.stdout.splitlines():
if line.startswith("package:"):
remote_path = line.replace("package:", "").strip()
if remote_path:
pulled_any = True
subprocess.run(
[adb_binary, "pull", remote_path, target_path],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
if not pulled_any:
log_print(f" [ADB] Warning: Could not find path for package (may be uninstalled): {pkg}")
log_print("\nArchive layout processing complete!")
except KeyboardInterrupt:
log_print("\n\n[!] Operation cancelled by user (Ctrl+C). Exiting safely...")
finally:
# Ensure database connection and log file are cleanly closed
conn.close()
log_file.close()
# end of apkhome.py
--