Skip to content

Commit

Permalink
Merge branch 'feature/208-implement-picture-in-picture'
Browse files Browse the repository at this point in the history
Close #208
  • Loading branch information
stantanasi committed Nov 6, 2024
2 parents b08dc68 + d749a01 commit b89918f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@

<activity
android:name=".activities.main.MainMobileActivity"
android:configChanges="orientation|screenLayout|screenSize"
android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize"
android:exported="true"
android:supportsPictureInPicture="true"
android:theme="@style/AppTheme.SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import com.tanasi.streamflix.BuildConfig
import com.tanasi.streamflix.R
import com.tanasi.streamflix.database.AppDatabase
import com.tanasi.streamflix.databinding.ActivityMainMobileBinding
import com.tanasi.streamflix.fragments.player.PlayerMobileFragment
import com.tanasi.streamflix.ui.UpdateAppMobileDialog
import com.tanasi.streamflix.utils.UserPreferences
import com.tanasi.streamflix.utils.getCurrentFragment
import kotlinx.coroutines.launch

class MainMobileActivity : FragmentActivity() {
Expand Down Expand Up @@ -127,4 +129,12 @@ class MainMobileActivity : FragmentActivity() {
}
})
}

override fun onUserLeaveHint() {
super.onUserLeaveHint()

when (val currentFragment = getCurrentFragment()) {
is PlayerMobileFragment -> currentFragment.onUserLeaveHint()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.tanasi.streamflix.fragments.player

import android.app.PictureInPictureParams
import android.content.Intent
import android.content.pm.ActivityInfo
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
Expand Down Expand Up @@ -237,8 +239,19 @@ class PlayerMobileFragment : Fragment() {
}
}

override fun onPause() {
super.onPause()
override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean) {
binding.pvPlayer.useController = !isInPictureInPictureMode
super.onPictureInPictureModeChanged(isInPictureInPictureMode)
}

fun onUserLeaveHint() {
if (::player.isInitialized && player.isPlaying) {
enterPIPMode()
}
}

override fun onStop() {
super.onStop()
player.pause()
}

Expand Down Expand Up @@ -305,6 +318,18 @@ class PlayerMobileFragment : Fragment() {

binding.pvPlayer.controller.tvExoSubtitle.text = args.subtitle

binding.pvPlayer.controller.btnExoPictureInPicture.setOnClickListener {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
Toast.makeText(
requireContext(),
requireContext().getString(R.string.player_picture_in_picture_not_supported),
Toast.LENGTH_SHORT
).show()
} else {
enterPIPMode()
}
}

binding.pvPlayer.controller.btnExoExternalPlayer.setOnClickListener {
Toast.makeText(
requireContext(),
Expand Down Expand Up @@ -478,6 +503,16 @@ class PlayerMobileFragment : Fragment() {
player.play()
}

private fun enterPIPMode() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
binding.pvPlayer.useController = false
requireActivity().enterPictureInPictureMode(
PictureInPictureParams.Builder()
.build()
)
}
}


private fun ExoPlayer.hasStarted(): Boolean {
return (this.currentPosition > (this.duration * 0.03) || this.currentPosition > 2.minutes.inWholeMilliseconds)
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/mobile/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@

<activity
android:name=".activities.main.MainMobileActivity"
android:configChanges="orientation|screenLayout|screenSize"
android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize"
android:exported="true"
android:supportsPictureInPicture="true"
android:theme="@style/AppTheme.SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M19,11h-8v6h8v-6zM23,19L23,4.98C23,3.88 22.1,3 21,3L3,3c-1.1,0 -2,0.88 -2,1.98L1,19c0,1.1 0.9,2 2,2h18c1.1,0 2,-0.9 2,-2zM21,19.02L3,19.02L3,4.97h18v14.05z"/>

</vector>
14 changes: 14 additions & 0 deletions app/src/main/res/layout/content_exo_controller_mobile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@
app:layout_constraintTop_toTopOf="parent"
tools:text="00:00:00" />

<ImageView
android:id="@+id/btn_exo_picture_in_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:padding="12dp"
android:src="@drawable/exo_styled_controls_picture_in_picture"
android:tint="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/btn_exo_aspect_ratio"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription,PrivateResource,UseAppTint" />


<ImageView
android:id="@+id/btn_exo_aspect_ratio"
android:layout_width="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@

<string name="player_subtitle_tv_show">S%d E%d • %s</string>
<string name="player_subtitle_tv_show_episode_only">E%d • %s</string>
<string name="player_picture_in_picture_not_supported">PiP is not supported on your device</string>
<string name="player_external_player_title">Play using</string>
<string name="player_external_player_error_video">Video not load yet</string>
<string name="player_aspect_ratio_fit">Fit to screen</string>
Expand Down

0 comments on commit b89918f

Please sign in to comment.