6.7.0 - Alpha23 - 替换 Kotlin 中的 Object() 锁为 Any() 或 CountDownLatch

This commit is contained in:
SuperMonster003
2026-03-07 21:17:21 +08:00
parent 528f3e59dc
commit 9190b2b605
7 changed files with 56 additions and 69 deletions

View File

@@ -9,6 +9,7 @@ import org.autojs.autojs.rhino.AutoJsContext
import org.autojs.autojs.runtime.ScriptRuntime import org.autojs.autojs.runtime.ScriptRuntime
import org.autojs.autojs.util.RhinoUtils.isMainThread import org.autojs.autojs.util.RhinoUtils.isMainThread
import org.mozilla.javascript.Context import org.mozilla.javascript.Context
import java.util.concurrent.CountDownLatch
import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.CopyOnWriteArrayList
import kotlin.concurrent.Volatile import kotlin.concurrent.Volatile
@@ -28,7 +29,7 @@ class Loopers(val scriptRuntime: ScriptRuntime) : MessageQueue.IdleHandler {
private var mLooperQuitHandlers = ThreadLocal<CopyOnWriteArrayList<LooperQuitHandler>>() private var mLooperQuitHandlers = ThreadLocal<CopyOnWriteArrayList<LooperQuitHandler>>()
private var mTimers = scriptRuntime.timers private var mTimers = scriptRuntime.timers
private var mThreads = scriptRuntime.threads private var mThreads = scriptRuntime.threads
private val mLock = Object() private val mServantLooperReady = CountDownLatch(1)
private var mMaxWaitId: ThreadLocal<Int> = object : ThreadLocal<Int>() { private var mMaxWaitId: ThreadLocal<Int> = object : ThreadLocal<Int>() {
override fun initialValue() = 0 override fun initialValue() = 0
@@ -44,18 +45,12 @@ class Loopers(val scriptRuntime: ScriptRuntime) : MessageQueue.IdleHandler {
get() { get() {
if (mServantLooper == null) { if (mServantLooper == null) {
initServantThread() initServantThread()
synchronized(mLock) {
try { try {
try { mServantLooperReady.await()
mLock.wait() } catch (_: InterruptedException) {
} finally {
mLock.notify()
}
} catch (ex: InterruptedException) {
/* Ignored. */ /* Ignored. */
} }
} }
}
return mServantLooper!! return mServantLooper!!
} }
@@ -160,9 +155,7 @@ class Loopers(val scriptRuntime: ScriptRuntime) : MessageQueue.IdleHandler {
ThreadCompat { ThreadCompat {
Looper.prepare() Looper.prepare()
mServantLooper = Looper.myLooper() mServantLooper = Looper.myLooper()
synchronized(mLock) { mServantLooperReady.countDown()
mLock.notifyAll()
}
Looper.loop() Looper.loop()
}.start() }.start()
} }

View File

@@ -5,6 +5,8 @@ import com.google.mlkit.vision.barcode.BarcodeScannerOptions
import com.google.mlkit.vision.barcode.BarcodeScanning import com.google.mlkit.vision.barcode.BarcodeScanning
import com.google.mlkit.vision.common.InputImage import com.google.mlkit.vision.common.InputImage
import org.autojs.autojs.core.image.ImageWrapper import org.autojs.autojs.core.image.ImageWrapper
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import com.google.mlkit.vision.barcode.common.Barcode as MLKitBarcode import com.google.mlkit.vision.barcode.common.Barcode as MLKitBarcode
/** /**
@@ -23,6 +25,7 @@ class Barcode {
val scanner = BarcodeScanning.getClient(options) val scanner = BarcodeScanning.getClient(options)
val inputImage = InputImage.fromBitmap(image.bitmap, 0) val inputImage = InputImage.fromBitmap(image.bitmap, 0)
val scanCompletedSignal = CountDownLatch(1)
var results: List<WrappedBarcode>? = null var results: List<WrappedBarcode>? = null
@@ -32,24 +35,22 @@ class Barcode {
onlyOneResult -> barcodes.firstOrNull()?.let { results = listOf(WrappedBarcode(it)) } onlyOneResult -> barcodes.firstOrNull()?.let { results = listOf(WrappedBarcode(it)) }
else -> results = barcodes.map { WrappedBarcode(it) } else -> results = barcodes.map { WrappedBarcode(it) }
} }
lockNotify() scanCompletedSignal.countDown()
} }
.addOnFailureListener { e -> .addOnFailureListener { e ->
e.printStackTrace() e.printStackTrace()
lockNotify() scanCompletedSignal.countDown()
} }
.addOnCanceledListener { lockNotify() } .addOnCanceledListener { scanCompletedSignal.countDown() }
.addOnCompleteListener { lockNotify() } .addOnCompleteListener { scanCompletedSignal.countDown() }
while (!resultScan.isComplete) { while (!resultScan.isComplete) {
synchronized(lock) {
try { try {
lock.wait(50) scanCompletedSignal.await(50, TimeUnit.MILLISECONDS)
} catch (_: InterruptedException) { } catch (_: InterruptedException) {
/* Ignored. */ /* Ignored. */
} }
} }
}
image.shoot() image.shoot()
@@ -59,12 +60,8 @@ class Barcode {
return results ?: emptyList() return results ?: emptyList()
} }
private fun lockNotify() = synchronized(lock) { lock.notify() }
companion object { companion object {
private val lock = Object()
private val TAG: String = Companion::class.java.simpleName private val TAG: String = Companion::class.java.simpleName
const val TYPE_NAME_UNKNOWN = "TYPE_NAME_UNKNOWN" const val TYPE_NAME_UNKNOWN = "TYPE_NAME_UNKNOWN"

View File

@@ -6,6 +6,8 @@ import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.TextRecognizer import com.google.mlkit.vision.text.TextRecognizer
import com.google.mlkit.vision.text.chinese.ChineseTextRecognizerOptions import com.google.mlkit.vision.text.chinese.ChineseTextRecognizerOptions
import org.autojs.autojs.core.image.ImageWrapper import org.autojs.autojs.core.image.ImageWrapper
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
/** /**
* Created by SuperMonster003 on Mar 18, 2023. * Created by SuperMonster003 on Mar 18, 2023.
@@ -34,24 +36,23 @@ class OcrMLKit {
if (bitmap.isRecycled) return emptyList<OcrResult>().also { image.shoot() } if (bitmap.isRecycled) return emptyList<OcrResult>().also { image.shoot() }
val inputImage = InputImage.fromBitmap(bitmap, 0) val inputImage = InputImage.fromBitmap(bitmap, 0)
val detectCompletedSignal = CountDownLatch(1)
val result = recognizer!!.process(inputImage) val result = recognizer!!.process(inputImage)
.addOnCanceledListener { lockNotify() } .addOnCanceledListener { detectCompletedSignal.countDown() }
.addOnCompleteListener { lockNotify() } .addOnCompleteListener { detectCompletedSignal.countDown() }
.addOnSuccessListener { lockNotify() } .addOnSuccessListener { detectCompletedSignal.countDown() }
.addOnFailureListener { e: Exception -> .addOnFailureListener { e: Exception ->
Log.w(TAG, "Failed to detect: ${e.message}") Log.w(TAG, "Failed to detect: ${e.message}")
e.printStackTrace() e.printStackTrace()
lockNotify() detectCompletedSignal.countDown()
} }
while (!result.isComplete) { while (!result.isComplete) {
synchronized(lock) {
try { try {
lock.wait(50) detectCompletedSignal.await(50, TimeUnit.MILLISECONDS)
} catch (_: InterruptedException) { } catch (_: InterruptedException) {
/* Ignored. */ /* Ignored. */
} }
} }
}
image.shoot() image.shoot()
if (!result.isSuccessful) { if (!result.isSuccessful) {
Log.w(TAG, "Detection is not successful") Log.w(TAG, "Detection is not successful")
@@ -77,12 +78,8 @@ class OcrMLKit {
} }
} }
private fun lockNotify() = synchronized(lock) { lock.notify() }
companion object { companion object {
private val lock = Object()
private val TAG: String = Companion::class.java.simpleName private val TAG: String = Companion::class.java.simpleName
} }

