Added error() method to DownloadListener interface.

Downloads failing due to connection errors will now send a bar notification instead of throwing an ErrorHandler dialog.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han
2015-03-07 23:57:18 -05:00
parent cfd335de88
commit f8e91cba64
6 changed files with 30 additions and 13 deletions

View File

@@ -34,6 +34,8 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import org.newdawn.slick.util.Log;
/**
* File download.
*/
@@ -76,6 +78,9 @@ public class Download {
public interface DownloadListener {
/** Indication that a download has completed. */
public void completed();
/** Indication that an error has occurred. */
public void error();
}
/** The local path. */
@@ -177,7 +182,9 @@ public class Download {
contentLength = conn.getContentLength();
} catch (IOException e) {
status = Status.ERROR;
ErrorHandler.error("Failed to open connection.", e, false);
Log.warn("Failed to open connection.", e);
if (listener != null)
listener.error();
return;
}
@@ -193,6 +200,7 @@ public class Download {
updateReadSoFar();
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
if (status == Status.DOWNLOADING) { // not interrupted
// TODO: if connection is lost before a download finishes, it's still marked as "complete"
status = Status.COMPLETE;
rbc.close();
fos.close();
@@ -205,7 +213,9 @@ public class Download {
}
} catch (Exception e) {
status = Status.ERROR;
ErrorHandler.error("Failed to start download.", e, false);
Log.warn("Failed to start download.", e);
if (listener != null)
listener.error();
}
}
}.start();

View File

@@ -225,6 +225,11 @@ public class DownloadNode {
public void completed() {
UI.sendBarNotification(String.format("Download complete: %s", getTitle()));
}
@Override
public void error() {
UI.sendBarNotification("Download failed due to a connection error.");
}
});
}
}

View File

@@ -175,6 +175,12 @@ public class Updater {
status = Status.UPDATE_DOWNLOADED;
UI.sendBarNotification("Update has finished downloading.");
}
@Override
public void error() {
status = Status.CONNECTION_ERROR;
UI.sendBarNotification("Update failed due to a connection error.");
}
});
}
}