From e73d89b5b8cdcd06b39fdfdedcb0073f79fc4ad0 Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 5 Apr 2019 10:03:51 +0800 Subject: [PATCH] Support showing repeater frequency in Android Q --- .../src/main/java/be/mygod/vpnhotspot/RepeaterService.kt | 6 ++++-- .../java/be/mygod/vpnhotspot/manage/RepeaterManager.kt | 7 +++++++ .../net/wifi/configuration/WifiApDialogFragment.kt | 9 ++++++++- .../net/wifi/configuration/WifiConfiguration.kt | 5 +++++ mobile/src/main/res/layout/listitem_repeater.xml | 2 +- mobile/src/main/res/values-zh-rCN/strings.xml | 1 + mobile/src/main/res/values/strings.xml | 1 + 7 files changed, 27 insertions(+), 4 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index b71f1c7d..ba62bb9b 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -21,6 +21,7 @@ import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.netId import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.requestPersistentGroupInfo import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.setWifiP2pChannels import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.startWps +import be.mygod.vpnhotspot.net.wifi.configuration.channelToFrequency import be.mygod.vpnhotspot.util.* import be.mygod.vpnhotspot.widget.SmartSnackbar import timber.log.Timber @@ -279,8 +280,9 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere } else p2pManager.createGroup(channel, WifiP2pConfig.Builder().apply { setNetworkName(PLACEHOLDER_NETWORK_NAME) setPassphrase(passphrase) - val frequency = operatingChannel - if (frequency == 0) setGroupOperatingBand(operatingBand) else setGroupOperatingFrequency(frequency) + val channel = operatingChannel + if (channel == 0) setGroupOperatingBand(operatingBand) + else setGroupOperatingFrequency(channelToFrequency(channel)) }.build().run { useParcel { p -> p.writeParcelable(this, 0) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt index b7add94c..f8c38527 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt @@ -55,6 +55,12 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic else -> false } + val title: CharSequence @Bindable get() { + if (BuildCompat.isAtLeastQ()) binder?.group?.frequency?.let { + return parent.getString(R.string.repeater_channel, it, frequencyToChannel(it)) + } + return parent.getString(R.string.title_repeater) + } val addresses: CharSequence @Bindable get() { return try { NetworkInterface.getByName(p2pInterface ?: return "")?.formatAddresses() ?: "" @@ -70,6 +76,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic } fun onGroupChanged(group: WifiP2pGroup? = null) { p2pInterface = group?.`interface` + if (BuildCompat.isAtLeastQ()) notifyPropertyChanged(BR.title) notifyPropertyChanged(BR.addresses) } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiApDialogFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiApDialogFragment.kt index ed61142b..7a28b7f1 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiApDialogFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiApDialogFragment.kt @@ -16,6 +16,7 @@ import android.widget.AdapterView import android.widget.ArrayAdapter import androidx.appcompat.app.AlertDialog import androidx.appcompat.widget.Toolbar +import androidx.core.os.BuildCompat import androidx.core.view.isGone import be.mygod.vpnhotspot.AlertDialogFragment import be.mygod.vpnhotspot.App.Companion.app @@ -112,7 +113,13 @@ class WifiApDialogFragment : AlertDialogFragment= 23) { bandOptions = mutableListOf().apply { - if (arg.p2pMode) add(BandOption.BandAny) else { + if (arg.p2pMode) { + add(BandOption.BandAny) + if (BuildCompat.isAtLeastQ()) { + add(BandOption.Band2GHz) + add(BandOption.Band5GHz) + } + } else { if (Build.VERSION.SDK_INT >= 28) add(BandOption.BandAny) add(BandOption.Band2GHz) add(BandOption.Band5GHz) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiConfiguration.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiConfiguration.kt index 661be180..5f290a14 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiConfiguration.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiConfiguration.kt @@ -64,6 +64,11 @@ fun channelToFrequency(channel: Int) = when (channel) { in 15..165 -> 5000 + 5 * channel else -> throw IllegalArgumentException("Invalid channel $channel") } +fun frequencyToChannel(frequency: Int) = when (frequency % 5) { + 2 -> ((frequency - 2407) / 5).also { check(it in 1..14) { "Invalid 2.4 GHz frequency $frequency" } } + 0 -> ((frequency - 5000) / 5).also { check(it in 15..165) { "Invalid 5 GHz frequency $frequency" } } + else -> throw IllegalArgumentException("Invalid frequency $frequency") +} val WifiConfiguration.apKeyManagement get() = allowedKeyManagement.nextSetBit(0).also { selected -> check(selected >= 0) { "No key management selected" } diff --git a/mobile/src/main/res/layout/listitem_repeater.xml b/mobile/src/main/res/layout/listitem_repeater.xml index d8723abb..2e6c09c0 100644 --- a/mobile/src/main/res/layout/listitem_repeater.xml +++ b/mobile/src/main/res/layout/listitem_repeater.xml @@ -40,7 +40,7 @@ 已连设备 设置选项 + 无线中继 (%1$d MHz, 频道 %2$d) WPS(不安全) 输入 PIN 一键加密 diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index 97f3aa67..60f79d31 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -14,6 +14,7 @@ Clients Settings + Repeater (%1$d MHz, channel %2$d) WPS (insecure) Enter PIN Push Button