From f16d34efc149b40397646eb88d464d629b0bdd0a Mon Sep 17 00:00:00 2001 From: Mygod Date: Sun, 21 Jul 2019 01:00:29 +0800 Subject: [PATCH] Allow enabling Wi-Fi for Wi-Fi direct --- .../java/be/mygod/vpnhotspot/RepeaterService.kt | 13 ++++++++++--- .../be/mygod/vpnhotspot/widget/SmartSnackbar.kt | 5 +++++ mobile/src/main/res/values-zh-rCN/strings.xml | 1 + mobile/src/main/res/values/strings.xml | 1 + 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index fdba849a..646c7222 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -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() } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/widget/SmartSnackbar.kt b/mobile/src/main/java/be/mygod/vpnhotspot/widget/SmartSnackbar.kt index a4711418..bf875ec5 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/widget/SmartSnackbar.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/widget/SmartSnackbar.kt @@ -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() { diff --git a/mobile/src/main/res/values-zh-rCN/strings.xml b/mobile/src/main/res/values-zh-rCN/strings.xml index 158486cc..1e1b4499 100644 --- a/mobile/src/main/res/values-zh-rCN/strings.xml +++ b/mobile/src/main/res/values-zh-rCN/strings.xml @@ -17,6 +17,7 @@ 删除多余 P2P 群组失败(原因:%s) Wi\u2011Fi 直连不可用,请打开 Wi\u2011Fi + 打开 创建 P2P 群组失败(原因:%s) 关闭已有 P2P 群组失败(原因:%s) 关闭 P2P 群组失败(原因:%s) diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index 2d09b436..54e3e2c5 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -26,6 +26,7 @@ Failed to remove redundant P2P group (reason: %s) Wi\u2011Fi direct unavailable, please enable Wi\u2011Fi + Enable Failed to create P2P group (reason: %s) Failed to remove P2P group (reason: %s) Failed to remove old P2P group (reason: %s)