diff --git a/app/src/main/java/com/xypower/mpapp/MainActivity.java b/app/src/main/java/com/xypower/mpapp/MainActivity.java index 98b8c99c..8cde7a38 100644 --- a/app/src/main/java/com/xypower/mpapp/MainActivity.java +++ b/app/src/main/java/com/xypower/mpapp/MainActivity.java @@ -57,8 +57,8 @@ public class MainActivity extends AppCompatActivity { } public static final int MAX_LOG_LINES = 480; + public static final int MIN_LOG_LINES = 120; - private int mLines = 0; private ActivityMainBinding binding; private Handler mHandler = null; @@ -141,27 +141,6 @@ public class MainActivity extends AppCompatActivity { @Override public void handleMessage(@NonNull Message msg) { switch (msg.what) { - case MicroPhotoService.MSG_WHAT_LOG: { - String log = (String) msg.obj; - binding.logs.append(log); - - // Reply - // Messenger clientMessenger = msg.replyTo; - // Message replyMessage = Message.obtain(null, 2); - // 设置回复消息中的数据 - Bundle replyData = new Bundle(); - replyData.putString("key", "reply value"); - // replyMessage.setData(replyData); - /* - try { - clientMessenger.send(replyMessage); - } catch (RemoteException e) { - e.printStackTrace(); - } - - */ - } - break; case MSG_WHAT_LOG_OBSERVER: { byte[] bytes = (byte[])msg.obj; @@ -170,35 +149,14 @@ public class MainActivity extends AppCompatActivity { try { log = new String(bytes, 0, bytesRead, "UTF-8"); } catch (Exception e) { + e.printStackTrace(); } if (log != null) { - - long lines = log.chars().filter(ch -> ch == '\n').count(); - mLines += lines; - if (mLines + lines > MAX_LOG_LINES) { - int excessLineNumber = MAX_LOG_LINES / 2; - - int eolIndex = -1; - CharSequence charSequence = binding.logs.getText(); - for(int i = 0; i < excessLineNumber; i++) { - do { - eolIndex++; - } while(eolIndex < charSequence.length() && charSequence.charAt(eolIndex) != '\n'); - } - if (eolIndex < charSequence.length()) { - binding.logs.getEditableText().delete(0, eolIndex+1); - } - mLines -= excessLineNumber; - } - binding.logs.append(log); - - // if (line > 9) {//超出屏幕自动滚动显示(9是当前页面显示的最大行数) int offset = mLines * binding.logs.getLineHeight(); if (offset > binding.logs.getHeight()) { binding.logs.scrollTo(0, offset - binding.logs.getHeight() + binding.logs.getLineHeight()); } - // } } } break; @@ -567,12 +525,31 @@ public class MainActivity extends AppCompatActivity { // call the superclass method first super.onResume(); - String logFilePath = MicroPhotoContext.buildAppDir(this.getApplicationContext()); - logFilePath += "logs/log.txt"; - - mLogFileObserver = new LogFileObserver(logFilePath); - mLogFileObserver.startWatching(); - Log.i(TAG, "Log Observer Started"); + try { + String logFilePath = MicroPhotoContext.buildAppDir(this.getApplicationContext()); + logFilePath += "logs/log.txt"; + + mLogFileObserver = new LogFileObserver(logFilePath); + mLogFileObserver.startWatching(); + Log.i(TAG, "Log Observer Started"); + + int lines = binding.logs.getLineCount(); + if (lines > MAX_LOG_LINES) { + int excessLineNumber = lines - MIN_LOG_LINES; + int eolIndex = -1; + CharSequence charSequence = binding.logs.getText(); + for (int i = 0; i < excessLineNumber; i++) { + do { + eolIndex++; + } while (eolIndex < charSequence.length() && charSequence.charAt(eolIndex) != '\n'); + } + if (eolIndex < charSequence.length()) { + binding.logs.getEditableText().delete(0, eolIndex + 1); + } + } + } catch (Exception e) { + e.printStackTrace(); + } } @Override @@ -580,16 +557,19 @@ public class MainActivity extends AppCompatActivity { // call the superclass method first super.onPause(); - if (mLogFileObserver != null) { - mLogFileObserver.stopWatching(); - mLogFileObserver = null; - Log.i(TAG, "Log Observer Stopped"); + try { + if (mLogFileObserver != null) { + mLogFileObserver.stopWatching(); + mLogFileObserver = null; + Log.i(TAG, "Log Observer Stopped"); + } + } catch (Exception e) { + e.printStackTrace(); } } @Override protected void onDestroy() { - super.onDestroy(); }