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
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<InputMethodManager>()!!.hideSoftInputFromWindow(view.windowToken, 0)
}

View File

@ -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<NotificationManager>()!!.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())
}
}

View File

@ -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<CaptioningManager>()!!
return if (!captioningManager.isEnabled) {
// system captions are disabled, using android default captions style
CaptionStyleCompat.DEFAULT

View File

@ -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<NotificationManager>()!!.cancel(PLAYER_NOTIFICATION_ID)
// Destroy the service
stopSelf()

View File

@ -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)

View File

@ -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<PowerManager>()!!.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()
}

View File

@ -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<NotificationManager>()!!.cancel(PLAYER_NOTIFICATION_ID)
}
companion object {

View File

@ -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<NotificationManager>()!!
// 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
}