@@ -243,55 +243,75 @@ class SplashActivity : AppCompatActivity() {
243243 }
244244 }
245245
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() {
252-
253- override fun run () {
254- try {
255-
256- val process = if (hasRoot)
257- ShellExecutor .getSuperUserRuntime()
258- else
259- ShellExecutor .getRuntime()
260-
261- if (process != null ) {
262-
263- val outputStream = DataOutputStream (process.outputStream)
264-
265- ScriptEnvironmen .executeShell(
266- context,
267- outputStream,
268- config.beforeStartSh,
269- config.variables,
270- null ,
271- " pio-splash"
272- )
273-
274- StreamReadThread (
275- process.inputStream.bufferedReader(),
276- updateHandler
277- ).start()
278-
279- StreamReadThread (
280- process.errorStream.bufferedReader(),
281- updateHandler
282- ).start()
283-
284- process.waitFor()
285- }
286-
287- } catch (e: Exception ) {
288- e.printStackTrace()
246+ private class BeforeStartThread (
247+ private var context : Context ,
248+ private val config : KrScriptConfig ,
249+ private var updateLogViewHandler : UpdateLogViewHandler
250+ ) : Thread() {
251+
252+ private val params = config.getVariables()
253+
254+ override fun run () {
255+ var process: Process ? = null
256+ var stdoutThread: Thread ? = null
257+ var stderrThread: Thread ? = null
258+
259+ try {
260+ process = if (CheckRootStatus .lastCheckResult)
261+ ShellExecutor .getSuperUserRuntime()
262+ else
263+ ShellExecutor .getRuntime()
264+
265+ if (process == null ) {
266+ updateLogViewHandler.onExit()
267+ return
289268 }
290-
291- // Quan trọng: giống code cũ
292- updateHandler.onExit()
269+
270+ val outputStream = DataOutputStream (process.outputStream)
271+
272+ // Gửi script vào shell
273+ ScriptEnvironmen .executeShell(
274+ context,
275+ outputStream,
276+ config.beforeStartSh,
277+ params,
278+ null ,
279+ " pio-splash"
280+ )
281+
282+ // QUAN TRỌNG: đóng output để tránh treo
283+ outputStream.flush()
284+ outputStream.close()
285+
286+ // Đọc stdout + stderr
287+ stdoutThread = StreamReadThread (
288+ process.inputStream.bufferedReader(),
289+ updateLogViewHandler
290+ ).apply { start() }
291+
292+ stderrThread = StreamReadThread (
293+ process.errorStream.bufferedReader(),
294+ updateLogViewHandler
295+ ).apply { start() }
296+
297+ // Chờ process kết thúc
298+ process.waitFor()
299+
300+ // Chờ đọc hết log (fix mất 2 dòng cuối)
301+ stdoutThread.join()
302+ stderrThread.join()
303+
304+ } catch (e: Exception ) {
305+ e.printStackTrace()
306+ } finally {
307+ try {
308+ process?.destroy()
309+ } catch (_: Exception ) {}
310+
311+ updateLogViewHandler.onExit()
293312 }
294313 }
314+ }
295315
296316 private class StreamReadThread (
297317 private val reader : BufferedReader ,
0 commit comments