Allow enabling Wi-Fi for Wi-Fi direct
This commit is contained in:
@@ -10,6 +10,7 @@ import android.net.wifi.p2p.*
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.provider.Settings
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.core.content.edit
|
||||
@@ -287,7 +288,8 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
|
||||
private fun doStart() {
|
||||
val listener = object : WifiP2pManager.ActionListener {
|
||||
override fun onFailure(reason: Int) {
|
||||
startFailure(formatReason(R.string.repeater_create_group_failure, reason))
|
||||
startFailure(formatReason(R.string.repeater_create_group_failure, reason),
|
||||
showWifiEnable = reason == WifiP2pManager.BUSY)
|
||||
}
|
||||
override fun onSuccess() { } // wait for WIFI_P2P_CONNECTION_CHANGED_ACTION to fire to go to step 3
|
||||
}
|
||||
@@ -368,8 +370,13 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
|
||||
status = Status.ACTIVE
|
||||
showNotification(group)
|
||||
}
|
||||
private fun startFailure(msg: CharSequence, group: WifiP2pGroup? = null) {
|
||||
SmartSnackbar.make(msg).show()
|
||||
private fun startFailure(msg: CharSequence, group: WifiP2pGroup? = null, showWifiEnable: Boolean = false) {
|
||||
SmartSnackbar.make(msg).apply {
|
||||
if (showWifiEnable) action(R.string.repeater_p2p_unavailable_enable) {
|
||||
if (Build.VERSION.SDK_INT >= 29) it.context.startActivity(Intent(Settings.Panel.ACTION_WIFI))
|
||||
else @Suppress("DEPRECATION") app.wifi.isWifiEnabled = true
|
||||
}
|
||||
}.show()
|
||||
showNotification()
|
||||
if (group != null) removeGroup() else cleanLocked()
|
||||
}
|
||||
|
||||
@@ -46,11 +46,16 @@ sealed class SmartSnackbar {
|
||||
}
|
||||
|
||||
abstract fun show()
|
||||
open fun action(@StringRes id: Int, listener: (View) -> Unit) { }
|
||||
open fun shortToast() = this
|
||||
}
|
||||
|
||||
private class SnackbarWrapper(private val snackbar: Snackbar) : SmartSnackbar() {
|
||||
override fun show() = snackbar.show()
|
||||
|
||||
override fun action(@StringRes id: Int, listener: (View) -> Unit) {
|
||||
snackbar.setAction(id, listener)
|
||||
}
|
||||
}
|
||||
|
||||
private class ToastWrapper(private val toast: Toast) : SmartSnackbar() {
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
<string name="repeater_clean_pog_failure">删除多余 P2P 群组失败(原因:%s)</string>
|
||||
|
||||
<string name="repeater_p2p_unavailable">Wi\u2011Fi 直连不可用,请打开 Wi\u2011Fi</string>
|
||||
<string name="repeater_p2p_unavailable_enable">打开</string>
|
||||
<string name="repeater_create_group_failure">创建 P2P 群组失败(原因:%s)</string>
|
||||
<string name="repeater_remove_group_failure">关闭已有 P2P 群组失败(原因:%s)</string>
|
||||
<string name="repeater_remove_old_group_failure">关闭 P2P 群组失败(原因:%s)</string>
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
<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_enable">Enable</string>
|
||||
<string name="repeater_create_group_failure">Failed to create P2P group (reason: %s)</string>
|
||||
<string name="repeater_remove_group_failure">Failed to remove P2P group (reason: %s)</string>
|
||||
<string name="repeater_remove_old_group_failure">Failed to remove old P2P group (reason: %s)</string>
|
||||
|
||||
Reference in New Issue
Block a user