Fix: Fixed a few errors with the backup system.

This commit is contained in:
HeCodes2Much
2024-12-02 20:00:43 +00:00
parent 2c29c11016
commit f6d6797964
6 changed files with 51 additions and 37 deletions

View File

@@ -33,5 +33,5 @@ data class AppInfo(
var createTime: String,
@ColumnInfo(name = "app_order")
var appOrder: Int = -1
var appOrder: Int = -1,
)

View File

@@ -16,7 +16,6 @@ import android.os.Vibrator
import android.os.VibratorManager
import android.text.SpannableStringBuilder
import android.text.style.ImageSpan
import android.util.JsonReader
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
@@ -38,11 +37,12 @@ import com.github.droidworksstudio.launcher.helper.weather.WeatherResponse
import com.github.droidworksstudio.launcher.utils.Constants
import com.github.droidworksstudio.launcher.utils.WeatherApiService
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import kotlinx.coroutines.flow.first
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.StringReader
import java.net.UnknownHostException
import java.text.SimpleDateFormat
import java.util.Calendar
@@ -383,27 +383,35 @@ class AppHelper @Inject constructor() {
try {
// Open an InputStream from the selected Uri
context.contentResolver.openInputStream(uri)?.use { inputStream ->
// Read the content from the InputStream
val jsonString = inputStream.bufferedReader().use { it.readText() }
// Create a JsonReader with lenient parsing
val jsonReader = JsonReader(StringReader(jsonString))
jsonReader.isLenient = true // Enable lenient mode
// Create a JsonReader for lenient parsing
val jsonReader = JsonReader(inputStream.bufferedReader())
jsonReader.isLenient = true // Enable lenient mode for parsing
// Convert JSON to List<AppInfo>
// Create Gson instance
val gson = Gson()
// Define the type for the List<AppInfo>
val type = object : TypeToken<List<AppInfo>>() {}.type
val appInfoList: List<AppInfo> = gson.fromJson(jsonString, type)
// Deserialize the JSON into a List<AppInfo>
val appInfoList: List<AppInfo> = gson.fromJson(jsonReader, type)
// Clear all apps first
resetDatabase(dao)
// Reinsert data into the database
dao.restoreAll(appInfoList)
dao.restoreAll(appInfoList) // Execute the method
} ?: throw Exception("Failed to open input stream from URI")
} catch (e: JsonSyntaxException) {
// Handle Gson parsing issues (e.g., malformed JSON)
e.printStackTrace()
} catch (e: Exception) {
// Catch other exceptions such as InputStream opening issues
e.printStackTrace()
}
}
suspend fun resetDatabase(dao: AppInfoDAO) {

View File

@@ -12,12 +12,11 @@ object AppReloader {
val componentName = intent?.component
val mainIntent = Intent.makeRestartActivityTask(componentName)
mainIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
mainIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
mainIntent.flags += Intent.FLAG_ACTIVITY_NEW_TASK
// Delay the restart slightly to ensure all current activities are finished
Handler(Looper.getMainLooper()).postDelayed({
context.startActivity(mainIntent)
Runtime.getRuntime().exit(0)
}, 250)
}
}

View File

@@ -1,5 +1,6 @@
package com.github.droidworksstudio.launcher.helper
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.content.res.Configuration
@@ -10,7 +11,9 @@ import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonDeserializer
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import dagger.hilt.android.qualifiers.ApplicationContext
import java.io.StringReader
import javax.inject.Inject
class PreferenceHelper @Inject constructor(@ApplicationContext context: Context) {
@@ -344,6 +347,7 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
return Gson().toJson(all)
}
@SuppressLint("CommitPrefEdits")
fun loadFromString(json: String) {
val editor = prefs.edit()
// Custom deserializer to handle numbers properly
@@ -391,25 +395,34 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
map
}).create()
val all: HashMap<String, Any?> = gson.fromJson(json, object : TypeToken<HashMap<String, Any?>>() {}.type)
try {
// Read JSON string with lenient mode enabled
val jsonReader = JsonReader(StringReader(json))
jsonReader.isLenient = true // Enable lenient parsing for malformed JSON
for ((key, value) in all) {
when (value) {
is String -> editor.putString(key, value)
is Boolean -> editor.putBoolean(key, value)
is Int -> editor.putInt(key, value)
is Float -> editor.putFloat(key, value)
is MutableSet<*> -> {
val list = value.filterIsInstance<String>().toSet()
editor.putStringSet(key, list)
}
// Deserialize the JSON to HashMap
val all: HashMap<String, Any?> = gson.fromJson(jsonReader, object : TypeToken<HashMap<String, Any?>>() {}.type)
else -> {
Log.d("backup error", "$key | $value")
for ((key, value) in all) {
when (value) {
is String -> editor.putString(key, value)
is Boolean -> editor.putBoolean(key, value)
is Int -> editor.putInt(key, value)
is Float -> editor.putFloat(key, value)
is MutableSet<*> -> {
val list = value.filterIsInstance<String>().toSet()
editor.putStringSet(key, list)
}
else -> {
Log.d("backup error", "$key | $value")
}
}
}
editor.apply()
} catch (e: Exception) {
e.printStackTrace()
}
editor.apply()
}
fun clear() {

View File

@@ -4,8 +4,6 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
@@ -100,9 +98,7 @@ class SettingsAdvancedFragment : Fragment(),
}
restartLauncher.setOnClickListener {
Handler(Looper.getMainLooper()).postDelayed({
AppReloader.restartApp(context)
}, 500)
AppReloader.restartApp(context)
}
backupRestore.setOnClickListener {
@@ -171,10 +167,8 @@ class SettingsAdvancedFragment : Fragment(),
preferenceHelper.clearAll(context)
lifecycleScope.launch {
appHelper.resetDatabase(appDAO)
}
Handler(Looper.getMainLooper()).postDelayed({
AppReloader.restartApp(context)
}, 500)
}
}
private fun dismissDialogs() {