Skip to content

Commit 1ae8387

Browse files
Fix(backup): Add exception handling for restore operations
This commit adds `try-catch` blocks to handle potential `FileNotFoundException` and `SecurityException` when restoring backups and themes. If a file cannot be opened or permission is denied during the restore process from a URI, the exception is now caught, its stack trace is printed, and a toast message is displayed to the user ("Selected file could not be opened" or "Permission denied"). This prevents the application from crashing and provides user feedback in case of an error.
1 parent a1f5d68 commit 1ae8387

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

app/src/main/java/com/github/droidworksstudio/mlauncher/MainActivity.kt

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import com.squareup.moshi.Types
3838
import org.xmlpull.v1.XmlPullParser
3939
import java.io.BufferedReader
4040
import java.io.File
41+
import java.io.FileNotFoundException
4142
import java.io.FileOutputStream
4243
import java.io.InputStream
4344
import java.io.InputStreamReader
@@ -201,21 +202,29 @@ class MainActivity : AppCompatActivity() {
201202
performFullRestore = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
202203
if (result.resultCode == RESULT_OK) {
203204
result.data?.data?.let { uri ->
204-
applicationContext.contentResolver.openInputStream(uri).use { inputStream ->
205-
val stringBuilder = StringBuilder()
206-
BufferedReader(InputStreamReader(inputStream)).use { reader ->
207-
var line: String? = reader.readLine()
208-
while (line != null) {
209-
stringBuilder.append(line)
210-
line = reader.readLine()
205+
try {
206+
applicationContext.contentResolver.openInputStream(uri).use { inputStream ->
207+
val stringBuilder = StringBuilder()
208+
BufferedReader(InputStreamReader(inputStream)).use { reader ->
209+
var line: String? = reader.readLine()
210+
while (line != null) {
211+
stringBuilder.append(line)
212+
line = reader.readLine()
213+
}
211214
}
212-
}
213215

214-
val string = stringBuilder.toString()
215-
val prefs = Prefs(applicationContext)
216-
prefs.clear()
217-
prefs.loadFromString(string)
218-
AppReloader.restartApp(applicationContext)
216+
val string = stringBuilder.toString()
217+
val prefs = Prefs(applicationContext)
218+
prefs.clear()
219+
prefs.loadFromString(string)
220+
AppReloader.restartApp(applicationContext)
221+
}
222+
} catch (e: FileNotFoundException) {
223+
e.printStackTrace()
224+
showLongToast("Selected file could not be opened")
225+
} catch (e: SecurityException) {
226+
e.printStackTrace()
227+
showLongToast("Permission denied")
219228
}
220229
}
221230
}
@@ -272,19 +281,27 @@ class MainActivity : AppCompatActivity() {
272281
}
273282

274283
result.data?.data?.let { uri ->
275-
applicationContext.contentResolver.openInputStream(uri).use { inputStream ->
276-
val stringBuilder = StringBuilder()
277-
BufferedReader(InputStreamReader(inputStream)).use { reader ->
278-
var line: String? = reader.readLine()
279-
while (line != null) {
280-
stringBuilder.append(line)
281-
line = reader.readLine()
284+
try {
285+
applicationContext.contentResolver.openInputStream(uri).use { inputStream ->
286+
val stringBuilder = StringBuilder()
287+
BufferedReader(InputStreamReader(inputStream)).use { reader ->
288+
var line: String? = reader.readLine()
289+
while (line != null) {
290+
stringBuilder.append(line)
291+
line = reader.readLine()
292+
}
282293
}
283-
}
284294

285-
val string = stringBuilder.toString()
286-
val prefs = Prefs(applicationContext)
287-
prefs.loadFromTheme(string)
295+
val string = stringBuilder.toString()
296+
val prefs = Prefs(applicationContext)
297+
prefs.loadFromTheme(string)
298+
}
299+
} catch (e: FileNotFoundException) {
300+
e.printStackTrace()
301+
showLongToast("Selected file could not be opened")
302+
} catch (e: SecurityException) {
303+
e.printStackTrace()
304+
showLongToast("Permission denied")
288305
}
289306
}
290307
}

0 commit comments

Comments
 (0)