From 4c9c9b4d06e9146519ea453f253f2d6167e854fe Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 7 Jan 2025 18:23:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把日志显示移到独立的activity --- app/src/main/AndroidManifest.xml | 5 +- .../java/com/xypower/mpapp/LogActivity.java | 191 +++++++++++++ .../java/com/xypower/mpapp/MainActivity.java | 260 ++++++------------ .../main/res/layout-land/activity_main.xml | 126 +-------- app/src/main/res/layout/activity_log.xml | 27 ++ app/src/main/res/layout/activity_main.xml | 91 +----- .../java/com/xypower/common/FilesUtils.java | 1 - .../com/xypower/common/MicroPhotoContext.java | 24 ++ .../com/xypower/mpmaster/MainActivity.java | 1 - .../com/xypower/mpmaster/MpMasterService.java | 110 ++++---- 10 files changed, 416 insertions(+), 420 deletions(-) create mode 100644 app/src/main/java/com/xypower/mpapp/LogActivity.java create mode 100644 app/src/main/res/layout/activity_log.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 012a5f3a..f95f9c1d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -99,7 +99,10 @@ android:supportsRtl="true" android:theme="@style/Theme.MicroPhoto" tools:targetApi="28"> - + binding.logs.getHeight()) { + binding.logs.scrollTo(0, offset - binding.logs.getHeight() + binding.logs.getLineHeight()); + } + } + } + break; + } + } + }; + + } + + + @Override + protected void onResume() { + // call the superclass method first + super.onResume(); + + try { + String logFilePath = MicroPhotoContext.buildAppDir(this.getApplicationContext()); + logFilePath += "logs"; + File file = new File(logFilePath); + if (!file.exists()) { + file.mkdirs(); + } + logFilePath += "/log.txt"; + file = new File(logFilePath); + if (!file.exists()) { + file.createNewFile(); + } + + 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 + protected void onPause() { + // call the superclass method first + super.onPause(); + + try { + if (mLogFileObserver != null) { + mLogFileObserver.stopWatching(); + mLogFileObserver = null; + Log.i(TAG, "Log Observer Stopped"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + // todo: goto back activity from here + + finish(); + return true; + + default: + return super.onOptionsItemSelected(item); + } + } + + + private class LogFileObserver extends FileObserver { + + private long mOffset = 0; + private String mPath = null; + public LogFileObserver(String path) { + super(path, FileObserver.MODIFY | FileObserver.CREATE); + + mPath = path; + File file = new File(path); + if (file.exists()) { + mOffset = file.length(); + } + } + @Override + public void onEvent(int event, String s) { + int e = event & FileObserver.ALL_EVENTS; + + if (e == FileObserver.MODIFY) { + File file = new File(mPath); + long newOffset = file.length(); + if (newOffset > mOffset) { + RandomReader reader = new RandomReader(mPath, mOffset); + + byte[] bytes = new byte[(int)(newOffset - mOffset)]; + int bytesRead = reader.read(bytes); + mOffset += bytesRead; + + Message msg = Message.obtain(); + msg.what = MSG_WHAT_LOG_OBSERVER; + msg.obj = bytes; + msg.arg1 = bytesRead; + mHandler.sendMessage(msg); + } + } else if (e == FileObserver.CREATE) { + mOffset = 0; + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/xypower/mpapp/MainActivity.java b/app/src/main/java/com/xypower/mpapp/MainActivity.java index b77aebc8..faceb9d5 100644 --- a/app/src/main/java/com/xypower/mpapp/MainActivity.java +++ b/app/src/main/java/com/xypower/mpapp/MainActivity.java @@ -50,60 +50,12 @@ public class MainActivity extends AppCompatActivity { public static final String TAG = "MPLOG"; private static int MY_PERMISSIONS_REQUEST_FOREGROUND_SERVICE = 100; - public static final int MSG_WHAT_LOG_OBSERVER = MicroPhotoService.MSG_WHAT_MAX + 10; - // Used to load the 'microphoto' library on application startup. - - public static final int MAX_LOG_LINES = 480; - public static final int MIN_LOG_LINES = 120; - private ActivityMainBinding binding; - private Handler mHandler = null; private Messenger mMessenger = null; - private LogFileObserver mLogFileObserver = null; - - private class LogFileObserver extends FileObserver { - - private long mOffset = 0; - private String mPath = null; - public LogFileObserver(String path) { - super(path, FileObserver.MODIFY | FileObserver.CREATE); - - mPath = path; - File file = new File(path); - if (file.exists()) { - mOffset = file.length(); - } - } - @Override - public void onEvent(int event, String s) { - int e = event & FileObserver.ALL_EVENTS; - - if (e == FileObserver.MODIFY) { - File file = new File(mPath); - long newOffset = file.length(); - if (newOffset > mOffset) { - RandomReader reader = new RandomReader(mPath, mOffset); - - byte[] bytes = new byte[(int)(newOffset - mOffset)]; - int bytesRead = reader.read(bytes); - mOffset += bytesRead; - - Message msg = Message.obtain(); - msg.what = MSG_WHAT_LOG_OBSERVER; - msg.obj = bytes; - msg.arg1 = bytesRead; - mHandler.sendMessage(msg); - } - } else if (e == FileObserver.CREATE) { - mOffset = 0; - } - } - } - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -126,39 +78,45 @@ public class MainActivity extends AppCompatActivity { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); actionBar.setTitle(actionBar.getTitle().toString() + " v" + MicroPhotoContext.getVersionName(getApplicationContext()) + " " + sdf.format(date)); - binding.logs.setText(""); - binding.logs.setMovementMethod(ScrollingMovementMethod.getInstance()); - binding.logs.setScrollbarFadingEnabled(false); + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); + StrictMode.setThreadPolicy(policy); - mHandler = new Handler(Looper.myLooper()) { - @Override - public void handleMessage(@NonNull Message msg) { - switch (msg.what) { - case MSG_WHAT_LOG_OBSERVER: - { - byte[] bytes = (byte[])msg.obj; - int bytesRead = msg.arg1; - String log = null; - try { - log = new String(bytes, 0, bytesRead, "UTF-8"); - } catch (Exception e) { - e.printStackTrace(); - } - if (log != null) { - binding.logs.append(log); - int offset = binding.logs.getLineCount() * binding.logs.getLineHeight(); - if (offset > binding.logs.getHeight()) { - binding.logs.scrollTo(0, offset - binding.logs.getHeight() + binding.logs.getLineHeight()); - } - } + initListener(); + + Context appContext = getApplicationContext(); + String appPath = MicroPhotoContext.buildMpAppDir(appContext); + File appPathFile = new File(appPath); + if (!appPathFile.exists()) { + try { + appPathFile.mkdirs(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + if (!MicroPhotoContext.hasMpAppConfig(appContext)) { + + String mstPath = MicroPhotoContext.buildMasterAppDir(appContext); + File mstPathFile = new File(mstPath); + File mpdataFile = new File(mstPathFile, "mpdata"); + + if (mpdataFile.exists()) { + File dataFile = new File(appPathFile, "data"); + if (dataFile.exists()) { + try { + dataFile.delete(); + } catch (Exception ex) { + ex.printStackTrace(); } - break; } - } - }; - StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); - StrictMode.setThreadPolicy(policy); + try { + mpdataFile.renameTo(dataFile); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } Intent intent = getIntent(); final int noDelay = intent.getIntExtra("noDelay", 0); @@ -173,7 +131,9 @@ public class MainActivity extends AppCompatActivity { Log.d(TAG, "MainActivity: reboot=" + rebootFlag + " noDelay=" + noDelay); - final MicroPhotoContext.AppConfig appConfig = getAppConfig(); + + + final MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(appContext); if (TextUtils.isEmpty(appConfig.cmdid)) { appConfig.cmdid = MicroPhotoService.getSerialNumber(); binding.cmdid.setText(appConfig.cmdid); @@ -204,6 +164,45 @@ public class MainActivity extends AppCompatActivity { binding.network.setSelection(appConfig.network); } + binding.btnStartServ.setEnabled(!MicroPhotoService.isRunning); + binding.btnStopServ.setEnabled(MicroPhotoService.isRunning); + + if (MicroPhotoService.isRunning) { + Intent intent2 = new Intent(MainActivity.this, MicroPhotoService.class); + try { + stopService(intent2); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + + + if (MicroPhotoContext.hasMpAppConfig(appContext)) { + Runnable runnable = new Runnable() { + @Override + public void run() { + + if (!MicroPhotoService.isRunning && !TextUtils.isEmpty(appConfig.cmdid) && !TextUtils.isEmpty(appConfig.server) && appConfig.port != 0) { + if (binding.btnStartServ.isEnabled()) { + binding.btnStartServ.performClick(); + } + } + } + }; + + Handler handler = new Handler(); + handler.postDelayed(runnable, 500); + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + } + + protected void initListener() { + this.binding.btnStartServ.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -224,17 +223,10 @@ public class MainActivity extends AppCompatActivity { // return; } - binding.logs.setText(""); - MicroPhotoContext.AppConfig curAppConfig = retrieveAndSaveAppConfig(); + Context appContext = getApplicationContext(); + MicroPhotoContext.AppConfig curAppConfig = MicroPhotoContext.getMpAppConfig(appContext); - // TakeAndThrowPhoto(2, 0xFF); - try { - // Thread.sleep(20); - } catch (Exception ex) { - ex.printStackTrace(); - } - - startMicroPhotoService(MainActivity.this.getApplicationContext(), appConfig, mMessenger); + startMicroPhotoService(appContext, curAppConfig, mMessenger); binding.btnStartServ.setEnabled(false); binding.btnStopServ.setEnabled(true); @@ -354,30 +346,13 @@ public class MainActivity extends AppCompatActivity { } }); - if (MicroPhotoService.isRunning) { - Intent intent2 = new Intent(MainActivity.this, MicroPhotoService.class); - try { - stopService(intent2); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - Runnable runnable = new Runnable() { + binding.btnLogs.setOnClickListener(new View.OnClickListener() { @Override - public void run() { - - if (!MicroPhotoService.isRunning && !TextUtils.isEmpty(appConfig.cmdid) && !TextUtils.isEmpty(appConfig.server) && appConfig.port != 0) { - if (binding.btnStartServ.isEnabled()) { - binding.btnStartServ.performClick(); - } - } + public void onClick(View v) { + Intent intent = new Intent(MainActivity.this, LogActivity.class); + startActivity(intent); } - }; - mHandler.postDelayed(runnable, noDelay != 0 ? 1000 : 5000); - - binding.btnStartServ.setEnabled(!MicroPhotoService.isRunning); - binding.btnStopServ.setEnabled(MicroPhotoService.isRunning); + }); binding.btnSendHb.setOnClickListener(new View.OnClickListener() { @Override @@ -442,17 +417,13 @@ public class MainActivity extends AppCompatActivity { } }; - mHandler.postDelayed(runnable, 1500); + Handler handler = new Handler(); + handler.postDelayed(runnable, 1500); } }); } - @Override - protected void onDestroy() { - super.onDestroy(); - } - public static void startMicroPhotoService(Context context, MicroPhotoContext.AppConfig curAppConfig, Messenger messenger) { if (TextUtils.isEmpty(curAppConfig.cmdid) || TextUtils.isEmpty(curAppConfig.server) || curAppConfig.port == 0) { @@ -523,63 +494,6 @@ public class MainActivity extends AppCompatActivity { MicroPhotoService.takePhoto(channel, preset, true, configFile.getAbsolutePath(), photoFile.getAbsolutePath()); } - @Override - protected void onResume() { - // call the superclass method first - super.onResume(); - - try { - String logFilePath = MicroPhotoContext.buildAppDir(this.getApplicationContext()); - logFilePath += "logs"; - File file = new File(logFilePath); - if (!file.exists()) { - file.mkdirs(); - } - logFilePath += "/log.txt"; - file = new File(logFilePath); - if (!file.exists()) { - file.createNewFile(); - } - - 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 - protected void onPause() { - // call the superclass method first - super.onPause(); - - try { - if (mLogFileObserver != null) { - mLogFileObserver.stopWatching(); - mLogFileObserver = null; - Log.i(TAG, "Log Observer Stopped"); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - private MicroPhotoContext.AppConfig retrieveAndSaveAppConfig() { MicroPhotoContext.AppConfig appConfig = new MicroPhotoContext.AppConfig(); diff --git a/app/src/main/res/layout-land/activity_main.xml b/app/src/main/res/layout-land/activity_main.xml index 99408fba..36b42e29 100644 --- a/app/src/main/res/layout-land/activity_main.xml +++ b/app/src/main/res/layout-land/activity_main.xml @@ -204,6 +204,16 @@ app:layout_constraintStart_toEndOf="@+id/btnSaveCfg" app:layout_constraintTop_toTopOf="@+id/btnStartServ" /> +