From b2bbaf050cf4aa9bd8a3824afde5fb4ff675c9a4 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 26 Mar 2020 14:16:23 +0530 Subject: [PATCH] util: Start converting to Kotlin Signed-off-by: Harsh Shandilya --- .../android/util/ExceptionLoggers.java | 36 ------------------- .../android/util/ExceptionLoggers.kt | 27 ++++++++++++++ .../wireguard/android/util/FragmentUtils.java | 30 ---------------- .../wireguard/android/util/FragmentUtils.kt | 21 +++++++++++ 4 files changed, 48 insertions(+), 66 deletions(-) delete mode 100644 ui/src/main/java/com/wireguard/android/util/ExceptionLoggers.java create mode 100644 ui/src/main/java/com/wireguard/android/util/ExceptionLoggers.kt delete mode 100644 ui/src/main/java/com/wireguard/android/util/FragmentUtils.java create mode 100644 ui/src/main/java/com/wireguard/android/util/FragmentUtils.kt diff --git a/ui/src/main/java/com/wireguard/android/util/ExceptionLoggers.java b/ui/src/main/java/com/wireguard/android/util/ExceptionLoggers.java deleted file mode 100644 index 8a58afbe..00000000 --- a/ui/src/main/java/com/wireguard/android/util/ExceptionLoggers.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -package com.wireguard.android.util; - -import android.util.Log; - -import androidx.annotation.Nullable; -import java9.util.function.BiConsumer; - -/** - * Helpers for logging exceptions from asynchronous tasks. These can be passed to - * {@code CompletionStage.whenComplete()} at the end of an asynchronous future chain. - */ - -public enum ExceptionLoggers implements BiConsumer { - D(Log.DEBUG), - E(Log.ERROR); - - private static final String TAG = "WireGuard/" + ExceptionLoggers.class.getSimpleName(); - private final int priority; - - ExceptionLoggers(final int priority) { - this.priority = priority; - } - - @Override - public void accept(final Object result, @Nullable final Throwable throwable) { - if (throwable != null) - Log.println(Log.ERROR, TAG, Log.getStackTraceString(throwable)); - else if (priority <= Log.DEBUG) - Log.println(priority, TAG, "Future completed successfully"); - } -} diff --git a/ui/src/main/java/com/wireguard/android/util/ExceptionLoggers.kt b/ui/src/main/java/com/wireguard/android/util/ExceptionLoggers.kt new file mode 100644 index 00000000..3fe56128 --- /dev/null +++ b/ui/src/main/java/com/wireguard/android/util/ExceptionLoggers.kt @@ -0,0 +1,27 @@ +/* + * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ +package com.wireguard.android.util + +import android.util.Log +import java9.util.function.BiConsumer + +/** + * Helpers for logging exceptions from asynchronous tasks. These can be passed to + * `CompletionStage.whenComplete()` at the end of an asynchronous future chain. + */ +enum class ExceptionLoggers(private val priority: Int) : BiConsumer { + D(Log.DEBUG), E(Log.ERROR); + + override fun accept(result: Any?, throwable: Throwable?) { + if (throwable != null) + Log.println(Log.ERROR, TAG, Log.getStackTraceString(throwable)) + else if (priority <= Log.DEBUG) + Log.println(priority, TAG, "Future completed successfully") + } + + companion object { + private val TAG = "WireGuard/" + ExceptionLoggers::class.java.simpleName + } +} diff --git a/ui/src/main/java/com/wireguard/android/util/FragmentUtils.java b/ui/src/main/java/com/wireguard/android/util/FragmentUtils.java deleted file mode 100644 index f93fd60d..00000000 --- a/ui/src/main/java/com/wireguard/android/util/FragmentUtils.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0 - */ -package com.wireguard.android.util; - -import android.content.Context; -import android.view.ContextThemeWrapper; - -import com.wireguard.android.activity.SettingsActivity; -import com.wireguard.util.NonNullForAll; - -import androidx.preference.Preference; - -@NonNullForAll -public final class FragmentUtils { - private FragmentUtils() { - // Prevent instantiation - } - - public static SettingsActivity getPrefActivity(final Preference preference) { - final Context context = preference.getContext(); - if (context instanceof ContextThemeWrapper) { - if (context instanceof SettingsActivity) { - return ((SettingsActivity) context); - } - } - return null; - } -} diff --git a/ui/src/main/java/com/wireguard/android/util/FragmentUtils.kt b/ui/src/main/java/com/wireguard/android/util/FragmentUtils.kt new file mode 100644 index 00000000..90e7ab0c --- /dev/null +++ b/ui/src/main/java/com/wireguard/android/util/FragmentUtils.kt @@ -0,0 +1,21 @@ +/* + * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ +package com.wireguard.android.util + +import android.view.ContextThemeWrapper +import androidx.preference.Preference +import com.wireguard.android.activity.SettingsActivity + +object FragmentUtils { + fun getPrefActivity(preference: Preference): SettingsActivity { + val context = preference.context + if (context is ContextThemeWrapper) { + if (context is SettingsActivity) { + return context + } + } + throw IllegalStateException("Failed to resolve SettingsActivity") + } +}