From 81505d6ec2029cde746c375f8f0dc8e256e02ab9 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Fri, 16 Feb 2018 17:23:54 +0100 Subject: #8857 fix dialog recreation in EipFragment --- .../java/se/leap/bitmaskclient/EipFragment.java | 103 ++++++++++++++------- .../java/se/leap/bitmaskclient/MainActivity.java | 17 ++-- 2 files changed, 81 insertions(+), 39 deletions(-) (limited to 'app') diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index e1c61801..5adb732a 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -17,7 +17,6 @@ package se.leap.bitmaskclient; import android.app.Activity; -import android.app.AlertDialog; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; @@ -30,6 +29,7 @@ import android.os.Bundle; import android.os.IBinder; import android.support.annotation.NonNull; import android.support.v4.app.Fragment; +import android.support.v7.app.AlertDialog; import android.support.v7.widget.AppCompatImageView; import android.util.Log; import android.view.LayoutInflater; @@ -65,7 +65,6 @@ public class EipFragment extends Fragment implements Observer { public final static String TAG = EipFragment.class.getSimpleName(); - protected static final String IS_CONNECTED = TAG + ".is_connected"; public static final String START_EIP_ON_BOOT = "start on boot"; public static final String ASK_TO_CANCEL_VPN = "ask_to_cancel_vpn"; @@ -93,6 +92,14 @@ public class EipFragment extends Fragment implements Observer { private EipStatus eipStatus; + //---saved Instance ------- + private final static String KEY_SHOW_PENDING_START_CANCELLATION = "KEY_SHOW_PENDING_START_CANCELLATION"; + private final static String KEY_SHOW_ASK_TO_STOP_EIP = "KEY_SHOW_ASK_TO_STOP_EIP"; + private boolean showPendingStartCancellation = false; + private boolean showAskToStopEip = false; + //------------------------ + AlertDialog alertDialog; + private IOpenVPNServiceInternal mService; private ServiceConnection openVpnConnection = new ServiceConnection() { @@ -152,8 +159,11 @@ public class EipFragment extends Fragment implements Observer { Bundle arguments = getArguments(); if (arguments != null && arguments.containsKey(ASK_TO_CANCEL_VPN) && arguments.getBoolean(ASK_TO_CANCEL_VPN)) { + arguments.remove(ASK_TO_CANCEL_VPN); + setArguments(arguments); askToStopEIP(); } + restoreFromSavedInstance(savedInstanceState); return view; } @@ -178,15 +188,32 @@ public class EipFragment extends Fragment implements Observer { } @Override - public void onDestroyView() { - super.onDestroyView(); - eipStatus.deleteObserver(this); + public void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + if (showAskToStopEip) { + outState.putBoolean(KEY_SHOW_ASK_TO_STOP_EIP, true); + alertDialog.dismiss(); + } else if (showPendingStartCancellation) { + outState.putBoolean(KEY_SHOW_PENDING_START_CANCELLATION, true); + alertDialog.dismiss(); + + } + } + + private void restoreFromSavedInstance(Bundle savedInstanceState) { + if (savedInstanceState != null && savedInstanceState.containsKey(KEY_SHOW_PENDING_START_CANCELLATION)) { + showPendingStartCancellation = true; + askPendingStartCancellation(); + } else if (savedInstanceState != null && savedInstanceState.containsKey(KEY_SHOW_ASK_TO_STOP_EIP)) { + showAskToStopEip = true; + askToStopEIP(); + } } @Override - public void onSaveInstanceState(@NonNull Bundle outState) { - outState.putBoolean(IS_CONNECTED, eipStatus.isConnected()); - super.onSaveInstanceState(outState); + public void onDestroyView() { + super.onDestroyView(); + eipStatus.deleteObserver(this); } private void saveStatus(boolean restartOnBoot) { @@ -252,11 +279,31 @@ public class EipFragment extends Fragment implements Observer { } } + public void startEipFromScratch() { + saveStatus(true); + Context context = getContext(); + if (context != null) { + EipCommand.startVPN(context); + } else { + Log.e(TAG, "context is null when trying to start VPN"); + } + } + + protected void stopEipIfPossible() { + Context context = getContext(); + if (context != null) { + EipCommand.stopVPN(getContext()); + } else { + Log.e(TAG, "context is null when trying to stop EIP"); + } + } + private void askPendingStartCancellation() { Activity activity = getActivity(); if (activity != null) { AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity()); - alertBuilder.setTitle(activity.getString(R.string.eip_cancel_connect_title)) + showPendingStartCancellation = true; + alertDialog = alertBuilder.setTitle(activity.getString(R.string.eip_cancel_connect_title)) .setMessage(activity.getString(R.string.eip_cancel_connect_text)) .setPositiveButton((android.R.string.yes), new DialogInterface.OnClickListener() { @Override @@ -268,37 +315,23 @@ public class EipFragment extends Fragment implements Observer { @Override public void onClick(DialogInterface dialog, int which) { } - }) - .show(); + }).setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + showPendingStartCancellation = false; + } + }).show(); } else { Log.e(TAG, "activity is null when asking to cancel"); } } - public void startEipFromScratch() { - saveStatus(true); - Context context = getContext(); - if (context != null) { - EipCommand.startVPN(context); - } else { - Log.e(TAG, "context is null when trying to start VPN"); - } - } - - protected void stopEipIfPossible() { - Context context = getContext(); - if (context != null) { - EipCommand.stopVPN(getContext()); - } else { - Log.e(TAG, "context is null when trying to stop EIP"); - } - } - protected void askToStopEIP() { Activity activity = getActivity(); if (activity != null) { AlertDialog.Builder alertBuilder = new AlertDialog.Builder(activity); - alertBuilder.setTitle(activity.getString(R.string.eip_cancel_connect_title)) + showAskToStopEip = true; + alertDialog = alertBuilder.setTitle(activity.getString(R.string.eip_cancel_connect_title)) .setMessage(activity.getString(R.string.eip_warning_browser_inconsistency)) .setPositiveButton((android.R.string.yes), new DialogInterface.OnClickListener() { @Override @@ -310,8 +343,12 @@ public class EipFragment extends Fragment implements Observer { @Override public void onClick(DialogInterface dialog, int which) { } - }) - .show(); + }).setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + showAskToStopEip = false; + } + }).show(); } else { Log.e(TAG, "activity is null when asking to stop EIP"); } diff --git a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java index 2a2a68a7..7c5ae515 100644 --- a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java @@ -197,6 +197,9 @@ public class MainActivity extends AppCompatActivity implements Observer, MainAct default: break; } + // on layout change / recreation of the activity, we don't want create new Fragments + // instead the fragments themselves care about recreation and state restoration + intent.setAction(null); if (fragment != null) { new FragmentManagerEnhanced(getSupportFragmentManager()).beginTransaction() @@ -256,6 +259,14 @@ public class MainActivity extends AppCompatActivity implements Observer, MainAct super.onDestroy(); } + + @Override + public void update(Observable observable, Object data) { + if (observable instanceof EipStatus) { + eipStatus = (EipStatus) observable; + } + } + private void setUpBroadcastReceiver() { IntentFilter updateIntentFilter = new IntentFilter(BROADCAST_EIP_EVENT); updateIntentFilter.addAction(BROADCAST_PROVIDER_API_EVENT); @@ -383,12 +394,6 @@ public class MainActivity extends AppCompatActivity implements Observer, MainAct } - @Override - public void update(Observable observable, Object data) { - if (observable instanceof EipStatus) { - eipStatus = (EipStatus) observable; - } - } private void stop() { preferences.edit().putBoolean(EIP_RESTART_ON_BOOT, false).apply(); -- cgit v1.2.3