From 239dd1d5a130fc7e6949a3b396b368b617466965 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 20 Sep 2016 12:11:06 -0700 Subject: [PATCH] Only display box art progress bar if a network load is required --- .../com/limelight/grid/AppGridAdapter.java | 11 ++--- .../limelight/grid/GenericGridAdapter.java | 13 +---- .../com/limelight/grid/PcGridAdapter.java | 17 ++++--- .../grid/assets/CachedAppAssetLoader.java | 47 +++++++++++++------ 4 files changed, 49 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/limelight/grid/AppGridAdapter.java b/app/src/main/java/com/limelight/grid/AppGridAdapter.java index b28f53d9..20957d75 100644 --- a/app/src/main/java/com/limelight/grid/AppGridAdapter.java +++ b/app/src/main/java/com/limelight/grid/AppGridAdapter.java @@ -3,6 +3,7 @@ package com.limelight.grid; import android.app.Activity; import android.graphics.BitmapFactory; import android.widget.ImageView; +import android.widget.ProgressBar; import android.widget.TextView; import com.limelight.AppView; @@ -82,9 +83,10 @@ public class AppGridAdapter extends GenericGridAdapter { itemList.remove(app); } - public boolean populateImageView(ImageView imgView, AppView.AppObject obj) { + @Override + public boolean populateImageView(ImageView imgView, ProgressBar prgView, AppView.AppObject obj) { // Let the cached asset loader handle it - loader.populateImageView(obj.app, imgView); + loader.populateImageView(obj.app, imgView, prgView); return true; } @@ -108,9 +110,4 @@ public class AppGridAdapter extends GenericGridAdapter { // No overlay return false; } - - @Override - public boolean shouldShowProgressBar(AppView.AppObject obj) { - return true; - } } diff --git a/app/src/main/java/com/limelight/grid/GenericGridAdapter.java b/app/src/main/java/com/limelight/grid/GenericGridAdapter.java index 7f8826ea..9e574a33 100644 --- a/app/src/main/java/com/limelight/grid/GenericGridAdapter.java +++ b/app/src/main/java/com/limelight/grid/GenericGridAdapter.java @@ -45,10 +45,9 @@ public abstract class GenericGridAdapter extends BaseAdapter { return i; } - public abstract boolean populateImageView(ImageView imgView, T obj); + public abstract boolean populateImageView(ImageView imgView, ProgressBar prgView, T obj); public abstract boolean populateTextView(TextView txtView, T obj); public abstract boolean populateOverlayView(ImageView overlayView, T obj); - public abstract boolean shouldShowProgressBar(T obj); @Override public View getView(int i, View convertView, ViewGroup viewGroup) { @@ -61,16 +60,8 @@ public abstract class GenericGridAdapter extends BaseAdapter { TextView txtView = (TextView) convertView.findViewById(R.id.grid_text); ProgressBar prgView = (ProgressBar) convertView.findViewById(R.id.grid_spinner); - if (prgView != null) { - if (shouldShowProgressBar(itemList.get(i))) { - prgView.setVisibility(View.VISIBLE); - } - else { - prgView.setVisibility(View.INVISIBLE); - } - } if (imgView != null) { - if (!populateImageView(imgView, itemList.get(i))) { + if (!populateImageView(imgView, prgView, itemList.get(i))) { imgView.setImageBitmap(null); } } diff --git a/app/src/main/java/com/limelight/grid/PcGridAdapter.java b/app/src/main/java/com/limelight/grid/PcGridAdapter.java index f28b0bfb..a0170090 100644 --- a/app/src/main/java/com/limelight/grid/PcGridAdapter.java +++ b/app/src/main/java/com/limelight/grid/PcGridAdapter.java @@ -1,7 +1,9 @@ package com.limelight.grid; import android.content.Context; +import android.view.View; import android.widget.ImageView; +import android.widget.ProgressBar; import android.widget.TextView; import com.limelight.PcView; @@ -36,7 +38,7 @@ public class PcGridAdapter extends GenericGridAdapter { } @Override - public boolean populateImageView(ImageView imgView, PcView.ComputerObject obj) { + public boolean populateImageView(ImageView imgView, ProgressBar prgView, PcView.ComputerObject obj) { if (obj.details.state == ComputerDetails.State.ONLINE) { imgView.setAlpha(1.0f); } @@ -44,6 +46,13 @@ public class PcGridAdapter extends GenericGridAdapter { imgView.setAlpha(0.4f); } + if (obj.details.reachability == ComputerDetails.Reachability.UNKNOWN) { + prgView.setVisibility(View.VISIBLE); + } + else { + prgView.setVisibility(View.INVISIBLE); + } + imgView.setImageResource(R.drawable.ic_computer); return true; } @@ -70,10 +79,4 @@ public class PcGridAdapter extends GenericGridAdapter { } return false; } - - @Override - public boolean shouldShowProgressBar(PcView.ComputerObject obj) { - // Still refreshing this PC so display the progress bar - return obj.details.reachability == ComputerDetails.Reachability.UNKNOWN; - } } diff --git a/app/src/main/java/com/limelight/grid/assets/CachedAppAssetLoader.java b/app/src/main/java/com/limelight/grid/assets/CachedAppAssetLoader.java index 66f1e093..2c85ab7f 100644 --- a/app/src/main/java/com/limelight/grid/assets/CachedAppAssetLoader.java +++ b/app/src/main/java/com/limelight/grid/assets/CachedAppAssetLoader.java @@ -5,7 +5,9 @@ import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.AsyncTask; +import android.view.View; import android.widget.ImageView; +import android.widget.ProgressBar; import com.limelight.nvstream.http.ComputerDetails; import com.limelight.nvstream.http.NvApp; @@ -130,12 +132,14 @@ public class CachedAppAssetLoader { private class LoaderTask extends AsyncTask { private final WeakReference imageViewRef; + private final WeakReference progressViewRef; private final boolean diskOnly; private LoaderTuple tuple; - public LoaderTask(ImageView imageView, boolean diskOnly) { + public LoaderTask(ImageView imageView, ProgressBar prgView, boolean diskOnly) { this.imageViewRef = new WeakReference<>(imageView); + this.progressViewRef = new WeakReference<>(prgView); this.diskOnly = diskOnly; } @@ -143,8 +147,8 @@ public class CachedAppAssetLoader { protected Bitmap doInBackground(LoaderTuple... params) { tuple = params[0]; - // Check whether it has been cancelled or the image view is gone - if (isCancelled() || imageViewRef.get() == null) { + // Check whether it has been cancelled or the views are gone + if (isCancelled() || imageViewRef.get() == null || progressViewRef.get() == null) { return null; } @@ -177,11 +181,17 @@ public class CachedAppAssetLoader { // If the current loader task for this view isn't us, do nothing final ImageView imageView = imageViewRef.get(); + final ProgressBar prgView = progressViewRef.get(); if (getLoaderTask(imageView) == this) { + // Now display the progress bar since we have to hit the network + if (prgView != null) { + prgView.setVisibility(View.VISIBLE); + } + // Set off another loader task on the network executor - LoaderTask task = new LoaderTask(imageView, false); + LoaderTask task = new LoaderTask(imageView, prgView, false); AsyncDrawable asyncDrawable = new AsyncDrawable(imageView.getResources(), placeholderBitmap, task); - imageView.setAlpha(1.0f); + imageView.setVisibility(View.VISIBLE); imageView.setImageDrawable(asyncDrawable); task.executeOnExecutor(networkExecutor, tuple); } @@ -195,14 +205,20 @@ public class CachedAppAssetLoader { } final ImageView imageView = imageViewRef.get(); + final ProgressBar prgView = progressViewRef.get(); if (getLoaderTask(imageView) == this) { // Set the bitmap if (bitmap != null) { imageView.setImageBitmap(bitmap); } + // Hide the progress bar + if (prgView != null) { + prgView.setVisibility(View.INVISIBLE); + } + // Show the view - imageView.setAlpha(1.0f); + imageView.setVisibility(View.VISIBLE); } } } @@ -280,31 +296,34 @@ public class CachedAppAssetLoader { }); } - public boolean populateImageView(NvApp app, ImageView view) { + public boolean populateImageView(NvApp app, ImageView imgView, ProgressBar prgView) { LoaderTuple tuple = new LoaderTuple(computer, app); // If there's already a task in progress for this view, // cancel it. If the task is already loading the same image, // we return and let that load finish. - if (!cancelPendingLoad(tuple, view)) { + if (!cancelPendingLoad(tuple, imgView)) { return true; } + // Hide the progress bar always on initial load + prgView.setVisibility(View.INVISIBLE); + // First, try the memory cache in the current context Bitmap bmp = memoryLoader.loadBitmapFromCache(tuple); if (bmp != null) { // Show the bitmap immediately - view.setAlpha(1.0f); - view.setImageBitmap(bmp); + imgView.setVisibility(View.VISIBLE); + imgView.setImageBitmap(bmp); return true; } // If it's not in memory, create an async task to load it. This task will be attached // via AsyncDrawable to this view. - final LoaderTask task = new LoaderTask(view, true); - final AsyncDrawable asyncDrawable = new AsyncDrawable(view.getResources(), placeholderBitmap, task); - view.setAlpha(0.0f); - view.setImageDrawable(asyncDrawable); + final LoaderTask task = new LoaderTask(imgView, prgView, true); + final AsyncDrawable asyncDrawable = new AsyncDrawable(imgView.getResources(), placeholderBitmap, task); + imgView.setVisibility(View.INVISIBLE); + imgView.setImageDrawable(asyncDrawable); // Run the task on our foreground executor task.executeOnExecutor(foregroundExecutor, tuple);