Skip to content

Commit

Permalink
添加支持下载任务使用异步请求方式
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Nov 13, 2017
1 parent 09869cb commit d614b7a
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 62 deletions.
15 changes: 8 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
lintOptions {
abortOnError false
}
Expand All @@ -21,15 +22,15 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
implementation project(':updatepluginlib')
// implementation 'com.github.yjfnypeu:UpdatePlugin:-2.5.0-g3523297'
implementation 'com.android.support:appcompat-v7:25.1.0'
implementation 'com.jakewharton:butterknife:8.5.1'
compile project(':updatepluginlib')
// implementation 'com.github.yjfnypeu:UpdatePlugin:2.5.2'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
implementation 'com.squareup.okhttp3:okhttp:3.6.0'
implementation 'com.tbruyelle.rxpermissions:rxpermissions:0.9.2'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.tbruyelle.rxpermissions:rxpermissions:0.9.2'

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

import com.tbruyelle.rxpermissions.RxPermissions;

Expand Down Expand Up @@ -41,7 +40,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);

requestStoragePermission();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ protected String check(CheckEntity entity) throws Exception {
return call.execute().body().string();
}

@Override
protected boolean useAsync() {
return false;
}

static {
sOkClient = new OkHttpClient();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ protected void download(String url, File target) throws Exception {
long end = System.currentTimeMillis();
if (end - start > 1000) {
// 发送下载进度信息。便于用于更新界面等操作
sendUpdateProgress(offset,contentLength);
sendDownloadProgress(offset,contentLength);
}
}

// 下载完成。通知下载完成
sendDownloadComplete(target);
}

