Skip to content

Commit

Permalink
Twelve: Allow to reset local media playback stats from Settings
Browse files Browse the repository at this point in the history
Change-Id: I33c53146d130aed29ec3e5b12b231b834c516f8e
  • Loading branch information
bvlj authored and SebaUbuntu committed Jan 26, 2025
1 parent ef70df1 commit 2d5f3a7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
31 changes: 30 additions & 1 deletion app/src/main/java/org/lineageos/twelve/SettingsActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-FileCopyrightText: 2024-2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -10,6 +10,7 @@ import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.annotation.CallSuper
import androidx.annotation.Px
Expand All @@ -21,10 +22,12 @@ import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.launch
import org.lineageos.twelve.ext.ENABLE_OFFLOAD_KEY
import org.lineageos.twelve.ext.SKIP_SILENCE_KEY
Expand Down Expand Up @@ -143,6 +146,7 @@ class SettingsActivity : AppCompatActivity(R.layout.activity_settings) {
class RootSettingsFragment : SettingsFragment(R.xml.root_preferences) {
// Preferences
private val enableOffload by lazy { findPreference<SwitchPreference>(ENABLE_OFFLOAD_KEY)!! }
private val resetLocalStats by lazy { findPreference<Preference>("reset_local_stats")!! }
private val skipSilence by lazy { findPreference<SwitchPreference>(SKIP_SILENCE_KEY)!! }

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
Expand All @@ -161,6 +165,31 @@ class SettingsActivity : AppCompatActivity(R.layout.activity_settings) {
}
true
}

resetLocalStats.setOnPreferenceClickListener {
showResetLocalStatsDialog()
true
}
}

private fun showResetLocalStatsDialog() {
val context = requireActivity()
MaterialAlertDialogBuilder(context)
.setTitle(R.string.reset_local_stats_confirm_title)
.setMessage(R.string.reset_local_stats_confirm_message)
.setPositiveButton(R.string.reset_local_stats_confirm_positive) { _, _ ->
lifecycleScope.launch {
viewModel.resetLocalStats()

Toast.makeText(
context,
R.string.reset_local_stats_success,
Toast.LENGTH_SHORT
).show()
}
}
.setNegativeButton(android.R.string.cancel) { _, _ -> /* Do nothing */ }
.show()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ interface MediaStatsDao {
@Query("DELETE FROM LocalMediaStats WHERE media_uri IN (:mediaUris)")
suspend fun delete(mediaUris: List<Uri>)

/**
* Delete all entries.
*/
@Query("DELETE FROM LocalMediaStats")
suspend fun deleteAll()

/**
* Increase the play count of an entry by 1.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,13 @@ class MediaRepository(
sharedPreferences.defaultProvider = providerIdentifier
}

/**
* Delete all local stats entries.
*/
suspend fun resetLocalStats() {
database.getLocalMediaStatsProviderDao().deleteAll()
}

/**
* @see MediaDataSource.status
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-FileCopyrightText: 2024-2025 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/

package org.lineageos.twelve.viewmodels

import android.app.Application
import android.content.ComponentName
import androidx.annotation.OptIn
import androidx.core.os.bundleOf
import androidx.lifecycle.AndroidViewModel
import androidx.media3.common.util.UnstableApi
import androidx.media3.session.MediaController
import androidx.media3.session.SessionToken
import kotlinx.coroutines.guava.await
import org.lineageos.twelve.ext.applicationContext
import org.lineageos.twelve.services.PlaybackService
import org.lineageos.twelve.services.PlaybackService.CustomCommand.Companion.sendCustomCommand

class SettingsViewModel(application: Application) : AndroidViewModel(application) {
private val sessionToken by lazy {
SessionToken(
applicationContext,
ComponentName(applicationContext, PlaybackService::class.java)
)
}

class SettingsViewModel(application: Application) : TwelveViewModel(application) {
@OptIn(UnstableApi::class)
suspend fun toggleOffload(offload: Boolean) {
withMediaController {
Expand All @@ -50,15 +38,13 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
}
}

private suspend fun withMediaController(block: suspend MediaController.() -> Unit) {
val mediaController = MediaController.Builder(applicationContext, sessionToken)
.buildAsync()
.await()
suspend fun resetLocalStats() {
mediaRepository.resetLocalStats()
}

try {
block(mediaController)
} finally {
mediaController.release()
private suspend fun withMediaController(block: suspend MediaController.() -> Unit) {
mediaController.value?.let {
block(it)
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@
<string name="skip_silence">Skip silence</string>
<string name="skip_silence_summary">Skip silence when playing audio</string>

<!-- Reset local stats -->
<string name="reset_local_stats">Reset local media playback statistics</string>
<string name="reset_local_stats_confirm_title">Reset local stats</string>
<string name="reset_local_stats_confirm_message">Are you sure you want to reset all local media playback statistics? This action cannot be undone.</string>
<string name="reset_local_stats_confirm_positive">Reset</string>
<string name="reset_local_stats_success">Local media playback statistics have been reset</string>

<!-- Experimental preferences -->
<string name="split_local_devices">Split local devices</string>
<string name="split_local_devices_summary">If enabled, internal storage and external devices (SD cards, USB devices, etc.) will appear under different providers</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
app:summary="@string/skip_silence_summary"
app:title="@string/skip_silence" />

<Preference
app:iconSpaceReserved="false"
app:key="reset_local_stats"
app:title="@string/reset_local_stats" />

</PreferenceCategory>

<PreferenceCategory
Expand Down

0 comments on commit 2d5f3a7

Please sign in to comment.