Merge pull request #3474 from Isira-Seneviratne/getSystemService_extension

Use getSystemService() extension.
This commit is contained in:
Isira Seneviratne 2023-04-02 17:04:33 +05:30 committed by GitHub
commit 26335ebd61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 22 additions and 27 deletions

View File

@ -1,11 +1,10 @@
package com.github.libretube.extensions package com.github.libretube.extensions
import android.app.Activity
import android.content.Context import android.content.Context
import android.view.View import android.view.View
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import androidx.core.content.getSystemService
fun Context.hideKeyboard(view: View) { fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager getSystemService<InputMethodManager>()!!.hideSoftInputFromWindow(view.windowToken, 0)
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
} }

View File

@ -4,9 +4,10 @@ import android.app.NotificationManager
import android.content.Context import android.content.Context
import android.content.ContextWrapper import android.content.ContextWrapper
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.os.Process
import androidx.core.content.getSystemService
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.os.postDelayed import androidx.core.os.postDelayed
import androidx.fragment.app.commitNow import androidx.fragment.app.commitNow
@ -127,15 +128,13 @@ object NavigationHelper {
*/ */
fun restartMainActivity(context: Context) { fun restartMainActivity(context: Context) {
// kill player notification // kill player notification
val nManager = context context.getSystemService<NotificationManager>()!!.cancelAll()
.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
nManager.cancelAll()
// start a new Intent of the app // start a new Intent of the app
val pm: PackageManager = context.packageManager val pm = context.packageManager
val intent = pm.getLaunchIntentForPackage(context.packageName) val intent = pm.getLaunchIntentForPackage(context.packageName)
intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
context.startActivity(intent) context.startActivity(intent)
// kill the old application // kill the old application
android.os.Process.killProcess(android.os.Process.myPid()) Process.killProcess(Process.myPid())
} }
} }

View File

@ -9,6 +9,7 @@ import android.view.accessibility.CaptioningManager
import android.widget.Toast import android.widget.Toast
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.core.app.RemoteActionCompat import androidx.core.app.RemoteActionCompat
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.IconCompat import androidx.core.graphics.drawable.IconCompat
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.obj.PipedStream import com.github.libretube.api.obj.PipedStream
@ -75,8 +76,7 @@ object PlayerHelper {
// get the system default caption style // get the system default caption style
fun getCaptionStyle(context: Context): CaptionStyleCompat { fun getCaptionStyle(context: Context): CaptionStyleCompat {
val captioningManager = val captioningManager = context.getSystemService<CaptioningManager>()!!
context.getSystemService(Context.CAPTIONING_SERVICE) as CaptioningManager
return if (!captioningManager.isEnabled) { return if (!captioningManager.isEnabled) {
// system captions are disabled, using android default captions style // system captions are disabled, using android default captions style
CaptionStyleCompat.DEFAULT CaptionStyleCompat.DEFAULT

View File

@ -2,9 +2,9 @@ package com.github.libretube.services
import android.app.NotificationManager import android.app.NotificationManager
import android.app.Service import android.app.Service
import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.IBinder import android.os.IBinder
import androidx.core.content.getSystemService
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
class ClosingService : Service() { class ClosingService : Service() {
@ -18,8 +18,7 @@ class ClosingService : Service() {
super.onTaskRemoved(rootIntent) super.onTaskRemoved(rootIntent)
// destroy the player notification when the app gets destroyed // destroy the player notification when the app gets destroyed
val nManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager getSystemService<NotificationManager>()!!.cancel(PLAYER_NOTIFICATION_ID)
nManager.cancel(PLAYER_NOTIFICATION_ID)
// Destroy the service // Destroy the service
stopSelf() stopSelf()

View File

@ -8,6 +8,7 @@ import android.os.IBinder
import android.util.SparseBooleanArray import android.util.SparseBooleanArray
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat import androidx.core.app.ServiceCompat
import androidx.core.content.getSystemService
import androidx.core.util.set import androidx.core.util.set
import androidx.core.util.valueIterator import androidx.core.util.valueIterator
import androidx.lifecycle.LifecycleService import androidx.lifecycle.LifecycleService
@ -341,7 +342,7 @@ class DownloadService : LifecycleService() {
} }
private fun notifyForeground() { private fun notifyForeground() {
notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager notificationManager = getSystemService()!!
summaryNotificationBuilder = NotificationCompat summaryNotificationBuilder = NotificationCompat
.Builder(this, DOWNLOAD_CHANNEL_ID) .Builder(this, DOWNLOAD_CHANNEL_ID)

View File

@ -24,6 +24,7 @@ import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.constraintlayout.motion.widget.MotionLayout import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.core.content.getSystemService
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.os.postDelayed import androidx.core.os.postDelayed
@ -587,12 +588,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// pauses the player if the screen is turned off // pauses the player if the screen is turned off
// check whether the screen is on // check whether the screen is on
val pm = context?.getSystemService(Context.POWER_SERVICE) as PowerManager val isInteractive = requireContext().getSystemService<PowerManager>()!!.isInteractive
val isScreenOn = pm.isInteractive
// pause player if screen off and setting enabled // pause player if screen off and setting enabled
if ( if (this::exoPlayer.isInitialized && !isInteractive &&
this::exoPlayer.isInitialized && !isScreenOn && PlayerHelper.pausePlayerOnScreenOffEnabled PlayerHelper.pausePlayerOnScreenOffEnabled
) { ) {
exoPlayer.pause() exoPlayer.pause()
} }

View File

@ -15,6 +15,7 @@ import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat import android.support.v4.media.session.PlaybackStateCompat
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.toBitmap import androidx.core.graphics.drawable.toBitmap
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import coil.request.ImageRequest import coil.request.ImageRequest
@ -307,10 +308,7 @@ class NowPlayingNotification(
player.stop() player.stop()
player.release() player.release()
val notificationManager = context.getSystemService( context.getSystemService<NotificationManager>()!!.cancel(PLAYER_NOTIFICATION_ID)
Context.NOTIFICATION_SERVICE
) as NotificationManager
notificationManager.cancel(PLAYER_NOTIFICATION_ID)
} }
companion object { companion object {

View File

@ -7,7 +7,7 @@ import android.content.Intent
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.content.getSystemService
import androidx.work.CoroutineWorker import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import com.github.libretube.R import com.github.libretube.R
@ -32,12 +32,11 @@ import kotlinx.coroutines.withContext
class NotificationWorker(appContext: Context, parameters: WorkerParameters) : class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
CoroutineWorker(appContext, parameters) { CoroutineWorker(appContext, parameters) {
private val notificationManager = NotificationManagerCompat.from(appContext) private val notificationManager = appContext.getSystemService<NotificationManager>()!!
// the id where notification channels start // the id where notification channels start
private var notificationId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { private var notificationId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val nManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.activeNotifications.size + DOWNLOAD_PROGRESS_NOTIFICATION_ID
nManager.activeNotifications.size + DOWNLOAD_PROGRESS_NOTIFICATION_ID
} else { } else {
DOWNLOAD_PROGRESS_NOTIFICATION_ID DOWNLOAD_PROGRESS_NOTIFICATION_ID
} }