Generate download clients using a builder class

This change allows to define a proper interface for the download
client and keep its implementation completely separate. It also
allows to create clients without starting the download right away,
which could be useful when defining callbacks that require a
reference to the client.

Note that this change also drops the unused methods of DownloadClient.
This commit is contained in:
Gabriele M
2017-07-08 17:20:47 +02:00
parent 5b0e37ea22
commit 81229329f1
5 changed files with 181 additions and 94 deletions

View File

@@ -28,6 +28,7 @@ import android.support.v7.app.NotificationCompat;
import android.util.Log;
import org.json.JSONException;
import org.lineageos.updater.download.DownloadClient;
import org.lineageos.updater.misc.Constants;
import org.lineageos.updater.misc.Utils;
@@ -64,7 +65,7 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
final File json = Utils.getCachedUpdateList(context);
final File jsonNew = new File(json.getAbsolutePath() + ".tmp");
String url = Utils.getServerURL(context);
DownloadClient.downloadFile(url, jsonNew, new DownloadClient.DownloadCallback() {
DownloadClient.DownloadCallback callback = new DownloadClient.DownloadCallback() {
@Override
public void onFailure(boolean cancelled) {
Log.e(TAG, "Could not download updates list, scheduling new check");
@@ -91,7 +92,14 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
scheduleUpdatesCheck(context);
}
}
});
};
DownloadClient downloadClient = new DownloadClient.Builder()
.setUrl(url)
.setDestination(jsonNew)
.setDownloadCallback(callback)
.build();
downloadClient.start();
}
}