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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- Needed to hand a downloaded APK to the system installer (in-app updates). -->
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <!-- Lets a self-update apply without a confirmation screen once Memby is its own
         installer of record (Android 12+). Ignored before that, and never required: the
         install session falls back to asking. -->
    <uses-permission android:name="android.permission.UPDATE_PACKAGES_WITHOUT_USER_ACTION" />

    <!-- Package visibility (Android 11+). Without these, resolveActivity() returns null and
         "Play in Emby" / "Screensaver settings" silently do nothing. -->
    <queries>
        <!-- EmbyAppLauncher hands an item to an installed Emby client. -->
        <package android:name="com.mb.android" />
        <package android:name="tv.emby.embyatv" />
        <!-- The per-app "install unknown apps" screen. Many TV builds do not implement it;
             declaring it here is what lets the updater tell the difference between "the
             viewer has not granted this yet" and "this TV has nowhere to grant it". -->
        <intent>
            <action android:name="android.settings.MANAGE_UNKNOWN_APP_SOURCES" />
        </intent>
        <!-- openScreensaverSettings probes the TV's own settings screens, best first. -->
        <intent>
            <action android:name="android.settings.DREAM_SETTINGS" />
        </intent>
        <intent>
            <action android:name="android.settings.DISPLAY_SETTINGS" />
        </intent>
        <intent>
            <action android:name="android.settings.SETTINGS" />
        </intent>
        <!-- Search's microphone button. Without this, SpeechRecognizer.isRecognitionAvailable
             reports false on Android 11+ even where a recogniser exists, and the button
             would be hidden on devices that support voice perfectly well. -->
        <intent>
            <action android:name="android.speech.RecognitionService" />
        </intent>
    </queries>

    <!-- This is a TV app: no touchscreen, uses the Leanback launcher. -->
    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />
    <uses-feature
        android:name="android.software.leanback"
        android:required="true" />

    <application
        android:name=".MembyApp"
        android:allowBackup="true"
        android:banner="@drawable/app_banner"
        android:icon="@drawable/emby_logo"
        android:label="@string/app_name"
        android:roundIcon="@drawable/emby_logo"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/Theme.Memby">
        <profileable android:shell="true" tools:targetApi="q" />

        <!-- Home / setup screen. Registered on the TV (Leanback) launcher.
             Televisions are fixed landscape, so the DiscouragedApi advice about adapting to
             other orientations does not apply to any device that can install this app. -->
        <activity
            android:name=".ui.MainActivity"
            android:exported="true"
            android:screenOrientation="landscape"
            tools:ignore="DiscouragedApi"
            android:configChanges="keyboard|keyboardHidden|navigation|screenSize|smallestScreenSize|screenLayout|orientation|uiMode">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- In-app preview of the screensaver (same UI the Daydream uses). -->
        <activity
            android:name=".ui.screensaver.ScreensaverActivity"
            android:exported="false"
            android:screenOrientation="landscape"
            android:theme="@style/Theme.Memby.Fullscreen"
            tools:ignore="DiscouragedApi" />

        <!-- Re-enter the normal app lifecycle after replacement. This preserves the
             stored profile/session and performs the next update check before login. -->
        <receiver
            android:name=".update.UpdateRecoveryReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
            </intent-filter>
        </receiver>

        <!-- Fullscreen Media3 player. -->
        <activity
            android:name=".ui.player.PlayerActivity"
            android:exported="false"
            android:screenOrientation="landscape"
            android:launchMode="singleTask"
            android:configChanges="keyboard|keyboardHidden|navigation|screenSize|orientation"
            android:theme="@style/Theme.Memby.Fullscreen"
            tools:ignore="DiscouragedApi" />

        <!-- The system screensaver (Daydream / Ambient mode source).
             Interactive: select to open the panel, play, or favourite. -->
        <service
            android:name=".screensaver.MembyDreamService"
            android:exported="true"
            android:icon="@drawable/emby_logo"
            android:label="@string/screensaver_name"
            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>
            <meta-data
                android:name="android.service.dream"
                android:resource="@xml/emby_dream" />
        </service>

        <!-- Outcome of a self-update's install session. Not exported: the only sender is
             the system, through a PendingIntent this app created. -->
        <receiver
            android:name=".update.InstallResultReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.ponzischeme89.memby.INSTALL_RESULT" />
            </intent-filter>
        </receiver>

    </application>
</manifest>
