-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Reflect WiFi hotspot availability on Android 13 (#573)
# What kind of change does this PR introduce? Fixes the hotspot state detection on Android 13. # What is the current behavior? This is a temporary fix for [#569 ](#569) but a long-term solution is still needed. # What is the new behavior (if this is a feature change)? On Android 13, we are looking to the gateway addresses request results to infer if hotspot is available. In previous versions of Android, there is no behavior change.
- Loading branch information
1 parent
4c08e68
commit 88a791d
Showing
12 changed files
with
285 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
app/src/main/java/tech/relaycorp/courier/background/ForegroundAppMonitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tech.relaycorp.courier.background | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import android.os.Bundle | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class ForegroundAppMonitor | ||
@Inject constructor() : Application.ActivityLifecycleCallbacks { | ||
private val activityCountFlow = MutableStateFlow(0) | ||
|
||
fun observe() = activityCountFlow.map { if (it == 0) State.Background else State.Foreground } | ||
|
||
override fun onActivityStarted(activity: Activity) { | ||
activityCountFlow.value++ | ||
} | ||
|
||
override fun onActivityStopped(activity: Activity) { | ||
activityCountFlow.value-- | ||
} | ||
|
||
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit | ||
override fun onActivityResumed(activity: Activity) = Unit | ||
override fun onActivityPaused(activity: Activity) = Unit | ||
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit | ||
override fun onActivityDestroyed(activity: Activity) = Unit | ||
|
||
enum class State { | ||
Foreground, Background | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
app/src/main/java/tech/relaycorp/courier/background/WifiHotspotStateReceiver.kt
This file was deleted.
Oops, something went wrong.
121 changes: 121 additions & 0 deletions
121
app/src/main/java/tech/relaycorp/courier/background/WifiHotspotStateWatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package tech.relaycorp.courier.background | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.net.wifi.WifiManager | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.emptyFlow | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onEach | ||
import tech.relaycorp.cogrpc.server.GatewayIPAddressException | ||
import tech.relaycorp.courier.AppModule.WifiApStateAvailability | ||
import tech.relaycorp.courier.common.Logging.logger | ||
import tech.relaycorp.courier.common.tickerFlow | ||
import javax.inject.Inject | ||
import javax.inject.Named | ||
import javax.inject.Singleton | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
@Singleton | ||
class WifiHotspotStateWatcher | ||
@Inject constructor( | ||
private val context: Context, | ||
private val wifiApState: WifiApStateAvailability, | ||
private val foregroundAppMonitor: ForegroundAppMonitor, | ||
@Named("GetGatewayIpAddress") private val getGatewayIpAddress: () -> String, | ||
@Named("BackgroundCoroutineContext") private val backgroundCoroutineContext: CoroutineContext | ||
) { | ||
|
||
private val state = MutableStateFlow(WifiHotspotState.Disabled) | ||
fun state() = state.asStateFlow() | ||
|
||
private var pollingGatewayAddressesJob: Job? = null | ||
|
||
fun start() { | ||
when (wifiApState) { | ||
WifiApStateAvailability.Available -> { | ||
context.registerReceiver( | ||
wifiApStateChangeReceiver, | ||
IntentFilter(WIFI_AP_STATE_CHANGED_ACTION) | ||
) | ||
} | ||
WifiApStateAvailability.Unavailable -> { | ||
startPollingGatewayAddresses() | ||
} | ||
} | ||
} | ||
|
||
fun stop() { | ||
when (wifiApState) { | ||
WifiApStateAvailability.Available -> { | ||
context.unregisterReceiver(wifiApStateChangeReceiver) | ||
} | ||
WifiApStateAvailability.Unavailable -> { | ||
stopPollingGatewayAddresses() | ||
} | ||
} | ||
} | ||
|
||
private fun startPollingGatewayAddresses() { | ||
pollingGatewayAddressesJob = foregroundAppMonitor.observe() | ||
.flatMapLatest { | ||
if (it == ForegroundAppMonitor.State.Foreground) { | ||
tickerFlow(POLLING_GATEWAY_ADDRESS_INTERVAL) | ||
} else { | ||
emptyFlow() | ||
} | ||
}.map { | ||
try { | ||
getGatewayIpAddress() | ||
WifiHotspotState.Enabled | ||
} catch (exception: GatewayIPAddressException) { | ||
WifiHotspotState.Disabled | ||
} | ||
} | ||
.distinctUntilChanged() | ||
.onEach { | ||
logger.info("Hotspot State $it") | ||
state.value = it | ||
} | ||
.launchIn(CoroutineScope(backgroundCoroutineContext)) | ||
} | ||
|
||
private fun stopPollingGatewayAddresses() { | ||
pollingGatewayAddressesJob?.cancel() | ||
pollingGatewayAddressesJob = null | ||
} | ||
|
||
private val wifiApStateChangeReceiver by lazy { | ||
object : BroadcastReceiver() { | ||
override fun onReceive(context: Context, intent: Intent) { | ||
if (intent.action != WIFI_AP_STATE_CHANGED_ACTION) return | ||
|
||
val stateFlag = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0) | ||
logger.info("Hotspot State $stateFlag") | ||
state.value = | ||
if (stateFlag == WIFI_AP_STATE_ENABLED) { | ||
WifiHotspotState.Enabled | ||
} else { | ||
WifiHotspotState.Disabled | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
// From WifiManager documentation | ||
private const val WIFI_AP_STATE_CHANGED_ACTION = "android.net.wifi.WIFI_AP_STATE_CHANGED" | ||
private const val WIFI_AP_STATE_ENABLED = 13 | ||
|
||
private val POLLING_GATEWAY_ADDRESS_INTERVAL = 2.seconds | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/tech/relaycorp/courier/common/TickerFlow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package tech.relaycorp.courier.common | ||
|
||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.flow | ||
import kotlin.time.Duration | ||
|
||
fun tickerFlow(duration: Duration) = flow { | ||
while (true) { | ||
emit(Unit) | ||
delay(duration) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.