Skip to content

Commit 27e5851

Browse files
committed
Update
1 parent a6d6274 commit 27e5851

4 files changed

Lines changed: 55 additions & 5 deletions

File tree

app/src/main/java/com/omarea/krscript/config/PageConfigReader.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,14 @@ class PageConfigReader {
436436
"mime" -> {
437437
option.mime = parser.getAttributeValue(i).lowercase(getDefault())
438438
}
439-
"checked-sh", "checkbox-sh", "check-sh" -> {
440-
// Chỉ dùng khi type="checkbox": lệnh shell sẽ được chạy lại mỗi lần
441-
// mở menu để xác định có hiện dấu tích hay không (KHÔNG dùng để ẩn/hiện
442-
// item như "visible"/"support" - đó là 2 cơ chế độc lập).
439+
"box", "visible", "check" -> {
443440
option.checkedSh = parser.getAttributeValue(i)
444441
}
442+
"silent", "hidden" -> {
443+
// Khi click, chạy script ẩn ở nền, không hiện dialog log cho người dùng thấy.
444+
val attrValue = parser.getAttributeValue(i)
445+
option.silent = attrValue.isEmpty() || attrValue == "silent" || attrValue == "hidden" || attrValue == "true" || attrValue == "1"
446+
}
445447
}
446448
}
447449
option.title = resolveStringRes(parser.nextText())

app/src/main/java/com/omarea/krscript/executor/ScriptEnvironmen.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.ArrayList;
2424
import java.util.HashMap;
2525
import java.util.Locale;
26+
import java.util.Map;
2627
import android.provider.Settings;
2728
import android.content.res.Configuration;
2829
import android.content.res.Resources;
@@ -152,6 +153,12 @@ private static String extractScript(Context context, String fileName) {
152153
}
153154

154155
public static String executeResultRoot(Context context, String script, NodeInfoBase nodeInfoBase) {
156+
return executeResultRoot(context, script, nodeInfoBase, null);
157+
}
158+
159+
// Overload cho phép truyền thêm các biến môi trường tuỳ chỉnh (ví dụ: state, menu_id)
160+
// - dùng khi cần chạy 1 lệnh/menu item với ngữ cảnh riêng mà không cần hiển thị dialog log.
161+
public static String executeResultRoot(Context context, String script, NodeInfoBase nodeInfoBase, HashMap<String, String> extraParams) {
155162
if (!inited) {
156163
init(context);
157164
}
@@ -190,6 +197,13 @@ public static String executeResultRoot(Context context, String script, NodeInfoB
190197
}
191198
}
192199

200+
if (extraParams != null) {
201+
for (Map.Entry<String, String> entry : extraParams.entrySet()) {
202+
String value = entry.getValue() == null ? "" : entry.getValue().replace("'", "'\\''");
203+
stringBuilder.append("export ").append(entry.getKey()).append("='").append(value).append("'\n");
204+
}
205+
}
206+
193207
stringBuilder.append("\n\n");
194208
stringBuilder.append(environmentPath + " \"" + path + "\"");
195209
if (shellTranslation != null) {

app/src/main/java/com/omarea/krscript/model/PageMenuOption.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ class PageMenuOption(currentConfigXml: String) : RunnableNode(currentConfigXml)
2020
// Trạng thái tích hiện tại - được cập nhật ở background thread (IO), chỉ đọc khi vẽ menu (Main thread).
2121
@Volatile
2222
var checked: Boolean = false
23+
// Nếu true: khi click, chạy script ẩn ở nền (không hiện dialog log/không cho người dùng thấy output)
24+
var silent: Boolean = false
2325
}

app/src/main/java/com/tool/tree/ActionPage.kt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,39 @@ class ActionPage : AppCompatActivity() {
241241
"killapp" -> killApp()
242242
"file" -> menuItemChooseFile(menuOption)
243243
else -> {
244-
menuItemExecute(menuOption, hashMapOf("state" to menuOption.key, "menu_id" to menuOption.key))
244+
if (menuOption.silent) {
245+
menuItemExecuteSilent(menuOption)
246+
} else {
247+
menuItemExecute(menuOption, hashMapOf("state" to menuOption.key, "menu_id" to menuOption.key))
248+
}
249+
}
250+
}
251+
}
252+
253+
// Chạy script của menu item ở NỀN (IO thread), không hiện DialogLogFragment/không có output hiển thị.
254+
// Vẫn tôn trọng các cờ auto-finish/reload-page/auto-kill/auto-restart sau khi script chạy xong.
255+
private fun menuItemExecuteSilent(menuOption: PageMenuOption) {
256+
val config = currentPageConfig ?: return
257+
val extraParams = hashMapOf("state" to menuOption.key, "menu_id" to menuOption.key)
258+
259+
lifecycleScope.launch(Dispatchers.IO) {
260+
ScriptEnvironmen.executeResultRoot(this@ActionPage, config.pageHandlerSh, config, extraParams)
261+
262+
if (!isActive) return@launch
263+
264+
withContext(Dispatchers.Main) {
265+
if (isFinishing) return@withContext
266+
when {
267+
menuOption.autoFinish -> finish()
268+
menuOption.reloadPage -> recreate()
269+
menuOption.autoKill -> killApp()
270+
menuOption.autoRestart -> restartApp()
271+
}
272+
// Nếu là menu checkbox, cập nhật lại dấu tích ngay sau khi script chạy xong
273+
// (không cần đợi người dùng mở lại menu).
274+
if (menuOption.type == "checkbox") {
275+
refreshCheckboxMenuStates()
276+
}
245277
}
246278
}
247279
}

0 commit comments

Comments
 (0)