Skip to content

Commit c68a6ee

Browse files
authored
Merge pull request #10436 from TeamNewPipe/fix/license-restore
Fix restoring software license dialog
2 parents 279fd23 + 94c1438 commit c68a6ee

77 files changed

Lines changed: 141 additions & 138 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/src/main/java/org/schabi/newpipe/about/LicenseFragment.kt

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
package org.schabi.newpipe.about
22

33
import android.os.Bundle
4+
import android.util.Base64
45
import android.view.LayoutInflater
56
import android.view.View
67
import android.view.ViewGroup
8+
import android.webkit.WebView
9+
import androidx.appcompat.app.AlertDialog
710
import androidx.core.os.bundleOf
811
import androidx.fragment.app.Fragment
12+
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
13+
import io.reactivex.rxjava3.core.Observable
914
import io.reactivex.rxjava3.disposables.CompositeDisposable
15+
import io.reactivex.rxjava3.disposables.Disposable
16+
import io.reactivex.rxjava3.schedulers.Schedulers
17+
import org.schabi.newpipe.BuildConfig
1018
import org.schabi.newpipe.R
1119
import org.schabi.newpipe.databinding.FragmentLicensesBinding
1220
import org.schabi.newpipe.databinding.ItemSoftwareComponentBinding
21+
import org.schabi.newpipe.util.Localization
22+
import org.schabi.newpipe.util.external_communication.ShareUtils
1323

