Skip to content
Open
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
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
<activity
android:name=".app.activities.PlayerActivity"
android:exported="false" />
<activity
android:name=".app.activities.SettingsActivity"
android:exported="false" />

<receiver
android:name="androidx.media3.session.MediaButtonReceiver"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -26,6 +27,7 @@ import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.HorizontalDivider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand Down Expand Up @@ -192,15 +194,35 @@ class BookListActivity : ComponentActivity() {
.padding(horizontal = 16.dp)
)
} else {
// Search button
Button(
onClick = onSearchToggle,
modifier = Modifier.size(40.dp)
// Button row with search and settings
Row(
horizontalArrangement = Arrangement.spacedBy(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
imageVector = Icons.Default.Search,
contentDescription = "Search"
)
// Search button
Button(
onClick = onSearchToggle,
modifier = Modifier.size(40.dp)
) {
Icon(
imageVector = Icons.Default.Search,
contentDescription = "Search"
)
}

// Settings button
Button(
onClick = {
val intent = Intent(this@BookListActivity, SettingsActivity::class.java)
startActivity(intent)
},
modifier = Modifier.size(40.dp)
) {
Icon(
imageVector = Icons.Default.Settings,
contentDescription = "Settings"
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package kaf.audiobookshelfwearos.app.activities

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
import androidx.wear.compose.material.*
import kaf.audiobookshelfwearos.app.theme.AudiobookshelfWearOSTheme
import kaf.audiobookshelfwearos.app.userdata.UserDataManager

class SettingsActivity : ComponentActivity() {
private lateinit var userDataManager: UserDataManager

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
userDataManager = UserDataManager(this)

setContent {
AudiobookshelfWearOSTheme {
SettingsScreen()
}
}
}

@Composable
private fun SettingsScreen() {
val listState = rememberScalingLazyListState()
var smartDeleteEnabled by remember { mutableStateOf(userDataManager.smartDeleteEnabled) }
var maxDownloads by remember { mutableStateOf(userDataManager.smartDeleteMaxDownloads) }

ScalingLazyColumn(
state = listState,
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
item {
Text(
text = "Settings",
style = MaterialTheme.typography.title2,
textAlign = TextAlign.Center,
modifier = Modifier.padding(vertical = 16.dp)
)
}

item {
ToggleChip(
checked = smartDeleteEnabled,
onCheckedChange = { enabled ->
smartDeleteEnabled = enabled
userDataManager.smartDeleteEnabled = enabled
},
label = {
Text("Smart Delete")
},
toggleControl = {
Switch(
checked = smartDeleteEnabled,
onCheckedChange = null
)
},
modifier = Modifier.fillMaxWidth()
)
}

if (smartDeleteEnabled) {
item {
Text(
text = "Old audiobooks will be deleted once this amount is reached:",
style = MaterialTheme.typography.body2,
modifier = Modifier.padding(vertical = 8.dp)
)
}

item {
Text(
text = "Maximum Downloads",
style = MaterialTheme.typography.body1,
modifier = Modifier.padding(vertical = 8.dp)
)
}

item {
Row(
horizontalArrangement = Arrangement.spacedBy(16.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.padding(horizontal = 16.dp)
) {
// Minus button
Button(
onClick = {
if (maxDownloads > 1) {
maxDownloads--
userDataManager.smartDeleteMaxDownloads = maxDownloads
}
},
modifier = Modifier.size(40.dp),
enabled = maxDownloads > 1
) {
Text("-")
}

// Current value
Text(
text = "$maxDownloads",
style = MaterialTheme.typography.title1,
modifier = Modifier.width(40.dp),
textAlign = TextAlign.Center
)

// Plus button
Button(
onClick = {
if (maxDownloads < 20) {
maxDownloads++
userDataManager.smartDeleteMaxDownloads = maxDownloads
}
},
modifier = Modifier.size(40.dp),
enabled = maxDownloads < 20
) {
Text("+")
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kaf.audiobookshelfwearos.app.data

data class SmartDeleteSettings(
val isEnabled: Boolean = true,
val maxDownloads: Int = 5
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import kaf.audiobookshelfwearos.app.data.DownloadState
import kaf.audiobookshelfwearos.app.data.Track
import kaf.audiobookshelfwearos.app.userdata.UserDataManager
import kaf.audiobookshelfwearos.app.utils.DownloadProgressCalculator
import kaf.audiobookshelfwearos.app.utils.SmartDeleteManager
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
Expand Down Expand Up @@ -205,6 +206,10 @@ class MyDownloadService : DownloadService(
Timber.i("Download completed: " + download.request.id)
// Clear speed history for completed downloads
DownloadProgressCalculator.clearSpeedHistory(download.request.id)

// Trigger smart delete after download completion
val smartDeleteManager = SmartDeleteManager(context)
smartDeleteManager.triggerSmartDeleteAfterDownload()
}

Timber.d("onDownloadChanged ${download.state}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class UserDataManager(context: Context) {
private const val KEY_TOKEN = "token"
private const val KEY_USERID = "userid"
private const val KEY_OFFLINEMODE = "offlinemode"
private const val KEY_SMART_DELETE_ENABLED = "smart_delete_enabled"
private const val KEY_SMART_DELETE_MAX_DOWNLOADS = "smart_delete_max_downloads"
}

private val masterKey = MasterKey.Builder(context)
Expand Down Expand Up @@ -63,6 +65,14 @@ class UserDataManager(context: Context) {
get() = sharedPreferences.getBoolean(KEY_OFFLINEMODE, false)
set(value) = sharedPreferences.edit().putBoolean(KEY_OFFLINEMODE, value).apply()

var smartDeleteEnabled: Boolean
get() = sharedPreferences.getBoolean(KEY_SMART_DELETE_ENABLED, true)
set(value) = sharedPreferences.edit().putBoolean(KEY_SMART_DELETE_ENABLED, value).apply()

var smartDeleteMaxDownloads: Int
get() = sharedPreferences.getInt(KEY_SMART_DELETE_MAX_DOWNLOADS, 5)
set(value) = sharedPreferences.edit().putInt(KEY_SMART_DELETE_MAX_DOWNLOADS, value).apply()

fun clearUserData() {
sharedPreferences.edit().clear().apply()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package kaf.audiobookshelfwearos.app.utils

import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
import android.content.Context
import android.widget.Toast
import androidx.media3.common.util.Log
import kaf.audiobookshelfwearos.app.MainApp
import kaf.audiobookshelfwearos.app.data.LibraryItem
import kaf.audiobookshelfwearos.app.services.MyDownloadService
import kaf.audiobookshelfwearos.app.userdata.UserDataManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import timber.log.Timber

class SmartDeleteManager(private val context: Context) {
private val userDataManager = UserDataManager(context)
private val database = (context.applicationContext as MainApp).database

fun triggerSmartDeleteAfterDownload() {
if (!userDataManager.smartDeleteEnabled) return

CoroutineScope(Dispatchers.IO).launch {
delay(3_000) // 3 second delay
performSmartDelete()
}
}

@OptIn(UnstableApi::class)
private suspend fun performSmartDelete() {
try {
val downloadManager = MyDownloadService.getDownloadManager(context)
val downloadedItems = database.libraryItemDao().getAllLibraryItems()
.filter { it.isDownloaded(context) }

// Get download completion order by checking download index
val itemsWithDownloadInfo = downloadedItems.mapNotNull { item ->
val firstTrack = item.media.tracks.firstOrNull()
if (firstTrack != null) {
val download = downloadManager.downloadIndex.getDownload(firstTrack.contentUrl)
if (download != null) {
item to download.updateTimeMs
} else null
} else null
}.sortedBy { it.second } // Sort by download update time (oldest first)

val maxDownloads = userDataManager.smartDeleteMaxDownloads
Timber.d("Smart delete max count: ${maxDownloads}")
val excessCount = itemsWithDownloadInfo.size - maxDownloads
Timber.d("Smart delete excess count: ${excessCount}")

if (excessCount > 0) {
val itemsToDelete = itemsWithDownloadInfo.take(excessCount).map { it.first }

for (item in itemsToDelete) {
// Remove downloads using the existing service
for (track in item.media.tracks) {
MyDownloadService.sendRemoveDownload(context, track)
}

// Remove from database
database.libraryItemDao().deleteLibraryItem(item)

CoroutineScope(Dispatchers.Main).launch {
Toast.makeText(
context,
"Removed ${item.title} to make space",
Toast.LENGTH_SHORT
).show()
}

Timber.d("Smart delete removed: ${item.title}")
}
}
} catch (e: Exception) {
Timber.e(e, "Error during smart delete")
}
}
}