From 4fff5814575113ce394f5ad84bd41e4fa03f82bc Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 17 Sep 2022 14:59:07 +0530 Subject: [PATCH] Use the use(), edit() and parseAsHtml() extension functions. --- .../libretube/activities/AboutActivity.kt | 20 ++--- .../com/github/libretube/util/BackupHelper.kt | 83 +++++++------------ 2 files changed, 35 insertions(+), 68 deletions(-) diff --git a/app/src/main/java/com/github/libretube/activities/AboutActivity.kt b/app/src/main/java/com/github/libretube/activities/AboutActivity.kt index cf7132c67..7a8eacacc 100644 --- a/app/src/main/java/com/github/libretube/activities/AboutActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/AboutActivity.kt @@ -7,7 +7,8 @@ import android.content.Intent import android.net.Uri import android.os.Build import android.os.Bundle -import android.text.Html +import androidx.core.text.HtmlCompat +import androidx.core.text.parseAsHtml import com.github.libretube.R import com.github.libretube.constants.DONATE_URL import com.github.libretube.constants.GITHUB_URL @@ -116,19 +117,10 @@ class AboutActivity : BaseActivity() { } private fun showLicense() { - val licenseString = assets - ?.open("gpl3.html") - ?.bufferedReader() - .use { - it?.readText() - } - - @Suppress("DEPRECATION") - val licenseHtml = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - Html.fromHtml(licenseString.toString(), 1) - } else { - Html.fromHtml(licenseString.toString()) - } + val licenseHtml = assets.open("gpl3.html") + .bufferedReader() + .use { it.readText() } + .parseAsHtml(HtmlCompat.FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH) MaterialAlertDialogBuilder(this) .setPositiveButton(getString(R.string.okay)) { _, _ -> } diff --git a/app/src/main/java/com/github/libretube/util/BackupHelper.kt b/app/src/main/java/com/github/libretube/util/BackupHelper.kt index 55d96ecd2..97db72faa 100644 --- a/app/src/main/java/com/github/libretube/util/BackupHelper.kt +++ b/app/src/main/java/com/github/libretube/util/BackupHelper.kt @@ -1,47 +1,33 @@ package com.github.libretube.util import android.content.Context -import android.content.SharedPreferences import android.net.Uri +import androidx.core.content.edit import androidx.preference.PreferenceManager import java.io.FileInputStream import java.io.FileOutputStream -import java.io.IOException import java.io.ObjectInputStream import java.io.ObjectOutputStream /** * Backup and restore the preferences */ -class BackupHelper( - private val context: Context -) { - +class BackupHelper(private val context: Context) { /** * Backup the default shared preferences to a file */ fun backupSharedPreferences(uri: Uri?) { if (uri == null) return - var output: ObjectOutputStream? = null try { - val fileDescriptor = - context.contentResolver.openFileDescriptor(uri, "w")?.fileDescriptor - output = ObjectOutputStream(FileOutputStream(fileDescriptor)) - val pref: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) - // write all preference objects to the output file - output.writeObject(pref.all) + context.contentResolver.openFileDescriptor(uri, "w")?.use { + ObjectOutputStream(FileOutputStream(it.fileDescriptor)).use { output -> + val pref = PreferenceManager.getDefaultSharedPreferences(context) + // write all preference objects to the output file + output.writeObject(pref.all) + } + } } catch (e: Exception) { e.printStackTrace() - } finally { - try { - // close the outputStream - if (output != null) { - output.flush() - output.close() - } - } catch (ex: IOException) { - ex.printStackTrace() - } } } @@ -51,42 +37,31 @@ class BackupHelper( @Suppress("UNCHECKED_CAST") fun restoreSharedPreferences(uri: Uri?) { if (uri == null) return - var input: ObjectInputStream? = null try { - val fileDescriptor = - context.contentResolver.openFileDescriptor(uri, "r")?.fileDescriptor - input = ObjectInputStream(FileInputStream(fileDescriptor)) - val editor = PreferenceManager.getDefaultSharedPreferences(context).edit() + context.contentResolver.openFileDescriptor(uri, "r")?.use { + ObjectInputStream(FileInputStream(it.fileDescriptor)).use { input -> + // map all the preference keys and their values + val entries = input.readObject() as Map + PreferenceManager.getDefaultSharedPreferences(context).edit(commit = true) { + // clear the previous settings + clear() - // clear the previous settings - editor.clear() - - // map all the preference keys and their values - val entries = input.readObject() as Map - - // decide for each preference which type it is and save it to the preferences - for ((key, value) in entries) { - if (value is Boolean) { - editor.putBoolean(key, value) - } else if (value is Float) { - editor.putFloat(key, value) - } else if (value is Int) { - editor.putInt(key, value) - } else if (value is Long) { - editor.putLong(key, value) - } else if (value is String) editor.putString(key, value) + // decide for each preference which type it is and save it to the + // preferences + for ((key, value) in entries) { + when (value) { + is Boolean -> putBoolean(key, value) + is Float -> putFloat(key, value) + is Int -> putInt(key, value) + is Long -> putLong(key, value) + is String -> putString(key, value) + } + } + } + } } - editor.commit() } catch (e: Exception) { e.printStackTrace() - } finally { - try { - if (input != null) { - input.close() - } - } catch (ex: IOException) { - ex.printStackTrace() - } } } }