2021-03-30 01:06:32 +02:00
|
|
|
package org.schabi.newpipe.about
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.util.Base64
|
|
|
|
import android.webkit.WebView
|
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
|
|
|
import io.reactivex.rxjava3.core.Observable
|
|
|
|
import io.reactivex.rxjava3.disposables.Disposable
|
|
|
|
import io.reactivex.rxjava3.schedulers.Schedulers
|
|
|
|
import org.schabi.newpipe.R
|
|
|
|
import org.schabi.newpipe.util.Localization
|
|
|
|
import org.schabi.newpipe.util.ThemeHelper
|
2021-06-11 12:19:29 +02:00
|
|
|
import org.schabi.newpipe.util.external_communication.ShareUtils
|
2021-03-30 01:06:32 +02:00
|
|
|
import java.io.IOException
|
|
|
|
|
2022-07-23 01:50:06 +02:00
|
|
|
/**
|
|
|
|
* @param context the context to use
|
|
|
|
* @param license the license
|
|
|
|
* @return String which contains a HTML formatted license page
|
|
|
|
* styled according to the context's theme
|
|
|
|
*/
|
|
|
|
private fun getFormattedLicense(context: Context, license: License): String {
|
|
|
|
try {
|
|
|
|
return context.assets.open(license.filename).bufferedReader().use { it.readText() }
|
|
|
|
// split the HTML file and insert the stylesheet into the HEAD of the file
|
|
|
|
.replace("</head>", "<style>${getLicenseStylesheet(context)}</style></head>")
|
|
|
|
} catch (e: IOException) {
|
|
|
|
throw IllegalArgumentException("Could not get license file: ${license.filename}", e)
|
2021-03-30 01:06:32 +02:00
|
|
|
}
|
2022-07-23 01:50:06 +02:00
|
|
|
}
|
2021-03-30 01:06:32 +02:00
|
|
|
|
2022-07-23 01:50:06 +02:00
|
|
|
/**
|
|
|
|
* @param context the Android context
|
|
|
|
* @return String which is a CSS stylesheet according to the context's theme
|
|
|
|
*/
|
|
|
|
private fun getLicenseStylesheet(context: Context): String {
|
|
|
|
val isLightTheme = ThemeHelper.isLightThemeSelected(context)
|
2022-07-23 02:30:08 +02:00
|
|
|
val licenseBackgroundColor = getHexRGBColor(
|
|
|
|
context, if (isLightTheme) R.color.light_license_background_color else R.color.dark_license_background_color
|
|
|
|
)
|
|
|
|
val licenseTextColor = getHexRGBColor(
|
|
|
|
context, if (isLightTheme) R.color.light_license_text_color else R.color.dark_license_text_color
|
|
|
|
)
|
|
|
|
val youtubePrimaryColor = getHexRGBColor(
|
|
|
|
context, if (isLightTheme) R.color.light_youtube_primary_color else R.color.dark_youtube_primary_color
|
|
|
|
)
|
|
|
|
return "body{padding:12px 15px;margin:0;background:#$licenseBackgroundColor;color:#$licenseTextColor}" +
|
|
|
|
"a[href]{color:#$youtubePrimaryColor}pre{white-space:pre-wrap}"
|
2022-07-23 01:50:06 +02:00
|
|
|
}
|
2021-03-30 01:06:32 +02:00
|
|
|
|
2022-07-23 01:50:06 +02:00
|
|
|
/**
|
|
|
|
* Cast R.color to a hexadecimal color value.
|
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @param color the color number from R.color
|
|
|
|
* @return a six characters long String with hexadecimal RGB values
|
|
|
|
*/
|
|
|
|
private fun getHexRGBColor(context: Context, color: Int): String {
|
|
|
|
return context.getString(color).substring(3)
|
|
|
|
}
|
2021-03-30 01:06:32 +02:00
|
|
|
|
2022-07-23 01:50:06 +02:00
|
|
|
fun showLicense(context: Context?, component: SoftwareComponent): Disposable {
|
2022-07-23 02:08:33 +02:00
|
|
|
return showLicense(context, component.license) {
|
|
|
|
setPositiveButton(R.string.dismiss) { dialog, _ ->
|
2022-07-23 01:50:06 +02:00
|
|
|
dialog.dismiss()
|
|
|
|
}
|
2022-07-23 02:08:33 +02:00
|
|
|
setNeutralButton(R.string.open_website_license) { _, _ ->
|
2022-07-23 01:50:06 +02:00
|
|
|
ShareUtils.openUrlInBrowser(context!!, component.link)
|
2022-03-18 18:48:07 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-23 01:50:06 +02:00
|
|
|
}
|
2022-03-18 18:48:07 +01:00
|
|
|
|
2022-07-23 02:08:33 +02:00
|
|
|
fun showLicense(context: Context?, license: License) = showLicense(context, license) {
|
|
|
|
setPositiveButton(R.string.ok) { dialog, _ -> dialog.dismiss() }
|
|
|
|
}
|
|
|
|
|
2022-07-23 01:50:06 +02:00
|
|
|
private fun showLicense(
|
|
|
|
context: Context?,
|
|
|
|
license: License,
|
2022-07-23 02:08:33 +02:00
|
|
|
block: AlertDialog.Builder.() -> AlertDialog.Builder
|
2022-07-23 01:50:06 +02:00
|
|
|
): Disposable {
|
|
|
|
return if (context == null) {
|
|
|
|
Disposable.empty()
|
|
|
|
} else {
|
|
|
|
Observable.fromCallable { getFormattedLicense(context, license) }
|
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe { formattedLicense ->
|
|
|
|
val webViewData =
|
|
|
|
Base64.encodeToString(formattedLicense.toByteArray(), Base64.NO_PADDING)
|
|
|
|
val webView = WebView(context)
|
|
|
|
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")
|
2022-03-18 18:48:07 +01:00
|
|
|
|
2022-07-23 02:08:33 +02:00
|
|
|
Localization.assureCorrectAppLanguage(context)
|
|
|
|
AlertDialog.Builder(context)
|
|
|
|
.setTitle(license.name)
|
|
|
|
.setView(webView)
|
|
|
|
.block()
|
|
|
|
.show()
|
2022-07-23 01:50:06 +02:00
|
|
|
}
|
2021-05-16 14:02:49 +02:00
|
|
|
}
|
2021-03-30 01:06:32 +02:00
|
|
|
}
|