From f75b522d82d9fc819d8de0cf7fe76f3377041404 Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 14 Mar 2019 19:15:33 +0800 Subject: [PATCH 01/64] Fix temp hotspot on Android Q beta --- mobile/src/main/AndroidManifest.xml | 2 ++ .../vpnhotspot/manage/LocalOnlyHotspotManager.kt | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/mobile/src/main/AndroidManifest.xml b/mobile/src/main/AndroidManifest.xml index 293052ae..76d87ee1 100644 --- a/mobile/src/main/AndroidManifest.xml +++ b/mobile/src/main/AndroidManifest.xml @@ -39,6 +39,8 @@ + + Date: Fri, 15 Mar 2019 17:59:30 +0800 Subject: [PATCH 02/64] Update parseNumericAddress on Android Q --- mobile/build.gradle | 3 +-- mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/mobile/build.gradle b/mobile/build.gradle index c5b65bd0..355c8930 100644 --- a/mobile/build.gradle +++ b/mobile/build.gradle @@ -9,8 +9,7 @@ if (!getGradle().getStartParameter().getTaskRequests().toString().contains("Fdro } android { - buildToolsVersion "28.0.3" - compileSdkVersion 28 + compileSdkVersion 'android-Q' compileOptions { sourceCompatibility 1.8 targetCompatibility 1.8 diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt index b3365485..563b4adf 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -1,6 +1,7 @@ package be.mygod.vpnhotspot.util import android.content.* +import android.net.InetAddresses import android.os.Build import android.text.Spannable import android.text.SpannableString @@ -9,6 +10,7 @@ import android.view.View import android.widget.ImageView import androidx.annotation.DrawableRes import androidx.core.net.toUri +import androidx.core.os.BuildCompat import androidx.core.view.isVisible import androidx.databinding.BindingAdapter import be.mygod.vpnhotspot.App.Companion.app @@ -76,7 +78,8 @@ private val parseNumericAddress by lazy { isAccessible = true } } -fun parseNumericAddress(address: String) = parseNumericAddress.invoke(null, address) as InetAddress +fun parseNumericAddress(address: String) = if (BuildCompat.isAtLeastQ()) + InetAddresses.parseNumericAddress(address) else parseNumericAddress.invoke(null, address) as InetAddress fun Context.launchUrl(url: String) { if (app.hasTouch) try { From da9d64733903fdd9e6324fa1a840fa1dc2fa1513 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 16 Mar 2019 16:30:31 +0800 Subject: [PATCH 03/64] Fix WifiP2p permissions --- .../vpnhotspot/manage/RepeaterManager.kt | 9 +++++++ .../vpnhotspot/manage/TetheringFragment.kt | 26 ++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt index 00050db7..3501a041 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt @@ -1,9 +1,11 @@ package be.mygod.vpnhotspot.manage +import android.Manifest import android.content.ComponentName import android.content.DialogInterface import android.content.Intent import android.content.ServiceConnection +import android.content.pm.PackageManager import android.net.wifi.WifiConfiguration import android.net.wifi.p2p.WifiP2pGroup import android.os.Bundle @@ -14,6 +16,7 @@ import android.view.WindowManager import android.widget.EditText import androidx.appcompat.app.AlertDialog import androidx.core.content.ContextCompat +import androidx.core.os.BuildCompat import androidx.databinding.BaseObservable import androidx.databinding.Bindable import androidx.lifecycle.ViewModel @@ -82,6 +85,12 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic when (binder?.service?.status) { RepeaterService.Status.IDLE -> { val context = parent.requireContext() + if (BuildCompat.isAtLeastQ() && context.checkSelfPermission( + Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + parent.requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), + TetheringFragment.START_REPEATER) + return + } ContextCompat.startForegroundService(context, Intent(context, RepeaterService::class.java)) } RepeaterService.Status.ACTIVE -> binder.shutdown() diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt index 9f859cd7..e2b22373 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt @@ -9,7 +9,10 @@ import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.os.IBinder -import android.view.* +import android.view.LayoutInflater +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup import androidx.core.content.ContextCompat import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment @@ -17,7 +20,10 @@ import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView -import be.mygod.vpnhotspot.* +import be.mygod.vpnhotspot.LocalOnlyHotspotService +import be.mygod.vpnhotspot.R +import be.mygod.vpnhotspot.RepeaterService +import be.mygod.vpnhotspot.TetheringService import be.mygod.vpnhotspot.databinding.FragmentTetheringBinding import be.mygod.vpnhotspot.net.TetherType import be.mygod.vpnhotspot.net.TetheringManager @@ -32,6 +38,7 @@ import java.net.SocketException class TetheringFragment : Fragment(), ServiceConnection, MenuItem.OnMenuItemClickListener { companion object { + const val START_REPEATER = 4 const val START_LOCAL_ONLY_HOTSPOT = 1 const val REPEATER_EDIT_CONFIGURATION = 2 const val REPEATER_WPS = 3 @@ -138,12 +145,19 @@ class TetheringFragment : Fragment(), ServiceConnection, MenuItem.OnMenuItemClic } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { - if (requestCode == START_LOCAL_ONLY_HOTSPOT) @TargetApi(26) { - if (grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) { + when (requestCode) { + START_REPEATER -> if (grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) @TargetApi(29) { val context = requireContext() - context.startForegroundService(Intent(context, LocalOnlyHotspotService::class.java)) + context.startForegroundService(Intent(context, RepeaterService::class.java)) } - } else super.onRequestPermissionsResult(requestCode, permissions, grantResults) + START_LOCAL_ONLY_HOTSPOT -> { + if (grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED) @TargetApi(26) { + val context = requireContext() + context.startForegroundService(Intent(context, LocalOnlyHotspotService::class.java)) + } + } + else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults) + } } override fun onServiceConnected(name: ComponentName?, service: IBinder?) { From 841ee5ce9ddf810d5cc8df2b267bc837402c0f44 Mon Sep 17 00:00:00 2001 From: Mygod Date: Wed, 20 Mar 2019 11:42:05 +0800 Subject: [PATCH 04/64] Support new Wi-Fi locks in Android Q --- README.md | 16 +++++- .../java/be/mygod/vpnhotspot/MainActivity.kt | 2 + .../vpnhotspot/SettingsPreferenceFragment.kt | 2 + .../vpnhotspot/net/wifi/WifiDoubleLock.kt | 55 +++++++++++++++++-- mobile/src/main/res/values-v29/arrays.xml | 13 +++++ mobile/src/main/res/values/strings.xml | 2 + mobile/src/main/res/xml/pref_settings.xml | 1 - 7 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 mobile/src/main/res/values-v29/arrays.xml diff --git a/README.md b/README.md index c4e45603..7aadba8f 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,20 @@ Default settings are picked to suit general use cases and maximize compatibility * Keep Wi-Fi alive: Acquire Wi-Fi locks when repeater, temporary hotspot or system VPN hotspot is activated. - Choose "System default" to save battery life; - - Choose "On" (default) if repeater/hotspot turns itself off automatically or stops working after a while; - - Choose "High Performance Mode" to minimize packet loss and latency (will consume more power). + - (up to Android 9) Choose "On" (default) if repeater/hotspot turns itself off automatically or stops working after a while; + - (up to Android 9) Choose "High Performance Mode" to minimize packet loss and latency (will consume more power); + - (since Android Q) Choose "Disable power save" to decrease packet latency. + An example use case is when a voice connection needs to be kept active even after the device screen goes off. + Using this mode may improve the call quality. + Requires support from the hardware. + - (since Android Q) Choose "Low latency mode" to optimize for reduced packet latency, and this might result in: + 1. Reduced battery life. + 2. Reduced throughput. + 3. Reduced frequency of Wi-Fi scanning. + This may cause the device not roaming or switching to the AP with highest signal quality, and location accuracy may be reduced. + Example use cases are real time gaming or virtual reality applications where low latency is a key factor for user experience. + Requires support from the hardware. + Note: Requires this app running in foreground with screen on. * Start repeater on boot: Self explanatory. * Network status monitor mode: This option controls how the app monitors connected devices as well as interface changes (when custom upstream is used). diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt b/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt index 4b859996..4914c620 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt @@ -15,6 +15,7 @@ import be.mygod.vpnhotspot.client.ClientViewModel import be.mygod.vpnhotspot.client.ClientsFragment import be.mygod.vpnhotspot.databinding.ActivityMainBinding import be.mygod.vpnhotspot.manage.TetheringFragment +import be.mygod.vpnhotspot.net.wifi.WifiDoubleLock import be.mygod.vpnhotspot.util.ServiceForegroundConnector import be.mygod.vpnhotspot.widget.SmartSnackbar import com.google.android.material.bottomnavigation.BottomNavigationMenuView @@ -41,6 +42,7 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS if (RepeaterService.supported) ServiceForegroundConnector(this, model, RepeaterService::class) model.clients.observe(this, Observer { badge.badgeNumber = it.size }) SmartSnackbar.Register(lifecycle, binding.fragmentHolder) + WifiDoubleLock.ActivityListener(this) } override fun onNavigationItemSelected(item: MenuItem) = when (item.itemId) { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt index 5a4faf26..e1b5986f 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt @@ -12,6 +12,7 @@ import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.net.Routing.Companion.IPTABLES import be.mygod.vpnhotspot.net.monitor.IpMonitor import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor +import be.mygod.vpnhotspot.net.wifi.WifiDoubleLock import be.mygod.vpnhotspot.preference.AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat import be.mygod.vpnhotspot.preference.SharedPreferenceDataStore import be.mygod.vpnhotspot.util.RootSession @@ -26,6 +27,7 @@ import java.net.SocketException class SettingsPreferenceFragment : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + WifiDoubleLock.mode = WifiDoubleLock.mode // handle complicated default value and possible system upgrades preferenceManager.preferenceDataStore = SharedPreferenceDataStore(app.pref) RoutingManager.masqueradeMode = RoutingManager.masqueradeMode // flush default value addPreferencesFromResource(R.xml.pref_settings) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiDoubleLock.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiDoubleLock.kt index 45261e1b..7032d710 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiDoubleLock.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiDoubleLock.kt @@ -4,7 +4,15 @@ import android.annotation.SuppressLint import android.content.SharedPreferences import android.net.wifi.WifiManager import android.os.PowerManager +import android.view.WindowManager +import androidx.activity.ComponentActivity +import androidx.annotation.RequiresApi +import androidx.core.content.edit import androidx.core.content.getSystemService +import androidx.core.os.BuildCompat +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.OnLifecycleEvent import be.mygod.vpnhotspot.App.Companion.app /** @@ -13,8 +21,12 @@ import be.mygod.vpnhotspot.App.Companion.app class WifiDoubleLock(lockType: Int) : AutoCloseable { companion object : SharedPreferences.OnSharedPreferenceChangeListener { private const val KEY = "service.wifiLock" - private val lockType get() = - WifiDoubleLock.Mode.valueOf(app.pref.getString(KEY, WifiDoubleLock.Mode.Full.toString()) ?: "").lockType + var mode: Mode + @Suppress("DEPRECATION") + get() = Mode.valueOf(app.pref.getString(KEY, Mode.Full.toString()) ?: "").let { + if (it == Mode.Full && BuildCompat.isAtLeastQ()) Mode.None else it + } + set(value) = app.pref.edit { putString(KEY, value.toString()) } private val service by lazy { app.getSystemService()!! } private var holders = mutableSetOf() @@ -23,7 +35,7 @@ class WifiDoubleLock(lockType: Int) : AutoCloseable { fun acquire(holder: Any) = synchronized(this) { if (holders.isEmpty()) { app.pref.registerOnSharedPreferenceChangeListener(this) - val lockType = lockType + val lockType = mode.lockType if (lockType != null) lock = WifiDoubleLock(lockType) } check(holders.add(holder)) @@ -40,14 +52,45 @@ class WifiDoubleLock(lockType: Int) : AutoCloseable { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { if (key == KEY) synchronized(this) { lock?.close() - val lockType = lockType + val lockType = mode.lockType lock = if (lockType == null) null else WifiDoubleLock(lockType) } } } - enum class Mode(val lockType: Int? = null) { - None, Full(WifiManager.WIFI_MODE_FULL), HighPerf(WifiManager.WIFI_MODE_FULL_HIGH_PERF) + enum class Mode(val lockType: Int? = null, val keepScreenOn: Boolean = false) { + None, + @Suppress("DEPRECATION") + @Deprecated("This constant was deprecated in API level Q.\n" + + "This API is non-functional and will have no impact.") + Full(WifiManager.WIFI_MODE_FULL), + HighPerf(WifiManager.WIFI_MODE_FULL_HIGH_PERF), + @RequiresApi(29) + LowLatency(WifiManager.WIFI_MODE_FULL_LOW_LATENCY, true), + } + + class ActivityListener(val activity: ComponentActivity) : + LifecycleObserver, SharedPreferences.OnSharedPreferenceChangeListener { + private var keepScreenOn: Boolean = false + set(value) { + if (field == value) return + field = value + if (value) activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + else activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + } + + init { + activity.lifecycle.addObserver(this) + app.pref.registerOnSharedPreferenceChangeListener(this) + keepScreenOn = mode.keepScreenOn + } + + override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { + if (key == KEY) keepScreenOn = mode.keepScreenOn + } + + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) + fun shutdown() = app.pref.unregisterOnSharedPreferenceChangeListener(this) } private val wifi = app.wifi.createWifiLock(lockType, "vpnhotspot:wifi").apply { acquire() } diff --git a/mobile/src/main/res/values-v29/arrays.xml b/mobile/src/main/res/values-v29/arrays.xml new file mode 100644 index 00000000..30824b47 --- /dev/null +++ b/mobile/src/main/res/values-v29/arrays.xml @@ -0,0 +1,13 @@ + + + + @string/settings_service_wifi_lock_none + @string/settings_service_wifi_lock_high_perf_v29 + @string/settings_service_wifi_lock_low_latency + + + None + HighPerf + LowLatency + + diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index 70098cbb..6b8a73b8 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -101,6 +101,8 @@ System default On High Performance Mode + Disable power save + Low latency mode Network status monitor mode Netlink monitor Netlink monitor with root diff --git a/mobile/src/main/res/xml/pref_settings.xml b/mobile/src/main/res/xml/pref_settings.xml index d78bea81..1edeb6b2 100644 --- a/mobile/src/main/res/xml/pref_settings.xml +++ b/mobile/src/main/res/xml/pref_settings.xml @@ -41,7 +41,6 @@ app:icon="@drawable/ic_device_wifi_lock" app:entries="@array/settings_service_wifi_lock" app:entryValues="@array/settings_service_wifi_lock_values" - app:defaultValue="Full" app:title="@string/settings_service_wifi_lock" app:useSimpleSummaryProvider="true"/> Date: Wed, 20 Mar 2019 11:53:04 +0800 Subject: [PATCH 05/64] Handle API changes --- mobile/src/main/java/be/mygod/vpnhotspot/App.kt | 7 ++++--- .../src/main/java/be/mygod/vpnhotspot/RepeaterService.kt | 4 ++-- .../src/main/java/be/mygod/vpnhotspot/TetheringService.kt | 4 ++-- .../src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt | 4 ++-- .../java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt | 2 ++ .../main/java/be/mygod/vpnhotspot/util/SpanFormatter.kt | 2 +- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt index ba743039..0e0eaa55 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/App.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/App.kt @@ -7,13 +7,13 @@ import android.content.res.Configuration import android.net.ConnectivityManager import android.net.wifi.WifiManager import android.os.Build -import android.preference.PreferenceManager import androidx.browser.customtabs.CustomTabsIntent import androidx.core.content.ContextCompat import androidx.core.content.getSystemService import androidx.core.provider.FontRequest import androidx.emoji.text.EmojiCompat import androidx.emoji.text.FontRequestEmojiCompatConfig +import androidx.preference.PreferenceManager import be.mygod.vpnhotspot.net.DhcpWorkaround import be.mygod.vpnhotspot.room.AppDatabase import be.mygod.vpnhotspot.util.DeviceStorageApp @@ -32,7 +32,8 @@ class App : Application() { app = this if (Build.VERSION.SDK_INT >= 24) { deviceStorage = DeviceStorageApp(this) - deviceStorage.moveSharedPreferencesFrom(this, PreferenceManager.getDefaultSharedPreferencesName(this)) + // alternative to PreferenceManager.getDefaultSharedPreferencesName(this) + deviceStorage.moveSharedPreferencesFrom(this, PreferenceManager(this).sharedPreferencesName) deviceStorage.moveDatabaseFrom(this, AppDatabase.DB_NAME) } else deviceStorage = this DebugHelper.init() @@ -51,7 +52,7 @@ class App : Application() { if (DhcpWorkaround.shouldEnable) DhcpWorkaround.enable(true) } - override fun onConfigurationChanged(newConfig: Configuration?) { + override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) ServiceNotification.updateNotificationChannels() } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 69fb257d..9d614aa6 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -120,9 +120,9 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere if (intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, 0) == WifiP2pManager.WIFI_P2P_STATE_DISABLED) clean() // ignore P2P enabled WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION -> onP2pConnectionChanged( - intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO), + intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO)!!, intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO), - intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP)) + intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_GROUP)!!) } } private val deviceListener = broadcastReceiver { _, intent -> diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt index 79af6088..33bbffcc 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt @@ -90,7 +90,7 @@ class TetheringService : IpNeighbourMonitoringService() { if (start()) check(downstreams.put(iface, this) == null) else destroy() } } - intent.getStringExtra(EXTRA_ADD_INTERFACE_MONITOR)?.let { iface -> + intent.getStringExtra(EXTRA_ADD_INTERFACE_MONITOR)?.also { iface -> val downstream = downstreams[iface] if (downstream == null) Downstream(this, iface, true).apply { start() @@ -98,7 +98,7 @@ class TetheringService : IpNeighbourMonitoringService() { downstreams[iface] = this } else downstream.monitor = true } - downstreams.remove(intent.getStringExtra(EXTRA_REMOVE_INTERFACE))?.destroy() + intent.getStringExtra(EXTRA_REMOVE_INTERFACE)?.also { downstreams.remove(it)?.destroy() } updateNotification() // call this first just in case we are shutting down immediately onDownstreamsChangedLocked() } else if (downstreams.isEmpty()) stopSelf(startId) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt index 5f7ed771..4a89e2b0 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt @@ -47,7 +47,7 @@ object MacLookup { try { val response = conn.inputStream.bufferedReader().readText() val obj = JSONObject(response).getJSONObject("result") - obj.optString("error", null)?.also { throw UnexpectedError(mac, it) } + obj.opt("error")?.also { throw UnexpectedError(mac, it.toString()) } val company = obj.getString("company") val match = extractCountry(mac, response, obj) val result = if (match != null) { @@ -71,7 +71,7 @@ object MacLookup { } private fun extractCountry(mac: Long, response: String, obj: JSONObject): MatchResult? { - obj.optString("country")?.let { countryCodeRegex.matchEntire(it) }?.also { return it } + countryCodeRegex.matchEntire(obj.optString("country"))?.also { return it } val address = obj.optString("address") if (address.isNullOrBlank()) return null countryCodeRegex.find(address)?.also { return it } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt index 653d3f78..c4fbd5ec 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt @@ -25,11 +25,13 @@ object WifiApManager { private fun WifiManager.setWifiApEnabled(wifiConfig: WifiConfiguration?, enabled: Boolean) = setWifiApEnabled.invoke(this, wifiConfig, enabled) as Boolean + @Suppress("DEPRECATION") @Deprecated("Not usable since API 26, malfunctioning on API 25") fun start(wifiConfig: WifiConfiguration? = null) { app.wifi.isWifiEnabled = false app.wifi.setWifiApEnabled(wifiConfig, true) } + @Suppress("DEPRECATION") @Deprecated("Not usable since API 26") fun stop() { app.wifi.setWifiApEnabled(null, false) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/SpanFormatter.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/SpanFormatter.kt index 8ed8a93a..e9fa604f 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/SpanFormatter.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/SpanFormatter.kt @@ -57,7 +57,7 @@ object SpanFormatter { i = m.start() val exprEnd = m.end() - val argTerm = m.group(1) + val argTerm = m.group(1)!! val modTerm = m.group(2) val typeTerm = m.group(3) From ef32866c6624e53684bb81335e871e18ad48472c Mon Sep 17 00:00:00 2001 From: Mygod Date: Wed, 20 Mar 2019 12:03:04 +0800 Subject: [PATCH 06/64] Update Gradle for Android Q --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 2ce3be2c..ac2db8c8 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ buildscript { } } dependencies { - classpath "com.android.tools.build:gradle:3.3.2" + classpath "com.android.tools.build:gradle:3.4.0-rc02" classpath 'com.github.ben-manes:gradle-versions-plugin:0.21.0' classpath 'com.google.gms:google-services:4.2.0' classpath 'io.fabric.tools:gradle:1.28.0' From 673ff9800aab7b761cdd05e480787c313c10db6a Mon Sep 17 00:00:00 2001 From: Mygod Date: Wed, 20 Mar 2019 12:21:22 +0800 Subject: [PATCH 07/64] Refine code style --- detekt.yml | 2 +- .../be/mygod/vpnhotspot/RepeaterService.kt | 47 +++++++++++-------- .../be/mygod/vpnhotspot/manage/Manager.kt | 4 +- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/detekt.yml b/detekt.yml index 305b961d..7c7e4559 100644 --- a/detekt.yml +++ b/detekt.yml @@ -363,7 +363,7 @@ style: OptionalUnit: active: true OptionalWhenBraces: - active: true + active: false PreferToOverPairSyntax: active: false ProtectedMemberInFinalClass: diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 9d614aa6..bf3787d0 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -22,10 +22,7 @@ import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.netId import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.requestPersistentGroupInfo import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.setWifiP2pChannels import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.startWps -import be.mygod.vpnhotspot.util.StickyEvent0 -import be.mygod.vpnhotspot.util.StickyEvent1 -import be.mygod.vpnhotspot.util.broadcastReceiver -import be.mygod.vpnhotspot.util.intentFilter +import be.mygod.vpnhotspot.util.* import be.mygod.vpnhotspot.widget.SmartSnackbar import timber.log.Timber import java.lang.reflect.InvocationTargetException @@ -233,29 +230,41 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere registerReceiver(receiver, intentFilter(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION, WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)) receiverRegistered = true - p2pManager.requestGroupInfo(channel) { - when { - it == null -> doStart() - it.isGroupOwner -> if (routingManager == null) doStart(it) - else -> { - Timber.i("Removing old group ($it)") - p2pManager.removeGroup(channel, object : WifiP2pManager.ActionListener { - override fun onSuccess() = doStart() - override fun onFailure(reason: Int) = - startFailure(formatReason(R.string.repeater_remove_old_group_failure, reason)) - }) + try { + p2pManager.requestGroupInfo(channel) { + when { + it == null -> doStart() + it.isGroupOwner -> if (routingManager == null) doStart(it) + else -> { + Timber.i("Removing old group ($it)") + p2pManager.removeGroup(channel, object : WifiP2pManager.ActionListener { + override fun onSuccess() = doStart() + override fun onFailure(reason: Int) = + startFailure(formatReason(R.string.repeater_remove_old_group_failure, reason)) + }) + } } } + } catch (e: SecurityException) { + Timber.w(e) + startFailure(e.readableMessage) } return START_NOT_STICKY } /** * startService Step 2 (if a group isn't already available) */ - private fun doStart() = p2pManager.createGroup(channel, object : WifiP2pManager.ActionListener { - override fun onFailure(reason: Int) = startFailure(formatReason(R.string.repeater_create_group_failure, reason)) - override fun onSuccess() { } // wait for WIFI_P2P_CONNECTION_CHANGED_ACTION to fire to go to step 3 - }) + private fun doStart() = try { + p2pManager.createGroup(channel, object : WifiP2pManager.ActionListener { + override fun onFailure(reason: Int) { + startFailure(formatReason(R.string.repeater_create_group_failure, reason)) + } + override fun onSuccess() { } // wait for WIFI_P2P_CONNECTION_CHANGED_ACTION to fire to go to step 3 + }) + } catch (e: SecurityException) { + Timber.w(e) + startFailure(e.readableMessage) + } /** * Used during step 2, also called when connection changed */ diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/Manager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/Manager.kt index 495d44d4..6dc70cc8 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/Manager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/Manager.kt @@ -1,5 +1,6 @@ package be.mygod.vpnhotspot.manage +import android.annotation.SuppressLint import android.annotation.TargetApi import android.view.LayoutInflater import android.view.ViewGroup @@ -21,7 +22,8 @@ abstract class Manager { const val VIEW_TYPE_REPEATER = 7 override fun areItemsTheSame(oldItem: Manager, newItem: Manager) = oldItem.isSameItemAs(newItem) - override fun areContentsTheSame(oldItem: Manager, newItem: Manager) = oldItem == newItem + @SuppressLint("DiffUtilEquals") + override fun areContentsTheSame(oldItem: Manager, newItem: Manager) = oldItem === newItem fun createViewHolder(inflater: LayoutInflater, parent: ViewGroup, type: Int) = when (type) { VIEW_TYPE_INTERFACE -> From bb41536eb12ede451857bc0d47a0943e24e440be Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 4 Apr 2019 11:31:27 +0800 Subject: [PATCH 08/64] Update to Android Q beta 2 APIs --- build.gradle | 2 +- .../java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt | 2 +- .../src/main/java/be/mygod/vpnhotspot/TetheringService.kt | 2 +- .../java/be/mygod/vpnhotspot/client/ClientViewModel.kt | 3 ++- .../mygod/vpnhotspot/manage/TetherListeningTileService.kt | 7 ++++--- .../java/be/mygod/vpnhotspot/manage/TetheringFragment.kt | 5 +++-- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index e3cfcaba..2b2cacd9 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ buildscript { } } dependencies { - classpath "com.android.tools.build:gradle:3.4.0-rc02" + classpath 'com.android.tools.build:gradle:3.4.0-rc03' classpath 'com.github.ben-manes:gradle-versions-plugin:0.21.0' classpath 'com.google.gms:google-services:4.2.0' classpath 'io.fabric.tools:gradle:1.28.1' diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt index fe7cb571..2ec37e78 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/LocalOnlyHotspotService.kt @@ -41,7 +41,7 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService() { private var routingManager: RoutingManager? = null private var receiverRegistered = false private val receiver = broadcastReceiver { _, intent -> - val ifaces = intent.localOnlyTetheredIfaces + val ifaces = intent.localOnlyTetheredIfaces ?: return@broadcastReceiver DebugHelper.log(TAG, "onTetherStateChangedLocked: $ifaces") check(ifaces.size <= 1) val iface = ifaces.singleOrNull() diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt index 33bbffcc..b803cd4f 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/TetheringService.kt @@ -48,7 +48,7 @@ class TetheringService : IpNeighbourMonitoringService() { private val receiver = broadcastReceiver { _, intent -> synchronized(downstreams) { val toRemove = downstreams.toMutableMap() // make a copy - for (iface in intent.tetheredIfaces) { + for (iface in intent.tetheredIfaces ?: return@synchronized) { val downstream = toRemove.remove(iface) ?: continue if (downstream.monitor) downstream.start() } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientViewModel.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientViewModel.kt index 7983ea25..09c7c479 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientViewModel.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/ClientViewModel.kt @@ -20,7 +20,8 @@ import be.mygod.vpnhotspot.util.broadcastReceiver class ClientViewModel : ViewModel(), ServiceConnection, IpNeighbourMonitor.Callback { private var tetheredInterfaces = emptySet() private val receiver = broadcastReceiver { _, intent -> - tetheredInterfaces = intent.tetheredIfaces.toSet() + intent.localOnlyTetheredIfaces + tetheredInterfaces = (intent.tetheredIfaces ?: return@broadcastReceiver).toSet() + + (intent.localOnlyTetheredIfaces ?: return@broadcastReceiver) populateClients() } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherListeningTileService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherListeningTileService.kt index 0917b7e0..7c29933f 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherListeningTileService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherListeningTileService.kt @@ -12,14 +12,15 @@ abstract class TetherListeningTileService : KillableTileService() { protected var tethered: List = emptyList() private val receiver = broadcastReceiver { _, intent -> - tethered = intent.tetheredIfaces + tethered = intent.tetheredIfaces ?: return@broadcastReceiver updateTile() } override fun onStartListening() { super.onStartListening() - val intent = registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED)) - if (intent != null) tethered = intent.tetheredIfaces + registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))?.tetheredIfaces?.let { + tethered = it + } } override fun onStopListening() { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt index 13ca8d05..1b616cea 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt @@ -98,8 +98,9 @@ class TetheringFragment : Fragment(), ServiceConnection, MenuItem.OnMenuItemClic var binder: TetheringService.Binder? = null private val adapter = ManagerAdapter() private val receiver = broadcastReceiver { _, intent -> - adapter.update(intent.tetheredIfaces, intent.localOnlyTetheredIfaces, - intent.getStringArrayListExtra(TetheringManager.EXTRA_ERRORED_TETHER)) + adapter.update(intent.tetheredIfaces ?: return@broadcastReceiver, + intent.localOnlyTetheredIfaces ?: return@broadcastReceiver, + intent.getStringArrayListExtra(TetheringManager.EXTRA_ERRORED_TETHER) ?: return@broadcastReceiver) } private fun updateMonitorList(canMonitor: List = emptyList()) { From 834498b1ff8186d6c040d0961846bdbec0718743 Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 4 Apr 2019 13:08:00 +0800 Subject: [PATCH 09/64] Deprecate reset credentials Due to persistent groups no longer useful in Android Q, this feature will be removed. Users on older releases can remove remembered groups from Wi-Fi Direct settings manually. --- .../be/mygod/vpnhotspot/RepeaterService.kt | 12 ----------- .../vpnhotspot/manage/RepeaterManager.kt | 21 ++++++++----------- .../vpnhotspot/manage/TetheringFragment.kt | 2 +- .../net/wifi/WifiP2pDialogFragment.kt | 1 - mobile/src/main/res/values-zh-rCN/strings.xml | 3 --- mobile/src/main/res/values/strings.xml | 3 --- 6 files changed, 10 insertions(+), 32 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 69fb257d..ec88da98 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -95,18 +95,6 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere fun shutdown() { if (active) removeGroup() } - - fun resetCredentials() { - val channel = channel - if (channel == null) SmartSnackbar.make(R.string.repeater_failure_disconnected).show() - else p2pManager.deletePersistentGroup(channel, (group ?: return).netId, - object : WifiP2pManager.ActionListener { - override fun onSuccess() = SmartSnackbar.make(R.string.repeater_reset_credentials_success) - .shortToast().show() - override fun onFailure(reason: Int) = SmartSnackbar.make( - formatReason(R.string.repeater_reset_credentials_failure, reason)).show() - }) - } } private val p2pManager get() = RepeaterService.p2pManager!! diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt index 00050db7..c6313cfb 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt @@ -168,18 +168,15 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic } } - fun onEditResult(which: Int, data: Intent?) { - when (which) { - DialogInterface.BUTTON_POSITIVE -> try { - val master = holder.config ?: return - val config = AlertDialogFragment.getRet(data!!).configuration - master.update(config.SSID, config.preSharedKey) - binder!!.group = null - } catch (e: Exception) { - Timber.w(e) - SmartSnackbar.make(e).show() - } - DialogInterface.BUTTON_NEUTRAL -> binder!!.resetCredentials() + fun onEditResult(data: Intent?) { + val master = holder.config ?: return + try { + val config = AlertDialogFragment.getRet(data!!).configuration + master.update(config.SSID, config.preSharedKey) + binder!!.group = null + } catch (e: Exception) { + Timber.w(e) + SmartSnackbar.make(e).show() } holder.config = null } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt index a4911964..8fd43896 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt @@ -133,7 +133,7 @@ class TetheringFragment : Fragment(), ServiceConnection, MenuItem.OnMenuItemClic override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) = when (requestCode) { REPEATER_WPS -> adapter.repeaterManager.onWpsResult(resultCode, data) - REPEATER_EDIT_CONFIGURATION -> adapter.repeaterManager.onEditResult(resultCode, data) + REPEATER_EDIT_CONFIGURATION -> adapter.repeaterManager.onEditResult(data) else -> super.onActivityResult(requestCode, resultCode, data) } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt index f430baf3..2c447ede 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt @@ -48,7 +48,6 @@ class WifiP2pDialogFragment : AlertDialogFragment打开 WPS 失败(原因:%s) 设置 WLAN 中继 未能找到有效的档案。请尝试先打开中继。 - 重置 - 凭据已重置。 - 重置凭据失败(原因:%s) 删除多余 P2P 群组失败(原因:%s) Wi\u2011Fi 直连不可用,请打开 Wi\u2011Fi diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index ee7c4a8a..213f4fe6 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -24,9 +24,6 @@ Failed to start WPS (reason: %s) Configure Wi\u2011Fi repeater Valid config not found. Please start repeater first. - Reset - Credentials reset. - Failed to reset credentials (reason: %s) Failed to remove redundant P2P group (reason: %s) Wi\u2011Fi direct unavailable, please enable Wi\u2011Fi From 1145b0f23b1e777f3eef0d5b33215e2aff2aaf48 Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 4 Apr 2019 16:43:48 +0800 Subject: [PATCH 10/64] Support editing native Wi-Fi AP configurations Support for repeater channel on Android 5 has been dropped because I am lazy. --- README.md | 4 +- mobile/src/main/AndroidManifest.xml | 2 + .../be/mygod/vpnhotspot/RepeaterService.kt | 10 +- .../manage/LocalOnlyHotspotManager.kt | 17 +- .../vpnhotspot/manage/RepeaterManager.kt | 66 ++++---- .../vpnhotspot/manage/TetheringFragment.kt | 93 ++++++++--- .../vpnhotspot/net/wifi/WifiApManager.kt | 27 +++- .../net/wifi/WifiP2pDialogFragment.kt | 74 --------- .../P2pSupplicantConfiguration.kt | 2 +- .../configuration/WifiApDialogFragment.kt | 149 ++++++++++++++++++ .../wifi/configuration/WifiConfiguration.kt | 84 ++++++++++ .../be/mygod/vpnhotspot/room/Converters.kt | 26 +-- .../java/be/mygod/vpnhotspot/util/Utils.kt | 12 +- .../src/main/res/drawable/ic_content_wave.xml | 11 -- mobile/src/main/res/layout/dialog_wifi_ap.xml | 42 ++++- .../src/main/res/layout/listitem_repeater.xml | 93 ----------- .../src/main/res/menu/toolbar_tethering.xml | 14 ++ mobile/src/main/res/values-ru/strings.xml | 18 ++- mobile/src/main/res/values-zh-rCN/strings.xml | 13 +- mobile/src/main/res/values/strings.xml | 14 +- mobile/src/main/res/values/styles.xml | 12 ++ 21 files changed, 488 insertions(+), 295 deletions(-) delete mode 100644 mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt rename mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/{ => configuration}/P2pSupplicantConfiguration.kt (99%) create mode 100644 mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiApDialogFragment.kt create mode 100644 mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiConfiguration.kt delete mode 100644 mobile/src/main/res/drawable/ic_content_wave.xml diff --git a/README.md b/README.md index a715049c..d5c49fcb 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,9 @@ Undocumented API list: * (since API 24) [`Landroid/net/ConnectivityManager;->getLastTetherError(Ljava/lang/String;)I,greylist`](https://android.googlesource.com/platform/prebuilts/runtime/+/aa21a6e/appcompat/hiddenapi-flags.csv#112882) * (since API 24) [`Landroid/net/ConnectivityManager;->startTethering(IZLandroid/net/ConnectivityManager$OnStartTetheringCallback;Landroid/os/Handler;)V,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/aa21a6e/appcompat/hiddenapi-flags.csv#112972) * (since API 24) [`Landroid/net/ConnectivityManager;->stopTethering(I)V,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/aa21a6e/appcompat/hiddenapi-flags.csv#112974) +* [`Landroid/net/wifi/WifiManager;->getWifiApConfiguration()Landroid/net/wifi/WifiConfiguration;,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/aa21a6e/appcompat/hiddenapi-flags.csv#121357) +* [`Landroid/net/wifi/WifiManager;->setWifiApConfiguration(Landroid/net/wifi/WifiConfiguration;)Z,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/aa21a6e/appcompat/hiddenapi-flags.csv#121416) +* (deprecated since API 26) `Landroid/net/wifi/WifiManager;->setWifiApEnabled(Landroid/net/wifi/WifiConfiguration;Z)Z` * [`Landroid/net/wifi/p2p/WifiP2pGroup;->getNetworkId()I,greylist`](https://android.googlesource.com/platform/prebuilts/runtime/+/aa21a6e/appcompat/hiddenapi-flags.csv#123194) * [`Landroid/net/wifi/p2p/WifiP2pGroupList;->getGroupList()Ljava/util/Collection;,greylist`](https://android.googlesource.com/platform/prebuilts/runtime/+/aa21a6e/appcompat/hiddenapi-flags.csv#123239) * [`Landroid/net/wifi/p2p/WifiP2pManager;->deletePersistentGroup(Landroid/net/wifi/p2p/WifiP2pManager$Channel;ILandroid/net/wifi/p2p/WifiP2pManager$ActionListener;)V,greylist`](https://android.googlesource.com/platform/prebuilts/runtime/+/aa21a6e/appcompat/hiddenapi-flags.csv#123431) @@ -123,7 +126,6 @@ Undocumented API list: * [`Landroid/net/wifi/p2p/WifiP2pManager;->setWifiP2pChannels(Landroid/net/wifi/p2p/WifiP2pManager$Channel;IILandroid/net/wifi/p2p/WifiP2pManager$ActionListener;)V,greylist`](https://android.googlesource.com/platform/prebuilts/runtime/+/aa21a6e/appcompat/hiddenapi-flags.csv#123458) * [`Landroid/net/wifi/p2p/WifiP2pManager;->startWps(Landroid/net/wifi/p2p/WifiP2pManager$Channel;Landroid/net/wifi/WpsInfo;Landroid/net/wifi/p2p/WifiP2pManager$ActionListener;)V,greylist`](https://android.googlesource.com/platform/prebuilts/runtime/+/aa21a6e/appcompat/hiddenapi-flags.csv#123459) * [`Ljava/net/InetAddress;->parseNumericAddress(Ljava/lang/String;)Ljava/net/InetAddress;,greylist`](https://android.googlesource.com/platform/prebuilts/runtime/+/aa21a6e/appcompat/hiddenapi-flags.csv#299587) -* (deprecated since API 26) `Landroid/net/wifi/WifiManager;->setWifiApEnabled(Landroid/net/wifi/WifiConfiguration;Z)Z` Undocumented system configurations: diff --git a/mobile/src/main/AndroidManifest.xml b/mobile/src/main/AndroidManifest.xml index a1ef8945..1c0274b0 100644 --- a/mobile/src/main/AndroidManifest.xml +++ b/mobile/src/main/AndroidManifest.xml @@ -32,6 +32,8 @@ + diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index ec88da98..4aab6beb 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -52,10 +52,12 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, SharedPrefere val supported get() = p2pManager != null var persistentSupported = false - val operatingChannel: Int get() { - val result = app.pref.getString(KEY_OPERATING_CHANNEL, null)?.toIntOrNull() ?: 0 - return if (result in 1..165) result else 0 - } + var operatingChannel: Int + get() { + val result = app.pref.getString(KEY_OPERATING_CHANNEL, null)?.toIntOrNull() ?: 0 + return if (result in 1..165) result else 0 + } + set(value) = app.pref.edit().putString(RepeaterService.KEY_OPERATING_CHANNEL, value.toString()).apply() } enum class Status { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/LocalOnlyHotspotManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/LocalOnlyHotspotManager.kt index 219a8680..456540cf 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/LocalOnlyHotspotManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/LocalOnlyHotspotManager.kt @@ -7,14 +7,10 @@ import android.content.ComponentName import android.content.Intent import android.content.ServiceConnection import android.content.pm.PackageManager -import android.graphics.Typeface import android.location.LocationManager import android.os.Build import android.os.IBinder import android.provider.Settings -import android.text.SpannableStringBuilder -import android.text.Spanned -import android.text.style.TypefaceSpan import android.view.View import android.widget.Toast import androidx.core.content.getSystemService @@ -24,7 +20,6 @@ import be.mygod.vpnhotspot.DebugHelper import be.mygod.vpnhotspot.LocalOnlyHotspotService import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.databinding.ListitemInterfaceBinding -import be.mygod.vpnhotspot.net.TetherType import be.mygod.vpnhotspot.util.ServiceForegroundConnector import be.mygod.vpnhotspot.util.formatAddresses import be.mygod.vpnhotspot.widget.SmartSnackbar @@ -76,15 +71,7 @@ class LocalOnlyHotspotManager(private val parent: TetheringFragment) : Manager() private val lookup: Map get() = parent.ifaceLookup override val icon get() = R.drawable.ic_action_perm_scan_wifi - override val title: CharSequence get() { - val configuration = binder?.configuration ?: return parent.getString(R.string.tethering_temp_hotspot) - return SpannableStringBuilder("${configuration.SSID} - ").apply { - val start = length - append(configuration.preSharedKey) - setSpan(if (Build.VERSION.SDK_INT >= 28) TypefaceSpan(Typeface.MONOSPACE) else - TypefaceSpan("monospace"), start, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) - } - } + override val title: CharSequence get() = parent.getString(R.string.tethering_temp_hotspot) override val text: CharSequence get() { return lookup[binder?.iface ?: return ""]?.formatAddresses() ?: "" } @@ -99,7 +86,7 @@ class LocalOnlyHotspotManager(private val parent: TetheringFragment) : Manager() override val type get() = VIEW_TYPE_LOCAL_ONLY_HOTSPOT private val data = Data() - private var binder: LocalOnlyHotspotService.Binder? = null + internal var binder: LocalOnlyHotspotService.Binder? = null override fun bindTo(viewHolder: RecyclerView.ViewHolder) { viewHolder as ViewHolder diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt index c6313cfb..db0be8c3 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt @@ -6,6 +6,7 @@ import android.content.Intent import android.content.ServiceConnection import android.net.wifi.WifiConfiguration import android.net.wifi.p2p.WifiP2pGroup +import android.os.Build import android.os.Bundle import android.os.IBinder import android.os.Parcelable @@ -21,10 +22,8 @@ import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.get import androidx.recyclerview.widget.RecyclerView import be.mygod.vpnhotspot.* -import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.databinding.ListitemRepeaterBinding -import be.mygod.vpnhotspot.net.wifi.P2pSupplicantConfiguration -import be.mygod.vpnhotspot.net.wifi.WifiP2pDialogFragment +import be.mygod.vpnhotspot.net.wifi.configuration.* import be.mygod.vpnhotspot.util.ServiceForegroundConnector import be.mygod.vpnhotspot.util.formatAddresses import be.mygod.vpnhotspot.widget.SmartSnackbar @@ -51,7 +50,6 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic else -> false } - val ssid @Bindable get() = binder?.group?.networkName ?: "" val addresses: CharSequence @Bindable get() { return try { NetworkInterface.getByName(p2pInterface ?: return "")?.formatAddresses() ?: "" @@ -59,12 +57,6 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic "" } } - var oc: CharSequence - @Bindable get() { - val oc = RepeaterService.operatingChannel - return if (oc in 1..165) oc.toString() else "" - } - set(value) = app.pref.edit().putString(RepeaterService.KEY_OPERATING_CHANNEL, value.toString()).apply() fun onStatusChanged() { notifyPropertyChanged(BR.switchEnabled) @@ -72,7 +64,6 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic notifyPropertyChanged(BR.addresses) } fun onGroupChanged(group: WifiP2pGroup? = null) { - notifyPropertyChanged(BR.ssid) p2pInterface = group?.`interface` notifyPropertyChanged(BR.addresses) } @@ -92,22 +83,6 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic fun wps() { if (binder?.active == true) WpsDialogFragment().show(parent, TetheringFragment.REPEATER_WPS) } - - fun editConfigurations() { - val group = binder?.group - if (group != null) try { - val config = P2pSupplicantConfiguration(group, binder?.thisDevice?.deviceAddress) - holder.config = config - WifiP2pDialogFragment().withArg(WifiP2pDialogFragment.Arg(WifiConfiguration().apply { - SSID = group.networkName - preSharedKey = config.psk - })).show(parent, TetheringFragment.REPEATER_EDIT_CONFIGURATION) - return - } catch (e: RuntimeException) { - Timber.w(e) - } - SmartSnackbar.make(R.string.repeater_configure_failure).show() - } } @Parcelize @@ -168,16 +143,35 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic } } - fun onEditResult(data: Intent?) { - val master = holder.config ?: return - try { - val config = AlertDialogFragment.getRet(data!!).configuration - master.update(config.SSID, config.preSharedKey) - binder!!.group = null - } catch (e: Exception) { + val configuration: WifiConfiguration? get() { + val group = binder?.group + if (group != null) try { + val config = P2pSupplicantConfiguration(group, binder?.thisDevice?.deviceAddress) + holder.config = config + return newWifiApConfiguration(group.networkName, config.psk).apply { + allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK) // is not actually used + if (Build.VERSION.SDK_INT >= 23) { + apBand = AP_BAND_ANY + apChannel = RepeaterService.operatingChannel + } + } + } catch (e: RuntimeException) { Timber.w(e) - SmartSnackbar.make(e).show() } - holder.config = null + SmartSnackbar.make(R.string.repeater_configure_failure).show() + return null + } + fun updateConfiguration(config: WifiConfiguration) { + holder.config?.let { master -> + if (binder?.group?.networkName != config.SSID || master.psk != config.preSharedKey) try { + master.update(config.SSID, config.preSharedKey) + binder!!.group = null + } catch (e: Exception) { + Timber.w(e) + SmartSnackbar.make(e).show() + } + holder.config = null + } + RepeaterService.operatingChannel = config.apChannel } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt index 8fd43896..48d71e58 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringFragment.kt @@ -1,15 +1,16 @@ package be.mygod.vpnhotspot.manage import android.annotation.TargetApi -import android.content.ComponentName -import android.content.Intent -import android.content.IntentFilter -import android.content.ServiceConnection +import android.content.* import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.os.IBinder -import android.view.* +import android.view.LayoutInflater +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup +import androidx.appcompat.widget.Toolbar import androidx.core.content.ContextCompat import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment @@ -23,24 +24,30 @@ import be.mygod.vpnhotspot.net.TetherType import be.mygod.vpnhotspot.net.TetheringManager import be.mygod.vpnhotspot.net.TetheringManager.localOnlyTetheredIfaces import be.mygod.vpnhotspot.net.TetheringManager.tetheredIfaces +import be.mygod.vpnhotspot.net.wifi.WifiApManager +import be.mygod.vpnhotspot.net.wifi.configuration.WifiApDialogFragment import be.mygod.vpnhotspot.util.ServiceForegroundConnector import be.mygod.vpnhotspot.util.broadcastReceiver import be.mygod.vpnhotspot.util.isNotGone +import be.mygod.vpnhotspot.widget.SmartSnackbar import kotlinx.android.synthetic.main.activity_main.* import timber.log.Timber +import java.lang.IllegalArgumentException +import java.lang.reflect.InvocationTargetException import java.net.NetworkInterface import java.net.SocketException -class TetheringFragment : Fragment(), ServiceConnection, MenuItem.OnMenuItemClickListener { +class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickListener { companion object { const val START_LOCAL_ONLY_HOTSPOT = 1 - const val REPEATER_EDIT_CONFIGURATION = 2 const val REPEATER_WPS = 3 + const val CONFIGURE_REPEATER = 2 + const val CONFIGURE_AP = 4 } inner class ManagerAdapter : ListAdapter(Manager) { internal val repeaterManager by lazy { RepeaterManager(this@TetheringFragment) } - private val localOnlyHotspotManager by lazy @TargetApi(26) { LocalOnlyHotspotManager(this@TetheringFragment) } + internal val localOnlyHotspotManager by lazy @TargetApi(26) { LocalOnlyHotspotManager(this@TetheringFragment) } private val tetherManagers by lazy @TargetApi(24) { listOf(TetherManager.Wifi(this@TetheringFragment), TetherManager.Usb(this@TetheringFragment), @@ -100,13 +107,47 @@ class TetheringFragment : Fragment(), ServiceConnection, MenuItem.OnMenuItemClic item.isNotGone = canMonitor.isNotEmpty() item.subMenu.apply { clear() - canMonitor.sorted().forEach { add(it).setOnMenuItemClickListener(this@TetheringFragment) } + for (iface in canMonitor.sorted()) add(iface).setOnMenuItemClickListener { + ContextCompat.startForegroundService(requireContext(), Intent(context, TetheringService::class.java) + .putExtra(TetheringService.EXTRA_ADD_INTERFACE_MONITOR, iface)) + true + } } } override fun onMenuItemClick(item: MenuItem?): Boolean { - ContextCompat.startForegroundService(requireContext(), Intent(context, TetheringService::class.java) - .putExtra(TetheringService.EXTRA_ADD_INTERFACE_MONITOR, item?.title ?: return false)) - return true + return when (item?.itemId) { + R.id.configuration -> item.subMenu.run { + findItem(R.id.configuration_repeater).isNotGone = RepeaterService.supported + findItem(R.id.configuration_temp_hotspot).isNotGone = + adapter.localOnlyHotspotManager.binder?.configuration != null + true + } + R.id.configuration_repeater -> { + WifiApDialogFragment().withArg(WifiApDialogFragment.Arg( + adapter.repeaterManager.configuration ?: return false, + p2pMode = true + )).show(this, CONFIGURE_REPEATER) + true + } + R.id.configuration_temp_hotspot -> { + WifiApDialogFragment().withArg(WifiApDialogFragment.Arg( + adapter.localOnlyHotspotManager.binder?.configuration ?: return false, + readOnly = true + )).show(this, 0) // read-only, no callback needed + true + } + R.id.configuration_ap -> try { + WifiApDialogFragment().withArg(WifiApDialogFragment.Arg( + WifiApManager.configuration + )).show(this, CONFIGURE_AP) + true + } catch (e: InvocationTargetException) { + if (e.targetException !is SecurityException) Timber.w(e) + SmartSnackbar.make(e.targetException).show() + false + } + else -> false + } } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { @@ -117,13 +158,19 @@ class TetheringFragment : Fragment(), ServiceConnection, MenuItem.OnMenuItemClic binding.interfaces.adapter = adapter adapter.update(emptyList(), emptyList(), emptyList()) ServiceForegroundConnector(this, this, TetheringService::class) - requireActivity().toolbar.inflateMenu(R.menu.toolbar_tethering) + requireActivity().toolbar.apply { + inflateMenu(R.menu.toolbar_tethering) + setOnMenuItemClickListener(this@TetheringFragment) + } return binding.root } override fun onDestroyView() { super.onDestroyView() - requireActivity().toolbar.menu.clear() + requireActivity().toolbar.apply { + menu.clear() + setOnMenuItemClickListener(null) + } } override fun onResume() { @@ -131,10 +178,20 @@ class TetheringFragment : Fragment(), ServiceConnection, MenuItem.OnMenuItemClic if (Build.VERSION.SDK_INT >= 27) ManageBar.Data.notifyChange() } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) = when (requestCode) { - REPEATER_WPS -> adapter.repeaterManager.onWpsResult(resultCode, data) - REPEATER_EDIT_CONFIGURATION -> adapter.repeaterManager.onEditResult(data) - else -> super.onActivityResult(requestCode, resultCode, data) + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + val configuration by lazy { AlertDialogFragment.getRet(data!!).configuration } + when (requestCode) { + REPEATER_WPS -> adapter.repeaterManager.onWpsResult(resultCode, data) + CONFIGURE_REPEATER -> if (resultCode == DialogInterface.BUTTON_POSITIVE) { + adapter.repeaterManager.updateConfiguration(configuration) + } + CONFIGURE_AP -> if (resultCode == DialogInterface.BUTTON_POSITIVE) try { + WifiApManager.configuration = configuration + } catch (e: IllegalArgumentException) { + SmartSnackbar.make(R.string.configuration_rejected).show() + } + else -> super.onActivityResult(requestCode, resultCode, data) + } } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt index 653d3f78..b9dd7200 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt @@ -3,15 +3,23 @@ package be.mygod.vpnhotspot.net.wifi import android.net.wifi.WifiConfiguration import android.net.wifi.WifiManager import be.mygod.vpnhotspot.App.Companion.app +import java.lang.IllegalArgumentException -/** - * Although the functionalities were removed in API 26, it is already not functioning correctly on API 25. - * - * See also: https://android.googlesource.com/platform/frameworks/base/+/5c0b10a4a9eecc5307bb89a271221f2b20448797%5E%21/ - */ object WifiApManager { - private val setWifiApEnabled = WifiManager::class.java.getDeclaredMethod("setWifiApEnabled", - WifiConfiguration::class.java, Boolean::class.java) + private val getWifiApConfiguration by lazy { WifiManager::class.java.getDeclaredMethod("getWifiApConfiguration") } + private val setWifiApConfiguration by lazy { + WifiManager::class.java.getDeclaredMethod("setWifiApConfiguration", WifiConfiguration::class.java) + } + var configuration: WifiConfiguration + get() = getWifiApConfiguration.invoke(app.wifi) as WifiConfiguration + set(value) { + if (setWifiApConfiguration.invoke(app.wifi, value) as? Boolean != true) throw IllegalArgumentException() + } + + private val setWifiApEnabled by lazy { + WifiManager::class.java.getDeclaredMethod("setWifiApEnabled", + WifiConfiguration::class.java, Boolean::class.java) + } /** * Start AccessPoint mode with the specified * configuration. If the radio is already running in @@ -25,6 +33,11 @@ object WifiApManager { private fun WifiManager.setWifiApEnabled(wifiConfig: WifiConfiguration?, enabled: Boolean) = setWifiApEnabled.invoke(this, wifiConfig, enabled) as Boolean + /** + * Although the functionalities were removed in API 26, it is already not functioning correctly on API 25. + * + * See also: https://android.googlesource.com/platform/frameworks/base/+/5c0b10a4a9eecc5307bb89a271221f2b20448797%5E%21/ + */ @Deprecated("Not usable since API 26, malfunctioning on API 25") fun start(wifiConfig: WifiConfiguration? = null) { app.wifi.isWifiEnabled = false diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt deleted file mode 100644 index 2c447ede..00000000 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialogFragment.kt +++ /dev/null @@ -1,74 +0,0 @@ -package be.mygod.vpnhotspot.net.wifi - -import android.content.DialogInterface -import android.net.wifi.WifiConfiguration -import android.net.wifi.WifiConfiguration.AuthAlgorithm -import android.os.Parcelable -import android.text.Editable -import android.text.TextWatcher -import android.view.View -import android.widget.EditText -import android.widget.TextView -import androidx.appcompat.app.AlertDialog -import be.mygod.vpnhotspot.AlertDialogFragment -import be.mygod.vpnhotspot.R -import kotlinx.android.parcel.Parcelize -import kotlinx.android.synthetic.main.dialog_wifi_ap.view.* -import java.nio.charset.Charset - -/** - * Based on: https://android.googlesource.com/platform/packages/apps/Settings/+/39b4674/src/com/android/settings/wifi/WifiApDialog.java - * - * This dialog has been deprecated in API 28, but we are still using it since it works better for our purposes. - * Related: https://android.googlesource.com/platform/packages/apps/Settings/+/defb1183ecb00d6231bac7d934d07f58f90261ea - */ -class WifiP2pDialogFragment : AlertDialogFragment(), TextWatcher { - @Parcelize - data class Arg(val configuration: WifiConfiguration) : Parcelable - - private lateinit var mView: View - private lateinit var mSsid: TextView - private lateinit var mPassword: EditText - override val ret: Arg? get() { - val config = WifiConfiguration() - config.SSID = mSsid.text.toString() - config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN) - if (mPassword.length() != 0) { - val password = mPassword.text.toString() - config.preSharedKey = password - } - return Arg(config) - } - - override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) { - mView = requireActivity().layoutInflater.inflate(R.layout.dialog_wifi_ap, null) - setView(mView) - setTitle(R.string.repeater_configure) - mSsid = mView.ssid - mPassword = mView.password - setPositiveButton(context.getString(R.string.wifi_save), listener) - setNegativeButton(context.getString(R.string.wifi_cancel), null) - mSsid.text = arg.configuration.SSID - mSsid.addTextChangedListener(this@WifiP2pDialogFragment) - mPassword.setText(arg.configuration.preSharedKey) - mPassword.addTextChangedListener(this@WifiP2pDialogFragment) - } - - override fun onStart() { - super.onStart() - validate() - } - - private fun validate() { - val mSsidString = mSsid.text.toString() - val ssidValid = mSsid.length() != 0 && Charset.forName("UTF-8").encode(mSsidString).limit() <= 32 - val passwordValid = mPassword.length() >= 8 - mView.password_wrapper.error = - if (passwordValid) null else requireContext().getString(R.string.credentials_password_too_short) - (dialog as AlertDialog).getButton(DialogInterface.BUTTON_POSITIVE).isEnabled = ssidValid && passwordValid - } - - override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { } - override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { } - override fun afterTextChanged(editable: Editable) = validate() -} diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/P2pSupplicantConfiguration.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/P2pSupplicantConfiguration.kt similarity index 99% rename from mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/P2pSupplicantConfiguration.kt rename to mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/P2pSupplicantConfiguration.kt index 06d9b626..396086b4 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/P2pSupplicantConfiguration.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/P2pSupplicantConfiguration.kt @@ -1,4 +1,4 @@ -package be.mygod.vpnhotspot.net.wifi +package be.mygod.vpnhotspot.net.wifi.configuration import android.net.wifi.p2p.WifiP2pGroup import android.os.Build diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiApDialogFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiApDialogFragment.kt new file mode 100644 index 00000000..b77eee0c --- /dev/null +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiApDialogFragment.kt @@ -0,0 +1,149 @@ +package be.mygod.vpnhotspot.net.wifi.configuration + +import android.annotation.TargetApi +import android.content.DialogInterface +import android.net.wifi.WifiConfiguration +import android.net.wifi.WifiConfiguration.AuthAlgorithm +import android.os.Build +import android.os.Parcelable +import android.text.Editable +import android.text.TextWatcher +import android.view.View +import android.widget.AdapterView +import android.widget.ArrayAdapter +import androidx.appcompat.app.AlertDialog +import androidx.core.view.isGone +import be.mygod.vpnhotspot.AlertDialogFragment +import be.mygod.vpnhotspot.App.Companion.app +import be.mygod.vpnhotspot.R +import kotlinx.android.parcel.Parcelize +import kotlinx.android.synthetic.main.dialog_wifi_ap.view.* +import java.lang.IllegalStateException +import java.nio.charset.Charset + +/** + * Based on: https://android.googlesource.com/platform/packages/apps/Settings/+/39b4674/src/com/android/settings/wifi/WifiApDialog.java + * + * This dialog has been deprecated in API 28, but we are still using it since it works better for our purposes. + * Related: https://android.googlesource.com/platform/packages/apps/Settings/+/defb1183ecb00d6231bac7d934d07f58f90261ea + */ +class WifiApDialogFragment : AlertDialogFragment(), TextWatcher { + @Parcelize + data class Arg(val configuration: WifiConfiguration, + val readOnly: Boolean = false, + /** + * KeyMgmt is enforced to WPA_PSK. + * Various values for apBand are allowed according to different rules. + */ + val p2pMode: Boolean = false) : Parcelable + + @TargetApi(23) + private sealed class BandOption { + open val apBand get() = AP_BAND_2GHZ + open val apChannel get() = 0 + + object BandAny : BandOption() { + override val apBand get() = AP_BAND_ANY + override fun toString() = app.getString(R.string.wifi_ap_choose_auto) + } + object Band2GHz : BandOption() { + override fun toString() = app.getString(R.string.wifi_ap_choose_2G) + } + object Band5GHz : BandOption() { + override val apBand get() = AP_BAND_5GHZ + override fun toString() = app.getString(R.string.wifi_ap_choose_5G) + } + class Channel(override val apChannel: Int) : BandOption() { + override fun toString() = "${channelToFrequency(apChannel)} MHz ($apChannel)" + } + } + + private lateinit var dialogView: View + override val ret: Arg? get() { + return Arg(WifiConfiguration().apply { + SSID = dialogView.ssid.text.toString() + allowedKeyManagement.set( + if (arg.p2pMode) WifiConfiguration.KeyMgmt.WPA_PSK else dialogView.security.selectedItemPosition) + allowedAuthAlgorithms.set(AuthAlgorithm.OPEN) + if (dialogView.password.length() != 0) preSharedKey = dialogView.password.text.toString() + if (Build.VERSION.SDK_INT >= 23) { + val bandOption = dialogView.band.selectedItem as BandOption + apBand = bandOption.apBand + apChannel = bandOption.apChannel + } + }) + } + + override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) { + val activity = requireActivity() + dialogView = activity.layoutInflater.inflate(R.layout.dialog_wifi_ap, null) + setView(dialogView) + setTitle(R.string.configuration_view) + if (!arg.readOnly) setPositiveButton(R.string.wifi_save, listener) + setNegativeButton(R.string.donations__button_close, null) + dialogView.ssid.setText(arg.configuration.SSID) + if (!arg.readOnly) dialogView.ssid.addTextChangedListener(this@WifiApDialogFragment) + if (arg.p2pMode) dialogView.security_wrapper.isGone = true else dialogView.security.apply { + adapter = ArrayAdapter(activity, android.R.layout.simple_spinner_item, 0, + WifiConfiguration.KeyMgmt.strings).apply { + setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) + } + onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + override fun onNothingSelected(parent: AdapterView<*>?) = + throw IllegalStateException("Must select something") + override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { + dialogView.password_wrapper.isGone = position == WifiConfiguration.KeyMgmt.NONE + } + } + val selected = arg.configuration.allowedKeyManagement.nextSetBit(0) + check(selected >= 0) { "No key management selected" } + check(arg.configuration.allowedKeyManagement.nextSetBit(selected + 1) < 0) { + "More than 1 key managements supplied" + } + setSelection(selected) + } + dialogView.password.setText(arg.configuration.preSharedKey) + if (!arg.readOnly) dialogView.password.addTextChangedListener(this@WifiApDialogFragment) + if (Build.VERSION.SDK_INT >= 23) dialogView.band.apply { + val options = mutableListOf().apply { + if (arg.p2pMode) add(BandOption.BandAny) else { + if (Build.VERSION.SDK_INT >= 28) add(BandOption.BandAny) + add(BandOption.Band2GHz) + add(BandOption.Band5GHz) + } + addAll((1..165).map { BandOption.Channel(it) }) + } + adapter = ArrayAdapter(activity, android.R.layout.simple_spinner_item, 0, options).apply { + setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) + } + setSelection(if (arg.configuration.apChannel in 1..165) { + options.indexOfFirst { it.apChannel == arg.configuration.apChannel } + } else options.indexOfFirst { it.apBand == arg.configuration.apBand }) + } else dialogView.band_wrapper.isGone = true + } + + override fun onResume() { + super.onResume() + if (!arg.readOnly) validate() + } + + /** + * This function is reached only if not arg.readOnly. + */ + private fun validate() { + val ssidValid = dialogView.ssid.length() != 0 && + Charset.forName("UTF-8").encode(dialogView.ssid.text.toString()).limit() <= 32 + val passwordValid = when (dialogView.security.selectedItemPosition) { + WifiConfiguration.KeyMgmt.WPA_PSK, WPA2_PSK -> dialogView.password.length() >= 8 + else -> true // do not try to validate + } + dialogView.password_wrapper.error = if (passwordValid) null else { + requireContext().getString(R.string.credentials_password_too_short) + } + (dialog as AlertDialog).getButton(DialogInterface.BUTTON_POSITIVE).isEnabled = ssidValid && passwordValid + } + + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { } + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { } + override fun afterTextChanged(editable: Editable) = validate() +} diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiConfiguration.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiConfiguration.kt new file mode 100644 index 00000000..d7e361ec --- /dev/null +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/configuration/WifiConfiguration.kt @@ -0,0 +1,84 @@ +package be.mygod.vpnhotspot.net.wifi.configuration + +import android.net.wifi.WifiConfiguration +import androidx.annotation.RequiresApi +import be.mygod.vpnhotspot.net.wifi.WifiApManager +import timber.log.Timber +import java.lang.reflect.InvocationTargetException + +val WPA2_PSK = WifiConfiguration.KeyMgmt.strings.indexOf("WPA2_PSK") + +/** + * apBand and apChannel is available since API 23. + * + * https://android.googlesource.com/platform/frameworks/base/+/android-6.0.0_r1/wifi/java/android/net/wifi/WifiConfiguration.java#242 + */ +private val apBandField by lazy { WifiConfiguration::class.java.getDeclaredField("apBand") } +private val apChannelField by lazy { WifiConfiguration::class.java.getDeclaredField("apChannel") } + +/** + * 2GHz band. + * + * https://android.googlesource.com/platform/frameworks/base/+/android-7.0.0_r1/wifi/java/android/net/wifi/WifiConfiguration.java#241 + */ +@RequiresApi(23) +const val AP_BAND_2GHZ = 0 +/** + * 5GHz band. + */ +@RequiresApi(23) +const val AP_BAND_5GHZ = 1 +/** + * Device is allowed to choose the optimal band (2Ghz or 5Ghz) based on device capability, + * operating country code and current radio conditions. + * + * Introduced in 9.0, but we will abuse this constant anyway. + * https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r1/wifi/java/android/net/wifi/WifiConfiguration.java#295 + */ +@RequiresApi(23) +const val AP_BAND_ANY = -1 + +/** + * The band which AP resides on + * -1:Any 0:2G 1:5G + * By default, 2G is chosen + */ +var WifiConfiguration.apBand: Int + @RequiresApi(23) get() = apBandField.get(this) as Int + @RequiresApi(23) set(value) = apBandField.set(this, value) +/** + * The channel which AP resides on + * 2G 1-11 + * 5G 36,40,44,48,149,153,157,161,165 + * 0 - find a random available channel according to the apBand + */ +var WifiConfiguration.apChannel: Int + @RequiresApi(23) get() = apChannelField.get(this) as Int + @RequiresApi(23) set(value) = apChannelField.set(this, value) + +/** + * The frequency which AP resides on (MHz). Resides in range [2412, 5815]. + */ +fun channelToFrequency(channel: Int) = when (channel) { + in 1..14 -> 2407 + 5 * channel + in 15..165 -> 5000 + 5 * channel + else -> throw IllegalArgumentException("Invalid channel $channel") +} + +/** + * Based on: + * https://android.googlesource.com/platform/packages/apps/Settings/+/android-5.0.0_r1/src/com/android/settings/wifi/WifiApDialog.java#88 + * https://android.googlesource.com/platform/packages/apps/Settings/+/b1af85d/src/com/android/settings/wifi/tether/WifiTetherSettings.java#162 + */ +fun newWifiApConfiguration(ssid: String, passphrase: String?) = try { + WifiApManager.configuration +} catch (e: InvocationTargetException) { + if (e.targetException !is SecurityException) Timber.w(e) + WifiConfiguration() +}.apply { + SSID = ssid + preSharedKey = passphrase + allowedKeyManagement.clear() + allowedAuthAlgorithms.clear() + allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN) +} diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/room/Converters.kt b/mobile/src/main/java/be/mygod/vpnhotspot/room/Converters.kt index 9a4c95de..4199a33b 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/room/Converters.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/room/Converters.kt @@ -1,8 +1,8 @@ package be.mygod.vpnhotspot.room -import android.os.Parcel import android.text.TextUtils import androidx.room.TypeConverter +import be.mygod.vpnhotspot.util.useParcel import java.net.InetAddress import java.nio.ByteBuffer import java.nio.ByteOrder @@ -10,27 +10,17 @@ import java.nio.ByteOrder object Converters { @JvmStatic @TypeConverter - fun persistCharSequence(cs: CharSequence): ByteArray { - val p = Parcel.obtain() - try { - TextUtils.writeToParcel(cs, p, 0) - return p.marshall() - } finally { - p.recycle() - } + fun persistCharSequence(cs: CharSequence) = useParcel { p -> + TextUtils.writeToParcel(cs, p, 0) + p.marshall() } @JvmStatic @TypeConverter - fun unpersistCharSequence(data: ByteArray): CharSequence { - val p = Parcel.obtain() - try { - p.unmarshall(data, 0, data.size) - p.setDataPosition(0) - return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p) - } finally { - p.recycle() - } + fun unpersistCharSequence(data: ByteArray) = useParcel { p -> + p.unmarshall(data, 0, data.size) + p.setDataPosition(0) + TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p) } @JvmStatic diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt index 42f174ef..54b63d2a 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -1,7 +1,9 @@ package be.mygod.vpnhotspot.util +import android.annotation.SuppressLint import android.content.* import android.os.Build +import android.os.Parcel import android.text.Spannable import android.text.SpannableString import android.text.SpannableStringBuilder @@ -15,7 +17,6 @@ import androidx.databinding.BindingAdapter import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.room.macToString import be.mygod.vpnhotspot.widget.SmartSnackbar -import java.lang.RuntimeException import java.net.InetAddress import java.net.NetworkInterface import java.net.SocketException @@ -32,6 +33,15 @@ fun Long.toPluralInt(): Int { return (this % 1000000000).toInt() + 1000000000 } +@SuppressLint("Recycle") +fun useParcel(block: (Parcel) -> T) = Parcel.obtain().run { + try { + block(this) + } finally { + recycle() + } +} + fun broadcastReceiver(receiver: (Context, Intent) -> Unit) = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) = receiver(context, intent) } diff --git a/mobile/src/main/res/drawable/ic_content_wave.xml b/mobile/src/main/res/drawable/ic_content_wave.xml deleted file mode 100644 index 68e040eb..00000000 --- a/mobile/src/main/res/drawable/ic_content_wave.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - diff --git a/mobile/src/main/res/layout/dialog_wifi_ap.xml b/mobile/src/main/res/layout/dialog_wifi_ap.xml index 2d3a445a..1290d53e 100644 --- a/mobile/src/main/res/layout/dialog_wifi_ap.xml +++ b/mobile/src/main/res/layout/dialog_wifi_ap.xml @@ -1,5 +1,5 @@ - + - Настройка Wi-Fi ретранслятора Действительный конфиг не найден. Пожалуйста, сначала запустите ретранслятор. Не удалось удалить избыточную группу P2P (причина: %s) @@ -15,7 +18,9 @@ неподдерживаемая операция Сервис недоступен. Попробуйте позже - + "USB-модем" + "Точка доступа Wi‑Fi" + "Bluetooth-модем" " (подключение)" " (доступный)" @@ -37,7 +42,16 @@ Ошибка: Нисходящий интерфейс не найден Что-то пошло не так, пожалуйста, проверьте отладочную информацию. + Настройка Wi-Fi ретранслятора + "Имя сети" + "Защита" + "Пароль" Пароль должен содержать не менее 8 символов. + "Диапазон частот Wi-Fi" + "Авто" + "2,4 ГГц" + "5,0 ГГц" + "Сохранить" Закрыть diff --git a/mobile/src/main/res/values-zh-rCN/strings.xml b/mobile/src/main/res/values-zh-rCN/strings.xml index 8f8bcfde..feea1ce4 100644 --- a/mobile/src/main/res/values-zh-rCN/strings.xml +++ b/mobile/src/main/res/values-zh-rCN/strings.xml @@ -12,7 +12,6 @@ 请在 2 分钟内在需要连接的设备上使用一键加密以连接到此中继。 成功注册 PIN。 打开 WPS 失败(原因:%s) - 设置 WLAN 中继 未能找到有效的档案。请尝试先打开中继。 删除多余 P2P 群组失败(原因:%s) @@ -44,7 +43,7 @@ 关闭 diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index 213f4fe6..656a3228 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -2,8 +2,7 @@ Close diff --git a/mobile/src/main/res/values/styles.xml b/mobile/src/main/res/values/styles.xml index 87353f85..ede8762e 100644 --- a/mobile/src/main/res/values/styles.xml +++ b/mobile/src/main/res/values/styles.xml @@ -21,6 +21,18 @@ vertical start + +