Added file selection from system file picker for restore
This commit is contained in:
parent
3c07edb5e5
commit
4fbcb5d77f
4 changed files with 74 additions and 58 deletions
|
@ -1,10 +1,12 @@
|
||||||
package code.name.monkey.retromusic.fragments.backup
|
package code.name.monkey.retromusic.fragments.backup
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
|
@ -16,9 +18,11 @@ import code.name.monkey.retromusic.R
|
||||||
import code.name.monkey.retromusic.adapter.backup.BackupAdapter
|
import code.name.monkey.retromusic.adapter.backup.BackupAdapter
|
||||||
import code.name.monkey.retromusic.databinding.FragmentBackupBinding
|
import code.name.monkey.retromusic.databinding.FragmentBackupBinding
|
||||||
import code.name.monkey.retromusic.helper.BackupHelper
|
import code.name.monkey.retromusic.helper.BackupHelper
|
||||||
|
import code.name.monkey.retromusic.helper.sanitize
|
||||||
import code.name.monkey.retromusic.util.BackupUtil
|
import code.name.monkey.retromusic.util.BackupUtil
|
||||||
import com.afollestad.materialdialogs.MaterialDialog
|
import com.afollestad.materialdialogs.MaterialDialog
|
||||||
import com.afollestad.materialdialogs.input.input
|
import com.afollestad.materialdialogs.input.input
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
|
@ -42,25 +46,16 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
||||||
backupAdapter?.swapDataset(listOf())
|
backupAdapter?.swapDataset(listOf())
|
||||||
}
|
}
|
||||||
backupViewModel.loadBackups()
|
backupViewModel.loadBackups()
|
||||||
setupButtons()
|
val openFilePicker = registerForActivityResult(ActivityResultContracts.OpenDocument()) {
|
||||||
}
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
|
backupViewModel.restoreBackup(requireActivity(), requireContext().contentResolver.openInputStream(it))
|
||||||
private fun setupButtons() {
|
|
||||||
binding.createBackup.setOnClickListener {
|
|
||||||
MaterialDialog(requireContext()).show {
|
|
||||||
title(res = R.string.action_rename)
|
|
||||||
input(prefill = System.currentTimeMillis().toString()) { _, text ->
|
|
||||||
// Text submitted with the action button
|
|
||||||
lifecycleScope.launch {
|
|
||||||
BackupHelper.createBackup(requireContext(), text.toString())
|
|
||||||
backupViewModel.loadBackups()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
positiveButton(android.R.string.ok)
|
|
||||||
negativeButton(R.string.action_cancel)
|
|
||||||
setTitle(R.string.title_new_backup)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
binding.createBackup.setOnClickListener {
|
||||||
|
showCreateBackupDialog()
|
||||||
|
}
|
||||||
|
binding.restoreBackup.setOnClickListener {
|
||||||
|
openFilePicker.launch(arrayOf("application/octet-stream"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +71,7 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
||||||
|
|
||||||
private fun checkIsEmpty() {
|
private fun checkIsEmpty() {
|
||||||
val isEmpty = backupAdapter!!.itemCount == 0
|
val isEmpty = backupAdapter!!.itemCount == 0
|
||||||
binding.empty.isVisible = isEmpty
|
binding.emptyText.isVisible = isEmpty
|
||||||
binding.backupTitle.isVisible = !isEmpty
|
binding.backupTitle.isVisible = !isEmpty
|
||||||
binding.backupRecyclerview.isVisible = !isEmpty
|
binding.backupRecyclerview.isVisible = !isEmpty
|
||||||
}
|
}
|
||||||
|
@ -88,13 +83,31 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("CheckResult")
|
||||||
|
private fun showCreateBackupDialog() {
|
||||||
|
MaterialDialog(requireContext()).show {
|
||||||
|
cornerRadius(res = R.dimen.m3_card_corner_radius)
|
||||||
|
title(res = R.string.action_rename)
|
||||||
|
input(prefill = System.currentTimeMillis().toString()) { _, text ->
|
||||||
|
// Text submitted with the action button
|
||||||
|
lifecycleScope.launch {
|
||||||
|
BackupHelper.createBackup(requireContext(), text.sanitize())
|
||||||
|
backupViewModel.loadBackups()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
positiveButton(android.R.string.ok)
|
||||||
|
negativeButton(R.string.action_cancel)
|
||||||
|
setTitle(R.string.title_new_backup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onBackupClicked(file: File) {
|
override fun onBackupClicked(file: File) {
|
||||||
AlertDialog.Builder(requireContext())
|
AlertDialog.Builder(requireContext())
|
||||||
.setTitle(R.string.restore)
|
.setTitle(R.string.restore)
|
||||||
.setMessage(R.string.restore_message)
|
.setMessage(R.string.restore_message)
|
||||||
.setPositiveButton(R.string.restore) { _, _ ->
|
.setPositiveButton(R.string.restore) { _, _ ->
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
backupViewModel.restoreBackup(requireActivity(), file)
|
backupViewModel.restoreBackup(requireActivity(), file.inputStream())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.setNegativeButton(android.R.string.cancel, null)
|
.setNegativeButton(android.R.string.cancel, null)
|
||||||
|
@ -102,6 +115,7 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
||||||
.show()
|
.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("CheckResult")
|
||||||
override fun onBackupMenuClicked(file: File, menuItem: MenuItem): Boolean {
|
override fun onBackupMenuClicked(file: File, menuItem: MenuItem): Boolean {
|
||||||
when (menuItem.itemId) {
|
when (menuItem.itemId) {
|
||||||
R.id.action_delete -> {
|
R.id.action_delete -> {
|
||||||
|
@ -119,7 +133,11 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
||||||
}
|
}
|
||||||
R.id.action_share -> {
|
R.id.action_share -> {
|
||||||
activity?.startActivity(
|
activity?.startActivity(
|
||||||
Intent.createChooser(BackupUtil.createShareFileIntent(file, requireContext()), null))
|
Intent.createChooser(
|
||||||
|
BackupUtil.createShareFileIntent(file, requireContext()),
|
||||||
|
null
|
||||||
|
)
|
||||||
|
)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
R.id.action_rename -> {
|
R.id.action_rename -> {
|
||||||
|
|
|
@ -9,6 +9,7 @@ import code.name.monkey.retromusic.helper.BackupHelper
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.InputStream
|
||||||
import kotlin.system.exitProcess
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,8 +25,8 @@ class BackupViewModel : ViewModel() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun restoreBackup(activity: Activity, file: File) {
|
suspend fun restoreBackup(activity: Activity, inputStream: InputStream?) {
|
||||||
BackupHelper.restoreBackup(activity, file)
|
BackupHelper.restoreBackup(activity, inputStream)
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
val intent = Intent(
|
val intent = Intent(
|
||||||
activity,
|
activity,
|
||||||
|
|
|
@ -15,7 +15,7 @@ import java.util.zip.ZipOutputStream
|
||||||
object BackupHelper {
|
object BackupHelper {
|
||||||
suspend fun createBackup(context: Context, name: String) {
|
suspend fun createBackup(context: Context, name: String) {
|
||||||
val backupFile =
|
val backupFile =
|
||||||
File(backupRootPath + name + APPEND_EXTENSION)
|
File(backupRootPath + File.separator + name + APPEND_EXTENSION)
|
||||||
if (backupFile.parentFile?.exists() != true) {
|
if (backupFile.parentFile?.exists() != true) {
|
||||||
backupFile.parentFile?.mkdirs()
|
backupFile.parentFile?.mkdirs()
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ object BackupHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun zipAll(zipItems: List<ZipItem>, backupFile: File) {
|
private suspend fun zipAll(zipItems: List<ZipItem>, backupFile: File) {
|
||||||
try {
|
try {
|
||||||
ZipOutputStream(BufferedOutputStream(FileOutputStream(backupFile))).use { out ->
|
ZipOutputStream(BufferedOutputStream(FileOutputStream(backupFile))).use { out ->
|
||||||
for (zipItem in zipItems) {
|
for (zipItem in zipItems) {
|
||||||
|
@ -36,13 +36,17 @@ object BackupHelper {
|
||||||
BufferedInputStream(fi).use { origin ->
|
BufferedInputStream(fi).use { origin ->
|
||||||
val entry = ZipEntry(zipItem.zipPath)
|
val entry = ZipEntry(zipItem.zipPath)
|
||||||
out.putNextEntry(entry)
|
out.putNextEntry(entry)
|
||||||
origin.copyTo(out, 1024)
|
origin.copyTo(out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (exception: FileNotFoundException) {
|
} catch (exception: FileNotFoundException) {
|
||||||
Toast.makeText(App.getContext(), "Couldn't create backup", Toast.LENGTH_SHORT).show()
|
exception.printStackTrace()
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
Toast.makeText(App.getContext(), "Couldn't create backup", Toast.LENGTH_SHORT)
|
||||||
|
.show()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,9 +76,9 @@ object BackupHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun restoreBackup(context: Context, file: File) {
|
suspend fun restoreBackup(context: Context, inputStream: InputStream?) {
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
ZipInputStream(FileInputStream(file)).use {
|
ZipInputStream(inputStream).use {
|
||||||
var entry = it.nextEntry
|
var entry = it.nextEntry
|
||||||
while (entry != null) {
|
while (entry != null) {
|
||||||
if (entry.isDatabaseEntry()) restoreDatabase(context, it, entry)
|
if (entry.isDatabaseEntry()) restoreDatabase(context, it, entry)
|
||||||
|
@ -156,4 +160,17 @@ object BackupHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data class ZipItem(val filePath: String, val zipPath: String)
|
data class ZipItem(val filePath: String, val zipPath: String)
|
||||||
|
|
||||||
|
fun CharSequence.sanitize(): String {
|
||||||
|
return toString().replace("/", "_")
|
||||||
|
.replace(":", "_")
|
||||||
|
.replace("*", "_")
|
||||||
|
.replace("?", "_")
|
||||||
|
.replace("\"", "_")
|
||||||
|
.replace("<", "_")
|
||||||
|
.replace(">", "_")
|
||||||
|
.replace("|", "_")
|
||||||
|
.replace("\\", "_")
|
||||||
|
.replace("&", "_")
|
||||||
|
}
|
|
@ -3,8 +3,7 @@
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent">
|
||||||
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
|
|
||||||
|
|
||||||
<com.google.android.material.button.MaterialButton
|
<com.google.android.material.button.MaterialButton
|
||||||
android:id="@+id/create_backup"
|
android:id="@+id/create_backup"
|
||||||
|
@ -61,36 +60,17 @@
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/backup_title" />
|
app:layout_constraintTop_toBottomOf="@+id/backup_title" />
|
||||||
|
|
||||||
<LinearLayout
|
<com.google.android.material.textview.MaterialTextView
|
||||||
android:id="@android:id/empty"
|
android:id="@+id/emptyText"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="center"
|
android:text="@string/no_backups_found"
|
||||||
android:orientation="vertical"
|
android:textAppearance="@style/TextViewHeadline5"
|
||||||
android:visibility="gone"
|
android:textColor="?android:attr/textColorSecondary"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/create_backup"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
tools:visibility="visible">
|
tools:visibility="visible" />
|
||||||
|
|
||||||
<com.google.android.material.textview.MaterialTextView
|
|
||||||
android:id="@+id/emptyEmoji"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginBottom="16dp"
|
|
||||||
android:text="@string/empty_text_emoji"
|
|
||||||
android:textAppearance="@style/TextViewHeadline3" />
|
|
||||||
|
|
||||||
<com.google.android.material.textview.MaterialTextView
|
|
||||||
android:id="@+id/emptyText"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_gravity="center"
|
|
||||||
android:text="@string/no_backups_found"
|
|
||||||
android:textAppearance="@style/TextViewHeadline5"
|
|
||||||
android:textColor="?android:attr/textColorSecondary"
|
|
||||||
tools:visibility="visible" />
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in a new issue