<?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-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.wifi" android:required="false" />
    <uses-feature android:name="android.hardware.location.gps" android:required="false" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!-- Needed since Android 10 to read the Wi-Fi network name for the network pill. Never requested at
         runtime: build-and-install grants it with pm grant, so lint's Android 12 "ask for COARSE with
         FINE" rule does not apply here. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
        tools:ignore="CoarseFineLocation" />
    <!-- Puts the remote's accessibility helpers back after a force stop of this app took them off the enabled
         list (ServiceRepair). Granted with pm grant by build-and-install when a helper is enabled. -->
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"
        tools:ignore="ProtectedPermissions" />
    <!-- YaOS (Yandex TV OS): HDMI, AV and the tuner all play in com.yandex.tv.live, whose activities require
         this permission (protection level normal, so it is granted at install). Ignored on other TVs. -->
    <uses-permission android:name="yandex.tv.permission.ACCESS_TV_SOURCE" />

    <!-- Android 11+ package visibility: lets the launcher list the TV apps it shows as tiles, and the
         apps that declare a module card (docs/specs/card.md). -->
    <queries>
        <!-- The standard settings handlers, so package visibility doesn't hide them and send the pills to a vendor fallback by mistake. -->
        <intent>
            <action android:name="android.settings.SETTINGS" />
        </intent>
        <intent>
            <action android:name="android.settings.WIFI_SETTINGS" />
        </intent>
        <intent>
            <action android:name="android.settings.DISPLAY_SETTINGS" />
        </intent>
        <intent>
            <action android:name="android.settings.MANAGE_APPLICATIONS_SETTINGS" />
        </intent>
        <!-- The tile menu and the app library offer App info only where this resolves (SystemSettings.appInfo). -->
        <intent>
            <action android:name="android.settings.APPLICATION_DETAILS_SETTINGS" />
            <data android:scheme="package" />
        </intent>
        <intent>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent>
        <intent>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
        </intent>
        <intent>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="com.gathin.tvhome.category.DOCK_CARD" />
        </intent>
        <!-- stock_home_redirect: HomeKeyService looks up the stock launcher's HOME activity. -->
        <intent>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
        </intent>
        <!-- China-market MIUI TV firmware: the Settings and Wi-Fi pills fall back to Xiaomi's settings app.
             Fire TV: the real settings menu lives in the stock launcher (see settings_fallback_components). -->
        <package android:name="com.xiaomi.mitv.settings" />
        <package android:name="com.tcl.settings" />
        <package android:name="com.amazon.tv.launcher" />
        <!-- Coocaa OS sources for the Inputs menu (coocaa_sources); see CoocaaSources. -->
        <provider android:authorities="com.skyworth.tv.provider" />
        <provider android:authorities="com.skyworth.tv.sdk.provider" />
    </queries>

    <application
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:banner="@drawable/banner"
        android:theme="@style/Theme.TvHome"
        android:allowBackup="false"
        android:localeConfig="@xml/locales_config">

        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTask"
            android:stateNotNeeded="true"
            android:excludeFromRecents="true"
            android:clearTaskOnLaunch="true"
            android:configChanges="orientation|screenSize|keyboard|keyboardHidden|uiMode"
            android:screenOrientation="landscape">
            <!-- CVTE firmware (ro.CVT_EN_COMMON_HOME_INTENT=1) sends the Home key and boot as MAIN + HOME +
                 COMMON_HOME, which only matches a filter that lists both (#1201). In this one filter, not a second
                 one: a plain HOME intent still matches it, and a second filter could list this activity twice. -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.COMMON_HOME" />
                <category android:name="android.intent.category.DEFAULT" />
                <!-- ColorOS TV: boot only promotes a HOME that declares this category.
                     docs/devices/oppo-k9-55-oppocnm632.md (prior art: aerial-tv oppo-coloros-tv.md). -->
                <category android:name="com.heytap.tv.launcher.category.MODERN_HOME" />
            </intent-filter>
            <!-- Listed in the TV's own app list, so it can be opened from the stock home screen too. -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".LauncherSettingsActivity"
            android:exported="false"
            android:excludeFromRecents="true"
            android:screenOrientation="landscape" />
        <!-- The system input switcher (com.android.tv.action.VIEW_INPUTS), which is what the remote's Input
             button becomes through the TV's Live TV app or InputKeyService. Disabled here and switched on by
             MainActivity only while handle_view_inputs is true (config.xml or a theme), for sets whose stock
             launcher was the only handler of the action (Hisense huangshan, Android TV 10): kept off, a TV with
             its own input picker never sees a second candidate and a chooser. -->
        <activity
            android:name=".InputsRequestActivity"
            android:exported="true"
            android:enabled="false"
            android:excludeFromRecents="true"
            android:noHistory="true"
            android:theme="@android:style/Theme.NoDisplay">
            <intent-filter>
                <action android:name="com.android.tv.action.VIEW_INPUTS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <!-- Remote Settings button panel (settings_key_panel); own task so the app behind stays put.
             No screenOrientation: a translucent activity may not request one on Android 8.0 (API 26),
             which throws IllegalStateException in onCreate (#1122); a TV is landscape anyway. -->
        <activity
            android:name=".QuickSettingsActivity"
            android:exported="false"
            android:taskAffinity=""
            android:launchMode="singleTop"
            android:excludeFromRecents="true"
            android:noHistory="true"
            android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
        <activity
            android:name=".HomeSettingsActivity"
            android:exported="false"
            android:taskAffinity=""
            android:excludeFromRecents="true"
            android:theme="@style/Theme.TvHome" />
        <!-- Home button for remotes that send KEYCODE_ALL_APPS (Xiaomi). Opt-in: HOME_KEY_SERVICE=1
             build-and-install.sh; see HomeKeyService. -->
        <service
            android:name=".HomeKeyService"
            android:label="@string/home_key_name"
            android:directBootAware="true"
            android:exported="true"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>
            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/home_key_service" />
        </service>

        <!-- market://launch?id=<package>: package-name app launches from the Android TV Remote service
             (Home Assistant android_tv_remote), normally served by the stock launcher. -->
        <activity
            android:name=".AppLaunchActivity"
            android:exported="true"
            android:excludeFromRecents="true"
            android:noHistory="true"
            android:theme="@android:style/Theme.NoDisplay">
            <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="market" android:host="launch" />
            </intent-filter>
        </activity>

        <!-- The same clock, started by Home itself after home_idle_clock_minutes without input. -->
        <activity
            android:name=".ClockActivity"
            android:exported="false"
            android:excludeFromRecents="true"
            android:noHistory="true"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" />

        <!-- stock_home_redirect at boot: HomeKeyService opens this invisible activity once, so the screen behind
             it sends a window event when it closes (HomeKeyService.probeAtBoot). -->
        <activity
            android:name=".ProbeActivity"
            android:exported="false"
            android:excludeFromRecents="true"
            android:noHistory="true"
            android:taskAffinity=""
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

        <!-- Screensaver: pick it in Settings, or `settings put secure screensaver_components
             com.gathin.tvhome/.ClockDream` over adb. -->
        <service
            android:name=".ClockDream"
            android:label="@string/dream_name"
            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>

        <!-- tools/theme.sh writes the theme through this where the shell can't reach Android/data;
             see ThemeProvider. WRITE_SECURE_SETTINGS: the adb shell has it, ordinary apps don't. -->
        <provider
            android:name=".ThemeProvider"
            android:authorities="${applicationId}.theme"
            android:exported="true"
            android:permission="android.permission.WRITE_SECURE_SETTINGS" />

        <!-- The remote's Input button (KEYCODE_TV or KEYCODE_TV_INPUT) opens the input switcher, and input_key_keycodes and
         app_keys (config.xml or a theme) bring back other remote buttons; see InputKeyService. -->
        <!-- Bound again by the system after a force stop, which restarts Home on firmware that kills
             third-party apps (TCL tc8000_ay30a2). It reads no notification.
             cmd notification allow_listener com.gathin.tvhome/.GuardRevival -->
        <service
            android:name=".GuardRevival"
            android:directBootAware="true"
            android:exported="true"
            android:label="@string/home_key_name"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>
        <receiver
            android:name=".GuardConfig"
            android:exported="true"
            android:permission="android.permission.WRITE_SECURE_SETTINGS">
            <intent-filter>
                <action android:name="com.gathin.tvhome.GUARD" />
            </intent-filter>
        </receiver>

        <service
            android:name=".InputKeyService"
            android:exported="false"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>
            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/input_key_service" />
        </service>
    </application>
</manifest>
