fixed crash issue and poster and download bar

This commit is contained in:
LagradOst 2021-09-25 00:51:08 +02:00
parent c4328ccd3c
commit 2f7f4a06a7
7 changed files with 107 additions and 119 deletions

View file

@ -35,6 +35,7 @@ import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.receivers.VideoDownloadRestartReceiver import com.lagradost.cloudstream3.receivers.VideoDownloadRestartReceiver
import com.lagradost.cloudstream3.ui.APIRepository import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.download.DOWNLOAD_NAVIGATE_TO import com.lagradost.cloudstream3.ui.download.DOWNLOAD_NAVIGATE_TO
import com.lagradost.cloudstream3.utils.AppUtils.isCastApiAvailable
import com.lagradost.cloudstream3.utils.AppUtils.loadResult import com.lagradost.cloudstream3.utils.AppUtils.loadResult
import com.lagradost.cloudstream3.utils.DataStore.getKey import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.removeKey import com.lagradost.cloudstream3.utils.DataStore.removeKey
@ -117,14 +118,26 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
mCastSession = mSessionManager.currentCastSession try {
mSessionManager.addSessionManagerListener(mSessionManagerListener) if(isCastApiAvailable()) {
mCastSession = mSessionManager.currentCastSession
mSessionManager.addSessionManagerListener(mSessionManagerListener)
}
} catch (e : Exception) {
logError(e)
}
} }
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
mSessionManager.removeSessionManagerListener(mSessionManagerListener) try {
mCastSession = null if(isCastApiAvailable()) {
mSessionManager.removeSessionManagerListener(mSessionManagerListener)
mCastSession = null
}
} catch (e : Exception) {
logError(e)
}
} }
companion object { companion object {
@ -188,7 +201,6 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
fun Context.updateLocale() { fun Context.updateLocale() {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this) val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
val localeCode = settingsManager.getString(getString(R.string.locale_key), null) val localeCode = settingsManager.getString(getString(R.string.locale_key), null)
println("LOCALE: " + localeCode)
setLocale(this, localeCode) setLocale(this, localeCode)
} }
} }
@ -219,43 +231,9 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
} }
} }
private fun AppCompatActivity.backPressed(): Boolean {
/*val currentFragment = supportFragmentManager.fragments.last {
it.isVisible
}
if (currentFragment is NavHostFragment) {
val child = currentFragment.childFragmentManager.fragments.last {
it.isVisible
}
if (child is DownloadChildFragment) {
val navController = findNavController(R.id.nav_host_fragment)
navController.navigate(R.id.navigation_downloads, Bundle(), navOptions)
return true
}
if (child is SearchFragment || child is HomeFragment || child is DownloadFragment || child is SettingsFragment) {
this.finish()
return true
}
}
if (currentFragment != null && supportFragmentManager.fragments.size > 2) {
//MainActivity.showNavbar()
supportFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.enter_anim, R.anim.exit_anim, R.anim.pop_enter, R.anim.pop_exit)
.remove(currentFragment)
.commitAllowingStateLoss()
backEvent.invoke(true)
return true
}
*/
backEvent.invoke(false)
return false
}
override fun onBackPressed() { override fun onBackPressed() {
this.updateLocale() this.updateLocale()
if (backPressed()) return backEvent.invoke(true)
super.onBackPressed() super.onBackPressed()
this.updateLocale() this.updateLocale()
} }
@ -315,7 +293,13 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
) // THEME IS SET BEFORE VIEW IS CREATED TO APPLY THE THEME TO THE MAIN VIEW ) // THEME IS SET BEFORE VIEW IS CREATED TO APPLY THE THEME TO THE MAIN VIEW
updateLocale() updateLocale()
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
mSessionManager = CastContext.getSharedInstance(this).sessionManager try {
if(isCastApiAvailable()) {
mSessionManager = CastContext.getSharedInstance(this).sessionManager
}
} catch (e : Exception) {
logError(e)
}
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN) window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

View file

