mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2026-11-23 15:47:20 +00:00
add backup platform actions
This commit is contained in:
1 parent
19e43ffa22
commit
6a6b8d1f3c
6 files changed
+412
No files matched your search
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* 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 android.content.Intent
|
||||
import androidx.core.content.edit
|
||||
import androidx.core.net.toUri
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.work.Constraints
|
||||
import androidx.work.ExistingWorkPolicy
|
||||
import androidx.work.NetworkType
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.OutOfQuotaPolicy
|
||||
import androidx.work.WorkManager
|
||||
import com.jakewharton.processphoenix.ProcessPhoenix
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import net.newpipe.app.model.BackupFile
|
||||
import net.newpipe.app.model.DataExportResult
|
||||
import net.newpipe.app.model.DataImportResult
|
||||
import net.newpipe.app.model.ErrorReport
|
||||
import net.newpipe.app.model.SettingsFormat
|
||||
import net.newpipe.app.platform.BackupRestoreHost
|
||||
import org.schabi.newpipe.NewPipeDatabase
|
||||
import org.schabi.newpipe.R
|
||||
import org.schabi.newpipe.error.ErrorInfo
|
||||
import org.schabi.newpipe.error.ErrorUtil
|
||||
import org.schabi.newpipe.error.UserAction
|
||||
import org.schabi.newpipe.local.subscription.SubscriptionFragment.Companion.JSON_MIME_TYPE
|
||||
import org.schabi.newpipe.local.subscription.workers.SubscriptionExportWorker
|
||||
import org.schabi.newpipe.local.subscription.workers.SubscriptionImportInput
|
||||
import org.schabi.newpipe.local.subscription.workers.SubscriptionImportWorker
|
||||
import org.schabi.newpipe.settings.export.BackupFileLocator
|
||||
import org.schabi.newpipe.settings.export.ImportExportManager
|
||||
import org.schabi.newpipe.streams.io.StoredFileHelper
|
||||
import org.schabi.newpipe.util.ZipHelper
|
||||
|
||||
/**
|
||||
* Host-side backup and restore for the Compose backup and restore settings screen
|
||||
*/
|
||||
@Suppress("unused")
|
||||
class ComposeBackupRestoreHost(private val context: Context) : BackupRestoreHost {
|
||||
|
||||
private val manager = ImportExportManager(BackupFileLocator(context))
|
||||
private val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
private val lastBackupFileKey = context.getString(R.string.import_export_data_path)
|
||||
|
||||
override fun createFilePickerIntent(file: BackupFile): Intent = when (file) {
|
||||
BackupFile.DATA_IMPORT -> {
|
||||
StoredFileHelper.getPicker(context, ZIP_MIME_TYPE, lastBackupFile())
|
||||
}
|
||||
|
||||
BackupFile.DATA_EXPORT -> {
|
||||
val name = "NewPipeData-${timestamp(DATA_DATE_PATTERN, Locale.US)}.zip"
|
||||
StoredFileHelper.getNewPicker(context, name, ZIP_MIME_TYPE, lastBackupFile())
|
||||
}
|
||||
|
||||
BackupFile.SUBSCRIPTIONS_IMPORT -> {
|
||||
StoredFileHelper.getPicker(context, JSON_MIME_TYPE)
|
||||
}
|
||||
|
||||
BackupFile.SUBSCRIPTIONS_EXPORT -> {
|
||||
val name = "newpipe_subscriptions_${timestamp(SUBSCRIPTIONS_DATE_PATTERN, Locale.ENGLISH)}.json"
|
||||
StoredFileHelper.getNewPicker(context, name, JSON_MIME_TYPE, null)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun exportData(file: String): DataExportResult = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
NewPipeDatabase.checkpoint()
|
||||
manager.exportDatabase(preferences, zipFile(file))
|
||||
// Remembered only on success, so the next picker opens where it worked
|
||||
saveLastBackupFile(file)
|
||||
DataExportResult.Success
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (error: Exception) {
|
||||
DataExportResult.Failed(errorReport(error, REQUEST_EXPORT))
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION") // Old backups can only be recognised through the deprecated format
|
||||
override suspend fun importDatabase(file: String): DataImportResult = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val backup = zipFile(file)
|
||||
if (!ZipHelper.isValidZipFile(backup)) {
|
||||
return@withContext DataImportResult.InvalidFile
|
||||
}
|
||||
manager.ensureDbDirectoryExists()
|
||||
val allFilesImported = manager.extractDb(backup)
|
||||
val settingsFormat = when {
|
||||
manager.exportHasJsonPrefs(backup) -> SettingsFormat.JSON
|
||||
manager.exportHasSerializedPrefs(backup) -> SettingsFormat.SERIALIZED
|
||||
else -> null
|
||||
}
|
||||
DataImportResult.Imported(allFilesImported, settingsFormat)
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (error: Exception) {
|
||||
DataImportResult.Failed(errorReport(error, REQUEST_IMPORT))
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION") // Old backups can only be read through the deprecated format
|
||||
override suspend fun importSettings(file: String, format: SettingsFormat) = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val backup = zipFile(file)
|
||||
when (format) {
|
||||
SettingsFormat.JSON -> manager.loadJsonPrefs(backup, preferences)
|
||||
SettingsFormat.SERIALIZED -> manager.loadSerializedPrefs(backup, preferences)
|
||||
}
|
||||
resetDeviceSpecificSettings()
|
||||
} catch (cancellation: CancellationException) {
|
||||
throw cancellation
|
||||
} catch (error: Exception) {
|
||||
ErrorUtil.createNotification(
|
||||
context,
|
||||
ErrorInfo(error, UserAction.DATABASE_IMPORT_EXPORT, REQUEST_IMPORT_SETTINGS)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun finishImport(file: String) {
|
||||
saveLastBackupFile(file)
|
||||
restartApp()
|
||||
}
|
||||
|
||||
override fun resetSettings() {
|
||||
preferences.edit { clear() }
|
||||
restartApp()
|
||||
}
|
||||
|
||||
override fun exportSubscriptions(file: String) = SubscriptionExportWorker.schedule(context, file.toUri())
|
||||
|
||||
override fun importSubscriptions(file: String) {
|
||||
val request = OneTimeWorkRequestBuilder<SubscriptionImportWorker>()
|
||||
.setInputData(SubscriptionImportInput.PreviousExportMode(file).toData())
|
||||
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
|
||||
.setConstraints(Constraints(requiredNetworkType = NetworkType.CONNECTED))
|
||||
.build()
|
||||
WorkManager.getInstance(context)
|
||||
.enqueueUniqueWork(SubscriptionImportWorker.WORK_NAME, ExistingWorkPolicy.APPEND_OR_REPLACE, request)
|
||||
}
|
||||
|
||||
/**
|
||||
* Media tunneling is switched off automatically on devices known to break it, so that choice
|
||||
* must not travel to another device with the imported settings
|
||||
*/
|
||||
private fun resetDeviceSpecificSettings() {
|
||||
val tunnelingKey = context.getString(R.string.disable_media_tunneling_key)
|
||||
val automaticTunnelingKey = context.getString(R.string.disabled_media_tunneling_automatically_key)
|
||||
val disabledAutomatically = preferences.getInt(automaticTunnelingKey, -1) == 1 &&
|
||||
preferences.getBoolean(tunnelingKey, false)
|
||||
if (disabledAutomatically) {
|
||||
preferences.edit {
|
||||
putInt(automaticTunnelingKey, -1)
|
||||
putBoolean(tunnelingKey, false)
|
||||
}
|
||||
NewPipeSettings.setMediaTunneling(context)
|
||||
}
|
||||
}
|
||||
|
||||
private fun restartApp() {
|
||||
NewPipeDatabase.close()
|
||||
ProcessPhoenix.triggerRebirth(context)
|
||||
}
|
||||
|
||||
private fun zipFile(file: String) = StoredFileHelper(context, file.toUri(), ZIP_MIME_TYPE)
|
||||
|
||||
private fun lastBackupFile() = preferences.getString(lastBackupFileKey, null)
|
||||
?.takeUnless { it.isBlank() }
|
||||
?.toUri()
|
||||
|
||||
private fun saveLastBackupFile(file: String) = preferences.edit { putString(lastBackupFileKey, file) }
|
||||
|
||||
private fun timestamp(pattern: String, locale: Locale) = SimpleDateFormat(pattern, locale).format(Date())
|
||||
|
||||
private fun errorReport(error: Exception, request: String): ErrorReport {
|
||||
val info = ErrorInfo(error, UserAction.DATABASE_IMPORT_EXPORT, request)
|
||||
return ErrorReport(
|
||||
message = info.getMessage(context).toString(),
|
||||
open = { ErrorUtil.openActivity(context, info) }
|
||||
)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val ZIP_MIME_TYPE = "application/zip"
|
||||
const val DATA_DATE_PATTERN = "yyyyMMdd_HHmmss"
|
||||
const val SUBSCRIPTIONS_DATE_PATTERN = "yyyyMMddHHmm"
|
||||
const val REQUEST_EXPORT = "Exporting database and settings"
|
||||
const val REQUEST_IMPORT = "Importing database and settings"
|
||||
const val REQUEST_IMPORT_SETTINGS = "Importing preferences"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.Activity
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberUpdatedState
|
||||
import co.touchlab.kermit.Logger
|
||||
import net.newpipe.app.model.BackupFile
|
||||
import net.newpipe.app.model.SettingsFormat
|
||||
import org.koin.core.annotation.Singleton
|
||||
|
||||
@Singleton(binds = [BackupRestoreActions::class])
|
||||
class AndroidBackupRestoreActions(context: Context) : BackupRestoreActions {
|
||||
|
||||
private val host: BackupRestoreHost? = loadHost(context)
|
||||
|
||||
@Composable
|
||||
override fun rememberFilePicker(file: BackupFile, onNoFileManager: () -> Unit, onPicked: (String) -> Unit): () -> Unit {
|
||||
val currentOnNoFileManager by rememberUpdatedState(onNoFileManager)
|
||||
val currentOnPicked by rememberUpdatedState(onPicked)
|
||||
|
||||
val launcher = rememberLauncherForActivityResult(StartActivityForResult()) { result ->
|
||||
val picked = result.data?.data
|
||||
if (result.resultCode == Activity.RESULT_OK && picked != null) {
|
||||
currentOnPicked(picked.toString())
|
||||
}
|
||||
}
|
||||
|
||||
return remember(launcher, file) {
|
||||
{
|
||||
val bridge = host
|
||||
if (bridge != null) {
|
||||
try {
|
||||
launcher.launch(bridge.createFilePickerIntent(file))
|
||||
} catch (exception: ActivityNotFoundException) {
|
||||
Logger.w(messageString = "Unable to launch the backup file picker", throwable = exception)
|
||||
currentOnNoFileManager()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun exportData(file: String) = host?.exportData(file) ?: NoOpBackupRestoreActions.exportData(file)
|
||||
|
||||
override suspend fun importDatabase(file: String) = host?.importDatabase(file) ?: NoOpBackupRestoreActions.importDatabase(file)
|
||||
|
||||
override suspend fun importSettings(file: String, format: SettingsFormat) {
|
||||
host?.importSettings(file, format)
|
||||
}
|
||||
|
||||
override fun finishImport(file: String) {
|
||||
host?.finishImport(file)
|
||||
}
|
||||
|
||||
override fun resetSettings() {
|
||||
host?.resetSettings()
|
||||
}
|
||||
|
||||
override fun exportSubscriptions(file: String) {
|
||||
host?.exportSubscriptions(file)
|
||||
}
|
||||
|
||||
override fun importSubscriptions(file: String) {
|
||||
host?.importSubscriptions(file)
|
||||
}
|
||||
|
||||
private fun loadHost(context: Context): BackupRestoreHost? = runCatching {
|
||||
Class.forName(HOST_CLASS)
|
||||
.getDeclaredConstructor(Context::class.java)
|
||||
.newInstance(context) as BackupRestoreHost
|
||||
}.onFailure { error ->
|
||||
Logger.w(messageString = "No host backup and restore actions found, skipping host-side changes", throwable = error)
|
||||
}.getOrNull()
|
||||
|
||||
private companion object {
|
||||
const val HOST_CLASS = "org.schabi.newpipe.settings.ComposeBackupRestoreHost"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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.content.Intent
|
||||
import net.newpipe.app.model.BackupFile
|
||||
import net.newpipe.app.model.DataExportResult
|
||||
import net.newpipe.app.model.DataImportResult
|
||||
import net.newpipe.app.model.SettingsFormat
|
||||
|
||||
interface BackupRestoreHost {
|
||||
|
||||
fun createFilePickerIntent(file: BackupFile): Intent
|
||||
|
||||
suspend fun exportData(file: String): DataExportResult
|
||||
|
||||
suspend fun importDatabase(file: String): DataImportResult
|
||||
|
||||
suspend fun importSettings(file: String, format: SettingsFormat)
|
||||
|
||||
fun finishImport(file: String)
|
||||
|
||||
fun resetSettings()
|
||||
|
||||
fun exportSubscriptions(file: String)
|
||||
|
||||
fun importSubscriptions(file: String)
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package net.newpipe.app.platform
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import co.touchlab.kermit.Logger
|
||||
import net.newpipe.app.model.BackupFile
|
||||
import net.newpipe.app.model.DataExportResult
|
||||
import net.newpipe.app.model.DataImportResult
|
||||
import net.newpipe.app.model.ErrorReport
|
||||
import net.newpipe.app.model.SettingsFormat
|
||||
|
||||
/**
|
||||
* Platform-specific actions triggered from the Backup and restore settings screen
|
||||
*/
|
||||
interface BackupRestoreActions {
|
||||
|
||||
@Composable
|
||||
fun rememberFilePicker(file: BackupFile, onNoFileManager: () -> Unit, onPicked: (String) -> Unit): () -> Unit
|
||||
|
||||
suspend fun exportData(file: String): DataExportResult
|
||||
|
||||
suspend fun importDatabase(file: String): DataImportResult
|
||||
|
||||
suspend fun importSettings(file: String, format: SettingsFormat)
|
||||
|
||||
fun finishImport(file: String)
|
||||
|
||||
fun resetSettings()
|
||||
|
||||
fun exportSubscriptions(file: String)
|
||||
|
||||
fun importSubscriptions(file: String)
|
||||
}
|
||||
|
||||
internal object NoOpBackupRestoreActions : BackupRestoreActions {
|
||||
|
||||
private val unsupportedError = ErrorReport(message = "Backup and restore is unsupported on this platform", open = {})
|
||||
|
||||
@Composable
|
||||
override fun rememberFilePicker(file: BackupFile, onNoFileManager: () -> Unit, onPicked: (String) -> Unit): () -> Unit = remember { { logUnsupported("pickFile") } }
|
||||
|
||||
override suspend fun exportData(file: String): DataExportResult {
|
||||
logUnsupported("exportData")
|
||||
return DataExportResult.Failed(unsupportedError)
|
||||
}
|
||||
|
||||
override suspend fun importDatabase(file: String): DataImportResult {
|
||||
logUnsupported("importDatabase")
|
||||
return DataImportResult.Failed(unsupportedError)
|
||||
}
|
||||
|
||||
override suspend fun importSettings(file: String, format: SettingsFormat) = logUnsupported("importSettings")
|
||||
|
||||
override fun finishImport(file: String) = logUnsupported("finishImport")
|
||||
|
||||
override fun resetSettings() = logUnsupported("resetSettings")
|
||||
|
||||
override fun exportSubscriptions(file: String) = logUnsupported("exportSubscriptions")
|
||||
|
||||
override fun importSubscriptions(file: String) = logUnsupported("importSubscriptions")
|
||||
|
||||
private fun logUnsupported(action: String) = Logger.w(messageString = "Backup and restore 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 = [BackupRestoreActions::class])
|
||||
class IOSBackupRestoreActions : BackupRestoreActions by NoOpBackupRestoreActions
|
||||
@@ -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 = [BackupRestoreActions::class])
|
||||
class JVMBackupRestoreActions : BackupRestoreActions by NoOpBackupRestoreActions
|
||||
Reference in new issue
Block a user