Skip to content

Commit 18db334

Browse files
Refactor: Clean up code and enable buildConfig
- Enabled `buildConfig = true` in `app/build.gradle.kts` to allow conditional logic based on build types. - Set the initial notification timeout to 10 seconds in debug builds for easier testing. - Refactored `SleepAudioWorker.kt` by removing an unnecessary version check and moving the `buildFocusRequest` function to be a private top-level function. - Cleaned up unused imports in `SleepNotificationReceiver.kt`. - Improved code formatting in `app/build.gradle.kts`.
1 parent bfe781e commit 18db334

File tree

4 files changed

+85
-51
lines changed

4 files changed

+85
-51
lines changed

app/build.gradle.kts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,32 @@ android {
2020
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2121
@Suppress("UnstableApiUsage")
2222
androidResources.localeFilters += listOf(
23-
"ar-rEG" , "bg-rBG" , "bn-rBD" , "de-rDE" , "en" , "es-rGQ" , "es-rMX" , "fil-rPH" , "fr-rFR" , "hi-rIN" , "hu-rHU" , "in-rID" , "it-rIT" , "ja-rJP" , "ko-rKR" , "pl-rPL" , "pt-rBR" , "ro-rRO" , "ru-rRU" , "sv-rSE" , "th-rTH" , "tr-rTR" , "uk-rUA" , "ur-rPK" , "vi-rVN" , "zh-rTW"
23+
"ar-rEG",
24+
"bg-rBG",
25+
"bn-rBD",
26+
"de-rDE",
27+
"en",
28+
"es-rGQ",
29+
"es-rMX",
30+
"fil-rPH",
31+
"fr-rFR",
32+
"hi-rIN",
33+
"hu-rHU",
34+
"in-rID",
35+
"it-rIT",
36+
"ja-rJP",
37+
"ko-rKR",
38+
"pl-rPL",
39+
"pt-rBR",
40+
"ro-rRO",
41+
"ru-rRU",
42+
"sv-rSE",
43+
"th-rTH",
44+
"tr-rTR",
45+
"uk-rUA",
46+
"ur-rPK",
47+
"vi-rVN",
48+
"zh-rTW"
2449
)
2550
}
2651

