From b1bd45bb330c1334d7509599bb9ad608acd3703a Mon Sep 17 00:00:00 2001 From: Johannes Hoffmann <27806916+j-m-hoffmann@users.noreply.github.com> Date: Sat, 4 Dec 2021 09:46:59 +0100 Subject: [PATCH] refactor: move start method to PlayerService --- .../easynoise/NotificationReceiver.kt | 3 +-- .../easynoise/OutsidePauseReceiver.kt | 2 +- .../com/cliambrown/easynoise/PlayerService.kt | 12 ++++++++++ .../com/cliambrown/easynoise/helpers/Util.kt | 22 ------------------- 4 files changed, 14 insertions(+), 25 deletions(-) delete mode 100644 app/src/main/java/com/cliambrown/easynoise/helpers/Util.kt diff --git a/app/src/main/java/com/cliambrown/easynoise/NotificationReceiver.kt b/app/src/main/java/com/cliambrown/easynoise/NotificationReceiver.kt index 7bee80b..98d13e5 100644 --- a/app/src/main/java/com/cliambrown/easynoise/NotificationReceiver.kt +++ b/app/src/main/java/com/cliambrown/easynoise/NotificationReceiver.kt @@ -3,7 +3,6 @@ package com.cliambrown.easynoise import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import android.os.Build import com.cliambrown.easynoise.helpers.* class NotificationReceiver : BroadcastReceiver() { @@ -14,6 +13,6 @@ class NotificationReceiver : BroadcastReceiver() { if (!actions.contains(action) || action == null) { return } - Util.startPlayerService(context, action) + PlayerService.start(context, action) } } \ No newline at end of file diff --git a/app/src/main/java/com/cliambrown/easynoise/OutsidePauseReceiver.kt b/app/src/main/java/com/cliambrown/easynoise/OutsidePauseReceiver.kt index 42b3f01..b93d4c0 100644 --- a/app/src/main/java/com/cliambrown/easynoise/OutsidePauseReceiver.kt +++ b/app/src/main/java/com/cliambrown/easynoise/OutsidePauseReceiver.kt @@ -48,7 +48,7 @@ class OutsidePauseReceiver : BroadcastReceiver() { } if (playerAction != null) { - Util.startPlayerService(context, playerAction) + PlayerService.start(context, playerAction) } } } \ No newline at end of file diff --git a/app/src/main/java/com/cliambrown/easynoise/PlayerService.kt b/app/src/main/java/com/cliambrown/easynoise/PlayerService.kt index 6269378..8c20b30 100644 --- a/app/src/main/java/com/cliambrown/easynoise/PlayerService.kt +++ b/app/src/main/java/com/cliambrown/easynoise/PlayerService.kt @@ -2,6 +2,7 @@ package com.cliambrown.easynoise import android.app.Activity import android.app.Service +import android.content.Context import android.content.Intent import android.content.IntentFilter import android.content.SharedPreferences @@ -9,6 +10,7 @@ import android.media.AudioAttributes import android.media.AudioManager import android.media.SoundPool import android.os.Binder +import android.os.Build import android.os.IBinder import com.cliambrown.easynoise.helpers.* import android.widget.Toast @@ -272,4 +274,14 @@ class PlayerService : Service(), SoundPool.OnLoadCompleteListener { } if (tempIsPlaying) play(false) } + + companion object { + fun start(context: Context, action: String): Boolean { + Intent(context, PlayerService::class.java).setAction(action).run { + if (Build.VERSION.SDK_INT < 26) context.startService(this) + else context.startForegroundService(this) + } + return true + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/cliambrown/easynoise/helpers/Util.kt b/app/src/main/java/com/cliambrown/easynoise/helpers/Util.kt deleted file mode 100644 index 1abc275..0000000 --- a/app/src/main/java/com/cliambrown/easynoise/helpers/Util.kt +++ /dev/null @@ -1,22 +0,0 @@ -package com.cliambrown.easynoise.helpers - -import android.content.Context -import android.content.Intent -import android.os.Build -import com.cliambrown.easynoise.PlayerService - -class Util { - - companion object { - fun startPlayerService(context: Context, action: String) : Boolean { - val intent = Intent(context, PlayerService::class.java) - intent.setAction(action) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.startForegroundService(intent) - } else { - context.startService(intent) - } - return true - } - } -} \ No newline at end of file