# Android App Blocker — backend integration

Your existing Capacitor setup (see `android-plugin/` in the main project) already includes:
- `AppBlockerAccessibilityService.java` — intercepts foreground app changes
- `AppBlockerPlugin.java` — Capacitor bridge
- Accessibility config + overlay layout

The blocker today reads its blocklist from local storage. To sync with the new PHP backend, do this on the JS side (inside the Capacitor WebView):

## 1. On app start — pull server config

```typescript
import { api } from '@/integrations/api/client';

async function syncBlockerOnStartup() {
  if (!api.token) return;
  const apps = await api.get('/blocker/apps');
  // Persist for the native plugin to read
  await Preferences.set({ key: 'blocked_apps', value: JSON.stringify(apps) });
  // Tell the native side to refresh
  await AppBlocker.refreshBlocklist();
}
```

## 2. On user changes — push to server

When the user toggles an app in the LifeOS UI:

```typescript
await api.post('/blocker/apps', {
  package_name: 'com.instagram.android',
  app_label: 'Instagram',
  is_blocked: true,
  daily_limit_minutes: 30,
});
```

## 3. Bulk sync after first install / re-install

```typescript
const installedApps = await AppBlocker.getInstalledApps(); // existing native call
await api.post('/blocker/sync', { apps: installedApps });
```

## 4. Native side — minimal changes

The Java plugin doesn't need to change at all. It still reads `blocked_apps` from `SharedPreferences` / Capacitor `Preferences`. The JS layer is the only one that talks to the new backend.

## 5. APK build

After Phase 2 is deployed:
```bash
git pull
npm install
npm run build
npx cap sync android
npx cap open android   # opens Android Studio → Build → Generate Signed Bundle/APK
```

## 6. Permissions reminder

Don't forget to keep these in `AndroidManifest.xml`:
- `android.permission.PACKAGE_USAGE_STATS` (Usage Stats)
- `android.permission.SYSTEM_ALERT_WINDOW` (overlay)
- `android.permission.BIND_ACCESSIBILITY_SERVICE` (on the service tag)
- `android.permission.INTERNET` (now required for backend sync)

## 7. Offline mode

The native blocker continues to work offline using its cached `SharedPreferences` blocklist. Sync just refreshes the cache when online.
