From d486768d6b0664d9032605897c8f1847ef4e3a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 20 Apr 2015 18:20:05 +0200 Subject: Remove textual status. I had a difficult moment trying to detect if the LogWindow had already been shown after an error. Finally, I implemented a LogWindowWrapper which contains a field for the reason it was shown, so that we can check if the previous error is the same than the current one leading to the avoidance of the second LogWindow. For this to work, we need to reset that reason each time we trigger a new vpn state (i.e. each time the user taps on the vpn icon). --- .../main/java/se/leap/bitmaskclient/Dashboard.java | 13 +------ .../java/se/leap/bitmaskclient/EipFragment.java | 45 ++++++---------------- .../se/leap/bitmaskclient/LogWindowWrapper.java | 41 ++++++++++++++++++++ .../java/se/leap/bitmaskclient/eip/EipStatus.java | 15 ++++++-- .../se/leap/bitmaskclient/eip/VoidVpnService.java | 4 ++ 5 files changed, 70 insertions(+), 48 deletions(-) create mode 100644 app/src/main/java/se/leap/bitmaskclient/LogWindowWrapper.java (limited to 'app/src/main/java/se') diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index d5d06322..921bf95b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -285,8 +285,8 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } public void showLog() { - Intent startLW = new Intent(getContext(), LogWindow.class); - startActivity(startLW); + LogWindowWrapper log_window_wrapper = LogWindowWrapper.getInstance(getContext()); + log_window_wrapper.showLog(); } @Override @@ -372,10 +372,6 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } protected void providerApiCommand(Bundle parameters, int progressbar_message_resId, String providerApi_action) { - if (eip_fragment != null && progressbar_message_resId != 0) { - setStatusMessage(progressbar_message_resId); - } - Intent command = prepareProviderAPICommand(parameters, providerApi_action); startService(command); } @@ -449,11 +445,6 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } } - private void setStatusMessage(int string_resId) { - if (eip_fragment != null && eip_fragment.status_message != null) - eip_fragment.status_message.setText(string_resId); - } - public static Context getContext() { return app; } diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index bed61d24..c92ae3cd 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -20,7 +20,6 @@ import android.app.*; import android.content.*; import android.os.*; import android.view.*; -import android.widget.*; import org.jetbrains.annotations.*; @@ -37,11 +36,9 @@ public class EipFragment extends Fragment implements Observer { protected static final String IS_PENDING = TAG + ".is_pending"; protected static final String IS_CONNECTED = TAG + ".is_connected"; - protected static final String STATUS_MESSAGE = TAG + ".status_message"; public static final String START_ON_BOOT = "start on boot"; + private static final String SHOWED_LOG = TAG + ".showed_log"; - @InjectView(R.id.status_message) - TextView status_message; @InjectView(R.id.vpn_Status_Image) FabButton vpn_status_image; @@ -83,8 +80,6 @@ public class EipFragment extends Fragment implements Observer { eip_status.setConnecting(); else if (savedInstanceState.getBoolean(IS_CONNECTED)) eip_status.setConnectedOrDisconnected(); - else - status_message.setText(savedInstanceState.getString(STATUS_MESSAGE)); } @Override @@ -98,7 +93,6 @@ public class EipFragment extends Fragment implements Observer { public void onSaveInstanceState(Bundle outState) { outState.putBoolean(IS_PENDING, eip_status.isConnecting()); outState.putBoolean(IS_CONNECTED, eip_status.isConnected()); - outState.putString(STATUS_MESSAGE, status_message.getText().toString()); super.onSaveInstanceState(outState); } @@ -109,6 +103,7 @@ public class EipFragment extends Fragment implements Observer { @OnClick(R.id.vpn_Status_Image) void handleIcon() { + LogWindowWrapper.getInstance(dashboard.getApplicationContext()).clearReason(); if (eip_status.isConnected() || eip_status.isConnecting()) handleSwitchOff(); else @@ -174,8 +169,6 @@ public class EipFragment extends Fragment implements Observer { public void startEipFromScratch() { wants_to_connect = false; eip_status.setConnecting(); - String status = dashboard.getString(R.string.eip_status_start_pending); - status_message.setText(status); saveStatus(); eipCommand(Constants.ACTION_START_EIP); @@ -194,9 +187,6 @@ public class EipFragment extends Fragment implements Observer { } protected void stopEipIfPossible() { - String message = dashboard.getString(R.string.eip_state_not_connected); - status_message.setText(message); - eipCommand(Constants.ACTION_STOP_EIP); } @@ -251,29 +241,24 @@ public class EipFragment extends Fragment implements Observer { } private void handleNewState(EipStatus eip_status) { - if (eip_status.wantsToDisconnect()) - setDisconnectedUI(); - else if (eip_status.isConnecting()) - setInProgressUI(eip_status); - else if (eip_status.isConnected()) - setConnectedUI(); - else if (eip_status.isDisconnected() && !eip_status.isConnecting()) - setDisconnectedUI(); + Context context = dashboard.getApplicationContext(); + String error = eip_status.lastError(5, context); + LogWindowWrapper log_window = LogWindowWrapper.getInstance(context); + + if (!error.isEmpty() && log_window != null && !log_window.reason().equalsIgnoreCase(error)) { + log_window.showLog(); + log_window.showedBecauseOf(error); + VoidVpnService.stop(); + } + adjustSwitch(); } private void setConnectedUI() { adjustSwitch(); - status_message.setText(dashboard.getString(R.string.eip_state_connected)); } private void setDisconnectedUI() { adjustSwitch(); - if (eip_status.errorInLast(5, dashboard.getApplicationContext()) - && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))) { - dashboard.showLog(); - VoidVpnService.stop(); - } - status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); } private void adjustSwitch() { @@ -292,16 +277,10 @@ public class EipFragment extends Fragment implements Observer { } private void setInProgressUI(EipStatus eip_status) { - int localizedResId = eip_status.getLocalizedResId(); - String logmessage = eip_status.getLogMessage(); - String prefix = dashboard.getString(localizedResId); - - status_message.setText(prefix + " " + logmessage); adjustSwitch(); } private void updatingCertificateUI() { - status_message.setText(getString(R.string.updating_certificate_message)); } protected class EIPReceiver extends ResultReceiver { diff --git a/app/src/main/java/se/leap/bitmaskclient/LogWindowWrapper.java b/app/src/main/java/se/leap/bitmaskclient/LogWindowWrapper.java new file mode 100644 index 00000000..441eb28e --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/LogWindowWrapper.java @@ -0,0 +1,41 @@ +package se.leap.bitmaskclient; + +import android.content.*; + +import de.blinkt.openvpn.activities.*; + +public class LogWindowWrapper { + private static LogWindowWrapper instance; + + private static String TAG = LogWindowWrapper.class.getName(); + private Context context; + private String reason = ""; + + public LogWindowWrapper(Context context) { + this.context = context; + } + + public void showLog() { + Intent startLW = new Intent(context, LogWindow.class); + startLW.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(startLW); + } + + public static LogWindowWrapper getInstance(Context context) { + if(instance == null) + instance = new LogWindowWrapper(context); + return instance; + } + + public void clearReason() { + reason = ""; + } + + public void showedBecauseOf(String reason) { + this.reason = reason; + } + + public String reason() { + return reason; + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index b7f95277..356cf6b0 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -137,18 +137,25 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { } public boolean errorInLast(int lines, Context context) { - boolean result = false; + return !lastError(lines, context).isEmpty(); + } + + public String lastError(int lines, Context context) { + String error = ""; + String[] error_keywords = {"error", "ERROR", "fatal", "FATAL"}; VpnStatus.LogItem[] log = VpnStatus.getlogbuffer(); String message = ""; for (int i = 1; i <= lines && log.length > i; i++) { - message = log[log.length - i].getString(context); + VpnStatus.LogItem log_item = log[log.length - i]; + message = log_item.getString(context); for (int j = 0; j < error_keywords.length; j++) if (message.contains(error_keywords[j])) - result = true; + error = message; } - return result; + + return error; } @Override diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index dac92fe2..cbf0fed2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -50,6 +50,10 @@ public class VoidVpnService extends VpnService { closeFd(); } + public static boolean isRunning() throws NullPointerException { + return thread.isAlive() && fd != null; + } + private static void closeFd() { try { if (fd != null) -- cgit v1.2.3