Skip to content

Commit e093573

Browse files
committed
添加命令执行状态
1 parent 9f6deba commit e093573

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private synchronized void schedule() {
5353
// count running task
5454
int runningTaskCount = 0;
5555
for (CommandTask task : mTaskQueue) {
56-
if (task.isRunning) {
56+
if (task.isRunning()) {
5757
runningTaskCount++;
5858
}
5959
}
@@ -87,9 +87,14 @@ public synchronized void clearAll() {
8787

8888
public class CommandTask {
8989

90+
private static final int STATUS_WAITING = 0;
91+
private static final int STATUS_FINISHED = 1;
92+
private static final int STATUS_RUNNING = 2;
93+
private static final int STATUS_INTERRUPT = 3;
94+
9095
private List<String> mCommand = null;
9196
private Process mProcess = null;
92-
private boolean isRunning = false;
97+
private int mStatus = STATUS_WAITING;
9398
private IListener mIListener = null;
9499

95100
private CommandTask(List<String> command, final IListener listener) {
@@ -111,7 +116,7 @@ public void onProgress(String message) {
111116

112117
@Override
113118
public void onError(Throwable t) {
114-
isRunning = false;
119+
mStatus = STATUS_FINISHED;
115120
if (listener != null) {
116121
listener.onError(t);
117122
}
@@ -120,7 +125,7 @@ public void onError(Throwable t) {
120125

121126
@Override
122127
public void onSuccess(String message) {
123-
isRunning = false;
128+
mStatus = STATUS_FINISHED;
124129
if (listener != null) {
125130
listener.onSuccess(message);
126131
}
@@ -131,10 +136,14 @@ public void onSuccess(String message) {
131136

132137
void deploy() {
133138
try {
134-
isRunning = true;
139+
mStatus = STATUS_RUNNING;
135140
Observable.create(new ObservableOnSubscribe<String>() {
136141
@Override
137142
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
143+
if (mStatus == STATUS_INTERRUPT) {
144+
return;
145+
}
146+
138147
StringBuilder cmd = new StringBuilder();
139148
for (String item : mCommand) {
140149
cmd.append(item).append(" ");
@@ -145,28 +154,36 @@ public void subscribe(ObservableEmitter<String> emitter) throws Exception {
145154
BufferedReader stdin = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
146155
StringBuilder result = new StringBuilder();
147156
String line = null;
148-
while ((line = stdin.readLine()) != null) {
157+
while ((line = stdin.readLine()) != null && mStatus != STATUS_INTERRUPT) {
149158
mIListener.onProgress(line);
150159
result.append(line);
151160
}
152161
stdin.close();
153162

154-
mIListener.onSuccess(result.toString());
163+
if (mStatus != STATUS_INTERRUPT) {
164+
mIListener.onSuccess(result.toString());
165+
}
155166
}
156-
}).observeOn(Schedulers.io()).subscribe();
167+
}).subscribeOn(Schedulers.io()).subscribe();
157168
} catch (Exception e) {
158169
mIListener.onError(e);
159170
}
160171
}
161172

162173
private void cancel() {
174+
mStatus = STATUS_INTERRUPT;
163175
if (mProcess != null) {
164176
mProcess.destroy();
165177
}
166178
mTaskQueue.remove(this);
167179
}
168180

181+
private boolean isRunning() {
182+
return mStatus == STATUS_RUNNING;
183+
}
184+
169185
public void discard() {
186+
mStatus = STATUS_INTERRUPT;
170187
if (mProcess != null) {
171188
mProcess.destroy();
172189
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public static CommandTask addUniqueTask() {
7171
return null;
7272
}
7373

74+
public static void destory() {
75+
checkCommander();
76+
mInstance.mCommand.clearAll();
77+
}
78+
7479
private static void checkCommander() {
7580
if (mInstance == null) {
7681
throw new RuntimeException("Commander not initialized!!!");

sample/src/main/java/com/excellence/exec/sample/MainActivity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.os.Bundle;
55
import android.util.Log;
66

7+
import com.excellence.exec.Command.CommandTask;
78
import com.excellence.exec.Commander;
89
import com.excellence.exec.IListener;
910

@@ -17,7 +18,7 @@ protected void onCreate(Bundle savedInstanceState) {
1718
setContentView(R.layout.activity_main);
1819

1920
Commander.init();
20-
Commander.addTask("ls", new IListener() {
21+
final CommandTask task = Commander.addTask("ls", new IListener() {
2122
@Override
2223
public void onPre(String command) {
2324
Log.i(TAG, "onPre: " + command);
@@ -38,5 +39,6 @@ public void onSuccess(String message) {
3839
Log.i(TAG, "onSuccess: " + message);
3940
}
4041
});
42+
// task.discard();
4143
}
4244
}

0 commit comments

Comments
 (0)