Skip to content

Commit

Permalink
添加后台任务停止功能
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Mar 22, 2018
1 parent 42c972b commit 077c30c
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ public void call(Boolean aBoolean) {
@OnClick(R.id.start_daemon_update)
void onDaemonStartClick() {
daemonTask = createBuilder();
daemonTask.setUpdateParser(new UpdateParser() {
@Override
public Update parse(String response) throws Exception {
// 设置个无效错误的解析器,使得更新任务失败,以触发后台任务重启逻辑
return null;
}
});
daemonTask.checkWithDaemon(5);// 后台更新时间间隔设置为5秒。
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
import org.lzh.framework.updatepluginlib.base.FileCreator;
import org.lzh.framework.updatepluginlib.base.InstallNotifier;
import org.lzh.framework.updatepluginlib.base.InstallStrategy;
import org.lzh.framework.updatepluginlib.base.RestartHandler;
import org.lzh.framework.updatepluginlib.base.UpdateChecker;
import org.lzh.framework.updatepluginlib.base.UpdateParser;
import org.lzh.framework.updatepluginlib.base.UpdateStrategy;
import org.lzh.framework.updatepluginlib.flow.CallbackDelegate;
import org.lzh.framework.updatepluginlib.flow.Launcher;
import org.lzh.framework.updatepluginlib.flow.RetryCallback;
import org.lzh.framework.updatepluginlib.impl.DefaultRestartHandler;
import org.lzh.framework.updatepluginlib.model.CheckEntity;

/**
Expand Down Expand Up @@ -65,8 +66,8 @@ public class UpdateBuilder {
private FileChecker fileChecker;
private InstallStrategy installStrategy;
private UpdateConfig config;
private RestartHandler restartHandler;

private RetryCallback retryCallback;
private CallbackDelegate callbackDelegate;

private UpdateBuilder(UpdateConfig config) {
Expand Down Expand Up @@ -101,13 +102,13 @@ public void check() {
}

/**
* 启动后台更新任务。特性:当检查更新失败或者当前无更新时。等待指定时间之后,自动重启更新任务。
* 启动后台更新任务。特性:当检查更新失败、当前无更新或者用户取消当前更新时。等待指定时间之后,自动重启更新任务。
* @param retryTime 重启时间间隔,单位为秒
*/
public void checkWithDaemon(long retryTime) {
RetryCallback retryCallback = getRetryCallback();
retryCallback.setRetryTime(retryTime);
this.callbackDelegate.setRetryCallback(retryCallback);
RestartHandler handler = getRestartHandler();
handler.attach(this, retryTime);
this.callbackDelegate.setRestartHandler(handler);
isDaemon = true;
Launcher.getInstance().launchCheck(this);
}
Expand Down Expand Up @@ -195,6 +196,10 @@ public UpdateBuilder setInstallStrategy(InstallStrategy installStrategy) {
return this;
}

public UpdateBuilder setRestartHandler(RestartHandler restartHandler) {
this.restartHandler = restartHandler;
return this;
}

public UpdateStrategy getUpdateStrategy() {
if (updateStrategy == null) {
Expand Down Expand Up @@ -285,6 +290,13 @@ public InstallStrategy getInstallStrategy() {
return installStrategy;
}

public RestartHandler getRestartHandler() {
if (restartHandler == null) {
restartHandler = new DefaultRestartHandler();
}
return restartHandler;
}

public final UpdateConfig getConfig() {
return config;
}
Expand All @@ -303,19 +315,9 @@ public boolean isDaemon() {
* <p>请注意此方法并不会让当前的更新任务停止,而是停止更新失败后的自动重启功能。</p>
*/
public void stopDaemon() {
if (isDaemon && retryCallback != null) {
retryCallback.detach();
retryCallback = null;
if (isDaemon) {
restartHandler.detach();
}
}

RetryCallback getRetryCallback() {
if (retryCallback == null) {
retryCallback = new RetryCallback(this);
}
return retryCallback;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.lzh.framework.updatepluginlib.base;

import org.lzh.framework.updatepluginlib.UpdateBuilder;
import org.lzh.framework.updatepluginlib.impl.DefaultRestartHandler;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.util.L;
import org.lzh.framework.updatepluginlib.util.Utils;

import java.io.File;

/**
* 后台重启任务定制接口。此接口用于定制后台任务的重启逻辑,可参考{@link DefaultRestartHandler}
* @author haoge on 2018/3/22.
*/
public abstract class RestartHandler implements CheckCallback, DownloadCallback{

protected UpdateBuilder builder;
protected long retryTime;// 重启间隔
private RetryTask task;

public final void attach(UpdateBuilder builder, long retryTime) {
this.builder = builder;
this.retryTime = Math.max(1, retryTime);
}

public final void detach() {
builder = null;
}

protected final void retry() {
if (builder == null) {
return;
}
if (task == null) {
task = new RetryTask();
}
Utils.getMainHandler().removeCallbacks(task);
Utils.getMainHandler().postDelayed(task, retryTime * 1000);
}

private class RetryTask implements Runnable {

@Override
public void run() {
if (builder != null) {
L.d("Restart update for daemon");
builder.checkWithDaemon(retryTime);
}
}
}

// =======所有生命周期回调均为空实现,由子类复写相应的生命周期函数进行重启操作。====
@Override
public void onDownloadStart() {}

@Override
public void onCheckStart() {}

@Override
public void onDownloadComplete(File file) {}

@Override
public void hasUpdate(Update update) {}

@Override
public void onDownloadProgress(long current, long total) {}

@Override
public void noUpdate() {}

@Override
public void onDownloadError(Throwable t) {}

@Override
public void onCheckError(Throwable t) {}

@Override
public void onUserCancel() {}

@Override
public void onCheckIgnore(Update update) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.lzh.framework.updatepluginlib.base.CheckCallback;
import org.lzh.framework.updatepluginlib.base.DownloadCallback;
import org.lzh.framework.updatepluginlib.base.RestartHandler;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.util.L;

Expand All @@ -16,7 +17,7 @@ public final class CallbackDelegate implements CheckCallback, DownloadCallback {

private CheckCallback checkProxy;
private DownloadCallback downloadProxy;
private RetryCallback retryCallback;
private RestartHandler restartHandler;

public void setCheckDelegate(CheckCallback checkProxy) {
this.checkProxy = checkProxy;
Expand All @@ -33,8 +34,8 @@ public void onDownloadStart() {
downloadProxy.onDownloadStart();
}

if (retryCallback != null) {
retryCallback.onDownloadStart();
if (restartHandler != null) {
restartHandler.onDownloadStart();
}
}

Expand All @@ -45,8 +46,8 @@ public void onDownloadComplete(File file) {
downloadProxy.onDownloadComplete(file);
}

if (retryCallback != null) {
retryCallback.onDownloadComplete(file);
if (restartHandler != null) {
restartHandler.onDownloadComplete(file);
}
}

Expand All @@ -57,8 +58,8 @@ public void onDownloadProgress(long current, long total) {
downloadProxy.onDownloadProgress(current, total);
}

if (retryCallback != null) {
retryCallback.onDownloadProgress(current, total);
if (restartHandler != null) {
restartHandler.onDownloadProgress(current, total);
}
}

Expand All @@ -69,8 +70,8 @@ public void onDownloadError(Throwable t) {
downloadProxy.onDownloadError(t);
}

if (retryCallback != null) {
retryCallback.onDownloadError(t);
if (restartHandler != null) {
restartHandler.onDownloadError(t);
}
}
@Override
Expand All @@ -80,8 +81,8 @@ public void onCheckStart() {
checkProxy.onCheckStart();
}

if (retryCallback != null) {
retryCallback.onCheckStart();
if (restartHandler != null) {
restartHandler.onCheckStart();
}
}

Expand All @@ -92,8 +93,8 @@ public void hasUpdate(Update update) {
checkProxy.hasUpdate(update);
}

if (retryCallback != null) {
retryCallback.hasUpdate(update);
if (restartHandler != null) {
restartHandler.hasUpdate(update);
}
}

Expand All @@ -104,8 +105,8 @@ public void noUpdate() {
checkProxy.noUpdate();
}

if (retryCallback != null) {
retryCallback.noUpdate();
if (restartHandler != null) {
restartHandler.noUpdate();
}
}

Expand All @@ -116,8 +117,8 @@ public void onCheckError(Throwable t) {
checkProxy.onCheckError(t);
}

if (retryCallback != null) {
retryCallback.onCheckError(t);
if (restartHandler != null) {
restartHandler.onCheckError(t);
}
}

Expand All @@ -128,8 +129,8 @@ public void onUserCancel() {
checkProxy.onUserCancel();
}

if (retryCallback != null) {
retryCallback.onUserCancel();
if (restartHandler != null) {
restartHandler.onUserCancel();
}
}

Expand All @@ -140,12 +141,12 @@ public void onCheckIgnore(Update update) {
checkProxy.onCheckIgnore(update);
}

if (retryCallback != null) {
retryCallback.onCheckIgnore(update);
if (restartHandler != null) {
restartHandler.onCheckIgnore(update);
}
}

public void setRetryCallback(RetryCallback retryCallback) {
this.retryCallback = retryCallback;
public void setRestartHandler(RestartHandler restartHandler) {
this.restartHandler = restartHandler;
}
}
Loading

0 comments on commit 077c30c

Please sign in to comment.