Newer
Older
Aleksandras Kostarevas
committed
package org.futo.inputmethod.latin
import android.content.Context
import android.content.res.Configuration
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.InsetDrawable
import android.graphics.drawable.LayerDrawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.StateListDrawable
import android.graphics.drawable.shapes.RoundRectShape
Aleksandras Kostarevas
committed
import android.inputmethodservice.InputMethodService
import android.util.AttributeSet
import android.util.TypedValue
Aleksandras Kostarevas
committed
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.CompletionInfo
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.view.inputmethod.InputMethodSubtype
import androidx.annotation.ColorInt
import androidx.appcompat.content.res.AppCompatResources
Aleksandras Kostarevas
committed
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
Aleksandras Kostarevas
committed
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.layout.onSizeChanged
Aleksandras Kostarevas
committed
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Aleksandras Kostarevas
committed
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.graphics.toColor
Aleksandras Kostarevas
committed
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.findViewTreeLifecycleOwner
import androidx.lifecycle.findViewTreeViewModelStoreOwner
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.lifecycle.setViewTreeViewModelStoreOwner
import androidx.savedstate.SavedStateRegistry
import androidx.savedstate.SavedStateRegistryController
import androidx.savedstate.SavedStateRegistryOwner
import androidx.savedstate.findViewTreeSavedStateRegistryOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import com.google.android.material.color.DynamicColors
import org.futo.inputmethod.latin.uix.ActionBar
import org.futo.inputmethod.latin.uix.theme.DarkColorScheme
interface KeyboardDrawableProvider {
val primaryKeyboardColor: Int
val keyboardBackground: Drawable
val keyBackground: Drawable
val spaceBarBackground: Drawable
val moreKeysKeyboardBackground: Drawable
val popupKey: Drawable
@ColorInt
fun getColor(i: Int): Int?
fun getDrawable(i: Int): Drawable?
companion object {
@ColorInt
fun getColorOrDefault(i: Int, @ColorInt default: Int, keyAttr: TypedArray, provider: KeyboardDrawableProvider?): Int {
return (provider?.getColor(i)) ?: keyAttr.getColor(i, default)
}
fun getDrawableOrDefault(i: Int, keyAttr: TypedArray, provider: KeyboardDrawableProvider?): Drawable? {
return (provider?.getDrawable(i)) ?: keyAttr.getDrawable(i)
}
}
}
// TODO: Expand the number of drawables this provides so it covers the full theme, and
// build some system to dynamically change these colors
class BasicThemeProvider(val context: Context) : KeyboardDrawableProvider {
override val primaryKeyboardColor: Int
override val keyboardBackground: Drawable
override val keyBackground: Drawable
override val spaceBarBackground: Drawable
override val moreKeysKeyboardBackground: Drawable
override val popupKey: Drawable
private val colors: HashMap<Int, Int> = HashMap()
override fun getColor(i: Int): Int? {
return colors[i]
}
private val drawables: HashMap<Int, Drawable> = HashMap()
override fun getDrawable(i: Int): Drawable? {
return drawables[i]
}
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
private fun dp(dp: Dp): Float {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp.value,
context.resources.displayMetrics
);
}
private fun coloredRectangle(@ColorInt color: Int): GradientDrawable {
return GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
setColor(color)
}
}
private fun coloredRoundedRectangle(@ColorInt color: Int, radius: Float): GradientDrawable {
return GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadius = radius
setColor(color)
}
}
private fun coloredOval(@ColorInt color: Int): GradientDrawable {
return GradientDrawable().apply {
shape = GradientDrawable.OVAL
cornerRadius = Float.MAX_VALUE
setColor(color)
}
}
private fun StateListDrawable.addStateWithHighlightLayerOnPressed(@ColorInt highlight: Int, stateSet: IntArray, drawable: Drawable) {
addState(intArrayOf(android.R.attr.state_pressed) + stateSet, LayerDrawable(arrayOf(
drawable,
coloredRoundedRectangle(highlight, dp(8.dp))
)))
addState(stateSet, drawable)
}
init {
val colorScheme = if(!DynamicColors.isDynamicColorAvailable()) {
DarkColorScheme
} else {
val dCtx = DynamicColors.wrapContextIfAvailable(context)
dynamicLightColorScheme(dCtx)
}
val primary = colorScheme.primary.toArgb()
val secondary = colorScheme.secondary.toArgb()
val highlight = colorScheme.outline.copy(alpha = 0.33f).toArgb()
val background = colorScheme.surface.toArgb()
val surface = colorScheme.background.toArgb()
val outline = colorScheme.outline.toArgb()
val onSecondary = colorScheme.onSecondary.toArgb()
val onBackground = colorScheme.onBackground.toArgb()
val onBackgroundHalf = colorScheme.onBackground.copy(alpha = 0.5f).toArgb()
val transparent = Color.TRANSPARENT
colors[R.styleable.Keyboard_Key_keyTextColor] = onBackground
colors[R.styleable.Keyboard_Key_keyTextInactivatedColor] = onBackgroundHalf
colors[R.styleable.Keyboard_Key_keyTextShadowColor] = 0
colors[R.styleable.Keyboard_Key_functionalTextColor] = onBackground
colors[R.styleable.Keyboard_Key_keyHintLetterColor] = onBackgroundHalf
colors[R.styleable.Keyboard_Key_keyHintLabelColor] = onBackgroundHalf
colors[R.styleable.Keyboard_Key_keyShiftedLetterHintInactivatedColor] = onBackgroundHalf
colors[R.styleable.Keyboard_Key_keyShiftedLetterHintActivatedColor] = onBackgroundHalf
colors[R.styleable.Keyboard_Key_keyPreviewTextColor] = onSecondary
colors[R.styleable.MainKeyboardView_languageOnSpacebarTextColor] = onBackgroundHalf
drawables[R.styleable.Keyboard_iconDeleteKey] = AppCompatResources.getDrawable(context, R.drawable.delete)!!.apply {
setTint(onBackground)
}
drawables[R.styleable.Keyboard_iconLanguageSwitchKey] = AppCompatResources.getDrawable(context, R.drawable.globe)!!.apply {
setTint(onBackground)
}
drawables[R.styleable.Keyboard_iconShiftKey] = AppCompatResources.getDrawable(context, R.drawable.shift)!!.apply {
setTint(onBackground)
}
drawables[R.styleable.Keyboard_iconShiftKeyShifted] = AppCompatResources.getDrawable(context, R.drawable.shiftshifted)!!.apply {
setTint(onBackground)
}
primaryKeyboardColor = background
keyboardBackground = coloredRectangle(background)
keyBackground = StateListDrawable().apply {
addStateWithHighlightLayerOnPressed(highlight, intArrayOf(android.R.attr.state_active),
coloredRoundedRectangle(primary, dp(8.dp)).apply {
setSize(dp(64.dp).toInt(), dp(48.dp).toInt())
}
)
addStateWithHighlightLayerOnPressed(highlight, intArrayOf(android.R.attr.state_checkable, android.R.attr.state_checked),
coloredRoundedRectangle(secondary, dp(8.dp))
)
addStateWithHighlightLayerOnPressed(highlight, intArrayOf(android.R.attr.state_checkable),
coloredRectangle(transparent)
)
addStateWithHighlightLayerOnPressed(highlight, intArrayOf(android.R.attr.state_empty),
coloredRectangle(transparent)
)
addStateWithHighlightLayerOnPressed(highlight, intArrayOf(),
coloredRectangle(transparent)
)
}
spaceBarBackground = StateListDrawable().apply {
addState(intArrayOf(android.R.attr.state_pressed),
LayerDrawable(arrayOf(
coloredRoundedRectangle(highlight, dp(32.dp)),
coloredRoundedRectangle(highlight, dp(32.dp))
))
)
addState(intArrayOf(),
coloredRoundedRectangle(highlight, dp(32.dp))
)
}
keyFeedback = ShapeDrawable().apply {
paint.color = secondary
shape = RoundRectShape(floatArrayOf(
dp(8.dp),dp(8.dp),dp(8.dp),dp(8.dp),
dp(8.dp),dp(8.dp),dp(8.dp),dp(8.dp),
), null, null)
intrinsicWidth = dp(48.dp).roundToInt()
intrinsicHeight = dp(24.dp).roundToInt()
setPadding(0, 0, 0, dp(50.dp).roundToInt())
moreKeysKeyboardBackground = coloredRoundedRectangle(surface, dp(8.dp))
popupKey = StateListDrawable().apply {
addStateWithHighlightLayerOnPressed(highlight, intArrayOf(),
coloredRoundedRectangle(surface, dp(8.dp))
)
}
}
}
interface KeyboardDrawableProviderOwner {
fun getDrawableProvider(): KeyboardDrawableProvider
}
class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, SavedStateRegistryOwner, LatinIMELegacy.SuggestionStripController, KeyboardDrawableProviderOwner {
private var drawableProvider: KeyboardDrawableProvider? = null
override fun getDrawableProvider(): KeyboardDrawableProvider {
if(drawableProvider == null) {
drawableProvider = BasicThemeProvider(this)
}
return drawableProvider!!
}
Aleksandras Kostarevas
committed
private val latinIMELegacy = LatinIMELegacy(
this as InputMethodService,
this as LatinIMELegacy.SuggestionStripController
)
Aleksandras Kostarevas
committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
private val mSavedStateRegistryController = SavedStateRegistryController.create(this)
override val savedStateRegistry: SavedStateRegistry
get() = mSavedStateRegistryController.savedStateRegistry
private val mLifecycleRegistry = LifecycleRegistry(this)
override val lifecycle
get() = mLifecycleRegistry
private val store = ViewModelStore()
override val viewModelStore
get() = store
private fun handleLifecycleEvent(event: Lifecycle.Event) =
mLifecycleRegistry.handleLifecycleEvent(event)
private val inputMethodManager: InputMethodManager
get() = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
override fun onCreate() {
super.onCreate()
mSavedStateRegistryController.performRestore(null)
handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
latinIMELegacy.onCreate()
}
private fun setOwners() {
val decorView = window.window?.decorView
if (decorView?.findViewTreeLifecycleOwner() == null) {
decorView?.setViewTreeLifecycleOwner(this)
}
if (decorView?.findViewTreeViewModelStoreOwner() == null) {
decorView?.setViewTreeViewModelStoreOwner(this)
}
if (decorView?.findViewTreeSavedStateRegistryOwner() == null) {
decorView?.setViewTreeSavedStateRegistryOwner(this)
}
}
private var composeView: ComposeView? = null
override fun onDestroy() {
latinIMELegacy.onDestroy()
super.onDestroy()
}
override fun onConfigurationChanged(newConfig: Configuration) {
latinIMELegacy.onConfigurationChanged(newConfig)
super.onConfigurationChanged(newConfig)
}
override fun onInitializeInterface() {
latinIMELegacy.onInitializeInterface()
}
Aleksandras Kostarevas
committed
private var legacyInputView: View? = null
Aleksandras Kostarevas
committed
override fun onCreateInputView(): View {
Aleksandras Kostarevas
committed
legacyInputView = latinIMELegacy.onCreateInputView()
composeView = ComposeView(this).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setParentCompositionContext(null)
this@LatinIME.setOwners()
}
latinIMELegacy.setComposeInputView(composeView)
private var shouldShowSuggestionStrip: Boolean = true
private var suggestedWords: SuggestedWords? = null
Aleksandras Kostarevas
committed
composeView?.setContent {
Column {
Spacer(modifier = Modifier.weight(1.0f))
Surface(modifier = Modifier.onSizeChanged {
touchableHeight = it.height
}, color = MaterialTheme.colorScheme.surface) {
Column {
if(shouldShowSuggestionStrip) {
ActionBar(
suggestedWords,
latinIMELegacy
)
}
key(legacyInputView) {
AndroidView(factory = {
legacyInputView!!
}, update = { })
}
Aleksandras Kostarevas
committed
}
}
}
Aleksandras Kostarevas
committed
// necessary for when KeyboardSwitcher updates the theme
fun updateLegacyView(newView: View) {
legacyInputView = newView
setContent()
if(composeView != null) {
latinIMELegacy.setComposeInputView(composeView)
}
latinIMELegacy.setInputView(legacyInputView)
Aleksandras Kostarevas
committed
}
override fun setInputView(view: View?) {
super.setInputView(view)
if(composeView != null) {
latinIMELegacy.setComposeInputView(composeView)
}
Aleksandras Kostarevas
committed
latinIMELegacy.setInputView(legacyInputView)
Aleksandras Kostarevas
committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
}
override fun setCandidatesView(view: View?) {
return latinIMELegacy.setCandidatesView(view)
}
override fun onStartInput(attribute: EditorInfo?, restarting: Boolean) {
super.onStartInput(attribute, restarting)
latinIMELegacy.onStartInput(attribute, restarting)
}
override fun onStartInputView(info: EditorInfo?, restarting: Boolean) {
super.onStartInputView(info, restarting)
latinIMELegacy.onStartInputView(info, restarting)
}
override fun onFinishInputView(finishingInput: Boolean) {
super.onFinishInputView(finishingInput)
latinIMELegacy.onFinishInputView(finishingInput)
}
override fun onFinishInput() {
super.onFinishInput()
latinIMELegacy.onFinishInput()
}
override fun onCurrentInputMethodSubtypeChanged(newSubtype: InputMethodSubtype?) {
super.onCurrentInputMethodSubtypeChanged(newSubtype)
latinIMELegacy.onCurrentInputMethodSubtypeChanged(newSubtype)
}
override fun onWindowShown() {
super.onWindowShown()
latinIMELegacy.onWindowShown()
}
override fun onWindowHidden() {
super.onWindowHidden()
latinIMELegacy.onWindowHidden()
}
override fun onUpdateSelection(
oldSelStart: Int,
oldSelEnd: Int,
newSelStart: Int,
newSelEnd: Int,
candidatesStart: Int,
candidatesEnd: Int
) {
super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, candidatesEnd)
latinIMELegacy.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, candidatesEnd)
}
override fun onExtractedTextClicked() {
latinIMELegacy.onExtractedTextClicked()
super.onExtractedTextClicked()
}
override fun onExtractedCursorMovement(dx: Int, dy: Int) {
latinIMELegacy.onExtractedCursorMovement(dx, dy)
super.onExtractedCursorMovement(dx, dy)
}
override fun hideWindow() {
latinIMELegacy.hideWindow()
super.hideWindow()
}
override fun onDisplayCompletions(completions: Array<out CompletionInfo>?) {
latinIMELegacy.onDisplayCompletions(completions)
}
override fun onComputeInsets(outInsets: Insets?) {
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// This method may be called before {@link #setInputView(View)}.
if (legacyInputView == null) {
return
}
val inputHeight: Int = composeView!!.height
if (latinIMELegacy.isImeSuppressedByHardwareKeyboard && !legacyInputView!!.isShown) {
// If there is a hardware keyboard and a visible software keyboard view has been hidden,
// no visual element will be shown on the screen.
latinIMELegacy.setInsets(outInsets!!.apply {
contentTopInsets = inputHeight
visibleTopInsets = inputHeight
})
return
}
val visibleTopY = inputHeight - touchableHeight
val touchLeft = 0
val touchTop = visibleTopY
val touchRight = legacyInputView!!.width
val touchBottom = inputHeight
latinIMELegacy.setInsets(outInsets!!.apply {
touchableInsets = Insets.TOUCHABLE_INSETS_REGION;
touchableRegion.set(touchLeft, touchTop, touchRight, touchBottom);
contentTopInsets = visibleTopY
visibleTopInsets = visibleTopY
})
Aleksandras Kostarevas
committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
}
override fun onShowInputRequested(flags: Int, configChange: Boolean): Boolean {
return latinIMELegacy.onShowInputRequested(flags, configChange) || super.onShowInputRequested(flags, configChange)
}
override fun onEvaluateInputViewShown(): Boolean {
return latinIMELegacy.onEvaluateInputViewShown() || super.onEvaluateInputViewShown()
}
override fun onEvaluateFullscreenMode(): Boolean {
return latinIMELegacy.onEvaluateFullscreenMode(super.onEvaluateFullscreenMode())
}
override fun updateFullscreenMode() {
super.updateFullscreenMode()
latinIMELegacy.updateFullscreenMode()
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
return latinIMELegacy.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event)
}
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
return latinIMELegacy.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event)
}
override fun updateVisibility(shouldShowSuggestionsStrip: Boolean, fullscreenMode: Boolean) {
this.shouldShowSuggestionStrip = shouldShowSuggestionsStrip
setContent()
}
override fun setSuggestions(suggestedWords: SuggestedWords?, rtlSubtype: Boolean) {
this.suggestedWords = suggestedWords
setContent()
}
override fun maybeShowImportantNoticeTitle(): Boolean {
return false
}