Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

3.0 KB
Raw Blame
import { AndroidConfig, type ConfigPlugin, withAndroidManifest } from "expo/config-plugins";

const NOTIFICATIONS_SERVICE = "space.pibot.android.PhoneNotifications";
const LINK_SERVICE = "space.pibot.android.RemoteLinkService";

/**
 * Config plugin of the Android local module. The module's own manifest declares nothing;
 * READ_SMS (the opt-in fallback for phones without Google services) is added here only when
 * `readSms` is true. A Google Play build passes false: Play restricts SMS permissions to default
 * SMS apps, and even an unused declaration needs a policy exception. `expo prebuild` regenerates
 * the native project from this, so nothing is edited by hand.
 * READ_CALL_LOG (also Play-restricted) and the notification listener service share the same
 * sideload-only boundary; without them the phone reports both capabilities unsupported.
 * Amendment (2026-11-11): the remote-control foreground service (type connectedDevice) is
 * declared in every build. Play accepts the type with a Console declaration (a link to the user's
 * own paired computer); its prerequisite CHANGE_WIFI_MULTICAST_STATE is in app.config.ts.
 */
export const withPibotAndroid: ConfigPlugin<{ readSms: boolean }> = (config, { readSms }) =>
  withAndroidManifest(config, (next) => {
    const manifest = next.modResults;
    const permissions = ["android.permission.READ_SMS", "android.permission.READ_CALL_LOG"];
    for (const permission of [
      "android.permission.FOREGROUND_SERVICE",
      "android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE",
      "android.permission.WAKE_LOCK",
    ])
      AndroidConfig.Permissions.addPermission(manifest, permission);
    if (readSms)
      for (const permission of permissions)
        AndroidConfig.Permissions.addPermission(manifest, permission);
    else AndroidConfig.Permissions.removePermissions(manifest, permissions);
    const app = AndroidConfig.Manifest.getMainApplicationOrThrow(manifest);
    const services = (app.service ?? []).filter(
      (service) =>
        service.$["android:name"] !== NOTIFICATIONS_SERVICE &&
        service.$["android:name"] !== LINK_SERVICE,
    );
    services.push({
      $: {
        "android:name": LINK_SERVICE,
        "android:exported": "false",
        "android:foregroundServiceType": "connectedDevice",
        "android:stopWithTask": "true",
      },
    } as (typeof services)[number]);
    // BIND_NOTIFICATION_LISTENER_SERVICE guards the binding (only the system holds it); it is not a
    // permission the app requests. Access is granted by the owner in system Settings.
    if (readSms)
      services.push({
        $: {
          "android:name": NOTIFICATIONS_SERVICE,
          "android:permission": "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE",
          "android:exported": "true",
        },
        "intent-filter": [
          {
            action: [
              { $: { "android:name": "android.service.notification.NotificationListenerService" } },
            ],
          },
        ],
      });
    app.service = services;
    return next;
  });