From 621f94fec257ddbec8a78a0e8b820c8183b5ed6c Mon Sep 17 00:00:00 2001 From: cyBerta Date: Sat, 19 Oct 2019 01:59:06 +0200 Subject: move EIP service related error types to EIP class, specify error if the end of the list of available gateways has been reached during setup --- .../bitmaskclient/MainActivityErrorDialog.java | 28 +++++---------- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 40 ++++++++++++++++++---- 2 files changed, 42 insertions(+), 26 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient') diff --git a/app/src/main/java/se/leap/bitmaskclient/MainActivityErrorDialog.java b/app/src/main/java/se/leap/bitmaskclient/MainActivityErrorDialog.java index 1065503b..cead4fa0 100644 --- a/app/src/main/java/se/leap/bitmaskclient/MainActivityErrorDialog.java +++ b/app/src/main/java/se/leap/bitmaskclient/MainActivityErrorDialog.java @@ -17,7 +17,6 @@ package se.leap.bitmaskclient; import android.app.Dialog; -import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -26,9 +25,11 @@ import android.support.v7.app.AlertDialog; import org.json.JSONObject; -import static se.leap.bitmaskclient.MainActivityErrorDialog.DOWNLOAD_ERRORS.DEFAULT; -import static se.leap.bitmaskclient.MainActivityErrorDialog.DOWNLOAD_ERRORS.valueOf; +import se.leap.bitmaskclient.eip.EIP; + import static se.leap.bitmaskclient.ProviderAPI.UPDATE_INVALID_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.eip.EIP.EIPErrors.UNKNOWN; +import static se.leap.bitmaskclient.eip.EIP.EIPErrors.valueOf; import static se.leap.bitmaskclient.eip.EIP.ERRORS; import static se.leap.bitmaskclient.eip.EIP.ERROR_ID; @@ -44,15 +45,10 @@ public class MainActivityErrorDialog extends DialogFragment { final private static String KEY_REASON_TO_FAIL = "key reason to fail"; final private static String KEY_PROVIDER = "key provider"; private String reasonToFail; - private DOWNLOAD_ERRORS downloadError = DEFAULT; + private EIP.EIPErrors downloadError = UNKNOWN; private Provider provider; - public enum DOWNLOAD_ERRORS { - DEFAULT, - ERROR_INVALID_VPN_CERTIFICATE, - } - /** * @return a new instance of this DialogFragment. */ @@ -98,18 +94,12 @@ public class MainActivityErrorDialog extends DialogFragment { public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(reasonToFail) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - } - }); + .setNegativeButton(R.string.cancel, (dialog, id) -> { + }); switch (downloadError) { case ERROR_INVALID_VPN_CERTIFICATE: - builder.setPositiveButton(R.string.update_certificate, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - ProviderAPICommand.execute(getContext(), UPDATE_INVALID_VPN_CERTIFICATE, provider); - } - }); + builder.setPositiveButton(R.string.update_certificate, (dialog, which) -> + ProviderAPICommand.execute(getContext(), UPDATE_INVALID_VPN_CERTIFICATE, provider)); break; default: break; 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 0246174e..2b03ce5c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -75,9 +75,10 @@ import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT; import static se.leap.bitmaskclient.Constants.PROVIDER_PROFILE; import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; -import static se.leap.bitmaskclient.MainActivityErrorDialog.DOWNLOAD_ERRORS.ERROR_INVALID_VPN_CERTIFICATE; import static se.leap.bitmaskclient.R.string.vpn_certificate_is_invalid; import static se.leap.bitmaskclient.R.string.warning_client_parsing_error_gateways; +import static se.leap.bitmaskclient.eip.EIP.EIPErrors.ERROR_INVALID_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.eip.EIP.EIPErrors.NO_MORE_GATEWAYS; import static se.leap.bitmaskclient.utils.ConfigHelper.ensureNotOnMainThread; import static se.leap.bitmaskclient.utils.PreferenceHelper.getUsePluggableTransports; @@ -110,6 +111,12 @@ public final class EIP extends JobIntentService implements Observer { */ static final int JOB_ID = 1312; + public enum EIPErrors { + UNKNOWN, + ERROR_INVALID_VPN_CERTIFICATE, + NO_MORE_GATEWAYS + } + /** * Convenience method for enqueuing work in to this service. */ @@ -218,7 +225,8 @@ public final class EIP extends JobIntentService implements Observer { if (launchActiveGateway(gateway, nClosestGateway)) { tellToReceiverOrBroadcast(EIP_ACTION_START, RESULT_OK); } else { - tellToReceiverOrBroadcast(EIP_ACTION_START, RESULT_CANCELED); + setErrorResult(result, NO_MORE_GATEWAYS.toString()); + tellToReceiverOrBroadcast(EIP_ACTION_START, RESULT_CANCELED, result); } } @@ -263,7 +271,6 @@ public final class EIP extends JobIntentService implements Observer { intent.putExtra(Gateway.KEY_N_CLOSEST_GATEWAY, nClosestGateway); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); return true; - } /** @@ -295,9 +302,7 @@ public final class EIP extends JobIntentService implements Observer { * @return GatewaysManager */ private GatewaysManager gatewaysFromPreferences() { - GatewaysManager gatewaysManager = new GatewaysManager(getApplicationContext(), preferences); - gatewaysManager.configureFromPreferences(); - return gatewaysManager; + return new GatewaysManager(getApplicationContext(), preferences); } /** @@ -385,7 +390,28 @@ public final class EIP extends JobIntentService implements Observer { } /** - * disable Bitmask starting on after phone reboot + * Helper function to add an error to result bundle. + * Error results set by this method will be handled as + * internal errors without any user interaction + * (Setting only ERROR_ID but no ERRORS in the errorjson + * will be ignored in MainActivity) + * + * @param result - result of an action + * @param errorId - error identifier + */ + void setErrorResult(Bundle result, String errorId) { + JSONObject errorJson = new JSONObject(); + try { + errorJson.put(ERROR_ID, errorId); + } catch (JSONException e) { + e.printStackTrace(); + } + result.putString(ERRORS, errorJson.toString()); + result.putBoolean(BROADCAST_RESULT_KEY, false); + } + + /** + * disable Bitmask starting after phone reboot * then stop VPN */ private boolean stop() { -- cgit v1.2.3