Skip to content

Commit

Permalink
fixup! Allow secondary user backup to USB
Browse files Browse the repository at this point in the history
Issue: calyxos#1058
Change-Id: I4aaadef5954e9c091f7048bffd8e0ed700fdd2e6
  • Loading branch information
Uldiniad committed Nov 1, 2023
1 parent 6ce2e27 commit 97d2d18
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
14 changes: 8 additions & 6 deletions app/src/main/java/com/stevesoltys/seedvault/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import android.app.Application
import android.app.backup.BackupManager.PACKAGE_MANAGER_SENTINEL
import android.app.backup.IBackupManager
import android.content.Context
import android.content.Context.BACKUP_SERVICE
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Build
import android.os.ServiceManager.getService
import android.os.StrictMode
import android.os.UserHandle
import android.os.UserManager
import com.stevesoltys.seedvault.crypto.cryptoModule
import com.stevesoltys.seedvault.header.headerModule
import com.stevesoltys.seedvault.metadata.MetadataManager
Expand Down Expand Up @@ -143,8 +142,11 @@ fun <T> permitDiskReads(func: () -> T): T {
}
}

fun Context.getSystemContext(isUsbStorage: () -> Boolean): Context {
return if (checkSelfPermission(INTERACT_ACROSS_USERS_FULL) == PERMISSION_GRANTED &&
isUsbStorage()
) createContextAsUser(UserHandle.SYSTEM, 0) else this
@Suppress("MissingPermission")
fun Context.getStorageContext(isUsbStorage: () -> Boolean): Context {
if (checkSelfPermission(INTERACT_ACROSS_USERS_FULL) == PERMISSION_GRANTED && isUsbStorage()) {
UserManager.get(this).getProfileParent(user)
?.let { parent -> return createContextAsUser(parent, 0) }
}
return this
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import android.content.pm.PackageManager
import android.util.Log
import androidx.documentfile.provider.DocumentFile
import com.stevesoltys.seedvault.getSystemContext
import com.stevesoltys.seedvault.getStorageContext
import com.stevesoltys.seedvault.plugins.EncryptedMetadata
import com.stevesoltys.seedvault.plugins.StoragePlugin
import com.stevesoltys.seedvault.settings.Storage
Expand All @@ -25,7 +25,7 @@ internal class DocumentsProviderStoragePlugin(
* Attention: This context might be from a different user. Use with care.
*/
private val context: Context
get() = appContext.getSystemContext {
get() = appContext.getStorageContext {
storage.storage?.isUsb == true
}

Expand Down Expand Up @@ -83,7 +83,7 @@ internal class DocumentsProviderStoragePlugin(
@Throws(IOException::class)
override suspend fun hasBackup(storage: Storage): Boolean {
// potentially get system user context if needed here
val c = appContext.getSystemContext { storage.isUsb }
val c = appContext.getStorageContext { storage.isUsb }
val parent = DocumentFile.fromTreeUri(c, storage.uri) ?: throw AssertionError()
val rootDir = parent.findFileBlocking(c, DIRECTORY_ROOT) ?: return false
val backupSets = getBackups(c, rootDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import android.provider.DocumentsContract.getDocumentId
import android.util.Log
import androidx.annotation.VisibleForTesting
import androidx.documentfile.provider.DocumentFile
import com.stevesoltys.seedvault.getSystemContext
import com.stevesoltys.seedvault.getStorageContext
import com.stevesoltys.seedvault.settings.SettingsManager
import com.stevesoltys.seedvault.settings.Storage
import kotlinx.coroutines.TimeoutCancellationException
Expand Down Expand Up @@ -55,7 +55,7 @@ internal class DocumentsStorage(
* Attention: This context might be from a different user. Use with care.
*/
private val context: Context
get() = appContext.getSystemContext {
get() = appContext.getStorageContext {
storage?.isUsb == true
}
private val contentResolver: ContentResolver get() = context.contentResolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.documentfile.provider.DocumentFile
import androidx.preference.PreferenceManager
import com.stevesoltys.seedvault.getSystemContext
import com.stevesoltys.seedvault.getStorageContext
import com.stevesoltys.seedvault.permitDiskReads
import com.stevesoltys.seedvault.transport.backup.BackupCoordinator
import java.util.concurrent.ConcurrentSkipListSet
Expand Down Expand Up @@ -131,7 +131,7 @@ class SettingsManager(private val context: Context) {
@WorkerThread
fun canDoBackupNow(): Boolean {
val storage = getStorage() ?: return false
val systemContext = context.getSystemContext { storage.isUsb }
val systemContext = context.getStorageContext { storage.isUsb }
return !storage.isUnavailableUsb(systemContext) && !storage.isUnavailableNetwork(context)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.stevesoltys.seedvault.storage
import android.content.Context
import androidx.documentfile.provider.DocumentFile
import com.stevesoltys.seedvault.crypto.KeyManager
import com.stevesoltys.seedvault.getSystemContext
import com.stevesoltys.seedvault.getStorageContext
import com.stevesoltys.seedvault.plugins.saf.DocumentsStorage
import org.calyxos.backup.storage.plugin.saf.SafStoragePlugin
import javax.crypto.SecretKey
Expand All @@ -17,7 +17,7 @@ internal class SeedvaultStoragePlugin(
* Attention: This context might be from a different user. Use with care.
*/
override val context: Context
get() = appContext.getSystemContext {
get() = appContext.getStorageContext {
storage.storage?.isUsb == true
}
override val root: DocumentFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import android.provider.DocumentsContract.Root.FLAG_SUPPORTS_CREATE
import android.provider.DocumentsContract.Root.FLAG_SUPPORTS_IS_CHILD
import android.util.Log
import com.stevesoltys.seedvault.R
import com.stevesoltys.seedvault.getSystemContext
import com.stevesoltys.seedvault.getStorageContext
import com.stevesoltys.seedvault.ui.storage.StorageOption.SafOption

internal object StorageRootResolver {
Expand All @@ -39,7 +39,7 @@ internal object StorageRootResolver {
}
}
// add special system user roots for USB devices
val c = context.getSystemContext {
val c = context.getStorageContext {
authority == AUTHORITY_STORAGE && UserHandle.myUserId() != UserHandle.USER_SYSTEM
}
// only proceed if we really got a different [Context], e.g. had permission for it
Expand Down

0 comments on commit 97d2d18

Please sign in to comment.