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
This commit is contained in:
2025-12-19 07:26:43 +01:00
parent 10358e939c
commit e2bb186ac9
6 changed files with 114 additions and 34 deletions

View File

@@ -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<CellInfo>? = 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<CellInfo>? = 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)

View File

@@ -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"

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<!-- Phone body -->
<path
android:fillColor="#1e293b"
android:pathData="M15.2,37h18.5c1.8,0 3.4,1.5 3.4,3.4v33.7c0,1.8 -1.5,3.4 -3.4,3.4H15.2c-1.8,0 -3.4,-1.5 -3.4,-3.4V40.4c0,-1.8 1.5,-3.4 3.4,-3.4z"/>
<!-- Phone screen -->
<path
android:fillColor="#ffffff"
android:pathData="M17.3,40h14.3c0.7,0 1.3,0.6 1.3,1.3v24.4c0,0.7 -0.6,1.3 -1.3,1.3H17.3c-0.7,0 -1.3,-0.6 -1.3,-1.3V41.3c0,-0.7 0.6,-1.3 1.3,-1.3z"/>
<!-- Phone button -->
<path
android:fillColor="#ffffff"
android:pathData="M21.1,66.5h6.7c0.3,0 0.6,0.3 0.6,0.6v1.3c0,0.3 -0.3,0.6 -0.6,0.6h-6.7c-0.3,0 -0.6,-0.3 -0.6,-0.6v-1.3c0,-0.3 0.3,-0.6 0.6,-0.6z"/>
<!-- Signal bars -->
<path
android:fillColor="#0ea5e9"
android:pathData="M40.5,56.8h4.2c0.4,0 0.8,0.4 0.8,0.8v9.3c0,0.4 -0.4,0.8 -0.8,0.8h-4.2c-0.4,0 -0.8,-0.4 -0.8,-0.8v-9.3c0,-0.4 0.4,-0.8 0.8,-0.8z"/>
<path
android:fillColor="#0ea5e9"
android:pathData="M47.2,51.8h4.2c0.4,0 0.8,0.4 0.8,0.8v14.3c0,0.4 -0.4,0.8 -0.8,0.8h-4.2c-0.4,0 -0.8,-0.4 -0.8,-0.8V52.6c0,-0.4 0.4,-0.8 0.8,-0.8z"/>
<path
android:fillColor="#0ea5e9"
android:pathData="M54,46.8h4.2c0.4,0 0.8,0.4 0.8,0.8v19.3c0,0.4 -0.4,0.8 -0.8,0.8H54c-0.4,0 -0.8,-0.4 -0.8,-0.8V47.6c0,-0.4 0.4,-0.8 0.8,-0.8z"/>
<!-- Laptop screen -->
<path
android:fillColor="#1e293b"
android:pathData="M65.7,41.3h27c1.3,0 2.5,1.2 2.5,2.5v18.5c0,1.3 -1.2,2.5 -2.5,2.5h-27c-1.3,0 -2.5,-1.2 -2.5,-2.5V43.8c0,-1.3 1.2,-2.5 2.5,-2.5z"/>
<!-- Laptop display -->
<path
android:fillColor="#ffffff"
android:pathData="M67.8,43.4h22.8c0.4,0 0.8,0.4 0.8,0.8v13.5c0,0.4 -0.4,0.8 -0.8,0.8H67.8c-0.4,0 -0.8,-0.4 -0.8,-0.8V44.2c0,-0.4 0.4,-0.8 0.8,-0.8z"/>
<!-- Laptop base -->
<path
android:fillColor="#1e293b"
android:pathData="M63.2,60.8h32c0.4,0 0.8,0.4 0.8,0.8v2.9c0,0.4 -0.4,0.8 -0.8,0.8h-32c-0.4,0 -0.8,-0.4 -0.8,-0.8v-2.9c0,-0.4 0.4,-0.8 0.8,-0.8z"/>
</vector>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>

View File

@@ -13,4 +13,5 @@
<color name="on_error">#FFFFFF</color>
<color name="status_running">#4CAF50</color>
<color name="status_stopped">#9E9E9E</color>
<color name="ic_launcher_background">#FFFFFF</color>
</resources>