summaryrefslogtreecommitdiff
path: root/app/src/main/java/se
diff options
context:
space:
mode:
authorParménides GV <parmegv@sdf.org>2015-04-22 14:20:09 +0200
committerParménides GV <parmegv@sdf.org>2015-04-22 14:20:09 +0200
commit449c3953ce79947554bbfd6ed0386d68aa9f04bd (patch)
tree289724ea5320f22a63cc293f5bec96a61d203629 /app/src/main/java/se
parent1a1b49a36e6c8ddccdb2a3308b6739601a691efe (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/src/main/java/se')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/Dashboard.java13
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/DefaultedURL.java37
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/Provider.java33
3 files changed, 65 insertions, 18 deletions
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();
}
}