Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import com.nextcloud.client.player.media3.session.MediaSessionFactory
import com.nextcloud.client.player.model.PlaybackSettings
import com.nextcloud.client.player.model.file.PlaybackFile
import com.nextcloud.client.player.model.file.PlaybackFiles
import com.nextcloud.client.player.model.state.PlayMode
import com.nextcloud.client.player.model.state.PlaybackState
import com.nextcloud.client.player.model.state.RepeatMode
import com.nextcloud.client.player.util.PeriodicAction
import com.nextcloud.client.player.util.PlayerUtil.indexOfFirst
import com.nextcloud.client.player.util.PlayerUtil.playbackFile
import com.nextcloud.client.player.util.PlayerUtil.readCurrentFiles
import com.nextcloud.client.player.util.PlayerUtil.readMediaIds
import com.nextcloud.client.player.util.PlayerUtil.setRepeatMode
import com.nextcloud.client.player.util.PlayerUtil.setPlayMode
import com.nextcloud.client.player.util.PlayerUtil.toMediaItem
import com.nextcloud.client.player.util.PlayerUtil.toPlaybackState
import com.nextcloud.client.player.util.PlayerUtil.updateMediaItems
Expand Down Expand Up @@ -138,8 +138,7 @@ class PlaybackModel @Inject constructor(
.await()
.apply {
addListener(playerListener)
setRepeatMode(playbackSettings.repeatMode)
shuffleModeEnabled = playbackSettings.isShuffle
setPlayMode(playbackSettings.playMode)
}
controllerScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
}
Expand Down Expand Up @@ -281,14 +280,9 @@ class PlaybackModel @Inject constructor(
controller?.seekTo(positionInMilliseconds)
}

fun setRepeatMode(repeatMode: RepeatMode) {
playbackSettings.setRepeatMode(repeatMode)
controller?.setRepeatMode(repeatMode)
}

