From 26adf47d5f25758c70881039c329ec709b619068 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Mon, 14 Jun 2021 17:08:54 +0200 Subject: Convert General Settings to Kotlin --- .../blinkt/openvpn/fragments/GeneralSettings.java | 198 --------------------- .../de/blinkt/openvpn/fragments/GeneralSettings.kt | 160 +++++++++++++++++ 2 files changed, 160 insertions(+), 198 deletions(-) delete mode 100644 main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.java create mode 100644 main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt (limited to 'main/src') diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.java b/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.java deleted file mode 100644 index 35179f87..00000000 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Copyright (c) 2012-2016 Arne Schwabe - * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt - */ - -package de.blinkt.openvpn.fragments; - -import java.io.File; - -import android.app.AlertDialog; -import android.app.AlertDialog.Builder; -import android.app.Dialog; -import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; -import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; -import android.os.Build; -import android.os.Bundle; -import android.widget.Toast; - - -import androidx.preference.CheckBoxPreference; -import androidx.preference.ListPreference; -import androidx.preference.Preference; -import androidx.preference.PreferenceCategory; -import androidx.preference.PreferenceFragmentCompat; - -import de.blinkt.openvpn.BuildConfig; -import de.blinkt.openvpn.R; -import de.blinkt.openvpn.VpnProfile; -import de.blinkt.openvpn.activities.OpenSSLSpeed; -import de.blinkt.openvpn.api.ExternalAppDatabase; -import de.blinkt.openvpn.core.ProfileManager; - - -public class GeneralSettings extends PreferenceFragmentCompat implements Preference.OnPreferenceClickListener, OnClickListener, Preference.OnPreferenceChangeListener { - - private ExternalAppDatabase mExtapp; - private ListPreference mAlwaysOnVPN; - - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - - - // Load the preferences from an XML resource - addPreferencesFromResource(R.xml.general_settings); - - - PreferenceCategory devHacks = findPreference("device_hacks"); - mAlwaysOnVPN = findPreference("alwaysOnVpn"); - mAlwaysOnVPN.setOnPreferenceChangeListener(this); - - - Preference loadtun = findPreference("loadTunModule"); - if (!isTunModuleAvailable()) { - loadtun.setEnabled(false); - devHacks.removePreference(loadtun); - } - - CheckBoxPreference cm9hack = (CheckBoxPreference) findPreference("useCM9Fix"); - if (!cm9hack.isChecked() && (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1)) { - devHacks.removePreference(cm9hack); - } - - CheckBoxPreference useInternalFS = (CheckBoxPreference) findPreference("useInternalFileSelector"); - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { - devHacks.removePreference(useInternalFS); - } - - /* Android P does not allow access to the file storage anymore */ - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) { - Preference useInternalFileSelector = findPreference("useInternalFileSelector"); - devHacks.removePreference(useInternalFileSelector); - } - - mExtapp = new ExternalAppDatabase(getActivity()); - Preference clearapi = findPreference("clearapi"); - clearapi.setOnPreferenceClickListener(this); - - findPreference("osslspeed").setOnPreferenceClickListener(this); - - if (devHacks.getPreferenceCount() == 0) - getPreferenceScreen().removePreference(devHacks); - - if (!BuildConfig.openvpn3) { - PreferenceCategory appBehaviour = (PreferenceCategory) findPreference("app_behaviour"); - CheckBoxPreference ovpn3 = (CheckBoxPreference) findPreference("ovpn3"); - ovpn3.setEnabled(false); - ovpn3.setChecked(false); - } - - ((CheckBoxPreference)findPreference("restartvpnonboot")).setOnPreferenceChangeListener((pref, newValue) -> { - if (newValue.equals(true)) { - VpnProfile vpn = ProfileManager.getAlwaysOnVPN(requireActivity()); - if (vpn == null) { - Toast.makeText(requireContext(), R.string.no_default_vpn_set, Toast.LENGTH_LONG).show(); - return false; - } - - } - return true; - }); - - setClearApiSummary(); - } - - - @Override - public void onResume () { - super.onResume(); - - - VpnProfile vpn = ProfileManager.getAlwaysOnVPN(getActivity()); - StringBuffer sb = new StringBuffer(getString(R.string.defaultvpnsummary)); - sb.append('\n'); - if (vpn == null) - sb.append(getString(R.string.novpn_selected)); - else - sb.append(getString(R.string.vpnselected, vpn.getName())); - mAlwaysOnVPN.setSummary(sb.toString()); - - } - - @Override - public boolean onPreferenceChange (Preference preference, Object newValue){ - if (preference == mAlwaysOnVPN) { - VpnProfile vpn = ProfileManager.get(getActivity(), (String) newValue); - mAlwaysOnVPN.setSummary(vpn.getName()); - } - return true; - } - - private void setClearApiSummary () { - Preference clearapi = findPreference("clearapi"); - - if (mExtapp.getExtAppList().isEmpty()) { - clearapi.setEnabled(false); - clearapi.setSummary(R.string.no_external_app_allowed); - } else { - clearapi.setEnabled(true); - clearapi.setSummary(getString(R.string.allowed_apps, getExtAppList(", "))); - } - } - - private String getExtAppList (String delim){ - ApplicationInfo app; - PackageManager pm = getActivity().getPackageManager(); - - StringBuilder applist = new StringBuilder(); - for (String packagename : mExtapp.getExtAppList()) { - try { - app = pm.getApplicationInfo(packagename, 0); - if (applist.length() != 0) - applist.append(delim); - applist.append(app.loadLabel(pm)); - - } catch (NameNotFoundException e) { - // App not found. Remove it from the list - mExtapp.removeApp(packagename); - } - } - - return applist.toString(); - } - - private boolean isTunModuleAvailable () { - // Check if the tun module exists on the file system - return new File("/system/lib/modules/tun.ko").length() > 10; - } - - @Override - public boolean onPreferenceClick (Preference preference){ - if (preference.getKey().equals("clearapi")) { - Builder builder = new AlertDialog.Builder(getActivity()); - builder.setPositiveButton(R.string.clear, this); - builder.setNegativeButton(android.R.string.cancel, null); - builder.setMessage(getString(R.string.clearappsdialog, getExtAppList("\n"))); - builder.show(); - } else if (preference.getKey().equals("osslspeed")) { - startActivity(new Intent(getActivity(), OpenSSLSpeed.class)); - } - - return true; - } - - @Override - public void onClick (DialogInterface dialog,int which){ - if (which == Dialog.BUTTON_POSITIVE) { - mExtapp.clearAllApiApps(); - setClearApiSummary(); - } - } - - - } \ No newline at end of file diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt b/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt new file mode 100644 index 00000000..710adad1 --- /dev/null +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/GeneralSettings.kt @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2012-2016 Arne Schwabe + * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt + */ +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.os.Build +import android.os.Bundle +import android.widget.Toast +import androidx.preference.* +import de.blinkt.openvpn.BuildConfig +import de.blinkt.openvpn.R +import de.blinkt.openvpn.activities.OpenSSLSpeed +import de.blinkt.openvpn.api.ExternalAppDatabase +import de.blinkt.openvpn.core.ProfileManager +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) { + + + // Load the preferences from an XML resource + addPreferencesFromResource(R.xml.general_settings) + val devHacks = findPreference("device_hacks") + mAlwaysOnVPN = findPreference("alwaysOnVpn") + mAlwaysOnVPN!!.onPreferenceChangeListener = this + val loadtun = findPreference("loadTunModule") + if (!isTunModuleAvailable) { + loadtun!!.isEnabled = false + devHacks!!.removePreference(loadtun) + } + val cm9hack = findPreference("useCM9Fix") as CheckBoxPreference? + if (!cm9hack!!.isChecked && Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) { + devHacks!!.removePreference(cm9hack) + } + val useInternalFS = + findPreference("useInternalFileSelector") as CheckBoxPreference? + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { + 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("useInternalFileSelector") + devHacks!!.removePreference(useInternalFileSelector) + } + mExtapp = ExternalAppDatabase(activity) + val clearapi = findPreference("clearapi") + clearapi!!.onPreferenceClickListener = this + findPreference("osslspeed")!!.onPreferenceClickListener = this + if (devHacks!!.preferenceCount == 0) preferenceScreen.removePreference(devHacks) + if (!BuildConfig.openvpn3) { + val appBehaviour = findPreference("app_behaviour") as PreferenceCategory? + val ovpn3 = findPreference("ovpn3") as CheckBoxPreference? + ovpn3!!.isEnabled = false + ovpn3.isChecked = false + } + (findPreference("restartvpnonboot") as CheckBoxPreference?)!!.onPreferenceChangeListener = + Preference.OnPreferenceChangeListener { pref: Preference?, newValue: Any -> + if (newValue == true) { + val vpn = ProfileManager.getAlwaysOnVPN(requireActivity()) + if (vpn == null) { + Toast.makeText( + requireContext(), + R.string.no_default_vpn_set, + Toast.LENGTH_LONG + ).show() + return@setOnPreferenceChangeListener false + } + } + true + } + setClearApiSummary() + } + + override fun onResume() { + super.onResume() + val vpn = ProfileManager.getAlwaysOnVPN(activity) + val sb = StringBuffer(getString(R.string.defaultvpnsummary)) + sb.append('\n') + if (vpn == null) sb.append(getString(R.string.novpn_selected)) else sb.append( + getString( + R.string.vpnselected, + vpn.name + ) + ) + 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 + } + return true + } + + private fun setClearApiSummary() { + val clearapi = findPreference("clearapi") + if (mExtapp!!.extAppList.isEmpty()) { + clearapi!!.isEnabled = false + clearapi.setSummary(R.string.no_external_app_allowed) + } else { + 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 applist = StringBuilder() + 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) + } + } + return applist.toString() + } + + // 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 + File("/system/lib/modules/tun.ko").length() > 10 + + override fun onPreferenceClick(preference: Preference): Boolean { + if (preference.key == "clearapi") { + val builder = AlertDialog.Builder( + activity + ) + builder.setPositiveButton(R.string.clear, this) + builder.setNegativeButton(android.R.string.cancel, null) + builder.setMessage(getString(R.string.clearappsdialog, getExtAppList("\n"))) + builder.show() + } else if (preference.key == "osslspeed") { + startActivity(Intent(activity, OpenSSLSpeed::class.java)) + } + return true + } + + override fun onClick(dialog: DialogInterface, which: Int) { + if (which == Dialog.BUTTON_POSITIVE) { + mExtapp!!.clearAllApiApps() + setClearApiSummary() + } + } +} \ No newline at end of file -- cgit v1.2.3