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

Update/replace deprecations in common module #3698

Merged
merged 1 commit into from
Jul 24, 2023
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 @@ -2,6 +2,7 @@ package io.homeassistant.companion.android.common.data.wifi

import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
import javax.inject.Inject
Expand Down Expand Up @@ -45,8 +46,22 @@ class WifiHelperImpl @Inject constructor(
}

override fun getWifiSsid(): String? =
wifiManager?.connectionInfo?.ssid
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
connectivityManager.activeNetwork?.let {
val info = connectivityManager.getNetworkCapabilities(it)?.transportInfo ?: return null
(info as? WifiInfo)?.ssid
}
} else {
wifiManager?.connectionInfo?.ssid
}

override fun getWifiBssid(): String? =
wifiManager?.connectionInfo?.bssid
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
connectivityManager.activeNetwork?.let {
val info = connectivityManager.getNetworkCapabilities(it)?.transportInfo ?: return null
(info as? WifiInfo)?.bssid
}
} else {
wifiManager?.connectionInfo?.bssid
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class AudioSensorManager : SensorManager {
}
}
} else {
// Use deprecated method as getDevices is API 23 and up only and we support API 21
@Suppress("DEPRECATION")
isHeadphones = audioManager.isWiredHeadsetOn
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.homeassistant.companion.android.common.sensors

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import io.homeassistant.companion.android.common.bluetooth.BluetoothDevice
Expand Down Expand Up @@ -139,6 +140,7 @@ class BluetoothSensorManager : SensorManager {
return listOf(bluetoothConnection, bluetoothState, bleTransmitter, beaconMonitor)
}

@SuppressLint("InlinedApi")
override fun requiredPermissions(sensorId: String): Array<String> {
return when {
(sensorId == bleTransmitter.id && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,10 @@ class NetworkSensorManager : SensorManager {
var connected = false

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

if (conInfo.networkId == -1) {
if (conInfo.linkSpeed == -1) {
if (conInfo == null || conInfo.networkId == -1) {
if (conInfo == null || conInfo.linkSpeed == -1) {
ssid = "<not connected>"
} else {
ssid = "<unknown>"
Expand Down Expand Up @@ -232,12 +230,10 @@ class NetworkSensorManager : SensorManager {
var conInfo: WifiInfo? = null

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

var bssid = if (conInfo!!.bssid == null) "<not connected>" else conInfo.bssid
var bssid = if (conInfo?.bssid == null) "<not connected>" else conInfo.bssid

val settingName = "network_replace_mac_var1:$bssid:"
val sensorDao = AppDatabase.getInstance(context).sensorDao()
Expand Down Expand Up @@ -277,14 +273,23 @@ class NetworkSensorManager : SensorManager {
var deviceIp = "Unknown"

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

deviceIp = if (conInfo.networkId == -1 && conInfo.linkSpeed == -1) {
deviceIp = if (conInfo == null || (conInfo.networkId == -1 && conInfo.linkSpeed == -1)) {
"<not connected>"
} else {
getIpAddress(conInfo.ipAddress)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val connectivityManager = context.applicationContext.getSystemService<ConnectivityManager>()
connectivityManager?.activeNetwork?.let {
// Get the IPv4 address without prefix length
connectivityManager.getLinkProperties(it)?.linkAddresses
?.firstOrNull { address -> !address.toString().contains(":") }
?.toString()?.split("/")?.get(0)
} ?: ""
} else {
@Suppress("DEPRECATION")
getIpAddress(conInfo.ipAddress)
}
}
}

Expand All @@ -306,23 +311,22 @@ class NetworkSensorManager : SensorManager {
var rssi = -1

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

linkSpeed = if (conInfo.linkSpeed == -1) {
linkSpeed = if (conInfo == null || conInfo.linkSpeed == -1) {
0
} else {
conInfo.linkSpeed
}

if (conInfo.networkId != -1 || conInfo.linkSpeed != -1) {
if (conInfo != null && (conInfo.networkId != -1 || conInfo.linkSpeed != -1)) {
rssi = conInfo.rssi
}
}

var signalStrength = -1
if (rssi != -1) {
@Suppress("DEPRECATION") // Always use 4 levels instead of depending on device
signalStrength = WifiManager.calculateSignalLevel(rssi, 4)
}

Expand Down Expand Up @@ -373,11 +377,9 @@ class NetworkSensorManager : SensorManager {
var frequency = 0

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

frequency = if (conInfo.networkId == -1 && conInfo.linkSpeed == -1) {
frequency = if (conInfo == null || (conInfo.networkId == -1 && conInfo.linkSpeed == -1)) {
0
} else {
conInfo.frequency
Expand All @@ -401,17 +403,16 @@ class NetworkSensorManager : SensorManager {
var rssi = -1

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

if (conInfo.networkId != -1 || conInfo.linkSpeed != -1) {
if (conInfo != null && (conInfo.networkId != -1 || conInfo.linkSpeed != -1)) {
rssi = conInfo.rssi
}
}

var signalStrength = -1
if (rssi != -1) {
@Suppress("DEPRECATION") // Always use 4 levels instead of depending on device
signalStrength = WifiManager.calculateSignalLevel(rssi, 4)
}

Expand Down Expand Up @@ -520,4 +521,16 @@ class NetworkSensorManager : SensorManager {
)
)
}

private fun getWifiConnectionInfo(context: Context): WifiInfo? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val connectivityManager = context.applicationContext.getSystemService<ConnectivityManager>()
connectivityManager?.activeNetwork?.let {
val info = connectivityManager.getNetworkCapabilities(it)?.transportInfo
return@let info as? WifiInfo
}
} else {
@Suppress("DEPRECATION")
context.applicationContext.getSystemService<WifiManager>()?.connectionInfo
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.homeassistant.companion.android.common.sensors

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
Expand Down Expand Up @@ -74,6 +75,7 @@ class PhoneStateSensorManager : SensorManager {
updateSimSensor(context, 1)
}

@SuppressLint("MissingPermission")
private fun checkPhoneState(context: Context) {
if (isEnabled(context, phoneState)) {
var currentPhoneState = "unknown"
Expand All @@ -82,7 +84,13 @@ class PhoneStateSensorManager : SensorManager {
val telephonyManager =
context.applicationContext.getSystemService<TelephonyManager>()!!

currentPhoneState = when (telephonyManager.callState) {
val callState = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
telephonyManager.callStateForSubscription
} else {
@Suppress("DEPRECATION")
telephonyManager.callState
}
currentPhoneState = when (callState) {
TelephonyManager.CALL_STATE_IDLE -> "idle"
TelephonyManager.CALL_STATE_RINGING -> "ringing"
TelephonyManager.CALL_STATE_OFFHOOK -> "offhook"
Expand All @@ -109,6 +117,7 @@ class PhoneStateSensorManager : SensorManager {
)
}

@SuppressLint("MissingPermission")
private fun updateSimSensor(context: Context, slotIndex: Int) {
val basicSimSensor = when (slotIndex) {
0 -> sim_1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.AppOpsManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Process.myPid
import android.os.Process.myUid
import androidx.core.content.getSystemService
Expand Down Expand Up @@ -76,9 +77,19 @@ interface SensorManager {

fun checkUsageStatsPermission(context: Context): Boolean {
val pm = context.packageManager
val appInfo = pm.getApplicationInfo(context.packageName, 0)
val appOpsManager = context.getSystemService<AppOpsManager>()!!
val mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, appInfo.uid, appInfo.packageName)
val appInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
pm.getApplicationInfo(context.packageName, PackageManager.ApplicationInfoFlags.of(0))
} else {
@Suppress("DEPRECATION")
pm.getApplicationInfo(context.packageName, 0)
}
val appOpsManager = context.getSystemService<AppOpsManager>()
val mode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
appOpsManager?.unsafeCheckOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, appInfo.uid, appInfo.packageName)
} else {
@Suppress("DEPRECATION")
appOpsManager?.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, appInfo.uid, appInfo.packageName)
}
return mode == AppOpsManager.MODE_ALLOWED
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ abstract class SensorReceiverBase : BroadcastReceiver() {
val allSettings = sensorDao.getSettings(LastUpdateManager.lastUpdate.id)
for (setting in allSettings) {
if (setting.value != "" && intent.action == setting.value) {
val eventData = intent.extras?.keySet()?.map { it.toString() to intent.extras?.get(it).toString() }?.toMap()?.plus("intent" to intent.action.toString())
val eventData = intent.extras?.keySet()
?.associate {
it.toString() to (intent.extras?.getString(it) ?: "")
}
?.plus("intent" to intent.action.toString())
?: mapOf("intent" to intent.action.toString())
Log.d(tag, "Event data: $eventData")
sensorDao.get(LastUpdateManager.lastUpdate.id).forEach { sensor ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class TrafficStatsManager : SensorManager {
return emptyArray()
}

@Suppress("DEPRECATION") // No synchronous option to get all networks
override fun hasSensor(context: Context): Boolean {
val cm = context.getSystemService<ConnectivityManager>()!!
val networkInfo = cm.allNetworks
Expand Down