Merge pull request #1318 from Isira-Seneviratne/Kotlin_extensions

Use Kotlin extension functions.
This commit is contained in:
Bnyro 2022-09-18 11:51:40 +02:00 committed by GitHub
commit 24a4a170cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 114 deletions

View File

@ -7,7 +7,8 @@ import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle 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.R
import com.github.libretube.constants.DONATE_URL import com.github.libretube.constants.DONATE_URL
import com.github.libretube.constants.GITHUB_URL import com.github.libretube.constants.GITHUB_URL
@ -116,19 +117,10 @@ class AboutActivity : BaseActivity() {
} }
private fun showLicense() { private fun showLicense() {
val licenseString = assets val licenseHtml = assets.open("gpl3.html")
?.open("gpl3.html") .bufferedReader()
?.bufferedReader() .use { it.readText() }
.use { .parseAsHtml(HtmlCompat.FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH)
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())
}
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setPositiveButton(getString(R.string.okay)) { _, _ -> } .setPositiveButton(getString(R.string.okay)) { _, _ -> }

View File

@ -1,47 +1,33 @@
package com.github.libretube.util package com.github.libretube.util
import android.content.Context import android.content.Context
import android.content.SharedPreferences
import android.net.Uri import android.net.Uri
import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import java.io.FileInputStream import java.io.FileInputStream
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException
import java.io.ObjectInputStream import java.io.ObjectInputStream
import java.io.ObjectOutputStream import java.io.ObjectOutputStream
/** /**
* Backup and restore the preferences * Backup and restore the preferences
*/ */
class BackupHelper( class BackupHelper(private val context: Context) {
private val context: Context
) {
/** /**
* Backup the default shared preferences to a file * Backup the default shared preferences to a file
*/ */
fun backupSharedPreferences(uri: Uri?) { fun backupSharedPreferences(uri: Uri?) {
if (uri == null) return if (uri == null) return
var output: ObjectOutputStream? = null
try { try {
val fileDescriptor = context.contentResolver.openFileDescriptor(uri, "w")?.use {
context.contentResolver.openFileDescriptor(uri, "w")?.fileDescriptor ObjectOutputStream(FileOutputStream(it.fileDescriptor)).use { output ->
output = ObjectOutputStream(FileOutputStream(fileDescriptor)) val pref = PreferenceManager.getDefaultSharedPreferences(context)
val pref: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
// write all preference objects to the output file // write all preference objects to the output file
output.writeObject(pref.all) output.writeObject(pref.all)
}
}
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() 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") @Suppress("UNCHECKED_CAST")
fun restoreSharedPreferences(uri: Uri?) { fun restoreSharedPreferences(uri: Uri?) {
if (uri == null) return if (uri == null) return
var input: ObjectInputStream? = null
try { try {
val fileDescriptor = context.contentResolver.openFileDescriptor(uri, "r")?.use {
context.contentResolver.openFileDescriptor(uri, "r")?.fileDescriptor ObjectInputStream(FileInputStream(it.fileDescriptor)).use { input ->
input = ObjectInputStream(FileInputStream(fileDescriptor))
val editor = PreferenceManager.getDefaultSharedPreferences(context).edit()
// clear the previous settings
editor.clear()
// map all the preference keys and their values // map all the preference keys and their values
val entries = input.readObject() as Map<String, *> val entries = input.readObject() as Map<String, *>
PreferenceManager.getDefaultSharedPreferences(context).edit(commit = true) {
// clear the previous settings
clear()
// decide for each preference which type it is and save it to the preferences // decide for each preference which type it is and save it to the
// preferences
for ((key, value) in entries) { for ((key, value) in entries) {
if (value is Boolean) { when (value) {
editor.putBoolean(key, value) is Boolean -> putBoolean(key, value)
} else if (value is Float) { is Float -> putFloat(key, value)
editor.putFloat(key, value) is Int -> putInt(key, value)
} else if (value is Int) { is Long -> putLong(key, value)
editor.putInt(key, value) is String -> putString(key, value)
} else if (value is Long) { }
editor.putLong(key, value) }
} else if (value is String) editor.putString(key, value) }
}
} }
editor.commit()
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} finally {
try {
if (input != null) {
input.close()
}
} catch (ex: IOException) {
ex.printStackTrace()
}
} }
} }
} }

View File

@ -15,49 +15,39 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import java.io.BufferedReader
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.InputStreamReader
class ImportHelper(
private val activity: Activity
) {
class ImportHelper(private val activity: Activity) {
/** /**
* Import subscriptions by a file uri * Import subscriptions by a file uri
*/ */
fun importSubscriptions(uri: Uri?) { fun importSubscriptions(uri: Uri?) {
if (uri == null) return if (uri == null) return
try { try {
var channels = ArrayList<String>() val channels = when (activity.contentResolver.getType(uri)) {
val fileType = activity.contentResolver.getType(uri) "application/json" -> {
if (fileType == "application/json") {
// NewPipe subscriptions format // NewPipe subscriptions format
val mapper = ObjectMapper() val mapper = ObjectMapper()
val json = readRawTextFromUri(uri) val json = activity.contentResolver.openInputStream(uri)?.use {
it.bufferedReader().use { reader -> reader.readText() }
}.orEmpty()
val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java) val subscriptions = mapper.readValue(json, NewPipeSubscriptions::class.java)
channels = subscriptions.subscriptions?.map { subscriptions.subscriptions.orEmpty().map {
it.url?.replace("https://www.youtube.com/channel/", "")!! it.url!!.replace("https://www.youtube.com/channel/", "")
} as ArrayList<String> }
} else if ( }
fileType == "text/csv" || "text/csv", "text/comma-separated-values" -> {
fileType == "text/comma-separated-values"
) {
// import subscriptions from Google/YouTube Takeout // import subscriptions from Google/YouTube Takeout
val inputStream = activity.contentResolver.openInputStream(uri) activity.contentResolver.openInputStream(uri)?.use {
BufferedReader(InputStreamReader(inputStream)).use { reader -> it.bufferedReader().useLines { lines ->
var line: String? = reader.readLine() lines.map { line -> line.substringBefore(",") }
while (line != null) { .filter { channelId -> channelId.length == 24 }
val channelId = line.substringBefore(",") .toList()
if (channelId.length == 24) channels.add(channelId)
line = reader.readLine()
} }
}.orEmpty()
} }
inputStream?.close() else -> throw IllegalArgumentException("Unsupported file type")
} else {
throw IllegalArgumentException("Unsupported file type")
} }
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
@ -75,20 +65,6 @@ class ImportHelper(
} }
} }
private fun readRawTextFromUri(uri: Uri): String {
val stringBuilder = StringBuilder()
activity.contentResolver.openInputStream(uri)?.use { inputStream ->
BufferedReader(InputStreamReader(inputStream)).use { reader ->
var line: String? = reader.readLine()
while (line != null) {
stringBuilder.append(line)
line = reader.readLine()
}
}
}
return stringBuilder.toString()
}
/** /**
* write the text to the document * write the text to the document
*/ */