Skip to content
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
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
# Changelog

## SimBridge 2.4.0

Update both apps. Pairing is kept.

### Main phone
- **Messages work like Google Messages / Gmail:**
- **Swipe** a conversation: read / unread, archive, delete or pin. Choose what each direction does
in Settings > Messages. Delete shows **Undo**.
- **Select several** (long-press, then tap): pin, archive, read / unread, delete, block, select all.
- **Pinned** conversations stay on top; **Archived** ones leave the list until a new text arrives.
- **Filters:** All, Unread, Archived, Starred. **Search** also looks inside messages; a
conversation has its own search too.
- **Links, e-mail addresses and phone numbers** in texts open when tapped; one-time codes copy.
- **Long-press a message:** copy, star, forward, details, delete (several at once).
- **Send later:** hold Send and pick a time. Waiting texts show above the keyboard and can be
cancelled; they go out when the SIM phone is reachable.
- **Faster call translation:**
- Words appear **while they are spoken** (cloud speech), and the translation is written out as it
arrives instead of all at once.
- The next sentence is recognized while the previous one is translated.
- Long sentences end on a shorter pause; the default pause is now "Fast".
- **Battery time left** for the SIM phone on the dashboard, from its real drain (or time to full).

### SIM phone (gateway)
- **Hotspot comes on by itself when Wi-Fi is lost** (with home/work Wi-Fi set up and its switch on),
also after a restart away from home, so the main phone can always reach it.
- Time-left estimate from the measured drain of the current discharge.
- The 20 % low-battery alert was sent twice; now once at 20 % and once at 10 %.

## SimBridge 2.3.0

Update both apps. Pairing is kept.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ main phone only ever uses Wi-Fi. See the [disclaimer](DISCLAIMER.md).
| 🎧 **HD voice** | 16 kHz wideband audio between the phones (L16), falling back to normal phone quality (G.711) when needed. |
| 🔒 **Encrypted and paired** | Pair once by scanning a QR code. Signalling runs over **TLS** pinned to the SIM phone's key, audio over **SRTP**, and every call or text needs the paired password. Mobile-data and internet addresses are refused. |
| 💬 **Texts, pictures and group chats** | A Google Messages-style app: conversations, contact photos, bubbles, notifications with **Reply** and **Mark as read**, **Sent / Delivered** reports, picture messages (MMS) and **group texts**. |
| 👆 **Swipe, select, pin, archive, search** | Swipe a conversation (you choose what each direction does), select several for one action, pin, archive, star, search inside messages, tap links and phone numbers, and **send later** by holding Send. |
| 📥 **System SMS integration (optional)** | Make SimBridge the **default SMS app**: "Message" buttons in Contacts and other apps open it, the Phone app's *Reply with message* sends through the SIM, and texts are saved to Android's SMS database. Or keep Google Messages as default and just copy relayed texts into it (read-only). |
| 📵 **Missed calls while offline** | Calls the SIM phone gets while the main phone is out of reach are added to the Phone app's history with a *Call back* notification. |
| 🚫 **Call blocking** | Block numbers (and hidden callers). The SIM phone rejects them before they ring. With SimBridge as the default SMS app, numbers blocked in the Phone app are included too. |
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
applicationId = "io.github.simbridge.gateway"
minSdk = 30
targetSdk = 36
versionCode = 440
versionName = "2.3.0"
versionCode = 450
versionName = "2.4.0"
// Filter all transitive JNI libraries as well as PJSIP's own module outputs.
ndk {
//abiFilters += listOf("arm64-v8a", "x86_64")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,41 @@ object SmsSender {
}
}

