Allow storage backups to launch foreground service from background

This commit is contained in:
Torsten Grote 2021-10-08 14:03:12 -03:00 committed by Chirayu Desai
parent b184f208c4
commit c5a295d31c
5 changed files with 21 additions and 4 deletions

View file

@ -3,6 +3,7 @@ package com.stevesoltys.seedvault.settings
import android.app.backup.IBackupManager import android.app.backup.IBackupManager
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.os.PowerManager
import android.os.RemoteException import android.os.RemoteException
import android.provider.Settings import android.provider.Settings
import android.provider.Settings.Secure.BACKUP_AUTO_RESTORE import android.provider.Settings.Secure.BACKUP_AUTO_RESTORE
@ -11,6 +12,8 @@ import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.Toast
import android.widget.Toast.LENGTH_LONG
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.Preference.OnPreferenceChangeListener import androidx.preference.Preference.OnPreferenceChangeListener
@ -228,6 +231,14 @@ class SettingsFragment : PreferenceFragmentCompat() {
.setTitle(R.string.settings_backup_storage_dialog_title) .setTitle(R.string.settings_backup_storage_dialog_title)
.setMessage(R.string.settings_backup_storage_dialog_message) .setMessage(R.string.settings_backup_storage_dialog_message)
.setPositiveButton(R.string.settings_backup_storage_dialog_ok) { dialog, _ -> .setPositiveButton(R.string.settings_backup_storage_dialog_ok) { dialog, _ ->
// warn if battery optimization is active
// we don't bother with yet another dialog, because the ROM should handle it
val context = requireContext()
val powerManager = context.getSystemService(PowerManager::class.java)
if (!powerManager.isIgnoringBatteryOptimizations(context.packageName)) {
Toast.makeText(context, R.string.settings_backup_storage_battery_optimization,
LENGTH_LONG).show()
}
viewModel.enableStorageBackup() viewModel.enableStorageBackup()
backupStorage.isChecked = true backupStorage.isChecked = true
dialog.dismiss() dialog.dismiss()

View file

@ -38,6 +38,7 @@
<string name="settings_backup_storage_dialog_title">Experimental feature</string> <string name="settings_backup_storage_dialog_title">Experimental feature</string>
<string name="settings_backup_storage_dialog_message">Backing up files is still experimental and might not work. Do not rely on it for important data.</string> <string name="settings_backup_storage_dialog_message">Backing up files is still experimental and might not work. Do not rely on it for important data.</string>
<string name="settings_backup_storage_dialog_ok">Enable anyway</string> <string name="settings_backup_storage_dialog_ok">Enable anyway</string>
<string name="settings_backup_storage_battery_optimization">Warning: No automatic backups, because battery optimization is active.</string>
<string name="settings_backup_new_code_dialog_title">New recovery code required</string> <string name="settings_backup_new_code_dialog_title">New recovery code required</string>
<string name="settings_backup_new_code_dialog_message">To continue using app backups, you need to generate a new recovery code.\n\nWe are sorry for the inconvenience.</string> <string name="settings_backup_new_code_dialog_message">To continue using app backups, you need to generate a new recovery code.\n\nWe are sorry for the inconvenience.</string>
<string name="settings_backup_new_code_code_dialog_ok">New code</string> <string name="settings_backup_new_code_code_dialog_ok">New code</string>

View file

@ -18,6 +18,9 @@ public abstract class BackupJobService(private val serviceClass: Class<out Servi
JobService() { JobService() {
public companion object { public companion object {
/**
* Warning: This works only if battery optimization is disabled for the app using this.
*/
public fun scheduleJob( public fun scheduleJob(
context: Context, context: Context,
jobServiceClass: Class<*>, jobServiceClass: Class<*>,

View file

@ -1,15 +1,16 @@
package org.calyxos.backup.storage.ui package org.calyxos.backup.storage.ui
import android.app.Notification import android.app.Notification
import android.app.Notification.FOREGROUND_SERVICE_IMMEDIATE
import android.app.NotificationChannel import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.app.NotificationManager.IMPORTANCE_LOW import android.app.NotificationManager.IMPORTANCE_LOW
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.os.Build.VERSION.SDK_INT
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
import androidx.core.app.NotificationCompat.PRIORITY_DEFAULT import androidx.core.app.NotificationCompat.PRIORITY_DEFAULT
import org.calyxos.backup.storage.R import org.calyxos.backup.storage.R
@ -136,7 +137,7 @@ internal class Notifications(private val context: Context) {
infoText: CharSequence?, infoText: CharSequence?,
transferred: Int = 0, transferred: Int = 0,
expected: Int = 0, expected: Int = 0,
) = NotificationCompat.Builder(context, CHANNEL_ID_BACKUP).apply { ) = Notification.Builder(context, CHANNEL_ID_BACKUP).apply {
setSmallIcon(icon) setSmallIcon(icon)
setContentTitle(title) setContentTitle(title)
setContentText(infoText) setContentText(infoText)
@ -144,8 +145,7 @@ internal class Notifications(private val context: Context) {
setShowWhen(false) setShowWhen(false)
setWhen(System.currentTimeMillis()) setWhen(System.currentTimeMillis())
setProgress(expected, transferred, expected == 0) setProgress(expected, transferred, expected == 0)
foregroundServiceBehavior = FOREGROUND_SERVICE_IMMEDIATE if (SDK_INT >= 31) setForegroundServiceBehavior(FOREGROUND_SERVICE_IMMEDIATE)
priority = PRIORITY_DEFAULT
}.build() }.build()
} }

View file

@ -3,4 +3,6 @@
<backup-transport-whitelisted-service <backup-transport-whitelisted-service
service="com.stevesoltys.seedvault/.transport.ConfigurableBackupTransportService" /> service="com.stevesoltys.seedvault/.transport.ConfigurableBackupTransportService" />
<hidden-api-whitelisted-app package="com.stevesoltys.seedvault" /> <hidden-api-whitelisted-app package="com.stevesoltys.seedvault" />
<!-- needed for storage backups launching foreground service from background -->
<allow-in-power-save package="com.stevesoltys.seedvault" />
</config> </config>