Skip to content

Commit

Permalink
Feat: Cleaned up backup system to save in a better location.
Browse files Browse the repository at this point in the history
Signed-off-by: HeCodes2Much <wayne6324@gmail.com>
  • Loading branch information
CreativeCodeCat committed May 30, 2024
1 parent 605eed4 commit 8bbc3b9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import android.view.WindowManager
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService
import androidx.fragment.app.Fragment

fun Activity.getDisplayWidth(): Int {
val windowManager = getSystemService<WindowManager>() ?: return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.graphics.Bitmap
import android.graphics.drawable.AdaptiveIconDrawable
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.os.UserHandle
import android.os.UserManager
import android.provider.AlarmClock
Expand Down Expand Up @@ -310,50 +311,63 @@ fun Context.isWorkProfileEnabled(): Boolean {

fun Context.backupSharedPreferences(backupFileName: String) {
val sharedPreferences: SharedPreferences =
this.getSharedPreferences(Constants.PREFS_FILENAME, 0)
this.getSharedPreferences(Constants.PREFS_FILENAME, Context.MODE_PRIVATE)
val allPrefs = sharedPreferences.all
val backupFile = File(filesDir, backupFileName)

println("Backup SharedPreferences to: ${backupFile.absolutePath}")
// Check if external storage is writable
if (!isExternalStorageWritable()) {
showLongToast("External storage is not writable.")
return
}

val backupDir = ContextCompat.getExternalFilesDirs(this, null)
if (backupDir.isEmpty()) {
showLongToast("No external storage directories found.")
return
}

val backupFile = File(backupDir[0], backupFileName)

try {
backupFile.bufferedWriter().use { writer ->
for ((key, value) in allPrefs) {
if (value != null) {
val line = when (value) {
is Boolean -> "$key=${value}\n"
is Int -> "$key=${value}\n"
is Float -> "$key=${value}\n"
is Long -> "$key=${value}\n"
is String -> "$key=${value}\n"
is Boolean, is Int, is Float, is Long, is String -> "$key=$value\n"
is Set<*> -> "$key=${value.joinToString(",")}\n"
else -> null
}
if (line != null) {
writer.write(line)
println("Writing: $line")
} else {
println("Skipping unsupported type for key: $key")
line?.let {
writer.write(it)
}
} else {
println("Null value for key: $key")
}
}
}
println("Backup completed successfully.")
showLongToast("Backup completed successfully.")
} catch (e: IOException) {
e.printStackTrace()
println("Failed to backup SharedPreferences: ${e.message}")
showLongToast("Failed to backup SharedPreferences: ${e.message}")
}
}

private fun isExternalStorageWritable(): Boolean {
return Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED
}

fun Context.restoreSharedPreferences(backupFileName: String) {
val sharedPreferences: SharedPreferences =
this.getSharedPreferences(Constants.PREFS_FILENAME, 0)
this.getSharedPreferences(Constants.PREFS_FILENAME, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
val backupFile = File(filesDir, backupFileName)

println("Restoring SharedPreferences from: ${backupFile.absolutePath}")
// Check if external storage is readable
if (!isExternalStorageReadable()) {
showLongToast("External storage is not readable.")
return
}

val backupDir = getExternalFilesDir(null)
val backupFile = File(backupDir, backupFileName)
Log.d("backupFile", "$backupFile")

if (backupFile.exists()) {
try {
Expand All @@ -371,19 +385,23 @@ fun Context.restoreSharedPreferences(backupFileName: String) {
value.contains(",") -> editor.putStringSet(key, value.split(",").toSet())
else -> editor.putString(key, value)
}
println("Restoring: $key=$value")
}
editor.apply()
println("Restore completed successfully.")
showLongToast("Restore completed successfully.")
} catch (e: IOException) {
e.printStackTrace()
println("Failed to restore SharedPreferences: ${e.message}")
showLongToast("Failed to restore SharedPreferences: ${e.message}")
}
} else {
println("Backup file does not exist.")
showLongToast("Backup file does not exist.")
}
}

private fun isExternalStorageReadable(): Boolean {
val state = Environment.getExternalStorageState()
return Environment.MEDIA_MOUNTED == state || Environment.MEDIA_MOUNTED_READ_ONLY == state
}

fun Context.isPackageInstalled(
packageName: String,
userHandle: UserHandle = android.os.Process.myUserHandle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import com.github.droidworksstudio.ktx.backupSharedPreferences
import com.github.droidworksstudio.ktx.restoreSharedPreferences
//import com.github.droidworksstudio.ktx.backupSharedPreferences
//import com.github.droidworksstudio.ktx.restoreSharedPreferences
import com.github.droidworksstudio.ktx.showLongToast
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.github.droidworksstudio.launcher.Constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import com.github.droidworksstudio.ktx.resetDefaultLauncher
import com.github.droidworksstudio.ktx.restartApp
import com.github.droidworksstudio.ktx.showLongToast
import com.github.droidworksstudio.launcher.R
import com.github.droidworksstudio.launcher.databinding.FragmentSettingsBinding
import com.github.droidworksstudio.launcher.helper.AppHelper
Expand Down Expand Up @@ -154,12 +153,10 @@ class SettingsFragment : Fragment(),

binding.backupView.setOnClickListener {
appHelper.backupSharedPreferences(requireContext())
context.showLongToast(getString(R.string.settings_reload_app_backup))
}

binding.restoreView.setOnClickListener {
appHelper.restoreSharedPreferences(requireContext())
context.showLongToast(getString(R.string.settings_reload_app_restore))
restartApp()
}
}
Expand Down

0 comments on commit 8bbc3b9

Please sign in to comment.