|
10 | 10 | import com.aries.library.fast.demo.R;
|
11 | 11 | import com.aries.library.fast.demo.entity.UpdateEntity;
|
12 | 12 | import com.aries.library.fast.demo.retrofit.repository.ApiRepository;
|
13 |
| -import com.aries.library.fast.demo.widget.ProgressDialog; |
14 | 13 | import com.aries.library.fast.manager.LoggerManager;
|
15 | 14 | import com.aries.library.fast.retrofit.FastDownloadObserver;
|
16 | 15 | import com.aries.library.fast.retrofit.FastLoadingObserver;
|
17 | 16 | import com.aries.library.fast.retrofit.FastObserver;
|
18 |
| -import com.aries.library.fast.retrofit.FastRetrofit; |
19 |
| -import com.aries.library.fast.util.FastFileUtil; |
20 |
| -import com.aries.library.fast.util.FastFormatUtil; |
21 | 17 | import com.aries.library.fast.util.FastStackUtil;
|
22 | 18 | import com.aries.library.fast.util.ToastUtil;
|
| 19 | +import com.download.library.DownloadImpl; |
23 | 20 | import com.trello.rxlifecycle3.android.ActivityEvent;
|
24 |
| -import com.trello.rxlifecycle3.components.support.RxAppCompatActivity; |
25 | 21 |
|
26 |
| -import java.io.File; |
27 | 22 | import java.lang.ref.SoftReference;
|
28 |
| -import java.util.HashMap; |
29 |
| -import java.util.Map; |
30 | 23 |
|
31 | 24 | import io.reactivex.annotations.NonNull;
|
32 | 25 |
|
@@ -161,74 +154,15 @@ public void downloadApk(UpdateEntity entity, String fileName, boolean isRangeEna
|
161 | 154 | if (activity == null || activity.isFinishing()) {
|
162 | 155 | return;
|
163 | 156 | }
|
164 |
| - |
165 |
| - ProgressDialog mProgressDialog = new ProgressDialog(activity); |
166 |
| - mProgressDialog.setTitle(entity.getTitle()); |
167 |
| - mProgressDialog.setIndeterminate(false); |
168 |
| - mProgressDialog.setMessage(entity.getMessage()); |
169 |
| - mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
170 |
| - mProgressDialog.setCancelable(!entity.force); |
171 |
| - mProgressDialog.setProgressNumberFormat(""); |
172 |
| - mProgressDialog.setCanceledOnTouchOutside(!entity.force); |
173 |
| - |
174 |
| - //暂停下载-慎用;建议使用 Disposable.dispose(); |
175 |
| -// mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "暂停", new DialogInterface.OnClickListener() { |
176 |
| -// @Override |
177 |
| -// public void onClick(DialogInterface dialog, int which) { |
178 |
| -// if (mDownloadObserver != null) { |
179 |
| -// mDownloadObserver.pause(); |
180 |
| -// } |
181 |
| -// } |
182 |
| -// }); |
183 |
| - |
184 |
| - File mLocal = new File(FastFileUtil.getCacheDir(), fileName); |
185 |
| - Map<String, Object> header = null; |
186 |
| - long length = mLocal.length(); |
187 |
| - if (isRangeEnable) { |
188 |
| - header = new HashMap<>(1); |
189 |
| - header.put("range", "bytes=" + length + "-"); |
190 |
| - LoggerManager.i("downloadApk", "length:" + length); |
191 |
| - } |
192 |
| - //不同url不能使用相同的本地绝对路径不然很可能将B的后半部分下载追加到A的后面--最终也是错误的 |
193 |
| - ProgressDialog finalMProgressDialog = mProgressDialog; |
194 |
| - mDownloadObserver = new FastDownloadObserver(fileName, finalMProgressDialog, isRangeEnable) { |
195 |
| - @Override |
196 |
| - public void onSuccess(File file) { |
197 |
| - FastFileUtil.installApk(file); |
198 |
| - } |
199 |
| - |
200 |
| - @Override |
201 |
| - public void onFail(Throwable e) { |
202 |
| - LoggerManager.e("downloadApk", e.getMessage()); |
203 |
| - //HTTP 416 Range Not Satisfiable 出现该错误--很大可能性是文件已下载完成传递的 |
204 |
| - boolean satisfiable = e != null && e.getMessage().contains("416") && e.getMessage().toLowerCase().contains("range"); |
205 |
| - if (satisfiable) { |
206 |
| - onSuccess(mLocal); |
207 |
| - return; |
208 |
| - } |
209 |
| - boolean isPause = e != null && e.getMessage().equals(FastDownloadObserver.DOWNLOAD_PAUSE); |
210 |
| - if (isPause) { |
211 |
| - ToastUtil.show("暂停下载"); |
212 |
| - return; |
213 |
| - } |
214 |
| - ToastUtil.show("下载失败:" + e.getMessage()); |
215 |
| - } |
216 |
| - |
217 |
| - @Override |
218 |
| - public void onProgress(float progress, long current, long total) { |
219 |
| - LoggerManager.i("downloadApk", "current:" + current + ";total:" + total); |
220 |
| - if (!finalMProgressDialog.isShowing()) { |
221 |
| - return; |
222 |
| - } |
223 |
| - finalMProgressDialog.setProgressNumberFormat(FastFormatUtil.formatDataSize(current) + "/" + FastFormatUtil.formatDataSize(total)); |
224 |
| - finalMProgressDialog.setMax((int) total); |
225 |
| - finalMProgressDialog.setProgress((int) current); |
226 |
| - } |
227 |
| - }; |
228 |
| - FastRetrofit.getInstance().downloadFile(entity.url, header) |
229 |
| - .compose(((RxAppCompatActivity) activity).bindUntilEvent(ActivityEvent.DESTROY)) |
230 |
| - //可自定义保存路径默认//storage/emulated/0/Android/data/<package-name>/cache/xxx/ |
231 |
| - .subscribe(mDownloadObserver); |
| 157 | + DownloadImpl.getInstance() |
| 158 | + .with(mContext) |
| 159 | + .url(entity.url) |
| 160 | + .quickProgress() |
| 161 | + .setEnableIndicator(true) |
| 162 | + .autoOpenIgnoreMD5() |
| 163 | + .setRetry(5) |
| 164 | + .setBlockMaxTime(100000L) |
| 165 | + .enqueue(); |
232 | 166 | }
|
233 | 167 |
|
234 | 168 | }
|
0 commit comments