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 {
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 {

View File

@ -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 {