<?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">

    <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.software.leanback"
        android:required="false" />

    <uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED" />
    <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_EXTERNAL_STORAGE"
        android:maxSdkVersion="32" />
    <!--
    Scoped storage on Android 10 (API 29) is inconvenient because users need to confirm edition on each individual file.
    So we request `WRITE_EXTERNAL_STORAGE` until Android 10 (API 29), and enable `requestLegacyExternalStorage`
    -->
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="29"
        tools:ignore="ScopedStorage" />

    <!-- to access media with original metadata with scoped storage (API >=29) -->
    <uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
    <!-- to provide a foreground service type, as required from Android 14 (API 34) -->
    <uses-permission
        android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"
        android:maxSdkVersion="34"
        tools:ignore="SystemPermissionTypo" />
    <uses-permission
        android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROCESSING"
        tools:ignore="SystemPermissionTypo" />
    <!-- TODO TLAD still needed to fetch map tiles / reverse geocoding / else ? check in release mode -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- from Android 12 (API 31), users can optionally grant access to the media management special permission -->
    <uses-permission
        android:name="android.permission.MANAGE_MEDIA"
        tools:ignore="ProtectedPermissions" />
    <!-- to show analysis service progress via notification -->
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <!-- to change wallpaper -->
    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    <!-- TODO TLAD remove this permission when this is fixed: https://github.com/flutter/flutter/issues/42451 -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-permission
        android:name="com.android.launcher.permission.INSTALL_SHORTCUT"
        android:maxSdkVersion="25" />

    <!-- additional permissions added by plugins:

    `{package}.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION` by `androidx.appcompat:appcompat`

    `android.permission.ACCESS_NETWORK_STATE`,
    `android.permission.FOREGROUND_SERVICE`,
    `android.permission.RECEIVE_BOOT_COMPLETED`,
    `android.permission.WAKE_LOCK` by `androidx.work:work-runtime-ktx`, to analyze media in a service

    `android.permission.ACCESS_NETWORK_STATE` by `dev.fluttercommunity.plus.connectivity`, TODO TLAD still needed?

    `android.permission.USE_BIOMETRIC` by `io.flutter.plugins.localauth`, to unlock vaults (API >=28)
    -->

    <!--
    allow install on API 21, despite the `minSdk` declared in dependencies:
    - FFmpegKit for Flutter is from API 24 (when not LTS)
    -->
    <uses-sdk tools:overrideLibrary="com.arthenica.ffmpegkit.flutter" />

    <!-- from Android 11, we should define <queries> to make other apps visible to this app -->
    <queries>
        <intent>
            <action android:name="android.intent.action.MAIN" />
        </intent>
        <!-- necessary to resolve image editor apps that are not visible in the launcher -->
        <intent>
            <action android:name="android.intent.action.EDIT" />
            <data android:mimeType="image/*" />
        </intent>
        <!-- necessary to resolve video editor apps that are not visible in the launcher -->
        <intent>
            <action android:name="android.intent.action.EDIT" />
            <data android:mimeType="video/*" />
        </intent>
        <!--
        from Android 11, `url_launcher` method `canLaunchUrl()` will return false,
        if appropriate intents are not declared, cf https://pub.dev/packages/url_launcher#configuration=
        -->
        <!-- to open https URLs -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" />
        </intent>
    </queries>

    <!--
    as of Flutter v3.22.2, predictive back gesture does not work
    as expected when extending `FlutterFragmentActivity`
    so we disable `enableOnBackInvokedCallback`
    -->
    <application
        android:allowBackup="true"
        android:appCategory="image"
        android:banner="@drawable/banner"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:enableOnBackInvokedCallback="false"
        android:fullBackupContent="@xml/full_backup_content"
        android:fullBackupOnly="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:showWhenLocked="true"
        android:supportsRtl="true"
        tools:targetApi="tiramisu">
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:exported="true"
            android:hardwareAccelerated="true"
            android:launchMode="singleTop"
            android:supportsPictureInPicture="true"
            android:theme="@style/NormalTheme"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter tools:ignore="AppLinkUrlError">
                <action android:name="android.intent.action.GET_CONTENT" />
                <action android:name="android.intent.action.PICK" />
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.provider.action.REVIEW" />
                <action android:name="android.provider.action.REVIEW_SECURE" />
                <action android:name="com.android.camera.action.REVIEW" />
                <action android:name="com.android.camera.action.SPLIT_SCREEN_REVIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.OPENABLE" />

                <data android:mimeType="image/*" />
                <data android:mimeType="video/*" />
                <data android:mimeType="vnd.android.cursor.dir/image" />
                <data android:mimeType="vnd.android.cursor.dir/video" />

                <!-- no specified scheme -->
            </intent-filter>
            <intent-filter tools:ignore="AppLinkUrlError">
                <action android:name="android.intent.action.GET_CONTENT" />
                <action android:name="android.intent.action.PICK" />
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.provider.action.REVIEW" />
                <action android:name="android.provider.action.REVIEW_SECURE" />
                <action android:name="com.android.camera.action.REVIEW" />
                <action android:name="com.android.camera.action.SPLIT_SCREEN_REVIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.OPENABLE" />

                <data android:mimeType="image/*" />
                <data android:mimeType="video/*" />
                <data android:mimeType="vnd.android.cursor.dir/image" />
                <data android:mimeType="vnd.android.cursor.dir/video" />

                <data android:scheme="content" />
                <data android:scheme="file" />
            </intent-filter>
            <!--
            <intent-filter>
                <action android:name="android.intent.action.EDIT" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="image/*" />
                <data android:mimeType="vnd.android.cursor.dir/image" />
            </intent-filter>
            -->
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
                android:name="io.flutter.embedding.android.NormalTheme"
                android:resource="@style/NormalTheme" />
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

        <activity
            android:name=".ScreenSaverSettingsActivity"
            android:exported="true"
            android:theme="@style/NormalTheme" />

        <activity
            android:name=".WallpaperActivity"
            android:exported="true"
            android:label="@string/wallpaper"
            android:theme="@style/NormalTheme">
            <intent-filter>
                <action android:name="android.intent.action.ATTACH_DATA" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="image/*" />
                <data android:mimeType="video/*" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.SET_WALLPAPER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <!-- exported for Android API 19 launcher to access this activity -->
        <activity
            android:name=".HomeWidgetSettingsActivity"
            android:exported="true"
            android:theme="@style/NormalTheme">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".HomeWidgetProvider"
            android:exported="false"
            android:label="@string/app_widget_label">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/app_widget_info" />
        </receiver>

        <receiver
            android:name="androidx.media.session.MediaButtonReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>
        </receiver>

        <!--
        anonymous service for analysis worker is specified here to provide service type:
        - `dataSync` for Android 14 (API 34)
        - `mediaProcessing` from Android 15 (API 35)
        -->
        <service
            android:name="androidx.work.impl.foreground.SystemForegroundService"
            android:foregroundServiceType="dataSync|mediaProcessing"
            tools:node="merge" />

        <service
            android:name=".MediaPlaybackService"
            android:exported="false"
            android:foregroundServiceType="mediaPlayback">
            <intent-filter>
                <action android:name="android.media.browse.MediaBrowserService" />
            </intent-filter>
        </service>

        <service
            android:name=".ScreenSaverService"
            android:exported="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_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/screen_saver" />
        </service>

        <!-- file provider to share files having a file:// URI -->
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.file_provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

        <provider
            android:name=".SearchSuggestionsProvider"
            android:authorities="@string/search_provider"
            android:exported="true"
            tools:ignore="ExportedContentProvider" />

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="${googleApiKey}" />
        <meta-data
            android:name="firebase_crashlytics_collection_enabled"
            android:value="false" />
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
        <!-- as of Flutter v3.22.0 (stable),
        Impeller fails to render videos (via `ffmpegkit`), and has random glitches -->
        <meta-data
            android:name="io.flutter.embedding.android.EnableImpeller"
            android:value="false" />
    </application>
</manifest>
