@@ -32,7 +32,6 @@ import java.io.BufferedReader
3232import java.io.DataOutputStream
3333import java.io.File
3434import java.util.Locale
35- import android.os.Looper
3635
3736class SplashActivity : AppCompatActivity () {
3837
@@ -179,98 +178,72 @@ class SplashActivity : AppCompatActivity() {
179178 }
180179
181180 private fun startToFinish () {
181+ binding.startStateText.text = getString(R .string.pop_started)
182182 val config = KrScriptConfig ().init (this )
183+
183184 if (config.beforeStartSh.isNotEmpty()) {
184- BeforeStartThread (
185- this ,
186- config,
187- hasRoot,
188- UpdateLogViewHandler (binding.startStateText) {
189- gotoHome()
190- }
191- ).start()
192- } else {
193- gotoHome()
194- }
185+ runBeforeStartSh(config, hasRoot)
186+ } else gotoHome()
195187 }
196188
197189 private fun gotoHome () {
198- if (this .intent != null && this .intent.hasExtra(" JumpActionPage" ) && this .intent.getBooleanExtra(" JumpActionPage" , false )) {
199- val actionPage = Intent (this .applicationContext, ActionPage ::class .java)
200- actionPage.putExtras(this .intent)
201- startActivity(actionPage)
202- } else {
203- val home = Intent (this .applicationContext, MainActivity ::class .java)
204- startActivity(home)
205- }
190+ startActivity(
191+ if (intent?.getBooleanExtra(" JumpActionPage" , false ) == true )
192+ Intent (this , ActionPage ::class .java).apply { putExtras(intent!! ) }
193+ else Intent (this , MainActivity ::class .java)
194+ )
206195 finish()
207196 }
208197
209- private class UpdateLogViewHandler (
210- private val logView : TextView ,
211- private val onExit : () -> Unit
212- ) {
213- private val handler = Handler (Looper .getMainLooper())
214- private val rows = ArrayList <String >()
215- private var ignored = false
216-
217- fun onLogOutput (log : String ) {
218- handler.post {
219- synchronized(rows) {
220- if (rows.size > 4 ) {
221- rows.removeAt(0 )
222- ignored = true
198+ private fun runBeforeStartSh (config : KrScriptConfig , hasRoot : Boolean ) {
199+ lifecycleScope.launch(Dispatchers .IO ) {
200+ try {
201+ val process = if (hasRoot)
202+ ShellExecutor .getSuperUserRuntime()
203+ else
204+ ShellExecutor .getRuntime()
205+ process?.let {
206+ DataOutputStream (it.outputStream).use { os ->
207+ ScriptEnvironmen .executeShell(
208+ this @SplashActivity,
209+ os,
210+ config.beforeStartSh,
211+ config.variables,
212+ null ,
213+ " pio-splash"
214+ )
223215 }
224- rows.add(log)
225- logView.text = rows.joinToString(
226- " \n " ,
227- if (ignored) " ……\n " else " "
228- ).trim()
229216 }
217+ } catch (_: Exception ) {
230218 }
231219 }
232-
233- fun onExit () {
234- handler.post { onExit() }
235- }
220+ gotoHome()
236221 }
237-
238- private class BeforeStartThread (private val context : Context = context.applicationContext, private val config : KrScriptConfig , private val hasRoot : Boolean , private var updateLogViewHandler : UpdateLogViewHandler ) : Thread() {
239- val params = config.getVariables();
240-
241- override fun run () {
242- try {
243- val process = if (hasRoot) ShellExecutor .getSuperUserRuntime() else ShellExecutor .getRuntime()
244- if (process != null ) {
245- val outputStream = DataOutputStream (process.outputStream)
246222
247- ScriptEnvironmen .executeShell(context, outputStream, config.beforeStartSh, params, null , " pio-splash" )
223+ // Buffer lưu 4 dòng cuối
224+ private val rows = mutableListOf<String >()
225+ private var ignored = false
226+ private val maxLines = 5
227+ private val handler = android.os.Handler (android.os.Looper .getMainLooper())
248228
249- StreamReadThread (process.inputStream.bufferedReader(), updateLogViewHandler).start()
250- StreamReadThread (process.errorStream.bufferedReader(), updateLogViewHandler).start()
251-
252- process.waitFor()
253- updateLogViewHandler.onExit()
254- } else {
255- updateLogViewHandler.onExit()
256- }
257- } catch (ex: Exception ) {
258- updateLogViewHandler.onExit()
229+ private fun readStreamAsync (reader : BufferedReader ) {
230+ Thread {
231+ reader.forEachLine { line ->
232+ onLogOutput(line)
259233 }
260- }
234+ }.start()
261235 }
262-
263- private class StreamReadThread (private var reader : BufferedReader , private var updateLogViewHandler : UpdateLogViewHandler ) : Thread() {
264- override fun run () {
265- var line: String? = " "
266- while (true ) {
267- line = reader.readLine()
268- if (line == null ) {
269- break
270- } else {
271- updateLogViewHandler.onLogOutput(line)
236+
237+ private fun onLogOutput (log : String ) {
238+ handler.post {
239+ synchronized(rows) {
240+ if (rows.size >= maxLines) {
241+ rows.removeAt(0 )
242+ ignored = true
272243 }
244+ rows.add(log)
245+ binding.startStateText.text = rows.joinToString(" \n " , if (ignored) " ……\n " else " " )
273246 }
274247 }
275248 }
276- }
249+ }
0 commit comments