Skip to content

Commit 077c30c

Browse files
committed
添加后台任务停止功能
1 parent 42c972b commit 077c30c

File tree

6 files changed

+170
-145
lines changed

6 files changed

+170
-145
lines changed

app/src/main/java/org/lzh/framework/updateplugin/SampleActivity.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ public void call(Boolean aBoolean) {
6363
@OnClick(R.id.start_daemon_update)
6464
void onDaemonStartClick() {
6565
daemonTask = createBuilder();
66-
daemonTask.setUpdateParser(new UpdateParser() {
67-
@Override
68-
public Update parse(String response) throws Exception {
69-
// 设置个无效错误的解析器,使得更新任务失败,以触发后台任务重启逻辑
70-
return null;
71-
}
72-
});
7366
daemonTask.checkWithDaemon(5);// 后台更新时间间隔设置为5秒。
7467
}
7568

updatepluginlib/src/main/java/org/lzh/framework/updatepluginlib/UpdateBuilder.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
import org.lzh.framework.updatepluginlib.base.FileCreator;
2626
import org.lzh.framework.updatepluginlib.base.InstallNotifier;
2727
import org.lzh.framework.updatepluginlib.base.InstallStrategy;
28+
import org.lzh.framework.updatepluginlib.base.RestartHandler;
2829
import org.lzh.framework.updatepluginlib.base.UpdateChecker;
2930
import org.lzh.framework.updatepluginlib.base.UpdateParser;
3031
import org.lzh.framework.updatepluginlib.base.UpdateStrategy;
3132
import org.lzh.framework.updatepluginlib.flow.CallbackDelegate;
3233
import org.lzh.framework.updatepluginlib.flow.Launcher;
33-
import org.lzh.framework.updatepluginlib.flow.RetryCallback;
34+
import org.lzh.framework.updatepluginlib.impl.DefaultRestartHandler;
3435
import org.lzh.framework.updatepluginlib.model.CheckEntity;
3536

3637
/**
@@ -65,8 +66,8 @@ public class UpdateBuilder {
6566
private FileChecker fileChecker;
6667
private InstallStrategy installStrategy;
6768
private UpdateConfig config;
69+
private RestartHandler restartHandler;
6870

69-
private RetryCallback retryCallback;
7071
private CallbackDelegate callbackDelegate;
7172

7273
private UpdateBuilder(UpdateConfig config) {
@@ -101,13 +102,13 @@ public void check() {
101102
}
102103

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

199+
public UpdateBuilder setRestartHandler(RestartHandler restartHandler) {
200+
this.restartHandler = restartHandler;
201+
return this;
202+
}
198203

199204
public UpdateStrategy getUpdateStrategy() {
200205
if (updateStrategy == null) {
@@ -285,6 +290,13 @@ public InstallStrategy getInstallStrategy() {
285290
return installStrategy;
286291
}
287292

293+
public RestartHandler getRestartHandler() {
294+
if (restartHandler == null) {
295+
restartHandler = new DefaultRestartHandler();
296+
}
297+
return restartHandler;
298+
}
299+
288300
public final UpdateConfig getConfig() {
289301
return config;
290302
}
@@ -303,19 +315,9 @@ public boolean isDaemon() {
303315
* <p>请注意此方法并不会让当前的更新任务停止,而是停止更新失败后的自动重启功能。</p>
304316
*/
305317
public void stopDaemon() {
306-
if (isDaemon && retryCallback != null) {
307-
retryCallback.detach();
308-
retryCallback = null;
318+
if (isDaemon) {
319+
restartHandler.detach();
309320
}
310321
}
311322

312-
RetryCallback getRetryCallback() {
313-
if (retryCallback == null) {
314-
retryCallback = new RetryCallback(this);
315-
}
316-
return retryCallback;
317-
}
318-
319-
320-
321323
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.lzh.framework.updatepluginlib.base;
2+
3+
import org.lzh.framework.updatepluginlib.UpdateBuilder;
4+
import org.lzh.framework.updatepluginlib.impl.DefaultRestartHandler;
5+
import org.lzh.framework.updatepluginlib.model.Update;
6+
import org.lzh.framework.updatepluginlib.util.L;
7+
import org.lzh.framework.updatepluginlib.util.Utils;
8+
9+
import java.io.File;
10+
11+
/**
12+
* 后台重启任务定制接口。此接口用于定制后台任务的重启逻辑,可参考{@link DefaultRestartHandler}
13+
* @author haoge on 2018/3/22.
14+
*/
15+
public abstract class RestartHandler implements CheckCallback, DownloadCallback{
16+
17+
protected UpdateBuilder builder;
18+
protected long retryTime;// 重启间隔
19+
private RetryTask task;
20+
21+
public final void attach(UpdateBuilder builder, long retryTime) {
22+
this.builder = builder;
23+
this.retryTime = Math.max(1, retryTime);
24+
}
25+
26+
public final void detach() {
27+
builder = null;
28+
}
29+
30+
protected final void retry() {
31+
if (builder == null) {
32+
return;
33+
}
34+
if (task == null) {
35+
task = new RetryTask();
36+
}
37+
Utils.getMainHandler().removeCallbacks(task);
38+
Utils.getMainHandler().postDelayed(task, retryTime * 1000);
39+
}
40+
41+
private class RetryTask implements Runnable {
42+
43+
@Override
44+
public void run() {
45+
if (builder != null) {
46+
L.d("Restart update for daemon");
47+
builder.checkWithDaemon(retryTime);
48+
}
49+
}
50+
}
51+
52+
// =======所有生命周期回调均为空实现,由子类复写相应的生命周期函数进行重启操作。====
53+
@Override
54+
public void onDownloadStart() {}
55+
56+
@Override
57+
public void onCheckStart() {}
58+
59+
@Override
60+
public void onDownloadComplete(File file) {}
61+
62+
@Override
63+
public void hasUpdate(Update update) {}
64+
65+
@Override
66+
public void onDownloadProgress(long current, long total) {}
67+
68+
@Override
69+
public void noUpdate() {}
70+
71+
@Override
72+
public void onDownloadError(Throwable t) {}
73+
74+
@Override
75+
public void onCheckError(Throwable t) {}
76+
77+
@Override
78+
public void onUserCancel() {}
79+
80+
@Override
81+
public void onCheckIgnore(Update update) {}
82+
}

updatepluginlib/src/main/java/org/lzh/framework/updatepluginlib/flow/CallbackDelegate.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.lzh.framework.updatepluginlib.base.CheckCallback;
44
import org.lzh.framework.updatepluginlib.base.DownloadCallback;
5+
import org.lzh.framework.updatepluginlib.base.RestartHandler;
56
import org.lzh.framework.updatepluginlib.model.Update;
67
import org.lzh.framework.updatepluginlib.util.L;
78

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

1718
private CheckCallback checkProxy;
1819
private DownloadCallback downloadProxy;
19-
private RetryCallback retryCallback;
20+
private RestartHandler restartHandler;
2021

2122
public void setCheckDelegate(CheckCallback checkProxy) {
2223
this.checkProxy = checkProxy;
@@ -33,8 +34,8 @@ public void onDownloadStart() {
3334
downloadProxy.onDownloadStart();
3435
}
3536

36-
if (retryCallback != null) {
37-
retryCallback.onDownloadStart();
37+
if (restartHandler != null) {
38+
restartHandler.onDownloadStart();
3839
}
3940
}
4041

@@ -45,8 +46,8 @@ public void onDownloadComplete(File file) {
4546
downloadProxy.onDownloadComplete(file);
4647
}
4748

48-
if (retryCallback != null) {
49-
retryCallback.onDownloadComplete(file);
49+
if (restartHandler != null) {
50+
restartHandler.onDownloadComplete(file);
5051
}
5152
}
5253

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

60-
if (retryCallback != null) {
61-
retryCallback.onDownloadProgress(current, total);
61+
if (restartHandler != null) {
62+
restartHandler.onDownloadProgress(current, total);
6263
}
6364
}
6465

@@ -69,8 +70,8 @@ public void onDownloadError(Throwable t) {
6970
downloadProxy.onDownloadError(t);
7071
}
7172

72-
if (retryCallback != null) {
73-
retryCallback.onDownloadError(t);
73+
if (restartHandler != null) {
74+
restartHandler.onDownloadError(t);
7475
}
7576
}
7677
@Override
@@ -80,8 +81,8 @@ public void onCheckStart() {
8081
checkProxy.onCheckStart();
8182
}
8283

83-
if (retryCallback != null) {
84-
retryCallback.onCheckStart();
84+
if (restartHandler != null) {
85+
restartHandler.onCheckStart();
8586
}
8687
}
8788

@@ -92,8 +93,8 @@ public void hasUpdate(Update update) {
9293
checkProxy.hasUpdate(update);
9394
}
9495

95-
if (retryCallback != null) {
96-
retryCallback.hasUpdate(update);
96+
if (restartHandler != null) {
97+
restartHandler.hasUpdate(update);
9798
}
9899
}
99100

@@ -104,8 +105,8 @@ public void noUpdate() {
104105
checkProxy.noUpdate();
105106
}
106107

107-
if (retryCallback != null) {
108-
retryCallback.noUpdate();
108+
if (restartHandler != null) {
109+
restartHandler.noUpdate();
109110
}
110111
}
111112

@@ -116,8 +117,8 @@ public void onCheckError(Throwable t) {
116117
checkProxy.onCheckError(t);
117118
}
118119

119-
if (retryCallback != null) {
120-
retryCallback.onCheckError(t);
120+
if (restartHandler != null) {
121+
restartHandler.onCheckError(t);
121122
}
122123
}
123124

@@ -128,8 +129,8 @@ public void onUserCancel() {
128129
checkProxy.onUserCancel();
129130
}
130131

131-
if (retryCallback != null) {
132-
retryCallback.onUserCancel();
132+
if (restartHandler != null) {
133+
restartHandler.onUserCancel();
133134
}
134135
}
135136

@@ -140,12 +141,12 @@ public void onCheckIgnore(Update update) {
140141
checkProxy.onCheckIgnore(update);
141142
}
142143

143-
if (retryCallback != null) {
144-
retryCallback.onCheckIgnore(update);
144+
if (restartHandler != null) {
145+
restartHandler.onCheckIgnore(update);
145146
}
146147
}
147148

148-
public void setRetryCallback(RetryCallback retryCallback) {
149-
this.retryCallback = retryCallback;
149+
public void setRestartHandler(RestartHandler restartHandler) {
150+
this.restartHandler = restartHandler;
150151
}
151152
}

0 commit comments

Comments
 (0)