diff options
author | Samuel Holland <samuel@sholland.org> | 2018-09-05 20:17:14 -0500 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-12-08 02:39:41 +0100 |
commit | d1e85633fbe8d871355d2b9feb51e2c9983d8a21 (patch) | |
tree | d95ad1ae84d02fc3e18a211aa1e1ef8150d8fa35 /app/src/main/java/com/wireguard/android/util | |
parent | a264f7ab36bf1335999d53cb4a0d753c54b231d0 (diff) |
Remodel the Model
- The configuration and crypto model is now entirely independent
of Android classes other than Nullable and TextUtils.
- Model classes are immutable and use builders that enforce the
appropriate optional/required attributes.
- The Android config proxies (for Parcelable and databinding) are
moved to the Android side of the codebase, and are designed to be
safe for two-way databinding. This allows proper observability in
TunnelDetailFragment.
- Various robustness fixes and documentation updates to helper classes.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/util')
4 files changed, 40 insertions, 11 deletions
diff --git a/app/src/main/java/com/wireguard/android/util/ExceptionLoggers.java b/app/src/main/java/com/wireguard/android/util/ExceptionLoggers.java index a32e77a4..199b1fbd 100644 --- a/app/src/main/java/com/wireguard/android/util/ExceptionLoggers.java +++ b/app/src/main/java/com/wireguard/android/util/ExceptionLoggers.java @@ -5,9 +5,15 @@ package com.wireguard.android.util; +import android.content.res.Resources; import android.support.annotation.Nullable; import android.util.Log; +import com.wireguard.android.Application; +import com.wireguard.android.R; +import com.wireguard.config.ParseException; +import com.wireguard.crypto.Key; + import java9.util.concurrent.CompletionException; import java9.util.function.BiConsumer; @@ -34,12 +40,35 @@ public enum ExceptionLoggers implements BiConsumer<Object, Throwable> { return throwable; } - public static String unwrapMessage(Throwable throwable) { - throwable = unwrap(throwable); - final String message = throwable.getMessage(); - if (message != null) - return message; - return throwable.getClass().getSimpleName(); + public static String unwrapMessage(final Throwable throwable) { + final Throwable innerThrowable = unwrap(throwable); + final Resources resources = Application.get().getResources(); + String message; + if (innerThrowable instanceof ParseException) { + final ParseException parseException = (ParseException) innerThrowable; + message = resources.getString(R.string.parse_error, parseException.getText(), parseException.getContext()); + if (parseException.getMessage() != null) + message += ": " + parseException.getMessage(); + } else if (innerThrowable instanceof Key.KeyFormatException) { + final Key.KeyFormatException keyFormatException = (Key.KeyFormatException) innerThrowable; + switch (keyFormatException.getFormat()) { + case BASE64: + message = resources.getString(R.string.key_length_base64_exception_message); + break; + case BINARY: + message = resources.getString(R.string.key_length_exception_message); + break; + case HEX: + message = resources.getString(R.string.key_length_hex_exception_message); + break; + default: + // Will never happen, as getFormat is not nullable. + message = null; + } + } else { + message = throwable.getMessage(); + } + return message != null ? message : innerThrowable.getClass().getSimpleName(); } @Override diff --git a/app/src/main/java/com/wireguard/android/util/FragmentUtils.java b/app/src/main/java/com/wireguard/android/util/FragmentUtils.java index d5838a95..b7fdd095 100644 --- a/app/src/main/java/com/wireguard/android/util/FragmentUtils.java +++ b/app/src/main/java/com/wireguard/android/util/FragmentUtils.java @@ -11,7 +11,6 @@ import android.view.ContextThemeWrapper; import com.wireguard.android.activity.SettingsActivity; public final class FragmentUtils { - private FragmentUtils() { // Prevent instantiation } diff --git a/app/src/main/java/com/wireguard/android/util/ObservableKeyedArrayList.java b/app/src/main/java/com/wireguard/android/util/ObservableKeyedArrayList.java index 2ba87535..7af829fb 100644 --- a/app/src/main/java/com/wireguard/android/util/ObservableKeyedArrayList.java +++ b/app/src/main/java/com/wireguard/android/util/ObservableKeyedArrayList.java @@ -64,13 +64,15 @@ public class ObservableKeyedArrayList<K, E extends Keyed<? extends K>> return indexOfKey(key) >= 0; } - @Override @Nullable + @Nullable + @Override public E get(final K key) { final int index = indexOfKey(key); return index >= 0 ? get(index) : null; } - @Override @Nullable + @Nullable + @Override public E getLast(final K key) { final int index = lastIndexOfKey(key); return index >= 0 ? get(index) : null; diff --git a/app/src/main/java/com/wireguard/android/util/ObservableSortedKeyedArrayList.java b/app/src/main/java/com/wireguard/android/util/ObservableSortedKeyedArrayList.java index 7ef94106..d287d33d 100644 --- a/app/src/main/java/com/wireguard/android/util/ObservableSortedKeyedArrayList.java +++ b/app/src/main/java/com/wireguard/android/util/ObservableSortedKeyedArrayList.java @@ -28,8 +28,7 @@ import java.util.Spliterator; public class ObservableSortedKeyedArrayList<K, E extends Keyed<? extends K>> extends ObservableKeyedArrayList<K, E> implements ObservableSortedKeyedList<K, E> { - @Nullable - private final Comparator<? super K> comparator; + @Nullable private final Comparator<? super K> comparator; private final transient KeyList<K, E> keyList = new KeyList<>(this); @SuppressWarnings("WeakerAccess") |