Merge pull request #875 from Bnyro/master

fix playlists crash and pip in background mode
This commit is contained in:
Bnyro 2022-07-26 07:55:51 +02:00 committed by GitHub
commit 6fe5c5a6e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package com.github.libretube.fragments
import android.app.ActivityManager
import android.app.NotificationManager
import android.app.PictureInPictureParams
import android.content.Context
@ -57,6 +58,7 @@ import com.github.libretube.obj.Streams
import com.github.libretube.obj.Subscribe
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.services.BackgroundMode
import com.github.libretube.util.BackgroundHelper
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.CronetHelper
@ -1708,14 +1710,29 @@ class PlayerFragment : Fragment() {
}
fun onUserLeaveHint() {
if (SDK_INT >= Build.VERSION_CODES.O && shouldStartPiP()) {
activity?.enterPictureInPictureMode(updatePipParams())
}
}
private fun shouldStartPiP(): Boolean {
val bounds = Rect()
binding.playerScrollView.getHitRect(bounds)
if (SDK_INT >= Build.VERSION_CODES.O &&
(binding.playerScrollView.getLocalVisibleRect(bounds) || Globals.IS_FULL_SCREEN)
) {
activity?.enterPictureInPictureMode(updatePipParams())
val backgroundModeRunning = isServiceRunning(requireContext(), BackgroundMode::class.java)
return (binding.playerScrollView.getLocalVisibleRect(bounds) || Globals.IS_FULL_SCREEN) &&
(exoPlayer.isPlaying || !backgroundModeRunning)
}
private fun isServiceRunning(context: Context, serviceClass: Class<*>): Boolean {
val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
if (serviceClass.name == service.service.className) {
return true
}
}
return false
}
private fun updatePipParams() = PictureInPictureParams.Builder()

View File

@ -22,6 +22,7 @@ class PlaylistFragment : Fragment() {
private lateinit var binding: FragmentPlaylistBinding
private var playlistId: String? = null
private var isOwner: Boolean = false
var nextPage: String? = null
private var playlistAdapter: PlaylistAdapter? = null
private var isLoading = true
@ -56,7 +57,9 @@ class PlaylistFragment : Fragment() {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.getPlaylist(playlistId!!)
// load locally stored playlists with the auth api
if (isPipedPlaylist()) RetrofitInstance.authApi.getPlaylist(playlistId!!)
else RetrofitInstance.api.getPlaylist(playlistId!!)
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
@ -76,7 +79,7 @@ class PlaylistFragment : Fragment() {
val user = PreferenceHelper.getUsername()
// check whether the user owns the playlist
val isOwner = response.uploaderUrl == null &&
isOwner = response.uploaderUrl == null &&
response.uploader.equals(user, true)
// show playlist options
@ -118,6 +121,8 @@ class PlaylistFragment : Fragment() {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
// load locally stored playlists with the auth api
if (isPipedPlaylist()) RetrofitInstance.authApi.getPlaylistNextPage(playlistId!!, nextPage!!)
RetrofitInstance.api.getPlaylistNextPage(playlistId!!, nextPage!!)
} catch (e: IOException) {
println(e)
@ -135,6 +140,11 @@ class PlaylistFragment : Fragment() {
run()
}
private fun isPipedPlaylist(): Boolean {
val regex = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
return playlistId?.contains(regex) == true || isOwner
}
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity