2424import android .os .Bundle ;
2525import android .os .Handler ;
2626import android .os .Message ;
27+ import android .text .method .ScrollingMovementMethod ;
2728import android .view .MotionEvent ;
2829import android .view .View ;
2930import android .widget .TextView ;
3233
3334import java .io .BufferedReader ;
3435import java .io .File ;
36+ import java .io .FileNotFoundException ;
3537import java .io .FileReader ;
3638import java .io .IOException ;
39+ import java .io .PrintWriter ;
3740
3841/**
3942 * 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) {
9497 private String target_path ;
9598 private static String full_log = "" ;
9699 private Process p ;
100+ private Thread logThread = null ;
101+ private boolean logUpdating = false ;
97102
98103 //create a Handler for updating UI
99104
@@ -180,29 +185,15 @@ public void onClick(View view) {
180185 findViewById (R .id .refresh_button ).setOnTouchListener (
181186 mDelayHideTouchListener );
182187 tv = (TextView ) contentView ;
188+ tv .setMovementMethod (new ScrollingMovementMethod ());
183189 target_path = getFilesDir ().getPath () + "/" ;
184190 assetManager = this .getAssets ();
185191
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 );
206197 }
207198
208199 @ Override
@@ -224,6 +215,60 @@ private void delayedHide(int delayMillis) {
224215 mHideHandler .postDelayed (mHideRunnable , delayMillis );
225216 }
226217
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+
227272 /**
228273 * Called when the user clicks the runService button
229274 */
@@ -238,13 +283,16 @@ public void refreshData(View view) {
238283
239284 public void stopTinywebService (View view ) {
240285 printLog ("Stop service..." );
286+ clearLog ();
241287 Intent startIntent = new Intent (FullscreenActivity .this , TinywebService .class );
242288 startIntent .setAction (Constants .ACTION .STOPFOREGROUND_ACTION );
243289 startService (startIntent );
290+ //printLog("Service is stopped");
244291 }
245292
246293 public void startTinywebService (View view ) {
247294 printLog ("Start service..." );
295+ restartLogThread ();
248296 Intent startIntent = new Intent (FullscreenActivity .this , TinywebService .class );
249297 startIntent .setAction (Constants .ACTION .STARTFOREGROUND_ACTION );
250298 startService (startIntent );
0 commit comments