From 0e1dc52aa755c5c2553c287ca61eeed1c7b9f60b Mon Sep 17 00:00:00 2001 From: Mihai-Cristian Condrea Date: Sun, 29 Dec 2024 16:33:00 +0200 Subject: [PATCH] refactor: Updated dependencies and improved code clarity 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. --- app/build.gradle.kts | 15 +++---- .../plus/ExampleInstrumentedTest.kt | 22 ----------- .../plus/notifications/SleepNotification.kt | 14 +++---- .../plus/receivers/SleepAudioReceiver.kt | 2 +- .../plus/services/SleepTileService.kt | 3 +- .../plus/workers/SleepAudioWorker.kt | 39 +++++++++++-------- app/src/main/res/xml/config_locales.xml | 9 +++-- .../musicsleeptimer/plus/ExampleUnitTest.kt | 16 -------- build.gradle.kts | 10 ++--- gradle/libs.versions.toml | 12 ++---- gradle/wrapper/gradle-wrapper.properties | 4 +- 11 files changed, 53 insertions(+), 93 deletions(-) delete mode 100644 app/src/androidTest/kotlin/com/d4rk/musicsleeptimer/plus/ExampleInstrumentedTest.kt delete mode 100644 app/src/test/kotlin/com/d4rk/musicsleeptimer/plus/ExampleUnitTest.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 404ac61..e1b5ebb 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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 { @@ -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" , @@ -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) } \ No newline at end of file diff --git a/app/src/androidTest/kotlin/com/d4rk/musicsleeptimer/plus/ExampleInstrumentedTest.kt b/app/src/androidTest/kotlin/com/d4rk/musicsleeptimer/plus/ExampleInstrumentedTest.kt deleted file mode 100644 index e5fe095..0000000 --- a/app/src/androidTest/kotlin/com/d4rk/musicsleeptimer/plus/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,22 +0,0 @@ -package com.d4rk.musicsleeptimer.plus - -import androidx.test.ext.junit.runners.AndroidJUnit4 -import androidx.test.platform.app.InstrumentationRegistry -import org.junit.Assert.assertEquals -import org.junit.Test -import org.junit.runner.RunWith - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("com.d4rk.musicsleeptimer.plus" , appContext.packageName) - } -} \ No newline at end of file diff --git a/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/notifications/SleepNotification.kt b/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/notifications/SleepNotification.kt index 72a72ea..02478b7 100644 --- a/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/notifications/SleepNotification.kt +++ b/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/notifications/SleepNotification.kt @@ -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") { @@ -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) @@ -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 } diff --git a/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/receivers/SleepAudioReceiver.kt b/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/receivers/SleepAudioReceiver.kt index a9e9eb1..fd84a59 100644 --- a/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/receivers/SleepAudioReceiver.kt +++ b/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/receivers/SleepAudioReceiver.kt @@ -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) } } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/services/SleepTileService.kt b/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/services/SleepTileService.kt index 85caac7..767a082 100644 --- a/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/services/SleepTileService.kt +++ b/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/services/SleepTileService.kt @@ -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 @@ -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) diff --git a/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/workers/SleepAudioWorker.kt b/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/workers/SleepAudioWorker.kt index 4094c01..af0bb4d 100644 --- a/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/workers/SleepAudioWorker.kt +++ b/app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/workers/SleepAudioWorker.kt @@ -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 @@ -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( @@ -38,7 +39,7 @@ class SleepAudioWorker( } internal fun startWork(context : Context) { - val workRequest = OneTimeWorkRequestBuilder().build() + val workRequest : OneTimeWorkRequest = OneTimeWorkRequestBuilder().build() WorkManager.getInstance(context).enqueueUniqueWork( UNIQUE_WORK_NAME , ExistingWorkPolicy.REPLACE , workRequest @@ -46,35 +47,39 @@ class SleepAudioWorker( } } - 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() } } diff --git a/app/src/main/res/xml/config_locales.xml b/app/src/main/res/xml/config_locales.xml index f9dce01..adf92a6 100644 --- a/app/src/main/res/xml/config_locales.xml +++ b/app/src/main/res/xml/config_locales.xml @@ -1,8 +1,8 @@ - + @@ -11,11 +11,12 @@ - + - + + - + \ No newline at end of file diff --git a/app/src/test/kotlin/com/d4rk/musicsleeptimer/plus/ExampleUnitTest.kt b/app/src/test/kotlin/com/d4rk/musicsleeptimer/plus/ExampleUnitTest.kt deleted file mode 100644 index 045e5be..0000000 --- a/app/src/test/kotlin/com/d4rk/musicsleeptimer/plus/ExampleUnitTest.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.d4rk.musicsleeptimer.plus - -import org.junit.Assert.assertEquals -import org.junit.Test - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4 , 2 + 2) - } -} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index cf539d5..fc09d37 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index dad678e..28794bc 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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] diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3ac83a4..c320371 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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