diff options
author | Parménides GV <parmegv@sdf.org> | 2014-11-18 18:38:06 +0100 |
---|---|---|
committer | Parménides GV <parmegv@sdf.org> | 2014-11-26 13:00:28 +0100 |
commit | bc3af9b45a962721002e249806e0e9edd01e3e8b (patch) | |
tree | 96e49a884b7b4315f4812469cd1c34f6febfe937 /app/src/main/java | |
parent | 06bc3b1898e1a419693c7fc3d6a48322ad6881e6 (diff) |
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.
Diffstat (limited to 'app/src/main/java')
3 files changed, 169 insertions, 66 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java index f35a3cfa..675619ca 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipServiceFragment.java @@ -13,8 +13,9 @@ import android.os.*; import android.util.Log; import android.view.*; import android.widget.*; +import java.util.*; -public class EipServiceFragment extends Fragment implements VpnStatus.StateListener, CompoundButton.OnCheckedChangeListener { +public class EipServiceFragment extends Fragment implements Observer, CompoundButton.OnCheckedChangeListener { protected static final String IS_EIP_PENDING = "is_eip_pending"; public static final String START_ON_BOOT = "start on boot"; @@ -23,6 +24,7 @@ public class EipServiceFragment extends Fragment implements VpnStatus.StateListe private static Switch eipSwitch; private View eipDetail; private TextView eipStatus; + private EipStatus eip_status; private static EIPReceiver mEIPReceiver; @@ -57,7 +59,7 @@ public class EipServiceFragment extends Fragment implements VpnStatus.StateListe @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - + EipStatus.getInstance().addObserver(this); mEIPReceiver = new EIPReceiver(new Handler()); if (savedInstanceState != null) @@ -66,18 +68,8 @@ public class EipServiceFragment extends Fragment implements VpnStatus.StateListe @Override public void onResume() { - super.onResume(); - - VpnStatus.addStateListener(this); - - eipCommand(Constants.ACTION_CHECK_CERT_VALIDITY); - } - - @Override - public void onPause() { - super.onPause(); - - VpnStatus.removeStateListener(this); + super.onResume(); + eipCommand(Constants.ACTION_CHECK_CERT_VALIDITY); } @Override @@ -205,47 +197,43 @@ public class EipServiceFragment extends Fragment implements VpnStatus.StateListe } @Override - public void updateState(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) { - boolean isNewLevel = EIP.lastConnectionStatusLevel != level; - boolean justDecidedOnDisconnect = EIP.lastConnectionStatusLevel == VpnStatus.ConnectionStatus.UNKNOWN_LEVEL; - Log.d(TAG, "update state with level " + level); - if(!justDecidedOnDisconnect && (isNewLevel || level == VpnStatus.ConnectionStatus.LEVEL_CONNECTED)) { - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - EIP.lastConnectionStatusLevel = level; - handleNewState(state, logmessage, localizedResId, level); - } - }); - } else if(justDecidedOnDisconnect && level == VpnStatus.ConnectionStatus.LEVEL_CONNECTED) { - EIP.lastConnectionStatusLevel = VpnStatus.ConnectionStatus.LEVEL_NOTCONNECTED; - updateState(state, logmessage, localizedResId, level); - } // else if(isNewLevel || level == ConnectionStatus.LEVEL_AUTH_FAILED) - // handleNewState(state, logmessage, localizedResId, level); + public void update (Observable observable, Object data) { + if(observable instanceof EipStatus) { + final EipStatus eip_status = (EipStatus) observable; + EipStatus previous_status = eip_status.getPreviousStatus(); + boolean isNewLevel = eip_status.getLevel() != previous_status.getLevel(); + if(!eip_status.wantsToDisconnect() && (isNewLevel || eip_status.isConnected())) { + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + handleNewState(eip_status); + } + }); + } else if(eip_status.wantsToDisconnect() && eip_status.isConnected()) { + setDisconnectedUI(); + // EIP.lastConnectionStatusLevel = VpnStatus.ConnectionStatus.LEVEL_NOTCONNECTED; + // updateState(state, logmessage, localizedResId, level); + } + } } - private void handleNewState(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) { + private void handleNewState(EipStatus eip_status) { + final String state = eip_status.getState(); + final String logmessage = eip_status.getLogMessage(); + final int localizedResId = eip_status.getLocalizedResId(); + final VpnStatus.ConnectionStatus level = eip_status.getLevel(); if (level == VpnStatus.ConnectionStatus.LEVEL_CONNECTED) setConnectedUI(); - else if (isDisconnectedLevel(level) && !EIP.mIsStarting) + else if (eip_status.isDisconnected() && !eip_status.isConnecting()) setDisconnectedUI(); else if (level == VpnStatus.ConnectionStatus.LEVEL_CONNECTING_NO_SERVER_REPLY_YET) setNoServerReplyUI(localizedResId, logmessage); else if (level == VpnStatus.ConnectionStatus.LEVEL_CONNECTING_SERVER_REPLIED) setServerReplyUI(state, localizedResId, logmessage); - // else if (level == VpnStatus.ConnectionStatus.LEVEL_AUTH_FAILED) - // handleSwitchOn(); - } - - private boolean isDisconnectedLevel(final VpnStatus.ConnectionStatus level) { - return level == VpnStatus.ConnectionStatus.LEVEL_NOTCONNECTED || level == VpnStatus.ConnectionStatus.LEVEL_AUTH_FAILED; } private void setConnectedUI() { hideProgressBar(); - Log.d(TAG, "mIsDisconnecting = false in setConnectedUI"); - EIP.mIsStarting = false; //TODO This should be done in the onReceiveResult from START_EIP command, but right now LaunchVPN isn't notifying anybody the resultcode of the request so we need to listen the states with this listener. - EIP.mIsDisconnecting = false; //TODO See comment above String status = getString(R.string.eip_state_connected); setEipStatus(status); adjustSwitch(); @@ -253,17 +241,15 @@ public class EipServiceFragment extends Fragment implements VpnStatus.StateListe private void setDisconnectedUI(){ hideProgressBar(); - EIP.mIsStarting = false; //TODO See comment in setConnectedUI() - Log.d(TAG, "mIsDisconnecting = false in setDisconnectedUI"); - EIP.mIsDisconnecting = false; //TODO See comment in setConnectedUI() String status = getString(R.string.eip_state_not_connected); setEipStatus(status); adjustSwitch(); } - private void adjustSwitch() { - if(EIP.isConnected()) { + private void adjustSwitch() { + EipStatus eip_status = EipStatus.getInstance(); + if(eip_status.isConnected()) { if(!eipSwitch.isChecked()) { eipSwitch.setChecked(true); } 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<Gateway> gateways = new ArrayList<Gateway>(); - 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 <http://www.gnu.org/licenses/>. + */ +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; + } + +} |