From d9f9b682d481aa98949a9ef99b8997cfd6520e31 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 25 Feb 2014 14:56:48 -0500 Subject: [PATCH] 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 --- src/org/fdroid/fdroid/ApkDownloader.java | 12 ++++-- src/org/fdroid/fdroid/ProgressListener.java | 39 +++++++++++-------- src/org/fdroid/fdroid/RepoXMLHandler.java | 6 +-- src/org/fdroid/fdroid/UpdateService.java | 5 +-- src/org/fdroid/fdroid/net/Downloader.java | 5 --- .../fdroid/fdroid/updater/RepoUpdater.java | 16 +------- 6 files changed, 37 insertions(+), 46 deletions(-) diff --git a/src/org/fdroid/fdroid/ApkDownloader.java b/src/org/fdroid/fdroid/ApkDownloader.java index bef04e314..429d8e9fc 100644 --- a/src/org/fdroid/fdroid/ApkDownloader.java +++ b/src/org/fdroid/fdroid/ApkDownloader.java @@ -24,9 +24,11 @@ import java.io.*; import android.util.Log; import org.fdroid.fdroid.data.Apk; +import org.fdroid.fdroid.net.Downloader; import org.fdroid.fdroid.net.HttpDownloader; 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_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... + String remoteAddress = remoteFile(); + HttpDownloader downloader = new HttpDownloader(remoteAddress, localfile); - HttpDownloader downloader = new HttpDownloader(remoteFile(), localfile); - downloader.setProgressListener(listener); + if (listener != null) { + 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(); if (httpStatus != 200 || !localfile.exists()) { diff --git a/src/org/fdroid/fdroid/ProgressListener.java b/src/org/fdroid/fdroid/ProgressListener.java index 5550c1492..c7371f0ee 100644 --- a/src/org/fdroid/fdroid/ProgressListener.java +++ b/src/org/fdroid/fdroid/ProgressListener.java @@ -1,8 +1,10 @@ + package org.fdroid.fdroid; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; +import android.text.TextUtils; public interface ProgressListener { @@ -14,6 +16,7 @@ public interface ProgressListener { public static class Event implements Parcelable { public static final int NO_VALUE = Integer.MIN_VALUE; + public static final String PROGRESS_DATA_REPO = "repo"; public final int type; public final Bundle data; @@ -29,27 +32,18 @@ public interface ProgressListener { this(type, NO_VALUE, NO_VALUE, null); } - public Event(int type, Bundle data) { - this(type, NO_VALUE, NO_VALUE, data); + public Event(int type, String repoAddress) { + this(type, NO_VALUE, NO_VALUE, repoAddress); } - public Event(int type, int progress) { - 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) { + public Event(int type, int progress, int total, String repoAddress) { this.type = type; this.progress = progress; this.total = total; - this.data = data == null ? new Bundle() : data; + if (TextUtils.isEmpty(repoAddress)) + this.data = new Bundle(); + else + this.data = createProgressData(repoAddress); } @Override @@ -68,7 +62,8 @@ public interface ProgressListener { public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { @Override 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 @@ -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; + } + } } diff --git a/src/org/fdroid/fdroid/RepoXMLHandler.java b/src/org/fdroid/fdroid/RepoXMLHandler.java index 1c7fb5009..541dfff2e 100644 --- a/src/org/fdroid/fdroid/RepoXMLHandler.java +++ b/src/org/fdroid/fdroid/RepoXMLHandler.java @@ -19,7 +19,6 @@ package org.fdroid.fdroid; -import android.os.Bundle; import org.fdroid.fdroid.data.Apk; import org.fdroid.fdroid.data.App; import org.fdroid.fdroid.data.Repo; @@ -279,12 +278,11 @@ public class RepoXMLHandler extends DefaultHandler { } else if (localName.equals("application") && curapp == null) { curapp = new App(); curapp.id = attributes.getValue("", "id"); - Bundle progressData = RepoUpdater.createProgressData(repo.address); progressCounter ++; progressListener.onProgress( new ProgressListener.Event( - RepoUpdater.PROGRESS_TYPE_PROCESS_XML, progressCounter, - totalAppCount, progressData)); + RepoUpdater.PROGRESS_TYPE_PROCESS_XML, + progressCounter, totalAppCount, repo.address)); } else if (localName.equals("package") && curapp != null && curapk == null) { curapk = new Apk(); diff --git a/src/org/fdroid/fdroid/UpdateService.java b/src/org/fdroid/fdroid/UpdateService.java index 471b120f2..06cd30a1a 100644 --- a/src/org/fdroid/fdroid/UpdateService.java +++ b/src/org/fdroid/fdroid/UpdateService.java @@ -194,7 +194,7 @@ public class UpdateService extends IntentService implements ProgressListener { if (message != null && message.length() > 0) resultData.putString(RESULT_MESSAGE, message); if (event == null) - event = new Event(statusCode); + event = new ProgressListener.Event(statusCode); resultData.putParcelable(RESULT_EVENT, event); receiver.send(statusCode, resultData); } @@ -675,14 +675,13 @@ public class UpdateService extends IntentService implements ProgressListener { @Override public void onProgress(ProgressListener.Event event) { String message = ""; + String repoAddress = event.getRepoAddress(); if (event.type == RepoUpdater.PROGRESS_TYPE_DOWNLOAD) { - String repoAddress = event.data.getString(RepoUpdater.PROGRESS_DATA_REPO); String downloadedSize = Utils.getFriendlySize( event.progress ); String totalSize = Utils.getFriendlySize( event.total ); int percent = (int)((double)event.progress/event.total * 100); message = getString(R.string.status_download, repoAddress, downloadedSize, totalSize, percent); } 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); } sendStatus(STATUS_INFO, message); diff --git a/src/org/fdroid/fdroid/net/Downloader.java b/src/org/fdroid/fdroid/net/Downloader.java index 304e3640b..fac02f64c 100644 --- a/src/org/fdroid/fdroid/net/Downloader.java +++ b/src/org/fdroid/fdroid/net/Downloader.java @@ -48,11 +48,6 @@ public abstract class Downloader { outputFile = null; } - public void setProgressListener(ProgressListener listener) { - this.progressListener = listener; - this.progressEvent = new ProgressListener.Event(EVENT_PROGRESS, totalDownloadSize()); - } - public void setProgressListener(ProgressListener progressListener, ProgressListener.Event progressEvent) { this.progressListener = progressListener; diff --git a/src/org/fdroid/fdroid/updater/RepoUpdater.java b/src/org/fdroid/fdroid/updater/RepoUpdater.java index a5817cc50..3382368cb 100644 --- a/src/org/fdroid/fdroid/updater/RepoUpdater.java +++ b/src/org/fdroid/fdroid/updater/RepoUpdater.java @@ -2,7 +2,6 @@ package org.fdroid.fdroid.updater; import android.content.ContentValues; import android.content.Context; -import android.os.Bundle; import android.util.Log; import org.fdroid.fdroid.ProgressListener; 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.Repo; import org.fdroid.fdroid.data.RepoProvider; -import org.fdroid.fdroid.net.Downloader; import org.fdroid.fdroid.net.HttpDownloader; import org.xml.sax.InputSource; 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_PROCESS_XML = 2; - public static final String PROGRESS_DATA_REPO = "repo"; public static RepoUpdater createUpdaterFor(Context ctx, Repo repo) { if (repo.fingerprint == null && repo.pubkey == null) { @@ -86,17 +83,14 @@ abstract public class RepoUpdater { protected abstract String getIndexAddress(); protected HttpDownloader downloadIndex() throws UpdateException { - Bundle progressData = createProgressData(repo.address); HttpDownloader downloader = null; try { downloader = new HttpDownloader(getIndexAddress(), context); downloader.setETag(repo.lastetag); if (isInteractive()) { - ProgressListener.Event event = - new ProgressListener.Event( - RepoUpdater.PROGRESS_TYPE_DOWNLOAD, progressData); - downloader.setProgressListener(progressListener, event); + downloader.setProgressListener(progressListener, + new ProgressListener.Event(PROGRESS_TYPE_DOWNLOAD, repo.address)); } int status = downloader.downloadHttpFile(); @@ -139,12 +133,6 @@ abstract public class RepoUpdater { 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) { int count = -1; try {