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

View File

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

View File

@ -10,9 +10,12 @@ import android.util.Log;
/** Parses several date formats. */
public class SyndDateUtils {
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 RFC822DAY = "EEE, " + RFC822;
public static final String[] RFC822DATES = { "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", "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. */
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>() {
@Override
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;
if (date.contains("PDT")) {
date = date.replace("PDT", "PST8PDT");
}
SimpleDateFormat format = RFC822Formatter.get();
for (int i = 0; i < RFC822DATES.length; i++) {
try {
result = format.parse(date);
break;
} catch (ParseException e) {
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;
}
@ -90,7 +93,11 @@ public class SyndDateUtils {
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) {
String[] parts = time.split(":");
long result = 0;
@ -102,7 +109,7 @@ public class SyndDateUtils {
}
result += Integer.valueOf(parts[idx]) * 60000;
idx++;
result += ( Float.valueOf(parts[idx])) * 1000;
result += (Float.valueOf(parts[idx])) * 1000;
return result;
}
}