/**
* Time until empty, from the real drain of the current discharge. Percent readings are whole
* numbers, so only the moments the percent drops are used (the battery is then exactly at that
* level), and the rate comes from the last [WINDOW_MS] when that holds enough drops, so a recent
* change in use (hotspot on, a long call) shows up without one noisy step swinging the answer.
*/
object BatteryEstimate {
private const val WINDOW_MS = 3 * 3_600_000L
private const val KEEP_MS = 24 * 3_600_000L
/** (time, percent) at each drop while discharging; times from a clock that runs during sleep. */
private val drops = ArrayDeque<Pair<Long, Int>>()
private var lastPct = -1

@Synchronized fun note(nowMs: Long, pct: Int, charging: Boolean) {
if (charging || (lastPct >= 0 && pct > lastPct)) drops.clear()
else if (lastPct >= 0 && pct < lastPct) drops.addLast(nowMs to pct)
lastPct = if (charging) -1 else pct
while (drops.isNotEmpty() && nowMs - drops.first().first > KEEP_MS) drops.removeFirst()
}

/** Minutes until empty, or null until at least 2 % has drained over 10 minutes or more. */
@Synchronized fun minutesLeft(nowMs: Long): Int? {
val last = drops.lastOrNull() ?: return null
val recent = drops.filter { nowMs - it.first <= WINDOW_MS }
val first = (if (recent.size >= 4) recent else drops).first()
val drained = first.second - last.second
val took = last.first - first.first
if (drained < 2 || took < 10 * 60_000L) return null
val msPerPct = took.toDouble() / drained
// At the last drop the battery was exactly at last.second; time has passed since.
val left = last.second * msPerPct - (nowMs - last.first)
return (left / 60_000).toInt().coerceAtLeast(0)
}
}

