|
|
|
|
@ -16,7 +16,7 @@ public class Download { |
|
|
|
|
|
|
|
|
|
private final File file; |
|
|
|
|
private final String url; |
|
|
|
|
private final Callback callback; |
|
|
|
|
private Callback callback; |
|
|
|
|
|
|
|
|
|
public static Download create(String url, File file) { |
|
|
|
|
return create(url, file, null); |
|
|
|
|
@ -38,14 +38,19 @@ public class Download { |
|
|
|
|
else App.execute(this::doInBackground); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void cancel() { |
|
|
|
|
OkHttp.cancel(url); |
|
|
|
|
callback = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void doInBackground() { |
|
|
|
|
try { |
|
|
|
|
Path.create(file); |
|
|
|
|
Response response = OkHttp.newCall(url).execute(); |
|
|
|
|
Response response = OkHttp.newCall(url, url).execute(); |
|
|
|
|
download(response.body().byteStream(), Double.parseDouble(response.header(HttpHeaders.CONTENT_LENGTH, "1"))); |
|
|
|
|
if (callback != null) App.post(() -> callback.success(file)); |
|
|
|
|
App.post(() -> {if (callback != null) callback.success(file);}); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
if (callback != null) App.post(() -> callback.error(e.getMessage())); |
|
|
|
|
App.post(() -> {if (callback != null) callback.error(e.getMessage());}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -59,7 +64,7 @@ public class Download { |
|
|
|
|
totalBytes += readBytes; |
|
|
|
|
os.write(buffer, 0, readBytes); |
|
|
|
|
int progress = (int) (totalBytes / length * 100.0); |
|
|
|
|
if (callback != null) App.post(() -> callback.progress(progress)); |
|
|
|
|
App.post(() -> {if (callback != null) callback.progress(progress);}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|