Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix restoring software license dialog #10436

Merged
merged 3 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 62 additions & 9 deletions app/src/main/java/org/schabi/newpipe/about/LicenseFragment.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
package org.schabi.newpipe.about

import android.os.Bundle
import android.util.Base64
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import androidx.appcompat.app.AlertDialog
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.schedulers.Schedulers
import org.schabi.newpipe.BuildConfig
import org.schabi.newpipe.R
import org.schabi.newpipe.databinding.FragmentLicensesBinding
import org.schabi.newpipe.databinding.ItemSoftwareComponentBinding
import org.schabi.newpipe.util.Localization
import org.schabi.newpipe.util.external_communication.ShareUtils

/**
* Fragment containing the software licenses.
*/
class LicenseFragment : Fragment() {
private lateinit var softwareComponents: Array<SoftwareComponent>
private var activeLicense: License? = null
private var activeSoftwareComponent: SoftwareComponent? = null
private val compositeDisposable = CompositeDisposable()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
softwareComponents = arguments?.getParcelableArray(ARG_COMPONENTS) as Array<SoftwareComponent>
activeLicense = savedInstanceState?.getSerializable(LICENSE_KEY) as? License
activeSoftwareComponent = savedInstanceState?.getSerializable(SOFTWARE_COMPONENT_KEY) as? SoftwareComponent
// Sort components by name
softwareComponents.sortBy { it.name }
}
Expand All @@ -39,9 +49,8 @@ class LicenseFragment : Fragment() {
): View {
val binding = FragmentLicensesBinding.inflate(inflater, container, false)
binding.licensesAppReadLicense.setOnClickListener {
activeLicense = StandardLicenses.GPL3
compositeDisposable.add(
showLicense(activity, StandardLicenses.GPL3)
showLicense(NEWPIPE_SOFTWARE_COMPONENT)
)
}
for (component in softwareComponents) {
Expand All @@ -57,26 +66,70 @@ class LicenseFragment : Fragment() {
val root: View = componentBinding.root
root.tag = component
root.setOnClickListener {
activeLicense = component.license
compositeDisposable.add(
showLicense(activity, component)
showLicense(component)
)
}
binding.licensesSoftwareComponents.addView(root)
registerForContextMenu(root)
}
activeLicense?.let { compositeDisposable.add(showLicense(activity, it)) }
activeSoftwareComponent?.let { compositeDisposable.add(showLicense(it)) }
return binding.root
}

override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
activeLicense?.let { savedInstanceState.putSerializable(LICENSE_KEY, it) }
activeSoftwareComponent?.let { savedInstanceState.putSerializable(SOFTWARE_COMPONENT_KEY, it) }
}

