日志文件不再需要写入frpc.log

This commit is contained in:
AceDroidX 2024-04-01 17:28:41 +08:00
parent 96ee504a2d
commit 7b8240c27e
3 changed files with 60 additions and 23 deletions

View File

@ -19,7 +19,6 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SwitchCompat import androidx.appcompat.widget.SwitchCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import java.io.File
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
@ -94,28 +93,17 @@ class MainActivity : AppCompatActivity() {
} }
val deleteButton = findViewById<Button>(R.id.deleteButton) val deleteButton = findViewById<Button>(R.id.deleteButton)
deleteButton.setOnClickListener { deleteButton.setOnClickListener {
val logfile = File(this.filesDir.toString() + "/${BuildConfig.LogFileName}") mService.clearOutput()
Log.d("adx", logfile.absoluteFile.toString())
logfile.delete()
readLog() readLog()
} }
} }
fun readLog() { fun readLog() {
val files: Array<String> = this.fileList()
val logTextView = findViewById<TextView>(R.id.logTextView) val logTextView = findViewById<TextView>(R.id.logTextView)
if (files.contains(BuildConfig.LogFileName)) { if (mBound) {
val mReader = this.openFileInput(BuildConfig.LogFileName).bufferedReader() logTextView.text = mService.getOutput()
val mRespBuff = StringBuffer()
val buff = CharArray(1024)
var ch = 0
while (mReader.read(buff).also { ch = it } != -1) {
mRespBuff.append(buff, 0, ch)
}
mReader.close()
logTextView.text = mRespBuff.toString()
} else { } else {
logTextView.text = "无日志" Log.w("adx", "readLog mBound==null")
} }
} }

View File

@ -11,11 +11,20 @@ import android.os.IBinder
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import java.util.* import java.io.File
import java.util.Random
class ShellService : Service() { class ShellService : Service() {
var p: Process? = null private var process_thread: Thread? = null
private val outputBuilder = StringBuilder()
fun getOutput(): String {
return outputBuilder.toString()
}
fun clearOutput() {
outputBuilder.clear()
}
// Binder given to clients // Binder given to clients
private val binder = LocalBinder() private val binder = LocalBinder()
@ -42,7 +51,7 @@ class ShellService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
var filename = "" var filename = ""
if (p != null) { if (process_thread != null) {
Log.w("adx", "process isn't null,service won't start") Log.w("adx", "process isn't null,service won't start")
Toast.makeText(this, "process isn't null,service won't start", Toast.LENGTH_SHORT) Toast.makeText(this, "process isn't null,service won't start", Toast.LENGTH_SHORT)
.show() .show()
@ -61,8 +70,10 @@ class ShellService : Service() {
packageManager.getApplicationInfo(packageName, PackageManager.GET_SHARED_LIBRARY_FILES) packageManager.getApplicationInfo(packageName, PackageManager.GET_SHARED_LIBRARY_FILES)
Log.d("adx", "native library dir ${ainfo.nativeLibraryDir}") Log.d("adx", "native library dir ${ainfo.nativeLibraryDir}")
try { try {
p = Runtime.getRuntime().exec( runCommand(
"${ainfo.nativeLibraryDir}/${filename} -c ${BuildConfig.ConfigFileName}", arrayOf(""), this.filesDir "${ainfo.nativeLibraryDir}/${filename} -c ${BuildConfig.ConfigFileName}",
arrayOf(""),
this.filesDir
) )
} catch (e: Exception) { } catch (e: Exception) {
Log.e("adx", e.stackTraceToString()) Log.e("adx", e.stackTraceToString())
@ -76,11 +87,15 @@ class ShellService : Service() {
} }
override fun onDestroy() { override fun onDestroy() {
p?.destroy() process_thread?.interrupt()
p = null
Toast.makeText(this, "已关闭frp服务", Toast.LENGTH_SHORT).show() Toast.makeText(this, "已关闭frp服务", Toast.LENGTH_SHORT).show()
} }
private fun runCommand(command: String, envp: Array<String>, dir: File) {
process_thread = ShellThread(command, envp, dir) { outputBuilder.append(it + "\n") }
process_thread?.start()
}
private fun showMotification(): Notification { private fun showMotification(): Notification {
val pendingIntent: PendingIntent = val pendingIntent: PendingIntent =
Intent(this, MainActivity::class.java).let { notificationIntent -> Intent(this, MainActivity::class.java).let { notificationIntent ->

View File

@ -0,0 +1,34 @@
package io.github.acedroidx.frp
import android.util.Log
import java.io.BufferedReader
import java.io.File
import java.io.IOException
import java.io.InputStreamReader
class ShellThread(
val command: String,
val envp: Array<String>,
val dir: File,
val outputCallback: (text: String) -> Unit
) : Thread() {
override fun run() {
try {
// Log.d("adx","线程启动")
val process = Runtime.getRuntime().exec(command, envp, dir)
val inputStream = process.inputStream
val reader = BufferedReader(InputStreamReader(inputStream))
var line: String?
while (((reader.readLine().also { line = it }) != null) && !isInterrupted) {
// outputBuilder.insert(0, line).append("\n")
// outputBuilder.append(line).append("\n")
line?.let { outputCallback(it) }
}
reader.close()
process.destroy()
// Log.d("adx","线程关闭")
} catch (e: IOException) {
e.printStackTrace()
}
}
}