From bc3af9b45a962721002e249806e0e9edd01e3e8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 18 Nov 2014 18:38:06 +0100 Subject: Extracted EipStatus, centralizing new state notifications. Observer pattern. The only notification is being received properly on EipServiceFragment is the connected one. I need to fix the other UI methods. --- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 22 +--- .../java/se/leap/bitmaskclient/eip/EipStatus.java | 133 +++++++++++++++++++++ 2 files changed, 136 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java (limited to 'app/src/main/java/se/leap/bitmaskclient/eip') diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java index 876ad172..97ef485a 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -46,21 +46,16 @@ import static se.leap.bitmaskclient.eip.Constants.*; public final class EIP extends IntentService { public final static String TAG = EIP.class.getSimpleName(); - public final static String SERVICE_API_PATH = "config/eip-service.json"; - private static SharedPreferences preferences; - private static Context context; private static ResultReceiver mReceiver; - private static boolean mBound = false; + private static SharedPreferences preferences; - private static int parsedEipSerial; private static JSONObject eip_definition = null; - - private static Gateway activeGateway = null; private static List gateways = new ArrayList(); - ProfileManager profile_manager; + private static ProfileManager profile_manager; + private static Gateway activeGateway = null; public static VpnStatus.ConnectionStatus lastConnectionStatusLevel; public static boolean mIsDisconnecting = false; @@ -81,15 +76,6 @@ public final class EIP extends IntentService { refreshEipDefinition(); } - @Override - public void onDestroy() { - - mBound = false; - - super.onDestroy(); - } - - @Override protected void onHandleIntent(Intent intent) { String action = intent.getAction(); @@ -115,7 +101,6 @@ public final class EIP extends IntentService { private void startEIP() { earlyRoutes(); GatewaySelector gateway_selector = new GatewaySelector(gateways); - activeGateway = gateway_selector.select(); if(activeGateway != null && activeGateway.getProfile() != null) { mReceiver = EipServiceFragment.getReceiver(); @@ -137,7 +122,6 @@ public final class EIP extends IntentService { Intent intent = new Intent(this,LaunchVPN.class); intent.setAction(Intent.ACTION_MAIN); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.putExtra(LaunchVPN.EXTRA_KEY, activeGateway.getProfile().getUUID().toString() ); intent.putExtra(LaunchVPN.EXTRA_NAME, activeGateway.getProfile().getName() ); intent.putExtra(LaunchVPN.EXTRA_HIDELOG, true); intent.putExtra(RECEIVER_TAG, mReceiver); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java new file mode 100644 index 00000000..c58650ba --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -0,0 +1,133 @@ +/** + * Copyright (c) 2013 LEAP Encryption Access Project and contributers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package se.leap.bitmaskclient.eip; + +import android.util.Log; +import java.util.*; + +import de.blinkt.openvpn.core.*; + +public class EipStatus extends Observable implements VpnStatus.StateListener { + public static String TAG = EipStatus.class.getSimpleName(); + private static EipStatus current_status; + + private static EipStatus previous_status; + private static VpnStatus.ConnectionStatus level = VpnStatus.ConnectionStatus.LEVEL_NOTCONNECTED; + private static boolean wants_to_disconnect = false; + private static boolean is_disconnecting = false; + private static boolean is_connecting = false; + + private String state, log_message; + private int localized_res_id; + + public static EipStatus getInstance() { + if(current_status == null) { + current_status = new EipStatus(); + VpnStatus.addStateListener(current_status); + } + return current_status; + } + + private EipStatus() { } + + @Override + public void updateState(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) { + current_status = getInstance(); + previous_status = current_status; + current_status.setState(state); + current_status.setLogMessage(logmessage); + current_status.setLocalizedResId(localizedResId); + current_status.setLevel(level); + current_status.setChanged(); + Log.d(TAG, "update state with level " + level); + current_status.notifyObservers(); + } + + public boolean isDisconnecting() { + return is_disconnecting; + } + + public boolean isConnecting() { + return is_connecting; + } + + public boolean wantsToDisconnect() { + return wants_to_disconnect; + } + + public boolean isConnected() { + return level == VpnStatus.ConnectionStatus.LEVEL_CONNECTED; + } + + public boolean isDisconnected() { + return level == VpnStatus.ConnectionStatus.LEVEL_NOTCONNECTED || level == VpnStatus.ConnectionStatus.LEVEL_AUTH_FAILED; + } + + public void setConnecting() { + is_connecting = true; + is_disconnecting = false; + wants_to_disconnect = false; + } + + public void setDisconnecting() { + is_disconnecting = true; + is_connecting = false; + wants_to_disconnect = false; + level = VpnStatus.ConnectionStatus.UNKNOWN_LEVEL; // Wait for the decision of the user + } + + public void setWantsToDisconnect() { + wants_to_disconnect = true; + } + + public String getState() { + return state; + } + + public String getLogMessage() { + return log_message; + } + + public int getLocalizedResId() { + return localized_res_id; + } + + public VpnStatus.ConnectionStatus getLevel() { + return level; + } + + public EipStatus getPreviousStatus() { + return previous_status; + } + + private void setState(String state) { + this.state = state; + } + + private void setLogMessage(String log_message) { + this.log_message = log_message; + } + + private void setLocalizedResId(int localized_res_id) { + this.localized_res_id = localized_res_id; + } + + private void setLevel(VpnStatus.ConnectionStatus level) { + this.level = level; + } + +} -- cgit v1.2.3