Various fixes
This commit is contained in:
@@ -79,7 +79,10 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
|
|||||||
|
|
||||||
suspend fun work() {
|
suspend fun work() {
|
||||||
for (callback in channel) when (callback) {
|
for (callback in channel) when (callback) {
|
||||||
is LocalOnlyHotspotCallbacks.OnStarted -> configuration = callback.config.toCompat()
|
is LocalOnlyHotspotCallbacks.OnStarted -> {
|
||||||
|
configuration = callback.config.toCompat()
|
||||||
|
onFrameworkStarted(this)
|
||||||
|
}
|
||||||
is LocalOnlyHotspotCallbacks.OnStopped -> reservation = null
|
is LocalOnlyHotspotCallbacks.OnStopped -> reservation = null
|
||||||
is LocalOnlyHotspotCallbacks.OnFailed -> onFrameworkFailed(callback.reason)
|
is LocalOnlyHotspotCallbacks.OnFailed -> onFrameworkFailed(callback.reason)
|
||||||
}
|
}
|
||||||
@@ -88,20 +91,45 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
|
|||||||
|
|
||||||
private val binder = Binder()
|
private val binder = Binder()
|
||||||
private var reservation: Reservation? = null
|
private var reservation: Reservation? = null
|
||||||
set(value) {
|
private val lohCallback = object : WifiManager.LocalOnlyHotspotCallback() {
|
||||||
if (value == null) field?.close()
|
override fun onStarted(reservation: WifiManager.LocalOnlyHotspotReservation?) {
|
||||||
field = value
|
if (reservation == null) onFailed(-2) else {
|
||||||
timeoutMonitor?.close()
|
val r = Framework(reservation)
|
||||||
timeoutMonitor = null
|
this@LocalOnlyHotspotService.reservation = r
|
||||||
if (value != null) {
|
launch { onFrameworkStarted(r) }
|
||||||
val configuration = binder.configuration
|
|
||||||
if (Build.VERSION.SDK_INT < 30 && configuration!!.isAutoShutdownEnabled) {
|
|
||||||
timeoutMonitor = TetherTimeoutMonitor(configuration.shutdownTimeoutMillis, coroutineContext) {
|
|
||||||
value.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
override fun onStopped() {
|
||||||
|
reservation = null
|
||||||
|
}
|
||||||
|
override fun onFailed(reason: Int) = onFrameworkFailed(reason)
|
||||||
|
}
|
||||||
|
private fun onFrameworkStarted(reservation: Reservation) {
|
||||||
|
val configuration = reservation.configuration
|
||||||
|
if (Build.VERSION.SDK_INT < 30 && configuration!!.isAutoShutdownEnabled) {
|
||||||
|
timeoutMonitor = TetherTimeoutMonitor(configuration.shutdownTimeoutMillis, coroutineContext) {
|
||||||
|
reservation.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// attempt to update again
|
||||||
|
registerReceiver(null, IntentFilter(WifiApManager.WIFI_AP_STATE_CHANGED_ACTION))?.let(this::updateState)
|
||||||
|
val state = lastState
|
||||||
|
unregisterStateReceiver()
|
||||||
|
checkNotNull(state) { "Failed to obtain latest AP state" }
|
||||||
|
val iface = state.second
|
||||||
|
if (state.first != WifiApManager.WIFI_AP_STATE_ENABLED || iface.isNullOrEmpty()) {
|
||||||
|
if (state.first == WifiApManager.WIFI_AP_STATE_FAILED) {
|
||||||
|
SmartSnackbar.make(getString(R.string.tethering_temp_hotspot_failure,
|
||||||
|
WifiApManager.failureReasonLookup(state.third))).show()
|
||||||
|
}
|
||||||
|
return stopService()
|
||||||
|
}
|
||||||
|
binder.iface = iface
|
||||||
|
BootReceiver.add<LocalOnlyHotspotService>(Starter())
|
||||||
|
check(routingManager == null)
|
||||||
|
routingManager = RoutingManager.LocalOnly(this, iface).apply { start() }
|
||||||
|
IpNeighbourMonitor.registerCallback(this)
|
||||||
|
}
|
||||||
private fun onFrameworkFailed(reason: Int) {
|
private fun onFrameworkFailed(reason: Int) {
|
||||||
SmartSnackbar.make(getString(R.string.tethering_temp_hotspot_failure, when (reason) {
|
SmartSnackbar.make(getString(R.string.tethering_temp_hotspot_failure, when (reason) {
|
||||||
WifiManager.LocalOnlyHotspotCallback.ERROR_NO_CHANNEL -> {
|
WifiManager.LocalOnlyHotspotCallback.ERROR_NO_CHANNEL -> {
|
||||||
@@ -173,37 +201,11 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
|
|||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.w(e)
|
Timber.w(e)
|
||||||
SmartSnackbar.make(e).show()
|
SmartSnackbar.make(e).show()
|
||||||
|
} finally {
|
||||||
|
reservation = null
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Services.wifi.startLocalOnlyHotspot(object : WifiManager.LocalOnlyHotspotCallback() {
|
Services.wifi.startLocalOnlyHotspot(lohCallback, null)
|
||||||
override fun onStarted(reservation: WifiManager.LocalOnlyHotspotReservation?) {
|
|
||||||
if (reservation == null) onFailed(-2) else {
|
|
||||||
this@LocalOnlyHotspotService.reservation = Framework(reservation)
|
|
||||||
}
|
|
||||||
registerReceiver(null, IntentFilter(WifiApManager.WIFI_AP_STATE_CHANGED_ACTION))
|
|
||||||
?.let(this@LocalOnlyHotspotService::updateState) // attempt to update again
|
|
||||||
val state = lastState
|
|
||||||
unregisterStateReceiver()
|
|
||||||
checkNotNull(state) { "Failed to obtain latest AP state" }
|
|
||||||
val iface = state.second
|
|
||||||
if (state.first != WifiApManager.WIFI_AP_STATE_ENABLED || iface.isNullOrEmpty()) {
|
|
||||||
if (state.first == WifiApManager.WIFI_AP_STATE_FAILED) {
|
|
||||||
SmartSnackbar.make(getString(R.string.tethering_temp_hotspot_failure,
|
|
||||||
WifiApManager.failureReasonLookup(state.third))).show()
|
|
||||||
}
|
|
||||||
return stopService()
|
|
||||||
}
|
|
||||||
binder.iface = iface
|
|
||||||
BootReceiver.add<LocalOnlyHotspotService>(Starter())
|
|
||||||
check(routingManager == null)
|
|
||||||
routingManager = RoutingManager.LocalOnly(this@LocalOnlyHotspotService, iface).apply { start() }
|
|
||||||
IpNeighbourMonitor.registerCallback(this@LocalOnlyHotspotService)
|
|
||||||
}
|
|
||||||
override fun onStopped() {
|
|
||||||
reservation = null
|
|
||||||
}
|
|
||||||
override fun onFailed(reason: Int) = onFrameworkFailed(reason)
|
|
||||||
}, null)
|
|
||||||
} catch (e: IllegalStateException) {
|
} catch (e: IllegalStateException) {
|
||||||
// throws IllegalStateException if the caller attempts to start the LocalOnlyHotspot while they
|
// throws IllegalStateException if the caller attempts to start the LocalOnlyHotspot while they
|
||||||
// have an outstanding request.
|
// have an outstanding request.
|
||||||
|
|||||||
Reference in New Issue
Block a user