Remove redundant P2P groups if found

If more than one group is found owned by the device, Android system will cycle through these groups using an LRU cache which is undesirable.

The reason for causing >1 groups is unknown.
This commit is contained in:
Mygod
2018-12-21 15:23:39 +08:00
parent f1db0f2c8f
commit dae07f79ce
2 changed files with 20 additions and 13 deletions

View File

@@ -58,8 +58,6 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere
val statusChanged = StickyEvent0() val statusChanged = StickyEvent0()
val groupChanged = StickyEvent1 { group } val groupChanged = StickyEvent1 { group }
private var groups: Collection<WifiP2pGroup> = emptyList()
fun startWps(pin: String? = null) { fun startWps(pin: String? = null) {
val channel = channel val channel = channel
if (channel == null) SmartSnackbar.make(R.string.repeater_failure_disconnected).show() if (channel == null) SmartSnackbar.make(R.string.repeater_failure_disconnected).show()
@@ -84,22 +82,30 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere
fun resetCredentials() { fun resetCredentials() {
val channel = channel val channel = channel
if (channel == null) SmartSnackbar.make(R.string.repeater_failure_disconnected).show() if (channel == null) SmartSnackbar.make(R.string.repeater_failure_disconnected).show()
else (groups + group).filterNotNull().forEach { else p2pManager.deletePersistentGroup(channel, (group ?: return).netId,
p2pManager.deletePersistentGroup(channel, it.netId, object : WifiP2pManager.ActionListener { object : WifiP2pManager.ActionListener {
override fun onSuccess() = SmartSnackbar.make(R.string.repeater_reset_credentials_success) override fun onSuccess() = SmartSnackbar.make(R.string.repeater_reset_credentials_success)
.shortToast().show() .shortToast().show()
override fun onFailure(reason: Int) = SmartSnackbar.make( override fun onFailure(reason: Int) = SmartSnackbar.make(
formatReason(R.string.repeater_reset_credentials_failure, reason)).show() formatReason(R.string.repeater_reset_credentials_failure, reason)).show()
}) })
} }
}
fun requestGroupUpdate() { fun requestGroupUpdate() {
group = null group = null
val channel = channel ?: return
try { try {
p2pManager.requestPersistentGroupInfo(channel ?: return) { p2pManager.requestPersistentGroupInfo(channel) {
groups = it val ownedGroups = it.filter { it.isGroupOwner }
if (it.size == 1) group = it.single() 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) { } catch (e: ReflectiveOperationException) {
Timber.w(e) Timber.w(e)

View File

@@ -27,6 +27,7 @@
<string name="repeater_reset_credentials">Reset</string> <string name="repeater_reset_credentials">Reset</string>
<string name="repeater_reset_credentials_success">Credentials reset.</string> <string name="repeater_reset_credentials_success">Credentials reset.</string>
<string name="repeater_reset_credentials_failure">Failed to reset credentials (reason: %s)</string> <string name="repeater_reset_credentials_failure">Failed to reset credentials (reason: %s)</string>
<string name="repeater_clean_pog_failure">Failed to remove redundant P2P group (reason: %s)</string>
<string name="repeater_p2p_unavailable">Wi\u2011Fi direct unavailable, please enable Wi\u2011Fi</string> <string name="repeater_p2p_unavailable">Wi\u2011Fi direct unavailable, please enable Wi\u2011Fi</string>
<string name="repeater_create_group_failure">Failed to create P2P group (reason: %s)</string> <string name="repeater_create_group_failure">Failed to create P2P group (reason: %s)</string>