Unify system service calls
Unify the calls to getSystemService to a custom function that uses the ContextCompat getSystemService instead of the clunky vanilla getSystemService code.
This commit is contained in:
parent
2f5a6984d9
commit
9d83619811
4 changed files with 26 additions and 14 deletions
|
@ -4,12 +4,12 @@ import android.animation.ValueAnimator
|
|||
import android.content.Context
|
||||
import android.media.AudioManager
|
||||
import androidx.core.animation.addListener
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.media.AudioFocusRequestCompat
|
||||
import androidx.media.AudioManagerCompat
|
||||
import com.google.android.exoplayer2.SimpleExoPlayer
|
||||
import org.oxycblt.auxio.playback.state.PlaybackStateManager
|
||||
import org.oxycblt.auxio.settings.SettingsManager
|
||||
import org.oxycblt.auxio.ui.getSystemServiceSafe
|
||||
|
||||
/**
|
||||
* Object that manages the AudioFocus state.
|
||||
|
@ -20,9 +20,7 @@ class AudioReactor(
|
|||
context: Context,
|
||||
private val player: SimpleExoPlayer
|
||||
) : AudioManager.OnAudioFocusChangeListener {
|
||||
private val audioManager = ContextCompat.getSystemService(
|
||||
context, AudioManager::class.java
|
||||
) ?: error("Cannot obtain AudioManager.")
|
||||
private val audioManager = context.getSystemServiceSafe(AudioManager::class)
|
||||
|
||||
private val settingsManager = SettingsManager.getInstance()
|
||||
private val playbackManager = PlaybackStateManager.getInstance()
|
||||
|
|
|
@ -182,15 +182,14 @@ class PlaybackNotification private constructor(
|
|||
const val ACTION_EXIT = "ACTION_AUXIO_EXIT_" + BuildConfig.BUILD_TYPE
|
||||
|
||||
/**
|
||||
* Build a new instance of [PlaybackNotification] from a [context] and [mediaSession]
|
||||
* Build a new instance of [PlaybackNotification].
|
||||
*/
|
||||
fun from(context: Context, mediaSession: MediaSessionCompat): PlaybackNotification {
|
||||
fun from(
|
||||
context: Context,
|
||||
notificationManager: NotificationManager,
|
||||
mediaSession: MediaSessionCompat
|
||||
): PlaybackNotification {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
// Create the notification channel if required.
|
||||
val notificationManager = context.getSystemService(
|
||||
Context.NOTIFICATION_SERVICE
|
||||
) as NotificationManager
|
||||
|
||||
val channel = NotificationChannel(
|
||||
CHANNEL_ID, context.getString(R.string.info_channel_name),
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
|
|
|
@ -43,6 +43,7 @@ import org.oxycblt.auxio.music.toURI
|
|||
import org.oxycblt.auxio.playback.state.LoopMode
|
||||
import org.oxycblt.auxio.playback.state.PlaybackStateManager
|
||||
import org.oxycblt.auxio.settings.SettingsManager
|
||||
import org.oxycblt.auxio.ui.getSystemServiceSafe
|
||||
|
||||
/**
|
||||
* A service that manages the system-side aspects of playback, such as:
|
||||
|
@ -135,8 +136,8 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
|||
|
||||
// --- NOTIFICATION SETUP ---
|
||||
|
||||
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
notification = PlaybackNotification.from(this, mediaSession)
|
||||
notificationManager = getSystemServiceSafe(NotificationManager::class)
|
||||
notification = PlaybackNotification.from(this, notificationManager, mediaSession)
|
||||
|
||||
// --- PLAYBACKSTATEMANAGER SETUP ---
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ import com.reddit.indicatorfastscroll.FastScrollItemIndicator
|
|||
import com.reddit.indicatorfastscroll.FastScrollerView
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.logE
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
// --- VIEW CONFIGURATION ---
|
||||
|
||||
|
@ -79,6 +80,19 @@ fun Context.getPlural(@PluralsRes pluralsRes: Int, value: Int): String {
|
|||
return resources.getQuantityString(pluralsRes, value, value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for getting a system service without nullability issues.
|
||||
* @param T The system service in question.
|
||||
* @param serviceClass The service's kotlin class [Java class will be used in function call]
|
||||
* @return The system service
|
||||
* @throws IllegalStateException If the system service cannot be retrieved.
|
||||
*/
|
||||
fun <T : Any> Context.getSystemServiceSafe(serviceClass: KClass<T>): T {
|
||||
return checkNotNull(ContextCompat.getSystemService(this, serviceClass.java)) {
|
||||
"System service ${serviceClass.simpleName} could not be instantiated"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a color.
|
||||
* @param context [Context] required
|
||||
|
@ -221,7 +235,7 @@ private fun isSystemBarOnBottom(activity: Activity): Boolean {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
(activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager).apply {
|
||||
(activity.getSystemServiceSafe(WindowManager::class)).apply {
|
||||
defaultDisplay.getRealSize(realPoint)
|
||||
defaultDisplay.getMetrics(metrics)
|
||||
|
||||
|
|
Loading…
Reference in a new issue