diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 4436e966..c1b6af4d 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -58,8 +58,6 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere val statusChanged = StickyEvent0() val groupChanged = StickyEvent1 { group } - private var groups: Collection = emptyList() - fun startWps(pin: String? = null) { val channel = channel if (channel == null) SmartSnackbar.make(R.string.repeater_failure_disconnected).show() @@ -84,22 +82,30 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere fun resetCredentials() { val channel = channel if (channel == null) SmartSnackbar.make(R.string.repeater_failure_disconnected).show() - else (groups + group).filterNotNull().forEach { - p2pManager.deletePersistentGroup(channel, it.netId, object : WifiP2pManager.ActionListener { - override fun onSuccess() = SmartSnackbar.make(R.string.repeater_reset_credentials_success) - .shortToast().show() - override fun onFailure(reason: Int) = SmartSnackbar.make( - formatReason(R.string.repeater_reset_credentials_failure, reason)).show() - }) - } + else p2pManager.deletePersistentGroup(channel, (group ?: return).netId, + object : WifiP2pManager.ActionListener { + override fun onSuccess() = SmartSnackbar.make(R.string.repeater_reset_credentials_success) + .shortToast().show() + override fun onFailure(reason: Int) = SmartSnackbar.make( + formatReason(R.string.repeater_reset_credentials_failure, reason)).show() + }) } fun requestGroupUpdate() { group = null + val channel = channel ?: return try { - p2pManager.requestPersistentGroupInfo(channel ?: return) { - groups = it - if (it.size == 1) group = it.single() + p2pManager.requestPersistentGroupInfo(channel) { + val ownedGroups = it.filter { it.isGroupOwner } + val main = ownedGroups.minBy { it.netId } + group = main + if (main != null) ownedGroups.filter { it.netId != main.netId }.forEach { + p2pManager.deletePersistentGroup(channel, it.netId, object : WifiP2pManager.ActionListener { + override fun onSuccess() = Timber.i("Removed redundant owned group: $it") + override fun onFailure(reason: Int) = SmartSnackbar.make( + formatReason(R.string.repeater_clean_pog_failure, reason)).show() + }) + } } } catch (e: ReflectiveOperationException) { Timber.w(e) diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index 68ca90a3..f6a5a3e6 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -27,6 +27,7 @@ Reset Credentials reset. Failed to reset credentials (reason: %s) + Failed to remove redundant P2P group (reason: %s) Wi\u2011Fi direct unavailable, please enable Wi\u2011Fi Failed to create P2P group (reason: %s)