fix: download failed while contentLength = -1

This commit is contained in:
hyb1996 2017-12-06 23:04:32 +08:00
parent 98e0e39e13
commit cd5def8700
4 changed files with 29 additions and 26 deletions

View File

@ -111,7 +111,7 @@ public class DownloadManager {
mInputStream = body.byteStream();
long total = body.contentLength();
long read = 0;
while (read < total) {
while (true) {
if (!mStatus.get()) {
onCancel();
return;
@ -122,7 +122,9 @@ public class DownloadManager {
}
read += len;
mFileOutputStream.write(buffer, 0, len);
mProgress.onNext((int) (100 * read / total));
if (total > 0) {
mProgress.onNext((int) (100 * read / total));
}
}
mProgress.onComplete();
recycle();

View File

@ -28,6 +28,7 @@ import com.stardust.scriptdroid.model.script.ScriptFile;
import com.stardust.scriptdroid.model.script.Scripts;
import com.stardust.scriptdroid.storage.file.StorageFileProvider;
import com.stardust.scriptdroid.network.download.DownloadManager;
import com.stardust.scriptdroid.tool.SimpleObserver;
import com.stardust.scriptdroid.ui.filechooser.FileChooserDialogBuilder;
import com.stardust.scriptdroid.ui.shortcut.ShortcutCreateActivity;
import com.stardust.scriptdroid.ui.timing.TimedTaskSettingActivity_;
@ -274,18 +275,22 @@ public class ScriptOperations {
DownloadManager.getInstance().download(url, path)
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(progressDialog::setProgress)
.doOnComplete(() -> {
progressDialog.dismiss();
subject.onNext(new ScriptFile(path));
subject.onComplete();
})
.doOnError(error -> {
Log.e(LOG_TAG, "Download failed", error);
progressDialog.dismiss();
showMessage(R.string.text_download_failed);
subject.onError(error);
})
.subscribe();
.subscribe(new SimpleObserver<Integer>() {
@Override
public void onComplete() {
progressDialog.dismiss();
subject.onNext(new ScriptFile(path));
subject.onComplete();
}
@Override
public void onError(Throwable error) {
Log.e(LOG_TAG, "Download failed", error);
progressDialog.dismiss();
showMessage(R.string.text_download_failed);
subject.onError(error);
}
});
return subject;
}

View File

@ -5,6 +5,7 @@ import android.net.Uri;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebView;
@ -21,6 +22,7 @@ import java.util.regex.Pattern;
import butterknife.OnClick;
import butterknife.Optional;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
/**
@ -55,7 +57,6 @@ public class CommunityWebView extends EWebView {
.title(fileName)
.item(R.id.save, R.drawable.ic_file_download_black_48dp, R.string.text_download)
.item(R.id.run, R.drawable.ic_play_arrow_white_48dp, R.string.text_run)
.item(R.id.edit, R.drawable.ic_edit_white_24dp, R.string.text_save_and_edit)
.bindItemClick(this)
.build());
mBottomSheetDialog.show();
@ -68,19 +69,12 @@ public class CommunityWebView extends EWebView {
new ScriptOperations(getContext(), CommunityWebView.this)
.download(mUrl)
.observeOn(AndroidSchedulers.mainThread())
.onErrorResumeNext(Observable.empty())
.subscribe(file ->
Snackbar.make(CommunityWebView.this, getResources().getString(R.string.format_file_downloaded, file.getPath())
, Snackbar.LENGTH_SHORT).show());
}
@Optional
@OnClick(R.id.edit)
void saveAndEdit() {
dismissBottomSheetDialog();
new ScriptOperations(getContext(), CommunityWebView.this)
.download(mUrl)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(Scripts::edit);
, Snackbar.LENGTH_LONG)
.setAction(R.string.text_open, v -> Scripts.edit(file))
.show());
}
@Optional
@ -90,6 +84,7 @@ public class CommunityWebView extends EWebView {
new ScriptOperations(getContext(), CommunityWebView.this)
.temporarilyDownload(mUrl)
.observeOn(AndroidSchedulers.mainThread())
.onErrorResumeNext(Observable.empty())
.subscribe(file -> {
Snackbar.make(CommunityWebView.this, R.string.text_start_running, Snackbar.LENGTH_SHORT).show();
Scripts.run(file);

View File

@ -325,4 +325,5 @@
<string name="no_apk_builder_plugin">没有安装打包插件,是否立即下载?</string>
<string name="text_apk_builder_plugin_unavailable">打包插件不可用</string>
<string name="text_detail">详情</string>
<string name="text_open">打开</string>
</resources>