|
|
|
|
@ -1,6 +1,5 @@ |
|
|
|
|
package com.github.catvod.spider; |
|
|
|
|
|
|
|
|
|
import android.app.ProgressDialog; |
|
|
|
|
import android.content.Context; |
|
|
|
|
import android.net.Uri; |
|
|
|
|
|
|
|
|
|
@ -11,6 +10,7 @@ import com.github.catvod.bean.market.Item; |
|
|
|
|
import com.github.catvod.crawler.Spider; |
|
|
|
|
import com.github.catvod.net.OkHttp; |
|
|
|
|
import com.github.catvod.utils.FileUtil; |
|
|
|
|
import com.github.catvod.utils.Notify; |
|
|
|
|
import com.github.catvod.utils.Path; |
|
|
|
|
import com.github.catvod.utils.Util; |
|
|
|
|
|
|
|
|
|
@ -26,17 +26,8 @@ import okhttp3.Response; |
|
|
|
|
|
|
|
|
|
public class Market extends Spider { |
|
|
|
|
|
|
|
|
|
private ProgressDialog dialog; |
|
|
|
|
private static final String TAG = Market.class.getSimpleName(); |
|
|
|
|
private List<Data> datas; |
|
|
|
|
private boolean busy; |
|
|
|
|
|
|
|
|
|
public boolean isBusy() { |
|
|
|
|
return busy; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setBusy(boolean busy) { |
|
|
|
|
this.busy = busy; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void init(Context context, String extend) throws Exception { |
|
|
|
|
@ -60,34 +51,28 @@ public class Market extends Spider { |
|
|
|
|
@Override |
|
|
|
|
public String action(String action) { |
|
|
|
|
try { |
|
|
|
|
if (isBusy()) return ""; |
|
|
|
|
setBusy(true); |
|
|
|
|
Init.post(this::setDialog, 500); |
|
|
|
|
Response response = OkHttp.newCall(action); |
|
|
|
|
File file = Path.create(new File(Path.download(), Uri.parse(action).getLastPathSegment())); |
|
|
|
|
download(file, response.body().byteStream(), Double.parseDouble(response.header("Content-Length", "1"))); |
|
|
|
|
OkHttp.cancel(TAG); |
|
|
|
|
String name = Uri.parse(action).getLastPathSegment(); |
|
|
|
|
Notify.show("正在下載..." + name); |
|
|
|
|
Response response = OkHttp.newCall(action, TAG); |
|
|
|
|
File file = Path.create(new File(Path.download(), name)); |
|
|
|
|
download(file, response.body().byteStream()); |
|
|
|
|
if (file.getName().endsWith(".zip")) FileUtil.unzip(file, Path.download()); |
|
|
|
|
if (file.getName().endsWith(".apk")) FileUtil.openFile(file); |
|
|
|
|
else Result.notify("下載完成"); |
|
|
|
|
checkCopy(action); |
|
|
|
|
response.close(); |
|
|
|
|
dismiss(); |
|
|
|
|
return ""; |
|
|
|
|
return Result.notify("下載完成"); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
dismiss(); |
|
|
|
|
return Result.notify(e.getMessage()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void download(File file, InputStream is, double length) throws Exception { |
|
|
|
|
private void download(File file, InputStream is) throws Exception { |
|
|
|
|
try (BufferedInputStream input = new BufferedInputStream(is); FileOutputStream os = new FileOutputStream(file)) { |
|
|
|
|
byte[] buffer = new byte[4096]; |
|
|
|
|
byte[] buffer = new byte[16384]; |
|
|
|
|
int readBytes; |
|
|
|
|
long totalBytes = 0; |
|
|
|
|
while ((readBytes = input.read(buffer)) != -1) { |
|
|
|
|
totalBytes += readBytes; |
|
|
|
|
os.write(buffer, 0, readBytes); |
|
|
|
|
setProgress((int) (totalBytes / length * 100.0)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -102,37 +87,8 @@ public class Market extends Spider { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void setDialog() { |
|
|
|
|
Init.post(() -> { |
|
|
|
|
try { |
|
|
|
|
dialog = new ProgressDialog(Init.activity()); |
|
|
|
|
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
|
|
|
|
dialog.setCancelable(false); |
|
|
|
|
if (isBusy()) dialog.show(); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void dismiss() { |
|
|
|
|
Init.post(() -> { |
|
|
|
|
try { |
|
|
|
|
setBusy(false); |
|
|
|
|
if (dialog != null) dialog.dismiss(); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void setProgress(int value) { |
|
|
|
|
Init.post(() -> { |
|
|
|
|
try { |
|
|
|
|
if (dialog != null) dialog.setProgress(value); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
@Override |
|
|
|
|
public void destroy() { |
|
|
|
|
OkHttp.cancel(TAG); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|