ConfigEditorFragment: Support renaming tunnels
Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
parent
30396b8718
commit
137325d3cc
@ -38,6 +38,7 @@ public class ConfigEditorFragment extends BaseFragment {
|
|||||||
private ConfigEditorFragmentBinding binding;
|
private ConfigEditorFragmentBinding binding;
|
||||||
private boolean isViewStateRestored;
|
private boolean isViewStateRestored;
|
||||||
private Config localConfig = new Config();
|
private Config localConfig = new Config();
|
||||||
|
private Tunnel localTunnel;
|
||||||
private String originalName;
|
private String originalName;
|
||||||
|
|
||||||
private static <T extends Parcelable> T copyParcelable(final T original) {
|
private static <T extends Parcelable> T copyParcelable(final T original) {
|
||||||
@ -51,21 +52,6 @@ public class ConfigEditorFragment extends BaseFragment {
|
|||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onConfigCreated(final Tunnel tunnel, final Throwable throwable) {
|
|
||||||
if (throwable != null) {
|
|
||||||
Log.e(TAG, "Cannot create tunnel", throwable);
|
|
||||||
final String message = "Cannot create tunnel: "
|
|
||||||
+ ExceptionLoggers.unwrap(throwable).getMessage();
|
|
||||||
if (binding != null) {
|
|
||||||
final CoordinatorLayout container = binding.mainContainer;
|
|
||||||
Snackbar.make(container, message, Snackbar.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Log.d(TAG, "Successfully created tunnel " + tunnel.getName());
|
|
||||||
onFinished(tunnel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onConfigLoaded(final Config config) {
|
private void onConfigLoaded(final Config config) {
|
||||||
localConfig = copyParcelable(config);
|
localConfig = copyParcelable(config);
|
||||||
if (binding != null && isViewStateRestored)
|
if (binding != null && isViewStateRestored)
|
||||||
@ -82,8 +68,8 @@ public class ConfigEditorFragment extends BaseFragment {
|
|||||||
Snackbar.make(container, message, Snackbar.LENGTH_LONG).show();
|
Snackbar.make(container, message, Snackbar.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "Successfully saved configuration for " + getSelectedTunnel().getName());
|
Log.d(TAG, "Successfully saved configuration for " + localTunnel.getName());
|
||||||
onFinished(getSelectedTunnel());
|
onFinished();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,6 +93,7 @@ public class ConfigEditorFragment extends BaseFragment {
|
|||||||
originalName = null;
|
originalName = null;
|
||||||
localName.set("");
|
localName.set("");
|
||||||
}
|
}
|
||||||
|
localTunnel = getSelectedTunnel();
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,7 +117,7 @@ public class ConfigEditorFragment extends BaseFragment {
|
|||||||
super.onDestroyView();
|
super.onDestroyView();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onFinished(final Tunnel tunnel) {
|
private void onFinished() {
|
||||||
// Hide the keyboard; it rarely goes away on its own.
|
// Hide the keyboard; it rarely goes away on its own.
|
||||||
final Activity activity = getActivity();
|
final Activity activity = getActivity();
|
||||||
final View focusedView = activity.getCurrentFocus();
|
final View focusedView = activity.getCurrentFocus();
|
||||||
@ -143,8 +130,11 @@ public class ConfigEditorFragment extends BaseFragment {
|
|||||||
}
|
}
|
||||||
// Tell the activity to finish itself or go back to the detail view.
|
// Tell the activity to finish itself or go back to the detail view.
|
||||||
getActivity().runOnUiThread(() -> {
|
getActivity().runOnUiThread(() -> {
|
||||||
setSelectedTunnel(null);
|
// The selected tunnel has to actually change, but we have to remember this one.
|
||||||
setSelectedTunnel(tunnel);
|
final Tunnel savedTunnel = localTunnel;
|
||||||
|
if (savedTunnel == getSelectedTunnel())
|
||||||
|
setSelectedTunnel(null);
|
||||||
|
setSelectedTunnel(savedTunnel);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,15 +142,19 @@ public class ConfigEditorFragment extends BaseFragment {
|
|||||||
public boolean onOptionsItemSelected(final MenuItem item) {
|
public boolean onOptionsItemSelected(final MenuItem item) {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case R.id.menu_action_save:
|
case R.id.menu_action_save:
|
||||||
if (getSelectedTunnel() != null) {
|
if (getSelectedTunnel() == null) {
|
||||||
Log.d(TAG, "Attempting to save config to " + getSelectedTunnel().getName());
|
|
||||||
getSelectedTunnel().setConfig(localConfig)
|
|
||||||
.whenComplete(this::onConfigSaved);
|
|
||||||
} else {
|
|
||||||
Log.d(TAG, "Attempting to create new tunnel " + localName.get());
|
Log.d(TAG, "Attempting to create new tunnel " + localName.get());
|
||||||
final TunnelManager manager = Application.getComponent().getTunnelManager();
|
final TunnelManager manager = Application.getComponent().getTunnelManager();
|
||||||
manager.create(localName.get(), localConfig)
|
manager.create(localName.get(), localConfig)
|
||||||
.whenComplete(this::onConfigCreated);
|
.whenComplete(this::onTunnelCreated);
|
||||||
|
} else if (!getSelectedTunnel().getName().equals(localName.get())) {
|
||||||
|
Log.d(TAG, "Attempting to rename tunnel to " + localName.get());
|
||||||
|
getSelectedTunnel().rename(localName.get())
|
||||||
|
.whenComplete(this::onTunnelRenamed);
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "Attempting to save config of " + getSelectedTunnel().getName());
|
||||||
|
getSelectedTunnel().setConfig(localConfig)
|
||||||
|
.whenComplete(this::onConfigSaved);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
@ -192,6 +186,43 @@ public class ConfigEditorFragment extends BaseFragment {
|
|||||||
originalName = null;
|
originalName = null;
|
||||||
localName.set("");
|
localName.set("");
|
||||||
}
|
}
|
||||||
|
localTunnel = newTunnel;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onTunnelCreated(final Tunnel tunnel, final Throwable throwable) {
|
||||||
|
if (throwable != null) {
|
||||||
|
Log.e(TAG, "Cannot create tunnel", throwable);
|
||||||
|
final String message = "Cannot create tunnel: "
|
||||||
|
+ ExceptionLoggers.unwrap(throwable).getMessage();
|
||||||
|
if (binding != null) {
|
||||||
|
final CoordinatorLayout container = binding.mainContainer;
|
||||||
|
Snackbar.make(container, message, Snackbar.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "Successfully created tunnel " + tunnel.getName());
|
||||||
|
localTunnel = tunnel;
|
||||||
|
onFinished();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void onTunnelRenamed(final Tunnel tunnel, final Throwable throwable) {
|
||||||
|
if (throwable != null) {
|
||||||
|
Log.e(TAG, "Cannot rename tunnel", throwable);
|
||||||
|
final String message = "Cannot rename tunnel: "
|
||||||
|
+ ExceptionLoggers.unwrap(throwable).getMessage();
|
||||||
|
if (binding != null) {
|
||||||
|
final CoordinatorLayout container = binding.mainContainer;
|
||||||
|
Snackbar.make(container, message, Snackbar.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "Successfully renamed tunnel to " + tunnel.getName());
|
||||||
|
localTunnel = tunnel;
|
||||||
|
// Now save the rest of configuration changes.
|
||||||
|
Log.d(TAG, "Attempting to save config of " + tunnel.getName());
|
||||||
|
tunnel.setConfig(localConfig)
|
||||||
|
.whenComplete(this::onConfigSaved);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -199,8 +230,6 @@ public class ConfigEditorFragment extends BaseFragment {
|
|||||||
super.onViewStateRestored(savedInstanceState);
|
super.onViewStateRestored(savedInstanceState);
|
||||||
binding.setConfig(localConfig);
|
binding.setConfig(localConfig);
|
||||||
binding.setName(localName);
|
binding.setName(localName);
|
||||||
// FIXME: Remove this when renaming works.
|
|
||||||
binding.interfaceNameText.setEnabled(originalName == null);
|
|
||||||
isViewStateRestored = true;
|
isViewStateRestored = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user