From 9a69c4006e6820795aa0b006fb7fe00f9e8c9c8a Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 11 Jul 2019 11:09:24 +0800 Subject: [PATCH] Add shortcut for tether offload Fix #41. --- README.md | 2 -- .../vpnhotspot/SettingsPreferenceFragment.kt | 17 ++++++++++++ .../be/mygod/vpnhotspot/manage/ManageBar.kt | 14 ++-------- .../vpnhotspot/net/TetherOffloadManager.kt | 27 +++++++++++++++++++ .../ic_device_battery_charging_full.xml | 10 +++++++ mobile/src/main/res/values-zh-rCN/strings.xml | 2 ++ mobile/src/main/res/values/strings.xml | 2 ++ mobile/src/main/res/xml/pref_settings.xml | 6 +++++ 8 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 mobile/src/main/java/be/mygod/vpnhotspot/net/TetherOffloadManager.kt create mode 100644 mobile/src/main/res/drawable/ic_device_battery_charging_full.xml diff --git a/README.md b/README.md index 75601883..2eee55c6 100644 --- a/README.md +++ b/README.md @@ -98,8 +98,6 @@ Search the [issue tracker](https://github.com/Mygod/VPNHotspot/issues) for more. ### [What changes exactly can this app do to my system? (and how to revert them)](https://github.com/Mygod/VPNHotspot/issues/8#issuecomment-448529512) -### [Unable to find "Tethering hardware acceleration" in Developer options?](https://github.com/Mygod/VPNHotspot/issues/41#issuecomment-425732001) - ### [No root?](https://github.com/Mygod/VPNHotspot/issues/62) ### Failed to create group due to internal error/repeater shuts down after a while? diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt index 9f4bc4fc..c77a29b2 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt @@ -10,6 +10,7 @@ import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreference import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.net.Routing.Companion.IPTABLES +import be.mygod.vpnhotspot.net.TetherOffloadManager import be.mygod.vpnhotspot.net.monitor.IpMonitor import be.mygod.vpnhotspot.net.monitor.UpstreamMonitor import be.mygod.vpnhotspot.net.wifi.WifiDoubleLock @@ -17,6 +18,7 @@ import be.mygod.vpnhotspot.preference.AlwaysAutoCompleteEditTextPreferenceDialog import be.mygod.vpnhotspot.preference.SharedPreferenceDataStore import be.mygod.vpnhotspot.util.RootSession import be.mygod.vpnhotspot.util.launchUrl +import be.mygod.vpnhotspot.widget.SmartSnackbar import com.google.android.material.snackbar.Snackbar import timber.log.Timber import java.io.File @@ -32,6 +34,21 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() { preferenceManager.preferenceDataStore = SharedPreferenceDataStore(app.pref) RoutingManager.masqueradeMode = RoutingManager.masqueradeMode // flush default value addPreferencesFromResource(R.xml.pref_settings) + findPreference("system.enableTetherOffload")!!.apply { + if (Build.VERSION.SDK_INT >= 27) { + isChecked = TetherOffloadManager.enabled + setOnPreferenceChangeListener { _, newValue -> + try { + TetherOffloadManager.enabled = newValue as Boolean + TetherOffloadManager.enabled == newValue + } catch (e: Exception) { + Timber.d(e) + SmartSnackbar.make(e).show() + false + } + } + } else parent!!.removePreference(this) + } val boot = findPreference("service.repeater.startOnBoot")!! if (RepeaterService.supported) { boot.setOnPreferenceChangeListener { _, value -> diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/ManageBar.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/ManageBar.kt index 261a389b..9ff634c9 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/ManageBar.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/ManageBar.kt @@ -3,14 +3,13 @@ package be.mygod.vpnhotspot.manage import android.content.Context import android.content.Intent import android.os.Build -import android.provider.Settings import android.view.View import androidx.core.os.bundleOf import androidx.databinding.BaseObservable import androidx.recyclerview.widget.RecyclerView -import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.DebugHelper import be.mygod.vpnhotspot.databinding.ListitemManageBinding +import be.mygod.vpnhotspot.net.TetherOffloadManager object ManageBar : Manager() { private const val TAG = "ManageBar" @@ -19,16 +18,7 @@ object ManageBar : Manager() { private const val SETTINGS_2 = "com.android.settings.TetherSettings" object Data : BaseObservable() { - /** - * It's hard to change tethering rules with Tethering hardware acceleration enabled for now. - * - * See also: - * android.provider.Settings.Global.TETHER_OFFLOAD_DISABLED - * https://android.googlesource.com/platform/frameworks/base/+/android-8.1.0_r1/services/core/java/com/android/server/connectivity/tethering/OffloadHardwareInterface.java#45 - * https://android.googlesource.com/platform/hardware/qcom/data/ipacfg-mgr/+/master/msm8998/ipacm/src/IPACM_OffloadManager.cpp - */ - val offloadEnabled get() = Build.VERSION.SDK_INT >= 27 && Settings.Global.getInt(app.contentResolver, - "tether_offload_disabled", 0) == 0 + val offloadEnabled get() = Build.VERSION.SDK_INT >= 27 && TetherOffloadManager.enabled } class ViewHolder(binding: ListitemManageBinding) : RecyclerView.ViewHolder(binding.root), View.OnClickListener { init { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherOffloadManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherOffloadManager.kt new file mode 100644 index 00000000..3f0b899f --- /dev/null +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherOffloadManager.kt @@ -0,0 +1,27 @@ +package be.mygod.vpnhotspot.net + +import android.provider.Settings +import androidx.annotation.RequiresApi +import be.mygod.vpnhotspot.App.Companion.app +import be.mygod.vpnhotspot.util.RootSession + +/** + * It's hard to change tethering rules with Tethering hardware acceleration enabled for now. + * + * See also: + * android.provider.Settings.Global.TETHER_OFFLOAD_DISABLED + * https://android.googlesource.com/platform/frameworks/base/+/android-8.1.0_r1/services/core/java/com/android/server/connectivity/tethering/OffloadHardwareInterface.java#45 + * https://android.googlesource.com/platform/hardware/qcom/data/ipacfg-mgr/+/master/msm8998/ipacm/src/IPACM_OffloadManager.cpp + */ +@RequiresApi(27) +object TetherOffloadManager { + private const val TETHER_OFFLOAD_DISABLED = "tether_offload_disabled" + @JvmStatic + var enabled: Boolean + get() = Settings.Global.getInt(app.contentResolver, TETHER_OFFLOAD_DISABLED, 0) == 0 + set(value) { + RootSession.use { + it.exec("settings put global $TETHER_OFFLOAD_DISABLED ${if (value) 0 else 1}") + } + } +} diff --git a/mobile/src/main/res/drawable/ic_device_battery_charging_full.xml b/mobile/src/main/res/drawable/ic_device_battery_charging_full.xml new file mode 100644 index 00000000..d50dc4a6 --- /dev/null +++ b/mobile/src/main/res/drawable/ic_device_battery_charging_full.xml @@ -0,0 +1,10 @@ + + + diff --git a/mobile/src/main/res/values-zh-rCN/strings.xml b/mobile/src/main/res/values-zh-rCN/strings.xml index 57e2c704..de7f442a 100644 --- a/mobile/src/main/res/values-zh-rCN/strings.xml +++ b/mobile/src/main/res/values-zh-rCN/strings.xml @@ -103,6 +103,8 @@ 将修改的设置应用到当前启用的服务上。也可用于修复偶尔会发生的竞态条件。 尝试修复 DHCP 如果设备无法获取 IP 地址,尝试打开这个选项。 + 网络共享硬件加速 + 系统“开发者选项”的快捷方式 杂项 帮助 导出调试信息 diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index 198e8a92..3611cba4 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -109,6 +109,8 @@ Clean/reapply routing rules Update changed settings to current active services. Can also fix rare race conditions. + Tethering hardware acceleration + Shortcut to system developer options Misc Help Export debug information diff --git a/mobile/src/main/res/xml/pref_settings.xml b/mobile/src/main/res/xml/pref_settings.xml index 1edeb6b2..a9219dff 100644 --- a/mobile/src/main/res/xml/pref_settings.xml +++ b/mobile/src/main/res/xml/pref_settings.xml @@ -28,6 +28,12 @@ app:title="@string/settings_service_disable_ipv6" app:summary="@string/settings_service_disable_ipv6_summary" app:defaultValue="true"/> +