Feat: Cleaned up backup system to save in a better location.
Signed-off-by: HeCodes2Much <wayne6324@gmail.com>
This commit is contained in:
@@ -12,7 +12,6 @@ import android.view.WindowManager
|
|||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.core.content.getSystemService
|
import androidx.core.content.getSystemService
|
||||||
import androidx.fragment.app.Fragment
|
|
||||||
|
|
||||||
fun Activity.getDisplayWidth(): Int {
|
fun Activity.getDisplayWidth(): Int {
|
||||||
val windowManager = getSystemService<WindowManager>() ?: return 0
|
val windowManager = getSystemService<WindowManager>() ?: return 0
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import android.graphics.Bitmap
|
|||||||
import android.graphics.drawable.AdaptiveIconDrawable
|
import android.graphics.drawable.AdaptiveIconDrawable
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import android.os.Environment
|
||||||
import android.os.UserHandle
|
import android.os.UserHandle
|
||||||
import android.os.UserManager
|
import android.os.UserManager
|
||||||
import android.provider.AlarmClock
|
import android.provider.AlarmClock
|
||||||
@@ -310,50 +311,63 @@ fun Context.isWorkProfileEnabled(): Boolean {
|
|||||||
|
|
||||||
fun Context.backupSharedPreferences(backupFileName: String) {
|
fun Context.backupSharedPreferences(backupFileName: String) {
|
||||||
val sharedPreferences: SharedPreferences =
|
val sharedPreferences: SharedPreferences =
|
||||||
this.getSharedPreferences(Constants.PREFS_FILENAME, 0)
|
this.getSharedPreferences(Constants.PREFS_FILENAME, Context.MODE_PRIVATE)
|
||||||
val allPrefs = sharedPreferences.all
|
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 {
|
try {
|
||||||
backupFile.bufferedWriter().use { writer ->
|
backupFile.bufferedWriter().use { writer ->
|
||||||
for ((key, value) in allPrefs) {
|
for ((key, value) in allPrefs) {
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
val line = when (value) {
|
val line = when (value) {
|
||||||
is Boolean -> "$key=${value}\n"
|
is Boolean, is Int, is Float, is Long, is String -> "$key=$value\n"
|
||||||
is Int -> "$key=${value}\n"
|
|
||||||
is Float -> "$key=${value}\n"
|
|
||||||
is Long -> "$key=${value}\n"
|
|
||||||
is String -> "$key=${value}\n"
|
|
||||||
is Set<*> -> "$key=${value.joinToString(",")}\n"
|
is Set<*> -> "$key=${value.joinToString(",")}\n"
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
if (line != null) {
|
line?.let {
|
||||||
writer.write(line)
|
writer.write(it)
|
||||||
println("Writing: $line")
|
|
||||||
} else {
|
|
||||||
println("Skipping unsupported type for key: $key")
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
println("Null value for key: $key")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println("Backup completed successfully.")
|
showLongToast("Backup completed successfully.")
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
e.printStackTrace()
|
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) {
|
fun Context.restoreSharedPreferences(backupFileName: String) {
|
||||||
val sharedPreferences: SharedPreferences =
|
val sharedPreferences: SharedPreferences =
|
||||||
this.getSharedPreferences(Constants.PREFS_FILENAME, 0)
|
this.getSharedPreferences(Constants.PREFS_FILENAME, Context.MODE_PRIVATE)
|
||||||
val editor = sharedPreferences.edit()
|
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()) {
|
if (backupFile.exists()) {
|
||||||
try {
|
try {
|
||||||
@@ -371,19 +385,23 @@ fun Context.restoreSharedPreferences(backupFileName: String) {
|
|||||||
value.contains(",") -> editor.putStringSet(key, value.split(",").toSet())
|
value.contains(",") -> editor.putStringSet(key, value.split(",").toSet())
|
||||||
else -> editor.putString(key, value)
|
else -> editor.putString(key, value)
|
||||||
}
|
}
|
||||||
println("Restoring: $key=$value")
|
|
||||||
}
|
}
|
||||||
editor.apply()
|
editor.apply()
|
||||||
println("Restore completed successfully.")
|
showLongToast("Restore completed successfully.")
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
println("Failed to restore SharedPreferences: ${e.message}")
|
showLongToast("Failed to restore SharedPreferences: ${e.message}")
|
||||||
}
|
}
|
||||||
} else {
|
} 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(
|
fun Context.isPackageInstalled(
|
||||||
packageName: String,
|
packageName: String,
|
||||||
userHandle: UserHandle = android.os.Process.myUserHandle()
|
userHandle: UserHandle = android.os.Process.myUserHandle()
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ import android.widget.TextView
|
|||||||
import androidx.appcompat.widget.LinearLayoutCompat
|
import androidx.appcompat.widget.LinearLayoutCompat
|
||||||
import com.github.droidworksstudio.ktx.backupSharedPreferences
|
import com.github.droidworksstudio.ktx.backupSharedPreferences
|
||||||
import com.github.droidworksstudio.ktx.restoreSharedPreferences
|
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.github.droidworksstudio.ktx.showLongToast
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import com.github.droidworksstudio.launcher.Constants
|
import com.github.droidworksstudio.launcher.Constants
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import androidx.navigation.NavController
|
|||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import com.github.droidworksstudio.ktx.resetDefaultLauncher
|
import com.github.droidworksstudio.ktx.resetDefaultLauncher
|
||||||
import com.github.droidworksstudio.ktx.restartApp
|
import com.github.droidworksstudio.ktx.restartApp
|
||||||
import com.github.droidworksstudio.ktx.showLongToast
|
|
||||||
import com.github.droidworksstudio.launcher.R
|
import com.github.droidworksstudio.launcher.R
|
||||||
import com.github.droidworksstudio.launcher.databinding.FragmentSettingsBinding
|
import com.github.droidworksstudio.launcher.databinding.FragmentSettingsBinding
|
||||||
import com.github.droidworksstudio.launcher.helper.AppHelper
|
import com.github.droidworksstudio.launcher.helper.AppHelper
|
||||||
@@ -154,12 +153,10 @@ class SettingsFragment : Fragment(),
|
|||||||
|
|
||||||
binding.backupView.setOnClickListener {
|
binding.backupView.setOnClickListener {
|
||||||
appHelper.backupSharedPreferences(requireContext())
|
appHelper.backupSharedPreferences(requireContext())
|
||||||
context.showLongToast(getString(R.string.settings_reload_app_backup))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.restoreView.setOnClickListener {
|
binding.restoreView.setOnClickListener {
|
||||||
appHelper.restoreSharedPreferences(requireContext())
|
appHelper.restoreSharedPreferences(requireContext())
|
||||||
context.showLongToast(getString(R.string.settings_reload_app_restore))
|
|
||||||
restartApp()
|
restartApp()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user