@@ -32,6 +32,7 @@ import java.io.BufferedReader
3232import java.io.DataOutputStream
3333import java.io.File
3434import java.util.Locale
35+ import android.os.Looper
3536
3637class SplashActivity : AppCompatActivity () {
3738
@@ -187,6 +188,8 @@ class SplashActivity : AppCompatActivity() {
187188 }
188189
189190 private fun gotoHome () {
191+ if (isFinishing || isDestroyed) return
192+
190193 startActivity(
191194 if (intent?.getBooleanExtra(" JumpActionPage" , false ) == true )
192195 Intent (this , ActionPage ::class .java).apply { putExtras(intent!! ) }
@@ -197,9 +200,59 @@ class SplashActivity : AppCompatActivity() {
197200
198201 private fun runBeforeStartSh (config : KrScriptConfig , hasRoot : Boolean ) {
199202
200- Thread {
203+ val updateHandler = UpdateLogViewHandler (binding.startStateText) {
204+ gotoHome()
205+ }
206+
207+ BeforeStartThread (
208+ this ,
209+ config,
210+ hasRoot,
211+ updateHandler
212+ ).start()
213+ }
214+
215+ private class UpdateLogViewHandler (
216+ private val logView : TextView ,
217+ private val onExit : () -> Unit
218+ ) {
219+ private val handler = Handler (Looper .getMainLooper())
220+ private val rows = ArrayList <String >()
221+ private var ignored = false
222+
223+ fun onLogOutput (log : String ) {
224+ handler.post {
225+ synchronized(rows) {
226+ if (rows.size > 6 ) {
227+ rows.removeAt(0 )
228+ ignored = true
229+ }
230+ rows.add(log)
231+ logView.text = rows.joinToString(
232+ " \n " ,
233+ if (ignored) " ……\n " else " "
234+ ).trim()
235+ }
236+ }
237+ }
238+
239+ fun onExit () {
240+ handler.post {
241+ onExit()
242+ }
243+ }
244+ }
245+
246+ private class BeforeStartThread (
247+ private val context : Context ,
248+ private val config : KrScriptConfig ,
249+ private val hasRoot : Boolean ,
250+ private val updateHandler : UpdateLogViewHandler
251+ ) : Thread() {
201252
253+ override fun run () {
202254 try {
255+
203256 val process = if (hasRoot)
204257 ShellExecutor .getSuperUserRuntime()
205258 else
@@ -210,67 +263,49 @@ class SplashActivity : AppCompatActivity() {
210263 val outputStream = DataOutputStream (process.outputStream)
211264
212265 ScriptEnvironmen .executeShell(
213- this @SplashActivity ,
266+ context ,
214267 outputStream,
215268 config.beforeStartSh,
216269 config.variables,
217270 null ,
218271 " pio-splash"
219272 )
220273
221- // Đọc stdout
222- Thread {
223- process.inputStream.bufferedReader().forEachLine { line ->
224- runOnUiThread { onLogOutput(line) }
225- }
226- }.start()
274+ StreamReadThread (
275+ process.inputStream.bufferedReader(),
276+ updateHandler
277+ ).start()
227278
228- // Đọc stderr
229- Thread {
230- process.errorStream.bufferedReader().forEachLine { line ->
231- runOnUiThread { onLogOutput(line) }
232- }
233- }.start()
279+ StreamReadThread (
280+ process.errorStream.bufferedReader(),
281+ updateHandler
282+ ).start()
234283
235- // Chờ shell kết thúc
236284 process.waitFor()
237285 }
238286
239287 } catch (e: Exception ) {
240288 e.printStackTrace()
241289 }
242290
243- // Khi shell exit mới vào Home
244- runOnUiThread {
245- gotoHome()
246- }
247-
248- }.start()
249- }
250-
251- // Buffer lưu 4 dòng cuối
252- private val rows = mutableListOf<String >()
253- private var ignored = false
254- private val maxLines = 5
255- private val handler = android.os.Handler (android.os.Looper .getMainLooper())
256-
257- private fun readStreamAsync (reader : BufferedReader ) {
258- Thread {
259- reader.forEachLine { line ->
260- onLogOutput(line)
261- }
262- }.start()
291+ // Quan trọng: giống code cũ
292+ updateHandler.onExit()
293+ }
263294 }
264-
265- private fun onLogOutput (log : String ) {
266- handler.post {
267- synchronized(rows) {
268- if (rows.size >= maxLines) {
269- rows.removeAt(0 )
270- ignored = true
295+
296+ private class StreamReadThread (
297+ private val reader : BufferedReader ,
298+ private val updateHandler : UpdateLogViewHandler
299+ ) : Thread() {
300+
301+ override fun run () {
302+ try {
303+ var line: String?
304+ while (true ) {
305+ line = reader.readLine() ? : break
306+ updateHandler.onLogOutput(line)
271307 }
272- rows.add(log)
273- binding.startStateText.text = rows.joinToString(" \n " , if (ignored) " ……\n " else " " )
308+ } catch (_: Exception ) {
274309 }
275310 }
276311 }
0 commit comments