Skip to content

Commit

Permalink
refactor: add canStart to RimeWrapper to prevent auto startup
Browse files Browse the repository at this point in the history
  • Loading branch information
goofyz committed Dec 30, 2023
1 parent 44d8e0a commit 3ee23b7
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions app/src/main/java/com/osfans/trime/ime/core/RimeWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,42 @@ object RimeWrapper {
private val _statusStateFlow = MutableStateFlow(Status.UN_INIT)
val statusStateFlow = _statusStateFlow.asStateFlow()

var canStart = false

fun startup(r: Runnable? = null) {
r.let {
myThreadSafeList.add(it)
}
if (mutex.tryLock()) {
if (_statusStateFlow.value == Status.UN_INIT) {
Timber.d("Starting in a thread")
_statusStateFlow.value = Status.IN_PROGRESS
mutex.unlock()

val scope = CoroutineScope(Dispatchers.IO)
scope.launch {
measureTimeMillis {
Rime.getInstance(false)
Theme.get()
}.also {
Timber.d("Startup completed. It takes ${it / 1000} seconds")
}
if (canStart) {
if (mutex.tryLock()) {
if (_statusStateFlow.value == Status.UN_INIT) {
Timber.d("Starting in a thread")
_statusStateFlow.value = Status.IN_PROGRESS
mutex.unlock()

mutex.withLock {
_statusStateFlow.value = Status.READY
}
val scope = CoroutineScope(Dispatchers.IO)
scope.launch {
measureTimeMillis {
Rime.getInstance(false)
Theme.get()
}.also {
Timber.d("Startup completed. It takes ${it / 1000} seconds")
}

notifyUnlock()
mutex.withLock {
_statusStateFlow.value = Status.READY
}

notifyUnlock()
}
} else {
mutex.unlock()
}
} else {
mutex.unlock()
}
}
} else
{
Timber.d("RimeWrapper shall not be started")
}
}

suspend fun deploy(): Boolean {
Expand Down Expand Up @@ -83,13 +90,16 @@ object RimeWrapper {
mainThreadHandler.post(it)
}
}
Timber.d("Unlock totally Completed")
Timber.d("Unlock Run Completed")
}

fun runCheck() {
if (isReady()) {
notifyUnlock()
}
} else if (_statusStateFlow.value == Status.UN_INIT)
{
startup()
}
}

fun isReady(): Boolean {
Expand Down

0 comments on commit 3ee23b7

Please sign in to comment.