-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed some of the deprecations inside the app.
- Loading branch information
Mihai-Cristian Condrea
committed
Nov 2, 2024
1 parent
1f29b50
commit 3846efd
Showing
6 changed files
with
109 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/receivers/SleepAudioReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
50 changes: 0 additions & 50 deletions
50
app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/services/SleepAudioService.kt
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
app/src/main/kotlin/com/d4rk/musicsleeptimer/plus/workers/SleepAudioWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |