add platform content actions

This commit is contained in:
Ida Delphine authored and Aayush Gupta committed 2026-11-09 13:13:35 +08:00
1 parent 79385cead2
commit b41387dcb5
6 files changed
+197

No files matched your search

@@ -0,0 +1,36 @@
/*
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
* 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()
}
}
@@ -0,0 +1,82 @@
/*
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
* 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"
}
}
@@ -1,2 +1,18 @@
/*
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
* 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()
}
@@ -1,2 +1,43 @@
/*
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
* 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")
}
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
* 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
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
* 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