Add progress dialog

This commit is contained in:
FongMi 2023-10-17 18:28:34 +08:00
parent c800627003
commit 03d0bdc868
3 changed files with 57 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package com.github.catvod.spider; package com.github.catvod.spider;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
@ -18,32 +19,42 @@ import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random;
import okhttp3.Response; import okhttp3.Response;
public class Market extends Spider { public class Market extends Spider {
private List<Item> mItems; private ProgressDialog dialog;
private List<Item> items;
private boolean busy;
public boolean isBusy() {
return busy;
}
public void setBusy(boolean busy) {
this.busy = busy;
}
@Override @Override
public void init(Context context, String extend) throws Exception { public void init(Context context, String extend) throws Exception {
mItems = Item.arrayFrom(extend); items = Item.arrayFrom(extend);
} }
@Override @Override
public String homeVideoContent() { public String homeVideoContent() {
List<Vod> list = new ArrayList<>(); List<Vod> list = new ArrayList<>();
for (Item item : mItems) list.add(item.vod()); for (Item item : items) list.add(item.vod());
return Result.string(list); return Result.string(list);
} }
@Override @Override
public String detailContent(List<String> ids) throws Exception { public String detailContent(List<String> ids) throws Exception {
Init.execute(() -> download(ids.get(0))); Init.run(this::finish);
Vod vod = new Vod(); Vod vod = new Vod();
vod.setVodPlayFrom("FongMi"); vod.setVodPlayFrom("FongMi");
vod.setVodPlayUrl("FongMi$FongMi"); vod.setVodPlayUrl("FongMi$FongMi");
Init.execute(() -> download(ids.get(0)));
return Result.string(vod); return Result.string(vod);
} }
@ -58,14 +69,17 @@ public class Market extends Spider {
private void download(String url) { private void download(String url) {
try { try {
Init.run(this::finish); if (isBusy()) return;
Utils.notify("正在下載..."); setBusy(true);
Init.run(this::setDialog, 500);
Response response = OkHttp.newCall(url); Response response = OkHttp.newCall(url);
File file = FileUtil.getCacheFile(Uri.parse(url).getLastPathSegment()); File file = FileUtil.getCacheFile(Uri.parse(url).getLastPathSegment());
download(file, response.body().byteStream(), Double.parseDouble(response.header("Content-Length", "1"))); download(file, response.body().byteStream(), Double.parseDouble(response.header("Content-Length", "1")));
FileUtil.openFile(FileUtil.chmod(file)); FileUtil.openFile(FileUtil.chmod(file));
dismiss();
} catch (Exception e) { } catch (Exception e) {
Utils.notify(e.getMessage()); Utils.notify(e.getMessage());
dismiss();
} }
} }
@ -78,9 +92,42 @@ public class Market extends Spider {
while ((readBytes = input.read(buffer)) != -1) { while ((readBytes = input.read(buffer)) != -1) {
totalBytes += readBytes; totalBytes += readBytes;
os.write(buffer, 0, readBytes); os.write(buffer, 0, readBytes);
int progress = (int) (totalBytes / length * 100.0); setProgress((int) (totalBytes / length * 100.0));
if (progress % 10 <= new Random().nextInt(6)) Utils.notify("正在下載..." + progress + "%");
} }
} }
} }
private void setDialog() {
Init.run(() -> {
try {
dialog = new ProgressDialog(Init.getActivity());
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setCancelable(false);
dialog.show();
} catch (Exception e) {
e.printStackTrace();
}
});
}
private void dismiss() {
Init.run(() -> {
try {
setBusy(false);
if (dialog != null) dialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
});
}
private void setProgress(int value) {
Init.run(() -> {
try {
if (dialog != null) dialog.setProgress(value);
} catch (Exception e) {
e.printStackTrace();
}
});
}
} }

Binary file not shown.

View File

@ -1 +1 @@
6d1c97eeff8c95ad0f456d066efde69b 8a9d215c9f79d7e5ce9e223b40ed81e8