Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Emojis for the following are chosen based on [gitmoji](https://gitmoji.dev/).

- Compose theme colors now come from the shared `scribe_*` resources. Dark mode button text stays orange on orange. The dark caption uses the named token `#CAC4D0`, the same grey this slot already used ([#782](https://github.com/scribe-org/Scribe-Android/issues/782)).

### 🐛 Bug Fixes

- ProGuard keep rules and defensive fallbacks in NativeSuggestionEngine were added for JNI-bound classes and methods, preventing crashes in release builds ([#796](https://github.com/scribe-org/Scribe-Android/issues/796)).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate the attention to detail in adding in the space here :)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do let me know if there are any other changes required @andrewtavis

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll let @angrezichatterbox give the final word on this one, but to me this all looks great!

### 🔒 Security

- The unused `WRITE_EXTERNAL_STORAGE` permission was removed so the app requests only network access ([#791](https://github.com/scribe-org/Scribe-Android/issues/791)).
Expand Down
7 changes: 7 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,18 @@ android {
}
}

val isMinify = project.findProperty("minify")?.let { it == "" || it.toString().toBoolean() } ?: false

buildTypes {
getByName("debug") {
applicationIdSuffix = ".debug"
enableUnitTestCoverage = true
enableAndroidTestCoverage = true
if (isMinify) {
isDebuggable = false
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
getByName("release") {
isMinifyEnabled = true
Expand Down
9 changes: 9 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: GPL-3.0-or-later

-keepclasseswithmembernames class * {
native <methods>;
}

-keep class be.scri.inputmethod.keyboard.** { *; }
-keep class be.scri.inputmethod.latin.** { *; }
-keep class be.scri.latin.** { *; }
47 changes: 35 additions & 12 deletions app/src/main/java/be/scri/helpers/NativeSuggestionEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import be.scri.latin.NgramContext
import be.scri.latin.common.ComposedData
import be.scri.latin.dictionary.ReadOnlyBinaryDictionary
import be.scri.latin.settings.SettingsValuesForSuggestion
import be.scri.latin.utils.JniUtils
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
Expand All @@ -30,7 +31,21 @@ class NativeSuggestionEngine(private val context: Context) {
}

private val loadedDicts = HashMap<String, ReadOnlyBinaryDictionary>()
private val dummyProximityInfo = ProximityInfo()

/**
* Checks whether the underlying native library (libjni_latinime.so) was loaded successfully.
*/
val isNativeAvailable: Boolean
get() = JniUtils.sHaveGestureLib

private val dummyProximityInfo by lazy {
try {
if (isNativeAvailable) ProximityInfo() else null
} catch (t: Throwable) {
Log.e(TAG, "Failed to initialize dummy ProximityInfo", t)
null
}
}

/**
* Map a language string to its corresponding main dictionary asset name and Locale.
Expand Down Expand Up @@ -108,6 +123,8 @@ class NativeSuggestionEngine(private val context: Context) {
*/
@Synchronized
fun getDictionary(language: String): ReadOnlyBinaryDictionary? {
if (!isNativeAvailable) return null

val cacheKey = language.lowercase(Locale.ROOT)
loadedDicts[cacheKey]?.let { return it }

Expand All @@ -132,8 +149,8 @@ class NativeSuggestionEngine(private val context: Context) {
dict.close()
null
}
} catch (e: Exception) {
Log.e(TAG, "Error initializing ReadOnlyBinaryDictionary for $language", e)
} catch (t: Throwable) {
Log.e(TAG, "Error initializing ReadOnlyBinaryDictionary for $language", t)
null
}
}
Expand All @@ -148,8 +165,9 @@ class NativeSuggestionEngine(private val context: Context) {
limit: Int = 3,
profanity: Set<String> = emptySet()
): List<String> {
if (!isNativeAvailable || prefix.isBlank()) return emptyList()
val proximityInfo = dummyProximityInfo ?: return emptyList()
val dict = getDictionary(language) ?: return emptyList()
if (prefix.isBlank()) return emptyList()

return try {
val composedData = ComposedData.createForWord(prefix)
Expand All @@ -162,7 +180,7 @@ class NativeSuggestionEngine(private val context: Context) {
val suggestions = dict.getSuggestions(
composedData,
ngramContext,
dummyProximityInfo.nativeProximityInfo, // proximityInfoHandle
proximityInfo.nativeProximityInfo, // proximityInfoHandle
SettingsValuesForSuggestion(true, false), // blockPotentiallyOffensive, spaceAwareGesture
1, // sessionId
1.0f, // weightForLocale
Expand All @@ -180,8 +198,8 @@ class NativeSuggestionEngine(private val context: Context) {
?.map { if (isCapitalized) it.replaceFirstChar { c -> c.uppercaseChar() } else it }
?.take(limit)
?: emptyList()
} catch (e: Exception) {
Log.e(TAG, "Error fetching native suggestions for $prefix", e)
} catch (t: Throwable) {
Log.e(TAG, "Error fetching native suggestions for $prefix", t)
emptyList()
}
}
Expand All @@ -195,8 +213,9 @@ class NativeSuggestionEngine(private val context: Context) {
limit: Int = 3,
profanity: Set<String> = emptySet()
): List<String> {
if (!isNativeAvailable || lastWord.isNullOrBlank()) return emptyList()
val proximityInfo = dummyProximityInfo ?: return emptyList()
val dict = getDictionary(language) ?: return emptyList()
if (lastWord.isNullOrBlank()) return emptyList()

return try {
val wordInfo = NgramContext.WordInfo(lastWord)
Expand All @@ -205,7 +224,7 @@ class NativeSuggestionEngine(private val context: Context) {
val suggestions = dict.getSuggestions(
composedData,
ngramContext,
dummyProximityInfo.nativeProximityInfo, // proximityInfoHandle
proximityInfo.nativeProximityInfo, // proximityInfoHandle
SettingsValuesForSuggestion(true, false), // blockPotentiallyOffensive, spaceAwareGesture
1, // sessionId
1.0f, // weightForLocale
Expand All @@ -220,8 +239,8 @@ class NativeSuggestionEngine(private val context: Context) {
}
?.take(limit)
?: emptyList()
} catch (e: Exception) {
Log.e(TAG, "Error fetching native next-word suggestions for $lastWord", e)
} catch (t: Throwable) {
Log.e(TAG, "Error fetching native next-word suggestions for $lastWord", t)
emptyList()
}
}
Expand All @@ -232,7 +251,11 @@ class NativeSuggestionEngine(private val context: Context) {
@Synchronized
fun close() {
for (dict in loadedDicts.values) {
dict.close()
try {
dict.close()
} catch (t: Throwable) {
Log.e(TAG, "Error closing dictionary", t)
}
}
loadedDicts.clear()
}
Expand Down
Loading