Skip to content

Commit 959e5dc

Browse files
committed
refactor: replace fire-and-forget threads/executors with shared instances
1 parent 5cd2876 commit 959e5dc

4 files changed

Lines changed: 11 additions & 7 deletions

File tree

app/src/main/java/helium314/keyboard/keyboard/emoji/EmojiPalettesView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ private void downloadEmojiDictionary() {
11681168

11691169
Toast.makeText(getContext(), "Downloading Emoji Dictionary...", Toast.LENGTH_SHORT).show();
11701170

1171-
java.util.concurrent.Executors.newSingleThreadExecutor().execute(() -> {
1171+
helium314.keyboard.latin.utils.ExecutorUtils.getBackgroundExecutor(helium314.keyboard.latin.utils.ExecutorUtils.KEYBOARD).execute(() -> {
11721172
try {
11731173
java.net.URL url = new java.net.URL(urlStr);
11741174
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();

app/src/main/java/helium314/keyboard/latin/gesture/SwipeGestureEngine.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626
import java.util.Locale;
2727
import java.util.Map;
2828
import java.util.concurrent.ConcurrentHashMap;
29+
import java.util.concurrent.ExecutorService;
30+
import java.util.concurrent.Executors;
2931

3032
public class SwipeGestureEngine {
3133

34+
private static final ExecutorService sSaveExecutor = Executors.newSingleThreadExecutor();
35+
3236
private static final int N_PTS = 16;
3337
private static final float FREQ_WEIGHT = 0.05f;
3438

@@ -122,11 +126,11 @@ private static void saveUserData() {
122126
}
123127

124128
private static void saveUserDataAsync() {
125-
new Thread(() -> {
129+
sSaveExecutor.execute(() -> {
126130
synchronized (SwipeGestureEngine.class) {
127131
saveUserData();
128132
}
129-
}).start();
133+
});
130134
}
131135

132136
private static void loadUserData() {

app/src/main/java/helium314/keyboard/latin/handwriting/HandwritingView.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class HandwritingView @JvmOverloads constructor(
402402
button.isEnabled = false
403403
android.widget.Toast.makeText(context, "Downloading Handwriting Plugin...", android.widget.Toast.LENGTH_SHORT).show()
404404

405-
java.util.concurrent.Executors.newSingleThreadExecutor().execute {
405+
recognitionExecutor.execute {
406406
try {
407407
val urlStr = "https://github.com/LeanBitLab/Leantype-Handwriting-Plugin/releases/latest/download/handwriting_plugin.apk"
408408
var url = java.net.URL(urlStr)
@@ -440,7 +440,7 @@ class HandwritingView @JvmOverloads constructor(
440440
val success = HandwritingLoader.importPlugin(context, android.net.Uri.fromFile(tempFile))
441441
tempFile.delete()
442442

443-
android.os.Handler(android.os.Looper.getMainLooper()).post {
443+
mainHandler.post {
444444
button.isEnabled = true
445445
if (success) {
446446
button.text = "Success"
@@ -459,7 +459,7 @@ class HandwritingView @JvmOverloads constructor(
459459
}
460460
} catch (e: Exception) {
461461
Log.e("HandwritingView", "Failed to download plugin", e)
462-
android.os.Handler(android.os.Looper.getMainLooper()).post {
462+
mainHandler.post {
463463
button.isEnabled = true
464464
button.text = "Download Plugin"
465465
android.widget.Toast.makeText(context, "Download failed: ${e.localizedMessage}", android.widget.Toast.LENGTH_LONG).show()

app/src/main/java/helium314/keyboard/latin/utils/DictionaryUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private fun hasAnythingOtherThanExtractedMainDictionary(context: Context, dir: F
249249
fun downloadDictionary(context: Context, locale: Locale, type: String, linkUrl: String, onComplete: (Boolean) -> Unit) {
250250
val cacheDir = DictionaryInfoUtils.getCacheDirectoryForLocale(locale, context) ?: return onComplete(false)
251251
val targetFile = File(cacheDir, "${type}.dict")
252-
CoroutineScope(Dispatchers.IO).launch {
252+
CoroutineScope(Dispatchers.IO + kotlinx.coroutines.SupervisorJob()).launch {
253253
var success = false
254254
try {
255255
var url = java.net.URL(linkUrl)

0 commit comments

Comments
 (0)