1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 08:30:44 +00:00

Clean up the about package a bit

This commit is contained in:
TacoTheDank 2022-03-18 13:18:23 -04:00
parent e29aaaf162
commit bc4a0a575c
2 changed files with 62 additions and 58 deletions

View File

@ -10,7 +10,6 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator import com.google.android.material.tabs.TabLayoutMediator
import org.schabi.newpipe.BuildConfig import org.schabi.newpipe.BuildConfig
import org.schabi.newpipe.R import org.schabi.newpipe.R
@ -21,30 +20,28 @@ import org.schabi.newpipe.util.ThemeHelper
import org.schabi.newpipe.util.external_communication.ShareUtils import org.schabi.newpipe.util.external_communication.ShareUtils
class AboutActivity : AppCompatActivity() { class AboutActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
Localization.assureCorrectAppLanguage(this) Localization.assureCorrectAppLanguage(this)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
ThemeHelper.setTheme(this) ThemeHelper.setTheme(this)
title = getString(R.string.title_activity_about) title = getString(R.string.title_activity_about)
val aboutBinding = ActivityAboutBinding.inflate(layoutInflater) val aboutBinding = ActivityAboutBinding.inflate(layoutInflater)
setContentView(aboutBinding.root) setContentView(aboutBinding.root)
setSupportActionBar(aboutBinding.aboutToolbar) setSupportActionBar(aboutBinding.aboutToolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
// Create the adapter that will return a fragment for each of the three // Create the adapter that will return a fragment for each of the three
// primary sections of the activity. // primary sections of the activity.
val mAboutStateAdapter = AboutStateAdapter(this) val mAboutStateAdapter = AboutStateAdapter(this)
// Set up the ViewPager with the sections adapter. // Set up the ViewPager with the sections adapter.
aboutBinding.aboutViewPager2.adapter = mAboutStateAdapter aboutBinding.aboutViewPager2.adapter = mAboutStateAdapter
TabLayoutMediator( TabLayoutMediator(
aboutBinding.aboutTabLayout, aboutBinding.aboutTabLayout,
aboutBinding.aboutViewPager2 aboutBinding.aboutViewPager2
) { tab: TabLayout.Tab, position: Int -> ) { tab, position ->
when (position) { tab.setText(mAboutStateAdapter.getPageTitle(position))
POS_ABOUT -> tab.setText(R.string.tab_about)
POS_LICENSE -> tab.setText(R.string.tab_licenses)
else -> throw IllegalArgumentException("Unknown position for ViewPager2")
}
}.attach() }.attach()
} }
@ -75,13 +72,14 @@ class AboutActivity : AppCompatActivity() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
val aboutBinding = FragmentAboutBinding.inflate(inflater, container, false) FragmentAboutBinding.inflate(inflater, container, false).apply {
aboutBinding.aboutAppVersion.text = BuildConfig.VERSION_NAME aboutAppVersion.text = BuildConfig.VERSION_NAME
aboutBinding.aboutGithubLink.openLink(R.string.github_url) aboutGithubLink.openLink(R.string.github_url)
aboutBinding.aboutDonationLink.openLink(R.string.donation_url) aboutDonationLink.openLink(R.string.donation_url)
aboutBinding.aboutWebsiteLink.openLink(R.string.website_url) aboutWebsiteLink.openLink(R.string.website_url)
aboutBinding.aboutPrivacyPolicyLink.openLink(R.string.privacy_policy_url) aboutPrivacyPolicyLink.openLink(R.string.privacy_policy_url)
return aboutBinding.root return root
}
} }
} }
@ -90,17 +88,29 @@ class AboutActivity : AppCompatActivity() {
* one of the sections/tabs/pages. * one of the sections/tabs/pages.
*/ */
private class AboutStateAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) { private class AboutStateAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
private val posAbout = 0
private val posLicense = 1
private val totalCount = 2
override fun createFragment(position: Int): Fragment { override fun createFragment(position: Int): Fragment {
return when (position) { return when (position) {
POS_ABOUT -> AboutFragment() posAbout -> AboutFragment()
POS_LICENSE -> LicenseFragment.newInstance(SOFTWARE_COMPONENTS) posLicense -> LicenseFragment.newInstance(SOFTWARE_COMPONENTS)
else -> throw IllegalArgumentException("Unknown position for ViewPager2") else -> throw IllegalArgumentException("Unknown position for ViewPager2")
} }
} }
override fun getItemCount(): Int { override fun getItemCount(): Int {
// Show 2 total pages. // Show 2 total pages.
return TOTAL_COUNT return totalCount
}
fun getPageTitle(position: Int): Int {
return when (position) {
posAbout -> R.string.tab_about
posLicense -> R.string.tab_licenses
else -> throw IllegalArgumentException("Unknown position for ViewPager2")
}
} }
} }
@ -187,8 +197,5 @@ class AboutActivity : AppCompatActivity() {
"https://github.com/ByteHamster/SearchPreference", StandardLicenses.MIT "https://github.com/ByteHamster/SearchPreference", StandardLicenses.MIT
), ),
) )
private const val POS_ABOUT = 0
private const val POS_LICENSE = 1
private const val TOTAL_COUNT = 2
} }
} }

