From 918d9b8b1fd0edaa390b1092023b3642be2bc202 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 27 Mar 2020 03:58:35 -0600 Subject: databinding: simplify and address warnings Signed-off-by: Jason A. Donenfeld --- .../android/databinding/BindingAdapters.kt | 4 +-- .../ObservableKeyedRecyclerViewAdapter.kt | 2 ++ .../databinding/ObservableSortedKeyedArrayList.kt | 31 +++------------------- .../preference/KernelModuleDisablerPreference.kt | 2 +- 4 files changed, 9 insertions(+), 30 deletions(-) (limited to 'ui/src/main/java/com/wireguard') diff --git a/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt b/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt index 87248b57..3460e96e 100644 --- a/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt +++ b/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt @@ -90,7 +90,7 @@ object BindingAdapters { @BindingAdapter(requireAll = false, value = ["items", "layout", "configurationHandler"]) fun > setItems(view: RecyclerView, oldList: ObservableKeyedArrayList?, oldLayoutId: Int, - oldRowConfigurationHandler: RowConfigurationHandler<*, *>?, + @Suppress("UNUSED_PARAMETER") oldRowConfigurationHandler: RowConfigurationHandler<*, *>?, newList: ObservableKeyedArrayList?, newLayoutId: Int, newRowConfigurationHandler: RowConfigurationHandler<*, *>?) { if (view.layoutManager == null) @@ -98,7 +98,7 @@ object BindingAdapters { if (oldList === newList && oldLayoutId == newLayoutId) return // The ListAdapter interface is not generic, so this cannot be checked. - var adapter = view.adapter as ObservableKeyedRecyclerViewAdapter? + @Suppress("UNCHECKED_CAST") var adapter = view.adapter as? ObservableKeyedRecyclerViewAdapter? // If the layout changes, any existing adapter must be replaced. if (adapter != null && oldList != null && oldLayoutId != newLayoutId) { adapter.setList(null) 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 72c2495b..c3bfb3bb 100644 --- a/ui/src/main/java/com/wireguard/android/databinding/ObservableKeyedRecyclerViewAdapter.kt +++ b/ui/src/main/java/com/wireguard/android/databinding/ObservableKeyedRecyclerViewAdapter.kt @@ -4,6 +4,7 @@ */ package com.wireguard.android.databinding +import android.annotation.SuppressLint import android.content.Context import android.view.LayoutInflater import android.view.ViewGroup @@ -55,6 +56,7 @@ class ObservableKeyedRecyclerViewAdapter> internal construct } fun setRowConfigurationHandler(rowConfigurationHandler: RowConfigurationHandler<*, *>?) { + @Suppress("UNCHECKED_CAST") this.rowConfigurationHandler = rowConfigurationHandler as? RowConfigurationHandler } 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 9261d202..d00065a1 100644 --- a/ui/src/main/java/com/wireguard/android/databinding/ObservableSortedKeyedArrayList.kt +++ b/ui/src/main/java/com/wireguard/android/databinding/ObservableSortedKeyedArrayList.kt @@ -16,7 +16,7 @@ import java.util.Spliterator * array-based nature of this class, insertion and removal of elements with anything but the largest * key still require O(n) time. */ -class ObservableSortedKeyedArrayList>(private val comparator: Comparator?) : ObservableKeyedArrayList() { +class ObservableSortedKeyedArrayList>(private val comparator: Comparator) : ObservableKeyedArrayList() { @Transient private val keyList = KeyList(this) @@ -54,34 +54,15 @@ class ObservableSortedKeyedArrayList>(private val comparator return true } - private fun getInsertionPoint(e: E): Int { - return if (comparator != null) { - -Collections.binarySearch(keyList, e.key, comparator) - 1 - } else { - val list = keyList as List> - -Collections.binarySearch(list, e.key) - 1 - } - } + private fun getInsertionPoint(e: E) = -Collections.binarySearch(keyList, e.key, comparator) - 1 override fun indexOfKey(key: K): Int { - val index: Int - index = if (comparator != null) { - Collections.binarySearch(keyList, key, comparator) - } else { - val list = keyList as List> - Collections.binarySearch(list, key) - } + val index = Collections.binarySearch(keyList, key, comparator) return if (index >= 0) index else -1 } override fun set(index: Int, element: E): E { - val order: Int - order = if (comparator != null) { - comparator.compare(element.key, get(index).key) - } else { - val key = element.key as Comparable - key.compareTo(get(index).key) - } + val order = comparator.compare(element.key, get(index).key) if (order != 0) { // Allow replacement if the new key would be inserted adjacent to the replaced element. val insertionPoint = getInsertionPoint(element) @@ -91,10 +72,6 @@ class ObservableSortedKeyedArrayList>(private val comparator return super.set(index, element) } - fun values(): Collection { - return this - } - private class KeyList>(private val list: ObservableSortedKeyedArrayList) : AbstractList(), Set { override fun get(index: Int): K = list[index].key diff --git a/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt b/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt index 1762fa58..8e834260 100644 --- a/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/KernelModuleDisablerPreference.kt @@ -35,7 +35,7 @@ class KernelModuleDisablerPreference(context: Context, attrs: AttributeSet?) : P } Application.getAsyncWorker().runAsync { Application.getTunnelManager().tunnels.thenApply { observableTunnels -> - val downings = observableTunnels.values().map { it.setStateAsync(Tunnel.State.DOWN).toCompletableFuture() }.toTypedArray() + val downings = observableTunnels.map { it.setStateAsync(Tunnel.State.DOWN).toCompletableFuture() }.toTypedArray() CompletableFuture.allOf(*downings).thenRun { val restartIntent = Intent(context, SettingsActivity::class.java) restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) -- cgit v1.2.3