Fall back to internal storage...

if external private storage is unavailable
pull/3973/head
Alex Baker 4 weeks ago
parent 94ad2a381e
commit 8e9f27c46e

@ -378,7 +378,7 @@ class Preferences @JvmOverloads constructor(
val backupDirectory: Uri? val backupDirectory: Uri?
get() = getDirectory(R.string.p_backup_dir, "backups") get() = getDirectory(R.string.p_backup_dir, "backups")
val externalStorage: Uri val appPrivateStorage: Uri
get() = root.uri get() = root.uri
val attachmentsDirectory: Uri? val attachmentsDirectory: Uri?
@ -412,13 +412,12 @@ class Preferences @JvmOverloads constructor(
?: getDefaultFileLocation(name)?.let { Uri.fromFile(it) } ?: getDefaultFileLocation(name)?.let { Uri.fromFile(it) }
private val root: DocumentFile private val root: DocumentFile
get() = DocumentFile.fromFile(context.getExternalFilesDir(null)!!) get() = DocumentFile.fromFile(context.getExternalFilesDir(null) ?: context.filesDir)
private fun getDefaultFileLocation(type: String): File? { private fun getDefaultFileLocation(type: String): File? {
val externalFilesDir = context.getExternalFilesDir(null) ?: return null val baseDir = context.getExternalFilesDir(null) ?: context.filesDir
val path = String.format("%s/%s", externalFilesDir.absolutePath, type) val path = File(baseDir, type)
val file = File(path) return if (path.isDirectory || path.mkdirs()) path else null
return if (file.isDirectory || file.mkdirs()) file else null
} }
private fun hasWritePermission(context: Context, uri: Uri): Boolean = private fun hasWritePermission(context: Context, uri: Uri): Boolean =

@ -2,6 +2,7 @@ package org.tasks.preferences
import android.content.ContentResolver import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.net.Uri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
@ -51,8 +52,16 @@ class PreferencesViewModel @Inject constructor(
get() = isStale(lastDriveBackup.value) && isStale(lastAndroidBackup.value) get() = isStale(lastDriveBackup.value) && isStale(lastAndroidBackup.value)
val usingPrivateStorage: Boolean val usingPrivateStorage: Boolean
get() = preferences.backupDirectory.let { get() = preferences.backupDirectory.let { backupDir ->
it == null || it.toString().startsWith(preferences.externalStorage.toString()) val backupDirStr = backupDir?.toString() ?: return true
context
.getExternalFilesDir(null)
?.let {
if (backupDirStr.startsWith(Uri.fromFile(it).toString())) {
return true
}
}
return backupDirStr.startsWith(Uri.fromFile(context.filesDir).toString())
} }
val driveAccount: String? val driveAccount: String?

@ -257,7 +257,7 @@ class Backups : InjectingPreferenceFragment() {
pref.summary = """ pref.summary = """
$location $location
${requireContext().getString(R.string.backup_location_warning, FileHelper.uri2String(preferences.externalStorage))} ${requireContext().getString(R.string.backup_location_warning, FileHelper.uri2String(preferences.appPrivateStorage))}
""".trimIndent() """.trimIndent()
} else { } else {
pref.icon = null pref.icon = null

Loading…
Cancel
Save