Skip to content

Commit 5244b06

Browse files
committed
添加时间单位和延时
1 parent 18030da commit 5244b06

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

AndroidExecLibrary/src/main/java/com/excellence/exec/Command.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Command {
2424

2525
private final LinkedList<CommandTask> mTaskQueue;
2626
private int mParallelTaskCount = 0;
27-
private int mTimeOut = 0;
27+
private long mTimeOut = 0;
2828
private Executor mResponsePoster = null;
2929

3030
protected Command(CommanderOptions options) {
@@ -47,7 +47,7 @@ public void execute(@NonNull Runnable command) {
4747
};
4848
}
4949

50-
protected int getTimeOut() {
50+
protected long getTimeOut() {
5151
return mTimeOut;
5252
}
5353

AndroidExecLibrary/src/main/java/com/excellence/exec/CommandTask.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,39 @@ public class CommandTask {
3535
private Command mManager = null;
3636
private Executor mResponsePoster = null;
3737
private List<String> mCommand = null;
38-
private int mTimeOut = 0;
38+
private TimeUnit mTimeUnit = null;
39+
private long mTimeOut = 0;
40+
private long mTimeDelay = 0;
3941

4042
private Process mProcess = null;
4143
private int mStatus = STATUS_WAITING;
4244
private CommandTaskListener mIListener = null;
4345
private Disposable mTimer = null;
4446
private String mCmd = null;
4547

46-
private CommandTask(List<String> command, int timeOut) {
48+
private CommandTask(Builder builder) {
4749
mManager = Commander.getCommand();
48-
mCommand = command;
49-
mTimeOut = timeOut;
50+
51+
mCommand = builder.mCommand;
52+
mTimeUnit = builder.mTimeUnit;
53+
mTimeOut = builder.mTimeOut;
5054
if (mTimeOut <= 0) {
5155
mTimeOut = mManager.getTimeOut();
5256
}
57+
mTimeDelay = builder.mTimeDelay;
58+
if (mTimeDelay < 0) {
59+
mTimeDelay = 0;
60+
}
5361
mResponsePoster = mManager.getResponsePoster();
5462
mIListener = new CommandTaskListener();
5563
}
5664

5765
public static class Builder {
5866

5967
private List<String> mCommand = null;
60-
private int mTimeOut = 0;
68+
private TimeUnit mTimeUnit = TimeUnit.MILLISECONDS;
69+
private long mTimeOut = 0;
70+
private long mTimeDelay = 0;
6171

6272
/**
6373
* 任务命令
@@ -71,18 +81,40 @@ public Builder command(List<String> command) {
7181
}
7282

7383
/**
74-
* 单独设置任务超时时间:ms
84+
* 延时、超时的时间单位,默认:ms {@link TimeUnit#MILLISECONDS}
85+
*
86+
* @param timeUnit
87+
* @return
88+
*/
89+
public Builder timeUnit(TimeUnit timeUnit) {
90+
mTimeUnit = timeUnit;
91+
return this;
92+
}
93+
94+
/**
95+
* 单独设置任务超时时间,默认:10 * 1000ms {@link Command#DEFAULT_TIME_OUT}
7596
*
7697
* @param timeOut
7798
* @return
7899
*/
79-
public Builder timeOut(int timeOut) {
100+
public Builder timeOut(long timeOut) {
80101
mTimeOut = timeOut;
81102
return this;
82103
}
83104

105+
/**
106+
* 设置任务延时启动,默认:0ms
107+
*
108+
* @param timeDelay
109+
* @return
110+
*/
111+
public Builder timeDelay(long timeDelay) {
112+
mTimeDelay = timeDelay;
113+
return this;
114+
}
115+
84116
public CommandTask build() {
85-
return new CommandTask(mCommand, mTimeOut);
117+
return new CommandTask(this);
86118
}
87119
}
88120

@@ -164,7 +196,7 @@ private void resetTimer() {
164196

165197
private void restartTimer() {
166198
resetTimer();
167-
mTimer = Observable.timer(mTimeOut, TimeUnit.MILLISECONDS).subscribe(new Consumer<Long>() {
199+
mTimer = Observable.timer(mTimeOut, mTimeUnit).subscribe(new Consumer<Long>() {
168200
@Override
169201
public void accept(Long aLong) throws Exception {
170202
mIListener.onError(new Throwable("Time out : " + mCmd));

AndroidExecLibrary/src/main/java/com/excellence/exec/CommanderOptions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class CommanderOptions {
1212

1313
protected int mParallelTaskCount = 0;
14-
protected int mTimeOut = 0;
14+
protected long mTimeOut = 0;
1515

1616
private CommanderOptions(Builder builder) {
1717
mParallelTaskCount = builder.mParallelTaskCount;
@@ -21,7 +21,7 @@ private CommanderOptions(Builder builder) {
2121
public static class Builder {
2222

2323
private int mParallelTaskCount = 0;
24-
private int mTimeOut = 0;
24+
private long mTimeOut = 0;
2525

2626
/**
2727
*
@@ -36,7 +36,7 @@ public Builder setParallelTaskCount(int parallelTaskCount) {
3636
*
3737
* @param timeOut ms:执行命令超时时间(存在某些命令会需要人为输入确认,此时命令会一直卡住等待),默认10s超时,终止指令
3838
*/
39-
public Builder setTimeOut(int timeOut) {
39+
public Builder setTimeOut(long timeOut) {
4040
mTimeOut = timeOut;
4141
return this;
4242
}

0 commit comments

Comments
 (0)