@@ -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 }
0 commit comments