Skip to content

Commit bc4a0a5

Browse files
committed
Clean up the about package a bit
1 parent e29aaaf commit bc4a0a5

2 files changed

Lines changed: 62 additions & 58 deletions

File tree

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import androidx.appcompat.app.AppCompatActivity
1010
import androidx.fragment.app.Fragment
1111
import androidx.fragment.app.FragmentActivity
1212
import androidx.viewpager2.adapter.FragmentStateAdapter
13-
import com.google.android.material.tabs.TabLayout
1413
import com.google.android.material.tabs.TabLayoutMediator
1514
import org.schabi.newpipe.BuildConfig
1615
import org.schabi.newpipe.R
@@ -21,30 +20,28 @@ import org.schabi.newpipe.util.ThemeHelper
2120
import org.schabi.newpipe.util.external_communication.ShareUtils
2221

2322
class AboutActivity : AppCompatActivity() {
23+
2424
override fun onCreate(savedInstanceState: Bundle?) {
2525
Localization.assureCorrectAppLanguage(this)
2626
super.onCreate(savedInstanceState)
2727
ThemeHelper.setTheme(this)
2828
title = getString(R.string.title_activity_about)
29+
2930
val aboutBinding = ActivityAboutBinding.inflate(layoutInflater)
3031
setContentView(aboutBinding.root)
3132
setSupportActionBar(aboutBinding.aboutToolbar)
32-
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
33+
supportActionBar?.setDisplayHomeAsUpEnabled(true)
34+
3335
// Create the adapter that will return a fragment for each of the three
3436
// primary sections of the activity.
3537
val mAboutStateAdapter = AboutStateAdapter(this)
36-
3738
// Set up the ViewPager with the sections adapter.
3839
aboutBinding.aboutViewPager2.adapter = mAboutStateAdapter
3940
TabLayoutMediator(
4041
aboutBinding.aboutTabLayout,
4142
aboutBinding.aboutViewPager2
42-
) { tab: TabLayout.Tab, position: Int ->
43-
when (position) {
44-
POS_ABOUT -> tab.setText(R.string.tab_about)
45-
POS_LICENSE -> tab.setText(R.string.tab_licenses)
46-
else -> throw IllegalArgumentException("Unknown position for ViewPager2")
47-
}
43+
) { tab, position ->
44+
tab.setText(mAboutStateAdapter.getPageTitle(position))
4845
}.attach()
4946
}
5047

@@ -75,13 +72,14 @@ class AboutActivity : AppCompatActivity() {
7572
container: ViewGroup?,
7673
savedInstanceState: Bundle?
7774
): View {
78-
val aboutBinding = FragmentAboutBinding.inflate(inflater, container, false)
79-
aboutBinding.aboutAppVersion.text = BuildConfig.VERSION_NAME
80-
aboutBinding.aboutGithubLink.openLink(R.string.github_url)
81-
aboutBinding.aboutDonationLink.openLink(R.string.donation_url)
82-
aboutBinding.aboutWebsiteLink.openLink(R.string.website_url)
83-
aboutBinding.aboutPrivacyPolicyLink.openLink(R.string.privacy_policy_url)
84-
return aboutBinding.root
75+
FragmentAboutBinding.inflate(inflater, container, false).apply {
76+
aboutAppVersion.text = BuildConfig.VERSION_NAME
77+
aboutGithubLink.openLink(R.string.github_url)
78+
aboutDonationLink.openLink(R.string.donation_url)
79+
aboutWebsiteLink.openLink(R.string.website_url)
80+
aboutPrivacyPolicyLink.openLink(R.string.privacy_policy_url)
81+
return root
82+
}
8583
}
8684
}
8785

@@ -90,17 +88,29 @@ class AboutActivity : AppCompatActivity() {
9088
* one of the sections/tabs/pages.
9189
*/
9290
private class AboutStateAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
91+
private val posAbout = 0
92+
private val posLicense = 1
93+
private val totalCount = 2
94+
9395
override fun createFragment(position: Int): Fragment {
9496
return when (position) {
95-
POS_ABOUT -> AboutFragment()
96-
POS_LICENSE -> LicenseFragment.newInstance(SOFTWARE_COMPONENTS)
97+
posAbout -> AboutFragment()
98+
posLicense -> LicenseFragment.newInstance(SOFTWARE_COMPONENTS)
9799
else -> throw IllegalArgumentException("Unknown position for ViewPager2")
98100
}
99101
}
100102

101103
override fun getItemCount(): Int {
102104
// Show 2 total pages.
103-
return TOTAL_COUNT
105+
return totalCount
106+
}
107+
108+
fun getPageTitle(position: Int): Int {
109+
return when (position) {
110+
posAbout -> R.string.tab_about
111+
posLicense -> R.string.tab_licenses
112+
else -> throw IllegalArgumentException("Unknown position for ViewPager2")
113+
}
104114
}
105115
}
106116

