Optimize download

pull/605/head
jhengazuji 4 months ago
parent 75bfb2f95c
commit 665498ec5c
  1. 4
      app/src/leanback/java/com/fongmi/android/tv/Updater.java
  2. 2
      app/src/main/java/com/fongmi/android/tv/api/EpgParser.java
  3. 39
      app/src/main/java/com/fongmi/android/tv/api/config/WallConfig.java
  4. 6
      app/src/main/java/com/fongmi/android/tv/player/extractor/TVBus.java
  5. 67
      app/src/main/java/com/fongmi/android/tv/utils/Download.java
  6. 4
      app/src/mobile/java/com/fongmi/android/tv/Updater.java

@ -44,7 +44,7 @@ public class Updater implements Download.Callback {
} }
public Updater() { public Updater() {
this.download = Download.create(getApk(), getFile(), this); this.download = Download.create(getApk(), getFile());
} }
public Updater force() { public Updater force() {
@ -96,7 +96,7 @@ public class Updater implements Download.Callback {
private void confirm(View view) { private void confirm(View view) {
view.setEnabled(false); view.setEnabled(false);
download.start(); download.start(this);
} }
private void dismiss() { private void dismiss() {

@ -36,7 +36,7 @@ public class EpgParser {
public static boolean start(Live live, String url) throws Exception { public static boolean start(Live live, String url) throws Exception {
File file = Path.epg(Uri.parse(url).getLastPathSegment()); File file = Path.epg(Uri.parse(url).getLastPathSegment());
Download.create(url, file).get(shouldDownload(file)); if (shouldDownload(file)) Download.create(url, file).get();
if (file.getName().endsWith(".gz")) readGzip(live, file); if (file.getName().endsWith(".gz")) readGzip(live, file);
else readXml(live, file); else readXml(live, file);
return true; return true;

@ -18,16 +18,20 @@ import com.fongmi.android.tv.utils.FileUtil;
import com.fongmi.android.tv.utils.Notify; import com.fongmi.android.tv.utils.Notify;
import com.fongmi.android.tv.utils.ResUtil; import com.fongmi.android.tv.utils.ResUtil;
import com.fongmi.android.tv.utils.UrlUtil; import com.fongmi.android.tv.utils.UrlUtil;
import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Path; import com.github.catvod.utils.Path;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InterruptedIOException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
public class WallConfig { public class WallConfig {
private static final String TAG = WallConfig.class.getSimpleName();
private final AtomicInteger taskId = new AtomicInteger(0);
private Config config; private Config config;
private Future<?> future; private Future<?> future;
private boolean sync; private boolean sync;
@ -64,7 +68,7 @@ public class WallConfig {
} }
private boolean isCanceled(Throwable e) { private boolean isCanceled(Throwable e) {
return e.getCause() instanceof InterruptedException || e.getCause() instanceof InterruptedIOException; return e instanceof InterruptedException || e instanceof RuntimeException;
} }
public void load() { public void load() {
@ -72,40 +76,47 @@ public class WallConfig {
} }
public void load(Callback callback) { public void load(Callback callback) {
int id = taskId.incrementAndGet();
if (future != null && !future.isDone()) future.cancel(true); if (future != null && !future.isDone()) future.cancel(true);
future = App.submit(() -> loadConfig(callback)); future = App.submit(() -> loadConfig(id, config, callback));
callback.start(); callback.start();
} }
private void loadConfig(Callback callback) { private void loadConfig(int id, Config config, Callback callback) {
try { try {
download(); OkHttp.cancel(TAG);
config.update(); download(id, callback);
RefreshEvent.wall(); if (taskId.get() == id) config.update();
App.post(callback::success);
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace();
if (isCanceled(e)) return; if (isCanceled(e)) return;
if (taskId.get() != id) return;
if (TextUtils.isEmpty(config.getUrl())) App.post(() -> callback.error("")); if (TextUtils.isEmpty(config.getUrl())) App.post(() -> callback.error(""));
else App.post(() -> callback.error(Notify.getError(R.string.error_config_get, e))); else App.post(() -> callback.error(Notify.getError(R.string.error_config_get, e)));
Setting.putWall(1); Setting.putWall(1);
RefreshEvent.wall(); RefreshEvent.wall();
e.printStackTrace();
} }
} }
private void download() throws Throwable { private void download(int id, Callback callback) throws Throwable {
File file = FileUtil.getWall(0); File file = FileUtil.getWall(0);
Path.clear(FileUtil.getWallCache());
if (getUrl().startsWith("file")) Path.copy(Path.local(getUrl()), file); if (getUrl().startsWith("file")) Path.copy(Path.local(getUrl()), file);
else Download.create(UrlUtil.convert(getUrl()), file).start(); else Download.create(UrlUtil.convert(getUrl()), file).tag(TAG).get();
if (!Path.exists(file)) throw new FileNotFoundException(); if (!Path.exists(file)) throw new FileNotFoundException();
createSnapshot(file); if (taskId.get() != id) return;
setWallType(file);
setSnapshot(file);
RefreshEvent.wall();
App.post(callback::success);
}
private void setWallType(File file) {
Setting.putWallType(0); Setting.putWallType(0);
if (isGif(file)) Setting.putWallType(1); if (isGif(file)) Setting.putWallType(1);
else if (isVideo(file)) Setting.putWallType(2); else if (isVideo(file)) Setting.putWallType(2);
} }
private void createSnapshot(File file) throws Throwable { private void setSnapshot(File file) throws Throwable {
Bitmap bitmap = Glide.with(App.get()).asBitmap().frame(0).load(file).override(ResUtil.getScreenWidth(), ResUtil.getScreenHeight()).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).submit().get(); Bitmap bitmap = Glide.with(App.get()).asBitmap().frame(0).load(file).override(ResUtil.getScreenWidth(), ResUtil.getScreenHeight()).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).submit().get();
try (FileOutputStream fos = new FileOutputStream(FileUtil.getWallCache())) { try (FileOutputStream fos = new FileOutputStream(FileUtil.getWallCache())) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

@ -47,9 +47,9 @@ public class TVBus implements Source.Extractor, Listener {
} }
private String getPath(String url) { private String getPath(String url) {
File file = new File(Path.so(), UrlUtil.path(url)); File so = new File(Path.so(), UrlUtil.path(url));
Download.create(url, file).get(!Path.exists(file)); if (!Path.exists(so)) Download.create(url, so).get();
return file.getAbsolutePath(); return so.getAbsolutePath();
} }
@Override @Override

@ -1,5 +1,7 @@
package com.fongmi.android.tv.utils; package com.fongmi.android.tv.utils;
import android.util.Log;
import com.fongmi.android.tv.App; import com.fongmi.android.tv.App;
import com.github.catvod.net.OkHttp; import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Path; import com.github.catvod.utils.Path;
@ -8,7 +10,9 @@ import com.google.common.net.HttpHeaders;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.concurrent.Future;
import okhttp3.Response; import okhttp3.Response;
@ -17,71 +21,64 @@ public class Download {
private final File file; private final File file;
private final String url; private final String url;
private Callback callback; private Callback callback;
private Future<?> future;
private String tag;
public static Download create(String url, File file) { public static Download create(String url, File file) {
return create(url, file, null); return new Download(url, file);
}
public static Download create(String url, File file, Callback callback) {
return new Download(url, file, callback);
} }
public Download(String url, File file, Callback callback) { public Download(String url, File file) {
this.tag = url;
this.url = url; this.url = url;
this.file = file; this.file = file;
this.callback = callback;
} }
public File get() { public Download tag(String tag) {
return get(true); this.tag = tag;
return this;
} }
public File get(boolean force) { public File get() {
if (force) performSync(); doInBackground();
return file; return file;
} }
public void start() { public void start(Callback callback) {
if (url.startsWith("file")) return; this.callback = callback;
if (callback == null) performSync(); future = App.submit(this::doInBackground);
else App.execute(this::performAsync);
}
public void cancel() {
OkHttp.cancel(url);
Path.clear(file);
callback = null;
} }
private void performSync() { public Download cancel() {
try (Response res = OkHttp.newCall(url, url).execute()) { if (future != null) future.cancel(true);
download(res.body().byteStream(), getLength(res)); OkHttp.cancel(tag);
} catch (Exception e) { future = null;
e.printStackTrace(); return this;
throw new RuntimeException(e.getMessage(), e);
}
} }
private void performAsync() { private void doInBackground() {
try (Response res = OkHttp.newCall(url, url).execute()) { try (Response res = OkHttp.newCall(url, tag).execute()) {
download(res.body().byteStream(), getLength(res)); download(res.body().byteStream(), getLength(res));
App.post(() -> {if (callback != null) callback.success(file);}); if (callback != null) App.post(() -> callback.success(file));
} catch (Exception e) { } catch (IOException e) {
App.post(() -> {if (callback != null) callback.error(e.getMessage());}); Path.clear(file);
if (callback != null) App.post(() -> callback.error(e.getMessage()));
else throw new RuntimeException(e.getMessage(), e);
} }
} }
private void download(InputStream is, double length) throws Exception { private void download(InputStream is, double length) throws IOException {
try (BufferedInputStream input = new BufferedInputStream(is); FileOutputStream os = new FileOutputStream(Path.create(file))) { try (BufferedInputStream input = new BufferedInputStream(is); FileOutputStream os = new FileOutputStream(Path.create(file))) {
byte[] buffer = new byte[16384]; byte[] buffer = new byte[16384];
int readBytes; int readBytes;
long totalBytes = 0; long totalBytes = 0;
while ((readBytes = input.read(buffer)) != -1) { while ((readBytes = input.read(buffer)) != -1) {
if (Thread.interrupted()) return;
totalBytes += readBytes; totalBytes += readBytes;
os.write(buffer, 0, readBytes); os.write(buffer, 0, readBytes);
if (length <= 0) continue; if (length <= 0) continue;
int progress = (int) (totalBytes / length * 100.0); int progress = (int) (totalBytes / length * 100.0);
App.post(() -> {if (callback != null) callback.progress(progress);}); if (callback != null) App.post(() -> callback.progress(progress));
} }
} }
} }

@ -45,7 +45,7 @@ public class Updater implements Download.Callback {
} }
public Updater() { public Updater() {
this.download = Download.create(getApk(), getFile(), this); this.download = Download.create(getApk(), getFile());
} }
public Updater force() { public Updater force() {
@ -96,7 +96,7 @@ public class Updater implements Download.Callback {
private void confirm(View view) { private void confirm(View view) {
view.setEnabled(false); view.setEnabled(false);
download.start(); download.start(this);
} }
private void dismiss() { private void dismiss() {

Loading…
Cancel
Save