diff options
author | Arne Schwabe <arne@rfc2549.org> | 2021-06-15 16:52:14 +0200 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2021-06-15 16:52:14 +0200 |
commit | d7d752f45e688134407372a83533c68e36bb9e8c (patch) | |
tree | 1cf22ad220eba67f850ece9226d281ee03912342 /main/src/ui | |
parent | 0d5277d7380ed5ae61216c7041bbafe934827613 (diff) |
Cleanup kotlin migration and message to use Always on VPN
Diffstat (limited to 'main/src/ui')
-rw-r--r-- | main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt | 97 |
1 files changed, 60 insertions, 37 deletions
diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt b/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt index 710adad1..811210d3 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt @@ -4,15 +4,17 @@ */ package de.blinkt.openvpn.fragments -import android.app.AlertDialog import android.app.Dialog import android.content.DialogInterface import android.content.Intent import android.content.pm.ApplicationInfo import android.content.pm.PackageManager +import android.net.VpnService import android.os.Build import android.os.Bundle +import android.provider.Settings import android.widget.Toast +import androidx.appcompat.app.AlertDialog import androidx.preference.* import de.blinkt.openvpn.BuildConfig import de.blinkt.openvpn.R @@ -23,47 +25,55 @@ import java.io.File class GeneralSettings : PreferenceFragmentCompat(), Preference.OnPreferenceClickListener, DialogInterface.OnClickListener, Preference.OnPreferenceChangeListener { - private var mExtapp: ExternalAppDatabase? = null - private var mAlwaysOnVPN: ListPreference? = null - override fun onCreatePreferences(savedInstanceState: Bundle, rootKey: String) { - + private lateinit var mExtapp: ExternalAppDatabase + private lateinit var mAlwaysOnVPN: ListPreference + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { // Load the preferences from an XML resource addPreferencesFromResource(R.xml.general_settings) - val devHacks = findPreference<PreferenceCategory>("device_hacks") - mAlwaysOnVPN = findPreference("alwaysOnVpn") - mAlwaysOnVPN!!.onPreferenceChangeListener = this - val loadtun = findPreference<Preference>("loadTunModule") + val devHacks = findPreference<PreferenceCategory>("device_hacks") as PreferenceCategory + mAlwaysOnVPN = findPreference("alwaysOnVpn")!! + mAlwaysOnVPN.onPreferenceChangeListener = this + val loadtun = findPreference<Preference>("loadTunModule")!! if (!isTunModuleAvailable) { - loadtun!!.isEnabled = false - devHacks!!.removePreference(loadtun) + loadtun.isEnabled = false + devHacks.removePreference(loadtun) } - val cm9hack = findPreference<Preference>("useCM9Fix") as CheckBoxPreference? - if (!cm9hack!!.isChecked && Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) { - devHacks!!.removePreference(cm9hack) + val cm9hack = findPreference<Preference>("useCM9Fix") as CheckBoxPreference + if (!cm9hack.isChecked && Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) { + devHacks.removePreference(cm9hack) } val useInternalFS = - findPreference<Preference>("useInternalFileSelector") as CheckBoxPreference? + findPreference<Preference>("useInternalFileSelector") as CheckBoxPreference if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { - devHacks!!.removePreference(useInternalFS) + devHacks.removePreference(useInternalFS) } /* Android P does not allow access to the file storage anymore */if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) { val useInternalFileSelector = findPreference<Preference>("useInternalFileSelector") - devHacks!!.removePreference(useInternalFileSelector) + devHacks.removePreference(useInternalFileSelector) } mExtapp = ExternalAppDatabase(activity) - val clearapi = findPreference<Preference>("clearapi") - clearapi!!.onPreferenceClickListener = this + val clearapi = findPreference<Preference>("clearapi") as Preference + clearapi.onPreferenceClickListener = this findPreference<Preference>("osslspeed")!!.onPreferenceClickListener = this - if (devHacks!!.preferenceCount == 0) preferenceScreen.removePreference(devHacks) + if (devHacks.preferenceCount == 0) preferenceScreen.removePreference(devHacks) if (!BuildConfig.openvpn3) { - val appBehaviour = findPreference<Preference>("app_behaviour") as PreferenceCategory? - val ovpn3 = findPreference<Preference>("ovpn3") as CheckBoxPreference? - ovpn3!!.isEnabled = false + val appBehaviour = findPreference<Preference>("app_behaviour") as PreferenceCategory + val ovpn3 = findPreference<Preference>("ovpn3") as CheckBoxPreference + ovpn3.isEnabled = false ovpn3.isChecked = false } - (findPreference<Preference>("restartvpnonboot") as CheckBoxPreference?)!!.onPreferenceChangeListener = + + val startOnBoot = + findPreference<Preference>("restartvpnonboot") as CheckBoxPreference + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val vpn:VpnService = VpnService() + startOnBoot.isChecked = vpn.isAlwaysOn + } + + startOnBoot.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { pref: Preference?, newValue: Any -> if (newValue == true) { val vpn = ProfileManager.getAlwaysOnVPN(requireActivity()) @@ -73,11 +83,24 @@ class GeneralSettings : PreferenceFragmentCompat(), Preference.OnPreferenceClick R.string.no_default_vpn_set, Toast.LENGTH_LONG ).show() - return@setOnPreferenceChangeListener false + return@OnPreferenceChangeListener false } } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && newValue == true) + { + val builder = AlertDialog.Builder( + requireContext() + ) + builder.setMessage(R.string.use_alwayson_vpn) + builder.setNeutralButton(R.string.open_vpn_settings, { dialog, which -> + startActivity(Intent(Settings.ACTION_VPN_SETTINGS)) + }) + builder.setPositiveButton(android.R.string.ok, null) + builder.show() + } true } + setClearApiSummary() } @@ -92,40 +115,40 @@ class GeneralSettings : PreferenceFragmentCompat(), Preference.OnPreferenceClick vpn.name ) ) - mAlwaysOnVPN!!.summary = sb.toString() + mAlwaysOnVPN.summary = sb.toString() } override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean { if (preference === mAlwaysOnVPN) { val vpn = ProfileManager.get(activity, newValue as String) - mAlwaysOnVPN!!.summary = vpn.name + mAlwaysOnVPN.summary = vpn.name } return true } private fun setClearApiSummary() { - val clearapi = findPreference<Preference>("clearapi") - if (mExtapp!!.extAppList.isEmpty()) { - clearapi!!.isEnabled = false + val clearapi = findPreference<Preference>("clearapi") as Preference + if (mExtapp.extAppList.isEmpty()) { + clearapi.isEnabled = false clearapi.setSummary(R.string.no_external_app_allowed) } else { - clearapi!!.isEnabled = true + clearapi.isEnabled = true clearapi.summary = getString(R.string.allowed_apps, getExtAppList(", ")) } } private fun getExtAppList(delim: String): String { var app: ApplicationInfo - val pm = activity!!.packageManager + val pm = requireActivity().packageManager val applist = StringBuilder() - for (packagename in mExtapp!!.extAppList) { + for (packagename in mExtapp.extAppList) { try { app = pm.getApplicationInfo(packagename, 0) if (applist.length != 0) applist.append(delim) applist.append(app.loadLabel(pm)) } catch (e: PackageManager.NameNotFoundException) { // App not found. Remove it from the list - mExtapp!!.removeApp(packagename) + mExtapp.removeApp(packagename) } } return applist.toString() @@ -133,13 +156,13 @@ class GeneralSettings : PreferenceFragmentCompat(), Preference.OnPreferenceClick // Check if the tun module exists on the file system private val isTunModuleAvailable: Boolean - private get() =// Check if the tun module exists on the file system + get() =// Check if the tun module exists on the file system File("/system/lib/modules/tun.ko").length() > 10 override fun onPreferenceClick(preference: Preference): Boolean { if (preference.key == "clearapi") { val builder = AlertDialog.Builder( - activity + requireContext() ) builder.setPositiveButton(R.string.clear, this) builder.setNegativeButton(android.R.string.cancel, null) @@ -153,7 +176,7 @@ class GeneralSettings : PreferenceFragmentCompat(), Preference.OnPreferenceClick override fun onClick(dialog: DialogInterface, which: Int) { if (which == Dialog.BUTTON_POSITIVE) { - mExtapp!!.clearAllApiApps() + mExtapp.clearAllApiApps() setClearApiSummary() } } |