Skip to content

Commit

Permalink
Merge pull request #1230 from aiselp/setup-v7
Browse files Browse the repository at this point in the history
对话框优化
  • Loading branch information
kkevsekk1 authored Oct 21, 2024
2 parents 5a0cee7 + b621ad8 commit d969052
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 257 deletions.
14 changes: 4 additions & 10 deletions app/src/main/java/com/aiselp/autox/ui/material3/BuildPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
Expand Down Expand Up @@ -57,6 +56,7 @@ import com.aiselp.autox.ui.material3.components.BaseDialog
import com.aiselp.autox.ui.material3.components.BuildCard
import com.aiselp.autox.ui.material3.components.CheckboxOption
import com.aiselp.autox.ui.material3.components.DialogController
import com.aiselp.autox.ui.material3.components.DialogTitle
import com.aiselp.autox.ui.material3.components.InputBox
import com.aiselp.autox.ui.material3.components.M3TopAppBar
import com.google.android.material.dialog.MaterialAlertDialogBuilder
Expand Down Expand Up @@ -408,10 +408,7 @@ fun DialogController.ChooseSignDialog(
BaseDialog(
onDismissRequest = { scope.launch { dismiss() } },
title = {
Text(
text = stringResource(R.string.text_sign_choose),
style = MaterialTheme.typography.titleLarge
)
DialogTitle(title = stringResource(R.string.text_sign_choose))
},
positiveText = "确定",
onPositiveClick = { scope.launch { dismiss(); onKeyStoreChange(current) } },
Expand Down Expand Up @@ -487,7 +484,7 @@ fun BuildingDialog(show: Boolean, model: BuildViewModel, onDismissRequest: () ->
onDismissRequest()
model.isShowBuildSuccessfullyDialog = false
val outApk = model.outApk
if (outApk?.isFile != true){
if (outApk?.isFile != true) {
return
}
MaterialAlertDialogBuilder(context)
Expand Down Expand Up @@ -536,10 +533,7 @@ private fun DialogController.FinishDialog(model: BuildViewModel) {
BaseDialog(
onDismissRequest = { scope.launch { dismiss() } },
title = {
Text(
text = stringResource(R.string.text_alert),
style = MaterialTheme.typography.titleLarge
)
DialogTitle(title = stringResource(R.string.text_alert))
},
positiveText = stringResource(id = R.string.text_save_and_exit),
onPositiveClick = {
Expand Down
20 changes: 7 additions & 13 deletions app/src/main/java/com/aiselp/autox/ui/material3/DrawerPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import coil.compose.rememberAsyncImagePainter
import com.aiselp.autox.ui.material3.components.AlertDialog
import com.aiselp.autox.ui.material3.components.BaseDialog
import com.aiselp.autox.ui.material3.components.DialogController
import com.aiselp.autox.ui.material3.components.DialogTitle
import com.aiselp.autox.ui.material3.components.SettingOptionSwitch
import com.aiselp.autox.ui.material3.components.Watch
import com.stardust.app.GlobalAppContext
Expand Down Expand Up @@ -195,7 +196,7 @@ private fun AccessibilityServiceSwitch() {
}
)
dialog.BaseDialog(onDismissRequest = { scope.launch { dialog.dismiss() } },
title = { Text(text = stringResource(R.string.text_need_to_enable_accessibility_service)) },
title = { DialogTitle(title = stringResource(R.string.text_need_to_enable_accessibility_service)) },
positiveText = stringResource(id = R.string.text_go_to_open),
onPositiveClick = {
scope.launch { dialog.dismiss() }
Expand Down Expand Up @@ -497,10 +498,7 @@ private fun DialogController.ConnectComputerDialog() {
BaseDialog(
onDismissRequest = { scope.launch { dismiss() } },
title = {
Text(
text = stringResource(id = R.string.text_server_address),
style = MaterialTheme.typography.titleLarge
)
DialogTitle(title = stringResource(id = R.string.text_server_address))
},
positiveText = stringResource(id = R.string.ok),
onPositiveClick = {
Expand Down Expand Up @@ -588,10 +586,7 @@ private fun DialogController.TimedTaskSchedulerDialog() {
toast(context, R.string.text_set_successfully)
},
title = {
Text(
text = stringResource(id = R.string.text_switch_timed_task_scheduler),
style = MaterialTheme.typography.titleLarge
)
DialogTitle(title = stringResource(id = R.string.text_switch_timed_task_scheduler))
},
) {
Column {
Expand Down Expand Up @@ -670,12 +665,11 @@ private fun CheckForUpdate(model: DrawerViewModel = viewModel()) {
dialog.BaseDialog(
onDismissRequest = { scope.launch { dialog.dismiss() } },
title = {
Text(
text = stringResource(
DialogTitle(
title = stringResource(
R.string.text_new_version2,
model.githubReleaseInfo!!.name
),
style = MaterialTheme.typography.titleMedium
)
)
},
positiveText = stringResource(id = R.string.text_download),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.aiselp.autox.apkbuilder.ApkKeyStore
import com.aiselp.autox.ui.material3.components.BaseDialog
import com.aiselp.autox.ui.material3.components.DialogController
import com.aiselp.autox.ui.material3.components.DialogTitle
import com.aiselp.autox.ui.material3.components.DropdownSingleChoiceInputBox
import com.aiselp.autox.ui.material3.components.PasswordInputBox
import com.mcal.apksigner.CertCreator
Expand All @@ -54,7 +55,7 @@ fun DialogController.ApkSignDeleteDialog(apkKeyStore: ApkKeyStore) {
val signManageModel = viewModel(SignManageModel::class.java)

BaseDialog(onDismissRequest = { scope.launch { dismiss() } }, title = {
Text(text = "删除签名", style = MaterialTheme.typography.titleLarge)
DialogTitle(title = "删除签名")
}, content = {
Column {
Text(text = "你确定要删除该文件?")
Expand All @@ -81,7 +82,7 @@ fun DialogController.ReviseSignKeyDialog(apkKeyStore: ApkKeyStore) {
var keyStore: KeyStore? = null

BaseDialog(onDismissRequest = { scope.launch { dismiss() } }, title = {
Text(text = "修改签名文件", style = MaterialTheme.typography.titleLarge)
DialogTitle(title = "修改签名文件")
}, positiveText = "确定", onPositiveClick = {
scope.launch {
if (alias.isNullOrEmpty() || keyStorePassword.isNullOrEmpty() || password.isNullOrEmpty()) {
Expand Down Expand Up @@ -204,7 +205,7 @@ fun DialogController.ApkSignCreateDialog() {
val context = LocalContext.current

BaseDialog(onDismissRequest = { scope.launch { dismiss() } }, title = {
Text(text = "创建签名文件", style = MaterialTheme.typography.titleLarge)
DialogTitle(title = "创建签名文件")
}, content = { SignKeyCreatePage() }, positiveText = "确定", onPositiveClick = {
if (!model.checkAll()) {
return@BaseDialog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class NodeScriptEngine(val context: Context, val uiHandler: UiHandler) :
private fun initializeModule(file: File): V8Value {
val parentFile = file.parentFile ?: File("/")
runtime.getNodeModule(NodeModuleProcess::class.java).workingDirectory = parentFile
val nodeModuleResolver = NodeModuleResolver(runtime, parentFile, moduleDirectory)
val nodeModuleResolver = NodeModuleResolver(runtime, file, moduleDirectory)
runtime.v8ModuleResolver = nodeModuleResolver
runtime.globalObject.delete(NodeModuleModule.PROPERTY_REQUIRE)
return if (NodeModuleResolver.isEsModule(file)) {
Expand Down
14 changes: 14 additions & 0 deletions autojs/src/main/java/com/aiselp/autox/module/ModuleFile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.aiselp.autox.module

import java.io.File

class ModuleFile(
val file: File,
val modelType: ModelType,
) {
enum class ModelType {
COMMONJS,
ES_MODULE,
JSON,
}
}
92 changes: 69 additions & 23 deletions autojs/src/main/java/com/aiselp/autox/module/NodeModuleResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,39 @@ import java.net.URI

class NodeModuleResolver(
val runtime: NodeRuntime,
val workingDirectory: File,
mainFile: File,
private val globalModuleDirectory: File
) : IV8ModuleResolver {
val workingDirectory: File = mainFile.parentFile ?: File("/")
private val esModuleCache = mutableMapOf<String, IV8Module>()
val require: V8ValueFunction = runtime.getNodeModule(NodeModuleModule::class.java).moduleObject
.invoke(
NodeModuleModule.FUNCTION_CREATE_REQUIRE,
workingDirectory.absolutePath
)
private val requireCache = mutableMapOf<String, V8ValueFunction>()
val require: V8ValueFunction = createRequire(mainFile.absolutePath)
private val loadFn: V8ValueFunction by lazy {
runtime.getExecutor(
"""
(require,filename)=>{
const model = require(filename);
if (model instanceof Object) {
Object.defineProperty(model, 'default', {
value: model,
writable: false,
enumerable: true
})
return model
} else {
return { default:model }
}
}
""".trimIndent()
).execute()
}

private fun createRequire(referrer: String): V8ValueFunction {
return requireCache.getOrPut(referrer) {
runtime.getNodeModule(NodeModuleModule::class.java).moduleObject
.invoke(NodeModuleModule.FUNCTION_CREATE_REQUIRE, referrer)
}
}

override fun resolve(
v8Runtime: V8Runtime, resourceName: String, v8ModuleReferrer: IV8Module
Expand All @@ -40,11 +64,7 @@ class NodeModuleResolver(
return when (uri.scheme) {
"node" -> loadNodeModule(resourceName)
null -> run {
try {
loadNodeModule("node:$resourceName")
} catch (e: Exception) {
parsingPackageModule(v8Runtime, workingDirectory, resourceName)
}
parsingPackageModule(v8Runtime, resourceName, v8ModuleReferrer.resourceName)
}

else -> parsingModule(v8Runtime, uri)
Expand All @@ -66,7 +86,8 @@ class NodeModuleResolver(
private fun parsingModule(v8Runtime: V8Runtime, uri: Uri): IV8Module? =
checkCacheModule(uri.toString()) {
if (uri.scheme == null || uri.scheme == "file") {
return@checkCacheModule parsingModule(v8Runtime, File(uri.path!!))
val path = uri.path!!
return@checkCacheModule parsingModule(v8Runtime, File(path))
}
return@checkCacheModule null
}
Expand All @@ -89,22 +110,47 @@ class NodeModuleResolver(
): IV8Module? {
if (directory == null) return null
if (!directory.isDirectory) return null
val packageDirectory = File(directory, name)
return PackageJson.create(packageDirectory)?.let {
it.main?.let { file -> parsingModule(v8Runtime, file, it.isEsModule()) }
val split = name.split("/")
val subPath = mutableListOf<String>()
var sufPath = ""

var packageJson: PackageJson? = null
split.forEach {
if (packageJson != null) {
subPath.add(it)
return@forEach
}
sufPath += "$it/"
packageJson = PackageJson.create(File(directory, sufPath))
}
return packageJson?.let {
it.resolveSubPath(
if (subPath.isEmpty()) "."
else subPath.joinToString("/"), true
)?.let { modelFile ->
parsingModule(
v8Runtime, modelFile.file, modelFile.modelType == ModuleFile.ModelType.ES_MODULE
)
}
}
}

private fun loadNpmPackageModule(require: V8ValueFunction, name: String): IV8Module? =
checkCacheModule(name) {
try {
runtime.createV8Module(name, loadFn.call<V8ValueObject>(null, require, name))
} catch (e: Exception) {
Log.e(TAG, "loadNpmPackageModule Error:\n ${e.stackTraceToString()}")
null
}
}

private fun parsingPackageModule(
v8Runtime: V8Runtime, workingDirectory: File?, name: String
v8Runtime: V8Runtime, name: String, referrer: String
): IV8Module? {
if (workingDirectory == null) return null
val modulesDirection = File(workingDirectory, "node_modules")
return loadPackageModule(v8Runtime, modulesDirection, name) ?: loadPackageModule(
v8Runtime,
globalModuleDirectory,
name
)
val require1 = createRequire(referrer)
return loadNpmPackageModule(require1, name)
?: loadPackageModule(v8Runtime, globalModuleDirectory, name)
}

fun addCacheModule(module: IV8Module) {
Expand Down
55 changes: 41 additions & 14 deletions autojs/src/main/java/com/aiselp/autox/module/PackageJson.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,62 @@ package com.aiselp.autox.module

import com.google.gson.Gson
import java.io.File
import java.net.URI

class PackageJson(val dir: File, private val data: Map<String, Any?>) {

val exports: Map<String, String>? by lazy {
val exports: Map<String, Any?>? by lazy {
when (data["exports"]) {
null -> null
is String -> mapOf("." to data["exports"] as String)
else -> {
val map = mutableMapOf<String, String>()
for ((k, v) in data["exports"] as Map<String, Any?>) {
if (v is String) map[k] = v
}
map
data["exports"] as Map<String, Any?>
}
}
}

val main: File?
val main: ModuleFile?
get() {
val main = exports?.let {
(if (isEsModule()) {
it["require"]
} else it["import"]) ?: it["."]
} ?: (data["main"] as? String) ?: (data["module"] as? String)
return main?.let {
File(dir, it.replace(Regex("^\\./"), ""))
return if (exports == null) {
data["main"]?.let { createModuleFile(it as String) }
} else resolveSubPath(".", false)
}

private fun createModuleFile(subPath: String, import: Boolean = isEsModule()): ModuleFile {
return ModuleFile(
File(URI(dir.path + "/#").resolve(subPath).path),
if (import) ModuleFile.ModelType.ES_MODULE
else ModuleFile.ModelType.COMMONJS
)
}

fun resolveSubPath(subPath: String, import: Boolean): ModuleFile? {
val e = exports ?: return main
val c = e[subPath]

fun select(m: Map<*, *>): ModuleFile? {
val es = (m["import"] as? String)?.let {
createModuleFile(it, true)
}
val commonjs = (m["require"] as? String)?.let {
createModuleFile(it, false)
}
return if (import) {
es ?: commonjs
} else commonjs ?: es
}
if (subPath == ".") {
if (c == null) {
return select(e)
}
}

if (c is String) return createModuleFile(c)
return if (c is Map<*, *>) {
select(c)
} else null
}

fun isEsModule(): Boolean {
return data["type"] == "module"
}
Expand Down
2 changes: 0 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ buildscript {
classpath("com.android.tools.build:gradle:8.2.1")
classpath(kotlin("gradle-plugin", version = kotlin_version))
classpath("com.jakewharton:butterknife-gradle-plugin:10.2.3")
classpath("org.codehaus.groovy:groovy-json:3.0.8")
classpath("com.yanzhenjie.andserver:plugin:2.1.12")
classpath(libs.okhttp)
}
}
Expand Down
3 changes: 0 additions & 3 deletions codeeditor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import okio.use
plugins {
id("com.android.library")
id("kotlin-android")
id("com.yanzhenjie.andserver")
id("kotlin-kapt")
}

Expand Down Expand Up @@ -44,9 +43,7 @@ android {

dependencies {
implementation(platform(libs.compose.bom))
implementation(libs.andserver.api)
implementation(libs.androidx.constraintlayout)
kapt(libs.andserver.processor)
implementation(libs.kotlinx.coroutines.android)
api(libs.androidx.webkit)
implementation(libs.google.gson)
Expand Down
Loading

0 comments on commit d969052

Please sign in to comment.