@ -81,7 +81,7 @@ class AllMoviesForYouProvider : MainAPI() {
document.selectFirst("div.Vote > div.post-ratings > span")?.text()?.toFloatOrNull()?.times(10)?.toInt() document.selectFirst("div.Vote > div.post-ratings > span")?.text()?.toFloatOrNull()?.times(10)?.toInt()
val year = document.selectFirst("span.Date")?.text() val year = document.selectFirst("span.Date")?.text()
val duration = document.selectFirst("span.Time").text() val duration = document.selectFirst("span.Time").text()
val backgroundPoster = fixUrl(document.selectFirst("div.Image > figure > img").attr("src")) val backgroundPoster = fixUrl(document.selectFirst("div.Image > figure > img").attr("data-src"))
if (type == TvType.TvSeries) { if (type == TvType.TvSeries) {
val list = ArrayList<Pair<Int, String>>() val list = ArrayList<Pair<Int, String>>()

View file

@ -14,7 +14,6 @@ import android.widget.TextView
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialog

View file

@ -53,6 +53,7 @@ class HomeViewModel : ViewModel() {
val resumeWatching: LiveData<List<SearchResponse>> = _resumeWatching val resumeWatching: LiveData<List<SearchResponse>> = _resumeWatching
fun loadResumeWatching(context: Context) = viewModelScope.launch { fun loadResumeWatching(context: Context) = viewModelScope.launch {
println("Resume::")
val resumeWatching = withContext(Dispatchers.IO) { val resumeWatching = withContext(Dispatchers.IO) {
context.getAllResumeStateIds().mapNotNull { id -> context.getAllResumeStateIds().mapNotNull { id ->
context.getLastWatched(id) context.getLastWatched(id)

View file

@ -70,10 +70,7 @@ import com.lagradost.cloudstream3.MainActivity.Companion.canEnterPipMode
import com.lagradost.cloudstream3.MainActivity.Companion.isInPIPMode import com.lagradost.cloudstream3.MainActivity.Companion.isInPIPMode
import com.lagradost.cloudstream3.MainActivity.Companion.showToast import com.lagradost.cloudstream3.MainActivity.Companion.showToast
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.mvvm.Resource import com.lagradost.cloudstream3.mvvm.*
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.mvvm.observe
import com.lagradost.cloudstream3.mvvm.observeDirectly
import com.lagradost.cloudstream3.ui.result.ResultEpisode import com.lagradost.cloudstream3.ui.result.ResultEpisode
import com.lagradost.cloudstream3.ui.result.ResultViewModel import com.lagradost.cloudstream3.ui.result.ResultViewModel
import com.lagradost.cloudstream3.ui.subtitles.SaveCaptionStyle import com.lagradost.cloudstream3.ui.subtitles.SaveCaptionStyle
@ -1046,79 +1043,83 @@ class PlayerFragment : Fragment() {
}*/ }*/
if (activity?.isCastApiAvailable() == true && !isDownloadedFile) { if (activity?.isCastApiAvailable() == true && !isDownloadedFile) {
CastButtonFactory.setUpMediaRouteButton(activity, player_media_route_button) try {
val castContext = CastContext.getSharedInstance(requireContext()) CastButtonFactory.setUpMediaRouteButton(activity, player_media_route_button)
val castContext = CastContext.getSharedInstance(requireContext())
if (castContext.castState != CastState.NO_DEVICES_AVAILABLE) player_media_route_button.visibility = VISIBLE if (castContext.castState != CastState.NO_DEVICES_AVAILABLE) player_media_route_button.visibility = VISIBLE
castContext.addCastStateListener { state -> castContext.addCastStateListener { state ->
if (player_media_route_button != null) { if (player_media_route_button != null) {
player_media_route_button.isVisible = state != CastState.NO_DEVICES_AVAILABLE player_media_route_button.isVisible = state != CastState.NO_DEVICES_AVAILABLE
if (state == CastState.CONNECTED) { if (state == CastState.CONNECTED) {
if (!this::exoPlayer.isInitialized) return@addCastStateListener if (!this::exoPlayer.isInitialized) return@addCastStateListener
val links = sortUrls(getUrls() ?: return@addCastStateListener) val links = sortUrls(getUrls() ?: return@addCastStateListener)
val epData = getEpisode() ?: return@addCastStateListener val epData = getEpisode() ?: return@addCastStateListener
val index = links.indexOf(getCurrentUrl()) val index = links.indexOf(getCurrentUrl())
(activity as MainActivity?)?.mCastSession?.startCast( (activity as MainActivity?)?.mCastSession?.startCast(
apiName, apiName,
currentIsMovie ?: return@addCastStateListener, currentIsMovie ?: return@addCastStateListener,
currentHeaderName, currentHeaderName,
currentPoster, currentPoster,
epData.index, epData.index,
episodes, episodes,
links, links,
context?.getSubs(supportsDownloadedFiles = false) ?: emptyList(), context?.getSubs(supportsDownloadedFiles = false) ?: emptyList(),
index, index,
exoPlayer.currentPosition exoPlayer.currentPosition
) )
/* /*
val customData = val customData =
links.map { JSONObject().put("name", it.name) } links.map { JSONObject().put("name", it.name) }
val jsonArray = JSONArray() val jsonArray = JSONArray()
for (item in customData) { for (item in customData) {
jsonArray.put(item) jsonArray.put(item)
}
val mediaItems = links.map {
val movieMetadata = MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE)
movieMetadata.putString(MediaMetadata.KEY_SUBTITLE,
epData.name ?: "Episode ${epData.episode}")
if (currentHeaderName != null)
movieMetadata.putString(MediaMetadata.KEY_TITLE, currentHeaderName)
val srcPoster = epData.poster ?: currentPoster
if (srcPoster != null) {
movieMetadata.addImage(WebImage(Uri.parse(srcPoster)))
} }
MediaQueueItem.Builder( val mediaItems = links.map {
MediaInfo.Builder(it.url) val movieMetadata = MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE)
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setContentType(MimeTypes.VIDEO_UNKNOWN)
.setCustomData(JSONObject().put("data", jsonArray)) movieMetadata.putString(MediaMetadata.KEY_SUBTITLE,
.setMetadata(movieMetadata) epData.name ?: "Episode ${epData.episode}")
if (currentHeaderName != null)
movieMetadata.putString(MediaMetadata.KEY_TITLE, currentHeaderName)
val srcPoster = epData.poster ?: currentPoster
if (srcPoster != null) {
movieMetadata.addImage(WebImage(Uri.parse(srcPoster)))
}
MediaQueueItem.Builder(
MediaInfo.Builder(it.url)
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setContentType(MimeTypes.VIDEO_UNKNOWN)
.setCustomData(JSONObject().put("data", jsonArray))
.setMetadata(movieMetadata)
.build()
)
.build() .build()
) }.toTypedArray()
.build()
}.toTypedArray()
val castPlayer = CastPlayer(castContext) val castPlayer = CastPlayer(castContext)
castPlayer.loadItems( castPlayer.loadItems(
mediaItems, mediaItems,
if (index > 0) index else 0, if (index > 0) index else 0,
exoPlayer.currentPosition, exoPlayer.currentPosition,
MediaStatus.REPEAT_MODE_REPEAT_SINGLE MediaStatus.REPEAT_MODE_REPEAT_SINGLE
)*/ )*/
// activity?.popCurrentPage(isInPlayer = true, isInExpandedView = false, isInResults = false) // activity?.popCurrentPage(isInPlayer = true, isInExpandedView = false, isInResults = false)
safeReleasePlayer() safeReleasePlayer()
activity?.popCurrentPage() activity?.popCurrentPage()
}
} }
} }
} catch (e : Exception) {
logError(e)
} }
} }

View file

@ -17,7 +17,6 @@ import android.view.View.VISIBLE
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
@ -37,8 +36,8 @@ import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.APIHolder.getApiFromName import com.lagradost.cloudstream3.APIHolder.getApiFromName
import com.lagradost.cloudstream3.APIHolder.getId import com.lagradost.cloudstream3.APIHolder.getId
import com.lagradost.cloudstream3.MainActivity.Companion.showToast import com.lagradost.cloudstream3.MainActivity.Companion.showToast
import com.lagradost.cloudstream3.MainActivity.Companion.updateLocale
import com.lagradost.cloudstream3.mvvm.Resource import com.lagradost.cloudstream3.mvvm.Resource
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.mvvm.observe import com.lagradost.cloudstream3.mvvm.observe
import com.lagradost.cloudstream3.ui.WatchType import com.lagradost.cloudstream3.ui.WatchType
import com.lagradost.cloudstream3.ui.download.DOWNLOAD_ACTION_DOWNLOAD import com.lagradost.cloudstream3.ui.download.DOWNLOAD_ACTION_DOWNLOAD
@ -304,16 +303,20 @@ class ResultFragment : Fragment() {
activity?.let { activity?.let {
if (it.isCastApiAvailable()) { if (it.isCastApiAvailable()) {
CastButtonFactory.setUpMediaRouteButton(it, media_route_button) try {
val castContext = CastContext.getSharedInstance(it.applicationContext) CastButtonFactory.setUpMediaRouteButton(it, media_route_button)
val castContext = CastContext.getSharedInstance(it.applicationContext)
if (castContext.castState != CastState.NO_DEVICES_AVAILABLE) media_route_button.visibility = VISIBLE if (castContext.castState != CastState.NO_DEVICES_AVAILABLE) media_route_button.visibility = VISIBLE
castContext.addCastStateListener { state -> castContext.addCastStateListener { state ->
if (media_route_button != null) { if (media_route_button != null) {
if (state == CastState.NO_DEVICES_AVAILABLE) media_route_button.visibility = GONE else { if (state == CastState.NO_DEVICES_AVAILABLE) media_route_button.visibility = GONE else {
if (media_route_button.visibility == GONE) media_route_button.visibility = VISIBLE if (media_route_button.visibility == GONE) media_route_button.visibility = VISIBLE
}
} }
} }
} catch (e : Exception) {
logError(e)
} }
} }
} }

View file

@ -260,7 +260,7 @@ object VideoDownloadManager {
} }
if (state == DownloadType.IsDownloading || state == DownloadType.IsPaused) { if (state == DownloadType.IsDownloading || state == DownloadType.IsPaused) {
builder.setProgress(total.toInt(), progress.toInt(), false) builder.setProgress((total / 1000).toInt(), (progress / 1000).toInt(), false)
} }
val rowTwoExtra = if (ep.name != null) " - ${ep.name}\n" else "" val rowTwoExtra = if (ep.name != null) " - ${ep.name}\n" else ""