Support showing repeater frequency in Android Q

This commit is contained in:
Mygod
2019-04-05 10:03:51 +08:00
parent 458dfaee2f
commit e73d89b5b8
7 changed files with 27 additions and 4 deletions

View File

@@ -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.requestPersistentGroupInfo
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.setWifiP2pChannels import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.setWifiP2pChannels
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.startWps 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.util.*
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber import timber.log.Timber
@@ -279,8 +280,9 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere
} else p2pManager.createGroup(channel, WifiP2pConfig.Builder().apply { } else p2pManager.createGroup(channel, WifiP2pConfig.Builder().apply {
setNetworkName(PLACEHOLDER_NETWORK_NAME) setNetworkName(PLACEHOLDER_NETWORK_NAME)
setPassphrase(passphrase) setPassphrase(passphrase)
val frequency = operatingChannel val channel = operatingChannel
if (frequency == 0) setGroupOperatingBand(operatingBand) else setGroupOperatingFrequency(frequency) if (channel == 0) setGroupOperatingBand(operatingBand)
else setGroupOperatingFrequency(channelToFrequency(channel))
}.build().run { }.build().run {
useParcel { p -> useParcel { p ->
p.writeParcelable(this, 0) p.writeParcelable(this, 0)

View File

@@ -55,6 +55,12 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
else -> false 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() { val addresses: CharSequence @Bindable get() {
return try { return try {
NetworkInterface.getByName(p2pInterface ?: return "")?.formatAddresses() ?: "" NetworkInterface.getByName(p2pInterface ?: return "")?.formatAddresses() ?: ""
@@ -70,6 +76,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
} }
fun onGroupChanged(group: WifiP2pGroup? = null) { fun onGroupChanged(group: WifiP2pGroup? = null) {
p2pInterface = group?.`interface` p2pInterface = group?.`interface`
if (BuildCompat.isAtLeastQ()) notifyPropertyChanged(BR.title)
notifyPropertyChanged(BR.addresses) notifyPropertyChanged(BR.addresses)
} }

View File

@@ -16,6 +16,7 @@ import android.widget.AdapterView
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.core.os.BuildCompat
import androidx.core.view.isGone import androidx.core.view.isGone
import be.mygod.vpnhotspot.AlertDialogFragment import be.mygod.vpnhotspot.AlertDialogFragment
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
@@ -112,7 +113,13 @@ class WifiApDialogFragment : AlertDialogFragment<WifiApDialogFragment.Arg, WifiA
if (!arg.readOnly) dialogView.password.addTextChangedListener(this@WifiApDialogFragment) if (!arg.readOnly) dialogView.password.addTextChangedListener(this@WifiApDialogFragment)
if (Build.VERSION.SDK_INT >= 23) { if (Build.VERSION.SDK_INT >= 23) {
bandOptions = mutableListOf<BandOption>().apply { bandOptions = mutableListOf<BandOption>().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) if (Build.VERSION.SDK_INT >= 28) add(BandOption.BandAny)
add(BandOption.Band2GHz) add(BandOption.Band2GHz)
add(BandOption.Band5GHz) add(BandOption.Band5GHz)

View File

@@ -64,6 +64,11 @@ fun channelToFrequency(channel: Int) = when (channel) {
in 15..165 -> 5000 + 5 * channel in 15..165 -> 5000 + 5 * channel
else -> throw IllegalArgumentException("Invalid channel $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 -> val WifiConfiguration.apKeyManagement get() = allowedKeyManagement.nextSetBit(0).also { selected ->
check(selected >= 0) { "No key management selected" } check(selected >= 0) { "No key management selected" }

View File

@@ -40,7 +40,7 @@
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/title_repeater" android:text="@{data.title}"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"/> android:textAppearance="@style/TextAppearance.AppCompat.Subhead"/>
<be.mygod.vpnhotspot.widget.AutoCollapseTextView <be.mygod.vpnhotspot.widget.AutoCollapseTextView

View File

@@ -6,6 +6,7 @@
<string name="title_clients">已连设备</string> <string name="title_clients">已连设备</string>
<string name="title_settings">设置选项</string> <string name="title_settings">设置选项</string>
<string name="repeater_channel">无线中继 (%1$d MHz, 频道 %2$d)</string>
<string name="repeater_wps">WPS不安全</string> <string name="repeater_wps">WPS不安全</string>
<string name="repeater_wps_dialog_title">输入 PIN</string> <string name="repeater_wps_dialog_title">输入 PIN</string>
<string name="repeater_wps_dialog_pbc">一键加密</string> <string name="repeater_wps_dialog_pbc">一键加密</string>

View File

@@ -14,6 +14,7 @@
<string name="title_clients">Clients</string> <string name="title_clients">Clients</string>
<string name="title_settings">Settings</string> <string name="title_settings">Settings</string>
<string name="repeater_channel">Repeater (%1$d MHz, channel %2$d)</string>
<string name="repeater_wps">WPS (insecure)</string> <string name="repeater_wps">WPS (insecure)</string>
<string name="repeater_wps_dialog_title">Enter PIN</string> <string name="repeater_wps_dialog_title">Enter PIN</string>
<string name="repeater_wps_dialog_pbc">Push Button</string> <string name="repeater_wps_dialog_pbc">Push Button</string>