private fun showLicense(
softwareComponent: SoftwareComponent
): Disposable {
return if (context == null) {
Disposable.empty()
} else {
val context = requireContext()
activeSoftwareComponent = softwareComponent
Observable.fromCallable { getFormattedLicense(context, softwareComponent.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")

Localization.assureCorrectAppLanguage(context)
val builder = AlertDialog.Builder(requireContext())
.setTitle(softwareComponent.name)
.setView(webView)
.setOnCancelListener { activeSoftwareComponent = null }
.setOnDismissListener { activeSoftwareComponent = null }
.setPositiveButton(R.string.done) { dialog, _ -> dialog.dismiss() }

if (softwareComponent != NEWPIPE_SOFTWARE_COMPONENT) {
builder.setNeutralButton(R.string.open_website_license) { _, _ ->
ShareUtils.openUrlInApp(requireContext(), softwareComponent.link)
}
}

builder.show()
}
}
}

companion object {
private const val ARG_COMPONENTS = "components"
private const val LICENSE_KEY = "ACTIVE_LICENSE"
private const val SOFTWARE_COMPONENT_KEY = "ACTIVE_SOFTWARE_COMPONENT"
private val NEWPIPE_SOFTWARE_COMPONENT = SoftwareComponent(
"NewPipe",
"2014-2023",
"Team NewPipe",
"https://newpipe.net/",
StandardLicenses.GPL3,
BuildConfig.VERSION_NAME
)
fun newInstance(softwareComponents: Array<SoftwareComponent>): LicenseFragment {
val fragment = LicenseFragment()
fragment.arguments = bundleOf(ARG_COMPONENTS to softwareComponents)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
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
import org.schabi.newpipe.util.external_communication.ShareUtils
import java.io.IOException

/**
Expand All @@ -20,7 +11,7 @@ import java.io.IOException
* @return String which contains a HTML formatted license page
* styled according to the context's theme
*/
private fun getFormattedLicense(context: Context, license: License): String {
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
Expand All @@ -34,7 +25,7 @@ private fun getFormattedLicense(context: Context, license: License): String {
* @param context the Android context
* @return String which is a CSS stylesheet according to the context's theme
*/
private fun getLicenseStylesheet(context: Context): String {
fun getLicenseStylesheet(context: Context): String {
val isLightTheme = ThemeHelper.isLightThemeSelected(context)
val licenseBackgroundColor = getHexRGBColor(
context, if (isLightTheme) R.color.light_license_background_color else R.color.dark_license_background_color
Expand All @@ -56,48 +47,6 @@ private fun getLicenseStylesheet(context: Context): String {
* @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 {
fun getHexRGBColor(context: Context, color: Int): String {
return context.getString(color).substring(3)
}

fun showLicense(context: Context?, component: SoftwareComponent): Disposable {
return showLicense(context, component.license) {
setPositiveButton(R.string.dismiss) { dialog, _ ->
dialog.dismiss()
}
setNeutralButton(R.string.open_website_license) { _, _ ->
ShareUtils.openUrlInApp(context!!, component.link)
}
}
}

fun showLicense(context: Context?, license: License) = showLicense(context, license) {
setPositiveButton(R.string.ok) { dialog, _ -> dialog.dismiss() }
}

private fun showLicense(
context: Context?,
license: License,
block: AlertDialog.Builder.() -> AlertDialog.Builder
): 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")

Localization.assureCorrectAppLanguage(context)
AlertDialog.Builder(context)
.setTitle(license.name)
.setView(webView)
.block()
.show()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.schabi.newpipe.about

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import java.io.Serializable

@Parcelize
class SoftwareComponent
Expand All @@ -13,4 +14,4 @@ constructor(
val link: String,
val license: License,
val version: String? = null
) : Parcelable
) : Parcelable, Serializable
2 changes: 1 addition & 1 deletion app/src/main/res/menu/menu_recaptcha.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<item
android:id="@+id/menu_item_done"
android:icon="@drawable/ic_done"
android:title="@string/recaptcha_done_button"
android:title="@string/done"
app:showAsAction="always" />
</menu>
2 changes: 1 addition & 1 deletion app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@
<string name="app_language_title">لغة التطبيق</string>
<string name="systems_language">النظام الافتراضي</string>
<string name="subtitle_activity_recaptcha">اضغط على \"تم\" عند حلها</string>
<string name="recaptcha_done_button">منجز</string>
<string name="done">منجز</string>
<string name="videos_string">الفيديوهات</string>
<plurals name="seconds">
<item quantity="zero">%d ثانية</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-az/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
<string name="no_dir_yet">Hələ endirmə qovluğu təyin edilməyib, indi standart endirmə qovluğu seç</string>
<string name="title_activity_recaptcha">reCAPTCHA çağırışı</string>
<string name="recaptcha_request_toast">reCAPTCHA sorğusu göndərildi</string>
<string name="recaptcha_done_button">Bitdi</string>
<string name="done">Bitdi</string>
<string name="settings_file_replacement_character_summary">Etibarsız simvollar bu dəyərlə əvəz olunur</string>
<string name="settings_file_replacement_character_title">Əvəzedici xarakter</string>
<string name="charset_most_special_characters">Ən xüsusi simvollar</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-b+ast/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@
<string name="app_description">Un aplicación llibre pa ver/sentir plataformes de tresmisión n\'Android.</string>
<string name="settings_file_replacement_character_title">Caráuteres de troquéu</string>
<string name="settings_file_replacement_character_summary">Los caráuteres que nun son válidos van trocase por esti valor</string>
<string name="recaptcha_done_button">Fecho</string>
<string name="done">Fecho</string>
<string name="subtitle_activity_recaptcha">Primi «Fecho» al resolvelu</string>
<string name="one_item_deleted">Desanicióse 1 elementu.</string>
<string name="no_available_dir">Defini una capeta de descargues dempués, nos axustes de l\'aplicación</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-b+uz+Latn/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
<string name="settings_file_replacement_character_summary">Noto\'g\'ri belgilar ushbu qiymat bilan almashtiriladi</string>
<string name="settings_file_charset_title">Fayl nomidagi ruxsat berilgan belgilar</string>
<string name="settings_category_downloads_title">Yuklab olish</string>
<string name="recaptcha_done_button">Bajarildi</string>
<string name="done">Bajarildi</string>
<string name="recaptcha_request_toast">reCAPTCHA muammosi so\'raldi</string>
<string name="subtitle_activity_recaptcha">Hal etilganda \"Bajarildi\" tugmasini bosing</string>
<string name="title_activity_recaptcha">reCAPTCHA muammosi</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-be/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@
<string name="no_playlist_bookmarked_yet">Няма закладак у плейлісце</string>
<string name="select_a_playlist">Выберыце плэйліст</string>
<string name="default_kiosk_page_summary">Кіёск па змаўчанні</string>
<string name="recaptcha_done_button">Так</string>
<string name="done">Так</string>
<string name="subtitle_activity_recaptcha">Націсніце \"Так\" калі вырашана</string>
<string name="infinite_videos">∞ відэа</string>
<string name="more_than_100_videos">100+ відэа</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-bg/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@
<string name="detail_heart_img_view_description">Отбелязан със сърце от автора</string>
<string name="conferences">Конференции</string>
<string name="most_liked">Най-харесвани</string>
<string name="recaptcha_done_button">Готово</string>
<string name="done">Готово</string>
<string name="comments_tab_description">Коментари</string>
<string name="localization_changes_requires_app_restart">Езикът ще се смени след рестартиране на приложението</string>
<string name="metadata_privacy_unlisted">Скрит</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-bn-rBD/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
<string name="peertube_instance_url_title">পিয়ার টিউব এর ইন্সটান্স সমূহ</string>
<string name="grid">ছক</string>
<string name="local">স্থানীয়</string>
<string name="recaptcha_done_button">হয়েছে</string>
<string name="done">হয়েছে</string>
<string name="events">ইভেন্টগুলো</string>
<string name="settings_category_updates_title">আপডেট</string>
<string name="minimize_on_exit_none_description">কোনোটি না</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-bn-rIN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
<string name="contribution_title">যোগদান</string>
<string name="title_activity_about">নিউ পাইপ এর সম্বন্ধে</string>
<string name="charset_letters_and_digits">শব্দ ও নম্বর</string>
<string name="recaptcha_done_button">হয়েছে</string>
<string name="done">হয়েছে</string>
<string name="no_comments">কোন মন্তব্য নেই</string>
<string name="no_subscribers">কোন সাবস্ক্রাইবার নেই</string>
<string name="no_streams_available_download">ডাউন লোড এর জন্য কোন স্ট্রিম নেই</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-bn/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<string name="title_activity_about">নিউপাইপ এর সম্বন্ধে</string>
<string name="charset_letters_and_digits">শব্দ ও নম্বর</string>
<string name="settings_category_downloads_title">ডাউনলোড</string>
<string name="recaptcha_done_button">হয়েছে</string>
<string name="done">হয়েছে</string>
<string name="recaptcha_request_toast">reCAPTCHA চ্যালেঞ্জ অনুরোধ করা হয়েছে</string>
<string name="title_activity_recaptcha">reCAPTCHA চ্যালেঞ্জ</string>
<string name="one_item_deleted">একটি আইটেম ডিলিট হয়েছে।</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-ca/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@
<string name="local">Local</string>
<string name="no_playlist_bookmarked_yet">Encara no hi ha llistes de reproducció favorites</string>
<string name="select_a_playlist">Sel·leccioneu una llista de reproducció</string>
<string name="recaptcha_done_button">Fet</string>
<string name="done">Fet</string>
<string name="msg_calculating_hash">Calculant-ne la funció de verificació</string>
<string name="error_report_open_github_notice">Si us plau, comproveu abans si el problema que ha causat aquesta fallada ja ha estat informat. Els tiquets per duplicat fan que perdem temps que podríem aprofitar resolent-los.</string>
<string name="error_report_open_issue_button_text">Avisa del problema a GitHub</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-ckb/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
<string name="download_already_pending">دابه‌زاندنێكی دیكه‌ له‌ نۆره‌دایه‌ بەهەمان ناو</string>
<string name="feed_group_dialog_empty_selection">هیچ بەژدارییەک دیار نەکراوە</string>
<string name="feed_group_dialog_empty_name">ناوی کۆمەڵە بەتاڵە</string>
<string name="recaptcha_done_button">كرا</string>
<string name="done">كرا</string>
<string name="detail_likes_img_view_description">بەدڵه‌كان</string>
<string name="popup_remember_size_pos_summary">بیرهاتنه‌وه‌ی كۆتا قه‌باره‌ و شوێنی په‌نجه‌ره‌</string>
<string name="create">سازکردن</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@
<string name="app_language_title">Jazyk aplikace</string>
<string name="systems_language">Jazyk systému</string>
<string name="subtitle_activity_recaptcha">Po vyřešení klepněte na „Hotovo“</string>
<string name="recaptcha_done_button">Hotovo</string>
<string name="done">Hotovo</string>
<string name="videos_string">Videa</string>
<plurals name="seconds">
<item quantity="one">%d vteřina</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-da/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@
<string name="most_liked">Mest likede</string>
<string name="error_unable_to_load_comments">Kunne ikke indlæse kommentarer</string>
<string name="default_kiosk_page_summary">Standard Kiosk</string>
<string name="recaptcha_done_button">Færdig</string>
<string name="done">Færdig</string>
<string name="subtitle_activity_recaptcha">Tryk på \"Færdig\" når den er løst</string>
<string name="no_comments">Ingen kommentarer</string>
<string name="infinite_videos">∞ videoer</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@
<string name="app_language_title">Sprache der App</string>
<string name="systems_language">Systemstandard</string>
<string name="subtitle_activity_recaptcha">„Fertig“ drücken, wenn es gelöst wurde</string>
<string name="recaptcha_done_button">Fertig</string>
<string name="done">Fertig</string>
<string name="videos_string">Videos</string>
<plurals name="seconds">
<item quantity="one">%d Sekunde</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-el/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@
<string name="title_activity_play_queue">Αναπαραγωγή ουράς</string>
<string name="no_playlist_bookmarked_yet">Δεν υπάρχουν σελιδοδείκτες λίστας αναπαραγωγής ακόμα</string>
<string name="select_a_playlist">Επιλέξτε μια λίστα αναπαραγωγής</string>
<string name="recaptcha_done_button">Τέλος</string>
<string name="done">Τέλος</string>
<string name="subtitle_activity_recaptcha">Πατήστε «Τέλος» όταν επιλυθεί</string>
<string name="infinite_videos">∞ βίντεο</string>
<string name="more_than_100_videos">100+ βίντεο</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-eo/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@
<string name="app_language_title">Preferata aplingvo</string>
<string name="systems_language">Sistemnormo</string>
<string name="subtitle_activity_recaptcha">Premu “Finita” kiam solvita</string>
<string name="recaptcha_done_button">Finita</string>
<string name="done">Finita</string>
<plurals name="seconds">
<item quantity="one">%d sekundo</item>
<item quantity="other">%d sekundoj</item>
Expand Down
Loading