Fragment/Tile: Make success/error messages translatable

Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
Samuel Holland
2018-01-08 20:30:03 -06:00
parent 6361628e16
commit 710ffc7bb3
8 changed files with 102 additions and 74 deletions
@@ -19,7 +19,6 @@ import com.wireguard.android.databinding.TunnelListItemBinding;
import com.wireguard.android.model.Tunnel;
import com.wireguard.android.model.Tunnel.State;
import com.wireguard.android.util.ExceptionLoggers;
import com.wireguard.android.util.RootShell;
/**
* Helper method shared by TunnelListFragment and TunnelDetailFragment.
@@ -48,7 +47,6 @@ public final class TunnelController {
tunnel.setState(State.of(checked)).whenComplete((state, throwable) -> {
if (throwable == null)
return;
Log.e(TAG, "Cannot set state of tunnel " + tunnel.getName(), throwable);
final Context context = view.getContext();
if (throwable instanceof ErrnoException
&& ((ErrnoException) throwable).errno == OsConstants.ENODEV) {
@@ -61,13 +59,13 @@ public final class TunnelController {
// Make links work.
((TextView) dialog.findViewById(android.R.id.message))
.setMovementMethod(LinkMovementMethod.getInstance());
} else if (throwable instanceof RootShell.NoRootException) {
Snackbar.make(view, R.string.error_rootshell, Snackbar.LENGTH_LONG).show();
Log.e(TAG, "WireGuard not supported");
} else {
final String message =
context.getString(checked ? R.string.error_up : R.string.error_down) + ": "
+ ExceptionLoggers.unwrap(throwable).getMessage();
final String error = ExceptionLoggers.unwrap(throwable).getMessage();
final int messageResId = checked ? R.string.error_up : R.string.error_down;
final String message = context.getString(messageResId, error);
Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
Log.e(TAG, message, throwable);
}
});
}
@@ -60,17 +60,19 @@ public class TunnelEditorFragment extends BaseFragment {
}
private void onConfigSaved(final Config config, final Throwable throwable) {
if (throwable != null) {
Log.e(TAG, "Cannot save configuration", throwable);
final String message = "Cannot save configuration: "
+ ExceptionLoggers.unwrap(throwable).getMessage();
final String message;
if (throwable == null) {
message = getString(R.string.config_save_success, localTunnel.getName());
Log.d(TAG, message);
onFinished();
} else {
final String error = ExceptionLoggers.unwrap(throwable).getMessage();
message = getString(R.string.config_save_error, localTunnel.getName(), error);
Log.e(TAG, message, throwable);
if (binding != null) {
final CoordinatorLayout container = binding.mainContainer;
Snackbar.make(container, message, Snackbar.LENGTH_LONG).show();
}
} else {
Log.d(TAG, "Successfully saved configuration for " + localTunnel.getName());
onFinished();
}
}
@@ -191,38 +193,40 @@ public class TunnelEditorFragment extends BaseFragment {
}
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();
final String message;
if (throwable == null) {
message = getString(R.string.tunnel_create_success, tunnel.getName());
Log.d(TAG, message);
onFinished();
} else {
final String error = ExceptionLoggers.unwrap(throwable).getMessage();
message = getString(R.string.tunnel_create_error, error);
Log.e(TAG, message, throwable);
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();
final String message;
if (throwable == null) {
message = getString(R.string.tunnel_rename_success, localTunnel.getName(),
tunnel.getName());
Log.d(TAG, message);
localTunnel = tunnel;
// Now save the rest of configuration changes.
Log.d(TAG, "Attempting to save config of renamed tunnel " + tunnel.getName());
tunnel.setConfig(localConfig).whenComplete(this::onConfigSaved);
} else {
final String error = ExceptionLoggers.unwrap(throwable).getMessage();
message = getString(R.string.tunnel_rename_error, error);
Log.e(TAG, message, throwable);
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);
}
}
@@ -148,13 +148,12 @@ public class TunnelListFragment extends BaseFragment {
private void onTunnelDeletionFinished(final Integer count, final Throwable throwable) {
final String message;
final String plural = count == 1 ? "" : "s";
if (throwable == null) {
message = "Successfully deleted " + count + " tunnel" + plural;
message = getResources().getQuantityString(R.plurals.delete_success, count, count);
} else {
message = "Unable to delete tunnel" + plural + ": "
+ ExceptionLoggers.unwrap(throwable).getMessage();
Log.e(TAG, "Cannot delete tunnel" + plural, throwable);
final String error = ExceptionLoggers.unwrap(throwable).getMessage();
message = getResources().getQuantityString(R.plurals.delete_error, count, count, error);
Log.e(TAG, message, throwable);
}
if (binding != null) {
final CoordinatorLayout container = binding.mainContainer;
@@ -165,11 +164,11 @@ public class TunnelListFragment extends BaseFragment {
private void onTunnelImportFinished(final Tunnel tunnel, final Throwable throwable) {
final String message;
if (throwable == null) {
message = "Successfully imported tunnel '" + tunnel.getName() + '\'';
message = getString(R.string.import_success, tunnel.getName());
} else {
message = "Cannot import tunnel: "
+ ExceptionLoggers.unwrap(throwable).getMessage();
Log.e(TAG, "Cannot import tunnel", throwable);
final String error = ExceptionLoggers.unwrap(throwable).getMessage();
message = getString(R.string.import_error, error);
Log.e(TAG, message, throwable);
}
if (binding != null) {
final CoordinatorLayout container = binding.mainContainer;
@@ -245,7 +244,7 @@ public class TunnelListFragment extends BaseFragment {
private void updateTitle(final ActionMode mode) {
final int count = (int) getCheckedPositions().count();
mode.setTitle(resources.getQuantityString(R.plurals.list_delete_title, count, count));
mode.setTitle(resources.getQuantityString(R.plurals.delete_title, count, count));
}
}