fun setShuffle(shuffle: Boolean) {
playbackSettings.setShuffle(shuffle)
controller?.shuffleModeEnabled = shuffle
fun setPlayMode(playMode: PlayMode) {
playbackSettings.setPlayMode(playMode)
controller?.setPlayMode(playMode)
}

fun switchToFile(file: PlaybackFile) {
Expand Down Expand Up @@ -320,8 +314,7 @@ class PlaybackModel @Inject constructor(
private fun releasedState(): PlaybackState = PlaybackState(
currentFiles = emptyList(),
currentItemState = null,
repeatMode = playbackSettings.repeatMode,
shuffle = playbackSettings.isShuffle
playMode = playbackSettings.playMode
)

private fun notifyPlaybackError(error: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,26 @@ package com.nextcloud.client.player.model

import android.content.Context
import androidx.core.content.edit
import com.nextcloud.client.player.model.state.RepeatMode
import com.nextcloud.client.player.model.state.PlayMode
import com.nextcloud.utils.serialization.AppJson
import javax.inject.Inject

class PlaybackSettings @Inject constructor(context: Context) {
companion object {
private const val PREFERENCES_FILE_NAME = "playback_settings"
private const val REPEAT_MODE_ID_KEY = "repeat_mode_id"
private const val SHUFFLE_KEY = "shuffle"
private val DEFAULT_REPEAT_MODE = RepeatMode.OFF
private const val PLAY_MODE_KEY = "play_mode"
}

private val preferences = context.getSharedPreferences(PREFERENCES_FILE_NAME, Context.MODE_PRIVATE)

val repeatMode: RepeatMode
get() = preferences.getInt(REPEAT_MODE_ID_KEY, -1)
.let { id -> RepeatMode.entries.firstOrNull { it.id == id } }
?.takeUnless { it == RepeatMode.ALL }
?: DEFAULT_REPEAT_MODE
val playMode: PlayMode
get() = preferences.getString(PLAY_MODE_KEY, null)
?.let { runCatching { AppJson.instance.decodeFromString<PlayMode>(it) }.getOrNull() }
?: PlayMode.REPEAT_ONE

val isShuffle: Boolean
get() = preferences.getBoolean(SHUFFLE_KEY, false)

fun setRepeatMode(repeatMode: RepeatMode) {
preferences.edit {
putInt(REPEAT_MODE_ID_KEY, repeatMode.id)
}
}

fun setShuffle(shuffle: Boolean) {
fun setPlayMode(playMode: PlayMode) {
preferences.edit {
putBoolean(SHUFFLE_KEY, shuffle)
putString(PLAY_MODE_KEY, AppJson.instance.encodeToString(playMode))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

package com.nextcloud.client.player.model.state

import androidx.annotation.DrawableRes
import com.owncloud.android.R
import kotlinx.serialization.Serializable

@Serializable
enum class PlayMode(@param:DrawableRes val iconRes: Int) {

// repeats the same file again and again
REPEAT_ONE(R.drawable.player_ic_repeat_one),

// plays all files in order, then starts over from the first one
REPEAT_ALL(R.drawable.player_ic_repeat_all),

// plays all files in random order, then starts over
SHUFFLE(R.drawable.player_ic_shuffle);

fun next(): PlayMode = when (this) {
REPEAT_ONE -> REPEAT_ALL
REPEAT_ALL -> SHUFFLE
SHUFFLE -> REPEAT_ONE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ import com.nextcloud.client.player.model.file.PlaybackFile
data class PlaybackState(
val currentFiles: List<PlaybackFile>,
val currentItemState: PlaybackItemState?,
val repeatMode: RepeatMode,
val shuffle: Boolean
val playMode: PlayMode
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.flowWithLifecycle
import com.nextcloud.android.common.ui.color.ColorUtil
import com.nextcloud.client.player.media3.PlaybackModel
import com.nextcloud.client.player.model.state.PlayMode
import com.nextcloud.client.player.model.state.PlaybackItemState
import com.nextcloud.client.player.model.state.PlaybackState
import com.nextcloud.client.player.model.state.PlayerState
import com.nextcloud.client.player.model.state.RepeatMode
import com.nextcloud.client.player.ui.MediaNavigator
import com.owncloud.android.R
import com.owncloud.android.databinding.PlayerControlViewBinding
Expand Down Expand Up @@ -85,15 +85,10 @@ class PlayerControlView @JvmOverloads constructor(
ContextCompat.getColorStateList(context, R.color.player_control_icon_tint)
}

private val toggleIconTint: ColorStateList by lazy {
ColorStateList(
arrayOf(intArrayOf(android.R.attr.state_checked), intArrayOf()),
intArrayOf(serverPrimaryColor, ContextCompat.getColor(context, R.color.player_default_icon_color))
)
}

var navigator: MediaNavigator? = null

private var playMode = PlayMode.REPEAT_ONE

init {
if (!isInEditMode) {
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this)
Expand Down Expand Up @@ -149,8 +144,7 @@ class PlayerControlView @JvmOverloads constructor(
binding.run {
ivPrevious.iconTint = transportIconTint
ivNext.iconTint = transportIconTint
ivRepeat.iconTint = toggleIconTint
ivRandom.iconTint = toggleIconTint
playModeButton.iconTint = transportIconTint
ivPlayPause.backgroundTintList = serverPrimaryTint
ivPlayPause.iconTint = onServerPrimaryTint

Expand All @@ -169,23 +163,7 @@ class PlayerControlView @JvmOverloads constructor(
}
}

binding.ivRepeat.setOnClickListener {
val repeatSingle = binding.ivRepeat.isChecked

// Repeat and Random are mutually exclusive
playbackModel.setShuffle(false)

playbackModel.setRepeatMode(if (repeatSingle) RepeatMode.SINGLE else RepeatMode.OFF)
}

binding.ivRandom.setOnClickListener {
val shuffle = binding.ivRandom.isChecked

// Repeat and Random are mutually exclusive
playbackModel.setRepeatMode(RepeatMode.OFF)

playbackModel.setShuffle(shuffle)
}
binding.playModeButton.setOnClickListener { applyNextPlayMode() }

binding.ivNext.setOnClickListener { navigator?.showNext() }

Expand All @@ -209,9 +187,20 @@ class PlayerControlView @JvmOverloads constructor(
.launchIn(viewScope)
}

private fun applyNextPlayMode() {
val nextMode = playMode.next()
playbackModel.setPlayMode(nextMode)

renderPlayMode(nextMode)
}

private fun renderPlayMode(mode: PlayMode) {
playMode = mode
binding.playModeButton.setIconResource(mode.iconRes)
}

private fun render(playbackState: PlaybackState) {
binding.ivRepeat.isChecked = playbackState.repeatMode == RepeatMode.SINGLE
binding.ivRandom.isChecked = playbackState.shuffle
renderPlayMode(playbackState.playMode)
binding.ivPlayPause.isChecked = playbackState.currentItemState?.playerState == PlayerState.PLAYING
binding.ivNext.isEnabled = navigator?.hasNext == true
binding.ivPrevious.isEnabled = navigator?.hasPrevious == true
Expand Down
22 changes: 9 additions & 13 deletions app/src/main/java/com/nextcloud/client/player/util/PlayerUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.Player
import com.nextcloud.client.player.model.file.PlaybackFile
import com.nextcloud.client.player.model.state.PlayMode
import com.nextcloud.client.player.model.state.PlaybackItemMetadata
import com.nextcloud.client.player.model.state.PlaybackItemState
import com.nextcloud.client.player.model.state.PlaybackState
import com.nextcloud.client.player.model.state.PlayerState
import com.nextcloud.client.player.model.state.RepeatMode
import com.nextcloud.client.player.model.state.VideoSize
import com.nextcloud.utils.extensions.resolveMimeType
import com.nextcloud.utils.serialization.AppJson
Expand Down Expand Up @@ -104,19 +104,15 @@ object PlayerUtil {
replaceMediaItems(0, oldCurrentMediaItemIndex, newMediaItems.subList(0, newCurrentMediaItemIndex))
}

fun Player.setRepeatMode(mode: RepeatMode) {
repeatMode = when (mode) {
RepeatMode.SINGLE -> Player.REPEAT_MODE_ONE
RepeatMode.ALL -> Player.REPEAT_MODE_ALL
RepeatMode.OFF -> Player.REPEAT_MODE_OFF
}
fun Player.setPlayMode(mode: PlayMode) {
repeatMode = if (mode == PlayMode.REPEAT_ONE) Player.REPEAT_MODE_ONE else Player.REPEAT_MODE_ALL
shuffleModeEnabled = mode == PlayMode.SHUFFLE
}

fun Player.toPlaybackState(currentFiles: List<PlaybackFile>): PlaybackState = PlaybackState(
currentFiles = currentFiles,
currentItemState = getCurrentItemState(),
repeatMode = mapRepeatMode(),
shuffle = shuffleModeEnabled
playMode = mapPlayMode()
)

fun Player.readMediaIds(): List<String> = buildList {
Expand Down Expand Up @@ -165,10 +161,10 @@ object PlayerUtil {
.takeIf { it.width > 0 && it.height > 0 }
?.let { VideoSize(width = it.width, height = it.height) }

private fun Player.mapRepeatMode(): RepeatMode = when (repeatMode) {
Player.REPEAT_MODE_ONE -> RepeatMode.SINGLE
Player.REPEAT_MODE_ALL -> RepeatMode.ALL
else -> RepeatMode.OFF
private fun Player.mapPlayMode(): PlayMode = when {
shuffleModeEnabled -> PlayMode.SHUFFLE
repeatMode == Player.REPEAT_MODE_ONE -> PlayMode.REPEAT_ONE
else -> PlayMode.REPEAT_ALL
}

fun OCFile.toPlaybackFile() = PlaybackFile(
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/drawable/player_ic_repeat_one.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--
~ Nextcloud - Android Client
~
~ SPDX-FileCopyrightText: 2018-2026 Google LLC
~ SPDX-License-Identifier: Apache-2.0
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">

<path
android:fillColor="@android:color/white"
android:pathData="M460,600L460,420L400,420L400,360L520,360L520,600L460,600ZM280,880L120,720L280,560L336,618L274,680L680,680L680,520L760,520L760,760L274,760L336,822L280,880ZM200,440L200,200L686,200L624,138L680,80L840,240L680,400L624,342L686,280L280,280L280,440L200,440Z" />

</vector>
19 changes: 11 additions & 8 deletions app/src/main/res/layout/player_control_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@
<Space style="@style/PlayerControlSpacerStyle" />

<com.google.android.material.button.MaterialButton
android:id="@+id/ivRepeat"
style="@style/PlayerControlToggleButtonStyle"
android:contentDescription="@string/player_control_view_repeat_button_content_description"
app:icon="@drawable/player_ic_repeat" />
android:id="@+id/playModePlaceholder"
style="@style/PlayerControlButtonStyle"
android:clickable="false"
android:focusable="false"
android:importantForAccessibility="no"
android:visibility="invisible"
app:icon="@drawable/player_ic_repeat_all" />

<Space style="@style/PlayerControlSpacerStyle" />

Expand Down Expand Up @@ -82,10 +85,10 @@
<Space style="@style/PlayerControlSpacerStyle" />

<com.google.android.material.button.MaterialButton
android:id="@+id/ivRandom"
style="@style/PlayerControlToggleButtonStyle"
android:contentDescription="@string/player_control_view_random_button_content_description"
app:icon="@drawable/player_ic_shuffle" />
android:id="@+id/playModeButton"
style="@style/PlayerControlButtonStyle"
android:contentDescription="@string/player_control_view_play_mode_button_content_description"
app:icon="@drawable/player_ic_repeat_all" />

<Space style="@style/PlayerControlSpacerStyle" />

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1640,11 +1640,10 @@
<string name="player_media_controls_close_action_title">Close</string>
<string name="player_last_modified">Modified: %s</string>
<string name="player_audio_title_with_artist">%1$s • %2$s</string>
<string name="player_control_view_repeat_button_content_description">Repeat button</string>
<string name="player_control_view_play_mode_button_content_description">Play mode button</string>
<string name="player_control_view_previous_button_content_description">Previous button</string>
<string name="player_control_view_play_pause_button_content_description">Play/Pause button</string>
<string name="player_control_view_next_button_content_description">Next button</string>
<string name="player_control_view_random_button_content_description">Random button</string>
<string name="server_not_allowed">Server not allowed</string>
<string name="run_two_way_sync">Run sync now</string>
</resources>
4 changes: 0 additions & 4 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,6 @@
<item name="android:layout_marginBottom">@dimen/player_button_margin_vertical</item>
</style>

<style name="PlayerControlToggleButtonStyle" parent="PlayerControlButtonStyle">
<item name="android:checkable">true</item>
</style>

<style name="PlayerPlayPauseButtonStyle" parent="Widget.Material3Expressive.Button.IconButton.Filled">
<item name="materialSizeOverlay">@style/SizeOverlay.Material3Expressive.Button.IconButton.Medium</item>
<item name="android:checkable">true</item>
Expand Down
Loading