@@ -343,151 +343,6 @@ fun Context.isWorkProfileEnabled(): Boolean {
343
343
}
344
344
}
345
345
346
- fun Context.backupSharedPreferences (backupFileNames : Array <String >) {
347
- // Check if external storage is writable
348
- if (! isExternalStorageWritable()) {
349
- showLongToast(" External storage is not writable." )
350
- return
351
- }
352
-
353
- val downloadDir = Environment .getExternalStoragePublicDirectory(Environment .DIRECTORY_DOWNLOADS )
354
- val backupDir = File (downloadDir, Constants .PACKAGE_NAME )
355
-
356
- // Ensure the directory exists, create it if necessary
357
- if (! backupDir.exists()) {
358
- backupDir.mkdirs()
359
- }
360
-
361
- for (backupFileName in backupFileNames) {
362
- val sharedPreferences: SharedPreferences =
363
- this .getSharedPreferences(backupFileName, Context .MODE_PRIVATE )
364
- val allPrefs = sharedPreferences.all
365
-
366
- val backupFile = File (backupDir, " $backupFileName .xml" )
367
-
368
- try {
369
- backupFile.bufferedWriter().use { writer ->
370
- writer.write(" <?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n " )
371
- writer.write(" <map>\n " )
372
-
373
- // Loop through all preferences
374
- for ((key, value) in allPrefs) {
375
- if (value != null ) {
376
- val valueString = value.toString().replace(" '" , " '" )
377
- val line = when (value) {
378
- is Boolean , is Int , is Float , is Long , is String -> " \t <${value::class .simpleName!! .lowercase()} name='$key ' value='$valueString ' />\n "
379
- else -> null
380
- }
381
- line?.let {
382
- writer.write(it)
383
- }
384
- }
385
- }
386
-
387
- writer.write(" </map>" )
388
- }
389
- showLongToast(" Backup for $backupFileName completed successfully." )
390
- } catch (e: IOException ) {
391
- e.printStackTrace()
392
- showLongToast(" Failed to backup SharedPreferences $backupFileName : ${e.message} " )
393
- }
394
- }
395
- }
396
-
397
-
398
- private fun isExternalStorageWritable (): Boolean {
399
- return Environment .getExternalStorageState() == Environment .MEDIA_MOUNTED
400
- }
401
-
402
- fun Context.restoreSharedPreferences (backupFileNames : Array <String >) {
403
- // Check if external storage is readable
404
- if (! isExternalStorageReadable()) {
405
- showLongToast(" External storage is not readable." )
406
- return
407
- }
408
-
409
- val downloadDir = Environment .getExternalStoragePublicDirectory(Environment .DIRECTORY_DOWNLOADS )
410
- val backupDir = File (downloadDir, Constants .PACKAGE_NAME )
411
-
412
- for (backupFileName in backupFileNames) {
413
- val backupFile = File (backupDir, " $backupFileName .xml" )
414
-
415
- if (backupFile.exists()) {
416
- try {
417
- FileInputStream (backupFile).use { fileInputStream ->
418
- val factory = XmlPullParserFactory .newInstance()
419
- factory.isNamespaceAware = true
420
- val xpp = factory.newPullParser()
421
- xpp.setInput(fileInputStream, null )
422
-
423
- var eventType = xpp.eventType
424
- var key: String? = null
425
- var value: Any? = null
426
-
427
- while (eventType != XmlPullParser .END_DOCUMENT ) {
428
- when (eventType) {
429
- XmlPullParser .START_TAG -> {
430
- when (xpp.name) {
431
- " boolean" , " int" , " float" , " long" , " string" -> {
432
- key = xpp.getAttributeValue(null , " name" )
433
- val valueString = xpp.getAttributeValue(null , " value" )
434
- value = when (xpp.name) {
435
- " boolean" -> valueString.toBoolean()
436
- " int" -> valueString.toInt()
437
- " float" -> valueString.toFloat()
438
- " long" -> valueString.toLong()
439
- else -> valueString
440
- }
441
- }
442
- }
443
- }
444
-
445
- XmlPullParser .END_TAG -> {
446
- when (xpp.name) {
447
- " boolean" , " int" , " float" , " long" , " string" -> {
448
- if (key != null && value != null ) {
449
- saveToSharedPreferences(backupFileName, key, value)
450
- key = null
451
- value = null
452
- }
453
- }
454
- }
455
- }
456
- }
457
- eventType = xpp.next()
458
- }
459
- }
460
- showLongToast(" Restore for $backupFileName completed successfully." )
461
- } catch (e: Exception ) {
462
- Log .e(" Exception" , e.toString())
463
- showLongToast(" Failed to restore SharedPreferences $backupFileName : ${e.message} " )
464
- }
465
- } else {
466
- showLongToast(" Backup file for $backupFileName does not exist." )
467
- }
468
- }
469
- }
470
-
471
- fun Context.saveToSharedPreferences (prefsFileName : String , key : String , value : Any ) {
472
- val sharedPreferences: SharedPreferences = this .getSharedPreferences(prefsFileName, Context .MODE_PRIVATE )
473
- val editor = sharedPreferences.edit()
474
-
475
- when (value) {
476
- is Boolean -> editor.putBoolean(key, value)
477
- is Int -> editor.putInt(key, value)
478
- is Float -> editor.putFloat(key, value)
479
- is Long -> editor.putLong(key, value)
480
- is String -> editor.putString(key, value)
481
- }
482
-
483
- editor.apply ()
484
- }
485
-
486
- private fun isExternalStorageReadable (): Boolean {
487
- val state = Environment .getExternalStorageState()
488
- return Environment .MEDIA_MOUNTED == state || Environment .MEDIA_MOUNTED_READ_ONLY == state
489
- }
490
-
491
346
fun Context.hasInternetPermission (): Boolean {
492
347
val permission = Manifest .permission.INTERNET
493
348
val result = ContextCompat .checkSelfPermission(this , permission)
0 commit comments