From 1cbfd11190a8626c8c0bef38f13279a669c057b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Mon, 24 Jul 2023 22:08:30 +0200 Subject: [PATCH] Use WifiManager deprecations again for instant 'location' info (#3705) --- .../common/data/wifi/WifiHelperImpl.kt | 19 ++----------------- .../common/sensors/NetworkSensorManager.kt | 7 +++++-- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/common/src/main/java/io/homeassistant/companion/android/common/data/wifi/WifiHelperImpl.kt b/common/src/main/java/io/homeassistant/companion/android/common/data/wifi/WifiHelperImpl.kt index 363f677e4ec..98a273dc013 100644 --- a/common/src/main/java/io/homeassistant/companion/android/common/data/wifi/WifiHelperImpl.kt +++ b/common/src/main/java/io/homeassistant/companion/android/common/data/wifi/WifiHelperImpl.kt @@ -2,7 +2,6 @@ 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 @@ -46,22 +45,8 @@ class WifiHelperImpl @Inject constructor( } override fun getWifiSsid(): String? = - 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 - } + wifiManager?.connectionInfo?.ssid // Deprecated but callback doesn't provide SSID info instantly override fun getWifiBssid(): String? = - 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 - } + wifiManager?.connectionInfo?.bssid // Deprecated but callback doesn't provide BSSID info instantly } diff --git a/common/src/main/java/io/homeassistant/companion/android/common/sensors/NetworkSensorManager.kt b/common/src/main/java/io/homeassistant/companion/android/common/sensors/NetworkSensorManager.kt index 921163d142a..c17b98ebca3 100644 --- a/common/src/main/java/io/homeassistant/companion/android/common/sensors/NetworkSensorManager.kt +++ b/common/src/main/java/io/homeassistant/companion/android/common/sensors/NetworkSensorManager.kt @@ -192,7 +192,8 @@ class NetworkSensorManager : SensorManager { var connected = false if (checkPermission(context, wifiConnection.id)) { - conInfo = getWifiConnectionInfo(context) + @Suppress("DEPRECATION") // Unable to get SSID info (instantly) using callback + conInfo = context.getSystemService()?.connectionInfo if (conInfo == null || conInfo.networkId == -1) { if (conInfo == null || conInfo.linkSpeed == -1) { @@ -230,7 +231,8 @@ class NetworkSensorManager : SensorManager { var conInfo: WifiInfo? = null if (checkPermission(context, bssidState.id)) { - conInfo = getWifiConnectionInfo(context) + @Suppress("DEPRECATION") // Unable to get BSSID info (instantly) using callback + conInfo = context.getSystemService()?.connectionInfo } var bssid = if (conInfo?.bssid == null) "" else conInfo.bssid @@ -522,6 +524,7 @@ class NetworkSensorManager : SensorManager { ) } + /** Get WiFi connection info (without location data such as (B)SSID on Android >=S) */ private fun getWifiConnectionInfo(context: Context): WifiInfo? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { val connectivityManager = context.applicationContext.getSystemService()