View File

@@ -48,23 +48,18 @@ class Plugins(private val context: Context, private val runtime: PluginRuntime)
) )
packages.putIfAbsent(packageName, packageContext)?.let { packageContext = it } packages.putIfAbsent(packageName, packageContext)?.let { packageContext = it }
val lock = Object() return try {
synchronized(lock) {
try {
val plugin = Plugin.load(context, packageContext, runtime, runtime.topLevelScope) val plugin = Plugin.load(context, packageContext, runtime, runtime.topLevelScope)
val scriptCacheDir = getScriptCacheDir(packageName) val scriptCacheDir = getScriptCacheDir(packageName)
copyAssetDir(packageContext.context.assets, plugin.assetsScriptDir, scriptCacheDir.path) copyAssetDir(packageContext.context.assets, plugin.assetsScriptDir, scriptCacheDir.path)
plugin.mainScriptPath = File(scriptCacheDir, "index.js").path plugin.mainScriptPath = File(scriptCacheDir, "index.js").path
bindService(plugin) bindService(plugin)
mPlugins[packageName] = plugin mPlugins[packageName] = plugin
lock.notify() plugin
return plugin
} catch (e: Exception) { } catch (e: Exception) {
lock.notify()
throw PluginLoadException(e) throw PluginLoadException(e)
} }
} }
}
private fun bindService(plugin: Plugin) { private fun bindService(plugin: Plugin) {
if (plugin.version < 2) return if (plugin.version < 2) return

View File

@@ -42,7 +42,7 @@ class ScriptPromiseAdapter : Resolvable {
} }
companion object { companion object {
private val UNSET = Object() private val UNSET = Any()
} }
} }

