mirror of
https://github.com/AntennaPod/AntennaPod.git
synced 2025-01-03 12:30:33 +01:00
Rewrote DownloadObserver
This commit is contained in:
parent
84eff944ab
commit
61e77e5522
@ -1,18 +1,20 @@
|
||||
package de.podfetcher.asynctask;
|
||||
|
||||
import de.podfetcher.storage.DownloadRequester;
|
||||
import de.podfetcher.feed.*;
|
||||
import de.podfetcher.R;
|
||||
import android.content.Context;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import android.app.DownloadManager;
|
||||
import android.util.Log;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import java.util.concurrent.Callable;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
import de.podfetcher.R;
|
||||
import de.podfetcher.feed.FeedFile;
|
||||
import de.podfetcher.storage.DownloadRequester;
|
||||
|
||||
/** Observes the status of a specific Download */
|
||||
public class DownloadObserver extends
|
||||
AsyncTask<FeedFile, DownloadStatus, Boolean> {
|
||||
public class DownloadObserver extends AsyncTask<Void, Void, Void> {
|
||||
private static final String TAG = "DownloadObserver";
|
||||
|
||||
/** Types of downloads to observe. */
|
||||
@ -28,43 +30,59 @@ public class DownloadObserver extends
|
||||
|
||||
private DownloadRequester requester;
|
||||
private Context context;
|
||||
private DownloadStatus[] statusList;
|
||||
private ArrayList<DownloadStatus> statusList;
|
||||
private List<DownloadObserver.Callback> observer;
|
||||
|
||||
public DownloadObserver(Context context) {
|
||||
super();
|
||||
this.context = context;
|
||||
requester = DownloadRequester.getInstance();
|
||||
statusList = new ArrayList<DownloadStatus>();
|
||||
observer = Collections
|
||||
.synchronizedList(new ArrayList<DownloadObserver.Callback>());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCancelled(Boolean result) {
|
||||
protected void onCancelled() {
|
||||
Log.d(TAG, "Task was cancelled.");
|
||||
}
|
||||
|
||||
protected Boolean doInBackground(FeedFile... files) {
|
||||
protected Void doInBackground(Void... params) {
|
||||
Log.d(TAG, "Background Task started.");
|
||||
statusList = new DownloadStatus[files.length];
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
FeedFile feedfile = files[i];
|
||||
statusList[i] = new DownloadStatus(feedfile);
|
||||
|
||||
if (feedfile.getFile_url() == null) {
|
||||
statusList[i].reason = NO_DOWNLOAD_FOUND;
|
||||
statusList[i].successful = false;
|
||||
statusList[i].done = true;
|
||||
}
|
||||
|
||||
if (feedfile.isDownloaded()) {
|
||||
statusList[i].reason = ALREADY_DOWNLOADED;
|
||||
statusList[i].successful = false;
|
||||
statusList[i].done = true;
|
||||
}
|
||||
}
|
||||
|
||||
while (downloadsLeft() && !isCancelled()) {
|
||||
for (DownloadStatus status : statusList) {
|
||||
if (status.done == false) {
|
||||
Cursor cursor = getDownloadCursor(status.feedfile);
|
||||
refreshStatuslist();
|
||||
publishProgress();
|
||||
try {
|
||||
Thread.sleep(DEFAULT_WAITING_INTERVALL);
|
||||
} catch (InterruptedException e) {
|
||||
Log.w(TAG, "Thread was interrupted while waiting.");
|
||||
}
|
||||
}
|
||||
Log.d(TAG, "Background Task finished.");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onProgressUpdate(Void... values) {
|
||||
for (DownloadObserver.Callback callback : observer) {
|
||||
callback.onProgressUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private void refreshStatuslist() {
|
||||
Cursor cursor = getDownloadCursor();
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
long downloadId = getDownloadStatus(cursor,
|
||||
DownloadManager.COLUMN_ID);
|
||||
FeedFile feedFile = requester.getFeedFile(downloadId);
|
||||
DownloadStatus status = findDownloadStatus(feedFile);
|
||||
if (status == null) {
|
||||
status = new DownloadStatus(feedFile);
|
||||
statusList.add(status);
|
||||
}
|
||||
|
||||
// refresh status
|
||||
int statusId = getDownloadStatus(cursor,
|
||||
DownloadManager.COLUMN_STATUS);
|
||||
getDownloadProgress(cursor, status);
|
||||
@ -91,23 +109,21 @@ public class DownloadObserver extends
|
||||
status.successful = false;
|
||||
status.statusMsg = R.string.download_cancelled_msg;
|
||||
}
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
publishProgress(statusList);
|
||||
|
||||
try {
|
||||
Thread.sleep(DEFAULT_WAITING_INTERVALL);
|
||||
} catch (InterruptedException e) {
|
||||
Log.w(TAG, "Thread was interrupted while waiting.");
|
||||
/** Request a cursor with all running Feedfile downloads */
|
||||
public Cursor getDownloadCursor() {
|
||||
// Collect download ids
|
||||
int numDownloads = requester.getNumberOfDownloads();
|
||||
long ids[] = new long[numDownloads];
|
||||
for (int i = 0; i < numDownloads; i++) {
|
||||
ids[i] = requester.downloads.get(i).getDownloadId();
|
||||
}
|
||||
}
|
||||
Log.d(TAG, "Background Task finished.");
|
||||
return Boolean.valueOf(true);
|
||||
}
|
||||
|
||||
public Cursor getDownloadCursor(FeedFile feedfile) {
|
||||
DownloadManager.Query query = buildQuery(feedfile.getDownloadId());
|
||||
DownloadManager.Query query = new DownloadManager.Query();
|
||||
query.setFilterById(ids);
|
||||
DownloadManager manager = (DownloadManager) context
|
||||
.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
|
||||
@ -116,16 +132,11 @@ public class DownloadObserver extends
|
||||
}
|
||||
|
||||
public int getDownloadStatus(Cursor c, String column) {
|
||||
if (c.moveToFirst()) {
|
||||
int status = c.getInt(c.getColumnIndex(column));
|
||||
return status;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private void getDownloadProgress(Cursor c, DownloadStatus status) {
|
||||
if (c.moveToFirst()) {
|
||||
status.size = c.getLong(c
|
||||
.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
|
||||
status.soFar = c
|
||||
@ -134,30 +145,43 @@ public class DownloadObserver extends
|
||||
status.progressPercent = (int) (((double) status.soFar / (double) status.size) * 100);
|
||||
Log.d(TAG, "Setting progress to " + status.progressPercent);
|
||||
}
|
||||
}
|
||||
|
||||
private DownloadManager.Query buildQuery(long id) {
|
||||
DownloadManager.Query query = new DownloadManager.Query();
|
||||
query.setFilterById(id);
|
||||
return query;
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
public DownloadStatus[] getStatusList() {
|
||||
public DownloadStatus findDownloadStatus(FeedFile f) {
|
||||
for (DownloadStatus status : statusList) {
|
||||
if (status.feedfile == f) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ArrayList<DownloadStatus> getStatusList() {
|
||||
return statusList;
|
||||
}
|
||||
|
||||
private boolean downloadsLeft() {
|
||||
boolean result = false;
|
||||
for (int i = 0; i < statusList.length; i++) {
|
||||
if (statusList[i].done == false) {
|
||||
for (DownloadStatus status : statusList) {
|
||||
if (status.done == false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void registerCallback(DownloadObserver.Callback callback) {
|
||||
observer.add(callback);
|
||||
}
|
||||
|
||||
public void unregisterCallback(DownloadObserver.Callback callback) {
|
||||
observer.remove(callback);
|
||||
}
|
||||
|
||||
public interface Callback {
|
||||
public void onProgressUpdate();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user