Prevent calling private API to set channel on Q

This commit is contained in:
Mygod
2019-04-05 10:12:37 +08:00
parent 0979f6e06a
commit bb4bf783f2
3 changed files with 17 additions and 9 deletions

View File

@@ -160,14 +160,18 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere
override fun onCreate() {
super.onCreate()
onChannelDisconnected()
if (!BuildCompat.isAtLeastQ()) @Suppress("DEPRECATION") registerReceiver(deviceListener, intentFilter(
WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION,
WifiP2pManagerHelper.WIFI_P2P_PERSISTENT_GROUPS_CHANGED_ACTION))
app.pref.registerOnSharedPreferenceChangeListener(this)
if (!BuildCompat.isAtLeastQ()) @Suppress("DEPRECATION") {
registerReceiver(deviceListener, intentFilter(
WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION,
WifiP2pManagerHelper.WIFI_P2P_PERSISTENT_GROUPS_CHANGED_ACTION))
app.pref.registerOnSharedPreferenceChangeListener(this)
}
}
override fun onBind(intent: Intent) = binder
@Deprecated("No longer used since Android Q")
@Suppress("DEPRECATION")
private fun setOperatingChannel(oc: Int = operatingChannel) = try {
val channel = channel
if (channel == null) SmartSnackbar.make(R.string.repeater_failure_disconnected).show()
@@ -190,13 +194,15 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere
channel = null
if (status != Status.DESTROYED) try {
channel = p2pManager.initialize(this, Looper.getMainLooper(), this)
setOperatingChannel()
if (!BuildCompat.isAtLeastQ()) @Suppress("DEPRECATION") setOperatingChannel()
} catch (e: RuntimeException) {
Timber.w(e)
handler.postDelayed(this::onChannelDisconnected, 1000)
}
}
@Deprecated("No longer used since Android Q")
@Suppress("DEPRECATION")
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == KEY_OPERATING_CHANNEL) setOperatingChannel()
}

View File

@@ -25,6 +25,7 @@ object WifiP2pManagerHelper {
WifiP2pManager::class.java.getDeclaredMethod("setWifiP2pChannels", WifiP2pManager.Channel::class.java,
Int::class.java, Int::class.java, WifiP2pManager.ActionListener::class.java)
}
@Deprecated("No longer used since Android Q")
fun WifiP2pManager.setWifiP2pChannels(c: WifiP2pManager.Channel, lc: Int, oc: Int,
listener: WifiP2pManager.ActionListener) {
try {
@@ -112,5 +113,6 @@ object WifiP2pManagerHelper {
* Source: https://android.googlesource.com/platform/frameworks/base/+/android-4.2_r1/wifi/java/android/net/wifi/p2p/WifiP2pGroup.java#253
*/
private val getNetworkId by lazy { WifiP2pGroup::class.java.getDeclaredMethod("getNetworkId") }
@Deprecated("No longer used since Android Q")
val WifiP2pGroup.netId get() = getNetworkId.invoke(this) as Int
}