Support obtaining p2p randomization setting

This commit is contained in:
Mygod
2021-05-29 20:49:51 -04:00
parent ec88c4dac2
commit ada55a1bf6
4 changed files with 27 additions and 2 deletions

View File

@@ -30,6 +30,7 @@ import be.mygod.vpnhotspot.net.MacAddressCompat
import be.mygod.vpnhotspot.net.wifi.P2pSupplicantConfiguration import be.mygod.vpnhotspot.net.wifi.P2pSupplicantConfiguration
import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat
import be.mygod.vpnhotspot.net.wifi.WifiApDialogFragment import be.mygod.vpnhotspot.net.wifi.WifiApDialogFragment
import be.mygod.vpnhotspot.net.wifi.WifiApManager
import be.mygod.vpnhotspot.util.ServiceForegroundConnector import be.mygod.vpnhotspot.util.ServiceForegroundConnector
import be.mygod.vpnhotspot.util.formatAddresses import be.mygod.vpnhotspot.util.formatAddresses
import be.mygod.vpnhotspot.util.showAllowingStateLoss import be.mygod.vpnhotspot.util.showAllowingStateLoss
@@ -197,6 +198,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
shutdownTimeoutMillis = RepeaterService.shutdownTimeoutMillis).apply { shutdownTimeoutMillis = RepeaterService.shutdownTimeoutMillis).apply {
bssid = RepeaterService.deviceAddress bssid = RepeaterService.deviceAddress
setChannel(RepeaterService.operatingChannel, RepeaterService.operatingBand) setChannel(RepeaterService.operatingChannel, RepeaterService.operatingBand)
setMacRandomizationEnabled(WifiApManager.p2pMacRandomizationSupported)
} to false } to false
} }
} else binder?.let { binder -> } else binder?.let { binder ->
@@ -206,13 +208,14 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
securityType = SoftApConfiguration.SECURITY_TYPE_WPA2_PSK, // is not actually used securityType = SoftApConfiguration.SECURITY_TYPE_WPA2_PSK, // is not actually used
isAutoShutdownEnabled = RepeaterService.isAutoShutdownEnabled, isAutoShutdownEnabled = RepeaterService.isAutoShutdownEnabled,
shutdownTimeoutMillis = RepeaterService.shutdownTimeoutMillis).run { shutdownTimeoutMillis = RepeaterService.shutdownTimeoutMillis).run {
setChannel(RepeaterService.operatingChannel)
setMacRandomizationEnabled(WifiApManager.p2pMacRandomizationSupported)
try { try {
val config = P2pSupplicantConfiguration(group) val config = P2pSupplicantConfiguration(group)
config.init(binder.obtainDeviceAddress()?.toString()) config.init(binder.obtainDeviceAddress()?.toString())
holder.config = config holder.config = config
passphrase = config.psk passphrase = config.psk
bssid = config.bssid bssid = config.bssid
setChannel(RepeaterService.operatingChannel)
this to false this to false
} catch (e: Exception) { } catch (e: Exception) {
if (e !is CancellationException) Timber.w(e) if (e !is CancellationException) Timber.w(e)

View File

@@ -46,7 +46,7 @@ class TetherTimeoutMonitor(private val timeout: Long = 0,
val info = WifiApManager.resolvedActivity.activityInfo val info = WifiApManager.resolvedActivity.activityInfo
val resources = app.packageManager.getResourcesForApplication(info.applicationInfo) val resources = app.packageManager.getResourcesForApplication(info.applicationInfo)
resources.getInteger(resources.findIdentifier("config_wifiFrameworkSoftApShutDownTimeoutMilliseconds", resources.getInteger(resources.findIdentifier("config_wifiFrameworkSoftApShutDownTimeoutMilliseconds",
"integer", "com.android.wifi.resources", info.packageName)) "integer", WifiApManager.RESOURCES_PACKAGE, info.packageName))
} }
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
Timber.w(e) Timber.w(e)

View File

@@ -343,6 +343,10 @@ data class SoftApConfigurationCompat(
channels = SparseIntArray(1).apply { put(band, channel) } channels = SparseIntArray(1).apply { put(band, channel) }
} }
fun setMacRandomizationEnabled(enabled: Boolean) {
macRandomizationSetting = if (enabled) RANDOMIZATION_PERSISTENT else RANDOMIZATION_NONE
}
/** /**
* Only single band/channel can be supplied on API 23-30 * Only single band/channel can be supplied on API 23-30
*/ */

View File

@@ -3,6 +3,7 @@ package be.mygod.vpnhotspot.net.wifi
import android.annotation.TargetApi import android.annotation.TargetApi
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.res.Resources
import android.net.wifi.SoftApConfiguration import android.net.wifi.SoftApConfiguration
import android.net.wifi.WifiManager import android.net.wifi.WifiManager
import android.os.Build import android.os.Build
@@ -15,6 +16,7 @@ import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat.Companion.toCompat
import be.mygod.vpnhotspot.util.ConstantLookup import be.mygod.vpnhotspot.util.ConstantLookup
import be.mygod.vpnhotspot.util.Services import be.mygod.vpnhotspot.util.Services
import be.mygod.vpnhotspot.util.callSuper import be.mygod.vpnhotspot.util.callSuper
import be.mygod.vpnhotspot.util.findIdentifier
import timber.log.Timber import timber.log.Timber
import java.lang.reflect.InvocationHandler import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method import java.lang.reflect.Method
@@ -27,6 +29,8 @@ object WifiApManager {
*/ */
@RequiresApi(30) @RequiresApi(30)
private const val ACTION_RESOURCES_APK = "com.android.server.wifi.intent.action.SERVICE_WIFI_RESOURCES_APK" private const val ACTION_RESOURCES_APK = "com.android.server.wifi.intent.action.SERVICE_WIFI_RESOURCES_APK"
@RequiresApi(30)
const val RESOURCES_PACKAGE = "com.android.wifi.resources"
/** /**
* Based on: https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/000ad45/service/java/com/android/server/wifi/WifiContext.java#66 * Based on: https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/000ad45/service/java/com/android/server/wifi/WifiContext.java#66
*/ */
@@ -34,6 +38,20 @@ object WifiApManager {
val resolvedActivity get() = app.packageManager.queryIntentActivities(Intent(ACTION_RESOURCES_APK), val resolvedActivity get() = app.packageManager.queryIntentActivities(Intent(ACTION_RESOURCES_APK),
PackageManager.MATCH_SYSTEM_ONLY).single() PackageManager.MATCH_SYSTEM_ONLY).single()
private const val CONFIG_P2P_MAC_RANDOMIZATION_SUPPORTED = "config_wifi_p2p_mac_randomization_supported"
val p2pMacRandomizationSupported get() = when (Build.VERSION.SDK_INT) {
29 -> Resources.getSystem().run {
getBoolean(getIdentifier(CONFIG_P2P_MAC_RANDOMIZATION_SUPPORTED, "bool", "android"))
}
in 30..Int.MAX_VALUE -> @TargetApi(30) {
val info = resolvedActivity.activityInfo
val resources = app.packageManager.getResourcesForApplication(info.applicationInfo)
resources.getBoolean(resources.findIdentifier(CONFIG_P2P_MAC_RANDOMIZATION_SUPPORTED, "bool",
RESOURCES_PACKAGE, info.packageName))
}
else -> false
}
private val getWifiApConfiguration by lazy { WifiManager::class.java.getDeclaredMethod("getWifiApConfiguration") } private val getWifiApConfiguration by lazy { WifiManager::class.java.getDeclaredMethod("getWifiApConfiguration") }
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
private val setWifiApConfiguration by lazy { private val setWifiApConfiguration by lazy {