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

    <!-- Android TV Integration -->
    <uses-permission android:name="com.android.providers.tv.permission.WRITE_EPG_DATA" />

    <!-- Generic permissions -->
    <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"
        tools:ignore="LeanbackUsesWifi" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <!-- Search permissions (voice to text) -->
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    <!-- Device feature requirements -->
    <uses-feature
        android:name="android.software.leanback"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.microphone"
        android:required="false" />

    <queries>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data
                android:host="youtube.com"
                android:scheme="https" />
        </intent>
    </queries>

    <application
        android:name=".JellyfinApplication"
        android:allowBackup="true"
        android:appCategory="video"
        android:banner="@drawable/app_banner"
        android:dataExtractionRules="@xml/backup_rules"
        android:fullBackupContent="@xml/backup_content"
        android:icon="@mipmap/app_icon"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/Theme.Jellyfin"
        android:usesCleartextTraffic="true"
        android:networkSecurityConfig="@xml/network_security_config">

        <!-- Legacy service required for AccountManagerMigration -->
        <service
            android:name=".auth.service.AuthenticatorService"
            android:exported="false">
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>
            <meta-data
                android:name="android.accounts.AccountAuthenticator"
                android:resource="@xml/authenticator" />
        </service>

        <!-- Screensaver -->
        <service
            android:name=".integration.dream.LibraryDreamService"
            android:exported="true"
            android:permission="android.permission.BIND_DREAM_SERVICE">

            <intent-filter>
                <action android:name="android.service.dreams.DreamService" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>

        <provider
            android:name="androidx.startup.InitializationProvider"
            android:authorities="${applicationId}.androidx-startup"
            android:exported="false"
            android:initOrder="1000"
            tools:node="merge">
            <meta-data
                android:name="org.jellyfin.androidtv.LogInitializer"
                android:value="androidx.startup" />
            <meta-data
                android:name="org.jellyfin.androidtv.di.KoinInitializer"
                android:value="androidx.startup" />
            <meta-data
                android:name="org.jellyfin.androidtv.SessionInitializer"
                android:value="androidx.startup" />
        </provider>

        <provider
            android:name=".integration.MediaContentProvider"
            android:authorities="${applicationId}.content"
            android:exported="true"
            android:initOrder="10" />

        <provider
            android:name=".integration.provider.ImageProvider"
            android:authorities="${applicationId}.integration.provider.ImageProvider"
            android:exported="true"
            android:initOrder="10" />

        <!-- Authentication -->
        <activity
            android:name=".ui.startup.StartupActivity"
            android:exported="true"
            android:launchMode="singleTask"
            android:noHistory="true"
            android:screenOrientation="landscape"
            android:windowSoftInputMode="adjustNothing">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.SEARCH" />
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
        <activity-alias
            android:name=".startup.StartupActivity"
            android:targetActivity=".ui.startup.StartupActivity" />

        <!-- Main application -->
        <activity
            android:name=".ui.browsing.MainActivity"
            android:launchMode="singleTask"
            android:screenOrientation="landscape"
            android:windowSoftInputMode="adjustNothing" />

        <activity
            android:name=".ui.preference.PreferencesActivity"
            android:screenOrientation="behind"
            android:theme="@style/Theme.Jellyfin.Preferences" />

        <activity android:name=".ui.playback.ExternalPlayerActivity" />
    </application>
</manifest>
