# Watchdog and APK Updater Integration

## Problem Solved

This integration resolves the conflict between the WatchdogService and APKUpdater where:
- WatchdogService continuously monitors and restarts the app every 3 seconds
- APKUpdater needs to update and restart the app without interference
- Without coordination, the watchdog would interfere with the update process

## How It Works

### Automatic Coordination

1. **Before Installation**: All installation methods (`install()`, `rootInstall()`, `ownerInstall()`) automatically pause the watchdog
2. **During Installation**: The watchdog remains paused and won't interfere with the update process
3. **After Success**: The watchdog resumes automatically when the new app starts
4. **After Failure**: If installation fails, the watchdog resumes immediately to maintain protection

### Key Components

#### WatchdogService Enhancements
- **Pause/Resume Mechanism**: Can be paused and resumed via broadcast intents
- **Persistent State**: Remembers pause state across app restarts using SharedPreferences
- **Smart Resumption**: Automatically resumes on app restart after successful updates

#### APKUpdater Integration
- **Automatic Pause**: All install methods pause watchdog before starting
- **Error Handling**: Resumes watchdog if installation fails
- **Manual Control**: New API methods for manual pause/resume

#### Recovery Mechanisms
- **Boot Recovery**: On device boot, watchdog resumes automatically
- **Package Replacement**: After successful update, watchdog resumes via broadcast receiver

### Usage

#### Automatic (Recommended)
```javascript
// The watchdog is automatically managed during these operations:
await ApkUpdater.install();       // Pauses watchdog, installs, resumes on success
await ApkUpdater.rootInstall();   // Same behavior
await ApkUpdater.ownerInstall();  // Same behavior
```

#### Manual Control (Advanced)
```javascript
// For custom update flows or debugging:
await ApkUpdater.pauseWatchdog();   // Pause watchdog manually
// ... your custom update logic ...
await ApkUpdater.resumeWatchdog();  // Resume watchdog manually
```

### Technical Details

#### Communication Method
- Uses Android broadcast intents for service communication
- Actions: `io.kiotplugins.PAUSE_WATCHDOG` and `io.kiotplugins.RESUME_WATCHDOG`

#### State Persistence
- Uses SharedPreferences to remember pause state
- Key: `watchdog_prefs.is_paused`
- Ensures proper recovery after unexpected shutdowns

#### Notification Updates
- Watchdog notification changes to indicate pause state
- "Watchdog Paused - APK update in progress" during updates
- "Watchdog Service Running - Home Console is protected" when active

### Benefits

1. **Seamless Updates**: No interference between watchdog and update process
2. **Automatic Recovery**: Smart resumption ensures continuous protection
3. **Error Resilience**: Watchdog resumes even if updates fail
4. **Transparent Operation**: Existing code works without changes
5. **Manual Override**: Advanced users can control timing manually

### Compatibility

- Works with all installation methods (regular, root, device owner)
- Compatible with existing APK updater workflows
- Maintains backward compatibility with existing code
