Skip to content

Commit

Permalink
添加默认任务的断点下载
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed May 13, 2016
1 parent 80f7638 commit a7fd537
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ checkWorker(new UpdateWorker() {
})
```

- 自定义文件下载接口的访问任务。默认参考:[DefaultDownloadWorker](https://github.com/yjfnypeu/UpdatePlugin/blob/master/updatepluginlib/src/main/java/org/lzh/framework/updatepluginlib/business/DefaultDownloadWorker.java)

```
downloadWorker(new DownloadWorker() {
@Override
protected void download(String url, File file) throws Exception {
// TODO: 2016/5/11 此处运行于子线程,在此进行文件下载任务
}
})
```

- 自定义下载文件缓存,默认参考:[DefaultFileCreator](https://github.com/yjfnypeu/UpdatePlugin/blob/master/updatepluginlib/src/main/java/org/lzh/framework/updatepluginlib/creator/DefaultFileCreator.java)

```
Expand Down
2 changes: 0 additions & 2 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Maven Android API 23 Platform" jdkType="Android SDK" />
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile project(':updatepluginlib')
// compile 'com.github.yjfnypeu:UpdatePlugin:0.1'
// compile 'com.github.yjfnypeu:UpdatePlugin:0.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.lzh.framework.updatepluginlib.UpdateConfig;
import org.lzh.framework.updatepluginlib.callback.EmptyCheckCB;
import org.lzh.framework.updatepluginlib.callback.EmptyDownloadCB;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.model.UpdateParser;

Expand Down Expand Up @@ -63,29 +64,14 @@ public void noUpdate() {
Toast.makeText(MyApplication.this, "无更新", Toast.LENGTH_SHORT).show();
}
})
/* // apk下载的回调
.downloadCB(new UpdateDownloadCB() {
// apk下载的回调
.downloadCB(new EmptyDownloadCB(){
@Override
public void onUpdateStart() {
// TODO: 2016/5/11 下载开始
}
@Override
public void onUpdateComplete(File file) {
// TODO: 2016/5/11 下载完成
}
@Override
public void onUpdateProgress(long l, long l1) {
// TODO: 2016/5/11 下载进度
}
@Override
public void onUpdateError(int i, String s) {
// TODO: 2016/5/11 下载apk错误
public void onUpdateError(int code, String errorMsg) {
Toast.makeText(MyApplication.this, "下载失败:code:" + code + ",errorMsg:" + errorMsg, Toast.LENGTH_SHORT).show();
}
})
// 自定义更新接口的访问任务
/* // 自定义更新接口的访问任务
.checkWorker(new UpdateWorker() {
@Override
protected String check(String url) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.lzh.framework.updatepluginlib.business;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import org.lzh.framework.updatepluginlib.UpdateConfig;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -11,11 +17,11 @@
* @author Administrator
*/
public class DefaultDownloadWorker extends DownloadWorker {
public static final String KEY_DOWN_SIZE = "update_download_size";
HttpURLConnection urlConn;
private URL httpUrl;
@Override
protected void download(String url, File target) throws Exception{
httpUrl = new URL(url);
URL httpUrl = new URL(url);
urlConn = (HttpURLConnection) httpUrl.openConnection();
setDefaultProperties();
urlConn.connect();
Expand All @@ -24,10 +30,15 @@ protected void download(String url, File target) throws Exception{
if (responseCode < 200 || responseCode >= 300) {
throw new HttpException(responseCode,urlConn.getResponseMessage());
}
target.delete();
RandomAccessFile raf = new RandomAccessFile(target,"rw");

long contentLength = urlConn.getContentLength();
if (checkIsDownAll(target,url,contentLength)) {
urlConn.disconnect();
urlConn = null;
return;
}
RandomAccessFile raf = supportBreadpointDownload(target, httpUrl, url);

long offset = target.exists() ? (int) target.length() : 0;
InputStream inputStream = urlConn.getInputStream();
byte[] buffer = new byte[8 * 1024];
Expand All @@ -37,6 +48,7 @@ protected void download(String url, File target) throws Exception{
while ((length = inputStream.read(buffer)) != -1) {
raf.write(buffer, 0, length);
offset += length;
saveDownloadSize(url,offset);
long end = System.currentTimeMillis();
if (end - start > 1000) {
sendUpdateProgress(offset,contentLength);
Expand All @@ -45,12 +57,86 @@ protected void download(String url, File target) throws Exception{

urlConn.disconnect();
raf.close();
urlConn = null;
}

private boolean checkIsDownAll(File target,String url,long contentLength) {
long lastDownSize = getLastDownloadSize(url);
long length = target.length();
long lastTotalSize = getLastDownloadTotalSize(url);
if (lastDownSize == length
&& lastTotalSize == lastTotalSize
&& lastDownSize != 0
&& lastDownSize == contentLength)
return true;
return false;
}

private RandomAccessFile supportBreadpointDownload(File target, URL httpUrl,String url) throws IOException {

String range = urlConn.getHeaderField("Accept-Ranges");
if (TextUtils.isEmpty(range) || !range.startsWith("bytes")) {
target.delete();
return new RandomAccessFile(target,"rw");
}

long lastDownSize = getLastDownloadSize(url);
long length = target.length();
long lastTotalSize = getLastDownloadTotalSize(url);
long contentLength = Long.parseLong(urlConn.getHeaderField("Content-Length"));
saveDownloadTotalSize(url,contentLength);
if (lastTotalSize != contentLength
|| lastDownSize != length
|| lastDownSize > contentLength) {
target.delete();
return new RandomAccessFile(target,"rw");
}
urlConn.disconnect();
urlConn = (HttpURLConnection) httpUrl.openConnection();

urlConn.setRequestProperty("RANGE", "bytes=" + length + "-" + contentLength);
setDefaultProperties();
urlConn.connect();

int responseCode = urlConn.getResponseCode();
if (responseCode < 200 || responseCode >= 300) {
throw new HttpException(responseCode,urlConn.getResponseMessage());
}
RandomAccessFile raf = new RandomAccessFile(target,"rw");
raf.seek(length);

return raf;
}

private void setDefaultProperties() throws IOException {
urlConn.setRequestProperty("Content-Type","text/html; charset=UTF-8");
urlConn.setRequestMethod("GET");
urlConn.setConnectTimeout(10000);
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
}

private long getLastDownloadSize(String url) {
SharedPreferences sp = UpdateConfig.getConfig().getContext().getSharedPreferences(KEY_DOWN_SIZE, Context.MODE_PRIVATE);
return sp.getLong(url,0);
}

private long getLastDownloadTotalSize (String url) {
SharedPreferences sp = UpdateConfig.getConfig().getContext().getSharedPreferences(KEY_DOWN_SIZE, Context.MODE_PRIVATE);
return sp.getLong(url + "_total_size",0);
}

private void saveDownloadSize (String url,long size) {
SharedPreferences sp = UpdateConfig.getConfig().getContext().getSharedPreferences(KEY_DOWN_SIZE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putLong(url,size);
editor.apply();
}

private void saveDownloadTotalSize(String url,long totalSize) {
SharedPreferences sp = UpdateConfig.getConfig().getContext().getSharedPreferences(KEY_DOWN_SIZE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putLong(url + "_total_size",totalSize);
editor.apply();
}
}
5 changes: 0 additions & 5 deletions updatepluginlib/updatepluginlib.iml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
Expand All @@ -85,11 +84,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/publications" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Maven Android API 23 Platform" jdkType="Android SDK" />
Expand Down

0 comments on commit a7fd537

Please sign in to comment.