From dab5ddb83804c28ebb2f71a8463b64e65cb0de4b Mon Sep 17 00:00:00 2001 From: Prathamesh More Date: Sat, 27 Nov 2021 20:07:22 +0530 Subject: [PATCH] Added Custom Artist images backup --- .../monkey/retromusic/helper/BackupHelper.kt | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/app/src/main/java/code/name/monkey/retromusic/helper/BackupHelper.kt b/app/src/main/java/code/name/monkey/retromusic/helper/BackupHelper.kt index 034e367d..aefe5511 100644 --- a/app/src/main/java/code/name/monkey/retromusic/helper/BackupHelper.kt +++ b/app/src/main/java/code/name/monkey/retromusic/helper/BackupHelper.kt @@ -23,6 +23,7 @@ object BackupHelper { zipItems.addAll(getDatabaseZipItems(context)) zipItems.addAll(getSettingsZipItems(context)) getUserImageZipItems(context)?.let { zipItems.addAll(it) } + zipItems.addAll(getCustomArtistZipItems(context)) zipAll(zipItems, backupFile) } @@ -80,6 +81,28 @@ object BackupHelper { } } + private fun getCustomArtistZipItems(context: Context): List { + val zipItemList = mutableListOf() + val sharedPrefPath = context.filesDir.parentFile?.absolutePath + "/shared_prefs/" + + zipItemList.addAll( + File(context.filesDir, "custom_artist_images") + .listFiles()?.map { + ZipItem( + it.absolutePath, + "$CUSTOM_ARTISTS_PATH${File.separator}custom_artist_images${File.separator}${it.name}" + ) + }?.toList() ?: listOf() + ) + zipItemList.add( + ZipItem( + sharedPrefPath + File.separator + "custom_artist_image.xml", + "$CUSTOM_ARTISTS_PATH${File.separator}prefs${File.separator}custom_artist_image.xml" + ) + ) + return zipItemList + } + suspend fun restoreBackup(context: Context, inputStream: InputStream?) { withContext(Dispatchers.IO) { ZipInputStream(inputStream).use { @@ -88,6 +111,16 @@ object BackupHelper { if (entry.isDatabaseEntry()) restoreDatabase(context, it, entry) if (entry.isPreferenceEntry()) restorePreferences(context, it, entry) if (entry.isImageEntry()) restoreImages(context, it, entry) + if (entry.isCustomArtistImageEntry()) restoreCustomArtistImages( + context, + it, + entry + ) + if (entry.isCustomArtistPrefEntry()) restoreCustomArtistPrefs( + context, + it, + entry + ) entry = it.nextEntry } } @@ -137,6 +170,48 @@ object BackupHelper { } } + private fun restoreCustomArtistImages( + context: Context, + zipIn: ZipInputStream, + zipEntry: ZipEntry + ) { + val parentFolder = File(context.filesDir, "custom_artist_images") + + if (!parentFolder.exists()) { + parentFolder.mkdirs() + } + BufferedOutputStream( + FileOutputStream( + File( + parentFolder, + zipEntry.getFileName() + ) + ) + ).use { bos -> + val bytesIn = ByteArray(DEFAULT_BUFFER_SIZE) + var read: Int + while (zipIn.read(bytesIn).also { read = it } != -1) { + bos.write(bytesIn, 0, read) + } + } + } + + private fun restoreCustomArtistPrefs( + context: Context, + zipIn: ZipInputStream, + zipEntry: ZipEntry + ) { + val filePath = + context.filesDir.parentFile?.absolutePath + "/shared_prefs/" + zipEntry.getFileName() + BufferedOutputStream(FileOutputStream(filePath)).use { bos -> + val bytesIn = ByteArray(DEFAULT_BUFFER_SIZE) + var read: Int + while (zipIn.read(bytesIn).also { read = it } != -1) { + bos.write(bytesIn, 0, read) + } + } + } + val backupRootPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) .toString() + "/RetroMusic/Backups/" @@ -145,6 +220,7 @@ object BackupHelper { private const val DATABASES_PATH = "databases" private const val SETTINGS_PATH = "prefs" private const val IMAGES_PATH = "userImages" + private const val CUSTOM_ARTISTS_PATH = "artistImages" private const val THEME_PREFS_KEY_DEFAULT = "[[kabouzeid_app-theme-helper]]" private fun ZipEntry.isDatabaseEntry(): Boolean { @@ -159,6 +235,14 @@ object BackupHelper { return name.startsWith(IMAGES_PATH) } + private fun ZipEntry.isCustomArtistImageEntry(): Boolean { + return name.startsWith(CUSTOM_ARTISTS_PATH) && name.contains("custom_artist_images") + } + + private fun ZipEntry.isCustomArtistPrefEntry(): Boolean { + return name.startsWith(CUSTOM_ARTISTS_PATH) && name.contains("prefs") + } + private fun ZipEntry.getFileName(): String { return name.substring(name.lastIndexOf(File.separator)) }