Skip to content

Commit

Permalink
Fixed some of the deprecations inside the app.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihai-Cristian Condrea committed Nov 2, 2024
1 parent 1f29b50 commit 3846efd
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 54 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
applicationId = "com.d4rk.musicsleeptimer.plus"
minSdk = 23
targetSdk = 35
versionCode = 30
versionCode = 31
versionName = "3.0.3"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
resourceConfigurations += listOf(
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@
android:value="true" />
</service>

<service android:name=".services.SleepAudioService" />
<receiver
android:name=".receivers.SleepAudioReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.d4rk.musicsleeptimer.plus.action.SLEEP_AUDIO" />
</intent-filter>
</receiver>

<service
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.d4rk.musicsleeptimer.plus.R
import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.CANCEL
import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.DECREMENT
import com.d4rk.musicsleeptimer.plus.notifications.SleepNotification.Action.INCREMENT
import com.d4rk.musicsleeptimer.plus.services.SleepAudioService
import com.d4rk.musicsleeptimer.plus.workers.SleepAudioWorker
import com.d4rk.musicsleeptimer.plus.services.SleepTileService
import java.lang.System.currentTimeMillis
import java.text.DateFormat
Expand Down Expand Up @@ -102,7 +102,8 @@ object SleepNotification {
.setOngoing(true).setSmallIcon(R.drawable.ic_music_off)
.setSubText(DateFormat.getTimeInstance(SHORT).format(Date(eta))).setShowWhen(true)
.setWhen(eta).setUsesChronometer(true).setChronometerCountDown(true)
.setTimeoutAfter(timeout).setDeleteIntent(SleepAudioService.pendingIntent(this))
.setTimeoutAfter(timeout)
.setDeleteIntent(SleepAudioWorker.pendingIntent(this)) // FIXME: Unresolved reference: pendingIntent
.addAction(INCREMENT.action(this).build()).addAction(
DECREMENT.action(this , cancel = timeout <= TIMEOUT_DECREMENT_MILLIS).build()
).addAction(CANCEL.action(this).build()).build()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.d4rk.musicsleeptimer.plus.receivers

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi
import com.d4rk.musicsleeptimer.plus.workers.SleepAudioWorker

@RequiresApi(Build.VERSION_CODES.O)
class SleepAudioReceiver : BroadcastReceiver() {
override fun onReceive(context : Context , intent : Intent) {
if (intent.action == SleepAudioWorker.ACTION_SLEEP_AUDIO) {
SleepAudioWorker.startWork(context)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.d4rk.musicsleeptimer.plus.workers

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.media.AudioAttributes
import android.media.AudioFocusRequest
import android.media.AudioManager
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.d4rk.musicsleeptimer.plus.receivers.SleepAudioReceiver
import java.util.concurrent.TimeUnit

@RequiresApi(Build.VERSION_CODES.O)
class SleepAudioWorker(
context : Context ,
workerParams : WorkerParameters ,
) : 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"

fun pendingIntent(context : Context) : PendingIntent? {
val intent = Intent(context , SleepAudioReceiver::class.java).apply {
action = ACTION_SLEEP_AUDIO
}
return PendingIntent.getBroadcast(
context , 0 , intent , PendingIntent.FLAG_IMMUTABLE
)
}

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

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

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

do {
adjustStreamVolume(
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 focusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(attributes).setOnAudioFocusChangeListener {}.build()

requestAudioFocus(focusRequest)

Thread.sleep(RESTORE_VOLUME_MILLIS)

setStreamVolume(AudioManager.STREAM_MUSIC , volumeIndex , 0)

abandonAudioFocusRequest(focusRequest)

return@run Result.success()
} ?: Result.failure()
} catch (e : Exception) {
Result.failure()
}
}
}

0 comments on commit 3846efd

Please sign in to comment.