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 --- .../java/se/leap/bitmaskclient/MainActivity.java | 34 +++++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/MainActivity.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java index ad7c8ae3..39c4a8f1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java @@ -35,6 +35,7 @@ import java.util.Observable; import java.util.Observer; import se.leap.bitmaskclient.drawer.NavigationDrawerFragment; +import se.leap.bitmaskclient.eip.EIP; import se.leap.bitmaskclient.eip.EipCommand; import se.leap.bitmaskclient.fragments.ExcludeAppsFragment; import se.leap.bitmaskclient.fragments.LogFragment; @@ -43,6 +44,7 @@ import se.leap.bitmaskclient.utils.PreferenceHelper; import static se.leap.bitmaskclient.Constants.ASK_TO_CANCEL_VPN; 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_ACTION_PREPARE_VPN; import static se.leap.bitmaskclient.Constants.EIP_ACTION_START; import static se.leap.bitmaskclient.Constants.EIP_REQUEST; import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; @@ -58,6 +60,7 @@ import static se.leap.bitmaskclient.ProviderAPI.USER_MESSAGE; import static se.leap.bitmaskclient.R.string.downloading_vpn_certificate_failed; import static se.leap.bitmaskclient.R.string.vpn_certificate_user_message; import static se.leap.bitmaskclient.eip.EIP.EIPErrors.ERROR_INVALID_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.eip.EIP.EIPErrors.ERROR_VPN_PREPARE; import static se.leap.bitmaskclient.utils.PreferenceHelper.storeProviderInPreferences; @@ -88,17 +91,12 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); provider = ProviderObservable.getInstance().getCurrentProvider(); + EipSetupObserver.addListener(this); // Set up the drawer. navigationDrawerFragment.setUp(R.id.navigation_drawer, findViewById(R.id.drawer_layout)); handleIntentAction(getIntent()); } - @Override - protected void onResume() { - super.onResume(); - EipSetupObserver.addListener(this); - } - @Override public void onBackPressed() { FragmentManagerEnhanced fragmentManagerEnhanced = new FragmentManagerEnhanced(getSupportFragmentManager()); @@ -214,8 +212,8 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, } @Override - protected void onPause() { - super.onPause(); + protected void onDestroy() { + super.onDestroy(); EipSetupObserver.removeListener(this); } @@ -247,6 +245,11 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, } } break; + case EIP_ACTION_PREPARE_VPN: + if (resultCode == RESULT_CANCELED) { + showMainActivityErrorDialog(getString(R.string.vpn_error_establish), ERROR_VPN_PREPARE); + } + break; } } @@ -297,7 +300,22 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, e.printStackTrace(); Log.w(TAG, "error dialog leaked!"); } + } + /** + * Shows an error dialog + */ + public void showMainActivityErrorDialog(String reasonToFail, EIP.EIPErrors error) { + try { + FragmentTransaction fragmentTransaction = new FragmentManagerEnhanced( + this.getSupportFragmentManager()).removePreviousFragment( + MainActivityErrorDialog.TAG); + DialogFragment newFragment = MainActivityErrorDialog.newInstance(provider, reasonToFail, error); + newFragment.show(fragmentTransaction, MainActivityErrorDialog.TAG); + } catch (IllegalStateException | NullPointerException e) { + e.printStackTrace(); + Log.w(TAG, "error dialog leaked!"); + } } /** -- cgit v1.2.3