Skip to content

Commit

Permalink
Do not remove starter from stopService
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed Feb 28, 2024
1 parent 1b22bbf commit cd91587
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
val ifaceChanged = StickyEvent1 { iface }
val configuration get() = reservation?.configuration

fun stop() {
fun stop(shouldDisable: Boolean = true) {
when (iface) {
null -> return // stopped
"" -> WifiApManager.cancelLocalOnlyHotspotRequest()
}
reservation?.close()
stopService()
stopService(shouldDisable)
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
private val receiver = broadcastReceiver { _, intent -> updateState(intent) }
private var receiverRegistered = false
private fun updateState(intent: Intent) {
// based on: https://android.googlesource.com/platform/packages/services/Car/+/72c71d2/service/src/com/android/car/CarProjectionService.java#160
// based on: https://android.googlesource.com/platform/packages/services/Car/+/407f65c/service/src/com/android/car/CarProjectionService.java#180
lastState = Triple(intent.wifiApState, intent.getStringExtra(WifiApManager.EXTRA_WIFI_AP_INTERFACE_NAME),
intent.getIntExtra(WifiApManager.EXTRA_WIFI_AP_FAILURE_REASON, 0))
}
Expand Down Expand Up @@ -245,13 +245,13 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
}

override fun onDestroy() {
binder.stop()
binder.stop(false)
unregisterReceiver(true)
super.onDestroy()
}

private fun stopService() {
BootReceiver.delete<LocalOnlyHotspotService>()
private fun stopService(shouldDisable: Boolean = true) {
if (shouldDisable) BootReceiver.delete<LocalOnlyHotspotService>()
binder.iface = null
unregisterReceiver()
ServiceNotification.stopForeground(this)
Expand Down
6 changes: 3 additions & 3 deletions mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,8 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
}
})
}
private fun cleanLocked() {
BootReceiver.delete<RepeaterService>()
private fun cleanLocked(shouldDisable: Boolean = true) {
if (shouldDisable) BootReceiver.delete<RepeaterService>()
if (receiverRegistered) {
ensureReceiverUnregistered(receiver)
p2pPoller?.cancel()
Expand All @@ -584,7 +584,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
override fun onDestroy() {
if (status != Status.IDLE) binder.shutdown()
launch { // force clean to prevent leakage
cleanLocked()
cleanLocked(false)
cancel()
}
app.pref.unregisterOnSharedPreferenceChangeListener(this)
Expand Down
16 changes: 10 additions & 6 deletions mobile/src/main/java/be/mygod/vpnhotspot/StaticIpSetter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,24 @@ class StaticIpSetter : BootReceiver.Startable {
false
}
} catch (e: RoutingCommands.UnexpectedOutputException) {
if (Routing.shouldSuppressIpError(e, enabled)) return@use false
Timber.w(IOException("Failed to add link", e))
if (Routing.shouldSuppressIpError(e, enabled)) return@use null
Timber.w(IOException("Failed to modify link", e))
SmartSnackbar.make(e).show()
false
null
}
}
} catch (_: CancellationException) {
false
null
} catch (e: Exception) {
Timber.w(e)
SmartSnackbar.make(e).show()
false
null
}
when (success) {
true -> BootReceiver.add<StaticIpSetter>(StaticIpSetter())
false -> BootReceiver.delete<StaticIpSetter>()
null -> { }
}
if (success) BootReceiver.add<StaticIpSetter>(StaticIpSetter()) else BootReceiver.delete<StaticIpSetter>()
ifaceEvent()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ class TetheringService : IpNeighbourMonitoringService(), TetheringManager.Tether

override fun onDestroy() {
launch {
BootReceiver.delete<TetheringService>()
unregisterReceiver()
downstreams.values.forEach { it.stop() } // force clean to prevent leakage
cancel()
Expand Down

0 comments on commit cd91587

Please sign in to comment.