From 7705b9ad99303228c94af31433830122a101fb51 Mon Sep 17 00:00:00 2001 From: MeGilles Date: Tue, 30 Apr 2024 18:27:33 +0200 Subject: [PATCH 01/10] Adding cell tower basic sensor --- .../android/common/sensors/NetworkSensorManager.kt | 10 ++++++++++ common/src/main/res/values/strings.xml | 2 ++ 2 files changed, 12 insertions(+) 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 349dfc275eb..937ec5400b1 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 @@ -141,6 +141,16 @@ class NetworkSensorManager : SensorManager { entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC, updateType = SensorManager.BasicSensor.UpdateType.INTENT ) + val cellTowerConnection = SensorManager.BasicSensor( + "cell_tower_connection", + "sensor", + commonR.string.basic_sensor_name_cell_tower, + commonR.string.sensor_description_cell_tower, + "mdi:radio-tower", + entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC, + updateType = SensorManager.BasicSensor.UpdateType.INTENT + ) + private const val SETTING_GET_CURRENT_BSSID = "network_get_current_bssid" } diff --git a/common/src/main/res/values/strings.xml b/common/src/main/res/values/strings.xml index a083e967d50..0cf721f402f 100644 --- a/common/src/main/res/values/strings.xml +++ b/common/src/main/res/values/strings.xml @@ -1090,6 +1090,8 @@ Undo Network type The type of transport the current active network is using + Cell tower connection + The id of the cell tower the device is currently connected to Volume level notification Volume level for notifications on the device Volume level system From 1c0eea25372fcfc8e0fc098366d4488e87a7afe9 Mon Sep 17 00:00:00 2001 From: MeGilles Date: Thu, 2 May 2024 14:13:23 +0200 Subject: [PATCH 02/10] sensor logic for lte and wcdma --- .../common/sensors/NetworkSensorManager.kt | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) 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 937ec5400b1..6d06d62b5f7 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 @@ -9,6 +9,9 @@ import android.net.NetworkCapabilities import android.net.wifi.WifiInfo import android.net.wifi.WifiManager import android.os.Build +import android.telephony.CellIdentityLte +import android.telephony.CellIdentityWcdma +import android.telephony.TelephonyManager import android.util.Log import androidx.annotation.RequiresApi import androidx.core.content.getSystemService @@ -169,7 +172,7 @@ class NetworkSensorManager : SensorManager { wifiFrequency, wifiSignalStrength ) - val list = if (hasWifi(context)) { + val listWithWifi = if (hasWifi(context)) { val withPublicIp = wifiSensors.plus(publicIp) if (hasHotspot(context)) { withPublicIp.plus(hotspotState) @@ -179,6 +182,13 @@ class NetworkSensorManager : SensorManager { } else { listOf(publicIp) } + + val list = if (hasTelephony(context)) { + listWithWifi.plus(cellTowerConnection) + } else { + listWithWifi + } + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { list.plus(networkType).plus(ip6Addresses) } else { @@ -215,12 +225,18 @@ class NetworkSensorManager : SensorManager { updateWifiFrequencySensor(context) updateWifiSignalStrengthSensor(context) updatePublicIpSensor(context) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){ + updateCellTowerConnectionSensor(context) + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { updateNetworkType(context) updateIP6Sensor(context) } } + private fun hasTelephony(context: Context): Boolean = + context.packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) + private fun hasWifi(context: Context): Boolean = context.applicationContext.getSystemService() != null @@ -580,6 +596,48 @@ class NetworkSensorManager : SensorManager { }) } + @SuppressLint("MissingPermission") + @RequiresApi(Build.VERSION_CODES.R) + private fun updateCellTowerConnectionSensor(context: Context){ + if (!isEnabled(context, cellTowerConnection) || !hasTelephony(context)) { + return + } + + val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager + + //get the list of cells + val cellInfoList = telephonyManager.getAllCellInfo() + + if (cellInfoList == null || cellInfoList.size == 0) { + STATE_UNAVAILABLE + } else { + //get the "registered" (= currently used for communication?) cell + val cell = cellInfoList.findLast { cell -> cell.isRegistered } + + val cellTowerStr = when (val a = cell?.cellIdentity) { + is CellIdentityLte -> { + "lte:" + a.tac + ":" + a.ci + ":" + a.pci + } + + is CellIdentityWcdma -> { + "wcdma:" + a.lac + ":" + a.cid + } + + else -> { + "" + } + } + + onSensorUpdated( + context, + cellTowerConnection, + cellTowerStr, + cellTowerConnection.statelessIcon, + mapOf() + ) + } + } + @SuppressLint("MissingPermission") @RequiresApi(Build.VERSION_CODES.M) private fun updateNetworkType(context: Context) { From d40d733f490801dd30caa75cc0fb3c478c3d7edf Mon Sep 17 00:00:00 2001 From: MeGilles Date: Thu, 2 May 2024 14:16:33 +0200 Subject: [PATCH 03/10] add logic for nr, tdscdma, cdma and gsm --- .../common/sensors/NetworkSensorManager.kt | 66 ++++++++++++------- 1 file changed, 42 insertions(+), 24 deletions(-) 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 6d06d62b5f7..5f56936e16f 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 @@ -9,8 +9,13 @@ import android.net.NetworkCapabilities import android.net.wifi.WifiInfo import android.net.wifi.WifiManager import android.os.Build +import android.telephony.CellIdentityCdma +import android.telephony.CellIdentityGsm import android.telephony.CellIdentityLte +import android.telephony.CellIdentityNr +import android.telephony.CellIdentityTdscdma import android.telephony.CellIdentityWcdma +import android.telephony.CellInfo import android.telephony.TelephonyManager import android.util.Log import androidx.annotation.RequiresApi @@ -596,9 +601,9 @@ class NetworkSensorManager : SensorManager { }) } - @SuppressLint("MissingPermission") @RequiresApi(Build.VERSION_CODES.R) - private fun updateCellTowerConnectionSensor(context: Context){ + @SuppressLint("MissingPermission") + private fun updateCellTowerConnectionSensor(context: Context) { if (!isEnabled(context, cellTowerConnection) || !hasTelephony(context)) { return } @@ -608,34 +613,47 @@ class NetworkSensorManager : SensorManager { //get the list of cells val cellInfoList = telephonyManager.getAllCellInfo() - if (cellInfoList == null || cellInfoList.size == 0) { - STATE_UNAVAILABLE - } else { - //get the "registered" (= currently used for communication?) cell - val cell = cellInfoList.findLast { cell -> cell.isRegistered } + val cell = cellInfoList.findLast { cell -> cell.cellConnectionStatus == CellInfo.CONNECTION_PRIMARY_SERVING } - val cellTowerStr = when (val a = cell?.cellIdentity) { - is CellIdentityLte -> { - "lte:" + a.tac + ":" + a.ci + ":" + a.pci - } + val cellTowerStr = when (val cellIdentity = cell?.cellIdentity) { + is CellIdentityNr -> { + "nr:" + cellIdentity.nci + } - is CellIdentityWcdma -> { - "wcdma:" + a.lac + ":" + a.cid - } + is CellIdentityLte -> { + "lte:" + cellIdentity.tac + ":" + cellIdentity.ci + ":" + cellIdentity.pci + } - else -> { - "" - } + is CellIdentityWcdma -> { + "wcdma:" + cellIdentity.lac + ":" + cellIdentity.cid } - onSensorUpdated( - context, - cellTowerConnection, - cellTowerStr, - cellTowerConnection.statelessIcon, - mapOf() - ) + is CellIdentityTdscdma -> { + "tdcdma:" + cellIdentity.lac + ":" + cellIdentity.cid + } + + is CellIdentityCdma -> { + "cdma:" + cellIdentity.systemId + ":" + cellIdentity.networkId + ":" + cellIdentity.basestationId + } + + is CellIdentityGsm -> { + // can't access the RNC, some information some information I found https://wiki.opencellid.org/wiki/FAQ + "gsm:" + cellIdentity.lac + ":" + ":" + cellIdentity.cid + } + + else -> { + "" + } } + + onSensorUpdated( + context, + cellTowerConnection, + cellTowerStr, + cellTowerConnection.statelessIcon, + mapOf() + ) + } @SuppressLint("MissingPermission") From 0c7ac9343c87afae92560389d7e08078b12f4ef1 Mon Sep 17 00:00:00 2001 From: MeGilles Date: Thu, 2 May 2024 14:24:03 +0200 Subject: [PATCH 04/10] add support for API < 29 --- .../common/sensors/NetworkSensorManager.kt | 89 ++++++++++++------- 1 file changed, 56 insertions(+), 33 deletions(-) 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 5f56936e16f..818bbf1df6e 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 @@ -9,13 +9,13 @@ import android.net.NetworkCapabilities import android.net.wifi.WifiInfo import android.net.wifi.WifiManager import android.os.Build -import android.telephony.CellIdentityCdma -import android.telephony.CellIdentityGsm -import android.telephony.CellIdentityLte import android.telephony.CellIdentityNr -import android.telephony.CellIdentityTdscdma -import android.telephony.CellIdentityWcdma -import android.telephony.CellInfo +import android.telephony.CellInfoCdma +import android.telephony.CellInfoGsm +import android.telephony.CellInfoLte +import android.telephony.CellInfoNr +import android.telephony.CellInfoTdscdma +import android.telephony.CellInfoWcdma import android.telephony.TelephonyManager import android.util.Log import androidx.annotation.RequiresApi @@ -230,9 +230,8 @@ class NetworkSensorManager : SensorManager { updateWifiFrequencySensor(context) updateWifiSignalStrengthSensor(context) updatePublicIpSensor(context) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){ - updateCellTowerConnectionSensor(context) - } + updateCellTowerConnectionSensor(context) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { updateNetworkType(context) updateIP6Sensor(context) @@ -601,7 +600,6 @@ class NetworkSensorManager : SensorManager { }) } - @RequiresApi(Build.VERSION_CODES.R) @SuppressLint("MissingPermission") private fun updateCellTowerConnectionSensor(context: Context) { if (!isEnabled(context, cellTowerConnection) || !hasTelephony(context)) { @@ -610,39 +608,64 @@ class NetworkSensorManager : SensorManager { val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager - //get the list of cells val cellInfoList = telephonyManager.getAllCellInfo() - val cell = cellInfoList.findLast { cell -> cell.cellConnectionStatus == CellInfo.CONNECTION_PRIMARY_SERVING } + val cell = cellInfoList.findLast { cell -> cell.isRegistered } //cell.cellConnectionStatus == CellInfo.CONNECTION_PRIMARY_SERVING - val cellTowerStr = when (val cellIdentity = cell?.cellIdentity) { - is CellIdentityNr -> { - "nr:" + cellIdentity.nci - } + if(cell == null){ + return + } - is CellIdentityLte -> { - "lte:" + cellIdentity.tac + ":" + cellIdentity.ci + ":" + cellIdentity.pci - } + val cellTowerStr : String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + when (cell) { + is CellInfoNr -> { + "nr:" + (cell.cellIdentity as CellIdentityNr).nci + } - is CellIdentityWcdma -> { - "wcdma:" + cellIdentity.lac + ":" + cellIdentity.cid - } + is CellInfoTdscdma -> { + "tdcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid + } - is CellIdentityTdscdma -> { - "tdcdma:" + cellIdentity.lac + ":" + cellIdentity.cid - } + is CellInfoLte -> { //tested + "lte:" + cell.cellIdentity.tac + ":" + cell.cellIdentity.ci + ":" + cell.cellIdentity.pci + } - is CellIdentityCdma -> { - "cdma:" + cellIdentity.systemId + ":" + cellIdentity.networkId + ":" + cellIdentity.basestationId - } + is CellInfoWcdma -> { //tested + "wcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid + } - is CellIdentityGsm -> { - // can't access the RNC, some information some information I found https://wiki.opencellid.org/wiki/FAQ - "gsm:" + cellIdentity.lac + ":" + ":" + cellIdentity.cid + is CellInfoCdma -> { + "cdma:" + cell.cellIdentity.systemId + ":" + cell.cellIdentity.networkId + ":" + cell.cellIdentity.basestationId + } + + is CellInfoGsm -> { + "gsm:" + cell.cellIdentity.lac + ":" + ":" + cell.cellIdentity.cid + } + else -> { "" } } + } + //support for older devices + else { + when (cell) { + // CellInfoNr requires Build.VERSION_CODES.Q + // CellInfoTdscdma requires Build.VERSION_CODES.Q - else -> { - "" + is CellInfoLte -> { //tested + "lte:" + cell.cellIdentity.tac + ":" + cell.cellIdentity.ci + ":" + cell.cellIdentity.pci + } + + is CellInfoWcdma -> { //tested + "wcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid + } + + is CellInfoCdma -> { + "cdma:" + cell.cellIdentity.systemId + ":" + cell.cellIdentity.networkId + ":" + cell.cellIdentity.basestationId + } + + is CellInfoGsm -> { // tested + "gsm:" + cell.cellIdentity.lac + ":" + ":" + cell.cellIdentity.cid + } + else -> { "" } } } From d11683b24713b2a7645e6b3f31f404c54b7ccf69 Mon Sep 17 00:00:00 2001 From: MeGilles Date: Thu, 2 May 2024 17:34:27 +0200 Subject: [PATCH 05/10] refactor and rename cellTower to cell --- .../common/sensors/NetworkSensorManager.kt | 32 ++++++++----------- common/src/main/res/values/strings.xml | 4 +-- 2 files changed, 15 insertions(+), 21 deletions(-) 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 818bbf1df6e..34a78f164f6 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 @@ -149,11 +149,11 @@ class NetworkSensorManager : SensorManager { entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC, updateType = SensorManager.BasicSensor.UpdateType.INTENT ) - val cellTowerConnection = SensorManager.BasicSensor( - "cell_tower_connection", + val cellConnection = SensorManager.BasicSensor( + "cell_connection", "sensor", - commonR.string.basic_sensor_name_cell_tower, - commonR.string.sensor_description_cell_tower, + commonR.string.basic_sensor_name_cell, + commonR.string.sensor_description_cell, "mdi:radio-tower", entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC, updateType = SensorManager.BasicSensor.UpdateType.INTENT @@ -189,7 +189,7 @@ class NetworkSensorManager : SensorManager { } val list = if (hasTelephony(context)) { - listWithWifi.plus(cellTowerConnection) + listWithWifi.plus(cellConnection) } else { listWithWifi } @@ -230,7 +230,7 @@ class NetworkSensorManager : SensorManager { updateWifiFrequencySensor(context) updateWifiSignalStrengthSensor(context) updatePublicIpSensor(context) - updateCellTowerConnectionSensor(context) + updateCellConnectionSensor(context) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { updateNetworkType(context) @@ -601,22 +601,16 @@ class NetworkSensorManager : SensorManager { } @SuppressLint("MissingPermission") - private fun updateCellTowerConnectionSensor(context: Context) { - if (!isEnabled(context, cellTowerConnection) || !hasTelephony(context)) { + private fun updateCellConnectionSensor(context: Context) { + if (!isEnabled(context, cellConnection) || !hasTelephony(context)) { return } val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager - val cellInfoList = telephonyManager.getAllCellInfo() + val cell = telephonyManager.getAllCellInfo().findLast { cell -> cell.isRegistered } ?: return - val cell = cellInfoList.findLast { cell -> cell.isRegistered } //cell.cellConnectionStatus == CellInfo.CONNECTION_PRIMARY_SERVING - - if(cell == null){ - return - } - - val cellTowerStr : String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val cellId : String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { when (cell) { is CellInfoNr -> { "nr:" + (cell.cellIdentity as CellIdentityNr).nci @@ -671,9 +665,9 @@ class NetworkSensorManager : SensorManager { onSensorUpdated( context, - cellTowerConnection, - cellTowerStr, - cellTowerConnection.statelessIcon, + cellConnection, + cellId, + cellConnection.statelessIcon, mapOf() ) diff --git a/common/src/main/res/values/strings.xml b/common/src/main/res/values/strings.xml index 0cf721f402f..97ba01afa4d 100644 --- a/common/src/main/res/values/strings.xml +++ b/common/src/main/res/values/strings.xml @@ -1090,8 +1090,8 @@ Undo Network type The type of transport the current active network is using - Cell tower connection - The id of the cell tower the device is currently connected to + Cell connection + The id of the mobile network cell the device is currently connected to Volume level notification Volume level for notifications on the device Volume level system From 233725a5f45d11c72605608a1b0620914836aa89 Mon Sep 17 00:00:00 2001 From: MeGilles Date: Thu, 2 May 2024 18:13:03 +0200 Subject: [PATCH 06/10] fix ktlint errors --- .../common/sensors/NetworkSensorManager.kt | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) 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 34a78f164f6..7f05f0bd7f2 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 @@ -610,7 +610,7 @@ class NetworkSensorManager : SensorManager { val cell = telephonyManager.getAllCellInfo().findLast { cell -> cell.isRegistered } ?: return - val cellId : String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val cellId: String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { when (cell) { is CellInfoNr -> { "nr:" + (cell.cellIdentity as CellIdentityNr).nci @@ -620,11 +620,11 @@ class NetworkSensorManager : SensorManager { "tdcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid } - is CellInfoLte -> { //tested + is CellInfoLte -> { // tested "lte:" + cell.cellIdentity.tac + ":" + cell.cellIdentity.ci + ":" + cell.cellIdentity.pci } - is CellInfoWcdma -> { //tested + is CellInfoWcdma -> { // tested "wcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid } @@ -635,20 +635,20 @@ class NetworkSensorManager : SensorManager { is CellInfoGsm -> { "gsm:" + cell.cellIdentity.lac + ":" + ":" + cell.cellIdentity.cid } - else -> { "" } + else -> { + "" + } } - } - //support for older devices - else { + } else { // support for older devices when (cell) { // CellInfoNr requires Build.VERSION_CODES.Q // CellInfoTdscdma requires Build.VERSION_CODES.Q - is CellInfoLte -> { //tested + is CellInfoLte -> { // tested "lte:" + cell.cellIdentity.tac + ":" + cell.cellIdentity.ci + ":" + cell.cellIdentity.pci } - is CellInfoWcdma -> { //tested + is CellInfoWcdma -> { // tested "wcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid } @@ -659,7 +659,9 @@ class NetworkSensorManager : SensorManager { is CellInfoGsm -> { // tested "gsm:" + cell.cellIdentity.lac + ":" + ":" + cell.cellIdentity.cid } - else -> { "" } + else -> { + "" + } } } @@ -670,7 +672,6 @@ class NetworkSensorManager : SensorManager { cellConnection.statelessIcon, mapOf() ) - } @SuppressLint("MissingPermission") From 3cb36c6734dc8075a1b85b72c0ee589ece6388a0 Mon Sep 17 00:00:00 2001 From: MeGilles Date: Fri, 3 May 2024 09:39:48 +0200 Subject: [PATCH 07/10] return STATE_UNKNOWN when no cell connection --- .../common/sensors/NetworkSensorManager.kt | 65 +++++-------------- 1 file changed, 16 insertions(+), 49 deletions(-) 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 7f05f0bd7f2..4155cb58cc1 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 @@ -608,60 +608,27 @@ class NetworkSensorManager : SensorManager { val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager - val cell = telephonyManager.getAllCellInfo().findLast { cell -> cell.isRegistered } ?: return + val cell = telephonyManager.getAllCellInfo().findLast { cell -> cell.isRegistered } - val cellId: String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val cellId = if (cell == null) { + STATE_UNKNOWN + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { when (cell) { - is CellInfoNr -> { - "nr:" + (cell.cellIdentity as CellIdentityNr).nci - } - - is CellInfoTdscdma -> { - "tdcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid - } - - is CellInfoLte -> { // tested - "lte:" + cell.cellIdentity.tac + ":" + cell.cellIdentity.ci + ":" + cell.cellIdentity.pci - } - - is CellInfoWcdma -> { // tested - "wcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid - } - - is CellInfoCdma -> { - "cdma:" + cell.cellIdentity.systemId + ":" + cell.cellIdentity.networkId + ":" + cell.cellIdentity.basestationId - } - - is CellInfoGsm -> { - "gsm:" + cell.cellIdentity.lac + ":" + ":" + cell.cellIdentity.cid - } - else -> { - "" - } + is CellInfoNr -> "nr:" + (cell.cellIdentity as CellIdentityNr).nci + is CellInfoTdscdma -> "tdcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid + is CellInfoLte -> "lte:" + cell.cellIdentity.tac + ":" + cell.cellIdentity.ci + ":" + cell.cellIdentity.pci + is CellInfoWcdma -> "wcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid + is CellInfoCdma -> "cdma:" + cell.cellIdentity.systemId + ":" + cell.cellIdentity.networkId + ":" + cell.cellIdentity.basestationId + is CellInfoGsm -> "gsm:" + cell.cellIdentity.lac + ":" + ":" + cell.cellIdentity.cid + else -> STATE_UNKNOWN } } else { // support for older devices when (cell) { - // CellInfoNr requires Build.VERSION_CODES.Q - // CellInfoTdscdma requires Build.VERSION_CODES.Q - - is CellInfoLte -> { // tested - "lte:" + cell.cellIdentity.tac + ":" + cell.cellIdentity.ci + ":" + cell.cellIdentity.pci - } - - is CellInfoWcdma -> { // tested - "wcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid - } - - is CellInfoCdma -> { - "cdma:" + cell.cellIdentity.systemId + ":" + cell.cellIdentity.networkId + ":" + cell.cellIdentity.basestationId - } - - is CellInfoGsm -> { // tested - "gsm:" + cell.cellIdentity.lac + ":" + ":" + cell.cellIdentity.cid - } - else -> { - "" - } + is CellInfoLte -> "lte:" + cell.cellIdentity.tac + ":" + cell.cellIdentity.ci + ":" + cell.cellIdentity.pci + is CellInfoWcdma -> "wcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid + is CellInfoCdma -> "cdma:" + cell.cellIdentity.systemId + ":" + cell.cellIdentity.networkId + ":" + cell.cellIdentity.basestationId + is CellInfoGsm -> "gsm:" + cell.cellIdentity.lac + ":" + ":" + cell.cellIdentity.cid + else -> STATE_UNKNOWN } } From 0501657d117af5ab0a816b676c25a80b60f3ec02 Mon Sep 17 00:00:00 2001 From: MeGilles Date: Fri, 3 May 2024 17:46:43 +0200 Subject: [PATCH 08/10] change returned value to the opencellid.org format (radio, mcc, mnc, lac, cid) --- .../common/sensors/NetworkSensorManager.kt | 72 +++++++++++++------ 1 file changed, 52 insertions(+), 20 deletions(-) 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 4155cb58cc1..88bb7a0b721 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 @@ -610,28 +610,60 @@ class NetworkSensorManager : SensorManager { val cell = telephonyManager.getAllCellInfo().findLast { cell -> cell.isRegistered } - val cellId = if (cell == null) { - STATE_UNKNOWN - } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - when (cell) { - is CellInfoNr -> "nr:" + (cell.cellIdentity as CellIdentityNr).nci - is CellInfoTdscdma -> "tdcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid - is CellInfoLte -> "lte:" + cell.cellIdentity.tac + ":" + cell.cellIdentity.ci + ":" + cell.cellIdentity.pci - is CellInfoWcdma -> "wcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid - is CellInfoCdma -> "cdma:" + cell.cellIdentity.systemId + ":" + cell.cellIdentity.networkId + ":" + cell.cellIdentity.basestationId - is CellInfoGsm -> "gsm:" + cell.cellIdentity.lac + ":" + ":" + cell.cellIdentity.cid - else -> STATE_UNKNOWN - } - } else { // support for older devices - when (cell) { - is CellInfoLte -> "lte:" + cell.cellIdentity.tac + ":" + cell.cellIdentity.ci + ":" + cell.cellIdentity.pci - is CellInfoWcdma -> "wcdma:" + cell.cellIdentity.lac + ":" + cell.cellIdentity.cid - is CellInfoCdma -> "cdma:" + cell.cellIdentity.systemId + ":" + cell.cellIdentity.networkId + ":" + cell.cellIdentity.basestationId - is CellInfoGsm -> "gsm:" + cell.cellIdentity.lac + ":" + ":" + cell.cellIdentity.cid - else -> STATE_UNKNOWN - } + if (cell == null) { + onSensorUpdated( + context, + cellConnection, + STATE_UNAVAILABLE, + cellConnection.statelessIcon, + mapOf() + ) + return + } + + val radio: String + val mcc = telephonyManager.networkOperator.substring(0, 3) // extracting mcc and mnc from the networkOperator https://developer.android.com/reference/android/telephony/TelephonyManager#getNetworkOperator() + val mnc = telephonyManager.networkOperator.substring(3).toInt().toString() + val lac: String + val cid: String + + if (cell is CellInfoGsm) { + radio = "GSM" + lac = cell.cellIdentity.lac.toString() + cid = cell.cellIdentity.cid.toString() + } else if (cell is CellInfoCdma) { + radio = "CDMA" + lac = cell.cellIdentity.networkId.toString() + cid = cell.cellIdentity.basestationId.toString() + } else if (cell is CellInfoWcdma) { + radio = "UMTS" + lac = cell.cellIdentity.lac.toString() + cid = cell.cellIdentity.cid.toString() + } else if (cell is CellInfoLte) { + radio = "LTE" + lac = cell.cellIdentity.tac.toString() + cid = cell.cellIdentity.ci.toString() + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && cell is CellInfoTdscdma) { + radio = "UTMS" + lac = cell.cellIdentity.lac.toString() + cid = cell.cellIdentity.cid.toString() + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && cell is CellInfoNr) { + radio = "NR" + lac = (cell.cellIdentity as CellIdentityNr).tac.toString() + cid = (cell.cellIdentity as CellIdentityNr).nci.toString() + } else { + onSensorUpdated( + context, + cellConnection, + STATE_UNKNOWN, + cellConnection.statelessIcon, + mapOf() + ) + return } + val cellId = "$radio:$mcc:$mnc:$lac:$cid" + onSensorUpdated( context, cellConnection, From cf62567a9ed7da915bbb6c7ceff950e097aa6eb1 Mon Sep 17 00:00:00 2001 From: MeGilles Date: Mon, 6 May 2024 12:16:20 +0200 Subject: [PATCH 09/10] adding signal strength attributes --- .../common/sensors/NetworkSensorManager.kt | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) 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 88bb7a0b721..fe9e19f4b3a 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 @@ -156,7 +156,7 @@ class NetworkSensorManager : SensorManager { commonR.string.sensor_description_cell, "mdi:radio-tower", entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC, - updateType = SensorManager.BasicSensor.UpdateType.INTENT + updateType = SensorManager.BasicSensor.UpdateType.WORKER ) private const val SETTING_GET_CURRENT_BSSID = "network_get_current_bssid" @@ -627,30 +627,45 @@ class NetworkSensorManager : SensorManager { val lac: String val cid: String + val signalStrengthLevel : Int + val signalStrengthDbm : Int + if (cell is CellInfoGsm) { radio = "GSM" lac = cell.cellIdentity.lac.toString() cid = cell.cellIdentity.cid.toString() + signalStrengthLevel = cell.cellSignalStrength.level + signalStrengthDbm = cell.cellSignalStrength.dbm } else if (cell is CellInfoCdma) { radio = "CDMA" lac = cell.cellIdentity.networkId.toString() cid = cell.cellIdentity.basestationId.toString() + signalStrengthLevel = cell.cellSignalStrength.level + signalStrengthDbm = cell.cellSignalStrength.dbm } else if (cell is CellInfoWcdma) { radio = "UMTS" lac = cell.cellIdentity.lac.toString() cid = cell.cellIdentity.cid.toString() + signalStrengthLevel = cell.cellSignalStrength.level + signalStrengthDbm = cell.cellSignalStrength.dbm } else if (cell is CellInfoLte) { radio = "LTE" lac = cell.cellIdentity.tac.toString() cid = cell.cellIdentity.ci.toString() + signalStrengthLevel = cell.cellSignalStrength.level + signalStrengthDbm = cell.cellSignalStrength.dbm } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && cell is CellInfoTdscdma) { radio = "UTMS" lac = cell.cellIdentity.lac.toString() cid = cell.cellIdentity.cid.toString() + signalStrengthLevel = cell.cellSignalStrength.level + signalStrengthDbm = cell.cellSignalStrength.dbm } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && cell is CellInfoNr) { radio = "NR" lac = (cell.cellIdentity as CellIdentityNr).tac.toString() cid = (cell.cellIdentity as CellIdentityNr).nci.toString() + signalStrengthLevel = cell.cellSignalStrength.level + signalStrengthDbm = cell.cellSignalStrength.dbm } else { onSensorUpdated( context, @@ -669,7 +684,10 @@ class NetworkSensorManager : SensorManager { cellConnection, cellId, cellConnection.statelessIcon, - mapOf() + mapOf( + "signal_strength_dbm" to signalStrengthDbm, + "signal_strength_level" to signalStrengthLevel + ) ) } From 051576f4314168919a120caf5600916174737334 Mon Sep 17 00:00:00 2001 From: MeGilles Date: Mon, 6 May 2024 12:18:07 +0200 Subject: [PATCH 10/10] fix klint --- .../companion/android/common/sensors/NetworkSensorManager.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 fe9e19f4b3a..3a9080dbe61 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 @@ -627,8 +627,8 @@ class NetworkSensorManager : SensorManager { val lac: String val cid: String - val signalStrengthLevel : Int - val signalStrengthDbm : Int + val signalStrengthLevel: Int + val signalStrengthDbm: Int if (cell is CellInfoGsm) { radio = "GSM"