Implemented async FeedImage loader
This commit is contained in:
parent
3768761e90
commit
84eff944ab
BIN
res/drawable-hdpi/default_cover.png
Normal file
BIN
res/drawable-hdpi/default_cover.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
@ -10,6 +10,7 @@ import com.actionbarsherlock.view.Menu;
|
|||||||
import com.actionbarsherlock.view.MenuItem;
|
import com.actionbarsherlock.view.MenuItem;
|
||||||
|
|
||||||
import de.podfetcher.R;
|
import de.podfetcher.R;
|
||||||
|
import de.podfetcher.asynctask.FeedImageLoader;
|
||||||
import de.podfetcher.feed.Feed;
|
import de.podfetcher.feed.Feed;
|
||||||
import de.podfetcher.feed.FeedManager;
|
import de.podfetcher.feed.FeedManager;
|
||||||
|
|
||||||
@ -41,8 +42,8 @@ public class FeedInfoActivity extends SherlockActivity {
|
|||||||
txtvDescription = (TextView) findViewById(R.id.txtvDescription);
|
txtvDescription = (TextView) findViewById(R.id.txtvDescription);
|
||||||
txtvLanguage = (TextView) findViewById(R.id.txtvLanguage);
|
txtvLanguage = (TextView) findViewById(R.id.txtvLanguage);
|
||||||
txtvAuthor = (TextView) findViewById(R.id.txtvAuthor);
|
txtvAuthor = (TextView) findViewById(R.id.txtvAuthor);
|
||||||
|
FeedImageLoader.getInstance().loadBitmap(feed.getImage(), imgvCover);
|
||||||
|
|
||||||
imgvCover.setImageBitmap(feed.getImage().getImageBitmap());
|
|
||||||
txtvTitle.setText(feed.getTitle());
|
txtvTitle.setText(feed.getTitle());
|
||||||
txtvDescription.setText(feed.getDescription());
|
txtvDescription.setText(feed.getDescription());
|
||||||
if (feed.getAuthor() != null) {
|
if (feed.getAuthor() != null) {
|
||||||
|
@ -42,7 +42,6 @@ public class FeedItemlistActivity extends SherlockFragmentActivity {
|
|||||||
if(feedId == -1) Log.e(TAG, "Received invalid feed selection.");
|
if(feedId == -1) Log.e(TAG, "Received invalid feed selection.");
|
||||||
|
|
||||||
feed = manager.getFeed(feedId);
|
feed = manager.getFeed(feedId);
|
||||||
getSupportActionBar().setLogo(new BitmapDrawable(feed.getImage().getImageBitmap()));
|
|
||||||
setTitle(feed.getTitle());
|
setTitle(feed.getTitle());
|
||||||
|
|
||||||
FragmentManager fragmentManager = getSupportFragmentManager();
|
FragmentManager fragmentManager = getSupportFragmentManager();
|
||||||
|
@ -69,8 +69,6 @@ public class ItemviewActivity extends SherlockFragmentActivity {
|
|||||||
private void populateUI() {
|
private void populateUI() {
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
setContentView(R.layout.feeditemview);
|
setContentView(R.layout.feeditemview);
|
||||||
getSupportActionBar().setLogo(
|
|
||||||
new BitmapDrawable(item.getFeed().getImage().getImageBitmap()));
|
|
||||||
txtvTitle = (TextView) findViewById(R.id.txtvItemname);
|
txtvTitle = (TextView) findViewById(R.id.txtvItemname);
|
||||||
txtvPublished = (TextView) findViewById(R.id.txtvPublished);
|
txtvPublished = (TextView) findViewById(R.id.txtvPublished);
|
||||||
setTitle(item.getFeed().getTitle());
|
setTitle(item.getFeed().getTitle());
|
||||||
|
@ -5,6 +5,7 @@ import java.text.DateFormat;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import de.podfetcher.R;
|
import de.podfetcher.R;
|
||||||
|
import de.podfetcher.asynctask.FeedImageLoader;
|
||||||
import de.podfetcher.feed.Feed;
|
import de.podfetcher.feed.Feed;
|
||||||
import de.podfetcher.storage.DownloadRequester;
|
import de.podfetcher.storage.DownloadRequester;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@ -25,12 +26,14 @@ public class FeedlistAdapter extends ArrayAdapter<Feed> {
|
|||||||
private static final String TAG = "FeedlistAdapter";
|
private static final String TAG = "FeedlistAdapter";
|
||||||
|
|
||||||
private int selectedItemIndex;
|
private int selectedItemIndex;
|
||||||
|
private FeedImageLoader imageLoader;
|
||||||
public static final int SELECTION_NONE = -1;
|
public static final int SELECTION_NONE = -1;
|
||||||
|
|
||||||
public FeedlistAdapter(Context context, int textViewResourceId,
|
public FeedlistAdapter(Context context, int textViewResourceId,
|
||||||
List<Feed> objects) {
|
List<Feed> objects) {
|
||||||
super(context, textViewResourceId, objects);
|
super(context, textViewResourceId, objects);
|
||||||
selectedItemIndex = SELECTION_NONE;
|
selectedItemIndex = SELECTION_NONE;
|
||||||
|
imageLoader = FeedImageLoader.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -82,16 +85,8 @@ public class FeedlistAdapter extends ArrayAdapter<Feed> {
|
|||||||
holder.newEpisodes.setVisibility(View.INVISIBLE);
|
holder.newEpisodes.setVisibility(View.INVISIBLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (feed.getImage() != null) {
|
imageLoader.loadBitmap(feed.getImage(), holder.image);
|
||||||
holder.image.setImageBitmap(feed.getImage().getImageBitmap()); // TODO
|
|
||||||
// select
|
|
||||||
// default
|
|
||||||
// picture
|
|
||||||
// when
|
|
||||||
// no
|
|
||||||
// image
|
|
||||||
// downloaded
|
|
||||||
}
|
|
||||||
// TODO find new Episodes txtvNewEpisodes.setText(feed)
|
// TODO find new Episodes txtvNewEpisodes.setText(feed)
|
||||||
return convertView;
|
return convertView;
|
||||||
}
|
}
|
||||||
|
101
src/de/podfetcher/asynctask/FeedImageLoader.java
Normal file
101
src/de/podfetcher/asynctask/FeedImageLoader.java
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
package de.podfetcher.asynctask;
|
||||||
|
|
||||||
|
import de.podfetcher.R;
|
||||||
|
import de.podfetcher.feed.FeedImage;
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.support.v4.util.LruCache;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
|
||||||
|
/** Caches and loads FeedImage bitmaps in the background */
|
||||||
|
public class FeedImageLoader {
|
||||||
|
private static FeedImageLoader singleton;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores references to loaded bitmaps. Bitmaps can be accessed by the id of
|
||||||
|
* the FeedImage the bitmap belongs to.
|
||||||
|
*/
|
||||||
|
private LruCache<Long, Bitmap> imageCache;
|
||||||
|
private static final int CACHE_SIZE = 4 * 1024 * 1024;
|
||||||
|
|
||||||
|
private FeedImageLoader() {
|
||||||
|
imageCache = new LruCache<Long, Bitmap>(CACHE_SIZE) {
|
||||||
|
|
||||||
|
@SuppressLint("NewApi")
|
||||||
|
@Override
|
||||||
|
protected int sizeOf(Long key, Bitmap value) {
|
||||||
|
if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= 12)
|
||||||
|
return value.getByteCount();
|
||||||
|
else
|
||||||
|
return (value.getRowBytes() * value.getHeight());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FeedImageLoader getInstance() {
|
||||||
|
if (singleton == null) {
|
||||||
|
singleton = new FeedImageLoader();
|
||||||
|
}
|
||||||
|
return singleton;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadBitmap(FeedImage image, ImageView target) {
|
||||||
|
if (image != null) {
|
||||||
|
Bitmap bitmap = getBitmapFromCache(image.getId());
|
||||||
|
if (bitmap != null) {
|
||||||
|
target.setImageBitmap(bitmap);
|
||||||
|
} else {
|
||||||
|
target.setImageResource(R.drawable.default_cover);
|
||||||
|
BitmapWorkerTask worker = new BitmapWorkerTask(target);
|
||||||
|
worker.execute(image);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
target.setImageResource(R.drawable.default_cover);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addBitmapToCache(long id, Bitmap bitmap) {
|
||||||
|
imageCache.put(id, bitmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Bitmap getBitmapFromCache(long id) {
|
||||||
|
return imageCache.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
class BitmapWorkerTask extends AsyncTask<FeedImage, Void, Void> {
|
||||||
|
private static final String TAG = "BitmapWorkerTask";
|
||||||
|
private ImageView target;
|
||||||
|
private Bitmap bitmap;
|
||||||
|
|
||||||
|
public BitmapWorkerTask(ImageView target) {
|
||||||
|
super();
|
||||||
|
this.target = target;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Void result) {
|
||||||
|
super.onPostExecute(result);
|
||||||
|
target.setImageBitmap(bitmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(FeedImage... params) {
|
||||||
|
if (params[0].getFile_url() != null) {
|
||||||
|
bitmap = BitmapFactory.decodeFile(params[0].getFile_url());
|
||||||
|
addBitmapToCache(params[0].getId(), bitmap);
|
||||||
|
Log.d(TAG, "Finished loading bitmaps");
|
||||||
|
} else {
|
||||||
|
Log.e(TAG, "FeedImage has no file url. Using default image");
|
||||||
|
bitmap = BitmapFactory.decodeResource(target.getResources(),
|
||||||
|
R.drawable.default_cover);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,19 +1,17 @@
|
|||||||
package de.podfetcher.feed;
|
package de.podfetcher.feed;
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.graphics.BitmapFactory;
|
|
||||||
|
|
||||||
public class FeedImage extends FeedFile {
|
public class FeedImage extends FeedFile {
|
||||||
protected String title;
|
protected String title;
|
||||||
protected Bitmap image_bitmap;
|
|
||||||
|
|
||||||
public FeedImage(String download_url, String title) {
|
public FeedImage(String download_url, String title) {
|
||||||
super(null, download_url, false);
|
super(null, download_url, false);
|
||||||
this.download_url = download_url;
|
this.download_url = download_url;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FeedImage(long id, String title, String file_url, String download_url, boolean downloaded) {
|
public FeedImage(long id, String title, String file_url,
|
||||||
|
String download_url, boolean downloaded) {
|
||||||
super(file_url, download_url, downloaded);
|
super(file_url, download_url, downloaded);
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
@ -26,22 +24,9 @@ public class FeedImage extends FeedFile {
|
|||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setTitle(String title) {
|
public void setTitle(String title) {
|
||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap getImageBitmap() {
|
|
||||||
if(image_bitmap == null) {
|
|
||||||
image_bitmap = BitmapFactory.decodeFile(getFile_url());
|
|
||||||
}
|
|
||||||
return image_bitmap;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import android.widget.TextView;
|
|||||||
import com.actionbarsherlock.app.SherlockFragment;
|
import com.actionbarsherlock.app.SherlockFragment;
|
||||||
|
|
||||||
import de.podfetcher.R;
|
import de.podfetcher.R;
|
||||||
|
import de.podfetcher.asynctask.FeedImageLoader;
|
||||||
import de.podfetcher.feed.Feed;
|
import de.podfetcher.feed.Feed;
|
||||||
import de.podfetcher.feed.FeedItem;
|
import de.podfetcher.feed.FeedItem;
|
||||||
import de.podfetcher.feed.FeedManager;
|
import de.podfetcher.feed.FeedManager;
|
||||||
@ -42,7 +43,7 @@ public class CoverFragment extends SherlockFragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
FeedManager manager = FeedManager.getInstance();
|
FeedManager manager = FeedManager.getInstance();
|
||||||
FeedItem item = null;
|
FeedItem item = null;
|
||||||
Bundle args = getArguments();
|
Bundle args = getArguments();
|
||||||
@ -68,7 +69,7 @@ public class CoverFragment extends SherlockFragment {
|
|||||||
imgvCover = (ImageView) root.findViewById(R.id.imgvCover);
|
imgvCover = (ImageView) root.findViewById(R.id.imgvCover);
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
@ -80,8 +81,8 @@ public class CoverFragment extends SherlockFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadMediaInfo() {
|
private void loadMediaInfo() {
|
||||||
imgvCover.setImageBitmap(media.getItem().getFeed().getImage()
|
FeedImageLoader.getInstance().loadBitmap(
|
||||||
.getImageBitmap());
|
media.getItem().getFeed().getImage(), imgvCover);
|
||||||
txtvTitle.setText(media.getItem().getTitle());
|
txtvTitle.setText(media.getItem().getTitle());
|
||||||
txtvFeed.setText(media.getItem().getFeed().getTitle());
|
txtvFeed.setText(media.getItem().getFeed().getTitle());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user