Skip to content

Commit f6d6797

Browse files
Fix: Fixed a few errors with the backup system.
1 parent 2c29c11 commit f6d6797

File tree

6 files changed

+52
-38
lines changed

6 files changed

+52
-38
lines changed

app/src/main/java/com/github/droidworksstudio/launcher/data/entities/AppInfo.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ data class AppInfo(
3333
var createTime: String,
3434

3535
@ColumnInfo(name = "app_order")
36-
var appOrder: Int = -1
36+
var appOrder: Int = -1,
3737
)

app/src/main/java/com/github/droidworksstudio/launcher/helper/AppHelper.kt

+18-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import android.os.Vibrator
1616
import android.os.VibratorManager
1717
import android.text.SpannableStringBuilder
1818
import android.text.style.ImageSpan
19-
import android.util.JsonReader
2019
import android.util.Log
2120
import android.util.TypedValue
2221
import android.view.Gravity
@@ -38,11 +37,12 @@ import com.github.droidworksstudio.launcher.helper.weather.WeatherResponse
3837
import com.github.droidworksstudio.launcher.utils.Constants
3938
import com.github.droidworksstudio.launcher.utils.WeatherApiService
4039
import com.google.gson.Gson
40+
import com.google.gson.JsonSyntaxException
4141
import com.google.gson.reflect.TypeToken
42+
import com.google.gson.stream.JsonReader
4243
import kotlinx.coroutines.flow.first
4344
import retrofit2.Retrofit
4445
import retrofit2.converter.gson.GsonConverterFactory
45-
import java.io.StringReader
4646
import java.net.UnknownHostException
4747
import java.text.SimpleDateFormat
4848
import java.util.Calendar
@@ -383,27 +383,35 @@ class AppHelper @Inject constructor() {
383383
try {
384384
// Open an InputStream from the selected Uri
385385
context.contentResolver.openInputStream(uri)?.use { inputStream ->
386-
// Read the content from the InputStream
387-
val jsonString = inputStream.bufferedReader().use { it.readText() }
388386

389-
// Create a JsonReader with lenient parsing
390-
val jsonReader = JsonReader(StringReader(jsonString))
391-
jsonReader.isLenient = true // Enable lenient mode
387+
// Create a JsonReader for lenient parsing
388+
val jsonReader = JsonReader(inputStream.bufferedReader())
389+
jsonReader.isLenient = true // Enable lenient mode for parsing
392390

393-
// Convert JSON to List<AppInfo>
391+
// Create Gson instance
394392
val gson = Gson()
393+
394+
// Define the type for the List<AppInfo>
395395
val type = object : TypeToken<List<AppInfo>>() {}.type
396-
val appInfoList: List<AppInfo> = gson.fromJson(jsonString, type)
396+
397+
// Deserialize the JSON into a List<AppInfo>
398+
val appInfoList: List<AppInfo> = gson.fromJson(jsonReader, type)
397399

398400
// Clear all apps first
399401
resetDatabase(dao)
402+
400403
// Reinsert data into the database
401-
dao.restoreAll(appInfoList)
404+
dao.restoreAll(appInfoList) // Execute the method
402405
} ?: throw Exception("Failed to open input stream from URI")
403406

407+
} catch (e: JsonSyntaxException) {
408+
// Handle Gson parsing issues (e.g., malformed JSON)
409+
e.printStackTrace()
404410
} catch (e: Exception) {
411+
// Catch other exceptions such as InputStream opening issues
405412
e.printStackTrace()
406413
}
414+
407415
}
408416

409417
suspend fun resetDatabase(dao: AppInfoDAO) {

app/src/main/java/com/github/droidworksstudio/launcher/helper/AppReloader.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ object AppReloader {
1212
val componentName = intent?.component
1313
val mainIntent = Intent.makeRestartActivityTask(componentName)
1414
mainIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
15-
mainIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
16-
15+
mainIntent.flags += Intent.FLAG_ACTIVITY_NEW_TASK
16+
1717
// Delay the restart slightly to ensure all current activities are finished
1818
Handler(Looper.getMainLooper()).postDelayed({
1919
context.startActivity(mainIntent)
20-
Runtime.getRuntime().exit(0)
2120
}, 250)
2221
}
2322
}

app/src/main/java/com/github/droidworksstudio/launcher/helper/PreferenceHelper.kt

