From 39e94d54313cf140929339a9f2613d79cbf734b0 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Mon, 14 May 2012 23:06:36 +0200 Subject: - Rework saving state of preferences fragment - fix state of nobind not loaded (hopefully closes issue #19) Version 0.5.4 --- src/de/blinkt/openvpn/ConfigConverter.java | 2 + .../blinkt/openvpn/OpenVpnPreferencesFragment.java | 52 ++++++++++++++++++++++ src/de/blinkt/openvpn/Settings_Authentication.java | 18 +++----- src/de/blinkt/openvpn/Settings_Basic.java | 24 +++++++--- src/de/blinkt/openvpn/Settings_IP.java | 23 +++------- src/de/blinkt/openvpn/Settings_Obscure.java | 16 ++----- src/de/blinkt/openvpn/VPNPreferences.java | 18 -------- src/de/blinkt/openvpn/VpnProfile.java | 5 +++ 8 files changed, 94 insertions(+), 64 deletions(-) create mode 100644 src/de/blinkt/openvpn/OpenVpnPreferencesFragment.java (limited to 'src/de/blinkt/openvpn') diff --git a/src/de/blinkt/openvpn/ConfigConverter.java b/src/de/blinkt/openvpn/ConfigConverter.java index 50d08b73..952ab968 100644 --- a/src/de/blinkt/openvpn/ConfigConverter.java +++ b/src/de/blinkt/openvpn/ConfigConverter.java @@ -44,6 +44,8 @@ public class ConfigConverter extends ListActivity { Intent result = new Intent(); ProfileManager vpl = ProfileManager.getInstance(this); vpl.addProfile(mResult); + vpl.saveProfile(this, mResult); + vpl.saveProfileList(this); result.putExtra(VpnProfile.EXTRA_PROFILEUUID,mResult.getUUID().toString()); setResult(Activity.RESULT_OK, result); finish(); diff --git a/src/de/blinkt/openvpn/OpenVpnPreferencesFragment.java b/src/de/blinkt/openvpn/OpenVpnPreferencesFragment.java new file mode 100644 index 00000000..10378a80 --- /dev/null +++ b/src/de/blinkt/openvpn/OpenVpnPreferencesFragment.java @@ -0,0 +1,52 @@ +package de.blinkt.openvpn; + +import android.os.Bundle; +import android.preference.PreferenceFragment; + +public abstract class OpenVpnPreferencesFragment extends PreferenceFragment { + + protected VpnProfile mProfile; + + protected abstract void loadSettings(); + protected abstract void saveSettings(); + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // Make sure there is an instance of the profile manager + ProfileManager.getInstance(getActivity()); + + String profileUUID = getArguments().getString(getActivity().getPackageName() + ".profileUUID"); + mProfile = ProfileManager.get(profileUUID); + } + + @Override + public void onPause() { + super.onPause(); + saveSettings(); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + if(savedInstanceState!=null) { + String profileUUID=savedInstanceState.getString(VpnProfile.EXTRA_PROFILEUUID); + ProfileManager.getInstance(getActivity()); + mProfile = ProfileManager.get(profileUUID); + loadSettings(); + } + } + + @Override + public void onStop() { + // TODO Auto-generated method stub + super.onStop(); + } + + @Override + public void onSaveInstanceState (Bundle outState) { + super.onSaveInstanceState(outState); + saveSettings(); + outState.putString(VpnProfile.EXTRA_PROFILEUUID, mProfile.getUUIDString()); + } +} diff --git a/src/de/blinkt/openvpn/Settings_Authentication.java b/src/de/blinkt/openvpn/Settings_Authentication.java index e8740b5d..6d6960e9 100644 --- a/src/de/blinkt/openvpn/Settings_Authentication.java +++ b/src/de/blinkt/openvpn/Settings_Authentication.java @@ -13,12 +13,11 @@ import android.preference.PreferenceFragment; import android.preference.SwitchPreference; -public class Settings_Authentication extends PreferenceFragment implements OnPreferenceChangeListener, OnPreferenceClickListener { +public class Settings_Authentication extends OpenVpnPreferencesFragment implements OnPreferenceChangeListener, OnPreferenceClickListener { private static final int SELECT_TLS_FILE = 23223232; private CheckBoxPreference mExpectTLSCert; private CheckBoxPreference mCheckRemoteCN; private EditTextPreference mRemoteCN; - private VpnProfile mProfile; private ListPreference mTLSAuthDirection; private Preference mTLSAuthFile; private SwitchPreference mUseTLSAuth; @@ -41,8 +40,7 @@ public class Settings_Authentication extends PreferenceFragment implements OnPre mTLSAuthFile = findPreference("tlsAuthFile"); mTLSAuthDirection = (ListPreference) findPreference("tls_direction"); - String profileUUID = getArguments().getString(getActivity().getPackageName() + ".profileUUID"); - mProfile = ProfileManager.get(profileUUID); + mTLSAuthFile.setOnPreferenceClickListener(this); mCipher =(EditTextPreference) findPreference("cipher"); @@ -52,7 +50,8 @@ public class Settings_Authentication extends PreferenceFragment implements OnPre } - private void loadSettings() { + @Override + protected void loadSettings() { mExpectTLSCert.setChecked(mProfile.mExpectTLSCert); mCheckRemoteCN.setChecked(mProfile.mCheckRemoteCN); @@ -67,7 +66,8 @@ public class Settings_Authentication extends PreferenceFragment implements OnPre onPreferenceChange(mCipher, mProfile.mCipher); } - private void saveSettings() { + @Override + protected void saveSettings() { mProfile.mExpectTLSCert=mExpectTLSCert.isChecked(); mProfile.mCheckRemoteCN=mCheckRemoteCN.isChecked(); mProfile.mRemoteCN=mRemoteCN.getText(); @@ -87,11 +87,7 @@ public class Settings_Authentication extends PreferenceFragment implements OnPre } - @Override - public void onPause() { - super.onPause(); - saveSettings(); - } + @Override public boolean onPreferenceChange(Preference preference, Object newValue) { diff --git a/src/de/blinkt/openvpn/Settings_Basic.java b/src/de/blinkt/openvpn/Settings_Basic.java index 3234d0df..3eb484cb 100644 --- a/src/de/blinkt/openvpn/Settings_Basic.java +++ b/src/de/blinkt/openvpn/Settings_Basic.java @@ -93,9 +93,9 @@ public class Settings_Basic extends Fragment implements View.OnClickListener, On public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); String profileuuid =getArguments().getString(getActivity().getPackageName() + ".profileUUID"); mProfile=ProfileManager.get(profileuuid); - super.onCreate(savedInstanceState); } @Override @@ -129,8 +129,7 @@ public class Settings_Basic extends Fragment implements View.OnClickListener, On addFileSelectLayout(mpkcs12); mpkcs12.setNoline(); - loadPreferences(); - + mType.setOnItemSelectedListener(this); mView.findViewById(R.id.select_keystore_button).setOnClickListener(this); @@ -143,6 +142,15 @@ public class Settings_Basic extends Fragment implements View.OnClickListener, On return mView; } + + @Override + public void onStart() { + super.onStart(); + String profileuuid =getArguments().getString(getActivity().getPackageName() + ".profileUUID"); + mProfile=ProfileManager.get(profileuuid); + loadPreferences(); + + } @Override public void onActivityResult(int request, int result, Intent data) { @@ -158,7 +166,6 @@ public class Settings_Basic extends Fragment implements View.OnClickListener, On @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - ((VPNPreferences) getActivity()).setmBS(this); } @Override @@ -167,6 +174,11 @@ public class Settings_Basic extends Fragment implements View.OnClickListener, On changeType(position); } } + @Override + public void onPause() { + super.onPause(); + savePreferences(); + } @@ -298,7 +310,9 @@ public class Settings_Basic extends Fragment implements View.OnClickListener, On public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); savePreferences(); - outState.putString(getActivity().getPackageName() + "profileUUID", mProfile.getUUID().toString()); + if(mProfile!=null) { + outState.putString(getActivity().getPackageName() + "profileUUID", mProfile.getUUID().toString()); + } } @Override diff --git a/src/de/blinkt/openvpn/Settings_IP.java b/src/de/blinkt/openvpn/Settings_IP.java index 52dcc5dc..dcdc10c0 100644 --- a/src/de/blinkt/openvpn/Settings_IP.java +++ b/src/de/blinkt/openvpn/Settings_IP.java @@ -8,7 +8,7 @@ import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.preference.SwitchPreference; -public class Settings_IP extends PreferenceFragment implements OnPreferenceChangeListener { +public class Settings_IP extends OpenVpnPreferencesFragment implements OnPreferenceChangeListener { private EditTextPreference mIPv4; private EditTextPreference mIPv6; private SwitchPreference mUsePull; @@ -18,7 +18,6 @@ public class Settings_IP extends PreferenceFragment implements OnPreferenceChang private EditTextPreference mDNS2; private EditTextPreference mCustomRoutes; private CheckBoxPreference mUseDefaultRoute; - private VpnProfile mProfile; private CheckBoxPreference mRouteNoPull; private CheckBoxPreference mNobind; @@ -26,10 +25,7 @@ public class Settings_IP extends PreferenceFragment implements OnPreferenceChang public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - String profileUUID = getArguments().getString(getActivity().getPackageName() + ".profileUUID"); - mProfile = ProfileManager.get(profileUUID); - - + // 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. @@ -62,7 +58,8 @@ public class Settings_IP extends PreferenceFragment implements OnPreferenceChang loadSettings(); } - private void loadSettings() { + @Override + protected void loadSettings() { mUsePull.setChecked(mProfile.mUsePull); mIPv4.setText(mProfile.mIPv4Address); @@ -74,6 +71,7 @@ public class Settings_IP extends PreferenceFragment implements OnPreferenceChang mUseDefaultRoute.setChecked(mProfile.mUseDefaultRoute); mCustomRoutes.setText(mProfile.mCustomRoutes); mRouteNoPull.setChecked(mProfile.mRoutenopull); + mNobind.setChecked(mProfile.mNobind); // Sets Summary onPreferenceChange(mIPv4, mIPv4.getText()); @@ -86,18 +84,9 @@ public class Settings_IP extends PreferenceFragment implements OnPreferenceChang setDNSState(); } - public void onSaveInstanceState (Bundle outState) { - saveSettings(); - } @Override - public void onStop() { - saveSettings(); - super.onStop(); - - } - - private void saveSettings() { + protected void saveSettings() { mProfile.mUsePull = mUsePull.isChecked(); mProfile.mIPv4Address = mIPv4.getText(); mProfile.mIPv6Address = mIPv6.getText(); diff --git a/src/de/blinkt/openvpn/Settings_Obscure.java b/src/de/blinkt/openvpn/Settings_Obscure.java index fd3b540d..aed61392 100644 --- a/src/de/blinkt/openvpn/Settings_Obscure.java +++ b/src/de/blinkt/openvpn/Settings_Obscure.java @@ -6,10 +6,8 @@ import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; -import android.preference.PreferenceFragment; -public class Settings_Obscure extends PreferenceFragment implements OnPreferenceChangeListener { - private VpnProfile mProfile; +public class Settings_Obscure extends OpenVpnPreferencesFragment implements OnPreferenceChangeListener { private CheckBoxPreference mUseRandomHostName; private CheckBoxPreference mUseFloat; private CheckBoxPreference mUseCustomConfig; @@ -22,9 +20,6 @@ public class Settings_Obscure extends PreferenceFragment implements OnPreference // Load the preferences from an XML resource addPreferencesFromResource(R.xml.vpn_obscure); - String profileUUID = getArguments().getString(getActivity().getPackageName() + ".profileUUID"); - mProfile = ProfileManager.get(profileUUID); - mUseRandomHostName = (CheckBoxPreference) findPreference("useRandomHostname"); mUseFloat = (CheckBoxPreference) findPreference("useFloat"); @@ -39,7 +34,7 @@ public class Settings_Obscure extends PreferenceFragment implements OnPreference } - private void loadSettings() { + protected void loadSettings() { mUseRandomHostName.setChecked(mProfile.mUseRandomHostname); mUseFloat.setChecked(mProfile.mUseFloat); mUseCustomConfig.setChecked(mProfile.mUseCustomConfig); @@ -49,13 +44,8 @@ public class Settings_Obscure extends PreferenceFragment implements OnPreference onPreferenceChange(mLogverbosity, mProfile.mVerb); } - @Override - public void onPause() { - saveSettings(); - super.onPause(); - } - private void saveSettings() { + protected void saveSettings() { mProfile.mUseRandomHostname = mUseRandomHostName.isChecked(); mProfile.mUseFloat = mUseFloat.isChecked(); mProfile.mUseCustomConfig = mUseCustomConfig.isChecked(); diff --git a/src/de/blinkt/openvpn/VPNPreferences.java b/src/de/blinkt/openvpn/VPNPreferences.java index e1c8a401..f128270a 100644 --- a/src/de/blinkt/openvpn/VPNPreferences.java +++ b/src/de/blinkt/openvpn/VPNPreferences.java @@ -9,10 +9,6 @@ import android.preference.PreferenceActivity; public class VPNPreferences extends PreferenceActivity { private String mProfileUUID; - private Settings_Basic mBS; - public void setmBS(Settings_Basic mBS) { - this.mBS = mBS; - } public VPNPreferences() { super(); @@ -20,13 +16,7 @@ public class VPNPreferences extends PreferenceActivity { } - protected void onPause() { - super.onPause(); - if(mBS!=null) - mBS.savePreferences(); - } - @Override protected void onStop() { @@ -39,14 +29,6 @@ public class VPNPreferences extends PreferenceActivity { protected void onCreate(Bundle savedInstanceState) { mProfileUUID = getIntent().getStringExtra(getPackageName() + ".profileUUID"); super.onCreate(savedInstanceState); - - - - /* if (hasHeaders()) { - Button button = new Button(this); - button.setText("Save"); - setListFooter(button); - } */ } diff --git a/src/de/blinkt/openvpn/VpnProfile.java b/src/de/blinkt/openvpn/VpnProfile.java index b5e421fb..a0f6db76 100644 --- a/src/de/blinkt/openvpn/VpnProfile.java +++ b/src/de/blinkt/openvpn/VpnProfile.java @@ -557,6 +557,11 @@ public class VpnProfile implements Serializable{ } + public String getUUIDString() { + return mUuid.toString(); + } + + } -- cgit v1.2.3