Move bundle keys where they are used

This both clarifies and simplifies the code.

Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
Samuel Holland 2018-01-07 20:32:01 -06:00
parent cef97b990e
commit 11f851abf8
3 changed files with 8 additions and 6 deletions

View File

@ -16,6 +16,8 @@ import java.util.Objects;
*/ */
public abstract class BaseActivity extends Activity { public abstract class BaseActivity extends Activity {
private static final String KEY_SELECTED_TUNNEL = "selected_tunnel";
private final SelectionChangeRegistry selectionChangeRegistry = new SelectionChangeRegistry(); private final SelectionChangeRegistry selectionChangeRegistry = new SelectionChangeRegistry();
private Tunnel selectedTunnel; private Tunnel selectedTunnel;
@ -33,9 +35,9 @@ public abstract class BaseActivity extends Activity {
// Restore the saved tunnel if there is one; otherwise grab it from the arguments. // Restore the saved tunnel if there is one; otherwise grab it from the arguments.
String savedTunnelName = null; String savedTunnelName = null;
if (savedInstanceState != null) if (savedInstanceState != null)
savedTunnelName = savedInstanceState.getString(TunnelManager.KEY_SELECTED_TUNNEL); savedTunnelName = savedInstanceState.getString(KEY_SELECTED_TUNNEL);
else if (getIntent() != null) else if (getIntent() != null)
savedTunnelName = getIntent().getStringExtra(TunnelManager.KEY_SELECTED_TUNNEL); savedTunnelName = getIntent().getStringExtra(KEY_SELECTED_TUNNEL);
if (savedTunnelName != null) { if (savedTunnelName != null) {
final TunnelManager manager = Application.getComponent().getTunnelManager(); final TunnelManager manager = Application.getComponent().getTunnelManager();
selectedTunnel = manager.getTunnels().get(savedTunnelName); selectedTunnel = manager.getTunnels().get(savedTunnelName);
@ -47,7 +49,7 @@ public abstract class BaseActivity extends Activity {
@Override @Override
protected void onSaveInstanceState(final Bundle outState) { protected void onSaveInstanceState(final Bundle outState) {
if (selectedTunnel != null) if (selectedTunnel != null)
outState.putString(TunnelManager.KEY_SELECTED_TUNNEL, selectedTunnel.getName()); outState.putString(KEY_SELECTED_TUNNEL, selectedTunnel.getName());
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }

View File

@ -32,6 +32,7 @@ import com.wireguard.config.Config;
public class ConfigEditorFragment extends BaseFragment { public class ConfigEditorFragment extends BaseFragment {
private static final String KEY_LOCAL_CONFIG = "local_config"; private static final String KEY_LOCAL_CONFIG = "local_config";
private static final String KEY_LOCAL_NAME = "local_name"; private static final String KEY_LOCAL_NAME = "local_name";
private static final String KEY_ORIGINAL_NAME = "original_name";
private static final String TAG = ConfigEditorFragment.class.getSimpleName(); private static final String TAG = ConfigEditorFragment.class.getSimpleName();
private final ObservableField<String> localName = new ObservableField<>(""); private final ObservableField<String> localName = new ObservableField<>("");
@ -79,7 +80,7 @@ public class ConfigEditorFragment extends BaseFragment {
if (savedInstanceState != null) { if (savedInstanceState != null) {
localConfig = savedInstanceState.getParcelable(KEY_LOCAL_CONFIG); localConfig = savedInstanceState.getParcelable(KEY_LOCAL_CONFIG);
localName.set(savedInstanceState.getString(KEY_LOCAL_NAME)); localName.set(savedInstanceState.getString(KEY_LOCAL_NAME));
originalName = savedInstanceState.getString(TunnelManager.KEY_SELECTED_TUNNEL); originalName = savedInstanceState.getString(KEY_ORIGINAL_NAME);
} }
// Erase the remains of creating or editing a different tunnel. // Erase the remains of creating or editing a different tunnel.
if (getSelectedTunnel() != null && !getSelectedTunnel().getName().equals(originalName)) { if (getSelectedTunnel() != null && !getSelectedTunnel().getName().equals(originalName)) {
@ -166,7 +167,7 @@ public class ConfigEditorFragment extends BaseFragment {
public void onSaveInstanceState(final Bundle outState) { public void onSaveInstanceState(final Bundle outState) {
outState.putParcelable(KEY_LOCAL_CONFIG, localConfig); outState.putParcelable(KEY_LOCAL_CONFIG, localConfig);
outState.putString(KEY_LOCAL_NAME, localName.get()); outState.putString(KEY_LOCAL_NAME, localName.get());
outState.putString(TunnelManager.KEY_SELECTED_TUNNEL, originalName); outState.putString(KEY_ORIGINAL_NAME, originalName);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }

View File

@ -33,7 +33,6 @@ import java9.util.stream.StreamSupport;
@ApplicationScope @ApplicationScope
public final class TunnelManager { public final class TunnelManager {
public static final String KEY_PRIMARY_TUNNEL = "primary_config"; public static final String KEY_PRIMARY_TUNNEL = "primary_config";
public static final String KEY_SELECTED_TUNNEL = "selected_tunnel";
private static final Comparator<String> COMPARATOR = Comparators.<String>thenComparing( private static final Comparator<String> COMPARATOR = Comparators.<String>thenComparing(
String.CASE_INSENSITIVE_ORDER, Comparators.naturalOrder()); String.CASE_INSENSITIVE_ORDER, Comparators.naturalOrder());
private static final String KEY_RESTORE_ON_BOOT = "restore_on_boot"; private static final String KEY_RESTORE_ON_BOOT = "restore_on_boot";