Skip to content

Commit

Permalink
chore: format code with ktlint 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Bambooin committed Oct 31, 2023
1 parent e66cd6f commit e236272
Show file tree
Hide file tree
Showing 73 changed files with 2,372 additions and 1,660 deletions.
69 changes: 42 additions & 27 deletions app/src/main/java/com/osfans/trime/TrimeApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class TrimeApplication : Application() {
private var instance: TrimeApplication? = null
private var lastPid: Int? = null

fun getInstance() =
instance ?: throw IllegalStateException("Trime application is not created!")
fun getInstance() = instance ?: throw IllegalStateException("Trime application is not created!")

fun getLastPid() = lastPid

private const val MAX_STACKTRACE_SIZE = 128000
}

Expand All @@ -40,13 +40,14 @@ class TrimeApplication : Application() {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
putExtra(LogActivity.FROM_CRASH, true)
// avoid transaction overflow
val truncated = e.stackTraceToString().let {
if (it.length > MAX_STACKTRACE_SIZE) {
it.take(MAX_STACKTRACE_SIZE) + "<truncated>"
} else {
it
val truncated =
e.stackTraceToString().let {
if (it.length > MAX_STACKTRACE_SIZE) {
it.take(MAX_STACKTRACE_SIZE) + "<truncated>"
} else {
it
}
}
}
putExtra(LogActivity.CRASH_STACK_TRACE, truncated)
},
)
Expand All @@ -60,27 +61,41 @@ class TrimeApplication : Application() {
initDefaultPreferences()
}
if (BuildConfig.DEBUG) {
Timber.plant(object : Timber.DebugTree() {
override fun createStackElementTag(element: StackTraceElement): String {
return "${super.createStackElementTag(element)}|${element.fileName}:${element.lineNumber}"
}
Timber.plant(
object : Timber.DebugTree() {
override fun createStackElementTag(element: StackTraceElement): String {
return "${super.createStackElementTag(element)}|${element.fileName}:${element.lineNumber}"
}

override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
super.log(
priority,
"[${Thread.currentThread().name}] ${tag?.substringBefore('|')}",
"${tag?.substringAfter('|')}] $message",
t,
)
}
})
override fun log(
priority: Int,
tag: String?,
message: String,
t: Throwable?,
) {
super.log(
priority,
"[${Thread.currentThread().name}] ${tag?.substringBefore('|')}",
"${tag?.substringAfter('|')}] $message",
t,
)
}
},
)
} else {
Timber.plant(object : Timber.Tree() {
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (priority < Log.INFO) return
Log.println(priority, "[${Thread.currentThread().name}]", message)
}
})
Timber.plant(
object : Timber.Tree() {
override fun log(
priority: Int,
tag: String?,
message: String,
t: Throwable?,
) {
if (priority < Log.INFO) return
Log.println(priority, "[${Thread.currentThread().name}]", message)
}
},
)
}
// record last pid for crash logs
val appPrefs = AppPrefs.defaultInstance()
Expand Down
38 changes: 27 additions & 11 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ class Rime(fullCheck: Boolean) {
private var mContext: RimeContext? = null
private var mStatus: RimeStatus? = null
private var isHandlingRimeNotification = false
private val notificationFlow_ = MutableSharedFlow<RimeNotification>(
extraBufferCapacity = 15,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)
private val notificationFlow_ =
MutableSharedFlow<RimeNotification>(
extraBufferCapacity = 15,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)

init {
System.loadLibrary("rime_jni")
Expand Down Expand Up @@ -175,13 +176,16 @@ class Rime(fullCheck: Boolean) {

@JvmStatic
fun isVoidKeycode(keycode: Int): Boolean {
val XK_VoidSymbol = 0xffffff
return keycode <= 0 || keycode == XK_VoidSymbol
val voidSymbol = 0xffffff
return keycode <= 0 || keycode == voidSymbol
}

// KeyProcess 调用JNI方法发送keycode和mask
@JvmStatic
fun processKey(keycode: Int, mask: Int): Boolean {
fun processKey(
keycode: Int,
mask: Int,
): Boolean {
if (isVoidKeycode(keycode)) return false
Timber.d("processKey: keyCode=$keycode, mask=$mask")
return processRimeKey(keycode, mask).also {
Expand Down Expand Up @@ -250,7 +254,10 @@ class Rime(fullCheck: Boolean) {
}

@JvmStatic
fun setOption(option: String, value: Boolean) {
fun setOption(
option: String,
value: Boolean,
) {
if (isHandlingRimeNotification) return
setRimeOption(option, value)
}
Expand Down Expand Up @@ -306,7 +313,10 @@ class Rime(fullCheck: Boolean) {

// input
@JvmStatic
external fun processRimeKey(keycode: Int, mask: Int): Boolean
external fun processRimeKey(
keycode: Int,
mask: Int,
): Boolean

@JvmStatic
external fun commitRimeComposition(): Boolean
Expand All @@ -326,7 +336,10 @@ class Rime(fullCheck: Boolean) {

// runtime options
@JvmStatic
external fun setRimeOption(option: String, value: Boolean)
external fun setRimeOption(
option: String,
value: Boolean,
)

@JvmStatic
external fun getRimeOption(option: String): Boolean
Expand Down Expand Up @@ -407,7 +420,10 @@ class Rime(fullCheck: Boolean) {
external fun selectRimeSchemas(schemaIds: Array<String>): Boolean

@JvmStatic
external fun getRimeStateLabel(optionName: String, state: Boolean): String?
external fun getRimeStateLabel(
optionName: String,
state: Boolean,
): String?

/** call from rime_jni */
@JvmStatic
Expand Down
17 changes: 9 additions & 8 deletions app/src/main/java/com/osfans/trime/core/RimeNotification.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.osfans.trime.core

sealed class RimeNotification {

abstract val messageType: MessageType

data class SchemaNotification(val messageValue: String) :
Expand Down Expand Up @@ -51,12 +50,14 @@ sealed class RimeNotification {

companion object RimeNotificationHandler {
@JvmStatic
fun create(type: String, value: String) =
when (type) {
"schema" -> SchemaNotification(value)
"option" -> OptionNotification(value)
"deploy" -> DeployNotification(value)
else -> UnknownNotification(value)
}
fun create(
type: String,
value: String,
) = when (type) {
"schema" -> SchemaNotification(value)
"option" -> OptionNotification(value)
"deploy" -> DeployNotification(value)
else -> UnknownNotification(value)
}
}
}
16 changes: 14 additions & 2 deletions app/src/main/java/com/osfans/trime/data/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class AppPrefs(
* The type is automatically derived from the given [default] value.
* @return The value for [key] or [default].
*/
private inline fun <reified T> getPref(key: String, default: T): T {
private inline fun <reified T> getPref(
key: String,
default: T,
): T {
return when {
false is T -> {
shared.getBoolean(key, default as Boolean) as T
Expand All @@ -53,7 +56,10 @@ class AppPrefs(
* Sets the [value] for [key] in the shared preferences, puts the value into the corresponding
* cache and returns it.
*/
private inline fun <reified T> setPref(key: String, value: T) {
private inline fun <reified T> setPref(
key: String,
value: T,
) {
when {
false is T -> {
shared.edit().putBoolean(key, value as Boolean).apply()
Expand Down Expand Up @@ -114,6 +120,7 @@ class AppPrefs(
const val PID = "general__pid"
const val LAST_BUILD_GIT_HASH = "general__last_build_git_hash"
}

var lastVersionName: String
get() = prefs.getPref(LAST_VERSION_NAME, "")
set(v) = prefs.setPref(LAST_VERSION_NAME, v)
Expand Down Expand Up @@ -173,6 +180,7 @@ class AppPrefs(
const val DELETE_CANDIDATE_TIMEOUT = "keyboard__key_delete_candidate_timeout"
const val SHOULD_LONG_CLICK_DELETE_CANDIDATE = "keyboard__long_click_delete_candidate"
}

var inlinePreedit: InlineModeType
get() = InlineModeType.fromString(prefs.getPref(INLINE_PREEDIT_MODE, "preview"))
set(v) = prefs.setPref(INLINE_PREEDIT_MODE, v)
Expand Down Expand Up @@ -298,6 +306,7 @@ class AppPrefs(
const val AUTO_DARK = "theme_auto_dark"
const val USE_MINI_KEYBOARD = "theme_use_mini_keyboard"
}

var selectedTheme: String
get() = prefs.getPref(SELECTED_THEME, "trime")
set(v) = prefs.setPref(SELECTED_THEME, v)
Expand Down Expand Up @@ -326,6 +335,7 @@ class AppPrefs(
const val LAST_BACKGROUND_SYNC = "profile_last_background_sync"
val EXTERNAL_PATH_PREFIX: String = PathUtils.getExternalStoragePath()
}

var sharedDataDir: String
get() = prefs.getPref(SHARED_DATA_DIR, "$EXTERNAL_PATH_PREFIX/rime")
set(v) = prefs.setPref(SHARED_DATA_DIR, v)
Expand Down Expand Up @@ -358,6 +368,7 @@ class AppPrefs(
const val DRAFT_LIMIT = "clipboard_draft_limit"
const val CLIPBOARD_LIMIT = "clipboard_clipboard_limit"
}

var clipboardCompareRules: List<String>
get() = prefs.getPref(CLIPBOARD_COMPARE_RULES, "").trim().split('\n')
set(v) = prefs.setPref(CLIPBOARD_COMPARE_RULES, v.joinToString("\n"))
Expand Down Expand Up @@ -388,6 +399,7 @@ class AppPrefs(
const val SHOW_STATUS_BAR_ICON = "other__show_status_bar_icon"
const val DESTROY_ON_QUIT = "other__destroy_on_quit"
}

var uiMode: String
get() = prefs.getPref(UI_MODE, "auto")
set(v) = prefs.setPref(UI_MODE, v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package com.osfans.trime.data
* Do something when data directory change.
*/
object DataDirectoryChangeListener {

// listener list
val directoryChangeListeners = mutableListOf<DataDirectoryChangeListener.Listener>()

Expand Down
25 changes: 16 additions & 9 deletions app/src/main/java/com/osfans/trime/data/DataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ object DataManager : DataDirectoryChangeListener.Listener {

sealed class Diff {
object New : Diff()

object Update : Diff()

object Keep : Diff()
}

Expand All @@ -48,7 +50,10 @@ object DataManager : DataDirectoryChangeListener.Listener {
return defaultPath.absolutePath
}

private fun diff(old: String, new: String): Diff {
private fun diff(
old: String,
new: String,
): Diff {
return when {
old.isBlank() -> Diff.New
!new.contentEquals(old) -> Diff.Update
Expand All @@ -64,14 +69,16 @@ object DataManager : DataDirectoryChangeListener.Listener {
diff(oldHash, newHash).run {
Timber.d("Diff: $this")
when (this) {
is Diff.New -> ResourceUtils.copyFileFromAssets(
"rime",
sharedDataDir.absolutePath,
)
is Diff.Update -> ResourceUtils.copyFileFromAssets(
"rime",
sharedDataDir.absolutePath,
)
is Diff.New ->
ResourceUtils.copyFileFromAssets(
"rime",
sharedDataDir.absolutePath,
)
is Diff.Update ->
ResourceUtils.copyFileFromAssets(
"rime",
sharedDataDir.absolutePath,
)
is Diff.Keep -> {}
}
}
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/com/osfans/trime/data/SymbolHistory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.osfans.trime.util.appContext
class SymbolHistory(
val capacity: Int,
) : LinkedHashMap<String, String>(0, .75f, true) {

companion object {
const val FILE_NAME = "symbol_history"
}
Expand All @@ -25,8 +24,7 @@ class SymbolHistory(
file.writeText(values.joinToString("\n"))
}

override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, String>?) =
size > capacity
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, String>?) = size > capacity

fun insert(s: String) = put(s, s)

Expand Down
Loading

0 comments on commit e236272

Please sign in to comment.