Reinstate code to allow resumed activity to query download status (rather than wait for broadcasts).

This commit is contained in:
Peter Serwylo 2015-08-03 22:37:00 +10:00
parent b2bde89834
commit a0bf49d1ea
4 changed files with 23 additions and 9 deletions

View File

@ -464,11 +464,10 @@ public class AppDetails extends ActionBarActivity implements ProgressListener, A
new IntentFilter(Downloader.LOCAL_ACTION_PROGRESS));
downloadHandler.setProgressListener(this);
if (downloadHandler.getTotalSize() == 0)
if (downloadHandler.getTotalBytes() == 0)
mHeaderFragment.startProgress();
else
mHeaderFragment.updateProgress(downloadHandler.getProgress(),
downloadHandler.getTotalSize());
mHeaderFragment.updateProgress(downloadHandler.getBytesRead(), downloadHandler.getTotalBytes());
}
}
}

View File

@ -52,7 +52,6 @@ public class ApkDownloader implements AsyncDownloadWrapper.Listener {
public static final int ERROR_HASH_MISMATCH = 101;
public static final int ERROR_DOWNLOAD_FAILED = 102;
public static final int ERROR_UNKNOWN = 103;
private static final String EVENT_SOURCE_ID = "sourceId";
private static long downloadIdCounter = 0;
@ -70,8 +69,6 @@ public class ApkDownloader implements AsyncDownloadWrapper.Listener {
private ProgressListener listener;
private AsyncDownloadWrapper dlWrapper = null;
private int progress = 0;
private int totalSize = 0;
private boolean isComplete = false;
private final long id = ++downloadIdCounter;
@ -274,7 +271,7 @@ public class ApkDownloader implements AsyncDownloadWrapper.Listener {
public Apk getApk() { return curApk; }
public int getProgress() { return progress; }
public int getBytesRead() { return dlWrapper != null ? dlWrapper.getBytesRead() : 0; }
public int getTotalSize() { return totalSize; }
public int getTotalBytes() { return dlWrapper != null ? dlWrapper.getTotalBytes() : 0; }
}

View File

@ -72,6 +72,14 @@ public class AsyncDownloadWrapper extends Handler {
}
}
public int getBytesRead() {
return downloader.getBytesRead();
}
public int getTotalBytes() {
return downloader.getTotalBytes();
}
public interface Listener extends ProgressListener {
void onErrorDownloading(String localisedExceptionDetails);
void onDownloadComplete();

View File

@ -33,6 +33,8 @@ public abstract class Downloader {
protected final URL sourceUrl;
protected String cacheTag = null;
protected int bytesRead = 0;
protected int totalBytes = 0;
public abstract InputStream getInputStream() throws IOException;
@ -145,7 +147,7 @@ public abstract class Downloader {
byte[] buffer = new byte[Utils.BUFFER_SIZE];
int bytesRead = 0;
int totalBytes = totalDownloadSize();
this.totalBytes = totalDownloadSize();
// Getting the total download size could potentially take time, depending on how
// it is implemented, so we may as well check this before we proceed.
@ -169,6 +171,7 @@ public abstract class Downloader {
}
protected void sendProgress(int bytesRead, int totalBytes) {
this.bytesRead = bytesRead;
Intent intent = new Intent(LOCAL_ACTION_PROGRESS);
intent.putExtra(EXTRA_ADDRESS, sourceUrl.toString());
intent.putExtra(EXTRA_BYTES_READ, bytesRead);
@ -176,4 +179,11 @@ public abstract class Downloader {
localBroadcastManager.sendBroadcast(intent);
}
public int getBytesRead() {
return bytesRead;
}
public int getTotalBytes() {
return totalBytes;
}
}