From 761b604e14b14a86a357816b266e77d458137c83 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Fri, 25 Oct 2019 17:10:13 +0200 Subject: implement error handling for edge case when Android throws an nullpointer exception while it tries to prepare the VpnService --- .../leap/bitmaskclient/eip/EipResultBroadcast.java | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 app/src/main/java/se/leap/bitmaskclient/eip/EipResultBroadcast.java (limited to 'app/src/main/java/se/leap/bitmaskclient/eip/EipResultBroadcast.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipResultBroadcast.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipResultBroadcast.java new file mode 100644 index 00000000..e1efb375 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipResultBroadcast.java @@ -0,0 +1,77 @@ +package se.leap.bitmaskclient.eip; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.ResultReceiver; +import android.support.v4.content.LocalBroadcastManager; +import android.util.Log; + +import static android.content.Intent.CATEGORY_DEFAULT; +import static se.leap.bitmaskclient.Constants.BROADCAST_EIP_EVENT; +import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE; +import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY; +import static se.leap.bitmaskclient.Constants.EIP_REQUEST; + +public class EipResultBroadcast { + private static final String TAG = EipResultBroadcast.class.getSimpleName(); + + + /** + * send resultCode and resultData to receiver or + * broadcast the result if no receiver is defined + * + * @param action the action that has been performed + * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise + */ + public static void tellToReceiverOrBroadcast(Context context, String action, int resultCode) { + tellToReceiverOrBroadcast(context, action, resultCode, null, new Bundle()); + } + + /** + * send resultCode and resultData to receiver or + * broadcast the result if no receiver is defined + * + * @param action the action that has been performed + * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise + * @param resultData other data to broadcast or return to receiver + */ + public static void tellToReceiverOrBroadcast(Context context, String action, int resultCode, ResultReceiver receiver, Bundle resultData) { + resultData.putString(EIP_REQUEST, action); + if (receiver != null) { + receiver.send(resultCode, resultData); + } else { + broadcastEvent(context, resultCode, resultData); + } + } + + /** + * send resultCode and resultData to receiver or + * broadcast the result if no receiver is defined + * + * @param action the action that has been performed + * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise + * @param resultData other data to broadcast or return to receiver + */ + public static void tellToReceiverOrBroadcast(Context context, String action, int resultCode, Bundle resultData) { + resultData.putString(EIP_REQUEST, action); + broadcastEvent(context, resultCode, resultData); + } + + + + /** + * broadcast result + * + * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise + * @param resultData other data to broadcast or return to receiver + */ + public static void broadcastEvent(Context context, int resultCode, Bundle resultData) { + Intent intentUpdate = new Intent(BROADCAST_EIP_EVENT); + intentUpdate.addCategory(CATEGORY_DEFAULT); + intentUpdate.putExtra(BROADCAST_RESULT_CODE, resultCode); + intentUpdate.putExtra(BROADCAST_RESULT_KEY, resultData); + Log.d(TAG, "sending broadcast"); + LocalBroadcastManager.getInstance(context).sendBroadcast(intentUpdate); + } +} -- cgit v1.2.3