From af4a789df914732e65b05dae582ed21eed0d91b7 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sun, 24 Oct 2021 18:14:18 +0200 Subject: Convert IP settings to Kotlin and use autosummary provider --- .../de/blinkt/openvpn/fragments/Settings_IP.java | 150 --------------------- .../de/blinkt/openvpn/fragments/Settings_IP.kt | 106 +++++++++++++++ main/src/ui/res/xml/vpn_ipsettings.xml | 71 +++++----- 3 files changed, 144 insertions(+), 183 deletions(-) delete mode 100644 main/src/ui/java/de/blinkt/openvpn/fragments/Settings_IP.java create mode 100644 main/src/ui/java/de/blinkt/openvpn/fragments/Settings_IP.kt (limited to 'main/src') diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_IP.java b/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_IP.java deleted file mode 100644 index 843ff750..00000000 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_IP.java +++ /dev/null @@ -1,150 +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 android.os.Bundle; - -import androidx.preference.CheckBoxPreference; -import androidx.preference.EditTextPreference; -import androidx.preference.Preference; -import androidx.preference.PreferenceManager; -import androidx.preference.SwitchPreference; - -import de.blinkt.openvpn.R; -import de.blinkt.openvpn.VpnProfile; - -public class Settings_IP extends OpenVpnPreferencesFragment implements Preference.OnPreferenceChangeListener { - private EditTextPreference mIPv4; - private EditTextPreference mIPv6; - private SwitchPreference mUsePull; - private CheckBoxPreference mOverrideDNS; - private EditTextPreference mSearchdomain; - private EditTextPreference mDNS1; - private EditTextPreference mDNS2; - private CheckBoxPreference mNobind; - - @Override - public void onResume() - { - super.onResume(); - - - // Make sure default values are applied. In a real app, you would - // want this in a shared function that is used to retrieve the - // SharedPreferences wherever they are needed. - PreferenceManager.setDefaultValues(requireActivity(), - R.xml.vpn_ipsettings, false); - - // Load the preferences from an XML resource - addPreferencesFromResource(R.xml.vpn_ipsettings); - mIPv4 = findPreference("ipv4_address"); - mIPv6 = findPreference("ipv6_address"); - mUsePull = findPreference("usePull"); - mOverrideDNS = findPreference("overrideDNS"); - mSearchdomain = findPreference("searchdomain"); - mDNS1 = findPreference("dns1"); - mDNS2 = findPreference("dns2"); - mNobind = findPreference("nobind"); - - mIPv4.setOnPreferenceChangeListener(this); - mIPv6.setOnPreferenceChangeListener(this); - mDNS1.setOnPreferenceChangeListener(this); - mDNS2.setOnPreferenceChangeListener(this); - mUsePull.setOnPreferenceChangeListener(this); - mOverrideDNS.setOnPreferenceChangeListener(this); - mSearchdomain.setOnPreferenceChangeListener(this); - - loadSettings(); - } - - @Override - protected void loadSettings() { - - if (mProfile.mAuthenticationType == VpnProfile.TYPE_STATICKEYS) - mUsePull.setEnabled(false); - else - mUsePull.setChecked(mProfile.mUsePull); - mIPv4.setText(mProfile.mIPv4Address); - mIPv6.setText(mProfile.mIPv6Address); - mDNS1.setText(mProfile.mDNS1); - mDNS2.setText(mProfile.mDNS2); - mOverrideDNS.setChecked(mProfile.mOverrideDNS); - mSearchdomain.setText(mProfile.mSearchDomain); - mNobind.setChecked(mProfile.mNobind); - - - //mUsePull.setEnabled(mProfile.mAuthenticationType != VpnProfile.TYPE_STATICKEYS); - mUsePull.setEnabled(true); - - if (mProfile.mAuthenticationType == VpnProfile.TYPE_STATICKEYS) - mUsePull.setChecked(false); - - // Sets Summary - onPreferenceChange(mIPv4, mIPv4.getText()); - onPreferenceChange(mIPv6, mIPv6.getText()); - onPreferenceChange(mDNS1, mDNS1.getText()); - onPreferenceChange(mDNS2, mDNS2.getText()); - onPreferenceChange(mSearchdomain, mSearchdomain.getText()); - - setDNSState(); - } - - - @Override - protected void saveSettings() { - mProfile.mUsePull = mUsePull.isChecked(); - mProfile.mIPv4Address = mIPv4.getText(); - mProfile.mIPv6Address = mIPv6.getText(); - mProfile.mDNS1 = mDNS1.getText(); - mProfile.mDNS2 = mDNS2.getText(); - mProfile.mOverrideDNS = mOverrideDNS.isChecked(); - mProfile.mSearchDomain = mSearchdomain.getText(); - mProfile.mNobind = mNobind.isChecked(); - - } - - @Override - public boolean onPreferenceChange(Preference preference, - Object newValue) { - if(preference==mIPv4 || preference == mIPv6 - || preference==mDNS1 || preference == mDNS2 - || preference == mSearchdomain - ) - - preference.setSummary((String)newValue); - - if(preference== mUsePull || preference == mOverrideDNS) - if(preference==mOverrideDNS) { - // Set so the function gets the right value - mOverrideDNS.setChecked((Boolean) newValue); - } - setDNSState(); - - saveSettings(); - return true; - } - - private void setDNSState() { - boolean enabled; - mOverrideDNS.setEnabled(mUsePull.isChecked()); - if(!mUsePull.isChecked()) - enabled =true; - else - enabled = mOverrideDNS.isChecked(); - - mDNS1.setEnabled(enabled); - mDNS2.setEnabled(enabled); - mSearchdomain.setEnabled(enabled); - - - } - - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - - } -} \ No newline at end of file diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_IP.kt b/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_IP.kt new file mode 100644 index 00000000..d2bf4b74 --- /dev/null +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_IP.kt @@ -0,0 +1,106 @@ +/* + * 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.os.Bundle +import androidx.preference.* +import de.blinkt.openvpn.R +import de.blinkt.openvpn.fragments.OpenVpnPreferencesFragment +import de.blinkt.openvpn.VpnProfile + +class Settings_IP : OpenVpnPreferencesFragment(), Preference.OnPreferenceChangeListener { + private lateinit var mIPv4: EditTextPreference + private lateinit var mIPv6: EditTextPreference + private lateinit var mUsePull: SwitchPreference + private lateinit var mOverrideDNS: CheckBoxPreference + private lateinit var mSearchdomain: EditTextPreference + private lateinit var mDNS1: EditTextPreference + private lateinit var mDNS2: EditTextPreference + private lateinit var mNobind: CheckBoxPreference + override fun onResume() { + super.onResume() + + + // Make sure default values are applied. In a real app, you would + // want this in a shared function that is used to retrieve the + // SharedPreferences wherever they are needed. + PreferenceManager.setDefaultValues( + requireActivity(), + R.xml.vpn_ipsettings, false + ) + + // Load the preferences from an XML resource + addPreferencesFromResource(R.xml.vpn_ipsettings) + mIPv4 = findPreference("ipv4_address")!! + mIPv6 = findPreference("ipv6_address")!! + mUsePull = findPreference("usePull")!! + mOverrideDNS = findPreference("overrideDNS")!! + mSearchdomain = findPreference("searchdomain")!! + mDNS1 = findPreference("dns1")!! + mDNS2 = findPreference("dns2")!! + mNobind = findPreference("nobind")!! + mUsePull.onPreferenceChangeListener = this + mOverrideDNS.onPreferenceChangeListener = this + loadSettings() + } + + override fun loadSettings() { + if (mProfile.mAuthenticationType == VpnProfile.TYPE_STATICKEYS) mUsePull.isEnabled = + false else mUsePull.isChecked = mProfile.mUsePull + mIPv4.text = mProfile.mIPv4Address + mIPv6.text = mProfile.mIPv6Address + mDNS1.text = mProfile.mDNS1 + mDNS2.text = mProfile.mDNS2 + mOverrideDNS.isChecked = mProfile.mOverrideDNS + mSearchdomain.text = mProfile.mSearchDomain + mNobind.isChecked = mProfile.mNobind + + + //mUsePull.setEnabled(mProfile.mAuthenticationType != VpnProfile.TYPE_STATICKEYS); + mUsePull.isEnabled = true + if (mProfile.mAuthenticationType == VpnProfile.TYPE_STATICKEYS) mUsePull.isChecked = false + + // Sets Summary + setDNSState() + } + + override fun saveSettings() { + mProfile.mUsePull = mUsePull.isChecked + mProfile.mIPv4Address = mIPv4.text + mProfile.mIPv6Address = mIPv6.text + mProfile.mDNS1 = mDNS1.text + mProfile.mDNS2 = mDNS2.text + mProfile.mOverrideDNS = mOverrideDNS.isChecked + mProfile.mSearchDomain = mSearchdomain.text + mProfile.mNobind = mNobind.isChecked + } + + override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean { + if (preference === mUsePull || preference === mOverrideDNS) + if (preference === mOverrideDNS) { + // Set so the function gets the right value + mOverrideDNS.isChecked = (newValue as Boolean) + } + setDNSState() + saveSettings() + return true + } + + override fun onPause() { + super.onPause() + saveSettings() + } + + private fun setDNSState() { + val enabled: Boolean + mOverrideDNS.isEnabled = mUsePull.isChecked + enabled = if (!mUsePull.isChecked) true else mOverrideDNS.isChecked + mDNS1.isEnabled = enabled + mDNS2.isEnabled = enabled + mSearchdomain.isEnabled = enabled + } + + override fun onCreatePreferences(savedInstanceState: Bundle, rootKey: String) {} +} \ No newline at end of file diff --git a/main/src/ui/res/xml/vpn_ipsettings.xml b/main/src/ui/res/xml/vpn_ipsettings.xml index 5df7ffa8..2b03c694 100644 --- a/main/src/ui/res/xml/vpn_ipsettings.xml +++ b/main/src/ui/res/xml/vpn_ipsettings.xml @@ -1,53 +1,58 @@ - - - + + android:disableDependentsState="true" + android:key="usePull" + android:summaryOff="@string/pull_off_summary" + android:summaryOn="@string/pull_on_summary" + android:title="@string/use_pull" /> - + + android:dependency="usePull" + android:dialogMessage="@string/ipv4_dialog_title" + android:key="ipv4_address" + android:title="@string/ipv4_address" + app:useSimpleSummaryProvider="true" /> + android:dependency="usePull" + android:dialogMessage="@string/ipv6_dialog_tile" + android:key="ipv6_address" + android:title="@string/ipv6_address" + app:useSimpleSummaryProvider="true" /> + android:key="nobind" + android:persistent="false" + android:summary="@string/nobind_summary" + android:title="@string/no_bind" /> - + + android:key="overrideDNS" + android:summary="@string/dns_override_summary" + android:title="@string/override_dns" /> + android:key="searchdomain" + android:title="@string/searchdomain" + app:useSimpleSummaryProvider="true" /> + android:dialogMessage="@string/dns1_summary" + android:key="dns1" + android:title="@string/dns_server" + app:useSimpleSummaryProvider="true" /> + android:dialogMessage="@string/secondary_dns_message" + android:key="dns2" + android:title="@string/backup_dns" + app:useSimpleSummaryProvider="true" /> \ No newline at end of file -- cgit v1.2.3