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:
OxygenCobalt 2021-03-13 10:50:39 -07:00
parent 2f5a6984d9
commit 9d83619811
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
4 changed files with 26 additions and 14 deletions

View file

@ -4,12 +4,12 @@ import android.animation.ValueAnimator
import android.content.Context import android.content.Context
import android.media.AudioManager import android.media.AudioManager
import androidx.core.animation.addListener import androidx.core.animation.addListener
import androidx.core.content.ContextCompat
import androidx.media.AudioFocusRequestCompat import androidx.media.AudioFocusRequestCompat
import androidx.media.AudioManagerCompat import androidx.media.AudioManagerCompat
import com.google.android.exoplayer2.SimpleExoPlayer import com.google.android.exoplayer2.SimpleExoPlayer
import org.oxycblt.auxio.playback.state.PlaybackStateManager import org.oxycblt.auxio.playback.state.PlaybackStateManager
import org.oxycblt.auxio.settings.SettingsManager import org.oxycblt.auxio.settings.SettingsManager
import org.oxycblt.auxio.ui.getSystemServiceSafe
/** /**
* Object that manages the AudioFocus state. * Object that manages the AudioFocus state.
@ -20,9 +20,7 @@ class AudioReactor(
context: Context, context: Context,
private val player: SimpleExoPlayer private val player: SimpleExoPlayer
) : AudioManager.OnAudioFocusChangeListener { ) : AudioManager.OnAudioFocusChangeListener {
private val audioManager = ContextCompat.getSystemService( private val audioManager = context.getSystemServiceSafe(AudioManager::class)
context, AudioManager::class.java
) ?: error("Cannot obtain AudioManager.")
private val settingsManager = SettingsManager.getInstance() private val settingsManager = SettingsManager.getInstance()
private val playbackManager = PlaybackStateManager.getInstance() private val playbackManager = PlaybackStateManager.getInstance()

View file

@ -182,15 +182,14 @@ class PlaybackNotification private constructor(
const val ACTION_EXIT = "ACTION_AUXIO_EXIT_" + BuildConfig.BUILD_TYPE 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) { 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( val channel = NotificationChannel(
CHANNEL_ID, context.getString(R.string.info_channel_name), CHANNEL_ID, context.getString(R.string.info_channel_name),
NotificationManager.IMPORTANCE_DEFAULT NotificationManager.IMPORTANCE_DEFAULT

View file

@ -43,6 +43,7 @@ import org.oxycblt.auxio.music.toURI
import org.oxycblt.auxio.playback.state.LoopMode import org.oxycblt.auxio.playback.state.LoopMode
import org.oxycblt.auxio.playback.state.PlaybackStateManager import org.oxycblt.auxio.playback.state.PlaybackStateManager
import org.oxycblt.auxio.settings.SettingsManager import org.oxycblt.auxio.settings.SettingsManager
import org.oxycblt.auxio.ui.getSystemServiceSafe
/** /**
* A service that manages the system-side aspects of playback, such as: * 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 --- // --- NOTIFICATION SETUP ---
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager = getSystemServiceSafe(NotificationManager::class)
notification = PlaybackNotification.from(this, mediaSession) notification = PlaybackNotification.from(this, notificationManager, mediaSession)
// --- PLAYBACKSTATEMANAGER SETUP --- // --- PLAYBACKSTATEMANAGER SETUP ---

View file

@ -27,6 +27,7 @@ import com.reddit.indicatorfastscroll.FastScrollItemIndicator
import com.reddit.indicatorfastscroll.FastScrollerView import com.reddit.indicatorfastscroll.FastScrollerView
import org.oxycblt.auxio.R import org.oxycblt.auxio.R
import org.oxycblt.auxio.logE import org.oxycblt.auxio.logE
import kotlin.reflect.KClass
// --- VIEW CONFIGURATION --- // --- VIEW CONFIGURATION ---
@ -79,6 +80,19 @@ fun Context.getPlural(@PluralsRes pluralsRes: Int, value: Int): String {
return resources.getQuantityString(pluralsRes, value, value) 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. * Resolve a color.
* @param context [Context] required * @param context [Context] required
@ -221,7 +235,7 @@ private fun isSystemBarOnBottom(activity: Activity): Boolean {
} }
} }
} else { } else {
(activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager).apply { (activity.getSystemServiceSafe(WindowManager::class)).apply {
defaultDisplay.getRealSize(realPoint) defaultDisplay.getRealSize(realPoint)
defaultDisplay.getMetrics(metrics) defaultDisplay.getMetrics(metrics)