# AUTO-START CONFIGURATION GUIDE

## ANDROID SETUP

### Step 1: Update AndroidManifest.xml
File: `android/app/src/main/AndroidManifest.xml`

Add these permissions and receiver inside the `<manifest>` tag:

```xml
<!-- Permissions -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- Inside <application> tag -->
<application ...>
  <!-- Existing activities -->
  <activity android:name=".MainActivity" ...>
    ...
  </activity>

  <!-- Boot receiver for auto-start -->
  <receiver android:name="com.dexterous.workmanager.WorkmanagerBroadcastReceiver"
    android:exported="true"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED" />
      <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
  </receiver>
</application>
```

### Step 2: Update build.gradle
File: `android/app/build.gradle.kts` (or `build.gradle`)

Ensure `compileSdk` is at least 31:
```gradle
android {
    compileSdk 33  // or higher
    ...
}
```

### Step 3: Battery Optimization Exemption (Optional)
Users may need to manually exempt the app from battery optimization for reliable background syncing:
- Settings → Battery → Battery Optimization → Select "autobackup" → Don't optimize

---

## WINDOWS SETUP

### Step 1: Scheduled Task (via Registry/Script)
File: `windows/runner/main.cpp` or create a helper script

To auto-start on Windows, we'll use Windows Task Scheduler.

**Option A: Batch Script**
Create `windows/autostart_setup.bat`:
```batch
@echo off
REM Add app to Windows startup folder
set APP_PATH=%cd%\build\windows\x64\runner\Release\autobackup.exe
set STARTUP_FOLDER=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup

REM Create shortcut (requires VBScript helper)
powershell -Command "$WshShell = New-Object -Com WScript.Shell; $Shortcut = $WshShell.CreateShortcut('%STARTUP_FOLDER%\AutoBackup.lnk'); $Shortcut.TargetPath = '%APP_PATH%'; $Shortcut.Save()"

echo Auto-start setup completed!
```

**Option B: Registry (Direct)**
Run as Administrator:
```cmd
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run" /v "AutoBackup" /t REG_SZ /d "C:\path\to\autobackup.exe" /f
```

### Step 2: Run After Build (Automatic)
Add to `pubspec.yaml` scripts or CI/CD to auto-register on Windows.

---

## LINUX SETUP

### Step 1: Desktop Entry (Autostart)
Create: `~/.config/autostart/com.autobackup.desktop`

```ini
[Desktop Entry]
Type=Application
Exec=/opt/autobackup/autobackup
Name=AutoBackup
Comment=Auto-sync files via SSH
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
```

Or for system-wide:
Create: `/etc/xdg/autostart/com.autobackup.desktop` (requires sudo)

### Step 2: Systemd Service (Optional - Better for servers)
Create: `~/.config/systemd/user/autobackup.service`

```ini
[Unit]
Description=AutoBackup Sync Service
After=network.target

[Service]
Type=simple
ExecStart=/opt/autobackup/autobackup
Restart=on-failure
RestartSec=10s

[Install]
WantedBy=default.target
```

Enable and start:
```bash
systemctl --user daemon-reload
systemctl --user enable autobackup.service
systemctl --user start autobackup.service
```

---

## TESTING AUTO-START

### Android
1. Build APK: `flutter build apk`
2. Install: `adb install build/app/outputs/flutter-apk/app-release.apk`
3. Reboot device
4. Check Logs: `adb logcat | grep autobackup`

### Windows
1. Build: `flutter build windows`
2. Test shortcut in Startup folder manually
3. Reboot and check if app launches

### Linux
1. Build: `flutter build linux`
2. Test: Run the desktop entry
3. Reboot and verify in system logs: `journalctl -u autobackup.service`

---

## TROUBLESHOOTING

**App doesn't auto-start after OS reboot?**
- ✓ Check permissions are granted in Android settings
- ✓ Verify auto-upload is enabled in the app
- ✓ Check app is not in battery optimization exclusion list
- ✓ Ensure network is available (background sync won't run on WiFi-only without network)

**Background sync not running?**
- Check logs: Settings → Logs (in-app)
- Background tasks only run when:
  - Auto-upload is ENABLED
  - Device has network connectivity
  - Sync day/time matches current day and hour
  - Device hasn't already synced today
