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 package com.github.libretube.fragments
import android.app.ActivityManager
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PictureInPictureParams import android.app.PictureInPictureParams
import android.content.Context import android.content.Context
@ -57,6 +58,7 @@ import com.github.libretube.obj.Streams
import com.github.libretube.obj.Subscribe import com.github.libretube.obj.Subscribe
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.services.BackgroundMode
import com.github.libretube.util.BackgroundHelper import com.github.libretube.util.BackgroundHelper
import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.CronetHelper import com.github.libretube.util.CronetHelper
@ -1708,14 +1710,29 @@ class PlayerFragment : Fragment() {
} }
fun onUserLeaveHint() { fun onUserLeaveHint() {
if (SDK_INT >= Build.VERSION_CODES.O && shouldStartPiP()) {
activity?.enterPictureInPictureMode(updatePipParams())
}
}
private fun shouldStartPiP(): Boolean {
val bounds = Rect() val bounds = Rect()
binding.playerScrollView.getHitRect(bounds) binding.playerScrollView.getHitRect(bounds)
if (SDK_INT >= Build.VERSION_CODES.O && val backgroundModeRunning = isServiceRunning(requireContext(), BackgroundMode::class.java)
(binding.playerScrollView.getLocalVisibleRect(bounds) || Globals.IS_FULL_SCREEN)
) { return (binding.playerScrollView.getLocalVisibleRect(bounds) || Globals.IS_FULL_SCREEN) &&
activity?.enterPictureInPictureMode(updatePipParams()) (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() private fun updatePipParams() = PictureInPictureParams.Builder()

View File

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