Added file selection from system file picker for restore

This commit is contained in:
Prathamesh More 2021-11-24 00:37:16 +05:30
parent 3c07edb5e5
commit 4fbcb5d77f
4 changed files with 74 additions and 58 deletions

View file

@ -1,10 +1,12 @@
package code.name.monkey.retromusic.fragments.backup
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
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.databinding.FragmentBackupBinding
import code.name.monkey.retromusic.helper.BackupHelper
import code.name.monkey.retromusic.helper.sanitize
import code.name.monkey.retromusic.util.BackupUtil
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.input.input
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
@ -42,25 +46,16 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
backupAdapter?.swapDataset(listOf())
}
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()
showCreateBackupDialog()
}
}
positiveButton(android.R.string.ok)
negativeButton(R.string.action_cancel)
setTitle(R.string.title_new_backup)
}
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() {
val isEmpty = backupAdapter!!.itemCount == 0
binding.empty.isVisible = isEmpty
binding.emptyText.isVisible = isEmpty
binding.backupTitle.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) {
AlertDialog.Builder(requireContext())
.setTitle(R.string.restore)
.setMessage(R.string.restore_message)
.setPositiveButton(R.string.restore) { _, _ ->
lifecycleScope.launch {
backupViewModel.restoreBackup(requireActivity(), file)
backupViewModel.restoreBackup(requireActivity(), file.inputStream())
}
}
.setNegativeButton(android.R.string.cancel, null)
@ -102,6 +115,7 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
.show()
}
@SuppressLint("CheckResult")
override fun onBackupMenuClicked(file: File, menuItem: MenuItem): Boolean {
when (menuItem.itemId) {
R.id.action_delete -> {
@ -119,7 +133,11 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
}
R.id.action_share -> {
activity?.startActivity(
Intent.createChooser(BackupUtil.createShareFileIntent(file, requireContext()), null))
Intent.createChooser(
BackupUtil.createShareFileIntent(file, requireContext()),
null
)
)
return true
}
R.id.action_rename -> {

View file

@ -9,6 +9,7 @@ import code.name.monkey.retromusic.helper.BackupHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.io.InputStream
import kotlin.system.exitProcess
@ -24,8 +25,8 @@ class BackupViewModel : ViewModel() {
}
}
suspend fun restoreBackup(activity: Activity, file: File) {
BackupHelper.restoreBackup(activity, file)
suspend fun restoreBackup(activity: Activity, inputStream: InputStream?) {
BackupHelper.restoreBackup(activity, inputStream)
withContext(Dispatchers.Main) {
val intent = Intent(
activity,

View file

@ -15,7 +15,7 @@ import java.util.zip.ZipOutputStream
object BackupHelper {
suspend fun createBackup(context: Context, name: String) {
val backupFile =
File(backupRootPath + name + APPEND_EXTENSION)
File(backupRootPath + File.separator + name + APPEND_EXTENSION)
if (backupFile.parentFile?.exists() != true) {
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 {
ZipOutputStream(BufferedOutputStream(FileOutputStream(backupFile))).use { out ->
for (zipItem in zipItems) {
@ -36,13 +36,17 @@ object BackupHelper {
BufferedInputStream(fi).use { origin ->
val entry = ZipEntry(zipItem.zipPath)
out.putNextEntry(entry)
origin.copyTo(out, 1024)
origin.copyTo(out)
}
}
}
}
} 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) {
ZipInputStream(FileInputStream(file)).use {
ZipInputStream(inputStream).use {
var entry = it.nextEntry
while (entry != null) {
if (entry.isDatabaseEntry()) restoreDatabase(context, it, entry)
@ -157,3 +161,16 @@ object BackupHelper {
}
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("&", "_")
}

View file

@ -3,8 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
android:layout_height="match_parent">
<com.google.android.material.button.MaterialButton
android:id="@+id/create_backup"
@ -61,36 +60,17 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/backup_title" />
<LinearLayout
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/create_backup"
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"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>