Skip to content

Commit d531217

Browse files
committed
Upload file
1 parent c60f555 commit d531217

3 files changed

Lines changed: 81 additions & 54 deletions

File tree

pio/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ android {
1919
resourceConfigurations += ['zh', 'vi', 'ru', 'hu']
2020
minSdkVersion 23
2121
targetSdkVersion 28
22-
// versionCode Integer.parseInt(new java.text.SimpleDateFormat("yyMMddHH").format(new Date()))
23-
versionCode 26021101
22+
versionCode Integer.parseInt(new java.text.SimpleDateFormat("yyMMddHH").format(new Date()))
23+
// versionCode 26021101
2424
versionName "1.3.0"
2525
}
2626
signingConfigs {

pio/src/main/java/com/projectkr/shell/SplashActivity.kt

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

pio/src/main/java/com/projectkr/shell/SplashActivity.kt.bak

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import java.io.BufferedReader
3232
import java.io.DataOutputStream
3333
import java.io.File
3434
import java.util.Locale
35+
import android.os.Looper
3536

3637
class 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!!) }
@@ -268,24 +271,28 @@ class SplashActivity : AppCompatActivity() {
268271
"pio-splash"
269272
)
270273

271-
StreamReadThread(
274+
val outThread = StreamReadThread(
272275
process.inputStream.bufferedReader(),
273276
updateHandler
274-
).start()
277+
)
275278

276-
StreamReadThread(
279+
val errThread = StreamReadThread(
277280
process.errorStream.bufferedReader(),
278281
updateHandler
279-
).start()
282+
)
283+
284+
outThread.start()
285+
errThread.start()
280286

281287
process.waitFor()
288+
289+
outThread.join()
290+
errThread.join()
282291
}
283292

284293
} catch (e: Exception) {
285294
e.printStackTrace()
286295
}
287-
288-
// Quan trọng: giống code cũ
289296
updateHandler.onExit()
290297
}
291298
}

0 commit comments

Comments
 (0)