diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-12-11 03:24:34 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-12-11 03:47:32 +0100 |
commit | dc2463a0ab032e09afbd4aaac5eeb1992c53d55e (patch) | |
tree | 392478daa48cd0b8883e98ad7426dc0e8c3e32d5 /app/src/main/java | |
parent | b514717076be159077cd1956928c1c51ba7b7225 (diff) |
Fix locale usage
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java')
12 files changed, 35 insertions, 49 deletions
diff --git a/app/src/main/java/com/wireguard/android/backend/GoBackend.java b/app/src/main/java/com/wireguard/android/backend/GoBackend.java index e9ac079b..b9cb573d 100644 --- a/app/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/app/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -27,7 +27,6 @@ import com.wireguard.config.Peer; import java.net.InetAddress; import java.util.Collections; -import java.util.Locale; import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -96,7 +95,7 @@ public final class GoBackend implements Backend { @Override public String getTypePrettyName() { - return context.getResources().getString(R.string.type_name_go_userspace); + return context.getString(R.string.type_name_go_userspace); } @Override @@ -112,7 +111,7 @@ public final class GoBackend implements Backend { if (state == originalState) return originalState; if (state == State.UP && currentTunnel != null) - throw new IllegalStateException(context.getResources().getString(R.string.multiple_tunnels_error)); + throw new IllegalStateException(context.getString(R.string.multiple_tunnels_error)); Log.d(TAG, "Changing tunnel " + tunnel.getName() + " to state " + state); setStateInternal(tunnel, tunnel.getConfig(), state); return getState(tunnel); @@ -124,10 +123,10 @@ public final class GoBackend implements Backend { if (state == State.UP) { Log.i(TAG, "Bringing tunnel up"); - Objects.requireNonNull(config, context.getResources().getString(R.string.no_config_error)); + Objects.requireNonNull(config, context.getString(R.string.no_config_error)); if (VpnService.prepare(context) != null) - throw new Exception(context.getResources().getString(R.string.vpn_not_authed_error)); + throw new Exception(context.getString(R.string.vpn_not_authed_error)); final VpnService service; if (!vpnService.isDone()) @@ -136,7 +135,7 @@ public final class GoBackend implements Backend { try { service = vpnService.get(2, TimeUnit.SECONDS); } catch (final TimeoutException e) { - throw new Exception(context.getResources().getString(R.string.vpn_start_error), e); + throw new Exception(context.getString(R.string.vpn_start_error), e); } if (currentTunnelHandle != -1) { @@ -174,12 +173,12 @@ public final class GoBackend implements Backend { builder.setBlocking(true); try (final ParcelFileDescriptor tun = builder.establish()) { if (tun == null) - throw new Exception(context.getResources().getString(R.string.tun_create_error)); + throw new Exception(context.getString(R.string.tun_create_error)); Log.d(TAG, "Go backend v" + wgVersion()); currentTunnelHandle = wgTurnOn(tunnel.getName(), tun.detachFd(), goConfig); } if (currentTunnelHandle < 0) - throw new Exception(String.format(Locale.getDefault(), context.getResources().getString(R.string.tunnel_on_error), currentTunnelHandle)); + throw new Exception(context.getString(R.string.tunnel_on_error, currentTunnelHandle)); currentTunnel = tunnel; diff --git a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java index 7535b95c..25f48d07 100644 --- a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java +++ b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java @@ -88,7 +88,7 @@ public final class WgQuickBackend implements Backend { @Override public String getTypePrettyName() { - return context.getResources().getString(R.string.type_name_kernel_module); + return context.getString(R.string.type_name_kernel_module); } @Override @@ -96,7 +96,7 @@ public final class WgQuickBackend implements Backend { final List<String> output = new ArrayList<>(); if (Application.getRootShell() .run(output, "cat /sys/module/wireguard/version") != 0 || output.isEmpty()) - throw new Exception(context.getResources().getString(R.string.module_version_error)); + throw new Exception(context.getString(R.string.module_version_error)); return output.get(0); } @@ -128,6 +128,6 @@ public final class WgQuickBackend implements Backend { // noinspection ResultOfMethodCallIgnored tempFile.delete(); if (result != 0) - throw new Exception(context.getResources().getString(R.string.tunnel_config_error)); + throw new Exception(context.getString(R.string.tunnel_config_error)); } } diff --git a/app/src/main/java/com/wireguard/android/configStore/FileConfigStore.java b/app/src/main/java/com/wireguard/android/configStore/FileConfigStore.java index 184dd6d5..a637eba4 100644 --- a/app/src/main/java/com/wireguard/android/configStore/FileConfigStore.java +++ b/app/src/main/java/com/wireguard/android/configStore/FileConfigStore.java @@ -18,7 +18,6 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.util.Locale; import java.util.Set; import java9.util.stream.Collectors; @@ -42,8 +41,7 @@ public final class FileConfigStore implements ConfigStore { Log.d(TAG, "Creating configuration for tunnel " + name); final File file = fileFor(name); if (!file.createNewFile()) - throw new IOException(String.format(Locale.getDefault(), - context.getResources().getString(R.string.config_file_exists_error), file.getName())); + throw new IOException(context.getString(R.string.config_file_exists_error, file.getName())); try (final FileOutputStream stream = new FileOutputStream(file, false)) { stream.write(config.toWgQuickString().getBytes(StandardCharsets.UTF_8)); } @@ -55,8 +53,7 @@ public final class FileConfigStore implements ConfigStore { Log.d(TAG, "Deleting configuration for tunnel " + name); final File file = fileFor(name); if (!file.delete()) - throw new IOException(String.format(Locale.getDefault(), - context.getResources().getString(R.string.config_delete_error), file.getName())); + throw new IOException(context.getString(R.string.config_delete_error, file.getName())); } @Override @@ -84,13 +81,11 @@ public final class FileConfigStore implements ConfigStore { final File file = fileFor(name); final File replacementFile = fileFor(replacement); if (!replacementFile.createNewFile()) - throw new IOException(String.format(Locale.getDefault(), - context.getResources().getString(R.string.config_exists_error), replacement)); + throw new IOException(context.getString(R.string.config_exists_error, replacement)); if (!file.renameTo(replacementFile)) { if (!replacementFile.delete()) Log.w(TAG, "Couldn't delete marker file for new name " + replacement); - throw new IOException(String.format(Locale.getDefault(), - context.getResources().getString(R.string.config_rename_error), file.getName())); + throw new IOException(context.getString(R.string.config_rename_error, file.getName())); } } @@ -99,8 +94,7 @@ public final class FileConfigStore implements ConfigStore { Log.d(TAG, "Saving configuration for tunnel " + name); final File file = fileFor(name); if (!file.isFile()) - throw new FileNotFoundException(String.format(Locale.getDefault(), - context.getResources().getString(R.string.config_not_found_error), file.getName())); + throw new FileNotFoundException(context.getString(R.string.config_not_found_error, file.getName())); try (final FileOutputStream stream = new FileOutputStream(file, false)) { stream.write(config.toWgQuickString().getBytes(StandardCharsets.UTF_8)); } diff --git a/app/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.java b/app/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.java index 878f9335..9c8a300d 100644 --- a/app/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/ConfigNamingDialogFragment.java @@ -24,7 +24,6 @@ import com.wireguard.config.ParseException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.util.Locale; import java.util.Objects; public class ConfigNamingDialogFragment extends DialogFragment { @@ -69,8 +68,7 @@ public class ConfigNamingDialogFragment extends DialogFragment { try { config = Config.parse(new ByteArrayInputStream(getArguments().getString(KEY_CONFIG_TEXT).getBytes(StandardCharsets.UTF_8))); } catch (final IOException | ParseException exception) { - throw new RuntimeException(String.format(Locale.getDefault(), - getResources().getString(R.string.invalid_config_error), getClass().getSimpleName()), exception); + throw new RuntimeException(getResources().getString(R.string.invalid_config_error, getClass().getSimpleName()), exception); } } diff --git a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java index 85d66baa..bbcbab94 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java @@ -115,8 +115,7 @@ public class TunnelListFragment extends BaseFragment { int idx = name.lastIndexOf('/'); if (idx >= 0) { if (idx >= name.length() - 1) - throw new IllegalArgumentException(String.format(Locale.getDefault(), - getResources().getString(R.string.illegal_filename_error), name)); + throw new IllegalArgumentException(getResources().getString(R.string.illegal_filename_error, name)); name = name.substring(idx + 1); } boolean isZip = name.toLowerCase(Locale.ENGLISH).endsWith(".zip"); diff --git a/app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java b/app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java index 5babb9ac..3836f9ac 100644 --- a/app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java +++ b/app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java @@ -45,7 +45,7 @@ public class LogExporterPreference extends Preference { final File file = new File(path, "wireguard-log.txt"); if (!path.isDirectory() && !path.mkdirs()) throw new IOException( - getContext().getResources().getString(R.string.create_output_dir_error)); + getContext().getString(R.string.create_output_dir_error)); /* We would like to simply run `builder.redirectOutput(file);`, but this is API 26. * Instead we have to do this dance, since logcat appends. diff --git a/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java b/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java index 9aa48d0a..f5385a2b 100644 --- a/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java +++ b/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java @@ -56,7 +56,7 @@ public class ZipExporterPreference extends Preference { futureConfigs.add(tunnel.getConfigAsync().toCompletableFuture()); if (futureConfigs.isEmpty()) { exportZipComplete(null, new IllegalArgumentException( - getContext().getResources().getString(R.string.no_tunnels_error))); + getContext().getString(R.string.no_tunnels_error))); return; } CompletableFuture.allOf(futureConfigs.toArray(new CompletableFuture[futureConfigs.size()])) @@ -67,7 +67,7 @@ public class ZipExporterPreference extends Preference { final File file = new File(path, "wireguard-export.zip"); if (!path.isDirectory() && !path.mkdirs()) throw new IOException( - getContext().getResources().getString(R.string.create_output_dir_error)); + getContext().getString(R.string.create_output_dir_error)); try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(file))) { for (int i = 0; i < futureConfigs.size(); ++i) { zip.putNextEntry(new ZipEntry(tunnels.get(i).getName() + ".conf")); diff --git a/app/src/main/java/com/wireguard/android/util/RootShell.java b/app/src/main/java/com/wireguard/android/util/RootShell.java index 0e04185a..e48e3564 100644 --- a/app/src/main/java/com/wireguard/android/util/RootShell.java +++ b/app/src/main/java/com/wireguard/android/util/RootShell.java @@ -20,7 +20,6 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; import java.util.Collection; -import java.util.Locale; import java.util.UUID; /** @@ -124,10 +123,9 @@ public class RootShell { } } if (markersSeen != 4) - throw new IOException(String.format(Locale.getDefault(), - context.getResources().getString(R.string.marker_count_error), markersSeen)); + throw new IOException(context.getString(R.string.marker_count_error, markersSeen)); if (errnoStdout != errnoStderr) - throw new IOException(context.getResources().getString(R.string.exit_status_read_error)); + throw new IOException(context.getString(R.string.exit_status_read_error)); Log.v(TAG, "exit: " + errnoStdout); return errnoStdout; } @@ -140,9 +138,9 @@ public class RootShell { if (isRunning()) return; if (!localBinaryDir.isDirectory() && !localBinaryDir.mkdirs()) - throw new FileNotFoundException(context.getResources().getString(R.string.create_bin_dir_error)); + throw new FileNotFoundException(context.getString(R.string.create_bin_dir_error)); if (!localTemporaryDir.isDirectory() && !localTemporaryDir.mkdirs()) - throw new FileNotFoundException(context.getResources().getString(R.string.create_temp_dir_error)); + throw new FileNotFoundException(context.getString(R.string.create_temp_dir_error)); try { final ProcessBuilder builder = new ProcessBuilder().command(SU); builder.environment().put("LC_ALL", "C"); @@ -172,8 +170,7 @@ public class RootShell { if (line.contains("Permission denied")) throw new NoRootException(deviceNotRootedMessage); } - throw new IOException(String.format(Locale.getDefault(), - context.getResources().getString(R.string.shell_start_error), process.exitValue())); + throw new IOException(context.getString(R.string.shell_start_error, process.exitValue())); } } catch (final IOException | NoRootException e) { stop(); diff --git a/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java b/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java index ebdfd3d3..dd381a18 100644 --- a/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java +++ b/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java @@ -106,7 +106,7 @@ public final class ToolsInstaller { } if (!areToolsAvailable) throw new FileNotFoundException( - context.getResources().getString(R.string.tools_unavailable_error)); + context.getString(R.string.tools_unavailable_error)); } } diff --git a/app/src/main/java/com/wireguard/config/Config.java b/app/src/main/java/com/wireguard/config/Config.java index 7b9bcab3..d2c1395f 100644 --- a/app/src/main/java/com/wireguard/config/Config.java +++ b/app/src/main/java/com/wireguard/config/Config.java @@ -30,9 +30,7 @@ public final class Config { private final List<Peer> peers; private Config(final Builder builder) { - if (builder.interfaze == null) - throw new IllegalArgumentException("An [Interface] section is required"); - interfaze = builder.interfaze; + interfaze = Objects.requireNonNull(builder.interfaze, "An [Interface] section is required"); // Defensively copy to ensure immutability even if the Builder is reused. peers = Collections.unmodifiableList(new ArrayList<>(builder.peers)); } @@ -189,6 +187,8 @@ public final class Config { } public Config build() { + if (interfaze == null) + throw new IllegalArgumentException("An [Interface] section is required"); return new Config(this); } diff --git a/app/src/main/java/com/wireguard/config/Interface.java b/app/src/main/java/com/wireguard/config/Interface.java index f97c4731..da30fbcc 100644 --- a/app/src/main/java/com/wireguard/config/Interface.java +++ b/app/src/main/java/com/wireguard/config/Interface.java @@ -44,14 +44,11 @@ public final class Interface { private final Optional<Integer> mtu; private Interface(final Builder builder) { - if (builder.keyPair == null) - throw new IllegalArgumentException("Interfaces must have a private key"); - // Defensively copy to ensure immutability even if the Builder is reused. addresses = Collections.unmodifiableSet(new LinkedHashSet<>(builder.addresses)); dnsServers = Collections.unmodifiableSet(new LinkedHashSet<>(builder.dnsServers)); excludedApplications = Collections.unmodifiableSet(new LinkedHashSet<>(builder.excludedApplications)); - keyPair = builder.keyPair; + keyPair = Objects.requireNonNull(builder.keyPair, "Interfaces must have a private key"); listenPort = builder.listenPort; mtu = builder.mtu; } @@ -264,6 +261,8 @@ public final class Interface { } public Interface build() { + if (keyPair == null) + throw new IllegalArgumentException("Interfaces must have a private key"); return new Interface(this); } diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java index 1eb30197..1250fcb3 100644 --- a/app/src/main/java/com/wireguard/config/Peer.java +++ b/app/src/main/java/com/wireguard/config/Peer.java @@ -35,14 +35,12 @@ public final class Peer { private final Key publicKey; private Peer(final Builder builder) { - if (builder.publicKey == null) - throw new IllegalArgumentException("Peers must have a public key"); // Defensively copy to ensure immutability even if the Builder is reused. allowedIps = Collections.unmodifiableSet(new LinkedHashSet<>(builder.allowedIps)); endpoint = builder.endpoint; persistentKeepalive = builder.persistentKeepalive; preSharedKey = builder.preSharedKey; - publicKey = builder.publicKey; + publicKey = Objects.requireNonNull(builder.publicKey, "Peers must have a public key"); } /** @@ -226,6 +224,8 @@ public final class Peer { } public Peer build() { + if (publicKey == null) + throw new IllegalArgumentException("Peers must have a public key"); return new Peer(this); } |