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 from Core KTX #2136

Merged
merged 2 commits into from
Jan 18, 2022
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
Expand Up @@ -5,9 +5,8 @@ import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
Expand Down Expand Up @@ -35,6 +34,7 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.RemoteInput
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.core.text.HtmlCompat
import androidx.core.text.isDigitsOnly
import com.google.firebase.messaging.FirebaseMessagingService
Expand Down Expand Up @@ -70,7 +70,6 @@ import java.net.URL
import java.net.URLDecoder
import java.util.Locale
import javax.inject.Inject
import kotlin.collections.HashMap
import io.homeassistant.companion.android.common.R as commonR

@AndroidEntryPoint
Expand Down Expand Up @@ -407,7 +406,7 @@ class MessagingService : FirebaseMessagingService() {
var textToSpeech: TextToSpeech? = null
var tts = data[TITLE]
val audioManager =
applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
applicationContext.getSystemService<AudioManager>()!!
val currentAlarmVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM)
val maxAlarmVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM)
if (tts.isNullOrEmpty())
Expand Down Expand Up @@ -477,7 +476,7 @@ class MessagingService : FirebaseMessagingService() {
COMMAND_DND -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val notificationManager =
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
applicationContext.getSystemService<NotificationManager>()!!
if (!notificationManager.isNotificationPolicyAccessGranted) {
notifyMissingPermission(data[MESSAGE].toString())
} else {
Expand All @@ -499,10 +498,10 @@ class MessagingService : FirebaseMessagingService() {
}
COMMAND_RINGER_MODE -> {
val audioManager =
applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
applicationContext.getSystemService<AudioManager>()!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val notificationManager =
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
applicationContext.getSystemService<NotificationManager>()!!
if (!notificationManager.isNotificationPolicyAccessGranted) {
notifyMissingPermission(data[MESSAGE].toString())
} else {
Expand Down Expand Up @@ -539,10 +538,10 @@ class MessagingService : FirebaseMessagingService() {
}
COMMAND_VOLUME_LEVEL -> {
val audioManager =
applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
applicationContext.getSystemService<AudioManager>()!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val notificationManager =
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
applicationContext.getSystemService<NotificationManager>()!!
if (!notificationManager.isNotificationPolicyAccessGranted) {
notifyMissingPermission(data[MESSAGE].toString())
} else {
Expand All @@ -561,11 +560,12 @@ class MessagingService : FirebaseMessagingService() {
}
}
COMMAND_BLUETOOTH -> {
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
val bluetoothAdapter =
applicationContext.getSystemService<BluetoothManager>()?.adapter
if (title == TURN_OFF)
bluetoothAdapter.disable()
bluetoothAdapter?.disable()
if (title == TURN_ON)
bluetoothAdapter.enable()
bluetoothAdapter?.enable()
}
COMMAND_BLE_TRANSMITTER -> {
if (title == TURN_OFF)
Expand Down Expand Up @@ -613,7 +613,7 @@ class MessagingService : FirebaseMessagingService() {
}

val powerManager =
applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
applicationContext.getSystemService<PowerManager>()!!
val wakeLock = powerManager.newWakeLock(
PowerManager.FULL_WAKE_LOCK or
PowerManager.ACQUIRE_CAUSES_WAKEUP or
Expand Down Expand Up @@ -1338,7 +1338,7 @@ class MessagingService : FirebaseMessagingService() {
private fun processMediaCommand(data: Map<String, String>) {
val title = data[TITLE]
val mediaSessionManager =
applicationContext.getSystemService(Context.MEDIA_SESSION_SERVICE) as MediaSessionManager
applicationContext.getSystemService<MediaSessionManager>()!!
val mediaList = mediaSessionManager.getActiveSessions(
ComponentName(
applicationContext,
Expand Down Expand Up @@ -1503,7 +1503,7 @@ class MessagingService : FirebaseMessagingService() {

private fun notifyMissingPermission(type: String) {
val appManager =
applicationContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
applicationContext.getSystemService<ActivityManager>()!!
val currentProcess = appManager.runningAppProcesses
if (currentProcess != null) {
for (item in currentProcess) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import android.os.Looper
import android.os.PowerManager
import android.util.Log
import android.widget.Toast
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.content.getSystemService
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingClient
Expand Down Expand Up @@ -790,7 +790,7 @@ class LocationSensorManager : LocationSensorManagerBase() {
request,
object : LocationCallback() {
val wakeLock: PowerManager.WakeLock? =
getSystemService(latestContext, PowerManager::class.java)
latestContext.getSystemService<PowerManager>()
?.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,
"HomeAssistant::AccurateLocation"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.homeassistant.companion.android.bluetooth.ble

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.bluetooth.le.AdvertiseCallback
import android.bluetooth.le.AdvertiseSettings
import android.content.Context
import androidx.core.content.getSystemService
import org.altbeacon.beacon.Beacon
import org.altbeacon.beacon.BeaconParser
import org.altbeacon.beacon.BeaconTransmitter
Expand Down Expand Up @@ -53,7 +54,8 @@ object TransmitterManager {
val parser = BeaconParser().setBeaconLayout(haTransmitter.beaconLayout)
physicalTransmitter = BeaconTransmitter(context, parser)
}
val bluetoothOn = BluetoothAdapter.getDefaultAdapter().isEnabled
val bluetoothAdapter = context.getSystemService<BluetoothManager>()?.adapter
val bluetoothOn = bluetoothAdapter?.isEnabled == true
if (bluetoothOn) {
val beacon = buildBeacon(haTransmitter)
if (!physicalTransmitter.isStarted) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package io.homeassistant.companion.android.onboarding

import android.app.Application
import android.net.nsd.NsdManager
import android.webkit.URLUtil
import android.widget.Toast
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.lifecycle.AndroidViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import io.homeassistant.companion.android.common.R
Expand All @@ -22,7 +21,7 @@ class OnboardingViewModel @Inject constructor(
) : AndroidViewModel(app) {

private val homeAssistantSearcher = HomeAssistantSearcher(
ContextCompat.getSystemService(app, NsdManager::class.java)!!,
app.getSystemService()!!,
{ instance ->
if (foundInstances.none { it.url == instance.url }) {
foundInstances.add(instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.platform.ComposeView
import androidx.core.content.getSystemService
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.google.android.material.composethemeadapter.MdcTheme
Expand Down Expand Up @@ -168,7 +169,7 @@ class MobileAppIntegrationFragment : Fragment() {

private fun isIgnoringBatteryOptimizations(): Boolean {
return Build.VERSION.SDK_INT <= Build.VERSION_CODES.M ||
context?.getSystemService(PowerManager::class.java)
context?.getSystemService<PowerManager>()
?.isIgnoringBatteryOptimizations(activity?.packageName ?: "")
?: false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.os.Build
import android.os.Process
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.common.sensors.SensorManager
import java.math.RoundingMode
Expand Down Expand Up @@ -123,7 +124,7 @@ class AppSensorManager : SensorManager {
updateAppTxGb(context, myUid)
updateImportanceCheck(context)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val usageStatsManager = context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val usageStatsManager = context.getSystemService<UsageStatsManager>()!!
updateAppInactive(context, usageStatsManager)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
updateAppStandbyBucket(context, usageStatsManager)
Expand Down Expand Up @@ -262,7 +263,7 @@ class AppSensorManager : SensorManager {
if (!isEnabled(context, app_importance.id))
return

val appManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val appManager = context.getSystemService<ActivityManager>()!!
val currentProcess = appManager.runningAppProcesses
var importance = "not_running"
if (currentProcess != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.media.AudioDeviceInfo
import android.media.AudioManager
import android.os.Build
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.common.sensors.SensorManager
import io.homeassistant.companion.android.common.R as commonR

Expand Down Expand Up @@ -97,7 +98,7 @@ class AudioSensorManager : SensorManager {
}

override fun requestSensorUpdate(context: Context) {
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
val audioManager = context.getSystemService<AudioManager>()!!
updateAudioSensor(context, audioManager)
updateAudioState(context, audioManager)
updateHeadphoneState(context, audioManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.KeyguardManager
import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.common.sensors.SensorManager
import io.homeassistant.companion.android.common.R as commonR

Expand Down Expand Up @@ -64,7 +65,7 @@ class KeyguardSensorManager : SensorManager {
override fun requestSensorUpdate(
context: Context
) {
val km = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
val km = context.getSystemService<KeyguardManager>()!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
updateDeviceLocked(context, km)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.common.sensors.SensorManager
import io.homeassistant.companion.android.common.R as commonR
Expand Down Expand Up @@ -55,7 +56,7 @@ class LastAppSensorManager : SensorManager {
if (!isEnabled(context, last_used.id))
return

val usageStats = context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val usageStats = context.getSystemService<UsageStatsManager>()!!
val current = System.currentTimeMillis()
var lastApp = usageStats.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, current - 1000 * 1000, current).maxByOrNull { it.lastTimeUsed }?.packageName ?: "none"
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.homeassistant.companion.android.sensors

import android.content.Context
import android.content.Context.SENSOR_SERVICE
import android.content.pm.PackageManager
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager.SENSOR_DELAY_NORMAL
import android.util.Log
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.common.sensors.SensorManager
import kotlin.math.roundToInt
import io.homeassistant.companion.android.common.R as commonR
Expand Down Expand Up @@ -64,7 +64,7 @@ class LightSensorManager : SensorManager, SensorEventListener {
if (!isEnabled(latestContext, lightSensor.id))
return

mySensorManager = latestContext.getSystemService(SENSOR_SERVICE) as android.hardware.SensorManager
mySensorManager = latestContext.getSystemService()!!

val lightSensors = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT)
if (lightSensors != null && !isListenerRegistered) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.pm.PackageManager
import android.provider.Settings
import android.provider.Settings.Global.getInt
import android.telephony.TelephonyManager
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.common.sensors.SensorManager
import io.homeassistant.companion.android.common.R as commonR

Expand Down Expand Up @@ -64,9 +65,8 @@ class MobileDataManager : SensorManager {
return

var enabled = false
val telephonyManager =
(context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager)
if (telephonyManager.simState == TelephonyManager.SIM_STATE_READY) {
val telephonyManager = context.applicationContext.getSystemService<TelephonyManager>()
if (telephonyManager?.simState == TelephonyManager.SIM_STATE_READY) {
enabled = getInt(context.contentResolver, settingKey, 0) == 1
}
onSensorUpdated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
import android.util.Log
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.common.sensors.SensorManager
import io.homeassistant.companion.android.database.AppDatabase
Expand Down Expand Up @@ -152,7 +153,7 @@ class NetworkSensorManager : SensorManager {

if (checkPermission(context, wifiConnection.id)) {
val wifiManager =
(context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager)
context.applicationContext.getSystemService<WifiManager>()!!
conInfo = wifiManager.connectionInfo

ssid = if (conInfo.networkId == -1) {
Expand Down Expand Up @@ -185,7 +186,7 @@ class NetworkSensorManager : SensorManager {

if (checkPermission(context, bssidState.id)) {
val wifiManager =
(context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager)
context.applicationContext.getSystemService<WifiManager>()!!
conInfo = wifiManager.connectionInfo
}

Expand Down Expand Up @@ -228,7 +229,7 @@ class NetworkSensorManager : SensorManager {

if (checkPermission(context, wifiIp.id)) {
val wifiManager =
(context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager)
context.applicationContext.getSystemService<WifiManager>()!!
val conInfo = wifiManager.connectionInfo

deviceIp = if (conInfo.networkId == -1) {
Expand Down Expand Up @@ -258,7 +259,7 @@ class NetworkSensorManager : SensorManager {

if (checkPermission(context, wifiLinkSpeed.id)) {
val wifiManager =
(context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager)
context.applicationContext.getSystemService<WifiManager>()!!
val conInfo = wifiManager.connectionInfo

linkSpeed = if (conInfo.networkId == -1) {
Expand Down Expand Up @@ -300,7 +301,7 @@ class NetworkSensorManager : SensorManager {

if (checkPermission(context, wifiState.id)) {
val wifiManager =
(context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager)
context.applicationContext.getSystemService<WifiManager>()!!

wifiEnabled = wifiManager.isWifiEnabled
}
Expand All @@ -323,7 +324,7 @@ class NetworkSensorManager : SensorManager {

if (checkPermission(context, wifiFrequency.id)) {
val wifiManager =
(context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager)
context.applicationContext.getSystemService<WifiManager>()!!
val conInfo = wifiManager.connectionInfo

frequency = if (conInfo.networkId == -1) {
Expand Down Expand Up @@ -352,7 +353,7 @@ class NetworkSensorManager : SensorManager {

if (checkPermission(context, wifiSignalStrength.id)) {
val wifiManager =
(context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager)
context.applicationContext.getSystemService<WifiManager>()!!
val conInfo = wifiManager.connectionInfo

lastScanStrength = wifiManager.scanResults.firstOrNull {
Expand Down
Loading