@@ -64,10 +89,16 @@ android {
6489
buildTypes.forEach { buildType ->
6590
with(receiver = buildType) {
6691
multiDexEnabled = true
67-
proguardFiles(getDefaultProguardFile(name = "proguard-android-optimize.txt") , "proguard-rules.pro")
92+
proguardFiles(
93+
getDefaultProguardFile(name = "proguard-android-optimize.txt"),
94+
"proguard-rules.pro"
95+
)
6896
}
6997
}
7098

99+
buildFeatures {
100+
buildConfig = true
101+
}
71102

72103
compileOptions {
73104
sourceCompatibility = JavaVersion.VERSION_21

app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/notifications/SleepNotification.kt

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import android.content.Context
1212
import android.content.Intent
1313
import android.graphics.drawable.Icon
1414
import android.os.Build
15+
import java.util.concurrent.TimeUnit.SECONDS
16+
import com.d4rk.musicsleeptimer.plus.BuildConfig
1517
import com.d4rk.musicsleeptimer.plus.R
1618
import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.CANCEL
1719
import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.DECREMENT
@@ -28,92 +30,98 @@ import java.util.concurrent.TimeUnit.MILLISECONDS
2830
import java.util.concurrent.TimeUnit.MINUTES
2931

3032
object SleepNotification {
31-
private val TIMEOUT_INITIAL_MILLIS : Long = MINUTES.toMillis(30)
32-
private val TIMEOUT_INCREMENT_MILLIS : Long = MINUTES.toMillis(10)
33-
private val TIMEOUT_DECREMENT_MILLIS : Long = MINUTES.toMillis(10)
33+
private val TIMEOUT_INITIAL_MILLIS: Long =
34+
if (BuildConfig.DEBUG) SECONDS.toMillis(10) else MINUTES.toMillis(30)
35+
private val TIMEOUT_INCREMENT_MILLIS: Long = MINUTES.toMillis(10)
36+
private val TIMEOUT_DECREMENT_MILLIS: Long = MINUTES.toMillis(10)
3437

35-
private enum class Action(private val value : String) {
38+
private enum class Action(private val value: String) {
3639
CANCEL("com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.CANCEL") {
37-
override fun title(context : Context) = context.getText(android.R.string.cancel)
38-
} ,
40+
override fun title(context: Context) = context.getText(android.R.string.cancel)
41+
},
3942
INCREMENT("com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.INCREMENT") {
40-
override fun title(context : Context) = context.getString(
41-
R.string.notification_action_increment , MILLISECONDS.toMinutes(TIMEOUT_INCREMENT_MILLIS)
43+
override fun title(context: Context) = context.getString(
44+
R.string.notification_action_increment,
45+
MILLISECONDS.toMinutes(TIMEOUT_INCREMENT_MILLIS)
4246
)
43-
} ,
47+
},
4448
DECREMENT("com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.DECREMENT") {
45-
override fun title(context : Context) = context.getString(
46-
R.string.notification_action_decrement , MILLISECONDS.toMinutes(TIMEOUT_DECREMENT_MILLIS)
49+
override fun title(context: Context) = context.getString(
50+
R.string.notification_action_decrement,
51+
MILLISECONDS.toMinutes(TIMEOUT_DECREMENT_MILLIS)
4752
)
48-
} , ;
53+
}, ;
4954

5055
companion object {
51-
fun parse(value : String?) : Action? = entries.firstOrNull { it.value == value }
56+
fun parse(value: String?): Action? = entries.firstOrNull { it.value == value }
5257
}
5358

54-
fun intent(context : Context) : Intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
59+
fun intent(context: Context): Intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
5560
serviceIntent(context)
5661
} else {
5762
broadcastIntent(context)
5863
}
5964

6065
@RequiresApi(Build.VERSION_CODES.N)
61-
private fun serviceIntent(context : Context) : Intent =
62-
Intent(context , SleepTileService::class.java).setAction(value)
66+
private fun serviceIntent(context: Context): Intent =
67+
Intent(context, SleepTileService::class.java).setAction(value)
6368

64-
private fun broadcastIntent(context : Context) : Intent =
65-
Intent(context , SleepNotificationReceiver::class.java).setAction(value)
69+
private fun broadcastIntent(context: Context): Intent =
70+
Intent(context, SleepNotificationReceiver::class.java).setAction(value)
6671

67-
fun pendingIntent(context : Context , cancel : Boolean = false) : PendingIntent? {
72+
fun pendingIntent(context: Context, cancel: Boolean = false): PendingIntent? {
6873
val requestCode = value.hashCode()
6974
val intent = intent(context)
7075
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
71-
PendingIntent.getService(context , requestCode , intent , FLAG_IMMUTABLE)
76+
PendingIntent.getService(context, requestCode, intent, FLAG_IMMUTABLE)
7277
} else {
73-
PendingIntent.getBroadcast(context , requestCode , intent , FLAG_IMMUTABLE)
78+
PendingIntent.getBroadcast(context, requestCode, intent, FLAG_IMMUTABLE)
7479
}
7580
return pendingIntent.apply { if (cancel) cancel() }
7681
}
7782

78-
fun action(context : Context , cancel : Boolean = false) : Notification.Action.Builder = Notification.Action.Builder(
79-
Icon.createWithResource(context , 0) , title(context) , pendingIntent(context , cancel)
80-
)
83+
fun action(context: Context, cancel: Boolean = false): Notification.Action.Builder =
84+
Notification.Action.Builder(
85+
Icon.createWithResource(context, 0), title(context), pendingIntent(context, cancel)
86+
)
8187

82-
abstract fun title(context : Context) : CharSequence?
88+
abstract fun title(context: Context): CharSequence?
8389
}
8490

85-
fun Context.notificationManager() : NotificationManager? = getSystemService(NotificationManager::class.java)
91+
fun Context.notificationManager(): NotificationManager? =
92+
getSystemService(NotificationManager::class.java)
8693

87-
fun Context.find() = notificationManager()?.activeNotifications?.firstOrNull { it.id == R.id.notification_id }?.notification
94+
fun Context.find() =
95+
notificationManager()?.activeNotifications?.firstOrNull { it.id == R.id.notification_id }?.notification
8896

89-
fun Context.handle(intent : Intent?) = when (Action.parse(intent?.action)) {
97+
fun Context.handle(intent: Intent?) = when (Action.parse(intent?.action)) {
9098
INCREMENT -> update(TIMEOUT_INCREMENT_MILLIS)
91-
DECREMENT -> update(- TIMEOUT_DECREMENT_MILLIS)
99+
DECREMENT -> update(-TIMEOUT_DECREMENT_MILLIS)
92100
CANCEL -> cancel()
93101
null -> Unit
94102
}
95103

96104
fun Context.toggle() = if (find() == null) show() else cancel()
97105
private fun Context.cancel() = notificationManager()?.cancel(R.id.notification_id) ?: Unit
98-
private fun Context.update(timeout : Long) = find()?.let {
106+
private fun Context.update(timeout: Long) = find()?.let {
99107
it.`when` - currentTimeMillis()
100108
}?.let {
101-
if (it > - timeout) it + timeout else it
109+
if (it > -timeout) it + timeout else it
102110
}?.let {
103111
show(it)
104112
}
105113

106-
private fun Context.show(timeout : Long = TIMEOUT_INITIAL_MILLIS) {
114+
private fun Context.show(timeout: Long = TIMEOUT_INITIAL_MILLIS) {
107115
require(timeout > 0)
108-
val eta : Long = currentTimeMillis() + timeout
109-
val builder : Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
116+
val eta: Long = currentTimeMillis() + timeout
117+
val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
110118
Notification.Builder(this, getString(R.string.notification_channel_id))
111119
} else {
112120
@Suppress("DEPRECATION")
113121
Notification.Builder(this)
114122
}
115123

116-
val notification : Notification = builder
124+
val notification: Notification = builder
117125
.setCategory(CATEGORY_EVENT)
118126
.setVisibility(VISIBILITY_PUBLIC)
119127
.setOnlyAlertOnce(true)
@@ -150,9 +158,9 @@ object SleepNotification {
150158

151159
private fun Context.createNotificationChannel() {
152160
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
153-
val id : String = getString(R.string.notification_channel_id)
154-
val name : CharSequence = getString(R.string.app_name)
155-
val channel : NotificationChannel = NotificationChannel(id, name, IMPORTANCE_LOW).apply {
161+
val id: String = getString(R.string.notification_channel_id)
162+
val name: CharSequence = getString(R.string.app_name)
163+
val channel: NotificationChannel = NotificationChannel(id, name, IMPORTANCE_LOW).apply {
156164
setBypassDnd(true)
157165
lockscreenVisibility = VISIBILITY_PUBLIC
158166
}

app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/receivers/SleepNotificationReceiver.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.content.BroadcastReceiver
44
import android.content.Context
55
import android.content.Intent
66
import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification
7-
import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.handle
87

98
class SleepNotificationReceiver : BroadcastReceiver() {
109
override fun onReceive(context : Context , intent : Intent?) {

app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/workers/SleepAudioWorker.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ class SleepAudioWorker(
6767
val canAdjustVolume: Boolean = !audioManager.isVolumeFixed && initialVolume > 0
6868

6969
val playbackStoppedLatch = CountDownLatch(1)
70-
val focusRequest: AudioFocusRequest = audioManager.buildFocusRequest(
70+
val focusRequest: AudioFocusRequest = buildFocusRequest(
7171
attributes = attributes,
7272
playbackStoppedLatch = playbackStoppedLatch
7373
)
7474

7575
var playbackCallback: AudioManager.AudioPlaybackCallback? = null
76-
var focusGranted: Boolean = false
77-
var playbackStopped: Boolean = false
76+
var focusGranted = false
77+
var playbackStopped = false
7878

7979
return try {
8080
playbackCallback = audioManager.registerPlaybackStopCallback(playbackStoppedLatch)
@@ -106,10 +106,10 @@ class SleepAudioWorker(
106106
playbackStopped = playbackStopped || !audioManager.isMusicActive
107107

108108
Result.success()
109-
} catch (interrupted: InterruptedException) {
109+
} catch (_: InterruptedException) {
110110
Thread.currentThread().interrupt()
111111
Result.failure()
112-
} catch (error: Throwable) {
112+
} catch (_: Throwable) {
113113
Result.failure()
114114
} finally {
115115
playbackCallback?.let(audioManager::unregisterAudioPlaybackCallback)
@@ -133,7 +133,7 @@ class SleepAudioWorker(
133133
.build()
134134
}
135135

136-
private fun AudioManager.buildFocusRequest(
136+
private fun buildFocusRequest(
137137
attributes: AudioAttributes,
138138
playbackStoppedLatch: CountDownLatch
139139
): AudioFocusRequest {
@@ -157,10 +157,6 @@ class SleepAudioWorker(
157157
private fun AudioManager.registerPlaybackStopCallback(
158158
playbackStoppedLatch: CountDownLatch
159159
): AudioManager.AudioPlaybackCallback? {
160-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
161-
return null
162-
}
163-
164160
val callback = object : AudioManager.AudioPlaybackCallback() {
165161
override fun onPlaybackConfigChanged(configs: MutableList<AudioPlaybackConfiguration>) {
166162
if (configs.none(::isRelevantPlaybackActive)) {

0 commit comments

Comments
 (0)