send Downloader progress only via its DownloaderProgressListener

Instead of duplicate APIs, standardize on a single API, and use that
everywhere via the Downloader.LOCAL_ACTION_PROGRESS event that is already
wired in.
This commit is contained in:
Hans-Christoph Steiner 2016-03-29 16:27:49 +02:00
parent ae0976d24a
commit 8befba0522
5 changed files with 2 additions and 38 deletions

View File

@ -459,12 +459,7 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
localBroadcastManager.registerReceiver(downloaderProgressReceiver, localBroadcastManager.registerReceiver(downloaderProgressReceiver,
new IntentFilter(Downloader.LOCAL_ACTION_PROGRESS)); new IntentFilter(Downloader.LOCAL_ACTION_PROGRESS));
downloadHandler.setProgressListener(this); downloadHandler.setProgressListener(this);
headerFragment.startProgress();
if (downloadHandler.getTotalBytes() == 0) {
headerFragment.startProgress();
} else {
headerFragment.updateProgress(downloadHandler.getBytesRead(), downloadHandler.getTotalBytes());
}
} }
} }
} }

View File

@ -261,12 +261,4 @@ public class ApkDownloader implements AsyncDownloader.Listener {
public Apk getApk() { public Apk getApk() {
return curApk; return curApk;
} }
public int getBytesRead() {
return dlWrapper != null ? dlWrapper.getBytesRead() : 0;
}
public int getTotalBytes() {
return dlWrapper != null ? dlWrapper.getTotalBytes() : 0;
}
} }

View File

@ -30,14 +30,6 @@ class AsyncDownloadWrapper extends Handler implements AsyncDownloader {
this.listener = listener; this.listener = listener;
} }
public int getBytesRead() {
return downloader.getBytesRead();
}
public int getTotalBytes() {
return downloader.getTotalBytes();
}
public void download() { public void download() {
downloadThread = new DownloadThread(); downloadThread = new DownloadThread();
downloadThread.start(); downloadThread.start();

View File

@ -10,10 +10,6 @@ public interface AsyncDownloader {
void onDownloadComplete(); void onDownloadComplete();
} }
int getBytesRead();
int getTotalBytes();
void download(); void download();
void attemptCancel(); void attemptCancel();

View File

@ -31,8 +31,6 @@ public abstract class Downloader {
protected final URL sourceUrl; protected final URL sourceUrl;
protected String cacheTag; protected String cacheTag;
private int bytesRead;
private int totalBytes;
interface DownloaderProgressListener { interface DownloaderProgressListener {
void sendProgress(URL sourceUrl, int bytesRead, int totalBytes); void sendProgress(URL sourceUrl, int bytesRead, int totalBytes);
@ -147,7 +145,7 @@ public abstract class Downloader {
private void copyInputToOutputStream(InputStream input, int bufferSize) throws IOException, InterruptedException { private void copyInputToOutputStream(InputStream input, int bufferSize) throws IOException, InterruptedException {
int bytesRead = 0; int bytesRead = 0;
this.totalBytes = totalDownloadSize(); int totalBytes = totalDownloadSize();
byte[] buffer = new byte[bufferSize]; byte[] buffer = new byte[bufferSize];
// Getting the total download size could potentially take time, depending on how // Getting the total download size could potentially take time, depending on how
@ -182,20 +180,11 @@ public abstract class Downloader {
} }
private void sendProgress(int bytesRead, int totalBytes) { private void sendProgress(int bytesRead, int totalBytes) {
this.bytesRead = bytesRead;
if (downloaderProgressListener != null) { if (downloaderProgressListener != null) {
downloaderProgressListener.sendProgress(sourceUrl, bytesRead, totalBytes); downloaderProgressListener.sendProgress(sourceUrl, bytesRead, totalBytes);
} }
} }
public int getBytesRead() {
return bytesRead;
}
public int getTotalBytes() {
return totalBytes;
}
/** /**
* Overrides every method in {@link InputStream} and delegates to the wrapped stream. * Overrides every method in {@link InputStream} and delegates to the wrapped stream.
* The only difference is that when we call the {@link WrappedInputStream#close()} method, * The only difference is that when we call the {@link WrappedInputStream#close()} method,