Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use getSystemService() extension. #3474

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down