From 6eef4093d8a545d90c9d007bcc4ff794da6ee7f2 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sat, 6 Jan 2018 04:05:43 -0600 Subject: [PATCH] databinding: Remove unused classes and methods Signed-off-by: Samuel Holland --- .../android/databinding/BindingAdapters.java | 27 ---- .../databinding/ObservableListAdapter.java | 119 ----------------- .../databinding/ObservableMapAdapter.java | 123 ------------------ .../databinding/ObservableNavigableMap.java | 13 -- .../databinding/ObservableTreeMap.java | 65 --------- 5 files changed, 347 deletions(-) delete mode 100644 app/src/main/java/com/wireguard/android/databinding/ObservableListAdapter.java delete mode 100644 app/src/main/java/com/wireguard/android/databinding/ObservableMapAdapter.java delete mode 100644 app/src/main/java/com/wireguard/android/databinding/ObservableNavigableMap.java delete mode 100644 app/src/main/java/com/wireguard/android/databinding/ObservableTreeMap.java diff --git a/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java b/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java index 121c292b..116b40cb 100644 --- a/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java +++ b/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java @@ -90,33 +90,6 @@ public final class BindingAdapters { adapter.setList(newList); } - @BindingAdapter({"items", "layout"}) - public static , V> void setItems(final ListView view, - final ObservableNavigableMap oldMap, - final int oldLayoutId, - final ObservableNavigableMap newMap, - final int newLayoutId) { - if (oldMap == newMap && oldLayoutId == newLayoutId) - return; - // The ListAdapter interface is not generic, so this cannot be checked. - @SuppressWarnings("unchecked") - ObservableMapAdapter adapter = (ObservableMapAdapter) view.getAdapter(); - // If the layout changes, any existing adapter must be replaced. - if (adapter != null && oldMap != null && oldLayoutId != newLayoutId) { - adapter.setMap(null); - adapter = null; - } - // Avoid setting an adapter when there is no new list or layout. - if (newMap == null || newLayoutId == 0) - return; - if (adapter == null) { - adapter = new ObservableMapAdapter<>(view.getContext(), newLayoutId, newMap); - view.setAdapter(adapter); - } - // Either the list changed, or this is an entirely new listener because the layout changed. - adapter.setMap(newMap); - } - @BindingAdapter({"onBeforeCheckedChanged"}) public static void setOnBeforeCheckedChanged(final ToggleSwitch view, final ToggleSwitch.OnBeforeCheckedChangeListener diff --git a/app/src/main/java/com/wireguard/android/databinding/ObservableListAdapter.java b/app/src/main/java/com/wireguard/android/databinding/ObservableListAdapter.java deleted file mode 100644 index 5c4e8c0a..00000000 --- a/app/src/main/java/com/wireguard/android/databinding/ObservableListAdapter.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.wireguard.android.databinding; - -import android.content.Context; -import android.databinding.DataBindingUtil; -import android.databinding.ObservableList; -import android.databinding.ViewDataBinding; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; - -import com.wireguard.android.BR; - -import java.lang.ref.WeakReference; - -/** - * A generic ListAdapter backed by an ObservableList. - */ - -class ObservableListAdapter extends BaseAdapter { - private final OnListChangedCallback callback = new OnListChangedCallback<>(this); - private final int layoutId; - private final LayoutInflater layoutInflater; - private ObservableList list; - - ObservableListAdapter(final Context context, final int layoutId, final ObservableList list) { - this.layoutId = layoutId; - layoutInflater = LayoutInflater.from(context); - setList(list); - } - - @Override - public int getCount() { - return list != null ? list.size() : 0; - } - - @Override - public T getItem(final int position) { - if (list == null || position < 0 || position >= list.size()) - return null; - return list.get(position); - } - - @Override - public long getItemId(final int position) { - if (list == null || position < 0 || position >= list.size()) - return -1; - return list.get(position).hashCode(); - } - - @Override - public View getView(final int position, final View convertView, final ViewGroup parent) { - ViewDataBinding binding = DataBindingUtil.getBinding(convertView); - if (binding == null) - binding = DataBindingUtil.inflate(layoutInflater, layoutId, parent, false); - binding.setVariable(BR.collection, list); - binding.setVariable(BR.item, getItem(position)); - binding.executePendingBindings(); - return binding.getRoot(); - } - - @Override - public boolean hasStableIds() { - return true; - } - - void setList(final ObservableList newList) { - if (list != null) - list.removeOnListChangedCallback(callback); - list = newList; - if (list != null) { - list.addOnListChangedCallback(callback); - } - notifyDataSetChanged(); - } - - private static final class OnListChangedCallback - extends ObservableList.OnListChangedCallback> { - - private final WeakReference> weakAdapter; - - private OnListChangedCallback(final ObservableListAdapter adapter) { - weakAdapter = new WeakReference<>(adapter); - } - - @Override - public void onChanged(final ObservableList sender) { - final ObservableListAdapter adapter = weakAdapter.get(); - if (adapter != null) - adapter.notifyDataSetChanged(); - else - sender.removeOnListChangedCallback(this); - } - - @Override - public void onItemRangeChanged(final ObservableList sender, final int positionStart, - final int itemCount) { - onChanged(sender); - } - - @Override - public void onItemRangeInserted(final ObservableList sender, final int positionStart, - final int itemCount) { - onChanged(sender); - } - - @Override - public void onItemRangeMoved(final ObservableList sender, final int fromPosition, - final int toPosition, final int itemCount) { - onChanged(sender); - } - - @Override - public void onItemRangeRemoved(final ObservableList sender, final int positionStart, - final int itemCount) { - onChanged(sender); - } - } -} diff --git a/app/src/main/java/com/wireguard/android/databinding/ObservableMapAdapter.java b/app/src/main/java/com/wireguard/android/databinding/ObservableMapAdapter.java deleted file mode 100644 index 4f63d6dd..00000000 --- a/app/src/main/java/com/wireguard/android/databinding/ObservableMapAdapter.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.wireguard.android.databinding; - -import android.content.Context; -import android.databinding.DataBindingUtil; -import android.databinding.ObservableMap; -import android.databinding.ViewDataBinding; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; - -import com.wireguard.android.BR; - -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * A generic ListAdapter backed by a TreeMap that adds observability. - */ - -public class ObservableMapAdapter, V> extends BaseAdapter { - private final OnMapChangedCallback callback = new OnMapChangedCallback<>(this); - private final int layoutId; - private final LayoutInflater layoutInflater; - private List keys; - private ObservableNavigableMap map; - - ObservableMapAdapter(final Context context, final int layoutId, - final ObservableNavigableMap map) { - this.layoutId = layoutId; - layoutInflater = LayoutInflater.from(context); - setMap(map); - } - - @Override - public int getCount() { - return map != null ? map.size() : 0; - } - - @Override - public V getItem(final int position) { - if (map == null || position < 0 || position >= map.size()) - return null; - return map.get(getKey(position)); - } - - @Override - public long getItemId(final int position) { - if (map == null || position < 0 || position >= map.size()) - return -1; - //final V item = getItem(position); - //return item != null ? item.hashCode() : -1; - final K key = getKey(position); - return key.hashCode(); - } - - private K getKey(final int position) { - return getKeys().get(position); - } - - private List getKeys() { - if (keys == null) - keys = new ArrayList<>(map.keySet()); - return keys; - } - - public int getPosition(final K key) { - if (map == null || key == null) - return -1; - return Collections.binarySearch(getKeys(), key); - } - - @Override - public View getView(final int position, final View convertView, final ViewGroup parent) { - ViewDataBinding binding = DataBindingUtil.getBinding(convertView); - if (binding == null) - binding = DataBindingUtil.inflate(layoutInflater, layoutId, parent, false); - binding.setVariable(BR.collection, map); - binding.setVariable(BR.key, getKey(position)); - binding.setVariable(BR.item, getItem(position)); - binding.executePendingBindings(); - return binding.getRoot(); - } - - @Override - public boolean hasStableIds() { - return true; - } - - void setMap(final ObservableNavigableMap newMap) { - if (map != null) - map.removeOnMapChangedCallback(callback); - keys = null; - map = newMap; - if (map != null) { - map.addOnMapChangedCallback(callback); - } - notifyDataSetChanged(); - } - - private static final class OnMapChangedCallback, V> - extends ObservableMap.OnMapChangedCallback, K, V> { - - private final WeakReference> weakAdapter; - - private OnMapChangedCallback(final ObservableMapAdapter adapter) { - weakAdapter = new WeakReference<>(adapter); - } - - @Override - public void onMapChanged(final ObservableNavigableMap sender, final K key) { - final ObservableMapAdapter adapter = weakAdapter.get(); - if (adapter != null) { - adapter.keys = null; - adapter.notifyDataSetChanged(); - } else { - sender.removeOnMapChangedCallback(this); - } - } - } -} diff --git a/app/src/main/java/com/wireguard/android/databinding/ObservableNavigableMap.java b/app/src/main/java/com/wireguard/android/databinding/ObservableNavigableMap.java deleted file mode 100644 index ab8a4a1f..00000000 --- a/app/src/main/java/com/wireguard/android/databinding/ObservableNavigableMap.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.wireguard.android.databinding; - -import android.databinding.ObservableMap; - -import java.util.NavigableMap; - -/** - * Interface for maps that are both observable and sorted. - */ - -public interface ObservableNavigableMap extends NavigableMap, ObservableMap { - // No additional methods. -} diff --git a/app/src/main/java/com/wireguard/android/databinding/ObservableTreeMap.java b/app/src/main/java/com/wireguard/android/databinding/ObservableTreeMap.java deleted file mode 100644 index 074e122c..00000000 --- a/app/src/main/java/com/wireguard/android/databinding/ObservableTreeMap.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.wireguard.android.databinding; - -import android.databinding.MapChangeRegistry; -import android.databinding.ObservableMap; -import android.support.annotation.NonNull; - -import java.util.Map; -import java.util.TreeMap; - -/** - * Observable version of a TreeMap. Only notifies for changes made through methods, not iterators or - * views. This behavior is in line with that of ObservableArrayMap. - */ - -public class ObservableTreeMap extends TreeMap implements ObservableNavigableMap { - private transient MapChangeRegistry listeners; - - @Override - public void addOnMapChangedCallback( - final OnMapChangedCallback, K, V> listener) { - if (listeners == null) - listeners = new MapChangeRegistry(); - listeners.add(listener); - } - - @Override - public void clear() { - super.clear(); - notifyChange(null); - } - - private void notifyChange(final K key) { - if (listeners != null) - listeners.notifyChange(this, key); - } - - @Override - public V put(final K key, final V value) { - final V oldValue = super.put(key, value); - notifyChange(key); - return oldValue; - } - - @Override - public void putAll(@NonNull final Map map) { - super.putAll(map); - for (final K key : map.keySet()) - notifyChange(key); - } - - @Override - public V remove(final Object key) { - final V oldValue = super.remove(key); - @SuppressWarnings("unchecked") final K k = (K) key; - notifyChange(k); - return oldValue; - } - - @Override - public void removeOnMapChangedCallback( - final OnMapChangedCallback, K, V> listener) { - if (listeners != null) - listeners.remove(listener); - } -}