From b41387dcb5242dd94bd21f164bdfb35139134b75 Mon Sep 17 00:00:00 2001 From: Ida Delphine Date: Mon, 26 Oct 2026 12:53:45 -0700 Subject: [PATCH] add platform content actions --- .../newpipe/settings/ComposeContentHost.kt | 36 ++++++++ .../app/platform/AndroidContentActions.kt | 82 +++++++++++++++++++ .../net/newpipe/app/platform/ContentHost.kt | 16 ++++ .../newpipe/app/platform/ContentActions.kt | 41 ++++++++++ .../newpipe/app/platform/IOSContentActions.kt | 11 +++ .../newpipe/app/platform/JVMContentActions.kt | 11 +++ 6 files changed, 197 insertions(+) create mode 100644 app/src/main/java/org/schabi/newpipe/settings/ComposeContentHost.kt create mode 100644 shared/src/androidMain/kotlin/net/newpipe/app/platform/AndroidContentActions.kt create mode 100644 shared/src/iosMain/kotlin/net/newpipe/app/platform/IOSContentActions.kt create mode 100644 shared/src/jvmMain/kotlin/net/newpipe/app/platform/JVMContentActions.kt diff --git a/app/src/main/java/org/schabi/newpipe/settings/ComposeContentHost.kt b/app/src/main/java/org/schabi/newpipe/settings/ComposeContentHost.kt new file mode 100644 index 000000000..8462c43d0 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/settings/ComposeContentHost.kt @@ -0,0 +1,36 @@ +/* + * SPDX-FileCopyrightText: 2026 NewPipe e.V. + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package org.schabi.newpipe.settings + +import android.content.Context +import coil3.SingletonImageLoader +import net.newpipe.app.platform.ContentHost +import org.schabi.newpipe.DownloaderImpl +import org.schabi.newpipe.extractor.NewPipe +import org.schabi.newpipe.player.helper.PlayerHelper +import org.schabi.newpipe.util.Localization +import org.schabi.newpipe.util.image.ImageStrategy +import org.schabi.newpipe.util.image.PreferredImageQuality + +class ComposeContentHost(private val context: Context) : ContentHost { + + override fun applyImageQuality(quality: String) { + ImageStrategy.setPreferredImageQuality(PreferredImageQuality.fromPreferenceKey(context, quality)) + val loader = SingletonImageLoader.get(context) + loader.memoryCache?.clear() + loader.diskCache?.clear() + } + + override fun applyYoutubeRestrictedMode(enabled: Boolean) = DownloaderImpl.getInstance().updateYoutubeRestrictedModeCookies(enabled) + + override fun applyContentLocalization() { + NewPipe.setupLocalization( + Localization.getPreferredLocalization(context), + Localization.getPreferredContentCountry(context) + ) + PlayerHelper.resetFormat() + } +} diff --git a/shared/src/androidMain/kotlin/net/newpipe/app/platform/AndroidContentActions.kt b/shared/src/androidMain/kotlin/net/newpipe/app/platform/AndroidContentActions.kt new file mode 100644 index 000000000..b1e8305b3 --- /dev/null +++ b/shared/src/androidMain/kotlin/net/newpipe/app/platform/AndroidContentActions.kt @@ -0,0 +1,82 @@ +/* + * SPDX-FileCopyrightText: 2026 NewPipe e.V. + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package net.newpipe.app.platform + +import android.app.LocaleManager +import android.content.ActivityNotFoundException +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.os.Build +import android.provider.Settings as AndroidSettings +import androidx.appcompat.app.AppCompatDelegate +import androidx.core.os.LocaleListCompat +import co.touchlab.kermit.Logger +import net.newpipe.app.preferences.ContentPreferences +import org.koin.core.annotation.Singleton + +@Singleton(binds = [ContentActions::class]) +class AndroidContentActions(private val context: Context) : ContentActions { + + private val host: ContentHost? = loadHost(context) + + // Android 13 moved per-app languages into the system settings + override val usesSystemAppLanguageSettings: Boolean + get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU + + override fun systemAppLanguageName(): String? { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { + return null + } + val locales = context.getSystemService(LocaleManager::class.java).applicationLocales + return if (locales.isEmpty) null else locales[0].displayName + } + + override fun openSystemAppLanguageSettings() { + try { + context.startActivity( + Intent( + AndroidSettings.ACTION_APP_LOCALE_SETTINGS, + Uri.fromParts(SCHEME_PACKAGE, context.packageName, null) + ).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + ) + } catch (exception: ActivityNotFoundException) { + Logger.e(messageString = "No activity found for app language settings", throwable = exception) + } + } + + override fun applyAppLanguage(languageCode: String) = AppCompatDelegate.setApplicationLocales( + when (languageCode) { + ContentPreferences.DEFAULT_LOCALIZATION -> LocaleListCompat.getEmptyLocaleList() + else -> LocaleListCompat.forLanguageTags(languageCode) + } + ) + + override fun applyImageQuality(quality: String) { + host?.applyImageQuality(quality) + } + + override fun applyYoutubeRestrictedMode(enabled: Boolean) { + host?.applyYoutubeRestrictedMode(enabled) + } + + override fun applyContentLocalization() { + host?.applyContentLocalization() + } + + private fun loadHost(context: Context): ContentHost? = runCatching { + Class.forName(HOST_CLASS) + .getDeclaredConstructor(Context::class.java) + .newInstance(context) as ContentHost + }.onFailure { error -> + Logger.w(messageString = "No host content actions found, skipping host-side changes", throwable = error) + }.getOrNull() + + private companion object { + const val HOST_CLASS = "org.schabi.newpipe.settings.ComposeContentHost" + const val SCHEME_PACKAGE = "package" + } +} diff --git a/shared/src/androidMain/kotlin/net/newpipe/app/platform/ContentHost.kt b/shared/src/androidMain/kotlin/net/newpipe/app/platform/ContentHost.kt index 38452c7f0..7c22e6085 100644 --- a/shared/src/androidMain/kotlin/net/newpipe/app/platform/ContentHost.kt +++ b/shared/src/androidMain/kotlin/net/newpipe/app/platform/ContentHost.kt @@ -1,2 +1,18 @@ +/* + * SPDX-FileCopyrightText: 2026 NewPipe e.V. + * SPDX-License-Identifier: GPL-3.0-or-later + */ + package net.newpipe.app.platform +/** + * Hooks into the image, network and extractor helpers that still live in the current Android app + */ +interface ContentHost { + + fun applyImageQuality(quality: String) + + fun applyYoutubeRestrictedMode(enabled: Boolean) + + fun applyContentLocalization() +} diff --git a/shared/src/commonMain/kotlin/net/newpipe/app/platform/ContentActions.kt b/shared/src/commonMain/kotlin/net/newpipe/app/platform/ContentActions.kt index 38452c7f0..c8cd3bc6f 100644 --- a/shared/src/commonMain/kotlin/net/newpipe/app/platform/ContentActions.kt +++ b/shared/src/commonMain/kotlin/net/newpipe/app/platform/ContentActions.kt @@ -1,2 +1,43 @@ +/* + * SPDX-FileCopyrightText: 2026 NewPipe e.V. + * SPDX-License-Identifier: GPL-3.0-or-later + */ + package net.newpipe.app.platform +import co.touchlab.kermit.Logger + +interface ContentActions { + val usesSystemAppLanguageSettings: Boolean + + fun systemAppLanguageName(): String? + + fun openSystemAppLanguageSettings() + + fun applyAppLanguage(languageCode: String) + + fun applyImageQuality(quality: String) + + fun applyYoutubeRestrictedMode(enabled: Boolean) + + fun applyContentLocalization() +} + +internal object NoOpContentActions : ContentActions { + + override val usesSystemAppLanguageSettings = false + + override fun systemAppLanguageName(): String? = null + + override fun openSystemAppLanguageSettings() = logUnsupported("openSystemAppLanguageSettings") + + override fun applyAppLanguage(languageCode: String) = logUnsupported("applyAppLanguage") + + override fun applyImageQuality(quality: String) = logUnsupported("applyImageQuality") + + override fun applyYoutubeRestrictedMode(enabled: Boolean) = logUnsupported("applyYoutubeRestrictedMode") + + override fun applyContentLocalization() = logUnsupported("applyContentLocalization") + + private fun logUnsupported(action: String) = Logger.w(messageString = "Content action '$action' is unsupported on this platform") +} diff --git a/shared/src/iosMain/kotlin/net/newpipe/app/platform/IOSContentActions.kt b/shared/src/iosMain/kotlin/net/newpipe/app/platform/IOSContentActions.kt new file mode 100644 index 000000000..1925c4095 --- /dev/null +++ b/shared/src/iosMain/kotlin/net/newpipe/app/platform/IOSContentActions.kt @@ -0,0 +1,11 @@ +/* + * SPDX-FileCopyrightText: 2026 NewPipe e.V. + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package net.newpipe.app.platform + +import org.koin.core.annotation.Singleton + +@Singleton(binds = [ContentActions::class]) +class IOSContentActions : ContentActions by NoOpContentActions diff --git a/shared/src/jvmMain/kotlin/net/newpipe/app/platform/JVMContentActions.kt b/shared/src/jvmMain/kotlin/net/newpipe/app/platform/JVMContentActions.kt new file mode 100644 index 000000000..f677c4f01 --- /dev/null +++ b/shared/src/jvmMain/kotlin/net/newpipe/app/platform/JVMContentActions.kt @@ -0,0 +1,11 @@ +/* + * SPDX-FileCopyrightText: 2026 NewPipe e.V. + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package net.newpipe.app.platform + +import org.koin.core.annotation.Singleton + +@Singleton(binds = [ContentActions::class]) +class JVMContentActions : ContentActions by NoOpContentActions