Drop document contracts intent check because of false positives
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package it.vfsfitvnm.vimusic.ui.screens.settings
|
package it.vfsfitvnm.vimusic.ui.screens.settings
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.widget.Toast
|
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.animation.ExperimentalAnimationApi
|
import androidx.compose.animation.ExperimentalAnimationApi
|
||||||
@@ -49,40 +48,41 @@ fun DatabaseSettings() {
|
|||||||
.collect { value = it }
|
.collect { value = it }
|
||||||
}
|
}
|
||||||
|
|
||||||
val openDocumentContract = ActivityResultContracts.OpenDocument()
|
val backupLauncher =
|
||||||
val createDocumentContract = ActivityResultContracts.CreateDocument("application/vnd.sqlite3")
|
rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument("application/vnd.sqlite3")) { uri ->
|
||||||
|
if (uri == null) return@rememberLauncherForActivityResult
|
||||||
|
|
||||||
val backupLauncher = rememberLauncherForActivityResult(createDocumentContract) { uri ->
|
query {
|
||||||
if (uri == null) return@rememberLauncherForActivityResult
|
Database.internal.checkpoint()
|
||||||
|
|
||||||
query {
|
context.applicationContext.contentResolver.openOutputStream(uri)
|
||||||
Database.internal.checkpoint()
|
?.use { outputStream ->
|
||||||
|
FileInputStream(Database.internal.path).use { inputStream ->
|
||||||
context.applicationContext.contentResolver.openOutputStream(uri)?.use { outputStream ->
|
inputStream.copyTo(outputStream)
|
||||||
FileInputStream(Database.internal.path).use { inputStream ->
|
}
|
||||||
inputStream.copyTo(outputStream)
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
val restoreLauncher = rememberLauncherForActivityResult(openDocumentContract) { uri ->
|
val restoreLauncher =
|
||||||
if (uri == null) return@rememberLauncherForActivityResult
|
rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->
|
||||||
|
if (uri == null) return@rememberLauncherForActivityResult
|
||||||
|
|
||||||
query {
|
query {
|
||||||
Database.internal.checkpoint()
|
Database.internal.checkpoint()
|
||||||
Database.internal.close()
|
Database.internal.close()
|
||||||
|
|
||||||
context.applicationContext.contentResolver.openInputStream(uri)?.use { inputStream ->
|
context.applicationContext.contentResolver.openInputStream(uri)
|
||||||
FileOutputStream(Database.internal.path).use { outputStream ->
|
?.use { inputStream ->
|
||||||
inputStream.copyTo(outputStream)
|
FileOutputStream(Database.internal.path).use { outputStream ->
|
||||||
}
|
inputStream.copyTo(outputStream)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
context.stopService(context.intent<PlayerService>())
|
||||||
|
exitProcess(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
context.stopService(context.intent<PlayerService>())
|
|
||||||
exitProcess(0)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
@@ -123,21 +123,7 @@ fun DatabaseSettings() {
|
|||||||
onClick = {
|
onClick = {
|
||||||
@SuppressLint("SimpleDateFormat")
|
@SuppressLint("SimpleDateFormat")
|
||||||
val dateFormat = SimpleDateFormat("yyyyMMddHHmmss")
|
val dateFormat = SimpleDateFormat("yyyyMMddHHmmss")
|
||||||
val input = "vimusic_${dateFormat.format(Date())}.db"
|
backupLauncher.launch("vimusic_${dateFormat.format(Date())}.db")
|
||||||
|
|
||||||
if (createDocumentContract.createIntent(context, input)
|
|
||||||
.resolveActivity(context.packageManager) != null
|
|
||||||
) {
|
|
||||||
backupLauncher.launch(input)
|
|
||||||
} else {
|
|
||||||
Toast
|
|
||||||
.makeText(
|
|
||||||
context,
|
|
||||||
"Can't copy the database to the external storage",
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
)
|
|
||||||
.show()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -151,23 +137,13 @@ fun DatabaseSettings() {
|
|||||||
title = "Restore",
|
title = "Restore",
|
||||||
text = "Import the database from the external storage",
|
text = "Import the database from the external storage",
|
||||||
onClick = {
|
onClick = {
|
||||||
val input = arrayOf(
|
restoreLauncher.launch(
|
||||||
"application/x-sqlite3",
|
arrayOf(
|
||||||
"application/vnd.sqlite3",
|
"application/x-sqlite3",
|
||||||
"application/octet-stream"
|
"application/vnd.sqlite3",
|
||||||
|
"application/octet-stream"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if (openDocumentContract.createIntent(context, input)
|
|
||||||
.resolveActivity(context.packageManager) != null
|
|
||||||
) {
|
|
||||||
restoreLauncher.launch(input)
|
|
||||||
} else {
|
|
||||||
Toast.makeText(
|
|
||||||
context,
|
|
||||||
"Can't read the database from the external storage",
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user