Optimize wall config

pull/605/head
jhengazuji 5 months ago
parent a4c28e1262
commit d498a771df
  1. 10
      app/src/main/java/com/fongmi/android/tv/api/config/WallConfig.java
  2. 30
      app/src/main/java/com/fongmi/android/tv/utils/Download.java

@ -23,6 +23,7 @@ import com.github.catvod.utils.Path;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InterruptedIOException;
import java.util.concurrent.Future;
public class WallConfig {
@ -67,8 +68,8 @@ public class WallConfig {
return this;
}
public Config getConfig() {
return config == null ? Config.wall() : config;
private boolean isCanceled(Throwable e) {
return e.getCause() instanceof InterruptedException || e.getCause() instanceof InterruptedIOException;
}
public void load() {
@ -88,6 +89,7 @@ public class WallConfig {
RefreshEvent.wall();
App.post(callback::success);
} catch (Throwable e) {
if (isCanceled(e)) return;
if (TextUtils.isEmpty(config.getUrl())) App.post(() -> callback.error(""));
else App.post(() -> callback.error(Notify.getError(R.string.error_config_get, e)));
Setting.putWall(1);
@ -138,4 +140,8 @@ public class WallConfig {
public boolean needSync(String url) {
return sync || TextUtils.isEmpty(config.getUrl()) || url.equals(config.getUrl());
}
public Config getConfig() {
return config == null ? Config.wall() : config;
}
}

@ -34,8 +34,8 @@ public class Download {
public void start() {
if (url.startsWith("file")) return;
if (callback == null) doInBackground();
else App.execute(this::doInBackground);
if (callback == null) performSync();
else App.execute(this::performAsync);
}
public void cancel() {
@ -44,10 +44,18 @@ public class Download {
callback = null;
}
private void doInBackground() {
private void performSync() {
try (Response res = OkHttp.newCall(url, url).execute()) {
Path.create(file);
download(res.body().byteStream(), Double.parseDouble(res.header(HttpHeaders.CONTENT_LENGTH, "1")));
download(res.body().byteStream(), getLength(res));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
}
private void performAsync() {
try (Response res = OkHttp.newCall(url, url).execute()) {
download(res.body().byteStream(), getLength(res));
App.post(() -> {if (callback != null) callback.success(file);});
} catch (Exception e) {
App.post(() -> {if (callback != null) callback.error(e.getMessage());});
@ -55,19 +63,29 @@ public class Download {
}
private void download(InputStream is, double length) throws Exception {
try (BufferedInputStream input = new BufferedInputStream(is); FileOutputStream os = new FileOutputStream(file)) {
try (BufferedInputStream input = new BufferedInputStream(is); FileOutputStream os = new FileOutputStream(Path.create(file))) {
byte[] buffer = new byte[16384];
int readBytes;
long totalBytes = 0;
while ((readBytes = input.read(buffer)) != -1) {
totalBytes += readBytes;
os.write(buffer, 0, readBytes);
if (length <= 0) continue;
int progress = (int) (totalBytes / length * 100.0);
App.post(() -> {if (callback != null) callback.progress(progress);});
}
}
}
private double getLength(Response res) {
try {
String header = res.header(HttpHeaders.CONTENT_LENGTH);
return header != null ? Double.parseDouble(header) : -1;
} catch (Exception e) {
return -1;
}
}
public interface Callback {
void progress(int progress);

Loading…
Cancel
Save