From 38e7693e78f15c75938d43ef345524b2456be4b4 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Fri, 31 Mar 2023 06:09:38 +0530 Subject: [PATCH] Use getSystemService() extension. --- .../com/github/libretube/extensions/HideKeyboard.kt | 5 ++--- .../com/github/libretube/helpers/NavigationHelper.kt | 11 +++++------ .../java/com/github/libretube/helpers/PlayerHelper.kt | 4 ++-- .../com/github/libretube/services/ClosingService.kt | 5 ++--- .../com/github/libretube/services/DownloadService.kt | 3 ++- .../github/libretube/ui/fragments/PlayerFragment.kt | 8 ++++---- .../github/libretube/util/NowPlayingNotification.kt | 6 ++---- .../github/libretube/workers/NotificationWorker.kt | 7 +++---- 8 files changed, 22 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/github/libretube/extensions/HideKeyboard.kt b/app/src/main/java/com/github/libretube/extensions/HideKeyboard.kt index f46ba492c..870186613 100644 --- a/app/src/main/java/com/github/libretube/extensions/HideKeyboard.kt +++ b/app/src/main/java/com/github/libretube/extensions/HideKeyboard.kt @@ -1,11 +1,10 @@ package com.github.libretube.extensions -import android.app.Activity import android.content.Context import android.view.View import android.view.inputmethod.InputMethodManager +import androidx.core.content.getSystemService fun Context.hideKeyboard(view: View) { - val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager - inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0) + getSystemService()!!.hideSoftInputFromWindow(view.windowToken, 0) } diff --git a/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt b/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt index 354a129b3..025075ee5 100644 --- a/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt @@ -4,9 +4,10 @@ import android.app.NotificationManager import android.content.Context import android.content.ContextWrapper import android.content.Intent -import android.content.pm.PackageManager import android.os.Handler import android.os.Looper +import android.os.Process +import androidx.core.content.getSystemService import androidx.core.os.bundleOf import androidx.core.os.postDelayed import androidx.fragment.app.commitNow @@ -127,15 +128,13 @@ object NavigationHelper { */ fun restartMainActivity(context: Context) { // kill player notification - val nManager = context - .getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - nManager.cancelAll() + context.getSystemService()!!.cancelAll() // start a new Intent of the app - val pm: PackageManager = context.packageManager + val pm = context.packageManager val intent = pm.getLaunchIntentForPackage(context.packageName) intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK context.startActivity(intent) // kill the old application - android.os.Process.killProcess(android.os.Process.myPid()) + Process.killProcess(Process.myPid()) } } diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index f370f37ca..f489414cc 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -9,6 +9,7 @@ import android.view.accessibility.CaptioningManager import android.widget.Toast import androidx.annotation.StringRes import androidx.core.app.RemoteActionCompat +import androidx.core.content.getSystemService import androidx.core.graphics.drawable.IconCompat import com.github.libretube.R import com.github.libretube.api.obj.PipedStream @@ -75,8 +76,7 @@ object PlayerHelper { // get the system default caption style fun getCaptionStyle(context: Context): CaptionStyleCompat { - val captioningManager = - context.getSystemService(Context.CAPTIONING_SERVICE) as CaptioningManager + val captioningManager = context.getSystemService()!! return if (!captioningManager.isEnabled) { // system captions are disabled, using android default captions style CaptionStyleCompat.DEFAULT diff --git a/app/src/main/java/com/github/libretube/services/ClosingService.kt b/app/src/main/java/com/github/libretube/services/ClosingService.kt index 12b16c237..e09aa8c47 100644 --- a/app/src/main/java/com/github/libretube/services/ClosingService.kt +++ b/app/src/main/java/com/github/libretube/services/ClosingService.kt @@ -2,9 +2,9 @@ package com.github.libretube.services import android.app.NotificationManager import android.app.Service -import android.content.Context import android.content.Intent import android.os.IBinder +import androidx.core.content.getSystemService import com.github.libretube.constants.PLAYER_NOTIFICATION_ID class ClosingService : Service() { @@ -18,8 +18,7 @@ class ClosingService : Service() { super.onTaskRemoved(rootIntent) // destroy the player notification when the app gets destroyed - val nManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - nManager.cancel(PLAYER_NOTIFICATION_ID) + getSystemService()!!.cancel(PLAYER_NOTIFICATION_ID) // Destroy the service stopSelf() diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index 5c9a317b0..449941743 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -8,6 +8,7 @@ import android.os.IBinder import android.util.SparseBooleanArray import androidx.core.app.NotificationCompat import androidx.core.app.ServiceCompat +import androidx.core.content.getSystemService import androidx.core.util.set import androidx.core.util.valueIterator import androidx.lifecycle.LifecycleService @@ -341,7 +342,7 @@ class DownloadService : LifecycleService() { } private fun notifyForeground() { - notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager + notificationManager = getSystemService()!! summaryNotificationBuilder = NotificationCompat .Builder(this, DOWNLOAD_CHANNEL_ID) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index faaf89f1b..268b7369e 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -24,6 +24,7 @@ import android.view.ViewGroup import android.widget.TextView import android.widget.Toast import androidx.constraintlayout.motion.widget.MotionLayout +import androidx.core.content.getSystemService import androidx.core.net.toUri import androidx.core.os.bundleOf import androidx.core.os.postDelayed @@ -587,12 +588,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { // pauses the player if the screen is turned off // check whether the screen is on - val pm = context?.getSystemService(Context.POWER_SERVICE) as PowerManager - val isScreenOn = pm.isInteractive + val isInteractive = requireContext().getSystemService()!!.isInteractive // pause player if screen off and setting enabled - if ( - this::exoPlayer.isInitialized && !isScreenOn && PlayerHelper.pausePlayerOnScreenOffEnabled + if (this::exoPlayer.isInitialized && !isInteractive && + PlayerHelper.pausePlayerOnScreenOffEnabled ) { exoPlayer.pause() } diff --git a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt index 5b86bc4c7..a353e7de7 100644 --- a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt +++ b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt @@ -15,6 +15,7 @@ import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.PlaybackStateCompat import androidx.annotation.DrawableRes import androidx.core.app.NotificationCompat +import androidx.core.content.getSystemService import androidx.core.graphics.drawable.toBitmap import androidx.core.os.bundleOf import coil.request.ImageRequest @@ -307,10 +308,7 @@ class NowPlayingNotification( player.stop() player.release() - val notificationManager = context.getSystemService( - Context.NOTIFICATION_SERVICE - ) as NotificationManager - notificationManager.cancel(PLAYER_NOTIFICATION_ID) + context.getSystemService()!!.cancel(PLAYER_NOTIFICATION_ID) } companion object { diff --git a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt index aa751a573..eed5cd6a5 100644 --- a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt @@ -7,7 +7,7 @@ import android.content.Intent import android.os.Build import android.util.Log import androidx.core.app.NotificationCompat -import androidx.core.app.NotificationManagerCompat +import androidx.core.content.getSystemService import androidx.work.CoroutineWorker import androidx.work.WorkerParameters import com.github.libretube.R @@ -32,12 +32,11 @@ import kotlinx.coroutines.withContext class NotificationWorker(appContext: Context, parameters: WorkerParameters) : CoroutineWorker(appContext, parameters) { - private val notificationManager = NotificationManagerCompat.from(appContext) + private val notificationManager = appContext.getSystemService()!! // the id where notification channels start private var notificationId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - val nManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - nManager.activeNotifications.size + DOWNLOAD_PROGRESS_NOTIFICATION_ID + notificationManager.activeNotifications.size + DOWNLOAD_PROGRESS_NOTIFICATION_ID } else { DOWNLOAD_PROGRESS_NOTIFICATION_ID }