object BatteryInfo {
data class State(val percent: Int, val charging: Boolean)
fun read(ctx: Context): State? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ class GatewayControl(private val app: Context, private val rt: GatewayRuntime) {
@SuppressLint("MissingPermission")
fun status(): JSONObject {
val o = JSONObject().put("op", "status").put("at", System.currentTimeMillis())
BatteryInfo.read(app)?.let { o.put("battery", it.percent).put("charging", it.charging) }
BatteryInfo.read(app)?.let { b ->
o.put("battery", b.percent).put("charging", b.charging)
if (b.charging) runCatching { app.getSystemService(android.os.BatteryManager::class.java).computeChargeTimeRemaining() }
.getOrNull()?.takeIf { it > 0 }?.let { o.put("chargeMin", (it / 60_000).toInt()) }
else BatteryEstimate.minutesLeft(android.os.SystemClock.elapsedRealtime())?.let { o.put("batteryMin", it) }
}
o.put("powerSaver", rt.companionPower.value).put("quiet", rt.quietFlags.value)
o.put("uptimeMin", SystemClock.elapsedRealtime() / 60_000)
o.put("version", runCatching { app.packageManager.getPackageInfo(app.packageName, 0).versionName }.getOrNull() ?: "")
Expand Down Expand Up @@ -421,22 +426,37 @@ class GatewayControl(private val app: Context, private val rt: GatewayRuntime) {

private var wifiWatch: android.net.ConnectivityManager.NetworkCallback? = null

/** Called once when the gateway starts. */
fun start() {
watchWifiLoss()
// After a restart away from every saved place: make sure the main phone can reach us.
main.postDelayed({ rt.worker { hotspotIfNoWifi("started without Wi-Fi") } }, 45_000)
}

/** "Hotspot when Wi-Fi is lost" is on (the Places switch) and at least one place is saved. */
private fun autoHotspot(): Boolean = runCatching {
val p = JSONObject(prefs.getString("places", "{}")!!)
p.optBoolean("auto", true) && (p.optJSONArray("list")?.length() ?: 0) > 0
}.getOrDefault(false)

private fun hotspotIfNoWifi(why: String) {
if (!autoHotspot() || hotspotOn()) return
val ssid = privilegedRaw("wifi-ssid", "")
if (ssid.startsWith("error") || ssid.isNotEmpty()) return
prefs.edit().remove("at_place").apply()
AppLog.i("control: $why, hotspot on")
privileged("hotspot", "on")
}

private fun watchWifiLoss() {
if (wifiWatch != null) return
val cm = app.getSystemService(android.net.ConnectivityManager::class.java)
val req = android.net.NetworkRequest.Builder().addTransportType(android.net.NetworkCapabilities.TRANSPORT_WIFI).build()
wifiWatch = object : android.net.ConnectivityManager.NetworkCallback() {
override fun onLost(network: android.net.Network) {
val auto = runCatching { JSONObject(prefs.getString("places", "{}")!!).optBoolean("auto", true) }.getOrDefault(true)
if (prefs.getString("at_place", "").isNullOrEmpty() || !auto) return
if (!autoHotspot()) return
// Brief drops happen; only act if Wi-Fi stays gone.
main.postDelayed({ rt.worker {
if (privilegedRaw("wifi-ssid", "").isEmpty()) {
prefs.edit().remove("at_place").apply()
AppLog.i("control: left place, hotspot back on")
privileged("hotspot", "on")
}
} }, 20_000)
main.postDelayed({ rt.worker { hotspotIfNoWifi("Wi-Fi lost") } }, 20_000)
}
}
runCatching { cm.registerNetworkCallback(req, wifiWatch!!) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class GatewayRuntime private constructor(context: Context) {
if (started) return@execute
started = true
watchBattery()
control.start()
cellular.onCallsChanged = { snapshot ->
_health.update { it.copy(calls = snapshot, inCallServiceBound = CellularEndpoint.isInCallServiceBound) }
worker.execute {
Expand Down Expand Up @@ -911,8 +912,8 @@ class GatewayRuntime private constructor(context: Context) {
}

/**
* Low-battery alerts to the main phone: once at 20 % and once at 10 % per discharge, reset when
* the charger is connected. Sent as a gateway text, so it waits in the queue if the link is down.
* Feeds the time-left estimate, and sends a second low-battery alert at 10 % (the first, at
* 20 %, comes from [GatewayAlerts]). Reset when the charger is connected.
*/
private fun watchBattery() {
val prefs = app.getSharedPreferences("control", Context.MODE_PRIVATE)
Expand All @@ -923,11 +924,11 @@ class GatewayRuntime private constructor(context: Context) {
if (level < 0) return
val pct = level * 100 / scale
val plugged = i.getIntExtra(android.os.BatteryManager.EXTRA_PLUGGED, 0) != 0
BatteryEstimate.note(android.os.SystemClock.elapsedRealtime(), pct, plugged)
val warned = prefs.getInt("battery_warned", 100)
when {
plugged -> if (warned != 100) prefs.edit().putInt("battery_warned", 100).apply()
pct <= 10 && warned > 10 -> { prefs.edit().putInt("battery_warned", 10).apply(); alerts.post("🪫 The SIM phone battery is at $pct %. Plug it in soon, or calls and texts stop.") }
pct <= 20 && warned > 20 -> { prefs.edit().putInt("battery_warned", 20).apply(); alerts.post("🔋 The SIM phone battery is at $pct %.") }
}
}
}, android.content.IntentFilter(android.content.Intent.ACTION_BATTERY_CHANGED))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.sparkcyf.voicesipagent.app

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test

class BatteryEstimateTest {
private val min = 60_000L

@Test
fun timeLeftFollowsTheRealDrain() {
var t = 0L
BatteryEstimate.note(t, 80, charging = true) // reset
BatteryEstimate.note(t, 80, charging = false)
// 1 % every 30 minutes, down to 76 %.
for (p in 79 downTo 76) { t += 30 * min; BatteryEstimate.note(t, p, charging = false) }
// 76 % at 30 min each, minus the 10 minutes since the last drop.
assertEquals(76 * 30 - 10, BatteryEstimate.minutesLeft(t + 10 * min))
}

@Test
fun noGuessFromOneDropOrAfterCharging() {
BatteryEstimate.note(0, 90, charging = true)
BatteryEstimate.note(0, 90, charging = false)
BatteryEstimate.note(5 * min, 89, charging = false)
assertNull(BatteryEstimate.minutesLeft(6 * min))
BatteryEstimate.note(7 * min, 89, charging = true)
assertNull(BatteryEstimate.minutesLeft(8 * min))
}
}
4 changes: 2 additions & 2 deletions phone/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId = "io.github.simbridge"
minSdk = 34
targetSdk = 36
versionCode = 240
versionName = "2.3.0"
versionCode = 250
versionName = "2.4.0"
}
// Same signing source as the gateway app: a git-ignored signing.properties in the project root.
val localSigning = Properties().apply {
Expand Down
Loading
Loading