static {
Expand Down
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.novoda:bintray-release:0.3.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -15,7 +14,6 @@ buildscript {

allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
2 changes: 1 addition & 1 deletion updatepluginlib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'maven'
android {
compileSdkVersion 26

buildToolsVersion "25.0.0"
lintOptions {
abortOnError false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public void downUpdate(Update update,UpdateBuilder builder) {
}

downloadWorker.setUpdate(update);
downloadWorker.setUpdateBuilder(builder);
downloadWorker.setDownloadCB(downloadCB);
downloadWorker.setCacheFileName(builder.getFileCreator().create(update.getVersionName()));

executor.download(downloadWorker);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,23 @@ protected void download(String url, File target) throws Exception{
byte[] buffer = new byte[8 * 1024];
int length;
long start = System.currentTimeMillis();
try {
while ((length = inputStream.read(buffer)) != -1) {
raf.write(buffer, 0, length);
offset += length;
long end = System.currentTimeMillis();
if (end - start > 1000) {
sendUpdateProgress(offset,contentLength);
start = System.currentTimeMillis();
}
while ((length = inputStream.read(buffer)) != -1) {
raf.write(buffer, 0, length);
offset += length;
long end = System.currentTimeMillis();
if (end - start > 1000) {
sendDownloadProgress(offset,contentLength);
start = System.currentTimeMillis();
}
} finally {
//
UpdatePreference.saveDownloadSize(url,offset);
}


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

// notify download completed
sendDownloadComplete(target);
}

private boolean checkIsDownAll(File target,String url,long contentLength) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package org.lzh.framework.updatepluginlib.business;

import org.lzh.framework.updatepluginlib.UpdateBuilder;
import org.lzh.framework.updatepluginlib.callback.DefaultDownloadCB;
import org.lzh.framework.updatepluginlib.callback.UpdateDownloadCB;
import org.lzh.framework.updatepluginlib.creator.ApkFileCreator;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.util.Recyclable;
import org.lzh.framework.updatepluginlib.util.Utils;
Expand All @@ -33,28 +33,20 @@
*/
public abstract class DownloadWorker extends UnifiedWorker implements Runnable,Recyclable {

/**
* 通过{@link Update#setUpdateUrl(String)}所设置的远程apk下载地址
*/
protected String url;
/**
* {@link DefaultDownloadCB}的实例。用于接收下载状态并进行后续流程通知
*/
private UpdateDownloadCB downloadCB;
/**
* 下载的缓存文件全路径。此路径名通过{@link ApkFileCreator#create(String)}进行获取
*/
private File cacheFileName;
// 将update实例提供给子类使用。

protected Update update;
protected UpdateBuilder builder;

public void setUpdate(Update update) {
this.update = update;
this.url = update.getUpdateUrl();
}

public void setCacheFileName(File cacheFileName) {
this.cacheFileName = cacheFileName;
public void setUpdateBuilder(UpdateBuilder builder) {
this.builder = builder;
}

public void setDownloadCB(UpdateDownloadCB downloadCB) {
Expand All @@ -64,20 +56,35 @@ public void setDownloadCB(UpdateDownloadCB downloadCB) {
@Override
public void run() {
try {
String url = update.getUpdateUrl();
File cacheFileName = this.builder.getFileCreator().create(update.getVersionName());
cacheFileName.getParentFile().mkdirs();
sendUpdateStart();
sendDownloadStart();
download(url,cacheFileName);
sendUpdateComplete(cacheFileName);
} catch (Throwable e) {
sendUpdateError(e);
} finally {
setRunning(false);
sendDownloadError(e);
}
}

/**
* 此为更新插件apk下载任务触发入口。
*
* <p>定制自己的网络层下载任务。需要复写此方法。并执行网络下载任务。
*
* <p>定制网络下载任务需要自己进行状态通知:
* <ol>
* <li>当使用的网络框架可以支持进度条通知时,调用{@link #sendDownloadProgress(long, long)}触发进度条消息通知</li>
* <li>当下载出现异常时。若使用的是同步请求。则无需理会,若使用的是异步请求。则需手动调用{@link #sendDownloadError(Throwable)}</li>
* <li>当下载任务执行完毕时:需手动调用{@link #sendDownloadComplete(File)}通知用户并启动下一步任务</li>
* </ol>
*
* @param url apk文件下载地址
* @param target 指定的下载文件地址
* @throws Exception 捕获出现的异常
*/
protected abstract void download(String url, File target) throws Exception;

private void sendUpdateStart() {
final void sendDownloadStart() {
if (downloadCB == null) return;

Utils.getMainHandler().post(new Runnable() {
Expand All @@ -89,7 +96,12 @@ public void run() {
});
}

protected void sendUpdateProgress(final long current, final long total) {
/**
* 通知当前下载进度,若实现类不调用此方法。将不会触发更新进度条的消息
* @param current 当前下载长度
* @param total 下载文件总长度
*/
public final void sendDownloadProgress(final long current, final long total) {
if (downloadCB == null) return;

Utils.getMainHandler().post(new Runnable() {
Expand All @@ -101,9 +113,13 @@ public void run() {
});
}

private void sendUpdateComplete(final File file) {
/**
* 通知当前下载任务执行完毕!当下载完成后。此回调必须被调用
* @param file 被下载的文件
*/
public final void sendDownloadComplete(final File file) {
setRunning(false);
if (downloadCB == null) return;

Utils.getMainHandler().post(new Runnable() {
@Override
public void run() {
Expand All @@ -114,7 +130,12 @@ public void run() {
});
}

private void sendUpdateError (final Throwable t) {
/**
* 通知当前下载任务出错。当下载出错时,此回调必须被调用
* @param t 错误异常信息
*/
public final void sendDownloadError(final Throwable t) {
setRunning(false);
if (downloadCB == null) return;

Utils.getMainHandler().post(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,24 @@ public static long getLastDownloadTotalSize (String url) {
public static void saveDownloadSize (String url,long size) {
SharedPreferences.Editor editor = getUpdatePref().edit();
editor.putLong(url,size);
editor.commit();
editor.apply();
}

public static void saveDownloadTotalSize(String url, long totalSize) {
SharedPreferences.Editor editor = getUpdatePref().edit();
editor.putLong(url + "_total_size",totalSize);
editor.commit();
editor.apply();
}

public static Set<String> getIgnoreVersions () {
Set<String> ignoreVersions = getUpdatePref().getStringSet("ignoreVersions", new HashSet<String>());
return ignoreVersions;
return getUpdatePref().getStringSet("ignoreVersions", new HashSet<String>());
}

public static void saveIgnoreVersion(int versionCode) {
Set<String> ignoreVersions = getIgnoreVersions();
if (!ignoreVersions.contains(String.valueOf(versionCode))) {
ignoreVersions.add(String.valueOf(versionCode));
getUpdatePref().edit().putStringSet("ignoreVersions",ignoreVersions).commit();
getUpdatePref().edit().putStringSet("ignoreVersions",ignoreVersions).apply();
}
}

Expand Down

0 comments on commit d614b7a

Please sign in to comment.