From e2bb186ac91a5a9ba9f6997eb38d26be0633fc14 Mon Sep 17 00:00:00 2001 From: Giovanni Harting <539@idlegandalf.com> Date: Fri, 19 Dec 2025 07:26:43 +0100 Subject: [PATCH] Fix signal strength and adaptive icon - Add signalStrength API fallback when allCellInfo returns empty - Create proper adaptive icon with vector foreground - Add logging to TetherStateReceiver for debugging autostart --- .../itsh/tetherapi/provider/StatusProvider.kt | 86 +++++++++++-------- .../tetherapi/receiver/TetherStateReceiver.kt | 7 ++ .../res/drawable/ic_launcher_foreground.xml | 44 ++++++++++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 ++ .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 ++ app/src/main/res/values/colors.xml | 1 + 6 files changed, 114 insertions(+), 34 deletions(-) create mode 100644 app/src/main/res/drawable/ic_launcher_foreground.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml diff --git a/app/src/main/java/dev/itsh/tetherapi/provider/StatusProvider.kt b/app/src/main/java/dev/itsh/tetherapi/provider/StatusProvider.kt index 2c55f8c..fc9e12a 100644 --- a/app/src/main/java/dev/itsh/tetherapi/provider/StatusProvider.kt +++ b/app/src/main/java/dev/itsh/tetherapi/provider/StatusProvider.kt @@ -132,47 +132,65 @@ class StatusProvider(private val context: Context) { context, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED - if (!hasLocationPermission) { - return Pair(-999, 0) + // Try allCellInfo first (requires location permission) + if (hasLocationPermission) { + try { + val cellInfoList: List? = telephonyManager.allCellInfo + if (!cellInfoList.isNullOrEmpty()) { + for (cellInfo in cellInfoList) { + if (!cellInfo.isRegistered) continue + + val dbm = when (cellInfo) { + is CellInfoLte -> cellInfo.cellSignalStrength.dbm + is CellInfoNr -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + cellInfo.cellSignalStrength.dbm + } else -999 + is CellInfoGsm -> cellInfo.cellSignalStrength.dbm + is CellInfoWcdma -> cellInfo.cellSignalStrength.dbm + is CellInfoCdma -> cellInfo.cellSignalStrength.dbm + else -> continue + } + + val level = when (cellInfo) { + is CellInfoLte -> cellInfo.cellSignalStrength.level + is CellInfoNr -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + cellInfo.cellSignalStrength.level + } else 0 + is CellInfoGsm -> cellInfo.cellSignalStrength.level + is CellInfoWcdma -> cellInfo.cellSignalStrength.level + is CellInfoCdma -> cellInfo.cellSignalStrength.level + else -> continue + } + + if (dbm != -999 && dbm != Int.MAX_VALUE) { + return Pair(dbm, level) + } + } + } + } catch (e: SecurityException) { + // Fall through to signalStrength fallback + } catch (e: Exception) { + // Fall through to signalStrength fallback + } } + // Fallback: use signalStrength API (doesn't require location for basic level) try { - val cellInfoList: List? = telephonyManager.allCellInfo - if (cellInfoList.isNullOrEmpty()) { - return Pair(-999, 0) - } - - for (cellInfo in cellInfoList) { - if (!cellInfo.isRegistered) continue - - val dbm = when (cellInfo) { - is CellInfoLte -> cellInfo.cellSignalStrength.dbm - is CellInfoNr -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - cellInfo.cellSignalStrength.dbm - } else -999 - is CellInfoGsm -> cellInfo.cellSignalStrength.dbm - is CellInfoWcdma -> cellInfo.cellSignalStrength.dbm - is CellInfoCdma -> cellInfo.cellSignalStrength.dbm - else -> continue + val signalStrength = telephonyManager.signalStrength + if (signalStrength != null) { + val level = signalStrength.level + // Try to get dBm from the signal strength + val dbm = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + signalStrength.cellSignalStrengths.firstOrNull()?.dbm ?: -999 + } else { + -999 } - - val level = when (cellInfo) { - is CellInfoLte -> cellInfo.cellSignalStrength.level - is CellInfoNr -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - cellInfo.cellSignalStrength.level - } else 0 - is CellInfoGsm -> cellInfo.cellSignalStrength.level - is CellInfoWcdma -> cellInfo.cellSignalStrength.level - is CellInfoCdma -> cellInfo.cellSignalStrength.level - else -> continue + if (level > 0 || dbm != -999) { + return Pair(dbm, level) } - - return Pair(dbm, level) } - } catch (e: SecurityException) { - return Pair(-999, 0) } catch (e: Exception) { - return Pair(-999, 0) + // Ignore } return Pair(-999, 0) diff --git a/app/src/main/java/dev/itsh/tetherapi/receiver/TetherStateReceiver.kt b/app/src/main/java/dev/itsh/tetherapi/receiver/TetherStateReceiver.kt index 9941e02..bfd44e4 100644 --- a/app/src/main/java/dev/itsh/tetherapi/receiver/TetherStateReceiver.kt +++ b/app/src/main/java/dev/itsh/tetherapi/receiver/TetherStateReceiver.kt @@ -5,12 +5,14 @@ import android.content.Context import android.content.Intent import android.net.ConnectivityManager import android.os.Build +import android.util.Log import androidx.preference.PreferenceManager import dev.itsh.tetherapi.service.TetherApiService class TetherStateReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { + Log.d(TAG, "Received broadcast: ${intent.action}") if (intent.action != ACTION_TETHER_STATE_CHANGED) return val prefs = PreferenceManager.getDefaultSharedPreferences(context) @@ -21,9 +23,13 @@ class TetherStateReceiver : BroadcastReceiver() { val isTethering = isTetherActive(context, intent) val port = prefs.getInt(PREF_PORT, TetherApiService.DEFAULT_PORT) + Log.d(TAG, "Tethering active: $isTethering, service running: ${TetherApiService.isRunning}") + if (isTethering && !TetherApiService.isRunning) { + Log.d(TAG, "Starting service on port $port") TetherApiService.start(context, port) } else if (!isTethering && TetherApiService.isRunning) { + Log.d(TAG, "Stopping service") TetherApiService.stop(context) } } @@ -54,6 +60,7 @@ class TetherStateReceiver : BroadcastReceiver() { } companion object { + private const val TAG = "TetherStateReceiver" const val ACTION_TETHER_STATE_CHANGED = "android.net.conn.TETHER_STATE_CHANGED" const val PREF_AUTO_START = "auto_start_on_tether" const val PREF_PORT = "api_port" diff --git a/app/src/main/res/drawable/ic_launcher_foreground.xml b/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..26047bb --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..5ed0a2d --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..5ed0a2d --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index cbcb603..9ba1860 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -13,4 +13,5 @@ #FFFFFF #4CAF50 #9E9E9E + #FFFFFF