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

    <!-- Internet permissions for Nostr relay connections -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    <!-- Bluetooth permissions -->
    <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.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    
    <!-- Location permission required for BLE scanning -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    
    <!-- Notification permissions -->
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <!-- Signature permission for internal UI shutdown broadcasts -->
    <uses-permission android:name="com.bitchat.android.permission.FORCE_FINISH" />
    <!-- Foreground service and boot permissions for long-running background mesh -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <!-- Connected device foreground service type for BLE operations (API 34+) -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />
    <!-- Data sync foreground service type (required when declaring dataSync) -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
    <!-- Location foreground service type required for BLE scanning in background -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    <!-- Microphone for voice notes -->
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <!-- Camera for QR verification -->
    <uses-permission android:name="android.permission.CAMERA" />

    <!-- Storage permissions for file sharing -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

    <!-- Haptic feedback permission -->
    <uses-permission android:name="android.permission.VIBRATE" />
    
    <!-- Battery optimization permission -->
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
    
    <!-- Hardware features -->
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
    <uses-feature android:name="android.hardware.bluetooth" android:required="true" />
    <uses-feature android:name="android.hardware.camera" android:required="false" />

    <permission
        android:name="com.bitchat.android.permission.FORCE_FINISH"
        android:protectionLevel="signature" />

    <application
        android:name=".BitchatApplication"
        android:allowBackup="false"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.BitchatAndroid"
        tools:targetApi="31">
        <!-- FileProvider for sharing temp/cache files with external viewers -->
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
        <activity
            android:name=".ui.GeohashPickerActivity"
            android:exported="false"
            android:theme="@style/Theme.BitchatAndroid"
            android:windowSoftInputMode="adjustResize" />
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.BitchatAndroid"
            android:windowSoftInputMode="adjustResize"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="bitchat" android:host="verify" />
            </intent-filter>
        </activity>

        <!-- Persistent foreground service to run the mesh in background -->
        <service
            android:name=".service.MeshForegroundService"
            android:exported="false"
            android:foregroundServiceType="connectedDevice|dataSync|location"
            tools:ignore="DataExtractionRules">
        </service>

        <!-- Listen for in-app broadcast when POST_NOTIFICATIONS is granted -->
        <receiver
            android:name=".service.NotificationPermissionChangedReceiver"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="com.bitchat.android.action.NOTIFICATION_PERMISSION_GRANTED" />
            </intent-filter>
        </receiver>

        <!-- Auto-start mesh service after boot if enabled -->
        <receiver
            android:name=".service.BootCompletedReceiver"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>
