simplify ProgressListener.Event creation and use
This aims to simplify the creation and use of Event objects among classes that implement the ProgressListener Interface. This is also needed in order to split out the downloading support from various places and put it all into a centralized refs #2598 https://dev.guardianproject.info/issues/2598 https://gitorious.org/f-droid/fdroidclient/merge_requests/29
This commit is contained in:
parent
a44ce0e4e7
commit
d9f9b682d4
@ -24,9 +24,11 @@ import java.io.*;
|
|||||||
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import org.fdroid.fdroid.data.Apk;
|
import org.fdroid.fdroid.data.Apk;
|
||||||
|
import org.fdroid.fdroid.net.Downloader;
|
||||||
import org.fdroid.fdroid.net.HttpDownloader;
|
import org.fdroid.fdroid.net.HttpDownloader;
|
||||||
|
|
||||||
public class ApkDownloader extends Thread {
|
public class ApkDownloader extends Thread {
|
||||||
|
private static final String TAG = "ApkDownloader";
|
||||||
|
|
||||||
public static final int EVENT_APK_DOWNLOAD_COMPLETE = 100;
|
public static final int EVENT_APK_DOWNLOAD_COMPLETE = 100;
|
||||||
public static final int EVENT_ERROR_HASH_MISMATCH = 101;
|
public static final int EVENT_ERROR_HASH_MISMATCH = 101;
|
||||||
@ -80,11 +82,15 @@ public class ApkDownloader extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If we haven't got the apk locally, we'll have to download it...
|
// If we haven't got the apk locally, we'll have to download it...
|
||||||
|
String remoteAddress = remoteFile();
|
||||||
|
HttpDownloader downloader = new HttpDownloader(remoteAddress, localfile);
|
||||||
|
|
||||||
HttpDownloader downloader = new HttpDownloader(remoteFile(), localfile);
|
if (listener != null) {
|
||||||
downloader.setProgressListener(listener);
|
downloader.setProgressListener(listener,
|
||||||
|
new ProgressListener.Event(Downloader.EVENT_PROGRESS, remoteAddress));
|
||||||
|
}
|
||||||
|
|
||||||
Log.d("FDroid", "Downloading apk from " + remoteFile());
|
Log.d(TAG, "Downloading apk from " + remoteAddress);
|
||||||
int httpStatus = downloader.downloadHttpFile();
|
int httpStatus = downloader.downloadHttpFile();
|
||||||
|
|
||||||
if (httpStatus != 200 || !localfile.exists()) {
|
if (httpStatus != 200 || !localfile.exists()) {
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
|
|
||||||
package org.fdroid.fdroid;
|
package org.fdroid.fdroid;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
public interface ProgressListener {
|
public interface ProgressListener {
|
||||||
|
|
||||||
@ -14,6 +16,7 @@ public interface ProgressListener {
|
|||||||
public static class Event implements Parcelable {
|
public static class Event implements Parcelable {
|
||||||
|
|
||||||
public static final int NO_VALUE = Integer.MIN_VALUE;
|
public static final int NO_VALUE = Integer.MIN_VALUE;
|
||||||
|
public static final String PROGRESS_DATA_REPO = "repo";
|
||||||
|
|
||||||
public final int type;
|
public final int type;
|
||||||
public final Bundle data;
|
public final Bundle data;
|
||||||
@ -29,27 +32,18 @@ public interface ProgressListener {
|
|||||||
this(type, NO_VALUE, NO_VALUE, null);
|
this(type, NO_VALUE, NO_VALUE, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Event(int type, Bundle data) {
|
public Event(int type, String repoAddress) {
|
||||||
this(type, NO_VALUE, NO_VALUE, data);
|
this(type, NO_VALUE, NO_VALUE, repoAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Event(int type, int progress) {
|
public Event(int type, int progress, int total, String repoAddress) {
|
||||||
this(type, progress, NO_VALUE, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Event(int type, int progress, Bundle data) {
|
|
||||||
this(type, NO_VALUE, NO_VALUE, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Event(int type, int progress, int total) {
|
|
||||||
this(type, progress, total, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Event(int type, int progress, int total, Bundle data) {
|
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.progress = progress;
|
this.progress = progress;
|
||||||
this.total = total;
|
this.total = total;
|
||||||
this.data = data == null ? new Bundle() : data;
|
if (TextUtils.isEmpty(repoAddress))
|
||||||
|
this.data = new Bundle();
|
||||||
|
else
|
||||||
|
this.data = createProgressData(repoAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -68,7 +62,8 @@ public interface ProgressListener {
|
|||||||
public static final Parcelable.Creator<Event> CREATOR = new Parcelable.Creator<Event>() {
|
public static final Parcelable.Creator<Event> CREATOR = new Parcelable.Creator<Event>() {
|
||||||
@Override
|
@Override
|
||||||
public Event createFromParcel(Parcel in) {
|
public Event createFromParcel(Parcel in) {
|
||||||
return new Event(in.readInt(), in.readInt(), in.readInt(), in.readBundle());
|
return new Event(in.readInt(), in.readInt(), in.readInt(),
|
||||||
|
in.readBundle().getString(PROGRESS_DATA_REPO));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -77,6 +72,16 @@ public interface ProgressListener {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public String getRepoAddress() {
|
||||||
|
return data.getString(PROGRESS_DATA_REPO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Bundle createProgressData(String repoAddress) {
|
||||||
|
Bundle data = new Bundle();
|
||||||
|
data.putString(PROGRESS_DATA_REPO, repoAddress);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
package org.fdroid.fdroid;
|
package org.fdroid.fdroid;
|
||||||
|
|
||||||
import android.os.Bundle;
|
|
||||||
import org.fdroid.fdroid.data.Apk;
|
import org.fdroid.fdroid.data.Apk;
|
||||||
import org.fdroid.fdroid.data.App;
|
import org.fdroid.fdroid.data.App;
|
||||||
import org.fdroid.fdroid.data.Repo;
|
import org.fdroid.fdroid.data.Repo;
|
||||||
@ -279,12 +278,11 @@ public class RepoXMLHandler extends DefaultHandler {
|
|||||||
} else if (localName.equals("application") && curapp == null) {
|
} else if (localName.equals("application") && curapp == null) {
|
||||||
curapp = new App();
|
curapp = new App();
|
||||||
curapp.id = attributes.getValue("", "id");
|
curapp.id = attributes.getValue("", "id");
|
||||||
Bundle progressData = RepoUpdater.createProgressData(repo.address);
|
|
||||||
progressCounter ++;
|
progressCounter ++;
|
||||||
progressListener.onProgress(
|
progressListener.onProgress(
|
||||||
new ProgressListener.Event(
|
new ProgressListener.Event(
|
||||||
RepoUpdater.PROGRESS_TYPE_PROCESS_XML, progressCounter,
|
RepoUpdater.PROGRESS_TYPE_PROCESS_XML,
|
||||||
totalAppCount, progressData));
|
progressCounter, totalAppCount, repo.address));
|
||||||
|
|
||||||
} else if (localName.equals("package") && curapp != null && curapk == null) {
|
} else if (localName.equals("package") && curapp != null && curapk == null) {
|
||||||
curapk = new Apk();
|
curapk = new Apk();
|
||||||
|
@ -194,7 +194,7 @@ public class UpdateService extends IntentService implements ProgressListener {
|
|||||||
if (message != null && message.length() > 0)
|
if (message != null && message.length() > 0)
|
||||||
resultData.putString(RESULT_MESSAGE, message);
|
resultData.putString(RESULT_MESSAGE, message);
|
||||||
if (event == null)
|
if (event == null)
|
||||||
event = new Event(statusCode);
|
event = new ProgressListener.Event(statusCode);
|
||||||
resultData.putParcelable(RESULT_EVENT, event);
|
resultData.putParcelable(RESULT_EVENT, event);
|
||||||
receiver.send(statusCode, resultData);
|
receiver.send(statusCode, resultData);
|
||||||
}
|
}
|
||||||
@ -675,14 +675,13 @@ public class UpdateService extends IntentService implements ProgressListener {
|
|||||||
@Override
|
@Override
|
||||||
public void onProgress(ProgressListener.Event event) {
|
public void onProgress(ProgressListener.Event event) {
|
||||||
String message = "";
|
String message = "";
|
||||||
|
String repoAddress = event.getRepoAddress();
|
||||||
if (event.type == RepoUpdater.PROGRESS_TYPE_DOWNLOAD) {
|
if (event.type == RepoUpdater.PROGRESS_TYPE_DOWNLOAD) {
|
||||||
String repoAddress = event.data.getString(RepoUpdater.PROGRESS_DATA_REPO);
|
|
||||||
String downloadedSize = Utils.getFriendlySize( event.progress );
|
String downloadedSize = Utils.getFriendlySize( event.progress );
|
||||||
String totalSize = Utils.getFriendlySize( event.total );
|
String totalSize = Utils.getFriendlySize( event.total );
|
||||||
int percent = (int)((double)event.progress/event.total * 100);
|
int percent = (int)((double)event.progress/event.total * 100);
|
||||||
message = getString(R.string.status_download, repoAddress, downloadedSize, totalSize, percent);
|
message = getString(R.string.status_download, repoAddress, downloadedSize, totalSize, percent);
|
||||||
} else if (event.type == RepoUpdater.PROGRESS_TYPE_PROCESS_XML) {
|
} else if (event.type == RepoUpdater.PROGRESS_TYPE_PROCESS_XML) {
|
||||||
String repoAddress = event.data.getString(RepoUpdater.PROGRESS_DATA_REPO);
|
|
||||||
message = getString(R.string.status_processing_xml, repoAddress, event.progress, event.total);
|
message = getString(R.string.status_processing_xml, repoAddress, event.progress, event.total);
|
||||||
}
|
}
|
||||||
sendStatus(STATUS_INFO, message);
|
sendStatus(STATUS_INFO, message);
|
||||||
|
@ -48,11 +48,6 @@ public abstract class Downloader {
|
|||||||
outputFile = null;
|
outputFile = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProgressListener(ProgressListener listener) {
|
|
||||||
this.progressListener = listener;
|
|
||||||
this.progressEvent = new ProgressListener.Event(EVENT_PROGRESS, totalDownloadSize());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setProgressListener(ProgressListener progressListener,
|
public void setProgressListener(ProgressListener progressListener,
|
||||||
ProgressListener.Event progressEvent) {
|
ProgressListener.Event progressEvent) {
|
||||||
this.progressListener = progressListener;
|
this.progressListener = progressListener;
|
||||||
|
@ -2,7 +2,6 @@ package org.fdroid.fdroid.updater;
|
|||||||
|
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import org.fdroid.fdroid.ProgressListener;
|
import org.fdroid.fdroid.ProgressListener;
|
||||||
import org.fdroid.fdroid.RepoXMLHandler;
|
import org.fdroid.fdroid.RepoXMLHandler;
|
||||||
@ -11,7 +10,6 @@ import org.fdroid.fdroid.data.Apk;
|
|||||||
import org.fdroid.fdroid.data.App;
|
import org.fdroid.fdroid.data.App;
|
||||||
import org.fdroid.fdroid.data.Repo;
|
import org.fdroid.fdroid.data.Repo;
|
||||||
import org.fdroid.fdroid.data.RepoProvider;
|
import org.fdroid.fdroid.data.RepoProvider;
|
||||||
import org.fdroid.fdroid.net.Downloader;
|
|
||||||
import org.fdroid.fdroid.net.HttpDownloader;
|
import org.fdroid.fdroid.net.HttpDownloader;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
@ -30,7 +28,6 @@ abstract public class RepoUpdater {
|
|||||||
|
|
||||||
public static final int PROGRESS_TYPE_DOWNLOAD = 1;
|
public static final int PROGRESS_TYPE_DOWNLOAD = 1;
|
||||||
public static final int PROGRESS_TYPE_PROCESS_XML = 2;
|
public static final int PROGRESS_TYPE_PROCESS_XML = 2;
|
||||||
public static final String PROGRESS_DATA_REPO = "repo";
|
|
||||||
|
|
||||||
public static RepoUpdater createUpdaterFor(Context ctx, Repo repo) {
|
public static RepoUpdater createUpdaterFor(Context ctx, Repo repo) {
|
||||||
if (repo.fingerprint == null && repo.pubkey == null) {
|
if (repo.fingerprint == null && repo.pubkey == null) {
|
||||||
@ -86,17 +83,14 @@ abstract public class RepoUpdater {
|
|||||||
protected abstract String getIndexAddress();
|
protected abstract String getIndexAddress();
|
||||||
|
|
||||||
protected HttpDownloader downloadIndex() throws UpdateException {
|
protected HttpDownloader downloadIndex() throws UpdateException {
|
||||||
Bundle progressData = createProgressData(repo.address);
|
|
||||||
HttpDownloader downloader = null;
|
HttpDownloader downloader = null;
|
||||||
try {
|
try {
|
||||||
downloader = new HttpDownloader(getIndexAddress(), context);
|
downloader = new HttpDownloader(getIndexAddress(), context);
|
||||||
downloader.setETag(repo.lastetag);
|
downloader.setETag(repo.lastetag);
|
||||||
|
|
||||||
if (isInteractive()) {
|
if (isInteractive()) {
|
||||||
ProgressListener.Event event =
|
downloader.setProgressListener(progressListener,
|
||||||
new ProgressListener.Event(
|
new ProgressListener.Event(PROGRESS_TYPE_DOWNLOAD, repo.address));
|
||||||
RepoUpdater.PROGRESS_TYPE_DOWNLOAD, progressData);
|
|
||||||
downloader.setProgressListener(progressListener, event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int status = downloader.downloadHttpFile();
|
int status = downloader.downloadHttpFile();
|
||||||
@ -139,12 +133,6 @@ abstract public class RepoUpdater {
|
|||||||
return downloader;
|
return downloader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Bundle createProgressData(String repoAddress) {
|
|
||||||
Bundle data = new Bundle();
|
|
||||||
data.putString(PROGRESS_DATA_REPO, repoAddress);
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int estimateAppCount(File indexFile) {
|
private int estimateAppCount(File indexFile) {
|
||||||
int count = -1;
|
int count = -1;
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user