2021-02-06 19:02:12 +01:00
package org.mosad.teapod.ui.activity.main.fragments
2020-12-05 21:03:14 +01:00
2021-01-21 19:53:16 +01:00
import android.content.Intent
import android.net.Uri
2020-12-05 21:03:14 +01:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2021-05-09 20:31:51 +02:00
import android.widget.Toast
2020-12-05 21:03:14 +01:00
import androidx.annotation.RawRes
import androidx.fragment.app.Fragment
import com.afollestad.materialdialogs.MaterialDialog
import org.mosad.teapod.BuildConfig
import org.mosad.teapod.R
import org.mosad.teapod.databinding.FragmentAboutBinding
import org.mosad.teapod.databinding.ItemComponentBinding
2021-05-09 20:31:51 +02:00
import org.mosad.teapod.preferences.Preferences
2020-12-05 21:03:14 +01:00
import org.mosad.teapod.util.DataTypes.License
import org.mosad.teapod.util.ThirdPartyComponent
import java.lang.StringBuilder
2021-05-09 20:31:51 +02:00
import java.util.Timer
import kotlin.concurrent.schedule
2020-12-05 21:03:14 +01:00
class AboutFragment : Fragment ( ) {
private lateinit var binding : FragmentAboutBinding
2021-05-09 20:31:51 +02:00
private val teapodRepoUrl = " https://git.mosad.xyz/Seil0/teapod "
private val devClickMax = 5
private var devClickCount = 0
2020-12-05 21:03:14 +01:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View {
binding = FragmentAboutBinding . inflate ( inflater , container , false )
return binding . root
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2021-01-21 19:53:16 +01:00
binding . textVersionDesc . text = getString ( R . string . version _desc , BuildConfig . VERSION _NAME , getString ( R . string . build _time ) )
2020-12-05 21:03:14 +01:00
getThirdPartyComponents ( ) . forEach { thirdParty ->
val componentBinding = ItemComponentBinding . inflate ( layoutInflater ) //(R.layout.item_component, container, false)
componentBinding . textComponentTitle . text = thirdParty . name
componentBinding . textComponentDesc . text = getString (
R . string . third _party _component _desc ,
thirdParty . year ,
thirdParty . copyrightOwner ,
thirdParty . license . short
)
componentBinding . linearComponent . setOnClickListener {
showLicense ( thirdParty . license )
}
binding . linearThirdParty . addView ( componentBinding . root )
}
2021-01-21 19:53:16 +01:00
initActions ( )
}
private fun initActions ( ) {
2021-05-09 20:31:51 +02:00
binding . imageAppIcon . setOnClickListener {
checkDevSettings ( )
}
2021-01-21 19:53:16 +01:00
binding . linearSource . setOnClickListener {
startActivity ( Intent ( Intent . ACTION _VIEW , Uri . parse ( teapodRepoUrl ) ) )
}
binding . linearLicense . setOnClickListener {
MaterialDialog ( requireContext ( ) )
. title ( text = License . GPL3 . long )
. message ( text = parseLicense ( R . raw . gpl _3 _full ) )
. show ( )
}
2020-12-05 21:03:14 +01:00
}
2021-05-09 20:31:51 +02:00
/ * *
* check if dev settings shall be enabled
* /
private fun checkDevSettings ( ) {
// if the dev settings are already enabled show a toast
if ( Preferences . devSettings ) {
Toast . makeText ( context , getString ( R . string . dev _settings _already ) , Toast . LENGTH _SHORT ) . show ( )
return
}
// reset dev settings count after 5 seconds
if ( devClickCount == 0 ) {
Timer ( " " , false ) . schedule ( 5000 ) {
devClickCount = 0
}
}
devClickCount ++
if ( devClickCount == devClickMax ) {
Preferences . saveDevSettings ( requireContext ( ) , true )
Toast . makeText ( context , getString ( R . string . dev _settings _enabled ) , Toast . LENGTH _SHORT ) . show ( )
}
}
2020-12-05 21:03:14 +01:00
private fun getThirdPartyComponents ( ) : List < ThirdPartyComponent > {
2021-01-21 19:53:16 +01:00
return listOf (
2020-12-05 21:03:14 +01:00
ThirdPartyComponent ( " AndroidX " , " " , " The Android Open Source Project " ,
" https://developer.android.com/jetpack/androidx " , License . APACHE2 ) ,
ThirdPartyComponent ( " Material Components for Android " , " 2020 " , " The Android Open Source Project " ,
" https://github.com/material-components/material-components-android " , License . APACHE2 ) ,
ThirdPartyComponent ( " ExoPlayer " , " 2014 - 2020 " , " The Android Open Source Project " ,
" https://github.com/google/ExoPlayer " , License . APACHE2 ) ,
ThirdPartyComponent ( " Gson " , " 2008 " , " Google Inc. " ,
" https://github.com/google/gson " , License . APACHE2 ) ,
ThirdPartyComponent ( " Material design icons " , " 2020 " , " Google Inc. " ,
" https://github.com/google/material-design-icons " , License . APACHE2 ) ,
ThirdPartyComponent ( " Material Dialogs " , " " , " Aidan Follestad " ,
" https://github.com/afollestad/material-dialogs " , License . APACHE2 ) ,
ThirdPartyComponent ( " Jsoup " , " 2009 - 2020 " , " Jonathan Hedley " ,
" https://jsoup.org/ " , License . MIT ) ,
ThirdPartyComponent ( " kotlinx.coroutines " , " 2016 - 2019 " , " JetBrains " ,
" https://github.com/Kotlin/kotlinx.coroutines " , License . APACHE2 ) ,
ThirdPartyComponent ( " Glide " , " 2014 " , " Google Inc. " ,
" https://github.com/bumptech/glide " , License . BSD2 ) ,
ThirdPartyComponent ( " Glide Transformations " , " 2020 " , " Wasabeef " ,
" https://github.com/wasabeef/glide-transformations " , License . APACHE2 )
)
}
private fun showLicense ( license : License ) {
val licenseText = when ( license ) {
License . APACHE2 -> parseLicense ( R . raw . al _20 _full )
License . BSD2 -> parseLicense ( R . raw . bsd _2 _full )
License . GPL3 -> parseLicense ( R . raw . gpl _3 _full )
License . MIT -> parseLicense ( R . raw . mit _full )
}
MaterialDialog ( requireContext ( ) )
. title ( text = license . long )
. message ( text = licenseText )
. show ( )
}
private fun parseLicense ( @RawRes id : Int ) : String {
val sb = StringBuilder ( )
resources . openRawResource ( id ) . bufferedReader ( ) . forEachLine {
if ( it . isEmpty ( ) ) {
sb . appendLine ( " " )
} else {
sb . append ( it . trim ( ) + " " )
}
}
return sb . toString ( )
}
}