Skip to content

Commit

Permalink
Fix #272 - use threads instead of asynctasks
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Development committed Nov 8, 2014
1 parent ff3b4f2 commit d1e38dc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 112 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
import de.luhmer.owncloudnewsreader.helper.ImageDownloadFinished;
import de.luhmer.owncloudnewsreader.helper.ImageHandler;

public class GetImageAsyncTask extends AsyncTask<Void, Void, Bitmap>
public class GetImageThreaded extends Thread
{
private static final String TAG = "GetImageAsyncTask";

//private static int count = 0;

private URL WEB_URL_TO_FILE;
private ImageDownloadFinished imageDownloadFinished;
private long AsyncTaskId;
private long ThreadId;
private String rootPath;
private Context cont;

Expand All @@ -59,10 +59,8 @@ public class GetImageAsyncTask extends AsyncTask<Void, Void, Bitmap>
public int dstWidth; // width in pixels

Bitmap bmp;
//private ImageView imgView;
//private WeakReference<ImageView> imageViewReference;

public GetImageAsyncTask(String WEB_URL_TO_FILE, ImageDownloadFinished imgDownloadFinished, long AsynkTaskId, String rootPath, Context cont) {
public GetImageThreaded(String WEB_URL_TO_FILE, ImageDownloadFinished imgDownloadFinished, long ThreadId, String rootPath, Context cont) {
try
{
this.WEB_URL_TO_FILE = new URL(WEB_URL_TO_FILE);
Expand All @@ -75,32 +73,23 @@ public GetImageAsyncTask(String WEB_URL_TO_FILE, ImageDownloadFinished imgDownlo

this.cont = cont;
imageDownloadFinished = imgDownloadFinished;
this.AsyncTaskId = AsynkTaskId;
this.ThreadId = ThreadId;
this.rootPath = rootPath;
//this.imageViewReference = new WeakReference<ImageView>(imageView);
}

@Override
protected void onPostExecute(Bitmap result) {
if(imageDownloadFinished != null)
imageDownloadFinished.DownloadFinished(AsyncTaskId, result);
//imgView.setImageDrawable(GetFavIconFromCache(WEB_URL_TO_FILE.toString(), context));
super.onPostExecute(result);
}

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
@Override
protected Bitmap doInBackground(Void... params) {
try
{
File cacheFile = ImageHandler.getFullPathOfCacheFile(WEB_URL_TO_FILE.toString(), rootPath);
if(!cacheFile.isFile())
{
File dir = new File(rootPath);
dir.mkdirs();
cacheFile = ImageHandler.getFullPathOfCacheFile(WEB_URL_TO_FILE.toString(), rootPath);
//cacheFile.createNewFile();
@Override
public void run() {
try
{
File cacheFile = ImageHandler.getFullPathOfCacheFile(WEB_URL_TO_FILE.toString(), rootPath);
if(!cacheFile.isFile())
{
File dir = new File(rootPath);
dir.mkdirs();
cacheFile = ImageHandler.getFullPathOfCacheFile(WEB_URL_TO_FILE.toString(), rootPath);
//cacheFile.createNewFile();


/* Open a connection to that URL. */
Expand Down Expand Up @@ -132,16 +121,20 @@ protected Bitmap doInBackground(Void... params) {
fos.write(baf.toByteArray());
fos.close();
}
}
//return cacheFile.getPath();
}
catch(Exception ex)
{
//ex.printStackTrace();
}
return bmp;
}

}
//return cacheFile.getPath();
}
catch(Exception ex)
{
ex.printStackTrace();
}
//return bmp;

if(imageDownloadFinished != null)
imageDownloadFinished.DownloadFinished(ThreadId, bmp);

super.run();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.lang.ref.WeakReference;

import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.async_tasks.GetImageAsyncTask;
import de.luhmer.owncloudnewsreader.async_tasks.GetImageThreaded;
import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm;
import de.luhmer.owncloudnewsreader.database.model.Feed;

Expand Down Expand Up @@ -88,13 +88,12 @@ public static int getResourceIdForRightDefaultFeedIcon(Context context)
}

public void PreCacheFavIcon(Feed feed) {
GetImageAsyncTask giAsync = new GetImageAsyncTask(feed.getFaviconUrl(), favIconDownloadFinished, feed.getId(), FileUtils.getPathFavIcons(context), context);
GetImageThreaded giAsync = new GetImageThreaded(feed.getFaviconUrl(), favIconDownloadFinished, feed.getId(), FileUtils.getPathFavIcons(context), context);
giAsync.scaleImage = true;
giAsync.dstHeight = 2*32;
giAsync.dstWidth = 2*32;


AsyncTaskHelper.StartAsyncTask(giAsync, ((Void)null));
giAsync.start();
}

ImageDownloadFinished favIconDownloadFinished = new ImageDownloadFinished() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@


import android.graphics.Bitmap;
import android.support.annotation.NonNull;

public interface ImageDownloadFinished {
void DownloadFinished(long AsynkTaskId, Bitmap bitmap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import de.luhmer.owncloudnewsreader.NewsReaderListActivity;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.async_tasks.GetImageAsyncTask;
import de.luhmer.owncloudnewsreader.async_tasks.GetImageThreaded;
import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm;
import de.luhmer.owncloudnewsreader.database.model.Feed;
import de.luhmer.owncloudnewsreader.database.model.RssItem;
Expand All @@ -65,10 +66,13 @@ public class DownloadImagesService extends IntentService {
private int NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private NotificationCompat.Builder NotificationDownloadImages;
private int count;

private int maxCount;
//private int total_size = 0;

private String pathToImageCache;
List<String> linksToImages = new LinkedList<String>();

public DownloadImagesService() {
super(null);
initService();
Expand All @@ -81,7 +85,8 @@ public DownloadImagesService(String name) {

private void initService()
{
count = 0;
pathToImageCache = FileUtils.getPathImageCache(this);

maxCount = 0;
if(random == null)
random = new Random();
Expand Down Expand Up @@ -129,16 +134,23 @@ protected void onHandleIntent(Intent intent) {
if (maxCount > 0)
notificationManager.notify(NOTIFICATION_ID, notify);

try {
for (String link : links)
new GetImageAsyncTask(link, imgDownloadFinished, 999, FileUtils.getPathImageCache(this), this).execute();
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(this, "Error while downloading images.", Toast.LENGTH_LONG).show();
}
linksToImages.addAll(links);

StartNextDownloadInQueue();
}
}

private synchronized void StartNextDownloadInQueue() {
try {
if(linksToImages.size() > 0)
new GetImageThreaded(linksToImages.get(0), imgDownloadFinished, 999, pathToImageCache, this).start();
linksToImages.remove(0);
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(this, "Error while downloading images.", Toast.LENGTH_LONG).show();
}
}

private Notification BuildNotification() {
Intent intentNewsReader = new Intent(this, NewsReaderListActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intentNewsReader, 0);
Expand Down Expand Up @@ -217,21 +229,21 @@ public static LinkedHashMap sortHashMapByValuesD(HashMap passedMap) {
ImageDownloadFinished imgDownloadFinished = new ImageDownloadFinished() {

@Override
public void DownloadFinished(long AsynkTaskId, Bitmap bitmap) {
public void DownloadFinished(long ThreadId, Bitmap bitmap) {

int count = maxCount - linksToImages.size();

count++;
// Sets the progress indicator to a max value, the
// current completion percentage, and "determinate"
// state
NotificationDownloadImages.setProgress(maxCount, count, false);
NotificationDownloadImages.setContentText("Downloading Images for offline usage - " + count + "/" + maxCount);
// Displays the progress bar for the first time.
notificationManager.notify(NOTIFICATION_ID, NotificationDownloadImages.build());

if(maxCount == count) {
notificationManager.cancel(NOTIFICATION_ID);
if(DownloadImagesService.this != null)
RemoveOldImages(DownloadImagesService.this);
} else {
NotificationDownloadImages.setProgress(maxCount, count+1, false);
NotificationDownloadImages.setContentText("Downloading Images for offline usage - " + (count+1) + "/" + maxCount);
notificationManager.notify(NOTIFICATION_ID, NotificationDownloadImages.build());

StartNextDownloadInQueue();
}
}
};
Expand Down

0 comments on commit d1e38dc

Please sign in to comment.