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,16 +244,24 @@ public class DownloadService extends Service {
} }
} else if (intent.getAction().equals(ACTION_CANCEL_ALL_DOWNLOADS)) { } else if (intent.getAction().equals(ACTION_CANCEL_ALL_DOWNLOADS)) {
for (Downloader d : downloads) { handler.post(new Runnable() {
d.interrupt();
DownloadRequester.getInstance().removeDownload( @Override
d.getStatus().getFeedFile()); public void run() {
d.getStatus().getFeedFile().setFile_url(null); for (Downloader d : downloads) {
if (AppConfig.DEBUG) d.interrupt();
Log.d(TAG, "Cancelled all downloads"); DownloadRequester.getInstance().removeDownload(
} d.getStatus().getFeedFile());
downloads.clear(); d.getStatus().getFeedFile().setFile_url(null);
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); if (AppConfig.DEBUG)
Log.d(TAG, "Cancelled all downloads");
}
downloads.clear();
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,11 +368,18 @@ 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) {
downloads.remove(d); handler.post(new Runnable() {
DownloadRequester.getInstance().removeDownload(
d.getStatus().getFeedFile()); @Override
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); public void run() {
downloads.remove(d);
DownloadRequester.getInstance().removeDownload(
d.getStatus().getFeedFile());
sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED));
}
});
} }
/** /**
@ -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,49 +10,52 @@ 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'";
/** RFC 3339 date format for localtime dates with offset. */ /** RFC 3339 date format for localtime dates with offset. */
public static final String RFC3339LOCAL = "yyyy-MM-dd'T'HH:mm:ssZ"; public static final String RFC3339LOCAL = "yyyy-MM-dd'T'HH:mm:ssZ";
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);
} }
};
private static ThreadLocal<SimpleDateFormat> RFC3339Formatter = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(RFC3339UTC, Locale.US);
}
}; };
public static Date parseRFC822Date(final String date) { private static ThreadLocal<SimpleDateFormat> RFC3339Formatter = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(RFC3339UTC, Locale.US);
}
};
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();
try { for (int i = 0; i < RFC822DATES.length; i++) {
result = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
format.applyPattern(RFC822);
try { try {
result = format.parse(date); result = format.parse(date);
} catch (ParseException e1) { break;
e1.printStackTrace(); } catch (ParseException e) {
Log.e(TAG, "Unable to parse feed date correctly"); e.printStackTrace();
} 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;
@ -102,7 +109,7 @@ public class SyndDateUtils {
} }
result += Integer.valueOf(parts[idx]) * 60000; result += Integer.valueOf(parts[idx]) * 60000;
idx++; idx++;
result += ( Float.valueOf(parts[idx])) * 1000; result += (Float.valueOf(parts[idx])) * 1000;
return result; return result;
} }
} }