add option to prefer the secondary stream, if present
This commit is contained in:
@ -35,6 +35,7 @@ import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.preferences.EncryptedPreferences
|
||||
import org.mosad.teapod.preferences.Preferences
|
||||
import org.mosad.teapod.ui.components.LoginDialog
|
||||
import org.mosad.teapod.ui.fragments.*
|
||||
import org.mosad.teapod.util.StorageController
|
||||
@ -104,7 +105,9 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
|
||||
private fun load() {
|
||||
// running login and list in parallel does not bring any speed improvements
|
||||
val time = measureTimeMillis {
|
||||
// make sure credentials are set
|
||||
Preferences.load(this)
|
||||
|
||||
// make sure credentials are set
|
||||
EncryptedPreferences.readCredentials(this)
|
||||
if (EncryptedPreferences.password.isEmpty()) {
|
||||
showLoginDialog(true)
|
||||
|
@ -1,22 +1,40 @@
|
||||
package org.mosad.teapod.preferences
|
||||
|
||||
import android.content.Context
|
||||
import org.mosad.teapod.R
|
||||
|
||||
object Preferences {
|
||||
|
||||
var login = ""
|
||||
internal set
|
||||
var password = ""
|
||||
var preferSecondary = false
|
||||
internal set
|
||||
|
||||
fun savePreferSecondary(context: Context, preferSecondary: Boolean) {
|
||||
val sharedPref = context.getSharedPreferences(
|
||||
context.getString(R.string.preference_file_key),
|
||||
Context.MODE_PRIVATE
|
||||
)
|
||||
|
||||
fun saveCredentials(login: String, password: String) {
|
||||
this.login = login
|
||||
this.password = password
|
||||
with(sharedPref.edit()) {
|
||||
putBoolean(context.getString(R.string.save_key_prefer_secondary), preferSecondary)
|
||||
apply()
|
||||
}
|
||||
|
||||
// TODO save
|
||||
this.preferSecondary = preferSecondary
|
||||
}
|
||||
|
||||
fun load() {
|
||||
// TODO
|
||||
/**
|
||||
* initially load the stored values
|
||||
*/
|
||||
fun load(context: Context) {
|
||||
val sharedPref = context.getSharedPreferences(
|
||||
context.getString(R.string.preference_file_key),
|
||||
Context.MODE_PRIVATE
|
||||
)
|
||||
|
||||
preferSecondary = sharedPref.getBoolean(
|
||||
context.getString(R.string.save_key_prefer_secondary), false
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -13,6 +13,7 @@ import org.mosad.teapod.BuildConfig
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.preferences.EncryptedPreferences
|
||||
import org.mosad.teapod.preferences.Preferences
|
||||
import org.mosad.teapod.ui.components.LoginDialog
|
||||
|
||||
class AccountFragment : Fragment() {
|
||||
@ -26,6 +27,7 @@ class AccountFragment : Fragment() {
|
||||
|
||||
text_account_login.text = EncryptedPreferences.login
|
||||
text_info_about_desc.text = getString(R.string.info_about_desc, BuildConfig.VERSION_NAME, getString(R.string.build_time))
|
||||
switch_secondary.isChecked = Preferences.preferSecondary
|
||||
|
||||
initActions()
|
||||
}
|
||||
@ -51,6 +53,10 @@ class AccountFragment : Fragment() {
|
||||
.build()
|
||||
.show()
|
||||
}
|
||||
|
||||
switch_secondary.setOnClickListener {
|
||||
Preferences.savePreferSecondary(requireContext(), switch_secondary.isChecked)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showLoginDialog(firstTry: Boolean) {
|
||||
|
@ -17,7 +17,9 @@ import kotlinx.android.synthetic.main.fragment_media.*
|
||||
import org.mosad.teapod.MainActivity
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.preferences.Preferences
|
||||
import org.mosad.teapod.util.DataTypes.MediaType
|
||||
import org.mosad.teapod.util.Episode
|
||||
import org.mosad.teapod.util.Media
|
||||
import org.mosad.teapod.util.StorageController
|
||||
import org.mosad.teapod.util.TMDBResponse
|
||||
@ -88,8 +90,8 @@ class MediaFragment(private val media: Media, private val tmdb: TMDBResponse) :
|
||||
private fun initActions() {
|
||||
button_play.setOnClickListener {
|
||||
when (media.type) {
|
||||
MediaType.MOVIE -> playStream(media.episodes.first().priStreamUrl)
|
||||
MediaType.TVSHOW -> playStream(media.episodes.first().priStreamUrl)
|
||||
MediaType.MOVIE -> playStream(media.episodes.first())
|
||||
MediaType.TVSHOW -> playStream(media.episodes.first())
|
||||
else -> Log.e(javaClass.name, "Wrong Type: $media.type")
|
||||
}
|
||||
}
|
||||
@ -114,13 +116,7 @@ class MediaFragment(private val media: Media, private val tmdb: TMDBResponse) :
|
||||
// set onItemClick only in adapter is initialized
|
||||
if (this::adapterRecEpisodes.isInitialized) {
|
||||
adapterRecEpisodes.onImageClick = { _, position ->
|
||||
// TODO add option to prefer secondary stream
|
||||
// try to use secondary stream if primary is missing
|
||||
if (media.episodes[position].priStreamUrl.isNotEmpty()) {
|
||||
playStream(media.episodes[position].priStreamUrl)
|
||||
} else if (media.episodes[position].secStreamUrl.isNotEmpty()) {
|
||||
playStream(media.episodes[position].secStreamUrl)
|
||||
}
|
||||
playStream(media.episodes[position])
|
||||
|
||||
// update watched state
|
||||
AoDParser.sendCallback(media.episodes[position].watchedCallback)
|
||||
@ -130,9 +126,23 @@ class MediaFragment(private val media: Media, private val tmdb: TMDBResponse) :
|
||||
}
|
||||
}
|
||||
|
||||
private fun playStream(url: String) {
|
||||
Log.d(javaClass.name, "Playing stream: $url")
|
||||
(activity as MainActivity).startPlayer(url)
|
||||
/**
|
||||
* Play the media's stream
|
||||
* If prefer secondary or primary is empty and secondary is present (secStreamOmU),
|
||||
* use the secondary stream. Else, if the primary stream is set use the primary stream.
|
||||
*/
|
||||
private fun playStream(ep: Episode) {
|
||||
val streamUrl = if ((Preferences.preferSecondary || ep.priStreamUrl.isEmpty()) && ep.secStreamOmU) {
|
||||
ep.secStreamUrl
|
||||
} else if (ep.priStreamUrl.isNotEmpty()) {
|
||||
ep.priStreamUrl
|
||||
} else {
|
||||
Log.e(javaClass.name, "No stream url set.")
|
||||
""
|
||||
}
|
||||
|
||||
Log.d(javaClass.name, "Playing stream: $streamUrl")
|
||||
(activity as MainActivity).startPlayer(streamUrl)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user