+28-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.droidworksstudio.launcher.helper
22

3+
import android.annotation.SuppressLint
34
import android.content.Context
45
import android.content.SharedPreferences
56
import android.content.res.Configuration
@@ -10,7 +11,9 @@ import com.google.gson.Gson
1011
import com.google.gson.GsonBuilder
1112
import com.google.gson.JsonDeserializer
1213
import com.google.gson.reflect.TypeToken
14+
import com.google.gson.stream.JsonReader
1315
import dagger.hilt.android.qualifiers.ApplicationContext
16+
import java.io.StringReader
1417
import javax.inject.Inject
1518

1619
class PreferenceHelper @Inject constructor(@ApplicationContext context: Context) {
@@ -344,6 +347,7 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
344347
return Gson().toJson(all)
345348
}
346349

350+
@SuppressLint("CommitPrefEdits")
347351
fun loadFromString(json: String) {
348352
val editor = prefs.edit()
349353
// Custom deserializer to handle numbers properly
@@ -391,25 +395,34 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
391395
map
392396
}).create()
393397

394-
val all: HashMap<String, Any?> = gson.fromJson(json, object : TypeToken<HashMap<String, Any?>>() {}.type)
395-
396-
for ((key, value) in all) {
397-
when (value) {
398-
is String -> editor.putString(key, value)
399-
is Boolean -> editor.putBoolean(key, value)
400-
is Int -> editor.putInt(key, value)
401-
is Float -> editor.putFloat(key, value)
402-
is MutableSet<*> -> {
403-
val list = value.filterIsInstance<String>().toSet()
404-
editor.putStringSet(key, list)
405-
}
398+
try {
399+
// Read JSON string with lenient mode enabled
400+
val jsonReader = JsonReader(StringReader(json))
401+
jsonReader.isLenient = true // Enable lenient parsing for malformed JSON
402+
403+
// Deserialize the JSON to HashMap
404+
val all: HashMap<String, Any?> = gson.fromJson(jsonReader, object : TypeToken<HashMap<String, Any?>>() {}.type)
405+
406+
for ((key, value) in all) {
407+
when (value) {
408+
is String -> editor.putString(key, value)
409+
is Boolean -> editor.putBoolean(key, value)
410+
is Int -> editor.putInt(key, value)
411+
is Float -> editor.putFloat(key, value)
412+
is MutableSet<*> -> {
413+
val list = value.filterIsInstance<String>().toSet()
414+
editor.putStringSet(key, list)
415+
}
406416

407-
else -> {
408-
Log.d("backup error", "$key | $value")
417+
else -> {
418+
Log.d("backup error", "$key | $value")
419+
}
409420
}
410421
}
422+
editor.apply()
423+
} catch (e: Exception) {
424+
e.printStackTrace()
411425
}
412-
editor.apply()
413426
}
414427

415428
fun clear() {

app/src/main/java/com/github/droidworksstudio/launcher/ui/settings/SettingsAdvancedFragment.kt

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import android.content.Context
44
import android.content.Intent
55
import android.net.Uri
66
import android.os.Bundle
7-
import android.os.Handler
8-
import android.os.Looper
97
import android.provider.Settings
108
import android.view.LayoutInflater
119
import android.view.View
@@ -100,9 +98,7 @@ class SettingsAdvancedFragment : Fragment(),
10098
}
10199

102100
restartLauncher.setOnClickListener {
103-
Handler(Looper.getMainLooper()).postDelayed({
104-
AppReloader.restartApp(context)
105-
}, 500)
101+
AppReloader.restartApp(context)
106102
}
107103

108104
backupRestore.setOnClickListener {
@@ -171,10 +167,8 @@ class SettingsAdvancedFragment : Fragment(),
171167
preferenceHelper.clearAll(context)
172168
lifecycleScope.launch {
173169
appHelper.resetDatabase(appDAO)
174-
}
175-
Handler(Looper.getMainLooper()).postDelayed({
176170
AppReloader.restartApp(context)
177-
}, 500)
171+
}
178172
}
179173

180174
private fun dismissDialogs() {

gradle/libs.versions.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[versions]
2-
agp = "8.7.2"
2+
agp = "8.7.3"
33
kotlin = "2.0.0"
44
coreKtx = "1.15.0"
55
junit = "4.13.2"

0 commit comments

Comments
 (0)