From d74b988f7586ce648be6860a21b22ff5370fb597 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 30 Mar 2020 13:57:43 -0600 Subject: global: cleanup code style Signed-off-by: Jason A. Donenfeld --- tunnel/src/main/AndroidManifest.xml | 3 +- .../android/backend/BackendException.java | 1 + .../com/wireguard/android/backend/GoBackend.java | 1 + .../java/com/wireguard/android/util/RootShell.java | 1 + .../wireguard/config/BadConfigExceptionTest.java | 19 ++++++------ .../test/java/com/wireguard/config/ConfigTest.java | 20 +++++++----- .../ObservableKeyedRecyclerViewAdapter.kt | 1 - .../databinding/ObservableSortedKeyedArrayList.kt | 1 - .../android/fragment/AppListDialogFragment.kt | 2 +- .../android/fragment/TunnelEditorFragment.kt | 1 - .../wireguard/android/model/ObservableTunnel.kt | 2 +- .../wireguard/android/model/TunnelComparator.kt | 3 ++ .../com/wireguard/android/model/TunnelManager.kt | 2 +- .../android/preference/ZipExporterPreference.kt | 6 ++-- .../java/com/wireguard/android/util/AsyncWorker.kt | 2 +- ui/src/main/res/layout/tunnel_detail_fragment.xml | 36 +++++++++++----------- ui/src/main/res/layout/tunnel_detail_peer.xml | 20 ++++++------ ui/src/main/res/values/styles.xml | 5 ++- 18 files changed, 69 insertions(+), 57 deletions(-) diff --git a/tunnel/src/main/AndroidManifest.xml b/tunnel/src/main/AndroidManifest.xml index 111e5e2d..ba9cc77c 100644 --- a/tunnel/src/main/AndroidManifest.xml +++ b/tunnel/src/main/AndroidManifest.xml @@ -5,8 +5,9 @@ + - diff --git a/tunnel/src/main/java/com/wireguard/android/backend/BackendException.java b/tunnel/src/main/java/com/wireguard/android/backend/BackendException.java index 55fdb602..5552b3ee 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/BackendException.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/BackendException.java @@ -11,6 +11,7 @@ import com.wireguard.util.NonNullForAll; public final class BackendException extends Exception { private final Object[] format; private final Reason reason; + public BackendException(final Reason reason, final Object... format) { this.reason = reason; this.format = format; diff --git a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java index 34008f45..019bc980 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -42,6 +42,7 @@ public final class GoBackend implements Backend { @Nullable private Config currentConfig; @Nullable private Tunnel currentTunnel; private int currentTunnelHandle = -1; + public GoBackend(final Context context) { SharedLibraryLoader.loadSharedLibrary(context, "wg-go"); this.context = context; diff --git a/tunnel/src/main/java/com/wireguard/android/util/RootShell.java b/tunnel/src/main/java/com/wireguard/android/util/RootShell.java index 9c175552..6fe89a83 100644 --- a/tunnel/src/main/java/com/wireguard/android/util/RootShell.java +++ b/tunnel/src/main/java/com/wireguard/android/util/RootShell.java @@ -189,6 +189,7 @@ public class RootShell { public static class RootShellException extends Exception { private final Object[] format; private final Reason reason; + public RootShellException(final Reason reason, final Object... format) { this.reason = reason; this.format = format; diff --git a/tunnel/src/test/java/com/wireguard/config/BadConfigExceptionTest.java b/tunnel/src/test/java/com/wireguard/config/BadConfigExceptionTest.java index 54935510..3743852d 100644 --- a/tunnel/src/test/java/com/wireguard/config/BadConfigExceptionTest.java +++ b/tunnel/src/test/java/com/wireguard/config/BadConfigExceptionTest.java @@ -18,9 +18,11 @@ import java.io.InputStream; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class BadConfigExceptionTest { + private static final Map CONFIG_MAP = new HashMap<>(); private static final String[] CONFIG_NAMES = { "invalid-key", "invalid-number", @@ -31,14 +33,6 @@ public class BadConfigExceptionTest { "unknown-attribute", "unknown-section" }; - private static final Map CONFIG_MAP = new HashMap<>(); - - @BeforeClass - public static void readConfigs() { - for (final String config: CONFIG_NAMES) { - CONFIG_MAP.put(config, BadConfigExceptionTest.class.getClassLoader().getResourceAsStream(config + ".conf")); - } - } @AfterClass public static void closeStreams() { @@ -50,6 +44,13 @@ public class BadConfigExceptionTest { } } + @BeforeClass + public static void readConfigs() { + for (final String config : CONFIG_NAMES) { + CONFIG_MAP.put(config, BadConfigExceptionTest.class.getClassLoader().getResourceAsStream(config + ".conf")); + } + } + @Test public void throws_correctly_with_INVALID_KEY_reason() { try { diff --git a/tunnel/src/test/java/com/wireguard/config/ConfigTest.java b/tunnel/src/test/java/com/wireguard/config/ConfigTest.java index 6d599217..693a37ea 100644 --- a/tunnel/src/test/java/com/wireguard/config/ConfigTest.java +++ b/tunnel/src/test/java/com/wireguard/config/ConfigTest.java @@ -5,7 +5,6 @@ package com.wireguard.config; -import static org.junit.Assert.*; import org.junit.Test; import java.io.IOException; @@ -15,8 +14,20 @@ import java.util.Collection; import java.util.HashSet; import java.util.Objects; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + public class ConfigTest { + @Test(expected = BadConfigException.class) + public void invalid_config_throws() throws IOException, BadConfigException { + try (final InputStream is = Objects.requireNonNull(getClass().getClassLoader()).getResourceAsStream("broken.conf")) { + Config.parse(is); + } + } + @Test public void valid_config_parses_correctly() throws IOException, ParseException { Config config = null; @@ -35,11 +46,4 @@ public class ConfigTest { assertEquals("Test config's allowed IPs are 0.0.0.0/0 and ::0/0", config.getPeers().get(0).getAllowedIps(), expectedAllowedIps); assertEquals("Test config has one DNS server", 1, config.getInterface().getDnsServers().size()); } - - @Test(expected = BadConfigException.class) - public void invalid_config_throws() throws IOException, BadConfigException { - try (final InputStream is = Objects.requireNonNull(getClass().getClassLoader()).getResourceAsStream("broken.conf")) { - Config.parse(is); - } - } } diff --git a/ui/src/main/java/com/wireguard/android/databinding/ObservableKeyedRecyclerViewAdapter.kt b/ui/src/main/java/com/wireguard/android/databinding/ObservableKeyedRecyclerViewAdapter.kt index c3bfb3bb..56591d6f 100644 --- a/ui/src/main/java/com/wireguard/android/databinding/ObservableKeyedRecyclerViewAdapter.kt +++ b/ui/src/main/java/com/wireguard/android/databinding/ObservableKeyedRecyclerViewAdapter.kt @@ -4,7 +4,6 @@ */ package com.wireguard.android.databinding -import android.annotation.SuppressLint import android.content.Context import android.view.LayoutInflater import android.view.ViewGroup diff --git a/ui/src/main/java/com/wireguard/android/databinding/ObservableSortedKeyedArrayList.kt b/ui/src/main/java/com/wireguard/android/databinding/ObservableSortedKeyedArrayList.kt index d00065a1..98e9e915 100644 --- a/ui/src/main/java/com/wireguard/android/databinding/ObservableSortedKeyedArrayList.kt +++ b/ui/src/main/java/com/wireguard/android/databinding/ObservableSortedKeyedArrayList.kt @@ -7,7 +7,6 @@ package com.wireguard.android.databinding import java.util.AbstractList import java.util.Collections import java.util.Comparator -import java.util.NoSuchElementException import java.util.Spliterator /** diff --git a/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt index 20f09354..8bf0cf14 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt @@ -15,9 +15,9 @@ import androidx.fragment.app.Fragment import com.wireguard.android.Application import com.wireguard.android.R import com.wireguard.android.databinding.AppListDialogFragmentBinding +import com.wireguard.android.databinding.ObservableKeyedArrayList import com.wireguard.android.model.ApplicationData import com.wireguard.android.util.ErrorMessages -import com.wireguard.android.databinding.ObservableKeyedArrayList class AppListDialogFragment : DialogFragment() { private val appData: ObservableKeyedArrayList = ObservableKeyedArrayList() diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt index 1c0cf2bd..31f049ab 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt @@ -4,7 +4,6 @@ */ package com.wireguard.android.fragment -import android.app.Activity import android.content.Context import android.os.Bundle import android.text.InputType diff --git a/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt b/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt index d5645e61..bb462b27 100644 --- a/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt +++ b/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt @@ -9,9 +9,9 @@ import androidx.databinding.Bindable import com.wireguard.android.BR import com.wireguard.android.backend.Statistics import com.wireguard.android.backend.Tunnel +import com.wireguard.android.databinding.Keyed import com.wireguard.android.util.ExceptionLoggers import com.wireguard.config.Config -import com.wireguard.android.databinding.Keyed import java9.util.concurrent.CompletableFuture import java9.util.concurrent.CompletionStage diff --git a/ui/src/main/java/com/wireguard/android/model/TunnelComparator.kt b/ui/src/main/java/com/wireguard/android/model/TunnelComparator.kt index 96e69c58..9fb96cab 100644 --- a/ui/src/main/java/com/wireguard/android/model/TunnelComparator.kt +++ b/ui/src/main/java/com/wireguard/android/model/TunnelComparator.kt @@ -25,7 +25,9 @@ object TunnelComparator : Comparator { return 0 } } + val tokens: MutableList = ArrayList() + init { for (s in NATURAL_SORT_DIGIT_FINDER.findAll(originalString.split(WHITESPACE_FINDER).joinToString(" ").toLowerCase(Locale.ENGLISH))) { try { @@ -36,6 +38,7 @@ object TunnelComparator : Comparator { } } } + private companion object { private val NATURAL_SORT_DIGIT_FINDER = Regex("""\d+|\D+""") private val WHITESPACE_FINDER = Regex("""\s""") diff --git a/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt b/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt index 7d3a5e37..5091ed3b 100644 --- a/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt +++ b/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt @@ -21,8 +21,8 @@ import com.wireguard.android.R import com.wireguard.android.backend.Statistics import com.wireguard.android.backend.Tunnel import com.wireguard.android.configStore.ConfigStore -import com.wireguard.android.util.ExceptionLoggers import com.wireguard.android.databinding.ObservableSortedKeyedArrayList +import com.wireguard.android.util.ExceptionLoggers import com.wireguard.config.Config import java9.util.concurrent.CompletableFuture import java9.util.concurrent.CompletionStage diff --git a/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt b/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt index 8ddce2b1..cdd25134 100644 --- a/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt @@ -97,9 +97,9 @@ class ZipExporterPreference(context: Context, attrs: AttributeSet?) : Preference } is BiometricAuthenticator.Result.Failure -> { Snackbar.make( - prefActivity.findViewById(android.R.id.content), - it.message, - Snackbar.LENGTH_SHORT + prefActivity.findViewById(android.R.id.content), + it.message, + Snackbar.LENGTH_SHORT ).show() } } diff --git a/ui/src/main/java/com/wireguard/android/util/AsyncWorker.kt b/ui/src/main/java/com/wireguard/android/util/AsyncWorker.kt index fd5eee2f..a6e5d4be 100644 --- a/ui/src/main/java/com/wireguard/android/util/AsyncWorker.kt +++ b/ui/src/main/java/com/wireguard/android/util/AsyncWorker.kt @@ -5,9 +5,9 @@ package com.wireguard.android.util import android.os.Handler -import java.util.concurrent.Executor import java9.util.concurrent.CompletableFuture import java9.util.concurrent.CompletionStage +import java.util.concurrent.Executor /** * Helper class for running asynchronous tasks and ensuring they are completed on the main thread. diff --git a/ui/src/main/res/layout/tunnel_detail_fragment.xml b/ui/src/main/res/layout/tunnel_detail_fragment.xml index ca1049e3..2da8a252 100644 --- a/ui/src/main/res/layout/tunnel_detail_fragment.xml +++ b/ui/src/main/res/layout/tunnel_detail_fragment.xml @@ -25,25 +25,25 @@ + android:orientation="vertical"> + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + app:layout_constraintEnd_toEndOf="parent" + app:onBeforeCheckedChanged="@{fragment::setTunnelState}" /> + app:layout_constraintTop_toBottomOf="@+id/interface_title" /> + tools:text="wg0" /> + app:layout_constraintTop_toBottomOf="@id/interface_name_text" /> + tools:text="wOs2eguFEohqIZxlSJ1CAT9584tc6ejj9hfGFsoBVkA=" /> + app:layout_constraintTop_toBottomOf="@id/public_key_text" /> + tools:text="fc00:bbbb:bbbb:bb11::3:368b/128" /> @@ -145,8 +145,8 @@ android:orientation="vertical" app:items="@{config.peers}" app:layout="@{@layout/tunnel_detail_peer}" - app:layout_constraintTop_toBottomOf="@+id/tunnel_detail_card" app:layout_constraintStart_toStartOf="parent" - tools:ignore="UselessLeaf"/> + app:layout_constraintTop_toBottomOf="@+id/tunnel_detail_card" + tools:ignore="UselessLeaf" /> diff --git a/ui/src/main/res/layout/tunnel_detail_peer.xml b/ui/src/main/res/layout/tunnel_detail_peer.xml index d34ae4c4..af939d9b 100644 --- a/ui/src/main/res/layout/tunnel_detail_peer.xml +++ b/ui/src/main/res/layout/tunnel_detail_peer.xml @@ -31,8 +31,8 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/peer" - app:layout_constraintTop_toTopOf="parent" - app:layout_constraintStart_toStartOf="parent"/> + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + app:layout_constraintTop_toBottomOf="@id/peer_title" /> + tools:text="wOs2eguFEohqIZxlSJ1CAT9584tc6ejj9hfGFsoBVkA=" /> + app:layout_constraintTop_toBottomOf="@+id/public_key_text" /> + tools:text="0.0.0.0/5, 8.0.0.0/7, 11.0.0.0/8, 12.0.0.0/6, 16.0.0.0/4, 32.0.0.0/3" /> + app:layout_constraintTop_toBottomOf="@+id/allowed_ips_text" /> + tools:text="1024 MB" + tools:visibility="visible" /> diff --git a/ui/src/main/res/values/styles.xml b/ui/src/main/res/values/styles.xml index a5079b8d..bfde4366 100644 --- a/ui/src/main/res/values/styles.xml +++ b/ui/src/main/res/values/styles.xml @@ -55,6 +55,7 @@ -- cgit v1.2.3