View File

@@ -11,6 +11,7 @@ import org.autojs.autojs.util.ClipboardUtils
import org.autojs.autojs.util.RootUtils import org.autojs.autojs.util.RootUtils
import org.autojs.autojs.util.ViewUtils import org.autojs.autojs.util.ViewUtils
import org.autojs.autojs6.R import org.autojs.autojs6.R
import java.util.concurrent.CountDownLatch
interface CommandBasedPermissionItemHelper : PermissionItemHelper, IPermissionRootItem, IPermissionShizukuItem, IPermissionAdbItem { interface CommandBasedPermissionItemHelper : PermissionItemHelper, IPermissionRootItem, IPermissionShizukuItem, IPermissionAdbItem {
@@ -92,17 +93,21 @@ interface CommandBasedPermissionItemHelper : PermissionItemHelper, IPermissionRo
} }
fun withAdb(action: ACTION): Boolean { fun withAdb(action: ACTION): Boolean {
val lock = Object() val dialogDismissSignal = CountDownLatch(1)
AdbDialogBuilder(context, getCommand(action)) AdbDialogBuilder(context, getCommand(action))
.setChecker(object : AdbDialogBuilder.Checker { .setChecker(object : AdbDialogBuilder.Checker {
override fun check() = has() override fun check() = has()
}) })
.build() .build()
.dismissListener { synchronized(lock) { lock.notify() } } .dismissListener { dialogDismissSignal.countDown() }
.let { Handler(Looper.getMainLooper()).post { it.show() } } .let { Handler(Looper.getMainLooper()).post { it.show() } }
synchronized(lock) { lock.wait() } try {
dialogDismissSignal.await()
} catch (_: InterruptedException) {
/* Ignored. */
}
return has() return has()
} }

View File

@@ -1,5 +1,5 @@
#Sat Mar 07 19:14:09 CST 2026 #Sat Mar 07 21:17:02 CST 2026
BUILD_TIME=1772882049270 BUILD_TIME=1772889422226
COMPILE_SDK_VERSION=36 COMPILE_SDK_VERSION=36
IMAGE_QUANT_CMAKE_VERSION=3.22.1 IMAGE_QUANT_CMAKE_VERSION=3.22.1
IMAGE_QUANT_NDK_VERSION=26.1.10909125 IMAGE_QUANT_NDK_VERSION=26.1.10909125
@@ -27,6 +27,6 @@ RAPID_OCR_OPENCV_MOBILE_LABEL_VERSION=13
RAPID_OCR_OPENCV_MOBILE_VERSION=4.5.3 RAPID_OCR_OPENCV_MOBILE_VERSION=4.5.3
TARGET_SDK_VERSION=36 TARGET_SDK_VERSION=36
TARGET_SDK_VERSION_INRT=29 TARGET_SDK_VERSION_INRT=29
VERSION_BUILD=3780 VERSION_BUILD=3781
VERSION_NAME=6.7.0 Alpha23 VERSION_NAME=6.7.0 Alpha23
VSCODE_EXT_REQUIRED_VERSION=1.0.13 VSCODE_EXT_REQUIRED_VERSION=1.0.13