aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/view/UpdateView.java')
-rw-r--r--app/src/main/java/github/daneren2005/dsub/view/UpdateView.java83
1 files changed, 38 insertions, 45 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java b/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java
index a4727643..d977bdf0 100644
--- a/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java
+++ b/app/src/main/java/github/daneren2005/dsub/view/UpdateView.java
@@ -43,7 +43,7 @@ import github.daneren2005.dsub.util.SilentBackgroundTask;
public abstract class UpdateView<T> extends LinearLayout {
private static final String TAG = UpdateView.class.getSimpleName();
- private static final WeakHashMap<UpdateView, ?> INSTANCES = new WeakHashMap<UpdateView, Object>();
+ private static final WeakHashMap<UpdateView<?>, ?> INSTANCES = new WeakHashMap<>();
protected static Handler backgroundHandler;
protected static Handler uiHandler;
@@ -79,7 +79,7 @@ public abstract class UpdateView<T> extends LinearLayout {
this.autoUpdate = autoUpdate;
setLayoutParams(new AbsListView.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT,
+ ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
if(autoUpdate) {
@@ -110,9 +110,15 @@ public abstract class UpdateView<T> extends LinearLayout {
updateBackground();
update();
}
+
public void setObject(T obj1, Object obj2) {
- setObject(obj1, null);
+ if (obj2 == null) {
+ setObject(obj1);
+ } else {
+ setObject(obj1, null);
+ }
}
+
protected abstract void setObjectImpl(T obj);
private static synchronized void startUpdater() {
@@ -123,20 +129,13 @@ public abstract class UpdateView<T> extends LinearLayout {
uiHandler = new Handler();
// Needed so handler is never null until thread creates it
backgroundHandler = uiHandler;
- updateRunnable = new Runnable() {
- @Override
- public void run() {
- updateAll();
- }
- };
+ updateRunnable = UpdateView::updateAll;
- new Thread(new Runnable() {
- public void run() {
- Looper.prepare();
- backgroundHandler = new Handler(Looper.myLooper());
- uiHandler.post(updateRunnable);
- Looper.loop();
- }
+ new Thread(() -> {
+ Looper.prepare();
+ backgroundHandler = new Handler(Looper.myLooper());
+ uiHandler.post(updateRunnable);
+ Looper.loop();
}, "UpdateView").start();
}
@@ -156,8 +155,8 @@ public abstract class UpdateView<T> extends LinearLayout {
return;
}
- List<UpdateView> views = new ArrayList<UpdateView>();
- for (UpdateView view : INSTANCES.keySet()) {
+ List<UpdateView<?>> views = new ArrayList<>();
+ for (UpdateView<?> view : INSTANCES.keySet()) {
if (view.isShown()) {
views.add(view);
}
@@ -171,32 +170,26 @@ public abstract class UpdateView<T> extends LinearLayout {
Log.w(TAG, "Error when updating song views.", x);
}
}
- private static void updateAllLive(final List<UpdateView> views) {
- final Runnable runnable = new Runnable() {
- @Override
- public void run() {
- try {
- for(UpdateView view: views) {
- view.update();
- }
- } catch (Throwable x) {
- Log.w(TAG, "Error when updating song views.", x);
+ private static void updateAllLive(final List<UpdateView<?>> views) {
+ final Runnable runnable = () -> {
+ try {
+ for(UpdateView<?> view: views) {
+ view.update();
}
- uiHandler.postDelayed(updateRunnable, 1000L);
+ } catch (Throwable x) {
+ Log.w(TAG, "Error when updating song views.", x);
}
+ uiHandler.postDelayed(updateRunnable, 1000L);
};
- backgroundHandler.post(new Runnable() {
- @Override
- public void run() {
- try {
- for(UpdateView view: views) {
- view.updateBackground();
- }
- uiHandler.post(runnable);
- } catch (Throwable x) {
- Log.w(TAG, "Error when updating song views.", x);
+ backgroundHandler.post(() -> {
+ try {
+ for(UpdateView<?> view: views) {
+ view.updateBackground();
}
+ uiHandler.post(runnable);
+ } catch (Throwable x) {
+ Log.w(TAG, "Error when updating song views.", x);
}
});
}
@@ -218,7 +211,7 @@ public abstract class UpdateView<T> extends LinearLayout {
}
public static MusicDirectory.Entry findEntry(MusicDirectory.Entry entry) {
- for(UpdateView view: INSTANCES.keySet()) {
+ for(UpdateView<?> view: INSTANCES.keySet()) {
MusicDirectory.Entry check = null;
if(view instanceof SongView) {
check = ((SongView) view).getEntry();
@@ -297,7 +290,7 @@ public abstract class UpdateView<T> extends LinearLayout {
startBackgroundDrawable = child.getBackground();
child.setBackgroundColor(DrawableTint.getColorRes(context, R.attr.colorPrimary));
} else if (!checked && startBackgroundDrawable != null) {
- child.setBackgroundDrawable(startBackgroundDrawable);
+ child.setBackground(startBackgroundDrawable);
startBackgroundDrawable = null;
}
}
@@ -311,11 +304,11 @@ public abstract class UpdateView<T> extends LinearLayout {
}
public static class UpdateViewHolder<T> extends RecyclerView.ViewHolder {
- private UpdateView updateView;
- private View view;
+ private UpdateView<T> updateView;
+ private final View view;
private T item;
- public UpdateViewHolder(UpdateView itemView) {
+ public UpdateViewHolder(UpdateView<T> itemView) {
super(itemView);
this.updateView = itemView;
@@ -323,7 +316,7 @@ public abstract class UpdateView<T> extends LinearLayout {
}
// Different is so that call is not ambiguous
- public UpdateViewHolder(View view, boolean different) {
+ public UpdateViewHolder(View view) {
super(view);
this.view = view;
}