Skip to content

Commit a8703e0

Browse files
authored
improve: android features and fixes (#1427)
1 parent 96734c6 commit a8703e0

File tree

20 files changed

+353
-71
lines changed

20 files changed

+353
-71
lines changed

android/app/src/main/java/com/otclient/AndroidManager.kt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ import android.os.Handler
55
import android.os.Looper
66
import android.view.View
77
import android.view.inputmethod.InputMethodManager
8+
import android.widget.TextView
9+
import androidx.core.view.isVisible
810

911
class AndroidManager(
1012
private val context: Context,
1113
private val editText: FakeEditText,
14+
private val previewContainer: View,
15+
private val previewText: TextView,
1216
) {
1317
private val handler = Handler(Looper.getMainLooper())
18+
private var isImeVisible = false
19+
private var shouldShowPreview = false
20+
private var pendingPreviewText: String = ""
1421

1522
/*
1623
* Methods called from JNI
@@ -30,11 +37,90 @@ class AndroidManager(
3037
editText.visibility = View.INVISIBLE
3138
val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
3239
imm.hideSoftInputFromWindow(editText.windowToken, 0)
40+
hidePreviewInternal(clearText = false)
41+
}
42+
}
43+
44+
fun showInputPreview(text: String) {
45+
handler.post {
46+
pendingPreviewText = text
47+
shouldShowPreview = true
48+
if (isImeVisible) showPreviewInternal()
49+
}
50+
}
51+
52+
fun updateInputPreview(text: String) {
53+
handler.post {
54+
pendingPreviewText = text
55+
if (shouldShowPreview && isImeVisible) showPreviewInternal(animate = false)
56+
}
57+
}
58+
59+
fun hideInputPreview() {
60+
handler.post {
61+
shouldShowPreview = false
62+
pendingPreviewText = ""
63+
hidePreviewInternal(clearText = true)
64+
}
65+
}
66+
67+
fun onImeVisibilityChanged(visible: Boolean) {
68+
handler.post {
69+
isImeVisible = visible
70+
if (visible) {
71+
if (shouldShowPreview) showPreviewInternal()
72+
} else {
73+
hidePreviewInternal(clearText = false)
74+
}
3375
}
3476
}
3577

3678
fun getDisplayDensity(): Float = context.resources.displayMetrics.density
3779

3880
external fun nativeInit()
3981
external fun nativeSetAudioEnabled(enabled: Boolean)
82+
83+
private fun showPreviewInternal(animate: Boolean = true) {
84+
previewContainer.animate().cancel()
85+
previewText.text = pendingPreviewText
86+
if (!previewContainer.isVisible) {
87+
previewContainer.alpha = if (animate) 0f else 1f
88+
previewContainer.isVisible = true
89+
if (animate) {
90+
previewContainer.animate()
91+
.alpha(1f)
92+
.setDuration(120L)
93+
.start()
94+
}
95+
} else if (animate) {
96+
previewContainer.animate()
97+
.alpha(1f)
98+
.setDuration(120L)
99+
.start()
100+
}
101+
}
102+
103+
private fun hidePreviewInternal(animate: Boolean = true, clearText: Boolean) {
104+
previewContainer.animate().cancel()
105+
if (!previewContainer.isVisible) {
106+
if (clearText) previewText.text = ""
107+
return
108+
}
109+
110+
if (animate) {
111+
previewContainer.animate()
112+
.alpha(0f)
113+
.setDuration(120L)
114+
.withEndAction {
115+
previewContainer.isVisible = false
116+
previewContainer.alpha = 1f
117+
if (clearText) previewText.text = ""
118+
}
119+
.start()
120+
} else {
121+
previewContainer.alpha = 1f
122+
previewContainer.isVisible = false
123+
if (clearText) previewText.text = ""
124+
}
125+
}
40126
}

android/app/src/main/java/com/otclient/MainActivity.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.otclient
22

33
import android.os.Bundle
44
import android.view.ViewGroup
5+
import androidx.core.view.ViewCompat
56
import androidx.core.view.WindowCompat
67
import androidx.core.view.WindowInsetsCompat
78
import androidx.core.view.WindowInsetsControllerCompat
@@ -20,11 +21,25 @@ class MainActivity : GameActivity() {
2021
androidManager = AndroidManager(
2122
context = this,
2223
editText = binding.editText,
24+
previewContainer = binding.keyboardPreviewContainer,
25+
previewText = binding.inputPreviewText,
2326
).apply {
2427
nativeInit()
2528
nativeSetAudioEnabled(true)
2629
}
2730

31+
var lastImeVisible = false
32+
ViewCompat.setOnApplyWindowInsetsListener(binding.keyboardPreviewContainer) { view, insets ->
33+
val imeInsets = insets.getInsets(WindowInsetsCompat.Type.ime())
34+
val imeVisible = imeInsets.bottom > 0
35+
if (imeVisible != lastImeVisible) {
36+
lastImeVisible = imeVisible
37+
androidManager.onImeVisibilityChanged(imeVisible)
38+
}
39+
view.translationY = -imeInsets.bottom.toFloat()
40+
insets
41+
}
42+
2843
hideSystemBars()
2944
}
3045

android/app/src/main/java/com/otclient/NativeInputConnection.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.otclient
22

3+
import android.view.KeyCharacterMap
34
import android.view.KeyEvent
45
import android.view.inputmethod.BaseInputConnection
56

@@ -12,9 +13,22 @@ class NativeInputConnection(
1213
override fun sendKeyEvent(event: KeyEvent): Boolean {
1314
val keyCode = event.keyCode
1415
if (event.action == KeyEvent.ACTION_DOWN) {
15-
if (event.isPrintingKey) {
16-
commitText(event.unicodeChar.toChar().toString(), 1)
16+
val text = when {
17+
keyCode == KeyEvent.KEYCODE_SPACE -> " "
18+
!event.characters.isNullOrEmpty() -> event.characters
19+
event.isPrintingKey -> {
20+
val unicodeChar = event.getUnicodeChar(event.metaState)
21+
if (unicodeChar == 0 ||
22+
unicodeChar and KeyCharacterMap.COMBINING_ACCENT != 0
23+
) {
24+
null
25+
} else {
26+
String(Character.toChars(unicodeChar))
27+
}
28+
}
29+
else -> null
1730
}
31+
if (!text.isNullOrEmpty()) commitText(text, 1)
1832
targetView.onNativeKeyDown(keyCode)
1933
return true
2034
} else if (event.action == KeyEvent.ACTION_UP) {
@@ -49,4 +63,4 @@ class NativeInputConnection(
4963
}
5064

5165
private external fun nativeCommitText(text: String)
52-
}
66+
}

android/app/src/main/res/layout/activity_main.xml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,36 @@
66
<com.otclient.FakeEditText
77
android:id="@+id/editText"
88
android:layout_width="wrap_content"
9+
android:layout_height="wrap_content" />
10+
11+
<LinearLayout
12+
android:id="@+id/keyboardPreviewContainer"
13+
android:layout_width="match_parent"
914
android:layout_height="wrap_content"
10-
/>
15+
android:layout_gravity="bottom"
16+
android:gravity="center_horizontal"
17+
android:orientation="vertical"
18+
android:paddingStart="16dp"
19+
android:paddingTop="12dp"
20+
android:paddingEnd="16dp"
21+
android:paddingBottom="24dp"
22+
android:visibility="gone">
23+
24+
<TextView
25+
android:id="@+id/inputPreviewText"
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:background="@drawable/keyboard_preview_background"
29+
android:elevation="6dp"
30+
android:maxLines="2"
31+
android:minWidth="120dp"
32+
android:paddingStart="16dp"
33+
android:paddingTop="8dp"
34+
android:paddingEnd="16dp"
35+
android:paddingBottom="8dp"
36+
android:textColor="@android:color/white"
37+
android:textSize="16sp"
38+
android:textStyle="bold" />
39+
</LinearLayout>
1140

12-
</FrameLayout>
41+
</FrameLayout>

data/fonts/verdana-9px.otfont

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Font
22
name: Verdana-9px
3-
texture: Verdana-9px
3+
texture: verdana-9px
44
height: 13
55
glyph-size: 13 13
66
space-width: 3

mods/game_bot/default_configs/cavebot_1.3/storage.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"on": false
117117
},
118118
"manaShield": "utamo vita",
119-
"autoTradeMessage": "I'm using OTClientV8!",
119+
"autoTradeMessage": "I'm using OTClient Redemption!",
120120
"antiParalyze": "utani hur",
121121
"manaTrain": {
122122
"max": 100,
@@ -125,4 +125,4 @@
125125
"min": 80,
126126
"text": "utevo lux"
127127
}
128-
}
128+
}

mods/game_bot/default_configs/cavebot_1.3/tools.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ macro(60000, "Send message on trade", function()
144144
sayChannel(trade, storage.autoTradeMessage)
145145
end
146146
end)
147-
UI.TextEdit(storage.autoTradeMessage or "I'm using OTClientV8!", function(widget, text)
147+
UI.TextEdit(storage.autoTradeMessage or "I'm using OTClient Redemption!", function(widget, text)
148148
storage.autoTradeMessage = text
149149
end)
150150

mods/game_bot/default_configs/vBot_4.8/vBot/tools.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ macro(60000, "Send message on trade", function()
3939
sayChannel(trade, storage.autoTradeMessage)
4040
end
4141
end)
42-
UI.TextEdit(storage.autoTradeMessage or "I'm using OTClientV8!", function(widget, text)
42+
UI.TextEdit(storage.autoTradeMessage or "I'm using OTClient Redemption!", function(widget, text)
4343
storage.autoTradeMessage = text
4444
end)
4545

mods/game_bot/functions/player_conditions.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ for i, state in ipairs(PlayerStates) do
44
context[state] = state
55
end
66

7-
context.hasCondition = function(condition) return bit.band(context.player:getStates(), condition) > 0 end
7+
context.hasCondition = function(condition) return Bit.band(context.player:getStates(), condition) > 0 end
88

99
context.isPoisioned = function() return context.hasCondition(PlayerStates.Poison) end
1010
context.isBurning = function() return context.hasCondition(PlayerStates.Burn) end

mods/game_bot/panels/tools.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Panels.TradeMessage = function(parent)
1111
context.sayChannel(trade, context.storage.autoTradeMessage)
1212
end
1313
end, parent)
14-
context.addTextEdit("autoTradeMessage", context.storage.autoTradeMessage or "I'm using OTClientV8 - https://github.com/OTCv8/otclientv8", function(widget, text)
14+
context.addTextEdit("autoTradeMessage", context.storage.autoTradeMessage or "I'm using OTClient Redemption - https://github.com/mehah/otclient", function(widget, text)
1515
context.storage.autoTradeMessage = text
1616
end, parent)
1717
end

0 commit comments

Comments
 (0)