Simplify handling start temp hotspot failures

This commit is contained in:
Mygod
2019-09-19 11:47:40 +08:00
parent ae3dc81aba
commit 134f0538a8

View File

@@ -2,7 +2,6 @@ package be.mygod.vpnhotspot
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.content.res.Configuration
import android.net.wifi.WifiManager import android.net.wifi.WifiManager
import android.os.Build import android.os.Build
import android.os.Handler import android.os.Handler
@@ -84,9 +83,6 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
if (binder.iface != null) return START_STICKY if (binder.iface != null) return START_STICKY
binder.iface = "" binder.iface = ""
updateNotification() // show invisible foreground notification to avoid being killed updateNotification() // show invisible foreground notification to avoid being killed
// throws IllegalStateException if the caller attempts to start the LocalOnlyHotspot while they
// have an outstanding request.
// https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/53e0284/service/java/com/android/server/wifi/WifiServiceImpl.java#1192
try { try {
app.wifi.startLocalOnlyHotspot(object : WifiManager.LocalOnlyHotspotCallback() { app.wifi.startLocalOnlyHotspot(object : WifiManager.LocalOnlyHotspotCallback() {
override fun onStarted(reservation: WifiManager.LocalOnlyHotspotReservation?) { override fun onStarted(reservation: WifiManager.LocalOnlyHotspotReservation?) {
@@ -116,27 +112,23 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
} }
else -> getString(R.string.failure_reason_unknown, reason) else -> getString(R.string.failure_reason_unknown, reason)
})).show() })).show()
startFailure() stopService()
} }
}, null) }, null)
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
Timber.w(e) // throws IllegalStateException if the caller attempts to start the LocalOnlyHotspot while they
// have an outstanding request.
// https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/53e0284/service/java/com/android/server/wifi/WifiServiceImpl.java#1192
WifiApManager.cancelLocalOnlyHotspotRequest()
SmartSnackbar.make(e).show() SmartSnackbar.make(e).show()
startFailure() stopService()
} catch (e: SecurityException) { } catch (e: SecurityException) {
SmartSnackbar.make(e).show() SmartSnackbar.make(e).show()
startFailure() stopService()
} }
return START_STICKY return START_STICKY
} }
private fun startFailure() {
binder.iface = null
updateNotification()
ServiceNotification.stopForeground(this@LocalOnlyHotspotService)
stopSelf()
}
override fun onIpNeighbourAvailable(neighbours: Collection<IpNeighbour>) { override fun onIpNeighbourAvailable(neighbours: Collection<IpNeighbour>) {
super.onIpNeighbourAvailable(neighbours) super.onIpNeighbourAvailable(neighbours)
if (Build.VERSION.SDK_INT >= 28) timeoutMonitor?.onClientsChanged(neighbours.none { if (Build.VERSION.SDK_INT >= 28) timeoutMonitor?.onClientsChanged(neighbours.none {