#!/bin/bash # ZenTap Android Emulator Launcher (Bash Version) # Simple script to launch ZenTap in Android emulator with resolution selection set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Script directory and project root SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Find Android SDK find_android_sdk() { local paths=( "$ANDROID_SDK_ROOT" "$ANDROID_HOME" "$HOME/Android/Sdk" "$HOME/Library/Android/sdk" "/opt/android-sdk" "/usr/local/android-sdk" ) for path in "${paths[@]}"; do if [[ -n "$path" && -d "$path" ]]; then echo "$path" return 0 fi done return 1 } # Print colored output print_status() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } # Check prerequisites check_prerequisites() { print_status "Checking prerequisites..." # Check Flutter if ! command -v flutter &> /dev/null; then print_error "Flutter not found in PATH" exit 1 fi # Check Android SDK if ! ANDROID_SDK=$(find_android_sdk); then print_error "Android SDK not found" print_error "Please install Android SDK and set ANDROID_SDK_ROOT or ANDROID_HOME" exit 1 fi # Check emulator EMULATOR_PATH="$ANDROID_SDK/emulator/emulator" if [[ ! -f "$EMULATOR_PATH" ]]; then print_error "Android emulator not found at $EMULATOR_PATH" exit 1 fi # Check ADB if ! command -v adb &> /dev/null; then print_error "ADB not found in PATH" exit 1 fi print_success "All prerequisites satisfied" } # Show resolution menu show_resolution_menu() { echo echo "=== ZenTap Android Emulator Launcher ===" echo echo "Select target resolution:" echo " 1) Phone Small (360x640)" echo " 2) Phone Medium (411x731)" echo " 3) Phone Large (414x896)" echo " 4) Tablet 7\" (600x960)" echo " 5) Tablet 10\" (800x1280)" echo " 6) Foldable (673x841)" echo " 7) Desktop (1920x1080)" echo " 8) Custom resolution" echo } # Get resolution choice get_resolution() { while true; do read -p "Enter choice (1-8): " choice case $choice in 1) WIDTH=360; HEIGHT=640; RESOLUTION_NAME="Phone_Small"; break ;; 2) WIDTH=411; HEIGHT=731; RESOLUTION_NAME="Phone_Medium"; break ;; 3) WIDTH=414; HEIGHT=896; RESOLUTION_NAME="Phone_Large"; break ;; 4) WIDTH=600; HEIGHT=960; RESOLUTION_NAME="Tablet_7"; break ;; 5) WIDTH=800; HEIGHT=1280; RESOLUTION_NAME="Tablet_10"; break ;; 6) WIDTH=673; HEIGHT=841; RESOLUTION_NAME="Foldable"; break ;; 7) WIDTH=1920; HEIGHT=1080; RESOLUTION_NAME="Desktop"; break ;; 8) read -p "Enter width: " WIDTH read -p "Enter height: " HEIGHT RESOLUTION_NAME="Custom" if ! [[ "$WIDTH" =~ ^[0-9]+$ ]] || ! [[ "$HEIGHT" =~ ^[0-9]+$ ]]; then print_error "Invalid resolution values" continue fi break ;; *) print_error "Invalid choice" ;; esac done } # Get additional options get_options() { echo read -p "Cold boot (wipe data)? (y/N): " cold_boot [[ "$cold_boot" =~ ^[Yy] ]] && COLD_BOOT=true || COLD_BOOT=false read -p "Build mode (debug/release) [debug]: " build_mode BUILD_MODE="${build_mode:-debug}" read -p "Auto-launch app? (Y/n): " auto_launch [[ "$auto_launch" =~ ^[Nn] ]] && AUTO_LAUNCH=false || AUTO_LAUNCH=true } # List available AVDs list_avds() { print_status "Available AVDs:" "$EMULATOR_PATH" -list-avds | while read -r avd; do echo " - $avd" done } # Create AVD if it doesn't exist ensure_avd() { local avd_name="$1" if "$EMULATOR_PATH" -list-avds | grep -q "^$avd_name$"; then print_status "Using existing AVD: $avd_name" return 0 fi print_status "Creating new AVD: $avd_name" # Try to find avdmanager local avdmanager_paths=( "$ANDROID_SDK/cmdline-tools/latest/bin/avdmanager" "$ANDROID_SDK/tools/bin/avdmanager" ) local avdmanager="" for path in "${avdmanager_paths[@]}"; do if [[ -f "$path" ]]; then avdmanager="$path" break fi done if [[ -z "$avdmanager" ]]; then print_warning "avdmanager not found, using existing AVD" local existing_avds=($("$EMULATOR_PATH" -list-avds)) if [[ ${#existing_avds[@]} -gt 0 ]]; then AVD_NAME="${existing_avds[0]}" print_status "Using first available AVD: $AVD_NAME" return 0 else print_error "No AVDs available and cannot create new one" return 1 fi fi # Create AVD echo "no" | "$avdmanager" create avd \ -n "$avd_name" \ -k "system-images;android-34;google_apis;x86_64" \ -d "pixel" \ --force >/dev/null 2>&1 || { print_warning "Failed to create AVD, will use existing one if available" return 1 } print_success "AVD created: $avd_name" } # Launch emulator launch_emulator() { local avd_name="$1" print_status "Starting emulator: $avd_name" print_status "Resolution: ${WIDTH}x${HEIGHT}" local cmd=("$EMULATOR_PATH" -avd "$avd_name") # Add resolution cmd+=(-skin "${WIDTH}x${HEIGHT}") # Add performance options cmd+=(-gpu host -no-boot-anim -memory 2048) # Add cold boot if requested if [[ "$COLD_BOOT" == "true" ]]; then cmd+=(-wipe-data) print_status "Cold boot enabled (wiping data)" fi print_status "Emulator command: ${cmd[*]}" # Start emulator in background "${cmd[@]}" &>/dev/null & local emulator_pid=$! # Wait a bit for emulator to start sleep 5 # Check if emulator process is still running if ! kill -0 $emulator_pid 2>/dev/null; then print_error "Failed to start emulator" return 1 fi print_success "Emulator started (PID: $emulator_pid)" return 0 } # Wait for emulator to boot wait_for_emulator() { print_status "Waiting for emulator to boot..." local max_wait=120 local elapsed=0 while [[ $elapsed -lt $max_wait ]]; do if adb shell getprop sys.boot_completed 2>/dev/null | grep -q "1"; then print_success "Emulator is ready!" return 0 fi echo -n "." sleep 2 ((elapsed += 2)) done echo print_error "Timeout waiting for emulator to boot" return 1 } # Build app build_app() { print_status "Building app in $BUILD_MODE mode..." cd "$PROJECT_ROOT" local build_cmd=(flutter build apk) if [[ "$BUILD_MODE" == "debug" ]]; then build_cmd+=(--debug) elif [[ "$BUILD_MODE" == "release" ]]; then build_cmd+=(--release) fi if "${build_cmd[@]}"; then print_success "Build completed successfully" return 0 else print_error "Build failed" return 1 fi } # Install app install_app() { print_status "Installing app on emulator..." local apk_path if [[ "$BUILD_MODE" == "debug" ]]; then apk_path="$PROJECT_ROOT/build/app/outputs/flutter-apk/app-debug.apk" else apk_path="$PROJECT_ROOT/build/app/outputs/flutter-apk/app-release.apk" fi if [[ ! -f "$apk_path" ]]; then print_error "APK not found: $apk_path" return 1 fi if adb install -r "$apk_path"; then print_success "App installed successfully" return 0 else print_error "Installation failed" return 1 fi } # Launch app launch_app() { print_status "Launching ZenTap..." if adb shell am start -n "hu.fsociety.zentap/hu.fsociety.zentap.MainActivity"; then print_success "App launched successfully!" return 0 else print_error "Failed to launch app" return 1 fi } # Main execution main() { # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in --list-avds|-l) check_prerequisites list_avds exit 0 ;; --width|-w) WIDTH="$2" shift 2 ;; --height) HEIGHT="$2" shift 2 ;; --avd|-a) AVD_NAME="$2" shift 2 ;; --cold-boot|-c) COLD_BOOT=true shift ;; --build-mode|-b) BUILD_MODE="$2" shift 2 ;; --no-launch) AUTO_LAUNCH=false shift ;; --help) echo "Usage: $0 [OPTIONS]" echo "Options:" echo " --list-avds, -l List available AVDs" echo " --width, -w WIDTH Set width" echo " --height HEIGHT Set height" echo " --avd, -a NAME Use specific AVD" echo " --cold-boot, -c Cold boot (wipe data)" echo " --build-mode, -b debug|release" echo " --no-launch Don't auto-launch app" echo " --help Show this help" exit 0 ;; *) print_error "Unknown option: $1" exit 1 ;; esac done # Check prerequisites check_prerequisites # Interactive mode if no resolution specified if [[ -z "$WIDTH" || -z "$HEIGHT" ]]; then show_resolution_menu get_resolution get_options fi # Set defaults AVD_NAME="${AVD_NAME:-ZenTap_${RESOLUTION_NAME}_${WIDTH}x${HEIGHT}}" BUILD_MODE="${BUILD_MODE:-debug}" AUTO_LAUNCH="${AUTO_LAUNCH:-true}" COLD_BOOT="${COLD_BOOT:-false}" # Create/ensure AVD exists ensure_avd "$AVD_NAME" # Launch emulator if ! launch_emulator "$AVD_NAME"; then exit 1 fi # Wait for emulator to boot if ! wait_for_emulator; then exit 1 fi # Build app if ! build_app; then exit 1 fi # Install app if ! install_app; then exit 1 fi # Launch app if requested if [[ "$AUTO_LAUNCH" == "true" ]]; then launch_app fi echo print_success "=== ZenTap is ready! ===" echo "Emulator: $AVD_NAME (${WIDTH}x${HEIGHT})" echo "Build mode: $BUILD_MODE" if [[ "$AUTO_LAUNCH" == "true" ]]; then echo "App: Launched automatically" else echo "App: Ready to launch manually" fi echo echo "Press Ctrl+C to stop the emulator when done." # Keep script running and handle cleanup trap 'echo; print_status "Stopping emulator..."; adb emu kill >/dev/null 2>&1; print_success "Emulator stopped."; exit 0' INT # Monitor emulator while true; do if ! adb devices | grep -q "emulator"; then print_warning "Emulator connection lost" break fi sleep 5 done } # Run main function main "$@"