Report if a failure is following a download abort

This commit is contained in:
Gabriele M
2017-07-05 23:35:14 +02:00
parent 3de555ea06
commit 1795bd8b11
4 changed files with 12 additions and 9 deletions

View File

@@ -43,10 +43,12 @@ public class DownloadClient {
private static final Object DOWNLOAD_TAG = new Object(); private static final Object DOWNLOAD_TAG = new Object();
private boolean mCancelled = false;
public interface DownloadCallback { public interface DownloadCallback {
void onResponse(int statusCode, String url, Headers headers); void onResponse(int statusCode, String url, Headers headers);
void onSuccess(String body); void onSuccess(String body);
void onFailure(); void onFailure(boolean cancelled);
} }
public interface ProgressListener { public interface ProgressListener {
@@ -103,6 +105,7 @@ public class DownloadClient {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
mCancelled = true;
mClient.cancel(DOWNLOAD_TAG); mClient.cancel(DOWNLOAD_TAG);
} }
}).start(); }).start();
@@ -116,8 +119,8 @@ public class DownloadClient {
mClient.newCall(request).enqueue(new Callback() { mClient.newCall(request).enqueue(new Callback() {
@Override @Override
public void onFailure(Request request, IOException e) { public void onFailure(Request request, IOException e) {
Log.d(TAG, "Download failed", e); Log.d(TAG, "Download failed, cancelled=" + mCancelled, e);
callback.onFailure(); callback.onFailure(mCancelled);
} }
@Override @Override
@@ -177,7 +180,7 @@ public class DownloadClient {
@Override @Override
public void onFailure(Request request, IOException e) { public void onFailure(Request request, IOException e) {
Log.d(TAG, "Download failed", e); Log.d(TAG, "Download failed", e);
callback.onFailure(); callback.onFailure(mCancelled);
} }
@Override @Override
@@ -190,7 +193,7 @@ public class DownloadClient {
Log.d(TAG, "The server fulfilled the partial content request"); Log.d(TAG, "The server fulfilled the partial content request");
} else if (!isSuccessful(response.code())) { } else if (!isSuccessful(response.code())) {
Log.e(TAG, "The server replied with code " + response.code()); Log.e(TAG, "The server replied with code " + response.code());
callback.onFailure(); callback.onFailure(mCancelled);
return; return;
} }

View File

@@ -156,7 +156,7 @@ public class UpdatesActivity extends AppCompatActivity {
Log.d(TAG, "Checking " + url); Log.d(TAG, "Checking " + url);
DownloadClient.downloadFile(url, jsonFile, new DownloadClient.DownloadCallback() { DownloadClient.downloadFile(url, jsonFile, new DownloadClient.DownloadCallback() {
@Override @Override
public void onFailure() { public void onFailure(boolean cancelled) {
Log.e(TAG, "Could not download updates list"); Log.e(TAG, "Could not download updates list");
} }

View File

@@ -65,7 +65,7 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
String url = Utils.getServerURL(context); String url = Utils.getServerURL(context);
DownloadClient.downloadFile(url, jsonNew, new DownloadClient.DownloadCallback() { DownloadClient.downloadFile(url, jsonNew, new DownloadClient.DownloadCallback() {
@Override @Override
public void onFailure() { public void onFailure(boolean cancelled) {
Log.e(TAG, "Could not download updates list, scheduling new check"); Log.e(TAG, "Could not download updates list, scheduling new check");
scheduleUpdatesCheck(context); scheduleUpdatesCheck(context);
} }

View File

@@ -177,9 +177,9 @@ public class UpdaterController implements UpdaterControllerInt {
} }
@Override @Override
public void onFailure() { public void onFailure(boolean cancelled) {
// The client is null if we intentionally stopped the download // The client is null if we intentionally stopped the download
boolean cancelled = mDownloads.get(downloadId).mDownloadClient == null; cancelled = mDownloads.get(downloadId).mDownloadClient == null;
UpdateDownload update = mDownloads.get(downloadId).mUpdate; UpdateDownload update = mDownloads.get(downloadId).mUpdate;
if (cancelled) { if (cancelled) {
Log.d(TAG, "Download cancelled"); Log.d(TAG, "Download cancelled");