24
24
import android .os .Bundle ;
25
25
import android .os .Handler ;
26
26
import android .os .Message ;
27
+ import android .text .method .ScrollingMovementMethod ;
27
28
import android .view .MotionEvent ;
28
29
import android .view .View ;
29
30
import android .widget .TextView ;
32
33
33
34
import java .io .BufferedReader ;
34
35
import java .io .File ;
36
+ import java .io .FileNotFoundException ;
35
37
import java .io .FileReader ;
36
38
import java .io .IOException ;
39
+ import java .io .PrintWriter ;
37
40
38
41
/**
39
42
* An example full-screen activity that shows and hides the system UI (i.e.
@@ -94,6 +97,8 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
94
97
private String target_path ;
95
98
private static String full_log = "" ;
96
99
private Process p ;
100
+ private Thread logThread = null ;
101
+ private boolean logUpdating = false ;
97
102
98
103
//create a Handler for updating UI
99
104
@@ -180,29 +185,15 @@ public void onClick(View view) {
180
185
findViewById (R .id .refresh_button ).setOnTouchListener (
181
186
mDelayHideTouchListener );
182
187
tv = (TextView ) contentView ;
188
+ tv .setMovementMethod (new ScrollingMovementMethod ());
183
189
target_path = getFilesDir ().getPath () + "/" ;
184
190
assetManager = this .getAssets ();
185
191
186
- Thread t = new Thread () {
187
- public void run () {
188
- try {
189
- BufferedReader br = new BufferedReader (new FileReader (new File (Constants .LOG .SERVICE_LOG_FILE )));
190
- String line ;
191
- while (true ) {
192
- line = br .readLine ();
193
- if (line == null ) {
194
- Thread .sleep (1000 );
195
- } else {
196
- printLog (line );
197
- }
198
- }
199
- }catch (IOException e ){
200
- e .printStackTrace ();
201
- } catch (InterruptedException e ) {
202
- e .printStackTrace ();
203
- }
204
- }
205
- };
192
+ printLog ("Start service..." );
193
+ restartLogThread ();
194
+ Intent startIntent = new Intent (FullscreenActivity .this , TinywebService .class );
195
+ startIntent .setAction (Constants .ACTION .STARTFOREGROUND_ACTION );
196
+ startService (startIntent );
206
197
}
207
198
208
199
@ Override
@@ -224,6 +215,60 @@ private void delayedHide(int delayMillis) {
224
215
mHideHandler .postDelayed (mHideRunnable , delayMillis );
225
216
}
226
217
218
+ private void restartLogThread (){
219
+ logUpdating = false ;
220
+ try {
221
+ Thread .sleep (200 );
222
+ } catch (InterruptedException e ) {
223
+ e .printStackTrace ();
224
+ }
225
+ if (!logUpdating ) {
226
+ logThread = new Thread () {
227
+ public void run () {
228
+ try {
229
+ BufferedReader br = new BufferedReader (new FileReader (new File (Constants .LOG .SERVICE_LOG_FILE )));
230
+ String line ;
231
+ while (logUpdating ) {
232
+ line = br .readLine ();
233
+ if (line == null ) {
234
+ Thread .sleep (1000 );
235
+ } else {
236
+ printLog (line );
237
+ }
238
+ }
239
+ }catch (IOException e ){
240
+ e .printStackTrace ();
241
+ } catch (InterruptedException e ) {
242
+ e .printStackTrace ();
243
+ }
244
+ }
245
+ };
246
+ logUpdating = true ;
247
+ logThread .start ();
248
+ }
249
+ }
250
+
251
+ private void clearLog (){
252
+ logUpdating = false ;
253
+ PrintWriter writer = null ;
254
+ try {
255
+ writer = new PrintWriter (Constants .LOG .SERVICE_LOG_FILE );
256
+ writer .print ("" );
257
+ writer .close ();
258
+ } catch (FileNotFoundException e ) {
259
+ e .printStackTrace ();
260
+ }
261
+
262
+ restartLogThread ();
263
+
264
+ Message message = new Message ();
265
+ Bundle bundle = new Bundle ();
266
+ full_log = "" ;
267
+ bundle .putString ("LOG" , full_log );
268
+ message .setData (bundle );
269
+ handler .sendMessage (message );
270
+ }
271
+
227
272
/**
228
273
* Called when the user clicks the runService button
229
274
*/
@@ -238,13 +283,16 @@ public void refreshData(View view) {
238
283
239
284
public void stopTinywebService (View view ) {
240
285
printLog ("Stop service..." );
286
+ clearLog ();
241
287
Intent startIntent = new Intent (FullscreenActivity .this , TinywebService .class );
242
288
startIntent .setAction (Constants .ACTION .STOPFOREGROUND_ACTION );
243
289
startService (startIntent );
290
+ //printLog("Service is stopped");
244
291
}
245
292
246
293
public void startTinywebService (View view ) {
247
294
printLog ("Start service..." );
295
+ restartLogThread ();
248
296
Intent startIntent = new Intent (FullscreenActivity .this , TinywebService .class );
249
297
startIntent .setAction (Constants .ACTION .STARTFOREGROUND_ACTION );
250
298
startService (startIntent );
0 commit comments