Skip to content

Commit

Permalink
refactor: Updated dependencies and improved code clarity
Browse files Browse the repository at this point in the history
Improved code quality and project stability:

- Updated app dependencies to the latest versions for enhanced performance and security.
- Added explicit arguments to function calls for better code readability and maintainability.
- Removed unused dependencies to streamline the project.
  • Loading branch information
Mihai-Cristian Condrea committed Dec 29, 2024
1 parent 3846efd commit 0e1dc52
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 93 deletions.
15 changes: 6 additions & 9 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
alias(libs.plugins.googlePlayServices)
alias(libs.plugins.googleFirebase)
alias(notation = libs.plugins.androidApplication)
alias(notation = libs.plugins.jetbrainsKotlinAndroid)
alias(notation = libs.plugins.googlePlayServices)
alias(notation = libs.plugins.googleFirebase)
}

android {
Expand All @@ -12,8 +12,8 @@ android {
applicationId = "com.d4rk.musicsleeptimer.plus"
minSdk = 23
targetSdk = 35
versionCode = 31
versionName = "3.0.3"
versionCode = 32
versionName = "3.0.4"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
resourceConfigurations += listOf(
"en" ,
Expand Down Expand Up @@ -87,7 +87,4 @@ dependencies {
implementation(dependencyNotation = libs.appcompat)
implementation(dependencyNotation = libs.work.runtime.ktx)
implementation(dependencyNotation = libs.multidex)
testImplementation(dependencyNotation = libs.junit)
androidTestImplementation(dependencyNotation = libs.ext.junit)
androidTestImplementation(dependencyNotation = libs.espresso.core)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import java.util.concurrent.TimeUnit.MINUTES

@RequiresApi(Build.VERSION_CODES.O)
object SleepNotification {
private val TIMEOUT_INITIAL_MILLIS = MINUTES.toMillis(30)
private val TIMEOUT_INCREMENT_MILLIS = MINUTES.toMillis(10)
private val TIMEOUT_DECREMENT_MILLIS = MINUTES.toMillis(10)
private val TIMEOUT_INITIAL_MILLIS : Long = MINUTES.toMillis(30)
private val TIMEOUT_INCREMENT_MILLIS : Long = MINUTES.toMillis(10)
private val TIMEOUT_DECREMENT_MILLIS : Long = MINUTES.toMillis(10)

private enum class Action(private val value : String) {
CANCEL("com.d4rk.musicsleeptimer.plus.action.CANCEL") {
Expand Down Expand Up @@ -96,8 +96,8 @@ object SleepNotification {

private fun Context.show(timeout : Long = TIMEOUT_INITIAL_MILLIS) {
require(timeout > 0)
val eta = currentTimeMillis() + timeout
val notification = Notification.Builder(this , getString(R.string.notification_channel_id))
val eta : Long = currentTimeMillis() + timeout
val notification : Notification = Notification.Builder(this , getString(R.string.notification_channel_id))
.setCategory(CATEGORY_EVENT).setVisibility(VISIBILITY_PUBLIC).setOnlyAlertOnce(true)
.setOngoing(true).setSmallIcon(R.drawable.ic_music_off)
.setSubText(DateFormat.getTimeInstance(SHORT).format(Date(eta))).setShowWhen(true)
Expand All @@ -112,9 +112,9 @@ object SleepNotification {
}

private fun Context.createNotificationChannel() {
val id = getString(R.string.notification_channel_id)
val id : String = getString(R.string.notification_channel_id)
val name : CharSequence = getString(R.string.app_name)
val channel = NotificationChannel(id , name , IMPORTANCE_LOW).apply {
val channel : NotificationChannel = NotificationChannel(id , name , IMPORTANCE_LOW).apply {
setBypassDnd(true)
lockscreenVisibility = VISIBILITY_PUBLIC
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.d4rk.musicsleeptimer.plus.workers.SleepAudioWorker
class SleepAudioReceiver : BroadcastReceiver() {
override fun onReceive(context : Context , intent : Intent) {
if (intent.action == SleepAudioWorker.ACTION_SLEEP_AUDIO) {
SleepAudioWorker.startWork(context)
SleepAudioWorker.startWork(context = context)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.d4rk.musicsleeptimer.plus.services

import android.app.Notification
import android.content.ComponentName
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -41,7 +42,7 @@ class SleepTileService : TileService() {
}

private fun refreshTile() = qsTile?.run {
when (val notification = find()) {
when (val notification : Notification? = find()) {
null -> {
state = STATE_INACTIVE
if (SDK_INT >= Q) subtitle = resources.getText(R.string.tile_subtitle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.media.AudioManager
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequest
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.Worker
Expand All @@ -23,13 +24,13 @@ class SleepAudioWorker(
) : Worker(context , workerParams) {

companion object {
private val FADE_STEP_MILLIS = TimeUnit.SECONDS.toMillis(1)
private val RESTORE_VOLUME_MILLIS = TimeUnit.SECONDS.toMillis(2)
private const val UNIQUE_WORK_NAME = "sleep_audio_work"
const val ACTION_SLEEP_AUDIO = "com.d4rk.musicsleeptimer.plus.action.SLEEP_AUDIO"
private val FADE_STEP_MILLIS : Long = TimeUnit.SECONDS.toMillis(1)
private val RESTORE_VOLUME_MILLIS : Long = TimeUnit.SECONDS.toMillis(2)
private const val UNIQUE_WORK_NAME : String = "sleep_audio_work"
const val ACTION_SLEEP_AUDIO : String = "com.d4rk.musicsleeptimer.plus.action.SLEEP_AUDIO"

fun pendingIntent(context : Context) : PendingIntent? {
val intent = Intent(context , SleepAudioReceiver::class.java).apply {
val intent : Intent = Intent(context , SleepAudioReceiver::class.java).apply {
action = ACTION_SLEEP_AUDIO
}
return PendingIntent.getBroadcast(
Expand All @@ -38,43 +39,47 @@ class SleepAudioWorker(
}

internal fun startWork(context : Context) {
val workRequest = OneTimeWorkRequestBuilder<SleepAudioWorker>().build()
val workRequest : OneTimeWorkRequest = OneTimeWorkRequestBuilder<SleepAudioWorker>().build()

WorkManager.getInstance(context).enqueueUniqueWork(
UNIQUE_WORK_NAME , ExistingWorkPolicy.REPLACE , workRequest
)
}
}

override fun doWork() : Result {
return try {
override fun doWork(): Result {
return runCatching {
applicationContext.getSystemService(AudioManager::class.java)?.run {
val volumeIndex = getStreamVolume(AudioManager.STREAM_MUSIC)
val volumeIndex: Int = getStreamVolume(AudioManager.STREAM_MUSIC)

do {
adjustStreamVolume(
AudioManager.STREAM_MUSIC , AudioManager.ADJUST_LOWER , 0
AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, 0
)
Thread.sleep(FADE_STEP_MILLIS)
} while (getStreamVolume(AudioManager.STREAM_MUSIC) > 0)

val attributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build()
val attributes: AudioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()

val focusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(attributes).setOnAudioFocusChangeListener {}.build()
val focusRequest: AudioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(attributes)
.setOnAudioFocusChangeListener {}
.build()

requestAudioFocus(focusRequest)

Thread.sleep(RESTORE_VOLUME_MILLIS)

setStreamVolume(AudioManager.STREAM_MUSIC , volumeIndex , 0)
setStreamVolume(AudioManager.STREAM_MUSIC, volumeIndex, 0)

abandonAudioFocusRequest(focusRequest)

return@run Result.success()
Result.success()
} ?: Result.failure()
} catch (e : Exception) {
}.getOrElse {
Result.failure()
}
}
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/res/xml/config_locales.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
<locale android:name="en" />
<locale android:name="bg" />
<locale android:name="de" />
<locale android:name="en" />
<locale android:name="es" />
<locale android:name="fr" />
<locale android:name="hi" />
Expand All @@ -11,11 +11,12 @@
<locale android:name="it" />
<locale android:name="ja" />
<locale android:name="pl" />
<locale android:name="pt" />
<locale android:name="pt-BR"/>
<locale android:name="ro" />
<locale android:name="ru" />
<locale android:name="sv" />
<locale android:name="th" />
<locale android:name="th"/>
<locale android:name="tr" />
<locale android:name="uk" />
<locale android:name="zh-Hans" />
<locale android:name="zh-Hant"/>
</locale-config>

This file was deleted.

10 changes: 5 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.jetbrainsKotlinAndroid) apply false
alias(libs.plugins.googlePlayServices) apply false
alias(libs.plugins.googleFirebase) apply false
alias(notation = libs.plugins.androidApplication) apply false
alias(notation = libs.plugins.androidLibrary) apply false
alias(notation = libs.plugins.jetbrainsKotlinAndroid) apply false
alias(notation = libs.plugins.googlePlayServices) apply false
alias(notation = libs.plugins.googleFirebase) apply false
}
12 changes: 3 additions & 9 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
[versions]
agp = "8.7.2"
agp = "8.7.3"
appcompat = "1.7.0"
espressoCore = "3.6.1"
firebaseBom = "33.5.1"
kotlin = "2.0.0"
firebaseBom = "33.7.0"
kotlin = "2.0.21"
google-firebase-crashlytics = "3.0.2"
google-services = "4.4.2"
junit = "4.13.2"
junitVersion = "1.2.1"
multidex = "2.0.1"
workRuntimeKtx = "2.10.0"

[libraries]
appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCore" }
ext-junit = { module = "androidx.test.ext:junit", version.ref = "junitVersion" }
firebase-analytics-ktx = { module = "com.google.firebase:firebase-analytics-ktx" }
firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebaseBom" }
firebase-crashlytics-ktx = { module = "com.google.firebase:firebase-crashlytics-ktx" }
firebase-perf = { module = "com.google.firebase:firebase-perf" }
multidex = { module = "androidx.multidex:multidex", version.ref = "multidex" }
junit = { module = "junit:junit", version.ref = "junit" }
work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version.ref = "workRuntimeKtx" }

[plugins]
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Nov 02 09:18:25 EET 2024
#Sun Dec 29 16:27:19 EET 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit 0e1dc52

Please sign in to comment.