BaseConfigActivity: Remember editing state

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland 2017-08-23 18:02:16 -05:00
parent b1c1889c50
commit d3e6b311ca
2 changed files with 39 additions and 12 deletions

View File

@ -16,22 +16,33 @@ import com.wireguard.config.Config;
abstract class BaseConfigActivity extends Activity { abstract class BaseConfigActivity extends Activity {
protected static final String KEY_CURRENT_CONFIG = "currentConfig"; protected static final String KEY_CURRENT_CONFIG = "currentConfig";
protected static final String KEY_IS_EDITING = "isEditing";
private Config currentConfig; private Config currentConfig;
private String initialConfig; private String initialConfig;
private boolean isEditing;
private boolean wasEditing;
protected Config getCurrentConfig() { protected Config getCurrentConfig() {
return currentConfig; return currentConfig;
} }
protected boolean isEditing() {
return isEditing;
}
@Override @Override
protected void onCreate(final Bundle savedInstanceState) { protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Restore the saved configuration if there is one; otherwise grab it from the intent. // 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); initialConfig = savedInstanceState.getString(KEY_CURRENT_CONFIG);
else wasEditing = savedInstanceState.getBoolean(KEY_IS_EDITING, false);
initialConfig = getIntent().getStringExtra(KEY_CURRENT_CONFIG); } 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 // Trigger starting the service as early as possible
if (VpnService.getInstance() != null) if (VpnService.getInstance() != null)
onServiceAvailable(); onServiceAvailable();
@ -42,24 +53,35 @@ abstract class BaseConfigActivity extends Activity {
protected abstract void onCurrentConfigChanged(Config config); protected abstract void onCurrentConfigChanged(Config config);
protected abstract void onEditingStateChanged(boolean isEditing);
@Override @Override
public void onSaveInstanceState(final Bundle outState) { public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
if (currentConfig != null) if (currentConfig != null)
outState.putString(KEY_CURRENT_CONFIG, currentConfig.getName()); outState.putString(KEY_CURRENT_CONFIG, currentConfig.getName());
outState.putBoolean(KEY_IS_EDITING, isEditing);
} }
protected void onServiceAvailable() { protected void onServiceAvailable() {
// Make sure the subclass activity is initialized before setting its config. // Make sure the subclass activity is initialized before setting its config.
if (initialConfig != null && currentConfig == null) if (initialConfig != null && currentConfig == null)
setCurrentConfig(VpnService.getInstance().get(initialConfig)); setCurrentConfig(VpnService.getInstance().get(initialConfig));
setIsEditing(wasEditing);
} }
public void setCurrentConfig(final Config config) { public void setCurrentConfig(final Config config) {
if (currentConfig == config) if (currentConfig == config)
return; return;
currentConfig = config; 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 { private class ServiceConnectionCallbacks implements ServiceConnection {

View File

@ -22,7 +22,6 @@ public class ConfigActivity extends BaseConfigActivity {
private final FragmentManager fm = getFragmentManager(); private final FragmentManager fm = getFragmentManager();
private final FragmentCache fragments = new FragmentCache(fm); private final FragmentCache fragments = new FragmentCache(fm);
private boolean isEditing;
private boolean isLayoutFinished; private boolean isLayoutFinished;
private boolean isServiceAvailable; private boolean isServiceAvailable;
private boolean isSplitLayout; private boolean isSplitLayout;
@ -98,7 +97,7 @@ public class ConfigActivity extends BaseConfigActivity {
private void moveToState(final Config config, final boolean shouldBeEditing) { private void moveToState(final Config config, final boolean shouldBeEditing) {
// Update the saved state. // Update the saved state.
setCurrentConfig(config); setCurrentConfig(config);
isEditing = shouldBeEditing; setIsEditing(shouldBeEditing);
// Avoid performing fragment transactions when the app is not fully initialized. // Avoid performing fragment transactions when the app is not fully initialized.
if (!isLayoutFinished || !isServiceAvailable || isStateSaved) if (!isLayoutFinished || !isServiceAvailable || isStateSaved)
return; return;
@ -129,12 +128,12 @@ public class ConfigActivity extends BaseConfigActivity {
public void onBackPressed() { public void onBackPressed() {
super.onBackPressed(); super.onBackPressed();
// The visible fragment is now the one that was on top of the back stack, if there was one. // 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; visibleFragmentTag = TAG_DETAIL;
else if (!isSplitLayout && TAG_DETAIL.equals(visibleFragmentTag)) else if (!isSplitLayout && TAG_DETAIL.equals(visibleFragmentTag))
visibleFragmentTag = TAG_LIST; visibleFragmentTag = TAG_LIST;
// If the user went back from the detail screen to the list, clear the current config. // 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 @Override
@ -144,7 +143,7 @@ public class ConfigActivity extends BaseConfigActivity {
isSplitLayout = findViewById(R.id.detail_fragment) != null; isSplitLayout = findViewById(R.id.detail_fragment) != null;
mainContainer = isSplitLayout ? R.id.detail_fragment : R.id.master_fragment; mainContainer = isSplitLayout ? R.id.detail_fragment : R.id.master_fragment;
isLayoutFinished = true; isLayoutFinished = true;
moveToState(getCurrentConfig(), isEditing); moveToState(getCurrentConfig(), isEditing());
} }
@Override @Override
@ -161,6 +160,12 @@ public class ConfigActivity extends BaseConfigActivity {
moveToState(config, false); moveToState(config, false);
} }
@Override
protected void onEditingStateChanged(final boolean isEditing) {
Log.d(getClass().getSimpleName(), "onEditingStateChanged: isEditing=" + isEditing);
moveToState(getCurrentConfig(), isEditing);
}
@Override @Override
public boolean onOptionsItemSelected(final MenuItem item) { public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
@ -173,7 +178,7 @@ public class ConfigActivity extends BaseConfigActivity {
return true; return true;
case R.id.menu_action_edit: case R.id.menu_action_edit:
// Try to make the editing fragment visible. // Try to make the editing fragment visible.
moveToState(getCurrentConfig(), true); setIsEditing(true);
return true; return true;
case R.id.menu_action_save: case R.id.menu_action_save:
// This menu item is handled by the editing fragment. // This menu item is handled by the editing fragment.
@ -191,7 +196,7 @@ public class ConfigActivity extends BaseConfigActivity {
super.onPostResume(); super.onPostResume();
// Allow changes to fragments. // Allow changes to fragments.
isStateSaved = false; isStateSaved = false;
moveToState(getCurrentConfig(), isEditing); moveToState(getCurrentConfig(), isEditing());
} }
@Override @Override
@ -209,7 +214,7 @@ public class ConfigActivity extends BaseConfigActivity {
super.onServiceAvailable(); super.onServiceAvailable();
// Allow creating fragments. // Allow creating fragments.
isServiceAvailable = true; isServiceAvailable = true;
moveToState(getCurrentConfig(), isEditing); moveToState(getCurrentConfig(), isEditing());
} }
private static class FragmentCache { private static class FragmentCache {