Skip to content

Commit

Permalink
Merge pull request #24 from itszechs/feature-ability-to-lock-playback…
Browse files Browse the repository at this point in the history
…-controls

Feature: Ability to lock playback controls
  • Loading branch information
itszechs authored Jun 25, 2022
2 parents 4375b61 + 9f14916 commit 8a833de
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
53 changes: 53 additions & 0 deletions app/src/main/java/zechs/drive/stream/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.widget.HorizontalScrollView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -69,6 +71,7 @@ class PlayerActivity : AppCompatActivity() {

// Player views
private lateinit var mainControlsRoot: LinearLayout
private lateinit var controlsScrollView: HorizontalScrollView
private lateinit var progressViewGroup: LinearLayout
private lateinit var toolbar: MaterialToolbar
private lateinit var btnPlay: MaterialButton
Expand All @@ -80,6 +83,8 @@ class PlayerActivity : AppCompatActivity() {
private lateinit var btnPip: MaterialButton
private lateinit var btnSpeed: MaterialButton
private lateinit var btnRotate: MaterialButton
private lateinit var btnLock: MaterialButton
private lateinit var btnUnlock: MaterialButton

// Dialogs
private var audioDialog: Dialog? = null
Expand All @@ -88,6 +93,7 @@ class PlayerActivity : AppCompatActivity() {

// States
private var onStopCalled = false
private var controlsLocked = false

// Configs
private var speed = arrayOf("0.25x", "0.5x", "Normal", "1.25x", "1.5x", "2x")
Expand All @@ -105,6 +111,7 @@ class PlayerActivity : AppCompatActivity() {
playerView = binding.playerView

mainControlsRoot = playerView.findViewById(R.id.mainControls)
controlsScrollView = playerView.findViewById(R.id.controlsScrollView)
progressViewGroup = playerView.findViewById(R.id.linearLayout2)
toolbar = playerView.findViewById(R.id.playerToolbar)
btnPlay = playerView.findViewById(R.id.btnPlay)
Expand All @@ -116,6 +123,8 @@ class PlayerActivity : AppCompatActivity() {
btnPip = playerView.findViewById(R.id.btnPip)
btnSpeed = playerView.findViewById(R.id.btnSpeed)
btnRotate = playerView.findViewById(R.id.btnRotate)
btnLock = playerView.findViewById(R.id.btnLock)
btnUnlock = playerView.findViewById(R.id.btnUnlock)

// Back button
toolbar.setNavigationOnClickListener {
Expand Down Expand Up @@ -194,6 +203,16 @@ class PlayerActivity : AppCompatActivity() {
setOrientation(this@PlayerActivity, orientation)
}

btnLock.setOnClickListener {
controlsLocked = true
handleLockingControls()
}

btnUnlock.setOnClickListener {
controlsLocked = false
handleLockingControls()
}

updateOrientation(resources.configuration)
initPlayer()
playMedia()
Expand Down Expand Up @@ -325,6 +344,40 @@ class PlayerActivity : AppCompatActivity() {
.setSeekForwardIncrementMs(10_000)
.setSeekBackIncrementMs(10_000)
.build()

playerView.setControllerVisibilityListener {
if (it == View.VISIBLE) {
handleLockingControls()
}
}

}

private fun handleLockingControls() {
TransitionManager.beginDelayedTransition(
playerView, AutoTransition().apply {
duration = 150L
}
)
if (controlsLocked) {
lockControls()
} else {
unlockControls()
}
}

private fun lockControls() {
controlsScrollView.visibility = View.GONE
mainControlsRoot.visibility = View.GONE
btnUnlock.visibility = View.VISIBLE
toolbar.visibility = View.GONE
}

private fun unlockControls() {
btnUnlock.visibility = View.GONE
toolbar.visibility = View.VISIBLE
controlsScrollView.visibility = View.VISIBLE
mainControlsRoot.visibility = View.VISIBLE
}

private fun releasePlayer() {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_lock_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM12,17c-1.1,0 -2,-0.9 -2,-2s0.9,-2 2,-2 2,0.9 2,2 -0.9,2 -2,2zM9,8L9,6c0,-1.66 1.34,-3 3,-3s3,1.34 3,3v2L9,8z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_unlock_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6h2c0,-1.66 1.34,-3 3,-3s3,1.34 3,3v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM18,20L6,20L6,10h12v10zM12,17c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2z" />
</vector>
36 changes: 34 additions & 2 deletions app/src/main/res/layout/player_control_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@
tools:subtitle="video metadata"
tools:title="Video title" />

<com.google.android.material.button.MaterialButton
android:id="@+id/btnUnlock"
style="@style/Widget.Material3.Button.IconButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:padding="16dp"
android:tooltipText="@string/unlock"
android:visibility="gone"
app:icon="@drawable/ic_lock_24"
app:iconTint="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/playerToolbar"
app:layout_constraintVertical_bias="0.0"
tools:visibility="visible" />

<LinearLayout
android:id="@+id/mainControls"
android:layout_width="0dp"
Expand Down Expand Up @@ -87,7 +109,7 @@
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/horizontalScrollView"
app:layout_constraintBottom_toTopOf="@+id/controlsScrollView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand Down Expand Up @@ -121,7 +143,7 @@
</LinearLayout>

<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:id="@+id/controlsScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
Expand All @@ -141,6 +163,16 @@
android:gravity="center"
android:orientation="horizontal">

<com.google.android.material.button.MaterialButton
android:id="@+id/btnLock"
style="@style/Widget.Material3.Button.IconButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:tooltipText="@string/lock"
app:icon="@drawable/ic_lock_24"
app:iconTint="@android:color/white" />

<com.google.android.material.button.MaterialButton
android:id="@+id/btnRotate"
style="@style/Widget.Material3.Button.IconButton"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<string name="ok">OK</string>
<string name="details">Details</string>
<string name="retry">Retry</string>
<string name="lock">Lock</string>
<string name="unlock">Unlock</string>
<string name="logout">Logout</string>
<string name="log_out_dialog_title">Are you sure you want to log out?</string>
<string name="no">No</string>
Expand Down

0 comments on commit 8a833de

Please sign in to comment.