1424
/**
1525
* Fragment containing the software licenses.
1626
*/
1727
class LicenseFragment : Fragment() {
1828
private lateinit var softwareComponents: Array<SoftwareComponent>
19-
private var activeLicense: License? = null
29+
private var activeSoftwareComponent: SoftwareComponent? = null
2030
private val compositeDisposable = CompositeDisposable()
2131

2232
override fun onCreate(savedInstanceState: Bundle?) {
2333
super.onCreate(savedInstanceState)
2434
softwareComponents = arguments?.getParcelableArray(ARG_COMPONENTS) as Array<SoftwareComponent>
25-
activeLicense = savedInstanceState?.getSerializable(LICENSE_KEY) as? License
35+
activeSoftwareComponent = savedInstanceState?.getSerializable(SOFTWARE_COMPONENT_KEY) as? SoftwareComponent
2636
// Sort components by name
2737
softwareComponents.sortBy { it.name }
2838
}
@@ -39,9 +49,8 @@ class LicenseFragment : Fragment() {
3949
): View {
4050
val binding = FragmentLicensesBinding.inflate(inflater, container, false)
4151
binding.licensesAppReadLicense.setOnClickListener {
42-
activeLicense = StandardLicenses.GPL3
4352
compositeDisposable.add(
44-
showLicense(activity, StandardLicenses.GPL3)
53+
showLicense(NEWPIPE_SOFTWARE_COMPONENT)
4554
)
4655
}
4756
for (component in softwareComponents) {
@@ -57,26 +66,70 @@ class LicenseFragment : Fragment() {
5766
val root: View = componentBinding.root
5867
root.tag = component
5968
root.setOnClickListener {
60-
activeLicense = component.license
6169
compositeDisposable.add(
62-
showLicense(activity, component)
70+
showLicense(component)
6371
)
6472
}
6573
binding.licensesSoftwareComponents.addView(root)
6674
registerForContextMenu(root)
6775
}
68-
activeLicense?.let { compositeDisposable.add(showLicense(activity, it)) }
76+
activeSoftwareComponent?.let { compositeDisposable.add(showLicense(it)) }
6977
return binding.root
7078
}
7179

7280
override fun onSaveInstanceState(savedInstanceState: Bundle) {
7381
super.onSaveInstanceState(savedInstanceState)
74-
activeLicense?.let { savedInstanceState.putSerializable(LICENSE_KEY, it) }
82+
activeSoftwareComponent?.let { savedInstanceState.putSerializable(SOFTWARE_COMPONENT_KEY, it) }
83+
}
84+
85+
private fun showLicense(
86+
softwareComponent: SoftwareComponent
87+
): Disposable {
88+
return if (context == null) {
89+
Disposable.empty()
90+
} else {
91+
val context = requireContext()
92+
activeSoftwareComponent = softwareComponent
93+
Observable.fromCallable { getFormattedLicense(context, softwareComponent.license) }
94+
.subscribeOn(Schedulers.io())
95+
.observeOn(AndroidSchedulers.mainThread())
96+
.subscribe { formattedLicense ->
97+
val webViewData = Base64.encodeToString(
98+
formattedLicense.toByteArray(), Base64.NO_PADDING
99+
)
100+
val webView = WebView(context)
101+
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")
102+
103+
Localization.assureCorrectAppLanguage(context)
104+
val builder = AlertDialog.Builder(requireContext())
105+
.setTitle(softwareComponent.name)
106+
.setView(webView)
107+
.setOnCancelListener { activeSoftwareComponent = null }
108+
.setOnDismissListener { activeSoftwareComponent = null }
109+
.setPositiveButton(R.string.done) { dialog, _ -> dialog.dismiss() }
110+
111+
if (softwareComponent != NEWPIPE_SOFTWARE_COMPONENT) {
112+
builder.setNeutralButton(R.string.open_website_license) { _, _ ->
113+
ShareUtils.openUrlInApp(requireContext(), softwareComponent.link)
114+
}
115+
}
116+
117+
builder.show()
118+
}
119+
}
75120
}
76121

77122
companion object {
78123
private const val ARG_COMPONENTS = "components"
79-
private const val LICENSE_KEY = "ACTIVE_LICENSE"
124+
private const val SOFTWARE_COMPONENT_KEY = "ACTIVE_SOFTWARE_COMPONENT"
125+
private val NEWPIPE_SOFTWARE_COMPONENT = SoftwareComponent(
126+
"NewPipe",
127+
"2014-2023",
128+
"Team NewPipe",
129+
"https://newpipe.net/",
130+
StandardLicenses.GPL3,
131+
BuildConfig.VERSION_NAME
132+
)
80133
fun newInstance(softwareComponents: Array<SoftwareComponent>): LicenseFragment {
81134
val fragment = LicenseFragment()
82135
fragment.arguments = bundleOf(ARG_COMPONENTS to softwareComponents)
Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
package org.schabi.newpipe.about
22

33
import android.content.Context
4-
import android.util.Base64
5-
import android.webkit.WebView
6-
import androidx.appcompat.app.AlertDialog
7-
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
8-
import io.reactivex.rxjava3.core.Observable
9-
import io.reactivex.rxjava3.disposables.Disposable
10-
import io.reactivex.rxjava3.schedulers.Schedulers
114
import org.schabi.newpipe.R
12-
import org.schabi.newpipe.util.Localization
135
import org.schabi.newpipe.util.ThemeHelper
14-
import org.schabi.newpipe.util.external_communication.ShareUtils
156
import java.io.IOException
167

178
/**
@@ -20,7 +11,7 @@ import java.io.IOException
2011
* @return String which contains a HTML formatted license page
2112
* styled according to the context's theme
2213
*/
23-
private fun getFormattedLicense(context: Context, license: License): String {
14+
fun getFormattedLicense(context: Context, license: License): String {
2415
try {
2516
return context.assets.open(license.filename).bufferedReader().use { it.readText() }
2617
// split the HTML file and insert the stylesheet into the HEAD of the file
@@ -34,7 +25,7 @@ private fun getFormattedLicense(context: Context, license: License): String {
3425
* @param context the Android context
3526
* @return String which is a CSS stylesheet according to the context's theme
3627
*/
37-
private fun getLicenseStylesheet(context: Context): String {
28+
fun getLicenseStylesheet(context: Context): String {
3829
val isLightTheme = ThemeHelper.isLightThemeSelected(context)
3930
val licenseBackgroundColor = getHexRGBColor(
4031
context, if (isLightTheme) R.color.light_license_background_color else R.color.dark_license_background_color
@@ -56,48 +47,6 @@ private fun getLicenseStylesheet(context: Context): String {
5647
* @param color the color number from R.color
5748
* @return a six characters long String with hexadecimal RGB values
5849
*/
59-
private fun getHexRGBColor(context: Context, color: Int): String {
50+
fun getHexRGBColor(context: Context, color: Int): String {
6051
return context.getString(color).substring(3)
6152
}
62-
63-
fun showLicense(context: Context?, component: SoftwareComponent): Disposable {
64-
return showLicense(context, component.license) {
65-
setPositiveButton(R.string.dismiss) { dialog, _ ->
66-
dialog.dismiss()
67-
}
68-
setNeutralButton(R.string.open_website_license) { _, _ ->
69-
ShareUtils.openUrlInApp(context!!, component.link)
70-
}
71-
}
72-
}
73-
74-
fun showLicense(context: Context?, license: License) = showLicense(context, license) {
75-
setPositiveButton(R.string.ok) { dialog, _ -> dialog.dismiss() }
76-
}
77-
78-
private fun showLicense(
79-
context: Context?,
80-
license: License,
81-
block: AlertDialog.Builder.() -> AlertDialog.Builder
82-
): Disposable {
83-
return if (context == null) {
84-
Disposable.empty()
85-
} else {
86-
Observable.fromCallable { getFormattedLicense(context, license) }
87-
.subscribeOn(Schedulers.io())
88-
.observeOn(AndroidSchedulers.mainThread())
89-
.subscribe { formattedLicense ->
90-
val webViewData =
91-
Base64.encodeToString(formattedLicense.toByteArray(), Base64.NO_PADDING)
92-
val webView = WebView(context)
93-
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")
94-
95-
Localization.assureCorrectAppLanguage(context)
96-
AlertDialog.Builder(context)
97-
.setTitle(license.name)
98-
.setView(webView)
99-
.block()
100-
.show()
101-
}
102-
}
103-
}

app/src/main/java/org/schabi/newpipe/about/SoftwareComponent.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.schabi.newpipe.about
22

33
import android.os.Parcelable
44
import kotlinx.parcelize.Parcelize
5+
import java.io.Serializable
56

67
@Parcelize
78
class SoftwareComponent
@@ -13,4 +14,4 @@ constructor(
1314
val link: String,
1415
val license: License,
1516
val version: String? = null
16-
) : Parcelable
17+
) : Parcelable, Serializable

app/src/main/res/menu/menu_recaptcha.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<item
66
android:id="@+id/menu_item_done"
77
android:icon="@drawable/ic_done"
8-
android:title="@string/recaptcha_done_button"
8+
android:title="@string/done"
99
app:showAsAction="always" />
1010
</menu>

app/src/main/res/values-ar/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@
466466
<string name="app_language_title">لغة التطبيق</string>
467467
<string name="systems_language">النظام الافتراضي</string>
468468
<string name="subtitle_activity_recaptcha">اضغط على \"تم\" عند حلها</string>
469-
<string name="recaptcha_done_button">منجز</string>
469+
<string name="done">منجز</string>
470470
<string name="videos_string">الفيديوهات</string>
471471
<plurals name="seconds">
472472
<item quantity="zero">%d ثانية</item>

app/src/main/res/values-az/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@
327327
<string name="no_dir_yet">Hələ endirmə qovluğu təyin edilməyib, indi standart endirmə qovluğu seç</string>
328328
<string name="title_activity_recaptcha">reCAPTCHA çağırışı</string>
329329
<string name="recaptcha_request_toast">reCAPTCHA sorğusu göndərildi</string>
330-
<string name="recaptcha_done_button">Bitdi</string>
330+
<string name="done">Bitdi</string>
331331
<string name="settings_file_replacement_character_summary">Etibarsız simvollar bu dəyərlə əvəz olunur</string>
332332
<string name="settings_file_replacement_character_title">Əvəzedici xarakter</string>
333333
<string name="charset_most_special_characters">Ən xüsusi simvollar</string>

app/src/main/res/values-b+ast/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@
420420
<string name="app_description">Un aplicación llibre pa ver/sentir plataformes de tresmisión n\'Android.</string>
421421
<string name="settings_file_replacement_character_title">Caráuteres de troquéu</string>
422422
<string name="settings_file_replacement_character_summary">Los caráuteres que nun son válidos van trocase por esti valor</string>
423-
<string name="recaptcha_done_button">Fecho</string>
423+
<string name="done">Fecho</string>
424424
<string name="subtitle_activity_recaptcha">Primi «Fecho» al resolvelu</string>
425425
<string name="one_item_deleted">Desanicióse 1 elementu.</string>
426426
<string name="no_available_dir">Defini una capeta de descargues dempués, nos axustes de l\'aplicación</string>

app/src/main/res/values-b+uz+Latn/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@
214214
<string name="settings_file_replacement_character_summary">Noto\'g\'ri belgilar ushbu qiymat bilan almashtiriladi</string>
215215
<string name="settings_file_charset_title">Fayl nomidagi ruxsat berilgan belgilar</string>
216216
<string name="settings_category_downloads_title">Yuklab olish</string>
217-
<string name="recaptcha_done_button">Bajarildi</string>
217+
<string name="done">Bajarildi</string>
218218
<string name="recaptcha_request_toast">reCAPTCHA muammosi so\'raldi</string>
219219
<string name="subtitle_activity_recaptcha">Hal etilganda \"Bajarildi\" tugmasini bosing</string>
220220
<string name="title_activity_recaptcha">reCAPTCHA muammosi</string>

app/src/main/res/values-be/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@
453453
<string name="no_playlist_bookmarked_yet">Няма закладак у плейлісце</string>
454454
<string name="select_a_playlist">Выберыце плэйліст</string>
455455
<string name="default_kiosk_page_summary">Кіёск па змаўчанні</string>
456-
<string name="recaptcha_done_button">Так</string>
456+
<string name="done">Так</string>
457457
<string name="subtitle_activity_recaptcha">Націсніце \"Так\" калі вырашана</string>
458458
<string name="infinite_videos">∞ відэа</string>
459459
<string name="more_than_100_videos">100+ відэа</string>

app/src/main/res/values-bg/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@
445445
<string name="detail_heart_img_view_description">Отбелязан със сърце от автора</string>
446446
<string name="conferences">Конференции</string>
447447
<string name="most_liked">Най-харесвани</string>
448-
<string name="recaptcha_done_button">Готово</string>
448+
<string name="done">Готово</string>
449449
<string name="comments_tab_description">Коментари</string>
450450
<string name="localization_changes_requires_app_restart">Езикът ще се смени след рестартиране на приложението</string>
451451
<string name="metadata_privacy_unlisted">Скрит</string>

0 commit comments

Comments
 (0)