Fixed problem with DownloadlistAdapter

This commit is contained in:
daniel oeh 2012-08-17 14:37:21 +02:00
parent 5fe7287ecd
commit 1057682860
3 changed files with 75 additions and 46 deletions

View File

@ -74,6 +74,9 @@ public class DownloadActivity extends SherlockListActivity implements
DownloadService.ACTION_DOWNLOADS_CONTENT_CHANGED)); DownloadService.ACTION_DOWNLOADS_CONTENT_CHANGED));
bindService(new Intent(this, DownloadService.class), mConnection, 0); bindService(new Intent(this, DownloadService.class), mConnection, 0);
startContentRefresher(); startContentRefresher();
if (dla != null) {
dla.notifyDataSetChanged();
}
} }
@Override @Override
@ -100,6 +103,7 @@ public class DownloadActivity extends SherlockListActivity implements
dla = new DownloadlistAdapter(DownloadActivity.this, 0, dla = new DownloadlistAdapter(DownloadActivity.this, 0,
downloadService.getDownloads()); downloadService.getDownloads());
setListAdapter(dla); setListAdapter(dla);
dla.notifyDataSetChanged();
} }
}; };

View File

@ -33,6 +33,7 @@ import android.graphics.BitmapFactory;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Binder; import android.os.Binder;
import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
@ -109,6 +110,8 @@ public class DownloadService extends Service {
/** True if service is running. */ /** True if service is running. */
public static boolean isRunning = false; public static boolean isRunning = false;
private Handler handler;
private final IBinder mBinder = new LocalBinder(); private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder { public class LocalBinder extends Binder {
@ -131,6 +134,7 @@ public class DownloadService extends Service {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Service started"); Log.d(TAG, "Service started");
isRunning = true; isRunning = true;
handler = new Handler();
completedDownloads = new ArrayList<DownloadStatus>(); completedDownloads = new ArrayList<DownloadStatus>();
downloads = new ArrayList<Downloader>(); downloads = new ArrayList<Downloader>();
registerReceiver(downloadQueued, new IntentFilter( registerReceiver(downloadQueued, new IntentFilter(
@ -240,6 +244,10 @@ public class DownloadService extends Service {
} }
} else if (intent.getAction().equals(ACTION_CANCEL_ALL_DOWNLOADS)) { } else if (intent.getAction().equals(ACTION_CANCEL_ALL_DOWNLOADS)) {
handler.post(new Runnable() {
@Override
public void run() {
for (Downloader d : downloads) { for (Downloader d : downloads) {
d.interrupt(); d.interrupt();
DownloadRequester.getInstance().removeDownload( DownloadRequester.getInstance().removeDownload(
@ -249,7 +257,11 @@ public class DownloadService extends Service {
Log.d(TAG, "Cancelled all downloads"); Log.d(TAG, "Cancelled all downloads");
} }
downloads.clear(); downloads.clear();
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); sendBroadcast(new Intent(
ACTION_DOWNLOADS_CONTENT_CHANGED));
}
});
} }
queryDownloads(); queryDownloads();
} }
@ -317,6 +329,7 @@ public class DownloadService extends Service {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Received 'Download Complete' - message."); Log.d(TAG, "Received 'Download Complete' - message.");
DownloadStatus status = downloader.getStatus(); DownloadStatus status = downloader.getStatus();
status.setCompletionDate(new Date());
boolean successful = status.isSuccessful(); boolean successful = status.isSuccessful();
int reason = status.getReason(); int reason = status.getReason();
@ -355,12 +368,19 @@ public class DownloadService extends Service {
* Remove download from the DownloadRequester list and from the * Remove download from the DownloadRequester list and from the
* DownloadService list. * DownloadService list.
*/ */
private void removeDownload(Downloader d) { private void removeDownload(final Downloader d) {
handler.post(new Runnable() {
@Override
public void run() {
downloads.remove(d); downloads.remove(d);
DownloadRequester.getInstance().removeDownload( DownloadRequester.getInstance().removeDownload(
d.getStatus().getFeedFile()); d.getStatus().getFeedFile());
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
} }
});
}
/** /**
* Adds a new DownloadStatus object to the list of completed downloads and * Adds a new DownloadStatus object to the list of completed downloads and
@ -617,7 +637,6 @@ public class DownloadService extends Service {
public void run() { public void run() {
image.setDownloaded(true); image.setDownloaded(true);
status.setCompletionDate(new Date());
saveDownloadStatus(status); saveDownloadStatus(status);
sendDownloadHandledIntent(DOWNLOAD_TYPE_IMAGE); sendDownloadHandledIntent(DOWNLOAD_TYPE_IMAGE);
manager.setFeedImage(DownloadService.this, image); manager.setFeedImage(DownloadService.this, image);
@ -658,7 +677,6 @@ public class DownloadService extends Service {
Log.d(TAG, "Duration of file is " + media.getDuration()); Log.d(TAG, "Duration of file is " + media.getDuration());
mediaplayer.reset(); mediaplayer.reset();
status.setCompletionDate(new Date());
saveDownloadStatus(status); saveDownloadStatus(status);
sendDownloadHandledIntent(DOWNLOAD_TYPE_MEDIA); sendDownloadHandledIntent(DOWNLOAD_TYPE_MEDIA);
manager.setFeedMedia(service, media); manager.setFeedMedia(service, media);

View File

@ -10,9 +10,12 @@ import android.util.Log;
/** Parses several date formats. */ /** Parses several date formats. */
public class SyndDateUtils { public class SyndDateUtils {
private static final String TAG = "DateUtils"; private static final String TAG = "DateUtils";
public static final String RFC822 = "dd MMM yyyy HH:mm:ss Z";
/** RFC 822 date format with day of the week. */ public static final String[] RFC822DATES = { "EEE, dd MMM yyyy HH:mm:ss Z",
public static final String RFC822DAY = "EEE, " + RFC822; "dd MMM yyyy HH:mm:ss Z", "EEE, dd MMM yy HH:mm:ss Z",
"dd MMM yy HH:mm:ss Z", "EEE, dd MMM yyyy HH:mm:ss z",
"dd MMM yyyy HH:mm:ss z", "EEE, dd MMM yy HH:mm:ss z",
"dd MMM yy HH:mm:ss z" };
/** RFC 3339 date format for UTC dates. */ /** RFC 3339 date format for UTC dates. */
public static final String RFC3339UTC = "yyyy-MM-dd'T'HH:mm:ss'Z'"; public static final String RFC3339UTC = "yyyy-MM-dd'T'HH:mm:ss'Z'";
@ -23,7 +26,7 @@ public class SyndDateUtils {
private static ThreadLocal<SimpleDateFormat> RFC822Formatter = new ThreadLocal<SimpleDateFormat>() { private static ThreadLocal<SimpleDateFormat> RFC822Formatter = new ThreadLocal<SimpleDateFormat>() {
@Override @Override
protected SimpleDateFormat initialValue() { protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(RFC822DAY, Locale.US); return new SimpleDateFormat(RFC822DATES[0], Locale.US);
} }
}; };
@ -36,23 +39,23 @@ public class SyndDateUtils {
}; };
public static Date parseRFC822Date(final String date) { public static Date parseRFC822Date(String date) {
Date result = null; Date result = null;
if (date.contains("PDT")) {
date = date.replace("PDT", "PST8PDT");
}
SimpleDateFormat format = RFC822Formatter.get(); SimpleDateFormat format = RFC822Formatter.get();
for (int i = 0; i < RFC822DATES.length; i++) {
try { try {
result = format.parse(date); result = format.parse(date);
break;
} catch (ParseException e) { } catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
format.applyPattern(RFC822);
try {
result = format.parse(date);
} catch (ParseException e1) {
e1.printStackTrace();
Log.e(TAG, "Unable to parse feed date correctly");
} finally {
format.applyPattern(RFC822DAY); // apply old pattern again
} }
} }
if (result == null) {
Log.e(TAG, "Unable to parse feed date correctly");
}
return result; return result;
} }
@ -90,7 +93,11 @@ public class SyndDateUtils {
return result; return result;
} }
/** Takes a string of the form [HH:]MM:SS[.mmm] and converts it to milliseconds. */
/**
* Takes a string of the form [HH:]MM:SS[.mmm] and converts it to
* milliseconds.
*/
public static long parseTimeString(final String time) { public static long parseTimeString(final String time) {
String[] parts = time.split(":"); String[] parts = time.split(":");
long result = 0; long result = 0;