Optimize updater

pull/589/head
FongMi 1 year ago
parent 040ad92578
commit 95aa3fc69d
  1. 4
      app/src/main/java/com/fongmi/android/tv/api/config/LiveConfig.java
  2. 2
      app/src/main/java/com/fongmi/android/tv/api/config/VodConfig.java
  3. 15
      app/src/main/java/com/fongmi/android/tv/utils/Download.java
  4. 20
      app/src/mobile/java/com/fongmi/android/tv/Updater.java
  5. 2
      app/src/mobile/java/com/fongmi/android/tv/ui/activity/MainActivity.java
  6. 4
      app/src/mobile/java/com/fongmi/android/tv/ui/fragment/SettingFragment.java

@ -148,7 +148,7 @@ public class LiveConfig {
}
private void checkJson(JsonObject object, Callback callback) {
if (object.has("msg") && callback != null) {
if (object.has("msg")) {
App.post(() -> callback.error(object.get("msg").getAsString()));
} else if (object.has("urls")) {
parseDepot(object, callback);
@ -174,7 +174,7 @@ public class LiveConfig {
} catch (Throwable e) {
e.printStackTrace();
} finally {
if (callback != null) App.post(callback::success);
App.post(callback::success);
}
}

@ -126,7 +126,7 @@ public class VodConfig {
}
private void checkJson(JsonObject object, Callback callback) {
if (object.has("msg") && callback != null) {
if (object.has("msg")) {
App.post(() -> callback.error(object.get("msg").getAsString()));
} else if (object.has("urls")) {
parseDepot(object, callback);

@ -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);});
}
}
}

@ -25,17 +25,10 @@ import java.util.Locale;
public class Updater implements Download.Callback {
private DialogUpdateBinding binding;
private final Download download;
private AlertDialog dialog;
private boolean dev;
private static class Loader {
static volatile Updater INSTANCE = new Updater();
}
public static Updater get() {
return Loader.INSTANCE;
}
private File getFile() {
return Path.cache("update.apk");
}
@ -48,6 +41,14 @@ public class Updater implements Download.Callback {
return Github.getApk(dev, BuildConfig.FLAVOR_mode + "-" + BuildConfig.FLAVOR_api + "-" + BuildConfig.FLAVOR_abi);
}
public static Updater create() {
return new Updater();
}
public Updater() {
this.download = Download.create(getApk(), getFile(), this);
}
public Updater force() {
Notify.show(R.string.update_check);
Setting.putUpdate(true);
@ -103,12 +104,13 @@ public class Updater implements Download.Callback {
private void cancel(View view) {
Setting.putUpdate(false);
download.cancel();
dialog.dismiss();
}
private void confirm(View view) {
Download.create(getApk(), getFile(), this).start();
view.setEnabled(false);
download.start();
}
private void dismiss() {

@ -61,7 +61,7 @@ public class MainActivity extends BaseActivity implements NavigationBarView.OnIt
@Override
protected void initView(Bundle savedInstanceState) {
Updater.get().release().start(this);
Updater.create().release().start(this);
initFragment(savedInstanceState);
Server.get().start();
initConfig();

@ -278,11 +278,11 @@ public class SettingFragment extends BaseFragment implements ConfigCallback, Sit
}
private void onVersion(View view) {
Updater.get().force().release().start(getActivity());
Updater.create().force().release().start(getActivity());
}
private boolean onVersionDev(View view) {
Updater.get().force().dev().start(getActivity());
Updater.create().force().dev().start(getActivity());
return true;
}

Loading…
Cancel
Save