Skip to content

Commit 1f29b4a

Browse files
committed
Merge pull request #20 from Shao-Feng/android_service
Start service by default
2 parents 04508c0 + 479d401 commit 1f29b4a

File tree

3 files changed

+79
-21
lines changed

3 files changed

+79
-21
lines changed

android/res/layout/activity_fullscreen.xml

+2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
android:id="@+id/fullscreen_content"
1616
android:layout_width="match_parent"
1717
android:layout_height="match_parent"
18+
android:scrollbars = "vertical"
1819
android:keepScreenOn="true"
20+
android:gravity="bottom"
1921
android:text="@string/output_content"
2022
android:textColor="#ffffff" />
2123

android/src/com/intel/tinywebtestservice/FullscreenActivity.java

+68-20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.os.Bundle;
2525
import android.os.Handler;
2626
import android.os.Message;
27+
import android.text.method.ScrollingMovementMethod;
2728
import android.view.MotionEvent;
2829
import android.view.View;
2930
import android.widget.TextView;
@@ -32,8 +33,10 @@
3233

3334
import java.io.BufferedReader;
3435
import java.io.File;
36+
import java.io.FileNotFoundException;
3537
import java.io.FileReader;
3638
import 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);

android/src/com/intel/tinywebtestservice/TinywebService.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ private void copyFile(String filename) {
102102
out.flush();
103103
} catch (Exception e) {
104104
Log.e(LOG_TAG, "Exception in copyFile() of " + newFileName);
105+
printLog("Exception in copyFile() of " + newFileName);
105106
Log.e(LOG_TAG, "Exception in copyFile() " + e.toString());
107+
printLog("Exception in copyFile() " + e.toString());
106108
}finally {
107109
try {
108110
in.close();
@@ -124,6 +126,7 @@ private void copyDir(String path) {
124126
String assets[] = null;
125127
try {
126128
Log.i(LOG_TAG, "copyFileOrDir() " + path);
129+
printLog("copyFileOrDir() " + path);
127130
assets = assetManager.list(path);
128131
if (assets.length == 0) {
129132
copyFile(path);
@@ -137,6 +140,7 @@ private void copyDir(String path) {
137140
if (!dir.mkdirs())
138141
;
139142
Log.i(LOG_TAG, "could not create dir " + fullPath);
143+
printLog("could not create dir " + fullPath);
140144
for (int i = 0; i < assets.length; ++i) {
141145
String p;
142146
if (path.equals(""))
@@ -152,6 +156,7 @@ private void copyDir(String path) {
152156
}
153157
} catch (IOException ex) {
154158
Log.e(LOG_TAG, "I/O Exception", ex);
159+
printLog("I/O Exception: " + ex.getMessage());
155160
}
156161
}
157162

@@ -163,6 +168,7 @@ private void unzipDocroot() {
163168
+ "system/libs/" + Build.CPU_ABI + "/busybox unzip /sdcard/docroot.zip -o -d "
164169
+ file_path;
165170
Log.i(LOG_TAG, "unzip docroot begin(cmd=" + cmd);
171+
printLog("unzip docroot begin(cmd=" + cmd);
166172
Process unzip = Runtime.getRuntime().exec(cmd);
167173
BufferedReader bufferedReader = new BufferedReader(
168174
new InputStreamReader(unzip.getInputStream()));
@@ -172,8 +178,10 @@ private void unzipDocroot() {
172178
}
173179
unzip.waitFor();
174180
Log.i(LOG_TAG, "unzip docroot end");
181+
printLog("unzip docroot end");
175182
} catch (Exception e) {
176183
Log.e(LOG_TAG, "error: " + e.getCause());
184+
printLog("error: " + e.getCause());
177185
e.printStackTrace();
178186
}
179187
}
@@ -241,7 +249,7 @@ public void run() {
241249
input.close();
242250
}
243251
Log.e(THREADLOGTAG, res);
244-
Log.i(THREADLOGTAG, "end");
252+
printLog(res);
245253
} catch (IOException e) {
246254
e.printStackTrace();
247255
}

0 commit comments

Comments
 (0)