@@ -187,8 +197,5 @@ class AboutActivity : AppCompatActivity() {
187197
"https://github.com/ByteHamster/SearchPreference", StandardLicenses.MIT
188198
),
189199
)
190-
private const val POS_ABOUT = 0
191-
private const val POS_LICENSE = 1
192-
private const val TOTAL_COUNT = 2
193200
}
194201
}

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

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

33
import android.content.Context
44
import android.util.Base64
5+
import android.view.View
56
import android.webkit.WebView
67
import androidx.appcompat.app.AlertDialog
78
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
@@ -87,61 +88,57 @@ object LicenseFragmentHelper {
8788
return context.getString(color).substring(3)
8889
}
8990

90-
@JvmStatic
9191
fun showLicense(context: Context?, license: License): Disposable {
9292
return if (context == null) {
9393
Disposable.empty()
9494
} else {
9595
Observable.fromCallable { getFormattedLicense(context, license) }
9696
.subscribeOn(Schedulers.io())
9797
.observeOn(AndroidSchedulers.mainThread())
98-
.subscribe { formattedLicense: String ->
99-
val webViewData = Base64.encodeToString(
100-
formattedLicense
101-
.toByteArray(StandardCharsets.UTF_8),
102-
Base64.NO_PADDING
103-
)
104-
val webView = WebView(context)
105-
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")
106-
val alert = AlertDialog.Builder(context)
107-
alert.setTitle(license.name)
108-
alert.setView(webView)
109-
Localization.assureCorrectAppLanguage(context)
110-
alert.setNegativeButton(
111-
context.getString(R.string.ok)
112-
) { dialog, _ -> dialog.dismiss() }
113-
alert.show()
98+
.subscribe { formattedLicense ->
99+
AlertDialog.Builder(context).apply {
100+
setTitle(license.name)
101+
setView(loadLicense(context, formattedLicense))
102+
Localization.assureCorrectAppLanguage(context)
103+
setPositiveButton(R.string.ok) { dialog, _ ->
104+
dialog.dismiss()
105+
}
106+
show()
107+
}
114108
}
115109
}
116110
}
117-
@JvmStatic
111+
118112
fun showLicense(context: Context?, component: SoftwareComponent): Disposable {
119113
return if (context == null) {
120114
Disposable.empty()
121115
} else {
122116
Observable.fromCallable { getFormattedLicense(context, component.license) }
123117
.subscribeOn(Schedulers.io())
124118
.observeOn(AndroidSchedulers.mainThread())
125-
.subscribe { formattedLicense: String ->
126-
val webViewData = Base64.encodeToString(
127-
formattedLicense
128-
.toByteArray(StandardCharsets.UTF_8),
129-
Base64.NO_PADDING
130-
)
131-
val webView = WebView(context)
132-
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")
133-
val alert = AlertDialog.Builder(context)
134-
alert.setTitle(component.license.name)
135-
alert.setView(webView)
136-
Localization.assureCorrectAppLanguage(context)
137-
alert.setPositiveButton(
138-
R.string.dismiss
139-
) { dialog, _ -> dialog.dismiss() }
140-
alert.setNeutralButton(R.string.open_website_license) { _, _ ->
141-
ShareUtils.openUrlInBrowser(context, component.link)
119+
.subscribe { formattedLicense ->
120+
AlertDialog.Builder(context).apply {
121+
setTitle(component.license.name)
122+
setView(loadLicense(context, formattedLicense))
123+
Localization.assureCorrectAppLanguage(context)
124+
setPositiveButton(R.string.dismiss) { dialog, _ ->
125+
dialog.dismiss()
126+
}
127+
setNeutralButton(R.string.open_website_license) { _, _ ->
128+
ShareUtils.openUrlInBrowser(context, component.link)
129+
}
130+
show()
142131
}
143-
alert.show()
144132
}
145133
}
146134
}
135+
136+
private fun loadLicense(context: Context, formattedLicense: String): View {
137+
val webViewData = Base64.encodeToString(
138+
formattedLicense.toByteArray(StandardCharsets.UTF_8), Base64.NO_PADDING
139+
)
140+
return WebView(context).apply {
141+
loadData(webViewData, "text/html; charset=UTF-8", "base64")
142+
}
143+
}
147144
}

0 commit comments

Comments
 (0)