<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <!-- BLE scanning and connecting — API ≥31 uses granular permissions;
         older releases need BLUETOOTH + ACCESS_FINE_LOCATION for scanning.
         neverForLocation avoids the location permission dialog on API 31+. -->
    <uses-permission android:name="android.permission.BLUETOOTH"
        android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
        android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
        android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN"
        android:usesPermissionFlags="neverForLocation"
        tools:targetApi="s" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

    <!-- BLE hardware — required=false so non-BT boxes still install. -->
    <uses-feature
        android:name="android.hardware.bluetooth_le"
        android:required="false" />

    <!-- Network access for WLED HTTP/UDP, web UI, MQTT -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <!-- MediaProjection requires a foreground service -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />

    <!-- POST_NOTIFICATIONS for Android 13+ foreground service notification -->
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

    <!-- Autostart on boot — BootReceiver spawns CaptureService in root
         mode so capture resumes without the user touching the remote. -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <!-- Exempt from Doze/App Standby so the FG service isn't killed
         overnight on phones; essentially a no-op on TV boxes. -->
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
    <!-- Optional wake lock for sustained-performance boxes that aggressively
         sleep the CPU with the display off. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!-- Android TV declarations -->
    <uses-feature
        android:name="android.software.leanback"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />

    <!-- USB host — for USB-to-TTL adapters driving Adalight/AmbiLED
         controllers. required=false so phones without USB host still install. -->
    <uses-feature
        android:name="android.hardware.usb.host"
        android:required="false" />

    <application
        android:name=".LedGrabApp"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:banner="@drawable/ic_launcher"
        android:networkSecurityConfig="@xml/network_security_config"
        android:theme="@style/Theme.LedGrab">

        <!-- TV launcher activity -->
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Foreground service for screen capture + Python server -->
        <service
            android:name=".CaptureService"
            android:foregroundServiceType="mediaProjection"
            android:exported="false" />

        <!-- Autostart — fires on device boot (and package replace).
             On rooted devices, launches CaptureService directly so capture
             resumes without the user tapping Start. Unrooted devices are
             no-op because MediaProjection consent cannot be bypassed. -->
        <receiver
            android:name=".BootReceiver"
            android:exported="true"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
