From 207368fd4d86b32fac84afdf35e471bdbfba2c46 Mon Sep 17 00:00:00 2001 From: Mygod Date: Wed, 14 Mar 2018 22:19:49 -0700 Subject: [PATCH] Refine onP2pConnectionChanged --- .../be/mygod/vpnhotspot/RepeaterService.kt | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 6f637f12..c8ae0f3c 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -272,28 +272,20 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, VpnMonitor.Ca * startService 3rd stop (if a group isn't already available), also called when connection changed */ private fun onP2pConnectionChanged(info: WifiP2pInfo, net: NetworkInfo?, group: WifiP2pGroup) { - when { - routing == null -> onGroupCreated(info, group) - group.isGroupOwner -> showNotification(group) - else -> { // P2P shutdown - clean() - return - } - } - this.group = group - binder.data?.onGroupChanged(group) debugLog(TAG, "P2P connection changed: $info\n$net\n$group") - } - private fun onGroupCreated(info: WifiP2pInfo, group: WifiP2pGroup) { - if (!info.groupFormed || !info.isGroupOwner) return - val owner = info.groupOwnerAddress ?: return - val downstream = group.`interface` ?: return - try { - if (initRouting(upstream, downstream, owner, dns)) doStart(group) + if (!info.groupFormed || !info.isGroupOwner || !group.isGroupOwner) { + if (routing != null) clean() // P2P shutdown + return + } + if (routing == null) try { + if (initRouting(upstream, group.`interface` ?: return, info.groupOwnerAddress ?: return, dns)) + doStart(group) } catch (e: SocketException) { startFailure(e.message, group) return } + this.group = group + binder.data?.onGroupChanged(group) } private fun initRouting(upstream: String?, downstream: String, owner: InetAddress, dns: List): Boolean {