diff options
author | Parménides GV <parmegv@sdf.org> | 2015-04-22 14:20:09 +0200 |
---|---|---|
committer | Parménides GV <parmegv@sdf.org> | 2015-04-22 14:20:09 +0200 |
commit | 449c3953ce79947554bbfd6ed0386d68aa9f04bd (patch) | |
tree | 289724ea5320f22a63cc293f5bec96a61d203629 /app | |
parent | 1a1b49a36e6c8ddccdb2a3308b6739601a691efe (diff) |
Avoid the check for a null provider.
A provider can be asked if it's configured or not, so that we don't find
more provider = null issues.
I've created a DefaultedURL which links to https://example.net, so that
we can initialize an URL. I use composition instead of inheritance,
since URL class is final.
Diffstat (limited to 'app')
5 files changed, 70 insertions, 27 deletions
diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index aac53a07..6ab08a06 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -97,8 +97,7 @@ public class ConfigurationWizard extends Activity outState.putInt(PROGRESSBAR_NUMBER, mProgressBar.getProgress()); if (progressbar_description != null) outState.putString(PROGRESSBAR_TEXT, progressbar_description.getText().toString()); - if (selected_provider != null) - outState.putParcelable(Provider.KEY, selected_provider); + outState.putParcelable(Provider.KEY, selected_provider); super.onSaveInstanceState(outState); } @@ -125,8 +124,7 @@ public class ConfigurationWizard extends Activity progress = savedInstanceState.getInt(PROGRESSBAR_NUMBER, -1); if (fragment_manager.findFragmentByTag(ProviderDetailFragment.TAG) == null && setting_up_provider) { - if (selected_provider != null) - onItemSelectedUi(); + onItemSelectedUi(); if (progress > 0) mProgressBar.setProgress(progress); } @@ -393,7 +391,7 @@ public class ConfigurationWizard extends Activity public void setUpProvider(boolean danger_on) { Intent provider_API_command = new Intent(this, ProviderAPI.class); Bundle parameters = new Bundle(); - parameters.putString(Provider.MAIN_URL, selected_provider.mainUrl().toString()); + parameters.putString(Provider.MAIN_URL, selected_provider.mainUrl().getUrl().toString()); parameters.putBoolean(ProviderItem.DANGER_ON, danger_on); parameters.putString(Provider.CA_CERT_FINGERPRINT, selected_provider.certificatePin()); diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 921bf95b..ddd03dbc 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -68,7 +68,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn ProgressBar user_session_status_progress_bar; EipFragment eip_fragment; - private Provider provider; + private Provider provider = new Provider(); private UserSessionStatus user_session_status; public ProviderAPIResultReceiver providerAPI_result_receiver; private boolean switching_provider; @@ -89,7 +89,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn User.init(); restoreProvider(savedInstanceState); - if (provider == null || provider.getName().isEmpty()) + if (!provider.isConfigured()) startActivityForResult(new Intent(this, ConfigurationWizard.class), CONFIGURE_LEAP); else { buildDashboard(getIntent().getBooleanExtra(ON_BOOT, false)); @@ -102,7 +102,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn if (savedInstanceState.containsKey(Provider.KEY)) provider = savedInstanceState.getParcelable(Provider.KEY); } - if (provider == null && preferences.getBoolean(Constants.PROVIDER_CONFIGURED, false)) + if (!provider.isConfigured() && preferences.getBoolean(Constants.PROVIDER_CONFIGURED, false)) provider = getSavedProviderFromSharedPreferences(); } @@ -116,8 +116,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn @Override protected void onSaveInstanceState(@NotNull Bundle outState) { - if (provider != null) - outState.putParcelable(Provider.KEY, provider); + outState.putParcelable(Provider.KEY, provider); if (user_session_status_text_view != null && user_session_status_text_view.getVisibility() == TextView.VISIBLE) outState.putSerializable(UserSessionStatus.TAG, user_session_status.sessionStatus()); @@ -125,9 +124,9 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } private Provider getSavedProviderFromSharedPreferences() { - Provider provider = null; + Provider provider = new Provider(); try { - provider = new Provider(new URL(preferences.getString(Provider.MAIN_URL, ""))); + provider.setUrl(new URL(preferences.getString(Provider.MAIN_URL, ""))); provider.define(new JSONObject(preferences.getString(Provider.KEY, ""))); } catch (MalformedURLException | JSONException e) { e.printStackTrace(); diff --git a/app/src/main/java/se/leap/bitmaskclient/DefaultedURL.java b/app/src/main/java/se/leap/bitmaskclient/DefaultedURL.java new file mode 100644 index 00000000..8daa7d8c --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/DefaultedURL.java @@ -0,0 +1,37 @@ +package se.leap.bitmaskclient; + +import java.net.*; + +public class DefaultedURL { + private URL DEFAULT_URL; + private String default_url = "https://example.net"; + + private URL url; + + public DefaultedURL() { + try { + DEFAULT_URL = new URL(default_url); + url = DEFAULT_URL; + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + + public boolean isDefault() { return url.equals(DEFAULT_URL); } + + public void setUrl(URL url) { + this.url = url; + } + + public String getDomain() { + return url.getHost(); + } + + public URL getUrl() { + return url; + } + + public String toString() { + return url.toString(); + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index a030927d..a66d53e7 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -30,8 +30,8 @@ import java.util.*; */ public final class Provider implements Parcelable { - private JSONObject definition; // Represents our Provider's provider.json - private URL main_url; + private JSONObject definition = new JSONObject(); // Represents our Provider's provider.json + private DefaultedURL main_url = new DefaultedURL(); private String certificate_pin = ""; final public static String @@ -59,12 +59,14 @@ public final class Provider implements Parcelable { private static final String API_TERM_DEFAULT_LANGUAGE = "default_language"; protected static final String[] API_EIP_TYPES = {"openvpn"}; + public Provider() { } + public Provider(URL main_url) { - this.main_url = main_url; + this.main_url.setUrl(main_url); } public Provider(URL main_url, String certificate_pin) { - this.main_url = main_url; + this.main_url.setUrl(main_url); this.certificate_pin = certificate_pin; } @@ -81,7 +83,7 @@ public final class Provider implements Parcelable { private Provider(Parcel in) { try { - main_url = new URL(in.readString()); + main_url.setUrl(new URL(in.readString())); String definition_string = in.readString(); if (!definition_string.isEmpty()) definition = new JSONObject((definition_string)); @@ -90,6 +92,14 @@ public final class Provider implements Parcelable { } } + public boolean isConfigured() { + return !main_url.isDefault() && definition.length() > 0; + } + + protected void setUrl(URL url) { + main_url.setUrl(url); + } + protected void define(JSONObject provider_json) { definition = provider_json; } @@ -99,10 +109,10 @@ public final class Provider implements Parcelable { } protected String getDomain() { - return main_url.getHost(); + return main_url.getDomain(); } - protected URL mainUrl() { + protected DefaultedURL mainUrl() { return main_url; } @@ -118,7 +128,7 @@ public final class Provider implements Parcelable { else throw new JSONException("Provider not defined"); } catch (JSONException e) { if (main_url != null) { - String host = main_url.getHost(); + String host = main_url.getDomain(); name = host.substring(0, host.indexOf(".")); } } @@ -181,7 +191,8 @@ public final class Provider implements Parcelable { @Override public void writeToParcel(Parcel parcel, int i) { - parcel.writeString(main_url.toString()); + if(main_url != null) + parcel.writeString(main_url.toString()); if (definition != null) parcel.writeString(definition.toString()); } @@ -190,7 +201,7 @@ public final class Provider implements Parcelable { public boolean equals(Object o) { if (o instanceof Provider) { Provider p = (Provider) o; - return p.mainUrl().getHost().equals(mainUrl().getHost()); + return p.mainUrl().getDomain().equals(mainUrl().getDomain()); } else return false; } @@ -206,6 +217,6 @@ public final class Provider implements Parcelable { @Override public int hashCode() { - return mainUrl().getHost().hashCode(); + return mainUrl().getDomain().hashCode(); } } diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java index 68ff9e47..2742e1e9 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -98,8 +98,7 @@ public class ConfigurationWizard extends Activity outState.putInt(PROGRESSBAR_NUMBER, mProgressBar.getProgress()); if (progressbar_description != null) outState.putString(PROGRESSBAR_TEXT, progressbar_description.getText().toString()); - if (selected_provider != null) - outState.putParcelable(Provider.KEY, selected_provider); + outState.putParcelable(Provider.KEY, selected_provider); super.onSaveInstanceState(outState); } @@ -125,8 +124,7 @@ public class ConfigurationWizard extends Activity selected_provider = savedInstanceState.getParcelable(Provider.KEY); if (fragment_manager.findFragmentByTag(ProviderDetailFragment.TAG) == null && setting_up_provider) { - if (selected_provider != null) - onItemSelectedUi(); + onItemSelectedUi(); if (progress > 0) mProgressBar.setProgress(progress); } |