View File

@ -2,6 +2,7 @@ package org.schabi.newpipe.about
import android.content.Context import android.content.Context
import android.util.Base64 import android.util.Base64
import android.view.View
import android.webkit.WebView import android.webkit.WebView
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
@ -87,7 +88,6 @@ object LicenseFragmentHelper {
return context.getString(color).substring(3) return context.getString(color).substring(3)
} }
@JvmStatic
fun showLicense(context: Context?, license: License): Disposable { fun showLicense(context: Context?, license: License): Disposable {
return if (context == null) { return if (context == null) {
Disposable.empty() Disposable.empty()
@ -95,26 +95,20 @@ object LicenseFragmentHelper {
Observable.fromCallable { getFormattedLicense(context, license) } Observable.fromCallable { getFormattedLicense(context, license) }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe { formattedLicense: String -> .subscribe { formattedLicense ->
val webViewData = Base64.encodeToString( AlertDialog.Builder(context).apply {
formattedLicense setTitle(license.name)
.toByteArray(StandardCharsets.UTF_8), setView(loadLicense(context, formattedLicense))
Base64.NO_PADDING
)
val webView = WebView(context)
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")
val alert = AlertDialog.Builder(context)
alert.setTitle(license.name)
alert.setView(webView)
Localization.assureCorrectAppLanguage(context) Localization.assureCorrectAppLanguage(context)
alert.setNegativeButton( setPositiveButton(R.string.ok) { dialog, _ ->
context.getString(R.string.ok) dialog.dismiss()
) { dialog, _ -> dialog.dismiss() } }
alert.show() show()
} }
} }
} }
@JvmStatic }
fun showLicense(context: Context?, component: SoftwareComponent): Disposable { fun showLicense(context: Context?, component: SoftwareComponent): Disposable {
return if (context == null) { return if (context == null) {
Disposable.empty() Disposable.empty()
@ -122,26 +116,29 @@ object LicenseFragmentHelper {
Observable.fromCallable { getFormattedLicense(context, component.license) } Observable.fromCallable { getFormattedLicense(context, component.license) }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe { formattedLicense: String -> .subscribe { formattedLicense ->
val webViewData = Base64.encodeToString( AlertDialog.Builder(context).apply {
formattedLicense setTitle(component.license.name)
.toByteArray(StandardCharsets.UTF_8), setView(loadLicense(context, formattedLicense))
Base64.NO_PADDING
)
val webView = WebView(context)
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64")
val alert = AlertDialog.Builder(context)
alert.setTitle(component.license.name)
alert.setView(webView)
Localization.assureCorrectAppLanguage(context) Localization.assureCorrectAppLanguage(context)
alert.setPositiveButton( setPositiveButton(R.string.dismiss) { dialog, _ ->
R.string.dismiss dialog.dismiss()
) { dialog, _ -> dialog.dismiss() } }
alert.setNeutralButton(R.string.open_website_license) { _, _ -> setNeutralButton(R.string.open_website_license) { _, _ ->
ShareUtils.openUrlInBrowser(context, component.link) ShareUtils.openUrlInBrowser(context, component.link)
} }
alert.show() show()
} }
} }
} }
}
private fun loadLicense(context: Context, formattedLicense: String): View {
val webViewData = Base64.encodeToString(
formattedLicense.toByteArray(StandardCharsets.UTF_8), Base64.NO_PADDING
)
return WebView(context).apply {
loadData(webViewData, "text/html; charset=UTF-8", "base64")
}
}
} }