From e206a26a85caeef92d201dbc7b8f9e8524e8565d Mon Sep 17 00:00:00 2001 From: TobiGr Date: Thu, 21 Sep 2023 10:31:04 +0200 Subject: [PATCH] Restore license dialog buttons to open the SoftwareComponent's website Do not keep the active License but the active SoftwareComponent. --- .../schabi/newpipe/about/LicenseFragment.kt | 66 ++++++++++--------- .../schabi/newpipe/about/SoftwareComponent.kt | 3 +- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.kt b/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.kt index a560f407a..10e825efd 100644 --- a/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.kt +++ b/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.kt @@ -14,6 +14,7 @@ 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 @@ -25,13 +26,13 @@ import org.schabi.newpipe.util.external_communication.ShareUtils */ class LicenseFragment : Fragment() { private lateinit var softwareComponents: Array - 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 - activeLicense = savedInstanceState?.getSerializable(LICENSE_KEY) as? License + activeSoftwareComponent = savedInstanceState?.getSerializable(SOFTWARE_COMPONENT_KEY) as? SoftwareComponent // Sort components by name softwareComponents.sortBy { it.name } } @@ -48,9 +49,8 @@ class LicenseFragment : Fragment() { ): View { val binding = FragmentLicensesBinding.inflate(inflater, container, false) binding.licensesAppReadLicense.setOnClickListener { - activeLicense = StandardLicenses.GPL3 compositeDisposable.add( - showLicense(StandardLicenses.GPL3) + showLicense(NEWPIPE_SOFTWARE_COMPONENT) ) } for (component in softwareComponents) { @@ -66,7 +66,6 @@ class LicenseFragment : Fragment() { val root: View = componentBinding.root root.tag = component root.setOnClickListener { - activeLicense = component.license compositeDisposable.add( showLicense(component) ) @@ -74,39 +73,24 @@ class LicenseFragment : Fragment() { binding.licensesSoftwareComponents.addView(root) registerForContextMenu(root) } - activeLicense?.let { compositeDisposable.add(showLicense(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) } - } - - private fun showLicense(component: SoftwareComponent): Disposable { - return showLicense(component.license) { - setPositiveButton(R.string.dismiss) { dialog, _ -> - dialog.dismiss() - } - setNeutralButton(R.string.open_website_license) { _, _ -> - ShareUtils.openUrlInApp(requireContext(), component.link) - } - } - } - - private fun showLicense(license: License) = showLicense(license) { - setPositiveButton(R.string.ok) { dialog, _ -> dialog.dismiss() } + activeSoftwareComponent?.let { savedInstanceState.putSerializable(SOFTWARE_COMPONENT_KEY, it) } } private fun showLicense( - license: License, - block: AlertDialog.Builder.() -> AlertDialog.Builder + softwareComponent: SoftwareComponent ): Disposable { return if (context == null) { Disposable.empty() } else { val context = requireContext() - Observable.fromCallable { getFormattedLicense(context, license) } + activeSoftwareComponent = softwareComponent + Observable.fromCallable { getFormattedLicense(context, softwareComponent.license) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { formattedLicense -> @@ -117,20 +101,38 @@ class LicenseFragment : Fragment() { webView.loadData(webViewData, "text/html; charset=UTF-8", "base64") Localization.assureCorrectAppLanguage(context) - AlertDialog.Builder(requireContext()) - .setTitle(license.name) + val builder = AlertDialog.Builder(requireContext()) + .setTitle(softwareComponent.name) .setView(webView) - .setOnCancelListener { activeLicense = null } - .setOnDismissListener { activeLicense = null } - .block() - .show() + .setOnCancelListener { activeSoftwareComponent = null } + .setOnDismissListener { activeSoftwareComponent = null } + if (softwareComponent == NEWPIPE_SOFTWARE_COMPONENT) { + builder.setPositiveButton(R.string.ok) { dialog, _ -> dialog.dismiss() } + } else { + builder.setPositiveButton(R.string.dismiss) { dialog, _ -> + dialog.dismiss() + } + .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): LicenseFragment { val fragment = LicenseFragment() fragment.arguments = bundleOf(ARG_COMPONENTS to softwareComponents) diff --git a/app/src/main/java/org/schabi/newpipe/about/SoftwareComponent.kt b/app/src/main/java/org/schabi/newpipe/about/SoftwareComponent.kt index 354e8fef7..262641caa 100644 --- a/app/src/main/java/org/schabi/newpipe/about/SoftwareComponent.kt +++ b/app/src/main/java/org/schabi/newpipe/about/SoftwareComponent.kt @@ -2,6 +2,7 @@ package org.schabi.newpipe.about import android.os.Parcelable import kotlinx.parcelize.Parcelize +import java.io.Serializable @Parcelize class SoftwareComponent @@ -13,4 +14,4 @@ constructor( val link: String, val license: License, val version: String? = null -) : Parcelable +) : Parcelable, Serializable