From d3e6b311cafc13c627201220d183d07ef748f2bb Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Wed, 23 Aug 2017 18:02:16 -0500 Subject: [PATCH] BaseConfigActivity: Remember editing state Signed-off-by: Jason A. Donenfeld --- .../wireguard/android/BaseConfigActivity.java | 30 ++++++++++++++++--- .../com/wireguard/android/ConfigActivity.java | 21 ++++++++----- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/wireguard/android/BaseConfigActivity.java b/app/src/main/java/com/wireguard/android/BaseConfigActivity.java index 0051d6bf..17ca3b6a 100644 --- a/app/src/main/java/com/wireguard/android/BaseConfigActivity.java +++ b/app/src/main/java/com/wireguard/android/BaseConfigActivity.java @@ -16,22 +16,33 @@ import com.wireguard.config.Config; abstract class BaseConfigActivity extends Activity { protected static final String KEY_CURRENT_CONFIG = "currentConfig"; + protected static final String KEY_IS_EDITING = "isEditing"; private Config currentConfig; private String initialConfig; + private boolean isEditing; + private boolean wasEditing; protected Config getCurrentConfig() { return currentConfig; } + protected boolean isEditing() { + return isEditing; + } + @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Restore the saved configuration if there is one; otherwise grab it from the intent. - if (savedInstanceState != null) + if (savedInstanceState != null) { initialConfig = savedInstanceState.getString(KEY_CURRENT_CONFIG); - else - initialConfig = getIntent().getStringExtra(KEY_CURRENT_CONFIG); + wasEditing = savedInstanceState.getBoolean(KEY_IS_EDITING, false); + } else { + final Intent intent = getIntent(); + initialConfig = intent.getStringExtra(KEY_CURRENT_CONFIG); + wasEditing = intent.getBooleanExtra(KEY_IS_EDITING, false); + } // Trigger starting the service as early as possible if (VpnService.getInstance() != null) onServiceAvailable(); @@ -42,24 +53,35 @@ abstract class BaseConfigActivity extends Activity { protected abstract void onCurrentConfigChanged(Config config); + protected abstract void onEditingStateChanged(boolean isEditing); + @Override public void onSaveInstanceState(final Bundle outState) { super.onSaveInstanceState(outState); if (currentConfig != null) outState.putString(KEY_CURRENT_CONFIG, currentConfig.getName()); + outState.putBoolean(KEY_IS_EDITING, isEditing); } protected void onServiceAvailable() { // Make sure the subclass activity is initialized before setting its config. if (initialConfig != null && currentConfig == null) setCurrentConfig(VpnService.getInstance().get(initialConfig)); + setIsEditing(wasEditing); } public void setCurrentConfig(final Config config) { if (currentConfig == config) return; currentConfig = config; - onCurrentConfigChanged(currentConfig); + onCurrentConfigChanged(config); + } + + public void setIsEditing(final boolean isEditing) { + if (this.isEditing == isEditing) + return; + this.isEditing = isEditing; + onEditingStateChanged(isEditing); } private class ServiceConnectionCallbacks implements ServiceConnection { diff --git a/app/src/main/java/com/wireguard/android/ConfigActivity.java b/app/src/main/java/com/wireguard/android/ConfigActivity.java index e81b6c9c..627dc646 100644 --- a/app/src/main/java/com/wireguard/android/ConfigActivity.java +++ b/app/src/main/java/com/wireguard/android/ConfigActivity.java @@ -22,7 +22,6 @@ public class ConfigActivity extends BaseConfigActivity { private final FragmentManager fm = getFragmentManager(); private final FragmentCache fragments = new FragmentCache(fm); - private boolean isEditing; private boolean isLayoutFinished; private boolean isServiceAvailable; private boolean isSplitLayout; @@ -98,7 +97,7 @@ public class ConfigActivity extends BaseConfigActivity { private void moveToState(final Config config, final boolean shouldBeEditing) { // Update the saved state. setCurrentConfig(config); - isEditing = shouldBeEditing; + setIsEditing(shouldBeEditing); // Avoid performing fragment transactions when the app is not fully initialized. if (!isLayoutFinished || !isServiceAvailable || isStateSaved) return; @@ -129,12 +128,12 @@ public class ConfigActivity extends BaseConfigActivity { public void onBackPressed() { super.onBackPressed(); // The visible fragment is now the one that was on top of the back stack, if there was one. - if (isEditing) + if (isEditing()) visibleFragmentTag = TAG_DETAIL; else if (!isSplitLayout && TAG_DETAIL.equals(visibleFragmentTag)) visibleFragmentTag = TAG_LIST; // If the user went back from the detail screen to the list, clear the current config. - moveToState(isEditing ? getCurrentConfig() : null, false); + moveToState(isEditing() ? getCurrentConfig() : null, false); } @Override @@ -144,7 +143,7 @@ public class ConfigActivity extends BaseConfigActivity { isSplitLayout = findViewById(R.id.detail_fragment) != null; mainContainer = isSplitLayout ? R.id.detail_fragment : R.id.master_fragment; isLayoutFinished = true; - moveToState(getCurrentConfig(), isEditing); + moveToState(getCurrentConfig(), isEditing()); } @Override @@ -161,6 +160,12 @@ public class ConfigActivity extends BaseConfigActivity { moveToState(config, false); } + @Override + protected void onEditingStateChanged(final boolean isEditing) { + Log.d(getClass().getSimpleName(), "onEditingStateChanged: isEditing=" + isEditing); + moveToState(getCurrentConfig(), isEditing); + } + @Override public boolean onOptionsItemSelected(final MenuItem item) { switch (item.getItemId()) { @@ -173,7 +178,7 @@ public class ConfigActivity extends BaseConfigActivity { return true; case R.id.menu_action_edit: // Try to make the editing fragment visible. - moveToState(getCurrentConfig(), true); + setIsEditing(true); return true; case R.id.menu_action_save: // This menu item is handled by the editing fragment. @@ -191,7 +196,7 @@ public class ConfigActivity extends BaseConfigActivity { super.onPostResume(); // Allow changes to fragments. isStateSaved = false; - moveToState(getCurrentConfig(), isEditing); + moveToState(getCurrentConfig(), isEditing()); } @Override @@ -209,7 +214,7 @@ public class ConfigActivity extends BaseConfigActivity { super.onServiceAvailable(); // Allow creating fragments. isServiceAvailable = true; - moveToState(getCurrentConfig(), isEditing); + moveToState(getCurrentConfig(), isEditing()); } private static class FragmentCache {