From 0b61cd79f84ab9c9285c4f900e0cdfb507af84d9 Mon Sep 17 00:00:00 2001 From: sober Date: Thu, 13 Jun 2024 19:24:10 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0log=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 19 +++++++--- .../java/com/lizongying/mytv/MainActivity.kt | 37 ++++++++++++++++++- .../java/com/lizongying/mytv/logFile.java | 32 ++++++++++++++++ 3 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/com/lizongying/mytv/logFile.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3b862ee..db7a453 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ - + - + + + + android:usesCleartextTraffic="true" + tools:ignore="UnusedAttribute"> - @@ -55,7 +61,8 @@ android:name=".NetworkChangeReceiver" android:exported="true"> - + @@ -75,4 +82,4 @@ android:resource="@xml/file_paths" /> - \ No newline at end of file + diff --git a/app/src/main/java/com/lizongying/mytv/MainActivity.kt b/app/src/main/java/com/lizongying/mytv/MainActivity.kt index 3d5d49a..5295c83 100644 --- a/app/src/main/java/com/lizongying/mytv/MainActivity.kt +++ b/app/src/main/java/com/lizongying/mytv/MainActivity.kt @@ -1,7 +1,9 @@ package com.lizongying.mytv +import android.content.pm.PackageManager import android.os.Build import android.os.Bundle +import android.os.Environment import android.os.Handler import android.os.Looper import android.util.Log @@ -15,6 +17,8 @@ import android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN import android.view.View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION import android.view.WindowManager import android.widget.Toast +import androidx.core.app.ActivityCompat +import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity import androidx.lifecycle.lifecycleScope @@ -24,7 +28,11 @@ import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.launch - +import java.io.File +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale +import android.Manifest class MainActivity : FragmentActivity(), Request.RequestListener, OnSharedPreferenceChangeListener { @@ -46,6 +54,7 @@ class MainActivity : FragmentActivity(), Request.RequestListener, OnSharedPrefer private val handler = Handler() private val delayHideMain: Long = 10000 private val delayHideSetting: Long = 15000 + private val PERMISSION_REQUEST_CODE = 1 init { Utils.setRequestListener(this) @@ -68,6 +77,32 @@ class MainActivity : FragmentActivity(), Request.RequestListener, OnSharedPrefer window.setAttributes(lp) } + // 检查并请求权限 + if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + Toast.makeText(this, "应用需要写入存储的权限以保存日志文件。", Toast.LENGTH_LONG).show() + } else { + ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE), PERMISSION_REQUEST_CODE) + } + } + + val dateFormat = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()) + val currentTime = dateFormat.format(Date()) + val fileName = "myTvLogFile_$currentTime.txt" + + try { + val downloadsDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + val outputFile = File(downloadsDirectory, fileName) + + if (!outputFile.exists()) { + outputFile.createNewFile() + } + logFile.startCapturingToFile(outputFile) + + Log.d("FileCreation", "File created: ${outputFile.absolutePath}") + } catch (e: Exception) { + Log.e("FileCreation", "Error creating file", e) + } window.decorView.apply { systemUiVisibility = diff --git a/app/src/main/java/com/lizongying/mytv/logFile.java b/app/src/main/java/com/lizongying/mytv/logFile.java new file mode 100644 index 0000000..ab08e13 --- /dev/null +++ b/app/src/main/java/com/lizongying/mytv/logFile.java @@ -0,0 +1,32 @@ +package com.lizongying.mytv; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileWriter; +import java.io.InputStreamReader; +import android.util.Log; + +public class logFile { + public static void startCapturingToFile(File outputFile) { + new Thread(() -> { + try { + Process process = Runtime.getRuntime().exec("logcat -c"); // 清除当前的日志缓存 + process = Runtime.getRuntime().exec("logcat"); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + FileWriter writer = new FileWriter(outputFile, true); + + String line; + while ((line = reader.readLine()) != null) { + writer.append(line).append("\n"); + } + + writer.flush(); + writer.close(); + reader.close(); + + } catch (Exception e) { + Log.e("log", "Error startCapturingToFile", e); + } + }).start(); + } +} From 70362456c0c2233d9f7b9bd62e71b06052035201 Mon Sep 17 00:00:00 2001 From: sober Date: Thu, 13 Jun 2024 19:24:10 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0log=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/lizongying/mytv/MainActivity.kt | 5 +++-- .../java/com/lizongying/mytv/logFile.java | 21 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/MainActivity.kt b/app/src/main/java/com/lizongying/mytv/MainActivity.kt index 5295c83..8254943 100644 --- a/app/src/main/java/com/lizongying/mytv/MainActivity.kt +++ b/app/src/main/java/com/lizongying/mytv/MainActivity.kt @@ -99,9 +99,9 @@ class MainActivity : FragmentActivity(), Request.RequestListener, OnSharedPrefer } logFile.startCapturingToFile(outputFile) - Log.d("FileCreation", "File created: ${outputFile.absolutePath}") + Log.d(TAG, "File created: ${outputFile.absolutePath}") } catch (e: Exception) { - Log.e("FileCreation", "Error creating file", e) + Log.e(TAG, "Error creating file", e) } window.decorView.apply { @@ -659,6 +659,7 @@ class MainActivity : FragmentActivity(), Request.RequestListener, OnSharedPrefer private companion object { const val TAG = "MainActivity" + const val PERMISSION_REQUEST_CODE = 1 } override fun onSharedPreferenceChanged(key: String) { diff --git a/app/src/main/java/com/lizongying/mytv/logFile.java b/app/src/main/java/com/lizongying/mytv/logFile.java index ab08e13..3e161bc 100644 --- a/app/src/main/java/com/lizongying/mytv/logFile.java +++ b/app/src/main/java/com/lizongying/mytv/logFile.java @@ -1,19 +1,27 @@ package com.lizongying.mytv; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.InputStreamReader; import android.util.Log; public class logFile { + private static final String TAG = "logFile"; // 日志标签定义 + public static void startCapturingToFile(File outputFile) { new Thread(() -> { + Process clearProcess = null; + Process logcatProcess = null; + try { - Process process = Runtime.getRuntime().exec("logcat -c"); // 清除当前的日志缓存 - process = Runtime.getRuntime().exec("logcat"); - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - FileWriter writer = new FileWriter(outputFile, true); + // 清除当前logcat缓存 + clearProcess = Runtime.getRuntime().exec("logcat -c"); + + logcatProcess = Runtime.getRuntime().exec("logcat"); + BufferedReader reader = new BufferedReader(new InputStreamReader(logcatProcess.getInputStream())); + BufferedWriter writer =new BufferedWriter(new FileWriter(outputFile, true)); String line; while ((line = reader.readLine()) != null) { @@ -25,7 +33,10 @@ public class logFile { reader.close(); } catch (Exception e) { - Log.e("log", "Error startCapturingToFile", e); + Log.e(TAG, "Error capturing log to file", e); + } finally { + if (clearProcess != null) clearProcess.destroy(); + if (logcatProcess != null) logcatProcess.destroy(); } }).start(); } From 947e4fdfbdc2349e4e9e2a55271bfb0893d2ba27 Mon Sep 17 00:00:00 2001 From: sober Date: Thu, 13 Jun 2024 19:24:10 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0log=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/lizongying/mytv/MainActivity.kt | 6 +++--- .../java/com/lizongying/mytv/logFile.java | 21 ++++++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/MainActivity.kt b/app/src/main/java/com/lizongying/mytv/MainActivity.kt index 5295c83..cee1495 100644 --- a/app/src/main/java/com/lizongying/mytv/MainActivity.kt +++ b/app/src/main/java/com/lizongying/mytv/MainActivity.kt @@ -54,7 +54,6 @@ class MainActivity : FragmentActivity(), Request.RequestListener, OnSharedPrefer private val handler = Handler() private val delayHideMain: Long = 10000 private val delayHideSetting: Long = 15000 - private val PERMISSION_REQUEST_CODE = 1 init { Utils.setRequestListener(this) @@ -99,9 +98,9 @@ class MainActivity : FragmentActivity(), Request.RequestListener, OnSharedPrefer } logFile.startCapturingToFile(outputFile) - Log.d("FileCreation", "File created: ${outputFile.absolutePath}") + Log.d(TAG, "File created: ${outputFile.absolutePath}") } catch (e: Exception) { - Log.e("FileCreation", "Error creating file", e) + Log.e(TAG, "Error creating file", e) } window.decorView.apply { @@ -659,6 +658,7 @@ class MainActivity : FragmentActivity(), Request.RequestListener, OnSharedPrefer private companion object { const val TAG = "MainActivity" + const val PERMISSION_REQUEST_CODE = 1 } override fun onSharedPreferenceChanged(key: String) { diff --git a/app/src/main/java/com/lizongying/mytv/logFile.java b/app/src/main/java/com/lizongying/mytv/logFile.java index ab08e13..3e161bc 100644 --- a/app/src/main/java/com/lizongying/mytv/logFile.java +++ b/app/src/main/java/com/lizongying/mytv/logFile.java @@ -1,19 +1,27 @@ package com.lizongying.mytv; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.InputStreamReader; import android.util.Log; public class logFile { + private static final String TAG = "logFile"; // 日志标签定义 + public static void startCapturingToFile(File outputFile) { new Thread(() -> { + Process clearProcess = null; + Process logcatProcess = null; + try { - Process process = Runtime.getRuntime().exec("logcat -c"); // 清除当前的日志缓存 - process = Runtime.getRuntime().exec("logcat"); - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - FileWriter writer = new FileWriter(outputFile, true); + // 清除当前logcat缓存 + clearProcess = Runtime.getRuntime().exec("logcat -c"); + + logcatProcess = Runtime.getRuntime().exec("logcat"); + BufferedReader reader = new BufferedReader(new InputStreamReader(logcatProcess.getInputStream())); + BufferedWriter writer =new BufferedWriter(new FileWriter(outputFile, true)); String line; while ((line = reader.readLine()) != null) { @@ -25,7 +33,10 @@ public class logFile { reader.close(); } catch (Exception e) { - Log.e("log", "Error startCapturingToFile", e); + Log.e(TAG, "Error capturing log to file", e); + } finally { + if (clearProcess != null) clearProcess.destroy(); + if (logcatProcess != null) logcatProcess.destroy(); } }).start(); }