summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2018-06-02 13:38:36 +0530
committerJason A. Donenfeld <Jason@zx2c4.com>2018-06-02 19:14:56 +0200
commit4671f59c67db5576ea340a2f586642b3243c85a4 (patch)
tree87ff1560eed926402bc29b1617f6799c1fd60a03
parent4986d92f3d5fb0d7bb4f15bf2b02f8b36ef23dff (diff)
android: Cleanup classes
- Use final modifer wherever possible - Use try-with-resources for input/output streams Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
-rw-r--r--app/src/main/java/com/wireguard/android/activity/SettingsActivity.java4
-rw-r--r--app/src/main/java/com/wireguard/android/backend/GoBackend.java13
-rw-r--r--app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java2
-rw-r--r--app/src/main/java/com/wireguard/android/configStore/FileConfigStore.java6
-rw-r--r--app/src/main/java/com/wireguard/android/model/TunnelManager.java8
-rw-r--r--app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java3
-rw-r--r--app/src/main/java/com/wireguard/android/preference/VersionPreference.java7
-rw-r--r--app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java3
-rw-r--r--app/src/main/java/com/wireguard/android/util/SharedLibraryLoader.java19
-rw-r--r--app/src/main/java/com/wireguard/android/util/ToolsInstaller.java2
-rw-r--r--app/src/main/java/com/wireguard/android/util/Topic.java14
11 files changed, 41 insertions, 40 deletions
diff --git a/app/src/main/java/com/wireguard/android/activity/SettingsActivity.java b/app/src/main/java/com/wireguard/android/activity/SettingsActivity.java
index d358c532..ed7c5457 100644
--- a/app/src/main/java/com/wireguard/android/activity/SettingsActivity.java
+++ b/app/src/main/java/com/wireguard/android/activity/SettingsActivity.java
@@ -100,7 +100,7 @@ public class SettingsActivity extends AppCompatActivity implements Topic.Subscri
}
@Override
- public void onTopicPublished(Topic topic) {
+ public void onTopicPublished(final Topic topic) {
recreate();
}
@@ -134,7 +134,7 @@ public class SettingsActivity extends AppCompatActivity implements Topic.Subscri
}
@Override
- public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
+ public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
if ("dark_theme".equals(key)) {
AppCompatDelegate.setDefaultNightMode(
sharedPreferences.getBoolean(key, false) ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
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 e1accdad..a6582cd6 100644
--- a/app/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/app/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -137,7 +137,7 @@ public final class GoBackend implements Backend {
// Build config
final Interface iface = config.getInterface();
final String goConfig;
- try (Formatter fmt = new Formatter(new StringBuilder())) {
+ try (final Formatter fmt = new Formatter(new StringBuilder())) {
fmt.format("replace_peers=true\n");
if (iface.getPrivateKey() != null)
fmt.format("private_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(iface.getPrivateKey())));
@@ -184,11 +184,12 @@ public final class GoBackend implements Backend {
builder.setMtu(mtu);
builder.setBlocking(true);
- final ParcelFileDescriptor tun = builder.establish();
- if (tun == null)
- throw new Exception("Unable to create tun device");
+ try (final ParcelFileDescriptor tun = builder.establish()) {
+ if (tun == null)
+ throw new Exception("Unable to create tun device");
- currentTunnelHandle = wgTurnOn(tunnel.getName(), tun.detachFd(), goConfig);
+ currentTunnelHandle = wgTurnOn(tunnel.getName(), tun.detachFd(), goConfig);
+ }
if (currentTunnelHandle < 0)
throw new Exception("Unable to turn tunnel on (wgTurnOn return " + currentTunnelHandle + ')');
@@ -237,7 +238,7 @@ public final class GoBackend implements Backend {
}
@Override
- public int onStartCommand(Intent intent, int flags, int startId) {
+ public int onStartCommand(final Intent intent, final int flags, final int startId) {
vpnService.complete(this);
if (intent == null || intent.getComponent() == null || !intent.getComponent().getPackageName().equals(getPackageName())) {
Log.d(TAG, "Service started by Always-on VPN feature");
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 1ffcfb41..6de40431 100644
--- a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java
+++ b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java
@@ -106,7 +106,7 @@ public final class WgQuickBackend implements Backend {
private void setStateInternal(final Tunnel tunnel, final Config config, final State state)
throws Exception {
final File tempFile = new File(localTemporaryDir, tunnel.getName() + ".conf");
- try (FileOutputStream stream = new FileOutputStream(tempFile, false)) {
+ try (final FileOutputStream stream = new FileOutputStream(tempFile, false)) {
stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
}
final String command = String.format("wg-quick %s '%s'",
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 ab801640..1655470b 100644
--- a/app/src/main/java/com/wireguard/android/configStore/FileConfigStore.java
+++ b/app/src/main/java/com/wireguard/android/configStore/FileConfigStore.java
@@ -42,7 +42,7 @@ public final class FileConfigStore implements ConfigStore {
final File file = fileFor(name);
if (!file.createNewFile())
throw new IOException("Configuration file " + file.getName() + " already exists");
- try (FileOutputStream stream = new FileOutputStream(file, false)) {
+ try (final FileOutputStream stream = new FileOutputStream(file, false)) {
stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
}
return config;
@@ -70,7 +70,7 @@ public final class FileConfigStore implements ConfigStore {
@Override
public Config load(final String name) throws IOException {
- try (FileInputStream stream = new FileInputStream(fileFor(name))) {
+ try (final FileInputStream stream = new FileInputStream(fileFor(name))) {
return Config.from(stream);
}
}
@@ -95,7 +95,7 @@ public final class FileConfigStore implements ConfigStore {
final File file = fileFor(name);
if (!file.isFile())
throw new FileNotFoundException("Configuration file " + file.getName() + " not found");
- try (FileOutputStream stream = new FileOutputStream(file, false)) {
+ try (final FileOutputStream stream = new FileOutputStream(file, false)) {
stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
}
return config;
diff --git a/app/src/main/java/com/wireguard/android/model/TunnelManager.java b/app/src/main/java/com/wireguard/android/model/TunnelManager.java
index b1197efe..8c9da7e1 100644
--- a/app/src/main/java/com/wireguard/android/model/TunnelManager.java
+++ b/app/src/main/java/com/wireguard/android/model/TunnelManager.java
@@ -57,7 +57,7 @@ public final class TunnelManager extends BaseObservable {
new ObservableSortedKeyedArrayList<>(COMPARATOR);
private Tunnel lastUsedTunnel;
private boolean haveLoaded;
- private ArrayList<CompletableFuture<Void>> delayedLoadRestoreTunnels = new ArrayList<>();
+ private final ArrayList<CompletableFuture<Void>> delayedLoadRestoreTunnels = new ArrayList<>();
@Inject
public TunnelManager(final AsyncWorker asyncWorker, final Backend backend,
@@ -150,7 +150,7 @@ public final class TunnelManager extends BaseObservable {
final String lastUsedName = preferences.getString(KEY_LAST_USED_TUNNEL, null);
if (lastUsedName != null)
setLastUsedTunnel(tunnels.get(lastUsedName));
- CompletableFuture<Void> toComplete[];
+ final CompletableFuture<Void>[] toComplete;
synchronized (delayedLoadRestoreTunnels) {
haveLoaded = true;
toComplete = delayedLoadRestoreTunnels.toArray(new CompletableFuture[delayedLoadRestoreTunnels.size()]);
@@ -175,12 +175,12 @@ public final class TunnelManager extends BaseObservable {
.whenComplete(ExceptionLoggers.E);
}
- public CompletionStage<Void> restoreState(boolean force) {
+ public CompletionStage<Void> restoreState(final boolean force) {
if (!force && !preferences.getBoolean(KEY_RESTORE_ON_BOOT, false))
return CompletableFuture.completedFuture(null);
synchronized (delayedLoadRestoreTunnels) {
if (!haveLoaded) {
- CompletableFuture<Void> f = new CompletableFuture<>();
+ final CompletableFuture<Void> f = new CompletableFuture<>();
delayedLoadRestoreTunnels.add(f);
return f;
}
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 cba30522..70d221a8 100644
--- a/app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java
+++ b/app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java
@@ -75,7 +75,8 @@ public class LogExporterPreference extends Preference {
throw new Exception(errors.toString());
}
}
- } catch (Exception e) {
+ } catch (final Exception e) {
+ // noinspection ResultOfMethodCallIgnored
file.delete();
throw e;
}
diff --git a/app/src/main/java/com/wireguard/android/preference/VersionPreference.java b/app/src/main/java/com/wireguard/android/preference/VersionPreference.java
index c0fb986b..b1a1cf49 100644
--- a/app/src/main/java/com/wireguard/android/preference/VersionPreference.java
+++ b/app/src/main/java/com/wireguard/android/preference/VersionPreference.java
@@ -27,10 +27,9 @@ public class VersionPreference extends Preference {
versionSummary = getContext().getString(R.string.version_userspace_summary, GoBackend.getVersion());
} else if (Application.getComponent().getBackendType() == WgQuickBackend.class) {
Application.getComponent().getToolsInstaller().getVersion().whenComplete((version, exception) -> {
- if (exception == null)
- versionSummary = getContext().getString(R.string.version_kernel_summary, version);
- else
- versionSummary = getContext().getString(R.string.version_kernel_unknown_summary);
+ versionSummary = exception == null
+ ? getContext().getString(R.string.version_kernel_summary, version)
+ : getContext().getString(R.string.version_kernel_unknown_summary);
notifyChanged();
});
}
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 b2f22a67..7625dec1 100644
--- a/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java
+++ b/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java
@@ -81,8 +81,7 @@ public class ZipExporterPreference extends Preference {
toString().getBytes(StandardCharsets.UTF_8));
}
zip.closeEntry();
- zip.close();
- } catch (Exception e) {
+ } catch (final Exception e) {
// noinspection ResultOfMethodCallIgnored
file.delete();
throw e;
diff --git a/app/src/main/java/com/wireguard/android/util/SharedLibraryLoader.java b/app/src/main/java/com/wireguard/android/util/SharedLibraryLoader.java
index 90ed8fcf..e9c3e647 100644
--- a/app/src/main/java/com/wireguard/android/util/SharedLibraryLoader.java
+++ b/app/src/main/java/com/wireguard/android/util/SharedLibraryLoader.java
@@ -22,7 +22,7 @@ public final class SharedLibraryLoader {
private SharedLibraryLoader() {
}
- public static final void loadSharedLibrary(final Context context, final String libName) {
+ public static void loadSharedLibrary(final Context context, final String libName) {
Throwable noAbiException;
try {
System.loadLibrary(libName);
@@ -35,12 +35,12 @@ public final class SharedLibraryLoader {
final ZipFile zipFile;
try {
zipFile = new ZipFile(new File(context.getApplicationInfo().sourceDir), ZipFile.OPEN_READ);
- } catch (IOException e) {
+ } catch (final IOException e) {
throw new RuntimeException(e);
}
final String mappedLibName = System.mapLibraryName(libName);
- byte buffer[] = new byte[1024 * 32];
+ final byte[] buffer = new byte[1024 * 32];
for (final String abi : Build.SUPPORTED_ABIS) {
final String libZipPath = "lib" + File.separatorChar + abi + File.separatorChar + mappedLibName;
final ZipEntry zipEntry = zipFile.getEntry(libZipPath);
@@ -50,13 +50,13 @@ public final class SharedLibraryLoader {
try {
f = File.createTempFile("lib", ".so", context.getCacheDir());
Log.d(TAG, "Extracting apk:/" + libZipPath + " to " + f.getAbsolutePath() + " and loading");
- FileOutputStream out = new FileOutputStream(f);
- InputStream in = zipFile.getInputStream(zipEntry);
- int len;
- while ((len = in.read(buffer)) != -1) {
- out.write(buffer, 0, len);
+ try (final FileOutputStream out = new FileOutputStream(f);
+ final InputStream in = zipFile.getInputStream(zipEntry)) {
+ int len;
+ while ((len = in.read(buffer)) != -1) {
+ out.write(buffer, 0, len);
+ }
}
- out.close();
System.load(f.getAbsolutePath());
return;
} catch (Exception e) {
@@ -64,6 +64,7 @@ public final class SharedLibraryLoader {
noAbiException = e;
} finally {
if (f != null)
+ // noinspection ResultOfMethodCallIgnored
f.delete();
}
}
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 f101bb32..de82e69c 100644
--- a/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java
+++ b/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java
@@ -58,7 +58,7 @@ public final class ToolsInstaller {
public CompletionStage<String> getVersion() {
return Application.getComponent().getAsyncWorker().supplyAsync(() -> {
- final ArrayList<String> output = new ArrayList<>();
+ final List<String> output = new ArrayList<>();
if (rootShell.run(output, "cat /sys/module/wireguard/version") != 0 || output.isEmpty())
throw new RuntimeException("Unable to determine kernel module version");
return output.get(0);
diff --git a/app/src/main/java/com/wireguard/android/util/Topic.java b/app/src/main/java/com/wireguard/android/util/Topic.java
index b7aee601..6d8080cf 100644
--- a/app/src/main/java/com/wireguard/android/util/Topic.java
+++ b/app/src/main/java/com/wireguard/android/util/Topic.java
@@ -28,7 +28,7 @@ public class Topic {
subscribers = new SyncArrayList<>();
}
- public synchronized void subscribe(Subscriber sub) {
+ public synchronized void subscribe(final Subscriber sub) {
subscribers.add(new WeakReference<>(sub));
}
@@ -36,10 +36,10 @@ public class Topic {
subscribers = new SyncArrayList<>();
}
- public synchronized void unsubscribe(Subscriber sub) {
+ public synchronized void unsubscribe(final Subscriber sub) {
List<WeakReference<Subscriber>> subs = subscribers;
subscribers = new ArrayList<>();
- for (WeakReference<Subscriber> subscriber : subs) {
+ for (final WeakReference<Subscriber> subscriber : subs) {
if (subscriber.get() != null && subscriber.get() != sub)
subscribers.add(subscriber);
}
@@ -64,7 +64,7 @@ public class Topic {
this.results = results;
// Snapshot
List<WeakReference<Subscriber>> subs = subscribers;
- for (WeakReference<Subscriber> subscriber : subs) {
+ for (final WeakReference<Subscriber> subscriber : subs) {
if (subscriber != null && subscriber.get() != null)
subscriber.get().onTopicPublished(this);
}
@@ -84,7 +84,7 @@ public class Topic {
public interface Subscriber {
default void subscribeTopics() {
- for (Topic topic : getSubscription()) {
+ for (final Topic topic : getSubscription()) {
if (topic.isPublished()) {
onTopicPublished(topic);
}
@@ -92,7 +92,7 @@ public class Topic {
}
}
default void unsubscribeTopics() {
- for (Topic event : getSubscription()) {
+ for (final Topic event : getSubscription()) {
event.unsubscribe(this);
}
}
@@ -102,7 +102,7 @@ public class Topic {
private static class SyncArrayList<E> extends ArrayList<E> {
@Override
- public synchronized boolean add(E e) {
+ public synchronized boolean add(final E e) {
return super.add(e);
}
}