diff options
author | cyBerta <cyberta@riseup.net> | 2018-02-17 11:04:01 +0100 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2018-02-17 11:04:01 +0100 |
commit | 8931c0b7e24b155130681cec4e25c701544b13ca (patch) | |
tree | fa2d11b86c22419eeafb5109b81e01c1f13310cb /app/src/main/java/se/leap/bitmaskclient | |
parent | 81505d6ec2029cde746c375f8f0dc8e256e02ab9 (diff) |
#8797 fix recreation / restore of MainActivityErrorDialogs
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/MainActivity.java | 64 | ||||
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/MainActivityErrorDialog.java | 64 |
2 files changed, 38 insertions, 90 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java index 7c5ae515..044b54df 100644 --- a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java @@ -63,21 +63,17 @@ import static se.leap.bitmaskclient.R.string.downloading_vpn_certificate_failed; import static se.leap.bitmaskclient.R.string.vpn_certificate_user_message; -public class MainActivity extends AppCompatActivity implements Observer, MainActivityErrorDialog.MainActivityErrorDialogInterface{ +public class MainActivity extends AppCompatActivity implements Observer { public final static String TAG = MainActivity.class.getSimpleName(); - private final String ACTIVITY_STATE = "state of activity"; - private final String DEFAULT_UI_STATE = "default state"; - private final String SHOW_DIALOG_STATE = "show dialog"; - private final String REASON_TO_FAIL = "reason to fail"; + private static final String KEY_ACTIVITY_STATE = "key state of activity"; + private static final String DEFAULT_UI_STATE = "default state"; + private static final String SHOW_DIALOG_STATE = "show dialog"; + private static final String REASON_TO_FAIL = "reason to fail"; - protected Intent mConfigState = new Intent(DEFAULT_UI_STATE); private static Provider provider = new Provider(); private SharedPreferences preferences; - - private String reasonToFail; - private EipStatus eipStatus; private NavigationDrawerFragment navigationDrawerFragment; private MainActivityBroadcastReceiver mainActivityBroadcastReceiver; @@ -125,47 +121,12 @@ public class MainActivity extends AppCompatActivity implements Observer, MainAct (DrawerLayout) findViewById(R.id.drawer_layout)); eipStatus = EipStatus.getInstance(); - - handleIntentAction(getIntent()); - if(savedInstanceState != null) { - restoreState(savedInstanceState); - } - - } - - @Override - protected void onSaveInstanceState(@NotNull Bundle outState) { - outState.putString(ACTIVITY_STATE, mConfigState.getAction()); - outState.putParcelable(PROVIDER_KEY, provider); - - DialogFragment dialogFragment = (DialogFragment) new FragmentManagerEnhanced(getSupportFragmentManager()).findFragmentByTag(MainActivityErrorDialog.TAG); - outState.putString(REASON_TO_FAIL, reasonToFail); - if (dialogFragment != null) { - dialogFragment.dismiss(); - } - - super.onSaveInstanceState(outState); - } - - private void restoreState(Bundle savedInstance) { - String activityState = savedInstance.getString(ACTIVITY_STATE, ""); - if (activityState.equals(SHOW_DIALOG_STATE)) { - reasonToFail = savedInstance.getString(REASON_TO_FAIL); - if (reasonToFail != null) { - showDownloadFailedDialog(reasonToFail); - } - } } @Override protected void onResume() { super.onResume(); bindOpenVpnService(); - - String action = mConfigState.getAction(); - if(action.equalsIgnoreCase(SHOW_DIALOG_STATE)) { - showDownloadFailedDialog(reasonToFail); - } } @Override @@ -275,12 +236,6 @@ public class MainActivity extends AppCompatActivity implements Observer, MainAct Log.d(TAG, "broadcast registered"); } - @Override - public void onDialogDismissed() { - mConfigState.setAction(DEFAULT_UI_STATE); - reasonToFail = null; - } - private class MainActivityBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { @@ -323,7 +278,7 @@ public class MainActivity extends AppCompatActivity implements Observer, MainAct case RESULT_CANCELED: String error = resultData.getString(ERRORS); if (LeapSRPSession.loggedIn() || provider.allowsAnonymous()) { - showDownloadFailedDialog(error); + showMainActivityErrorDialog(error); } else { askUserToLogIn(getString(vpn_certificate_user_message)); } @@ -360,7 +315,7 @@ public class MainActivity extends AppCompatActivity implements Observer, MainAct break; case INCORRECTLY_DOWNLOADED_VPN_CERTIFICATE: if (LeapSRPSession.loggedIn() || provider.allowsAnonymous()) { - showDownloadFailedDialog(getString(downloading_vpn_certificate_failed)); + showMainActivityErrorDialog(getString(downloading_vpn_certificate_failed)); } else { askUserToLogIn(getString(vpn_certificate_user_message)); } @@ -371,9 +326,7 @@ public class MainActivity extends AppCompatActivity implements Observer, MainAct /** * Shows an error dialog */ - public void showDownloadFailedDialog(String reasonToFail) { - this.reasonToFail = reasonToFail; - mConfigState.setAction(SHOW_DIALOG_STATE); + public void showMainActivityErrorDialog(String reasonToFail) { try { FragmentTransaction fragmentTransaction = new FragmentManagerEnhanced( @@ -390,6 +343,7 @@ public class MainActivity extends AppCompatActivity implements Observer, MainAct newFragment.show(fragmentTransaction, MainActivityErrorDialog.TAG); } catch (IllegalStateException | NullPointerException e) { e.printStackTrace(); + Log.w(TAG, "error dialog leaked!"); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/MainActivityErrorDialog.java b/app/src/main/java/se/leap/bitmaskclient/MainActivityErrorDialog.java index fd84e83d..7756bcfd 100644 --- a/app/src/main/java/se/leap/bitmaskclient/MainActivityErrorDialog.java +++ b/app/src/main/java/se/leap/bitmaskclient/MainActivityErrorDialog.java @@ -16,34 +16,37 @@ */ package se.leap.bitmaskclient; -import android.app.AlertDialog; import android.app.Dialog; -import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; +import android.support.v7.app.AlertDialog; +import android.util.Log; import org.json.JSONObject; -import static se.leap.bitmaskclient.MainActivityErrorDialog.DOWNLOAD_ERRORS.*; +import static se.leap.bitmaskclient.MainActivityErrorDialog.DOWNLOAD_ERRORS.DEFAULT; +import static se.leap.bitmaskclient.MainActivityErrorDialog.DOWNLOAD_ERRORS.valueOf; import static se.leap.bitmaskclient.ProviderAPI.DOWNLOAD_VPN_CERTIFICATE; import static se.leap.bitmaskclient.eip.EIP.ERRORS; import static se.leap.bitmaskclient.eip.EIP.ERROR_ID; /** - * Implements a error dialog for the main activity. + * Implements an error dialog for the main activity. * * @author fupduck * @author cyberta */ public class MainActivityErrorDialog extends DialogFragment { - public static String TAG = "downloaded_failed_dialog"; + final public static String TAG = "downloaded_failed_dialog"; + 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 MainActivityErrorDialogInterface callbackInterface; private Provider provider; public enum DOWNLOAD_ERRORS { @@ -86,14 +89,18 @@ public class MainActivityErrorDialog extends DialogFragment { } @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + restoreFromSavedInstance(savedInstanceState); + } + + @Override @NonNull 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) { - dialog.dismiss(); - callbackInterface.onDialogDismissed(); } }); switch (downloadError) { @@ -101,19 +108,11 @@ public class MainActivityErrorDialog extends DialogFragment { builder.setPositiveButton(R.string.update_certificate, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - dismiss(); ProviderAPICommand.execute(getContext(), DOWNLOAD_VPN_CERTIFICATE, provider); - callbackInterface.onDialogDismissed(); } }); break; default: - builder.setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dismiss(); - callbackInterface.onDialogDismissed(); - } - }); break; } @@ -121,29 +120,24 @@ public class MainActivityErrorDialog extends DialogFragment { return builder.create(); } - @Override - public void dismiss() { - super.dismiss(); - } - public interface MainActivityErrorDialogInterface { - void onDialogDismissed(); + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString(KEY_REASON_TO_FAIL, reasonToFail); + outState.putParcelable(KEY_PROVIDER, provider); } - @Override - public void onAttach(Context context) { - super.onAttach(context); - try { - callbackInterface = (MainActivityErrorDialogInterface) context; - } catch (ClassCastException e) { - throw new ClassCastException(context.toString() - + " must implement NoticeDialogListener"); + private void restoreFromSavedInstance(Bundle savedInstanceState) { + if (savedInstanceState == null) { + return; + } + if (savedInstanceState.containsKey(KEY_PROVIDER)) { + this.provider = savedInstanceState.getParcelable(KEY_PROVIDER); + } + if (savedInstanceState.containsKey(KEY_REASON_TO_FAIL)) { + this.reasonToFail = savedInstanceState.getString(KEY_REASON_TO_FAIL); } } - @Override - public void onCancel(DialogInterface dialog) { - super.onCancel(dialog); - callbackInterface.onDialogDismissed(); - } } |