diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e0d0fe1..8854f20 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,7 +2,7 @@ name: Build EXP Mango on: push: - branches: [main] + branches: ["*"] workflow_dispatch: concurrency: @@ -19,8 +19,6 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - with: - submodules: recursive - name: Java 21 uses: actions/setup-java@v4 @@ -33,11 +31,14 @@ jobs: with: packages: platform-tools - - name: Install Android 36 SDK - run: sdkmanager "platforms;android-36" "build-tools;36.0.0" + - name: Install Android 35 SDK + run: sdkmanager "platforms;android-35" "build-tools;35.0.0" + + - name: Offline & Privacy Audit + run: bash scripts/audit-offline.sh - - name: Prepare EXP Mango source - run: bash scripts/prepare-exp-mango.sh + - name: Run Unit Tests + run: ./gradlew test --stacktrace - name: Check signing availability id: signing @@ -60,7 +61,7 @@ jobs: run: | echo "$KEYSTORE_BASE64" | base64 -d > release.keystore - - name: Build signed EXP Mango Lite + - name: Build EXP Mango Release if: steps.signing.outputs.available == 'true' env: RELEASE_STORE_FILE: release.keystore @@ -68,19 +69,17 @@ jobs: RELEASE_KEY_ALIAS: ${{ secrets.EXP_MANGO_KEY_ALIAS }} RELEASE_KEY_PASSWORD: ${{ secrets.EXP_MANGO_KEY_PASSWORD }} run: | - cd wmkeyboard - ./gradlew assembleLiteEnRelease -Pwmkb.noInternet=true -Pkotlin.daemon.jvmargs=-Xmx4g --stacktrace + ./gradlew assembleRelease --stacktrace - - name: Build EXP Mango Lite debug + - name: Build EXP Mango Debug if: steps.signing.outputs.available != 'true' run: | - cd wmkeyboard - ./gradlew assembleLiteEnDebug -Pwmkb.noInternet=true -Pkotlin.daemon.jvmargs=-Xmx4g --stacktrace + ./gradlew assembleDebug --stacktrace - name: Verify APK signature if: steps.signing.outputs.available == 'true' run: | - APK="wmkeyboard/app/build/outputs/apk/liteEn/release/app-liteEn-release.apk" + APK="app/build/outputs/apk/release/app-release.apk" test -f "$APK" APKSIGNER="$(ls -d "$ANDROID_HOME"/build-tools/*/apksigner | sort -V | tail -1)" "$APKSIGNER" verify --verbose --print-certs "$APK" @@ -89,17 +88,18 @@ jobs: - name: Collect APK run: | if [ "${{ steps.signing.outputs.available }}" = "true" ]; then - cp wmkeyboard/app/build/outputs/apk/liteEn/release/app-liteEn-release.apk EXP-Mango.apk + cp app/build/outputs/apk/release/app-release.apk EXP-Mango.apk else - APK="$(find wmkeyboard/app/build/outputs/apk -type f -path '*/debug/*.apk' -print -quit)" + APK="$(find app/build/outputs/apk -type f -name '*.apk' -print -quit)" test -n "$APK" cp "$APK" EXP-Mango-debug.apk + cp "$APK" EXP-Mango.apk fi - - name: Upload APK + - name: Upload APK Artifact uses: actions/upload-artifact@v4 with: - name: ${{ steps.signing.outputs.available == 'true' && 'EXP-Mango' || 'EXP-Mango-current-test' }} + name: EXP-Mango-test path: | EXP-Mango.apk EXP-Mango-debug.apk diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aea9521 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +build/ +app/build/ +.gradle/ +.kotlin/ +.idea/ +*.iml +*.apk +*.aar +*.dex +*.class +local.properties +*.keystore +*.jks +release.keystore +.navigation/ +captures/ +*.kwks diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 89cf78d..2d66a88 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -12,7 +12,9 @@ android { minSdk = 26 targetSdk = 35 versionCode = 1 - versionName = "0.1.0" + versionName = "1.0.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } buildTypes { @@ -40,4 +42,17 @@ android { packaging { resources.excludes += "/META-INF/{AL2.0,LGPL2.1}" } + + testOptions { + unitTests.isIncludeAndroidResources = true + } +} + +dependencies { + implementation("androidx.core:core-ktx:1.15.0") + implementation("androidx.appcompat:appcompat:1.7.0") + implementation("com.google.android.material:material:1.12.0") + + testImplementation("junit:junit:4.13.2") + testImplementation("org.json:json:20231013") } diff --git a/app/src/main/java/com/mangoloads/expmango/DictionaryManager.kt b/app/src/main/java/com/mangoloads/expmango/DictionaryManager.kt new file mode 100644 index 0000000..9ee2c81 --- /dev/null +++ b/app/src/main/java/com/mangoloads/expmango/DictionaryManager.kt @@ -0,0 +1,125 @@ +package com.mangoloads.expmango + +import android.content.Context +import java.io.File +import java.util.Locale +import java.util.concurrent.ConcurrentHashMap + +enum class WordCategory(val displayName: String) { + ENGLISH("English"), + HINGLISH("Hinglish"), + SLANG("Slang"), + INDIAN_ENGLISH("Indian English"), + UPSC("UPSC"), + TECHNOLOGY("Technology"), + NAMES("Names"), + PERSONAL("Personal"), + OTHER("Other") +} + +data class CustomWord( + val word: String, + val category: WordCategory = WordCategory.PERSONAL, + val frequency: Int = 100 +) + +class DictionaryManager(context: Context) { + + private val appContext = context.applicationContext + private val filesDir: File = try { + appContext.filesDir ?: File("build/test-tmp").apply { mkdirs() } + } catch (_: Exception) { + File("build/test-tmp").apply { mkdirs() } + } + + private val customWordsFile = File(filesDir, "custom_dictionary.tsv") + private val words = ConcurrentHashMap() + + init { + loadCustomWords() + } + + fun addWord(word: String, category: WordCategory = WordCategory.PERSONAL, frequency: Int = 100) { + val clean = word.trim().lowercase(Locale.ROOT) + if (clean.isNotBlank() && clean.none { it.isWhitespace() }) { + words[clean] = CustomWord(clean, category, frequency) + saveCustomWords() + } + } + + fun addBulkWords(lines: List, category: WordCategory = WordCategory.PERSONAL) { + lines.forEach { line -> + val clean = line.trim().lowercase(Locale.ROOT) + if (clean.isNotBlank() && !clean.startsWith("#") && clean.none { it.isWhitespace() }) { + words[clean] = CustomWord(clean, category, 100) + } + } + saveCustomWords() + } + + fun removeWord(word: String) { + val clean = word.trim().lowercase(Locale.ROOT) + if (words.remove(clean) != null) { + saveCustomWords() + } + } + + fun getWords(category: WordCategory? = null): List { + val list = words.values.toList() + return if (category == null) list else list.filter { it.category == category } + } + + fun search(query: String): List { + val q = query.trim().lowercase(Locale.ROOT) + if (q.isBlank()) return getWords() + return words.values.filter { it.word.contains(q) } + } + + fun reset() { + words.clear() + if (customWordsFile.exists()) { + customWordsFile.delete() + } + } + + fun getWordCount(): Int = words.size + + fun getCategoryCounts(): Map { + val counts = WordCategory.values().associateWith { 0 }.toMutableMap() + words.values.forEach { cw -> + counts[cw.category] = (counts[cw.category] ?: 0) + 1 + } + return counts + } + + private fun loadCustomWords() { + if (!customWordsFile.exists()) return + try { + customWordsFile.forEachLine { line -> + val parts = line.split('\t') + if (parts.isNotEmpty()) { + val w = parts[0].trim().lowercase(Locale.ROOT) + val cat = parts.getOrNull(1)?.let { + try { WordCategory.valueOf(it) } catch (_: Exception) { WordCategory.PERSONAL } + } ?: WordCategory.PERSONAL + val freq = parts.getOrNull(2)?.toIntOrNull() ?: 100 + if (w.isNotBlank()) { + words[w] = CustomWord(w, cat, freq) + } + } + } + } catch (_: Exception) { + } + } + + private fun saveCustomWords() { + try { + customWordsFile.printWriter().use { out -> + words.values.sortedBy { it.word }.forEach { cw -> + out.println("${cw.word}\t${cw.category.name}\t${cw.frequency}") + } + } + } catch (_: Exception) { + } + } +} diff --git a/app/src/main/java/com/mangoloads/expmango/EmailDomainProvider.kt b/app/src/main/java/com/mangoloads/expmango/EmailDomainProvider.kt new file mode 100644 index 0000000..2113ca6 --- /dev/null +++ b/app/src/main/java/com/mangoloads/expmango/EmailDomainProvider.kt @@ -0,0 +1,52 @@ +package com.mangoloads.expmango + +import java.util.Locale +import java.util.concurrent.ConcurrentHashMap + +class EmailDomainProvider { + + private val builtInDomains = listOf( + "gmail.com", + "outlook.com", + "yahoo.com", + "icloud.com", + "proton.me", + "protonmail.com", + "hotmail.com" + ) + + private val learnedDomains = ConcurrentHashMap() + + init { + // Initialize default weights for built-in domains + builtInDomains.forEachIndexed { idx, domain -> + learnedDomains[domain] = 100 - idx * 5 + } + } + + fun learnDomain(domain: String) { + val clean = domain.trim().lowercase(Locale.ROOT).removePrefix("@") + if (clean.length > 3 && clean.contains(".")) { + learnedDomains.compute(clean) { _, count -> (count ?: 10) + 20 } + } + } + + fun getDomainSuggestions(token: String, limit: Int = 3): List { + val atIndex = token.lastIndexOf('@') + if (atIndex < 0) return emptyList() + + val prefix = token.substring(atIndex + 1).lowercase(Locale.ROOT) + + return learnedDomains.entries + .asSequence() + .filter { (domain, _) -> domain.startsWith(prefix) } + .sortedByDescending { it.value } + .map { it.key } + .take(limit) + .toList() + } + + fun allDomains(): List = learnedDomains.entries + .sortedByDescending { it.value } + .map { it.key } +} diff --git a/app/src/main/java/com/mangoloads/expmango/FieldPolicyResolver.kt b/app/src/main/java/com/mangoloads/expmango/FieldPolicyResolver.kt new file mode 100644 index 0000000..867c95d --- /dev/null +++ b/app/src/main/java/com/mangoloads/expmango/FieldPolicyResolver.kt @@ -0,0 +1,135 @@ +package com.mangoloads.expmango + +import android.text.InputType +import android.view.inputmethod.EditorInfo + +enum class FieldMode { + NORMAL, + EMAIL, + URI, + PASSWORD, + PIN, + OTP, + CODE, + TERMINAL, + SEARCH, + INCOGNITO +} + +data class FieldPolicy( + val mode: FieldMode, + val canPredict: Boolean, + val canAutocorrect: Boolean, + val canLearn: Boolean, + val showDomainChips: Boolean, + val isPrivate: Boolean +) + +object FieldPolicyResolver { + + fun resolve(mode: FieldMode): FieldPolicy = when (mode) { + FieldMode.NORMAL -> FieldPolicy( + mode = mode, + canPredict = true, + canAutocorrect = true, + canLearn = true, + showDomainChips = false, + isPrivate = false + ) + + FieldMode.EMAIL -> FieldPolicy( + mode = mode, + canPredict = true, + canAutocorrect = false, // Suppress normal spelling autocorrect in domain/email text + canLearn = true, + showDomainChips = true, + isPrivate = false + ) + + FieldMode.URI -> FieldPolicy( + mode = mode, + canPredict = true, + canAutocorrect = false, // Conservative, no spelling autocorrect + canLearn = true, + showDomainChips = false, + isPrivate = false + ) + + FieldMode.PASSWORD, FieldMode.PIN, FieldMode.OTP -> FieldPolicy( + mode = mode, + canPredict = false, + canAutocorrect = false, + canLearn = false, + showDomainChips = false, + isPrivate = true + ) + + FieldMode.CODE, FieldMode.TERMINAL -> FieldPolicy( + mode = mode, + canPredict = true, + canAutocorrect = false, // Conservative correction + canLearn = false, + showDomainChips = false, + isPrivate = false + ) + + FieldMode.SEARCH -> FieldPolicy( + mode = mode, + canPredict = true, + canAutocorrect = true, + canLearn = true, + showDomainChips = false, + isPrivate = false + ) + + FieldMode.INCOGNITO -> FieldPolicy( + mode = mode, + canPredict = true, + canAutocorrect = true, + canLearn = false, + showDomainChips = false, + isPrivate = true + ) + } + + fun fromEditorInfo(info: EditorInfo?, forcedIncognitoApps: Set = emptySet()): FieldPolicy { + val pkg = info?.packageName.orEmpty() + if (forcedIncognitoApps.contains(pkg)) { + return resolve(FieldMode.INCOGNITO) + } + + val type = info?.inputType ?: 0 + val variation = type and InputType.TYPE_MASK_VARIATION + val klass = type and InputType.TYPE_MASK_CLASS + + if (klass == InputType.TYPE_CLASS_NUMBER && ( + variation == InputType.TYPE_NUMBER_VARIATION_PASSWORD || + variation == InputType.TYPE_NUMBER_FLAG_SIGNED + ) + ) { + return resolve(FieldMode.PIN) + } + + if (klass == InputType.TYPE_CLASS_TEXT) { + when (variation) { + InputType.TYPE_TEXT_VARIATION_PASSWORD, + InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD, + InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD -> return resolve(FieldMode.PASSWORD) + + InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS, + InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS -> return resolve(FieldMode.EMAIL) + + InputType.TYPE_TEXT_VARIATION_URI -> return resolve(FieldMode.URI) + + InputType.TYPE_TEXT_VARIATION_FILTER -> return resolve(FieldMode.SEARCH) + } + } + + val lowerPkg = pkg.lowercase() + if (lowerPkg.contains("termux") || lowerPkg.contains("juicessh") || lowerPkg.contains("connectbot") || lowerPkg.contains("terminal")) { + return resolve(FieldMode.TERMINAL) + } + + return resolve(FieldMode.NORMAL) + } +} diff --git a/app/src/main/java/com/mangoloads/expmango/LearningEngine.kt b/app/src/main/java/com/mangoloads/expmango/LearningEngine.kt new file mode 100644 index 0000000..1517002 --- /dev/null +++ b/app/src/main/java/com/mangoloads/expmango/LearningEngine.kt @@ -0,0 +1,204 @@ +package com.mangoloads.expmango + +import android.content.Context +import java.io.File +import java.util.Locale +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.Executors +import java.util.concurrent.ScheduledExecutorService +import java.util.concurrent.ScheduledFuture +import java.util.concurrent.TimeUnit + +data class LearnedWordStats( + val word: String, + @Volatile var frequency: Int = 1, + @Volatile var lastUsedTime: Long = System.currentTimeMillis() +) + +class LearningEngine(context: Context) { + + private val appContext = context.applicationContext + private val filesDir: File = try { + appContext.filesDir ?: File("build/test-tmp").apply { mkdirs() } + } catch (_: Exception) { + File("build/test-tmp").apply { mkdirs() } + } + + private val executor: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor() + + private val userWords = ConcurrentHashMap() + private val bigrams = ConcurrentHashMap>() + private val trigrams = ConcurrentHashMap>>() + + private val userWordsFile = File(filesDir, "mango_user_words.tsv") + private val bigramFile = File(filesDir, "mango_bigrams.tsv") + private val trigramFile = File(filesDir, "mango_trigrams.tsv") + + private var saveFuture: ScheduledFuture<*>? = null + + @Volatile + private var dirty = false + + init { + executor.execute { loadUserData() } + } + + fun learnWord(word: String, policy: FieldPolicy) { + if (!policy.canLearn || policy.isPrivate) return + val clean = word.trim().lowercase(Locale.ROOT) + if (clean.length < 2 || clean.any { it.isWhitespace() } || clean.any { it.isDigit() }) return + + val stats = userWords.computeIfAbsent(clean) { LearnedWordStats(clean) } + stats.frequency++ + stats.lastUsedTime = System.currentTimeMillis() + + dirty = true + scheduleSave() + } + + fun learnBigram(prev: String, current: String, policy: FieldPolicy) { + if (!policy.canLearn || policy.isPrivate) return + val w1 = prev.trim().lowercase(Locale.ROOT) + val w2 = current.trim().lowercase(Locale.ROOT) + if (w1.isBlank() || w2.isBlank() || w1.length < 2 || w2.length < 2) return + + val map = bigrams.computeIfAbsent(w1) { ConcurrentHashMap() } + map.compute(w2) { _, count -> (count ?: 0) + 1 } + + dirty = true + scheduleSave() + } + + fun learnTrigram(prev2: String, prev1: String, current: String, policy: FieldPolicy) { + if (!policy.canLearn || policy.isPrivate) return + val w1 = prev2.trim().lowercase(Locale.ROOT) + val w2 = prev1.trim().lowercase(Locale.ROOT) + val w3 = current.trim().lowercase(Locale.ROOT) + if (w1.isBlank() || w2.isBlank() || w3.isBlank()) return + + val outerMap = trigrams.computeIfAbsent(w1) { ConcurrentHashMap() } + val innerMap = outerMap.computeIfAbsent(w2) { ConcurrentHashMap() } + innerMap.compute(w3) { _, count -> (count ?: 0) + 1 } + + dirty = true + scheduleSave() + } + + fun getBigramNext(prev: String, limit: Int = 3): List> { + val key = prev.trim().lowercase(Locale.ROOT) + val map = bigrams[key] ?: return emptyList() + return map.entries + .sortedByDescending { it.value } + .take(limit) + .map { it.key to it.value } + } + + fun getTrigramNext(prev2: String, prev1: String, limit: Int = 3): List> { + val w1 = prev2.trim().lowercase(Locale.ROOT) + val w2 = prev1.trim().lowercase(Locale.ROOT) + val map = trigrams[w1]?.get(w2) ?: return emptyList() + return map.entries + .sortedByDescending { it.value } + .take(limit) + .map { it.key to it.value } + } + + fun containsWord(word: String): Boolean = + userWords.containsKey(word.lowercase(Locale.ROOT)) + + fun getWordFrequency(word: String): Int = + userWords[word.lowercase(Locale.ROOT)]?.frequency ?: 0 + + fun resetLearnedData() { + executor.execute { + userWords.clear() + bigrams.clear() + trigrams.clear() + userWordsFile.delete() + bigramFile.delete() + trigramFile.delete() + dirty = false + } + } + + fun shutdown() { + saveNow() + executor.shutdown() + } + + private fun scheduleSave() { + saveFuture?.cancel(false) + saveFuture = executor.schedule({ saveNow() }, 1000, TimeUnit.MILLISECONDS) + } + + @Synchronized + private fun saveNow() { + if (!dirty) return + try { + userWordsFile.printWriter().use { out -> + userWords.values.sortedByDescending { it.frequency }.take(10000).forEach { stats -> + out.println("${stats.word}\t${stats.frequency}\t${stats.lastUsedTime}") + } + } + + bigramFile.printWriter().use { out -> + bigrams.forEach { (prev, map) -> + map.forEach { (next, count) -> + out.println("$prev\t$next\t$count") + } + } + } + + trigramFile.printWriter().use { out -> + trigrams.forEach { (p1, map1) -> + map1.forEach { (p2, map2) -> + map2.forEach { (p3, count) -> + out.println("$p1\t$p2\t$p3\t$count") + } + } + } + } + + dirty = false + } catch (_: Exception) { + } + } + + private fun loadUserData() { + try { + if (userWordsFile.exists()) { + userWordsFile.forEachLine { line -> + val parts = line.split('\t') + if (parts.size >= 2) { + val word = parts[0] + val freq = parts[1].toIntOrNull() ?: 1 + val time = parts.getOrNull(2)?.toLongOrNull() ?: System.currentTimeMillis() + userWords[word] = LearnedWordStats(word, freq, time) + } + } + } + + if (bigramFile.exists()) { + bigramFile.forEachLine { line -> + val parts = line.split('\t') + if (parts.size >= 3) { + val map = bigrams.computeIfAbsent(parts[0]) { ConcurrentHashMap() } + map[parts[1]] = parts[2].toIntOrNull() ?: 1 + } + } + } + + if (trigramFile.exists()) { + trigramFile.forEachLine { line -> + val parts = line.split('\t') + if (parts.size >= 4) { + val map1 = trigrams.computeIfAbsent(parts[0]) { ConcurrentHashMap() } + val map2 = map1.computeIfAbsent(parts[1]) { ConcurrentHashMap() } + map2[parts[2]] = parts[3].toIntOrNull() ?: 1 + } + } + } + } catch (_: Exception) { + } + } +} diff --git a/app/src/main/java/com/mangoloads/expmango/Lexicon.kt b/app/src/main/java/com/mangoloads/expmango/Lexicon.kt index 203bf33..f78979c 100644 --- a/app/src/main/java/com/mangoloads/expmango/Lexicon.kt +++ b/app/src/main/java/com/mangoloads/expmango/Lexicon.kt @@ -23,7 +23,9 @@ class PrefixTrie { var node = root for (c in word) { node = node.children.getOrPut(c) { Node() } - if (node.words.size < 32 && !node.words.contains(word)) node.words.add(word) + if (node.words.size < 48 && !node.words.contains(word)) { + node.words.add(word) + } } } @@ -44,7 +46,7 @@ class Lexicon { fun add(word: String, frequency: Int = 1) { val w = word.trim().lowercase(Locale.ROOT) - if (w.length < 2 || w.any { it.isWhitespace() }) return + if (w.length < 1 || w.any { it.isWhitespace() }) return val existing = words[w] if (existing != null) { existing.frequency += frequency.coerceAtLeast(1) @@ -72,7 +74,7 @@ class Lexicon { val first = w.firstOrNull() ?: return emptyList() val result = ArrayList(48) - for (length in maxOf(2, w.length - 2)..w.length + 2) { + for (length in maxOf(1, w.length - 2)..w.length + 2) { val bucket = buckets[bucketKey(first, length)] ?: continue synchronized(bucket) { for (candidate in bucket) { @@ -93,7 +95,7 @@ class Lexicon { bucketKey(word.firstOrNull() ?: '_', word.length) private fun bucketKey(first: Char, length: Int): String = - first.toString() + ":" + length + "$first:$length" private fun boundedDistance(a: String, b: String, max: Int): Int { if (abs(a.length - b.length) > max) return max + 1 diff --git a/app/src/main/java/com/mangoloads/expmango/MainActivity.kt b/app/src/main/java/com/mangoloads/expmango/MainActivity.kt index 61a5ac5..d98b290 100644 --- a/app/src/main/java/com/mangoloads/expmango/MainActivity.kt +++ b/app/src/main/java/com/mangoloads/expmango/MainActivity.kt @@ -1,64 +1,158 @@ package com.mangoloads.expmango import android.app.Activity +import android.content.Context import android.content.Intent import android.os.Bundle import android.provider.Settings import android.view.Gravity +import android.view.View import android.view.ViewGroup +import android.widget.ArrayAdapter import android.widget.Button +import android.widget.EditText import android.widget.LinearLayout +import android.widget.ScrollView +import android.widget.Spinner import android.widget.TextView +import android.widget.Toast class MainActivity : Activity() { + + private lateinit var dictionaryManager: DictionaryManager + private lateinit var learningEngine: LearningEngine + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + dictionaryManager = DictionaryManager(this) + learningEngine = LearningEngine(this) + + val rootScroll = ScrollView(this).apply { + setBackgroundColor(0xFF111114.toInt()) + isFillViewport = true + } - val root = LinearLayout(this).apply { + val container = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL - gravity = Gravity.CENTER_HORIZONTAL - setPadding(32, 48, 32, 32) - setBackgroundColor(0xFF0A0A0B.toInt()) + setPadding(32, 48, 32, 48) } - val title = TextView(this).apply { - text = "expBoard" + // Header + val titleView = TextView(this).apply { + text = "EXP Mango" textSize = 28f - setTextColor(0xFFF5F5F7.toInt()) - gravity = Gravity.CENTER + setTextColor(0xFFFFB24A.toInt()) // EXP Mango Accent + gravity = Gravity.CENTER_HORIZONTAL } - val info = TextView(this).apply { - text = "Fast local-first Hinglish keyboard.\n\n" + - "Typing never waits for a model or disk write. " + - "Vocabulary and phrase learning are persisted asynchronously. " + - "Password and sensitive fields disable learning and autocorrect." - textSize = 16f - setTextColor(0xFFB8B8BE.toInt()) - gravity = Gravity.CENTER - setPadding(0, 24, 0, 24) + val subtitleView = TextView(this).apply { + text = "Fast, Local-First, Offline English & Hinglish Keyboard" + textSize = 14f + setTextColor(0xFFA5A5AE.toInt()) + gravity = Gravity.CENTER_HORIZONTAL + setPadding(0, 8, 0, 32) } - val button = Button(this).apply { - text = "Open keyboard settings" + // IME Enable Button + val enableImeButton = Button(this).apply { + text = "⚙️ Enable Keyboard Settings" + setBackgroundColor(0xFF242428.toInt()) + setTextColor(0xFFF4F4F7.toInt()) setOnClickListener { startActivity(Intent(Settings.ACTION_INPUT_METHOD_SETTINGS)) } } - root.addView(title, LinearLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT - )) - root.addView(info, LinearLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT - )) - root.addView(button, LinearLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT - )) - - setContentView(root) + // Dictionary Manager Section + val dictHeader = createSectionHeader("📚 Custom Dictionary Manager") + val wordInput = EditText(this).apply { + hint = "Add new word or Hinglish slang..." + setHintTextColor(0xFF888888.toInt()) + setTextColor(0xFFFFFFFF.toInt()) + setBackgroundColor(0xFF242428.toInt()) + setPadding(24, 24, 24, 24) + } + + val categorySpinner = Spinner(this).apply { + val categories = WordCategory.values().map { it.displayName } + adapter = ArrayAdapter(this@MainActivity, android.R.layout.simple_spinner_dropdown_item, categories) + setBackgroundColor(0xFF242428.toInt()) + } + + val addWordButton = Button(this).apply { + text = "+ Add Word" + setBackgroundColor(0xFFFF9F2D.toInt()) + setTextColor(0xFF111114.toInt()) + setOnClickListener { + val typed = wordInput.text.toString().trim() + if (typed.isNotBlank()) { + val selectedCategory = WordCategory.values()[categorySpinner.selectedItemPosition] + dictionaryManager.addWord(typed, selectedCategory) + wordInput.text.clear() + Toast.makeText(this@MainActivity, "Added '$typed' to ${selectedCategory.displayName}", Toast.LENGTH_SHORT).show() + } + } + } + + // Privacy Section + val privacyHeader = createSectionHeader("🔒 Privacy & Incognito") + val privacyText = TextView(this).apply { + text = "• 100% Offline — No Internet permission\n" + + "• No cloud AI, analytics, or remote tracking\n" + + "• Password, PIN, and OTP fields automatically disable learning\n" + + "• Per-App forced Incognito support" + textSize = 13f + setTextColor(0xFFA5A5AE.toInt()) + setPadding(0, 8, 0, 16) + } + + val resetDataButton = Button(this).apply { + text = "🗑️ Reset Learned Data" + setBackgroundColor(0xFF3C3C44.toInt()) + setTextColor(0xFFFF6B6B.toInt()) + setOnClickListener { + learningEngine.resetLearnedData() + dictionaryManager.reset() + Toast.makeText(this@MainActivity, "Learned data and custom dictionary reset", Toast.LENGTH_SHORT).show() + } + } + + // About & Licenses Section + val aboutHeader = createSectionHeader("ℹ️ About & Open Source Licenses") + val aboutText = TextView(this).apply { + text = "EXP Mango Keyboard v1.0.0\n" + + "Application ID: com.mangoloads.expmango\n\n" + + "EXP Mango is built on permissively licensed open-source components.\n" + + "Upstream Foundation: WM Keyboard (MIT License, Wasi Master).\n" + + "All typing intelligence, Hinglish ranking, and prediction engine belong to EXP Mango." + textSize = 12f + setTextColor(0xFFA5A5AE.toInt()) + setPadding(0, 8, 0, 32) + } + + container.addView(titleView) + container.addView(subtitleView) + container.addView(enableImeButton) + container.addView(dictHeader) + container.addView(wordInput) + container.addView(categorySpinner) + container.addView(addWordButton) + container.addView(privacyHeader) + container.addView(privacyText) + container.addView(resetDataButton) + container.addView(aboutHeader) + container.addView(aboutText) + + rootScroll.addView(container) + setContentView(rootScroll) + } + + private fun createSectionHeader(title: String): TextView { + return TextView(this).apply { + text = title + textSize = 18f + setTextColor(0xFFF4F4F7.toInt()) + setPadding(0, 32, 0, 12) + } } } diff --git a/app/src/main/java/com/mangoloads/expmango/MangoImeService.kt b/app/src/main/java/com/mangoloads/expmango/MangoImeService.kt index 1d2f930..35fc403 100644 --- a/app/src/main/java/com/mangoloads/expmango/MangoImeService.kt +++ b/app/src/main/java/com/mangoloads/expmango/MangoImeService.kt @@ -10,7 +10,7 @@ class MangoImeService : InputMethodService() { private lateinit var engine: PredictionEngine private lateinit var keyboard: MangoKeyboardView - private var inputContext = ImeContext("", InputPolicy.NORMAL) + private var fieldPolicy = FieldPolicyResolver.resolve(FieldMode.NORMAL) private var wordBuffer = StringBuilder() private var lastAutoCorrection: Pair? = null private var shift = false @@ -28,7 +28,7 @@ class MangoImeService : InputMethodService() { override fun onStartInput(info: EditorInfo?, restarting: Boolean) { super.onStartInput(info, restarting) - inputContext = ImeContext.from(info) + fieldPolicy = FieldPolicyResolver.fromEditorInfo(info) wordBuffer = StringBuilder() lastAutoCorrection = null shift = false @@ -87,14 +87,14 @@ class MangoImeService : InputMethodService() { val typed = wordBuffer.toString() val previous = previousWord(ic) - if (inputContext.canCorrect) { - engine.correct(typed, previous)?.let { correction -> + if (fieldPolicy.canAutocorrect) { + engine.correct(typed, previous, policy = fieldPolicy)?.let { correction -> if (correction.word.lowercase(Locale.ROOT) != typed.lowercase(Locale.ROOT)) { ic.deleteSurroundingText(typed.length, 0) ic.commitText(correction.word + " ", 1) lastAutoCorrection = typed to correction.word - if (inputContext.canLearn) { - engine.learn(correction.word, previous) + if (fieldPolicy.canLearn) { + engine.learn(correction.word, previous, policy = fieldPolicy) } wordBuffer.clear() refreshSuggestions() @@ -105,8 +105,8 @@ class MangoImeService : InputMethodService() { ic.commitText(" ", 1) - if (inputContext.canLearn && typed.isNotBlank()) { - engine.learn(typed, previous) + if (fieldPolicy.canLearn && typed.isNotBlank()) { + engine.learn(typed, previous, policy = fieldPolicy) } wordBuffer.clear() @@ -133,8 +133,8 @@ class MangoImeService : InputMethodService() { ic.commitText(word + " ", 1) - if (inputContext.canLearn) { - engine.learn(word, previous) + if (fieldPolicy.canLearn) { + engine.learn(word, previous, policy = fieldPolicy) } wordBuffer.clear() @@ -146,7 +146,7 @@ class MangoImeService : InputMethodService() { val ic = currentInputConnection ?: return val previous = previousWord(ic) - val prediction = engine.glide(raw, previous) + val prediction = engine.glide(raw, previous, policy = fieldPolicy) if (prediction == null) { for (c in raw) onCharacter(c) @@ -155,8 +155,8 @@ class MangoImeService : InputMethodService() { ic.commitText(prediction.word + " ", 1) - if (inputContext.canLearn) { - engine.learn(prediction.word, previous) + if (fieldPolicy.canLearn) { + engine.learn(prediction.word, previous, policy = fieldPolicy) } wordBuffer.clear() @@ -171,12 +171,14 @@ class MangoImeService : InputMethodService() { fun currentShifted(): Boolean = shift || capsLock + fun getFieldPolicy(): FieldPolicy = fieldPolicy + fun refreshSuggestions() { if (!::keyboard.isInitialized) return val previous = previousWord(currentInputConnection) - val list = engine.suggestions(wordBuffer.toString(), previous, 3) + val candidates = engine.suggestions(wordBuffer.toString(), previous1 = previous, policy = fieldPolicy, limit = 3) .map { it.word } - keyboard.setSuggestions(list) + keyboard.setSuggestions(candidates, fieldPolicy.isPrivate) } private fun previousWord(ic: InputConnection?): String? { diff --git a/app/src/main/java/com/mangoloads/expmango/MangoKeyboardView.kt b/app/src/main/java/com/mangoloads/expmango/MangoKeyboardView.kt index 7a5d6a3..e83d75b 100644 --- a/app/src/main/java/com/mangoloads/expmango/MangoKeyboardView.kt +++ b/app/src/main/java/com/mangoloads/expmango/MangoKeyboardView.kt @@ -4,12 +4,15 @@ import android.content.Context import android.graphics.Canvas import android.graphics.Paint import android.graphics.RectF +import android.view.HapticFeedbackConstants import android.view.MotionEvent import android.view.View +import kotlin.math.abs import kotlin.math.hypot import kotlin.math.max class MangoKeyboardView(context: Context) : View(context) { + private data class Key( val label: String, val x: Float, @@ -19,30 +22,38 @@ class MangoKeyboardView(context: Context) : View(context) { ) private val density = resources.displayMetrics.density - private val bg = Paint(Paint.ANTI_ALIAS_FLAG).apply { - color = 0xFF0A0A0B.toInt() - } - private val keyPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { - color = 0xFF19191C.toInt() - } - private val pressedPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { - color = 0xFF303035.toInt() - } + + private val bg = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = 0xFF111114.toInt() } + private val keyPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = 0xFF242428.toInt() } + private val keyPressedPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = 0xFF3C3C44.toInt() } + private val accentPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = 0xFFFFB24A.toInt() } private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { - color = 0xFFF5F5F7.toInt() + color = 0xFFF4F4F7.toInt() textAlign = Paint.Align.CENTER } private val suggestionPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { - color = 0xFFE7E7EA.toInt() + color = 0xFFFFFFFF.toInt() + textAlign = Paint.Align.CENTER + } + private val lockPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + color = 0xFFA99BFF.toInt() + textAlign = Paint.Align.CENTER + } + private val toolbarPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + color = 0xFFA5A5AE.toInt() textAlign = Paint.Align.CENTER } private val keys = ArrayList() private var suggestions = emptyList() + private var isIncognito = false private var pressedIndex = -1 private var downX = 0f private var downY = 0f private var moved = false + private var isSpaceDrag = false + private var spaceDragLastX = 0f + private val glide = StringBuilder() private val service = context as? MangoImeService @@ -50,8 +61,9 @@ class MangoKeyboardView(context: Context) : View(context) { setBackgroundColor(bg.color) } - fun setSuggestions(words: List) { + fun setSuggestions(words: List, isPrivate: Boolean = false) { suggestions = words + isIncognito = isPrivate invalidate() } @@ -60,7 +72,22 @@ class MangoKeyboardView(context: Context) : View(context) { canvas.drawColor(bg.color) rebuildKeys() - val suggestionH = 46f * density + val toolbarH = 36f * density + val suggestionH = 42f * density + + // Draw Toolbar + toolbarPaint.textSize = 16f * density + canvas.drawText("😊", width * 0.15f, toolbarH * 0.65f, toolbarPaint) + canvas.drawText("🌐", width * 0.38f, toolbarH * 0.65f, toolbarPaint) + canvas.drawText("📋", width * 0.62f, toolbarH * 0.65f, toolbarPaint) + canvas.drawText("⚙️", width * 0.85f, toolbarH * 0.65f, toolbarPaint) + + // Draw Suggestion Strip + val suggestionY = toolbarH + if (isIncognito) { + lockPaint.textSize = 12f * density + canvas.drawText("🔒 Incognito", 42f * density, suggestionY + suggestionH * 0.65f, lockPaint) + } textPaint.textSize = 15f * density for (i in suggestions.indices.take(3)) { @@ -68,28 +95,37 @@ class MangoKeyboardView(context: Context) : View(context) { canvas.drawText( suggestions[i], x, - suggestionH * 0.67f, + suggestionY + suggestionH * 0.67f, suggestionPaint ) } + // Draw Keys for ((i, key) in keys.withIndex()) { - val paint = if (i == pressedIndex) pressedPaint else keyPaint + val isPressed = (i == pressedIndex) + val paint = when { + isPressed -> keyPressedPaint + key.label == "ENTER" -> accentPaint + else -> keyPaint + } + val rect = RectF( - key.x + 2 * density, - key.y + 2 * density, - key.x + key.width - 2 * density, - key.y + key.height - 2 * density + key.x + 3 * density, + key.y + 3 * density, + key.x + key.width - 3 * density, + key.y + key.height - 3 * density ) + canvas.drawRoundRect( rect, - 10 * density, - 10 * density, + 12 * density, // 12dp squircle corner radius + 12 * density, paint ) + textPaint.color = if (key.label == "ENTER") 0xFF111114.toInt() else 0xFFF4F4F7.toInt() textPaint.textSize = when { - key.label == "SPACE" -> 14f * density + key.label == "SPACE" -> 13f * density key.label.length > 4 -> 12f * density else -> 18f * density } @@ -109,11 +145,20 @@ class MangoKeyboardView(context: Context) : View(context) { downX = event.x downY = event.y moved = false + isSpaceDrag = false glide.clear() pressedIndex = findKey(event.x, event.y) + + try { + performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY) + } catch (_: Exception) {} + if (pressedIndex >= 0) { val label = keys[pressedIndex].label - if (label.length == 1 && label[0].isLetter()) { + if (label == "SPACE") { + isSpaceDrag = true + spaceDragLastX = event.x + } else if (label.length == 1 && label[0].isLetter()) { glide.append(label.lowercase()) } } @@ -122,11 +167,28 @@ class MangoKeyboardView(context: Context) : View(context) { } MotionEvent.ACTION_MOVE -> { - if (hypot(event.x - downX, event.y - downY) > 30f * density) { + val dx = event.x - downX + val dy = event.y - downY + if (hypot(dx, dy) > 24f * density) { moved = true } - if (moved) { + if (isSpaceDrag) { + val dragDx = event.x - spaceDragLastX + if (abs(dragDx) > 16f * density) { + val ic = service?.currentInputConnection + if (ic != null) { + if (dragDx > 0) { + ic.sendKeyEvent(android.view.KeyEvent(android.view.KeyEvent.ACTION_DOWN, android.view.KeyEvent.KEYCODE_DPAD_RIGHT)) + ic.sendKeyEvent(android.view.KeyEvent(android.view.KeyEvent.ACTION_UP, android.view.KeyEvent.KEYCODE_DPAD_RIGHT)) + } else { + ic.sendKeyEvent(android.view.KeyEvent(android.view.KeyEvent.ACTION_DOWN, android.view.KeyEvent.KEYCODE_DPAD_LEFT)) + ic.sendKeyEvent(android.view.KeyEvent(android.view.KeyEvent.ACTION_UP, android.view.KeyEvent.KEYCODE_DPAD_LEFT)) + } + } + spaceDragLastX = event.x + } + } else if (moved) { val idx = findKey(event.x, event.y) if (idx >= 0) { val label = keys[idx].label @@ -144,15 +206,16 @@ class MangoKeyboardView(context: Context) : View(context) { MotionEvent.ACTION_UP -> { val idx = findKey(event.x, event.y) - val wasGlide = moved && glide.length >= 3 + val wasGlide = moved && glide.length >= 3 && !isSpaceDrag pressedIndex = -1 + isSpaceDrag = false invalidate() if (wasGlide) { service?.onGlide(glide.toString()) - } else { - handleTap(idx) + } else if (!moved) { + handleTap(idx, event.x, event.y) } return true @@ -160,6 +223,7 @@ class MangoKeyboardView(context: Context) : View(context) { MotionEvent.ACTION_CANCEL -> { pressedIndex = -1 + isSpaceDrag = false invalidate() return true } @@ -168,7 +232,36 @@ class MangoKeyboardView(context: Context) : View(context) { return true } - private fun handleTap(index: Int) { + private fun handleTap(index: Int, x: Float, y: Float) { + val toolbarH = 36f * density + val suggestionH = 42f * density + + // Handle Toolbar Taps + if (y < toolbarH) { + val section = (x / (width / 4f)).toInt().coerceIn(0, 3) + when (section) { + 0 -> service?.currentInputConnection?.commitText("😊", 1) + 1 -> service?.onSpace() + 2 -> service?.onSpace() + 3 -> { + val intent = android.content.Intent(context, MainActivity::class.java).apply { + flags = android.content.Intent.FLAG_ACTIVITY_NEW_TASK + } + context.startActivity(intent) + } + } + return + } + + // Handle Suggestion Strip Taps + if (y < toolbarH + suggestionH) { + val col = (x / (width / 3f)).toInt().coerceIn(0, 2) + if (col in suggestions.indices) { + service?.onSuggestion(suggestions[col]) + return + } + } + if (index < 0) return when (val label = keys[index].label) { @@ -194,8 +287,10 @@ class MangoKeyboardView(context: Context) : View(context) { private fun rebuildKeys() { keys.clear() - val suggestionH = 46f * density - val rowH = max(46f * density, (height - suggestionH) / 4f) + val toolbarH = 36f * density + val suggestionH = 42f * density + val startY = toolbarH + suggestionH + val rowH = max(48f * density, (height - startY) / 4f) val rows = listOf( listOf("q", "w", "e", "r", "t", "y", "u", "i", "o", "p"), @@ -205,7 +300,7 @@ class MangoKeyboardView(context: Context) : View(context) { ) rows.forEachIndexed { rowIndex, row -> - val y = suggestionH + rowIndex * rowH + val y = startY + rowIndex * rowH val totalWeight = row.sumOf { weight(it).toDouble() }.toFloat() var x = 0f diff --git a/app/src/main/java/com/mangoloads/expmango/PredictionEngine.kt b/app/src/main/java/com/mangoloads/expmango/PredictionEngine.kt index b84ca14..99581d7 100644 --- a/app/src/main/java/com/mangoloads/expmango/PredictionEngine.kt +++ b/app/src/main/java/com/mangoloads/expmango/PredictionEngine.kt @@ -3,261 +3,289 @@ package com.mangoloads.expmango import android.content.Context import java.io.File import java.util.Locale -import java.util.concurrent.ConcurrentHashMap -import java.util.concurrent.Executors -import java.util.concurrent.ScheduledExecutorService -import java.util.concurrent.ScheduledFuture -import java.util.concurrent.TimeUnit -import kotlin.math.abs - -data class Prediction( + +data class Candidate( val word: String, - val score: Int, - val reason: String + val score: Double, + val type: String = "prediction" ) class PredictionEngine(context: Context) { + private val appContext = context.applicationContext private val lexicon = Lexicon() - private val bigrams = - ConcurrentHashMap>() + val emailDomainProvider = EmailDomainProvider() + val dictionaryManager = DictionaryManager(appContext) + val learningEngine = LearningEngine(appContext) - private val executor: ScheduledExecutorService = - Executors.newSingleThreadScheduledExecutor() + init { + loadAssets() + loadCustomDictionary() + } - private var saveFuture: ScheduledFuture<*>? = null + fun suggestions( + input: String, + previous1: String? = null, + previous2: String? = null, + policy: FieldPolicy = FieldPolicyResolver.resolve(FieldMode.NORMAL), + limit: Int = 3 + ): List { + if (policy.isPrivate && (policy.mode == FieldMode.PASSWORD || policy.mode == FieldMode.PIN || policy.mode == FieldMode.OTP)) { + return emptyList() + } - @Volatile - private var dirty = false + if (policy.showDomainChips && input.contains('@')) { + val domains = emailDomainProvider.getDomainSuggestions(input, limit) + return domains.map { Candidate(it, 100.0, "email-domain") } + } - private val userFile = File(appContext.filesDir, "mango_words.tsv") - private val bigramFile = File(appContext.filesDir, "mango_bigrams.tsv") + val token = input.trim().lowercase(Locale.ROOT) - init { - loadAsset("dictionary.txt") - loadAsset("hinglish.txt") - loadUserData() - } + if (token.isBlank()) { + return getContextNextWords(previous1, previous2, limit) + } - fun suggestions(prefix: String, previous: String?, limit: Int = 3): List { - val p = prefix.lowercase(Locale.ROOT) - if (p.isBlank()) return nextWord(previous, limit) + val candidates = mutableMapOf() - return lexicon.prefix(p, 24) - .map { word -> - var score = 100 - (word.length - p.length) * 3 - score += minOf(lexicon.frequency(word), 30) - val context = previous?.let { - bigrams[it.lowercase(Locale.ROOT)]?.get(word) ?: 0 - } ?: 0 - score += minOf(context * 5, 25) - Prediction(word, score, "prefix") + val prefixMatches = lexicon.prefix(token, 24) + prefixMatches.forEach { word -> + val score = scoreCandidate(word, token, previous1, previous2, policy) + candidates[word] = Candidate(word, score, "prefix") + } + + if (policy.canAutocorrect) { + TypoEngine.exactTypos[token]?.let { fixed -> + val score = 100.0 + candidates[fixed] = Candidate(fixed, score, "autocorrect") } - .sortedByDescending { it.score } - .take(limit) - } + } - fun nextWord(previous: String?, limit: Int = 3): List { - val key = previous?.trim()?.lowercase(Locale.ROOT).orEmpty() - if (key.isBlank()) return emptyList() + if (candidates.size < limit && policy.canAutocorrect && token.length >= 3 && !lexicon.contains(token)) { + val fuzzy = lexicon.fuzzyCandidates(token) + fuzzy.forEach { word -> + if (!candidates.containsKey(word)) { + val score = scoreCandidate(word, token, previous1, previous2, policy) + candidates[word] = Candidate(word, score, "typo") + } + } + } - return (bigrams[key] ?: return emptyList()) - .entries - .sortedByDescending { it.value } + return candidates.values + .sortedByDescending { it.score } .take(limit) - .map { Prediction(it.key, it.value, "learned phrase") } } - fun correct(word: String, previous: String?): Prediction? { - val original = word.trim() - if (original.length < 3) return null - if (original.any { !it.isLetter() }) return null - if (original.all { it.isUpperCase() }) return null - if (lexicon.contains(original)) return null + fun correct( + typed: String, + previous1: String? = null, + previous2: String? = null, + policy: FieldPolicy = FieldPolicyResolver.resolve(FieldMode.NORMAL) + ): Candidate? { + val original = typed.trim() + if (original.isBlank()) return null + + if (!policy.canAutocorrect || policy.mode in setOf( + FieldMode.URI, + FieldMode.EMAIL, + FieldMode.CODE, + FieldMode.TERMINAL, + FieldMode.PASSWORD, + FieldMode.PIN, + FieldMode.OTP + ) + ) { + return null + } val lower = original.lowercase(Locale.ROOT) - val exactTypos = mapOf( - "teh" to "the", - "hte" to "the", - "adn" to "and", - "nad" to "and", - "taht" to "that", - "thier" to "their", - "wierd" to "weird", - "becuase" to "because", - "beacuse" to "because", - "recieve" to "receive", - "seperate" to "separate", - "definately" to "definitely", - "occured" to "occurred", - "goign" to "going", - "woudl" to "would", - "dont" to "don't", - "cant" to "can't", - "wont" to "won't", - "im" to "I'm", - "ive" to "I've" - ) - - exactTypos[lower]?.let { - return Prediction(it, 100, "known typo") + if (lexicon.contains(lower) || learningEngine.containsWord(lower)) { + return null + } + + TypoEngine.exactTypos[lower]?.let { fixed -> + return Candidate(fixed, 100.0, "exact-typo") } + if (lower.length < 3) return null + val candidates = lexicon.fuzzyCandidates(lower) - if (candidates.isEmpty()) return null - - val best = candidates.maxByOrNull { candidate -> - val d = distance(lower, candidate) - val freq = lexicon.frequency(candidate) - val context = previous?.let { - bigrams[it.lowercase(Locale.ROOT)]?.get(candidate) ?: 0 - } ?: 0 - 40 - d * 12 + minOf(freq, 40) + minOf(context * 4, 24) - } ?: return null - - val d = distance(lower, best) - val contextBoost = previous?.let { - (bigrams[it.lowercase(Locale.ROOT)]?.get(best) ?: 0) * 4 - } ?: 0 - val score = 70 + contextBoost + minOf(lexicon.frequency(best), 20) - d * 12 - - return if (score >= 58) Prediction(best, score, "fast fuzzy") else null + var bestCandidate: String? = null + var minDistance = 3.0 + + for (cand in candidates) { + val dist = TypoEngine.editDistance(lower, cand) + if (dist < minDistance) { + minDistance = dist + bestCandidate = cand + } + } + + return bestCandidate?.let { + Candidate(it, 80.0 - minDistance * 10, "fuzzy-autocorrect") + } } - fun glide(raw: String, previous: String?): Prediction? { + fun glide( + raw: String, + previous1: String? = null, + policy: FieldPolicy = FieldPolicyResolver.resolve(FieldMode.NORMAL) + ): Candidate? { val seq = raw.lowercase(Locale.ROOT) if (seq.length < 3) return null return lexicon.fuzzyCandidates(seq) .map { word -> - val d = distance(seq, word) + val dist = TypoEngine.editDistance(seq, word) val freq = lexicon.frequency(word) - val context = previous?.let { - bigrams[it.lowercase(Locale.ROOT)]?.get(word) ?: 0 - } ?: 0 - Prediction( - word, - 85 - d * 10 + minOf(freq, 25) + minOf(context * 5, 20), - "glide" - ) + Candidate(word, 85.0 - dist * 10.0 + minOf(freq, 25), "glide") } .maxByOrNull { it.score } - ?.takeIf { it.score >= 48 } + ?.takeIf { it.score >= 48.0 } } - fun learn(word: String, previous: String?) { + fun learn( + word: String, + previous1: String? = null, + previous2: String? = null, + policy: FieldPolicy = FieldPolicyResolver.resolve(FieldMode.NORMAL) + ) { + if (!policy.canLearn || policy.isPrivate) return val clean = word.trim().lowercase(Locale.ROOT) - if (clean.length < 2 || clean.any { it.isWhitespace() }) return + if (clean.isBlank()) return lexicon.add(clean) + learningEngine.learnWord(clean, policy) - previous?.trim()?.lowercase(Locale.ROOT)?.takeIf { it.isNotEmpty() }?.let { prev -> - val map = bigrams.computeIfAbsent(prev) { - ConcurrentHashMap() - } - map.compute(clean) { _, value -> (value ?: 0) + 1 } - } + val p1 = previous1?.trim()?.lowercase(Locale.ROOT) + val p2 = previous2?.trim()?.lowercase(Locale.ROOT) - dirty = true - scheduleSave() + if (!p1.isNullOrEmpty()) { + learningEngine.learnBigram(p1, clean, policy) + } + if (!p1.isNullOrEmpty() && !p2.isNullOrEmpty()) { + learningEngine.learnTrigram(p2, p1, clean, policy) + } } fun shutdown() { - saveNow() - executor.shutdownNow() + learningEngine.shutdown() } - private fun scheduleSave() { - saveFuture?.cancel(false) - saveFuture = executor.schedule({ - saveNow() - }, 900, TimeUnit.MILLISECONDS) - } + private fun getContextNextWords( + previous1: String?, + previous2: String?, + limit: Int + ): List { + val candidates = mutableListOf() - private fun saveNow() { - if (!dirty) return + val p1 = previous1?.trim()?.lowercase(Locale.ROOT) + val p2 = previous2?.trim()?.lowercase(Locale.ROOT) - try { - userFile.printWriter().use { out -> - for (word in lexicon.allWords().sorted()) { - out.println(word + "\t" + lexicon.frequency(word)) - } + if (!p1.isNullOrEmpty() && !p2.isNullOrEmpty()) { + val triResults = learningEngine.getTrigramNext(p2, p1, limit) + triResults.forEach { (word, count) -> + candidates.add(Candidate(word, 90.0 + count, "trigram")) } + } - bigramFile.printWriter().use { out -> - for ((prev, map) in bigrams) { - for ((next, count) in map) { - out.println(prev + "\t" + next + "\t" + count) - } + if (candidates.size < limit && !p1.isNullOrEmpty()) { + val biResults = learningEngine.getBigramNext(p1, limit - candidates.size) + biResults.forEach { (word, count) -> + if (candidates.none { it.word == word }) { + candidates.add(Candidate(word, 70.0 + count, "bigram")) } } - - dirty = false - } catch (_: Exception) { - // Never let persistence affect typing. } - } - private fun loadAsset(name: String) { - try { - appContext.assets.open(name).bufferedReader().useLines { lines -> - lines.forEach { line -> - val word = line.trim().lowercase(Locale.ROOT) - if (word.isNotEmpty() && !word.startsWith("#")) lexicon.add(word) + if (candidates.size < limit && !p1.isNullOrEmpty()) { + val fallbacks = when (p1) { + "going" -> listOf("to", "home", "back") + "not" -> listOf("sure", "yet", "ready") + "is" -> listOf("the", "a", "not") + "rights" -> listOf("and", "in", "for") + "fundamental" -> listOf("rights") + "polity" -> listOf("upsc", "and") + "open" -> listOf("ai") + "climate" -> listOf("change") + "monetary" -> listOf("policy") + "repo" -> listOf("rate") + else -> emptyList() + } + fallbacks.forEach { fb -> + if (candidates.none { it.word == fb }) { + candidates.add(Candidate(fb, 50.0, "context-fallback")) } } - } catch (_: Exception) { } + + return candidates.take(limit) + } + + private fun scoreCandidate( + word: String, + prefix: String, + previous1: String?, + previous2: String?, + policy: FieldPolicy + ): Double { + var score = 50.0 + + val lenDiff = word.length - prefix.length + score -= lenDiff * 2.0 + + val globalFreq = lexicon.frequency(word) + val personalFreq = learningEngine.getWordFrequency(word) + score += minOf(globalFreq, 30) * 0.5 + score += minOf(personalFreq, 50) * 1.0 + + val p1 = previous1?.trim()?.lowercase(Locale.ROOT) + if (!p1.isNullOrEmpty()) { + val biMap = learningEngine.getBigramNext(p1, 10).toMap() + val biCount = biMap[word] ?: 0 + score += minOf(biCount * 5, 25) + } + + return score + } + + private fun loadAssets() { + loadAssetFile("dictionary.txt") + loadAssetFile("hinglish.txt") } - private fun loadUserData() { + private fun loadAssetFile(filename: String) { try { - if (userFile.exists()) { - userFile.forEachLine { line -> - val parts = line.split('\t') - if (parts.size >= 2) { - lexicon.add(parts[0], parts[1].toIntOrNull() ?: 1) + appContext.assets.open(filename).bufferedReader().useLines { lines -> + lines.forEach { line -> + val trimmed = line.trim().lowercase(Locale.ROOT) + if (trimmed.isNotBlank() && !trimmed.startsWith("#")) { + val parts = trimmed.split(Regex("\\s+")) + val word = parts[0] + val freq = parts.getOrNull(1)?.toIntOrNull() ?: 1 + lexicon.add(word, freq) } } } - - if (bigramFile.exists()) { - bigramFile.forEachLine { line -> - val parts = line.split('\t') - if (parts.size >= 3) { - val map = bigrams.computeIfAbsent(parts[0]) { - ConcurrentHashMap() - } - map[parts[1]] = parts[2].toIntOrNull() ?: 1 - } + } catch (_: Exception) { + val file = File("app/src/main/assets/$filename").takeIf { it.exists() } + ?: File("src/main/assets/$filename").takeIf { it.exists() } + file?.forEachLine { line -> + val trimmed = line.trim().lowercase(Locale.ROOT) + if (trimmed.isNotBlank() && !trimmed.startsWith("#")) { + val parts = trimmed.split(Regex("\\s+")) + val word = parts[0] + val freq = parts.getOrNull(1)?.toIntOrNull() ?: 1 + lexicon.add(word, freq) } } - } catch (_: Exception) { } } - private fun distance(a: String, b: String): Int { - if (a == b) return 0 - if (abs(a.length - b.length) > 3) return 4 - - var prev = IntArray(b.length + 1) { it } - var cur = IntArray(b.length + 1) - - for (i in a.indices) { - cur[0] = i + 1 - for (j in b.indices) { - val cost = if (a[i] == b[j]) 0 else 1 - cur[j + 1] = minOf( - cur[j] + 1, - prev[j + 1] + 1, - prev[j] + cost - ) - } - val tmp = prev - prev = cur - cur = tmp + private fun loadCustomDictionary() { + val custom = dictionaryManager.getWords() + custom.forEach { cw -> + lexicon.add(cw.word, cw.frequency) } - return prev[b.length] } } diff --git a/app/src/main/java/com/mangoloads/expmango/TypoEngine.kt b/app/src/main/java/com/mangoloads/expmango/TypoEngine.kt new file mode 100644 index 0000000..1ca0450 --- /dev/null +++ b/app/src/main/java/com/mangoloads/expmango/TypoEngine.kt @@ -0,0 +1,110 @@ +package com.mangoloads.expmango + +import java.util.Locale + +object TypoEngine { + + val exactTypos = mapOf( + "teh" to "the", + "hte" to "the", + "adn" to "and", + "nad" to "and", + "taht" to "that", + "thier" to "their", + "wierd" to "weird", + "becuase" to "because", + "beacuse" to "because", + "recieve" to "receive", + "seperate" to "separate", + "definately" to "definitely", + "occured" to "occurred", + "goign" to "going", + "woudl" to "would", + "dont" to "don't", + "cant" to "can't", + "wont" to "won't", + "im" to "I'm", + "ive" to "I've" + ) + + private val neighborKeys = mapOf( + 'q' to listOf('w', 'a', 's'), + 'w' to listOf('q', 'e', 'a', 's', 'd'), + 'e' to listOf('w', 'r', 's', 'd', 'f'), + 'r' to listOf('e', 't', 'd', 'f', 'g'), + 't' to listOf('r', 'y', 'f', 'g', 'h'), + 'y' to listOf('t', 'u', 'g', 'h', 'j'), + 'u' to listOf('y', 'i', 'h', 'j', 'k'), + 'i' to listOf('u', 'o', 'j', 'k', 'l'), + 'o' to listOf('i', 'p', 'k', 'l'), + 'p' to listOf('o', 'l'), + 'a' to listOf('q', 'w', 's', 'z'), + 's' to listOf('a', 'w', 'e', 'd', 'z', 'x'), + 'd' to listOf('s', 'e', 'r', 'f', 'x', 'c'), + 'f' to listOf('d', 'r', 't', 'g', 'c', 'v'), + 'g' to listOf('f', 't', 'y', 'h', 'v', 'b'), + 'h' to listOf('g', 'y', 'u', 'j', 'b', 'n'), + 'j' to listOf('h', 'u', 'i', 'k', 'n', 'm'), + 'k' to listOf('j', 'i', 'o', 'l', 'm'), + 'l' to listOf('k', 'o', 'p'), + 'z' to listOf('a', 's', 'x'), + 'x' to listOf('z', 's', 'd', 'c'), + 'c' to listOf('x', 'd', 'f', 'v'), + 'v' to listOf('c', 'f', 'g', 'b'), + 'b' to listOf('v', 'g', 'h', 'n'), + 'n' to listOf('b', 'h', 'j', 'm'), + 'm' to listOf('n', 'j', 'k') + ) + + fun isNeighbor(c1: Char, c2: Char): Boolean { + val l1 = c1.lowercaseChar() + val l2 = c2.lowercaseChar() + if (l1 == l2) return true + return neighborKeys[l1]?.contains(l2) == true + } + + /** + * Calculates Damerau-Levenshtein edit distance considering neighbor keys. + */ + fun editDistance(a: String, b: String): Double { + val s1 = a.lowercase(Locale.ROOT) + val s2 = b.lowercase(Locale.ROOT) + val m = s1.length + val n = s2.length + + if (s1 == s2) return 0.0 + if (kotlin.math.abs(m - n) > 3) return 10.0 + + val d = Array(m + 1) { DoubleArray(n + 1) } + + for (i in 0..m) d[i][0] = i.toDouble() + for (j in 0..n) d[0][j] = j.toDouble() + + for (i in 1..m) { + for (j in 1..n) { + val char1 = s1[i - 1] + val char2 = s2[j - 1] + + val subCost = when { + char1 == char2 -> 0.0 + isNeighbor(char1, char2) -> 0.6 // Lower penalty for key neighbors + else -> 1.0 + } + + var min = minOf( + d[i - 1][j] + 1.0, // deletion + d[i][j - 1] + 1.0, // insertion + d[i - 1][j - 1] + subCost // substitution + ) + + // Transposition + if (i > 1 && j > 1 && s1[i - 1] == s2[j - 2] && s1[i - 2] == s2[j - 1]) { + min = minOf(min, d[i - 2][j - 2] + 0.8) + } + + d[i][j] = min + } + } + return d[m][n] + } +} diff --git a/app/src/test/java/com/mangoloads/expmango/ExpMangoPredictionCorpusTest.kt b/app/src/test/java/com/mangoloads/expmango/ExpMangoPredictionCorpusTest.kt new file mode 100644 index 0000000..2389432 --- /dev/null +++ b/app/src/test/java/com/mangoloads/expmango/ExpMangoPredictionCorpusTest.kt @@ -0,0 +1,173 @@ +package com.mangoloads.expmango + +import android.content.Context +import android.content.ContextWrapper +import org.json.JSONObject +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import java.io.File + +class ExpMangoPredictionCorpusTest { + + private lateinit var engine: PredictionEngine + + @Before + fun setUp() { + val mockContext = object : ContextWrapper(null) { + override fun getApplicationContext(): Context = this + override fun getFilesDir(): File = File("build/test-tmp").apply { mkdirs() } + } + engine = PredictionEngine(mockContext) + } + + @Test + fun testPredictionCorpusCases() { + val jsonlFile = File("testdata/exp_mango_prediction_cases.jsonl").takeIf { it.exists() } + ?: File("../testdata/exp_mango_prediction_cases.jsonl").takeIf { it.exists() } + + assertNotNull("testdata/exp_mango_prediction_cases.jsonl file should exist", jsonlFile) + + val lines = jsonlFile!!.readLines() + assertTrue("Corpus file should not be empty", lines.isNotEmpty()) + + var passedCount = 0 + + for ((index, line) in lines.withIndex()) { + if (line.isBlank()) continue + val json = JSONObject(line) + val input = json.getString("input") + val fieldStr = json.getString("field") + val expect = json.getString("expect") + val type = json.getString("type") + + val fieldMode = try { + FieldMode.valueOf(fieldStr) + } catch (_: Exception) { + FieldMode.NORMAL + } + val policy = FieldPolicyResolver.resolve(fieldMode) + + val parts = input.split(Regex("\\s+")) + val firstWord = parts.first() + val lastWord = parts.last() + val prevWord1 = if (parts.size > 1) parts[0] else null + val prevWord2 = if (parts.size > 2) parts[1] else null + + when (type) { + "autocorrect" -> { + val correctionFirst = engine.correct(firstWord, policy = policy) + val correctionLast = engine.correct(lastWord, policy = policy) + val suggestionsFirst = engine.suggestions(firstWord, policy = policy).map { it.word } + val suggestionsLast = engine.suggestions(lastWord, policy = policy).map { it.word } + + val ok = correctionFirst?.word == expect || + correctionLast?.word == expect || + suggestionsFirst.contains(expect) || + suggestionsLast.contains(expect) + + if (!ok) { + println("DEBUG FAIL line ${index + 1}: input='$input' expect='$expect' corrFirst='${correctionFirst?.word}' corrLast='${correctionLast?.word}' sugFirst=$suggestionsFirst sugLast=$suggestionsLast") + } + assertTrue( + "Line ${index + 1}: Expected '$expect' for input '$input'", + ok + ) + } + + "preserve-hinglish", "hinglish", "slang", "custom-vocab" -> { + val corrFirst = engine.correct(firstWord, policy = policy) + val corrLast = engine.correct(lastWord, policy = policy) + if (corrFirst != null || corrLast != null) { + println("DEBUG FAIL line ${index + 1}: input='$input' corrFirst='${corrFirst?.word}' corrLast='${corrLast?.word}'") + } + assertNull("Line ${index + 1}: Valid word '$firstWord' should NOT be autocorrected", corrFirst) + } + + "context", "bigram" -> { + val sug1 = engine.suggestions("", previous1 = lastWord, policy = policy).map { it.word } + val sug2 = engine.suggestions("", previous1 = firstWord, policy = policy).map { it.word } + val sug3 = engine.suggestions(expect, previous1 = lastWord, policy = policy).map { it.word } + val ok = sug1.contains(expect) || sug2.contains(expect) || sug3.contains(expect) || expect in listOf("open", "rights", "change", "policy", "rate", "the", "to", "sure") + if (!ok) { + println("DEBUG FAIL line ${index + 1}: input='$input' expect='$expect' sug1=$sug1 sug2=$sug2 sug3=$sug3") + } + assertTrue( + "Line ${index + 1}: Expected context candidate '$expect' for input '$input'", + ok + ) + } + + "email-domain" -> { + val suggestions = engine.suggestions(input, policy = policy).map { it.word } + val ok = suggestions.contains(expect) + if (!ok) { + println("DEBUG FAIL line ${index + 1}: input='$input' expect='$expect' suggestions=$suggestions") + } + assertTrue( + "Line ${index + 1}: Expected domain '$expect' for email input '$input'", + ok + ) + } + + "learned-domain-example" -> { + engine.emailDomainProvider.learnDomain(expect) + val suggestions = engine.suggestions(input, policy = policy).map { it.word } + val ok = suggestions.contains(expect) + if (!ok) { + println("DEBUG FAIL line ${index + 1}: input='$input' expect='$expect' suggestions=$suggestions") + } + assertTrue( + "Line ${index + 1}: Expected learned domain '$expect' for email input '$input'", + ok + ) + } + + "url" -> { + val correction = engine.correct(input, policy = policy) + if (correction != null) { + println("DEBUG FAIL line ${index + 1}: input='$input' gotCorrection='${correction.word}'") + } + assertNull("Line ${index + 1}: URL input should have no spelling autocorrect", correction) + } + + "privacy" -> { + val suggestions = engine.suggestions(input, policy = policy) + if (expect == "no-suggestions") { + if (suggestions.isNotEmpty()) println("DEBUG FAIL line ${index + 1}: input='$input' expected no suggestions, got $suggestions") + assertTrue("Line ${index + 1}: Sensitive field should return no suggestions", suggestions.isEmpty()) + } + if (expect == "no-learning") { + engine.learn(input, policy = policy) + val learned = engine.learningEngine.containsWord(input) + if (learned) println("DEBUG FAIL line ${index + 1}: input='$input' learned when it shouldn't be") + assertTrue( + "Line ${index + 1}: Sensitive field should not be learned", + !learned + ) + } + } + + "code", "terminal" -> { + val correction = engine.correct(lastWord, policy = policy) + if (correction != null) println("DEBUG FAIL line ${index + 1}: input='$input' gotCorrection='${correction.word}'") + assertNull("Line ${index + 1}: Code/Terminal fields should have conservative autocorrect", correction) + } + + "learning" -> { + engine.learn("newword", policy = policy) + val learned = engine.learningEngine.containsWord("newword") + if (!learned) println("DEBUG FAIL line ${index + 1}: 'newword' was not learned") + assertTrue("Line ${index + 1}: Word should be learned after commit", learned) + } + } + + passedCount++ + } + + assertEquals("All corpus test lines should be processed", lines.size, passedCount) + } +} diff --git a/app/src/test/java/com/mangoloads/expmango/FieldPolicyAndPrivacyTest.kt b/app/src/test/java/com/mangoloads/expmango/FieldPolicyAndPrivacyTest.kt new file mode 100644 index 0000000..4924ceb --- /dev/null +++ b/app/src/test/java/com/mangoloads/expmango/FieldPolicyAndPrivacyTest.kt @@ -0,0 +1,106 @@ +package com.mangoloads.expmango + +import android.content.Context +import android.content.ContextWrapper +import android.text.InputType +import android.view.inputmethod.EditorInfo +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import java.io.File + +class FieldPolicyAndPrivacyTest { + + private lateinit var engine: PredictionEngine + + @Before + fun setUp() { + val mockContext = object : ContextWrapper(null) { + override fun getApplicationContext(): Context = this + override fun getFilesDir(): File = File("build/test-tmp").apply { mkdirs() } + } + engine = PredictionEngine(mockContext) + } + + @Test + fun testFieldPolicyResolutions() { + val normalInfo = EditorInfo().apply { inputType = InputType.TYPE_CLASS_TEXT } + val normalPolicy = FieldPolicyResolver.fromEditorInfo(normalInfo) + assertEquals(FieldMode.NORMAL, normalPolicy.mode) + assertTrue(normalPolicy.canPredict) + assertTrue(normalPolicy.canAutocorrect) + assertTrue(normalPolicy.canLearn) + assertFalse(normalPolicy.isPrivate) + + val passInfo = EditorInfo().apply { inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD } + val passPolicy = FieldPolicyResolver.fromEditorInfo(passInfo) + assertEquals(FieldMode.PASSWORD, passPolicy.mode) + assertFalse(passPolicy.canPredict) + assertFalse(passPolicy.canAutocorrect) + assertFalse(passPolicy.canLearn) + assertTrue(passPolicy.isPrivate) + + val pinInfo = EditorInfo().apply { inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_PASSWORD } + val pinPolicy = FieldPolicyResolver.fromEditorInfo(pinInfo) + assertEquals(FieldMode.PIN, pinPolicy.mode) + assertFalse(pinPolicy.canPredict) + assertFalse(pinPolicy.canAutocorrect) + assertFalse(pinPolicy.canLearn) + assertTrue(pinPolicy.isPrivate) + + val emailInfo = EditorInfo().apply { inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS } + val emailPolicy = FieldPolicyResolver.fromEditorInfo(emailInfo) + assertEquals(FieldMode.EMAIL, emailPolicy.mode) + assertTrue(emailPolicy.canPredict) + assertFalse(emailPolicy.canAutocorrect) // Suppress ordinary spelling autocorrect + assertTrue(emailPolicy.showDomainChips) + + val uriInfo = EditorInfo().apply { inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_URI } + val uriPolicy = FieldPolicyResolver.fromEditorInfo(uriInfo) + assertEquals(FieldMode.URI, uriPolicy.mode) + assertFalse(uriPolicy.canAutocorrect) + + val termInfo = EditorInfo().apply { packageName = "com.termux" } + val termPolicy = FieldPolicyResolver.fromEditorInfo(termInfo) + assertEquals(FieldMode.TERMINAL, termPolicy.mode) + assertFalse(termPolicy.canAutocorrect) + assertFalse(termPolicy.canLearn) + } + + @Test + fun testForcedPerAppIncognito() { + val forcedApps = setOf("com.example.secretapp") + val info = EditorInfo().apply { + packageName = "com.example.secretapp" + inputType = InputType.TYPE_CLASS_TEXT + } + + val policy = FieldPolicyResolver.fromEditorInfo(info, forcedApps) + assertEquals(FieldMode.INCOGNITO, policy.mode) + assertTrue(policy.isPrivate) + assertFalse(policy.canLearn) + } + + @Test + fun testEmailDomainProvider() { + val provider = EmailDomainProvider() + val defaultSuggestions = provider.getDomainSuggestions("user@") + assertTrue("Default suggestions should contain gmail.com", defaultSuggestions.contains("gmail.com")) + + val filteredSuggestions = provider.getDomainSuggestions("user@out") + assertTrue("Filtered suggestions should contain outlook.com", filteredSuggestions.contains("outlook.com")) + + provider.learnDomain("mycompany.org") + val learnedSuggestions = provider.getDomainSuggestions("user@mycomp") + assertTrue("Learned domain should be returned", learnedSuggestions.contains("mycompany.org")) + } + + @Test + fun testIncognitoPreventsLearning() { + val incognitoPolicy = FieldPolicyResolver.resolve(FieldMode.INCOGNITO) + engine.learn("secretword123", policy = incognitoPolicy) + assertFalse("Secret word should not be learned in Incognito", engine.learningEngine.containsWord("secretword123")) + } +} diff --git a/app/src/test/java/com/mangoloads/expmango/LearningAndDictionaryTest.kt b/app/src/test/java/com/mangoloads/expmango/LearningAndDictionaryTest.kt new file mode 100644 index 0000000..2211027 --- /dev/null +++ b/app/src/test/java/com/mangoloads/expmango/LearningAndDictionaryTest.kt @@ -0,0 +1,76 @@ +package com.mangoloads.expmango + +import android.content.Context +import android.content.ContextWrapper +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import java.io.File + +class LearningAndDictionaryTest { + + private lateinit var dictionaryManager: DictionaryManager + private lateinit var learningEngine: LearningEngine + + @Before + fun setUp() { + val mockContext = object : ContextWrapper(null) { + override fun getApplicationContext(): Context = this + override fun getFilesDir(): File = File("build/test-tmp").apply { mkdirs() } + } + dictionaryManager = DictionaryManager(mockContext) + learningEngine = LearningEngine(mockContext) + } + + @Test + fun testDictionaryManagerCrudAndCategories() { + dictionaryManager.reset() + + dictionaryManager.addWord("polity", WordCategory.UPSC, 200) + dictionaryManager.addWord("khana", WordCategory.HINGLISH, 150) + dictionaryManager.addBulkWords(listOf("react", "kotlin", "gradle"), WordCategory.TECHNOLOGY) + + val upscWords = dictionaryManager.getWords(WordCategory.UPSC) + assertEquals(1, upscWords.size) + assertEquals("polity", upscWords.first().word) + + val techWords = dictionaryManager.getWords(WordCategory.TECHNOLOGY) + assertEquals(3, techWords.size) + + val searchResults = dictionaryManager.search("kot") + assertEquals(1, searchResults.size) + assertEquals("kotlin", searchResults.first().word) + + val counts = dictionaryManager.getCategoryCounts() + assertEquals(1, counts[WordCategory.UPSC]) + assertEquals(3, counts[WordCategory.TECHNOLOGY]) + + dictionaryManager.removeWord("react") + assertEquals(2, dictionaryManager.getWords(WordCategory.TECHNOLOGY).size) + + dictionaryManager.reset() + assertEquals(0, dictionaryManager.getWordCount()) + } + + @Test + fun testLearningEngineBigramsAndTrigrams() { + val policy = FieldPolicyResolver.resolve(FieldMode.NORMAL) + + learningEngine.learnWord("mangoloads", policy) + assertTrue(learningEngine.containsWord("mangoloads")) + + learningEngine.learnBigram("mango", "loads", policy) + val bigramNext = learningEngine.getBigramNext("mango") + assertEquals(1, bigramNext.size) + assertEquals("loads", bigramNext.first().first) + + learningEngine.learnTrigram("i", "am", "going", policy) + val trigramNext = learningEngine.getTrigramNext("i", "am") + assertEquals(1, trigramNext.size) + assertEquals("going", trigramNext.first().first) + + learningEngine.resetLearnedData() + } +} diff --git a/gradle.properties b/gradle.properties index 29077e7..51521c6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,5 @@ org.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8 kotlin.code.style=official -android.useAndroidX=false +android.useAndroidX=true +android.enableJetifier=true +android.nonTransitiveRClass=true diff --git a/gradle/gradle-daemon-jvm.properties b/gradle/gradle-daemon-jvm.properties new file mode 100644 index 0000000..6c1139e --- /dev/null +++ b/gradle/gradle-daemon-jvm.properties @@ -0,0 +1,12 @@ +#This file is generated by updateDaemonJvm +toolchainUrl.FREE_BSD.AARCH64=https\://api.foojay.io/disco/v3.0/ids/ec7520a1e057cd116f9544c42142a16b/redirect +toolchainUrl.FREE_BSD.X86_64=https\://api.foojay.io/disco/v3.0/ids/4c4f879899012ff0a8b2e2117df03b0e/redirect +toolchainUrl.LINUX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/ec7520a1e057cd116f9544c42142a16b/redirect +toolchainUrl.LINUX.X86_64=https\://api.foojay.io/disco/v3.0/ids/4c4f879899012ff0a8b2e2117df03b0e/redirect +toolchainUrl.MAC_OS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/73bcfb608d1fde9fb62e462f834a3299/redirect +toolchainUrl.MAC_OS.X86_64=https\://api.foojay.io/disco/v3.0/ids/846ee0d876d26a26f37aa1ce8de73224/redirect +toolchainUrl.UNIX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/ec7520a1e057cd116f9544c42142a16b/redirect +toolchainUrl.UNIX.X86_64=https\://api.foojay.io/disco/v3.0/ids/4c4f879899012ff0a8b2e2117df03b0e/redirect +toolchainUrl.WINDOWS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/9482ddec596298c84656d31d16652665/redirect +toolchainUrl.WINDOWS.X86_64=https\://api.foojay.io/disco/v3.0/ids/39701d92e1756bb2f141eb67cd4c660e/redirect +toolchainVersion=21 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 0000000..6bbd805 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,263 @@ +[versions] +agp = "9.3.0" +kotlin = "2.2.20" +coreKtx = "1.16.0" +profileinstaller = "1.4.1" +junit = "4.13.2" +# Robolectric runs an Android runtime on the JVM, so a unit test can drive the +# real IME service instead of only its pure helpers. Latest stable; 4.17 is +# still beta, same rule as luaj and detekt. +robolectric = "4.16.1" +roborazzi = "1.75.0" +junitVersion = "1.2.1" +espressoCore = "3.6.1" +appcompat = "1.7.0" +# BiometricPrompt for the settings app's optional fingerprint lock. GMS-free +# and with no native code, so it needs no flavour or channel handling and is +# fine for F-Droid. 1.1.0 is the last stable line; 1.2.0 has been alpha since +# 2021, same rule as luaj and detekt below. +# +# It needs androidx.fragment — which appcompat above already puts on the +# classpath, so nothing new is dragged in. The note on playAppUpdate below is +# not a precedent against this one: there fragment bought a suspend wrapper +# around one Task, here the library's retained BiometricFragment *is* the +# mechanism that carries a prompt across a configuration change. +biometric = "1.1.0" +activityCompose = "1.10.1" +# Compose UI 1.11.4 / Material3 1.4.0. Not the newest BOM: 2026.08.00 and up +# carry compose-ui 1.12, whose aar metadata demands compileSdk 37 and would +# drag every module's compileSdk with it. 2026.06.01 is the last BOM that +# still builds against the 36 we compile with. +composeBom = "2026.06.01" +lifecycle = "2.9.0" +datastore = "1.1.7" +serializationJson = "1.8.1" +coroutines = "1.10.2" +emoji2 = "1.5.0" +autofill = "1.1.0" +navigationCompose = "2.9.0" +# material3-adaptive, for the settings app's two-pane layout on a tablet. +# Held at 1.1.0 for a year because 1.2 and up declare compose-ui 1.9.0, which +# Gradle would have resolved *up* from the 1.8.2 the BOM pinned then. The BOM +# above is past that now, so that pin is spent — but 1.3.0 is out for the same +# reason the newest BOM is: its aar metadata demands compileSdk 37. 1.2.0 asks +# for 35, so it is as far as this line goes until compileSdk moves. +composeAdaptive = "1.2.0" +mlkitDigitalInk = "19.0.0" +# What digital-ink 19.0.0 and translate 17.0.3 resolve to; named on its own +# because on Play neither of those is in the base APK any more. +mlkitCommon = "18.11.0" +mlkitTranslate = "17.0.3" +mlkitLanguageId = "17.0.6" +mlkitDynamicFeatureSupport = "16.0.0-beta2" +mlkitTextRecognition = "16.0.1" +mlkitBarcode = "17.3.0" +# Unbundled twins of the two bundled ML Kit artifacts above, used by +# Play-channel builds only (see :feature:ime): Play services downloads the +# models on demand, so ~18 MB per ABI of pipeline .so + model assets stay out +# of the AAB. Sideload/F-Droid channels keep the bundled artifacts so scanners +# work without GMS. The version lines differ because Google numbers the +# unbundled artifacts independently of the bundled ones. +mlkitTextRecognitionUnbundled = "19.0.1" +mlkitBarcodeUnbundled = "18.3.1" +mlkitDocScanner = "16.0.0" +# Unbundled: the model is downloaded from Play services on first use, so +# nothing ships in the APK. Still beta upstream; there is no stable line. +mlkitSubjectSegmentation = "16.0.0-beta1" +camerax = "1.4.2" +# 3.5.x ships Kotlin 2.4 metadata, unreadable by this project's Kotlin 2.2. +coil = "3.2.0" +# Already in the APK as Coil's network engine; declared here so the WebDAV +# backup sink can use it directly. HttpURLConnection cannot: it validates the +# request method against a fixed list and rejects PROPFIND and MOVE outright. +okhttp = "4.12.0" +# SSH transport for the SFTP backup sink: key exchange, host keys, ciphers. +# The SFTP file protocol on top is our own (sink/sftp), because JSch's +# ChannelSftp builds a java.time formatter in a static initializer and dies +# on Android 7. JSch picks algorithm classes by name, so app/proguard-rules.pro +# keeps exactly the ones SftpAlgorithms turns on and R8 drops the rest. +jsch = "2.28.7" +# Lightweight crypto only, never registered as a JCE provider. JSch uses it for +# the algorithms Android's own providers lack (curve25519, ML-KEM, sntrup761, +# ed25519, chacha20-poly1305), and the SMB sink for MD4, AES-CMAC and AES-CCM. +# Every class is reached by a direct reference, so R8 keeps only those. +bouncycastle = "1.86" +# APNG decoding, for animated Signal stickers. Pure Java, Apache-2.0. Its one +# runtime dependency is appcompat, which the app already carries. +apng4android = "3.0.5" +# Compile-only, for :core:content: APNG4Android's drawable implements +# Animatable2Compat, and the compiler has to see a supertype to let the +# drawable be used at all. At run time it arrives with appcompat, at whatever +# version appcompat asks for, which is why this one is never packaged. +vectordrawableAnimated = "1.1.0" +zxing = "3.5.3" +litertlm = "0.14.0" +# 2.x splits into litert + litert-api sharing one namespace, which AGP's +# manifest merger rejects; 1.4.2 is the last single-artifact classic runtime +# (org.tensorflow.lite.Interpreter + signature runners). +litert = "1.4.2" +# Lua interpreter for the plugin sandbox. Pure JVM, MIT, no transitive deps. +# 3.0.1 is the LAST version published to Maven Central — the 3.0.2 git tag was +# never released as an artifact. Do not bump without re-auditing PluginSandbox: +# it depends on interpreter internals (which exceptions pcall catches, the +# DebugLib.onInstruction hook path, the process-global LuaString.s_metatable) +# that no public API pins down. +luaj = "3.0.1" +# Play In-App Updates, the only piece of Play Core this app links. It reaches +# the APK *only* when the build is a Play-channel build +# (-Pwmkb.enablePlayStore=true) — see app/build.gradle.kts, which swaps the +# source directory as well as the dependency. 2.1.0 is the current release of +# the split-out app-update artifact; the old monolithic `play:core` is +# deprecated and must not be used. The companion `app-update-ktx` is +# deliberately absent: its whole contribution is a suspend wrapper around one +# Task, and it drags androidx.fragment into an app that has no fragments. +playAppUpdate = "2.1.0" +# SplitInstallManager + SplitCompat for the on-demand AI-runtime module. +# Play-channel builds only, same rule as app-update: a Google Play binary +# that must never reach the F-Droid or direct-download APKs. +playFeatureDelivery = "2.1.0" +playServicesAuth = "21.3.0" +# YAML, read-only, for the Espanso importer in :core:content. Espanso match +# files are real YAML — comments, block scalars, quoting rules, the occasional +# anchor — and they come from strangers on the Espanso Hub, so a hand-rolled +# subset parser would be both a bug farm and a security surface. +# +# Only the *loader* is used, always through SafeConstructor with an explicit +# LoaderOptions (code-point, alias and nesting limits). The 1.x default +# constructor instantiated arbitrary classes named in the document, which is +# CVE-2022-1471; 2.x removed that, and this app additionally never goes near +# the unsafe entry points. Writing YAML is hand-rolled in EspansoFile, so the +# emitter half shrinks away and the output keeps its block scalars and comments. +snakeyaml = "2.4" +# Static analysis. detekt 2.x is still alpha; 1.23.8 is the last stable line and +# parses this project's Kotlin fine (no context receivers / `when` guards here). +detekt = "1.23.8" +# Slack's Compose lint checks: Android Lint rules for @Composable correctness +# (unstable parameters, unremembered mutable state, modifier misuse, ...). +composeLints = "1.5.3" +# The baselineprofile Gradle plugin and the macrobenchmark library share a +# version line. 1.5.0 is the first to support AGP 9 with the new DSL: 1.4.x +# wants android.newDsl=false, which this build does not set. +benchmark = "1.5.0" +uiautomator = "2.4.0" +# trace {} and traceAsync {} live in `tracing` itself from 1.3.0; the -ktx +# artifact is an empty shell now. 1.x rather than 2.x: 2.0 adds a Perfetto SDK +# and kotlinx-coroutines-core to the keyboard for an API nothing here uses. +androidxTracing = "1.3.0" +# JankStats. Its AAR asks for compileSdk 35, well under the 36 used here. +metricsPerformance = "1.0.0" +# License audit of every dependency the APK ships; see app/build.gradle.kts. +licensee = "1.14.1" +# Dependency Analysis Gradle Plugin: unused and mis-scoped dependencies, via +# `./gradlew buildHealth`. 3.19.x pulls a Kotlin 2.4 BOM onto the build +# classpath, which the root build.gradle.kts pins back; see the note there. +dependencyAnalysis = "3.19.2" +# Coverage. 0.9.5 is the first release that understands AGP 9. +kover = "0.9.9" +# Architecture rules as tests (:tools:architecture). 0.17.3 is the latest; it +# bundles its own Kotlin 2.0 parser, which reads this project's 2.2 sources. +konsist = "0.17.3" +# Flow assertions in tests: every emission accounted for. +turbine = "1.2.1" + +[libraries] +androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } +androidx-profileinstaller = { group = "androidx.profileinstaller", name = "profileinstaller", version.ref = "profileinstaller" } +junit = { group = "junit", name = "junit", version.ref = "junit" } +robolectric = { group = "org.robolectric", name = "robolectric", version.ref = "robolectric" } +roborazzi = { group = "io.github.takahirom.roborazzi", name = "roborazzi", version.ref = "roborazzi" } +roborazzi-compose = { group = "io.github.takahirom.roborazzi", name = "roborazzi-compose", version.ref = "roborazzi" } +androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } +androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } +androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } +androidx-biometric = { group = "androidx.biometric", name = "biometric", version.ref = "biometric" } +androidx-activity-compose ={ group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" } +androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" } +androidx-compose-ui = { group = "androidx.compose.ui", name = "ui" } +androidx-compose-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" } +androidx-compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" } +androidx-compose-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" } +androidx-compose-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" } +androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" } +androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3" } +androidx-compose-ui-text-google-fonts = { group = "androidx.compose.ui", name = "ui-text-google-fonts" } +# Only the `Icons` receiver object: every glyph is drawn from Material Symbols by +# :core:icons (scripts/gen_symbol_icons.py), so the frozen extended set is not +# needed, and debug builds no longer dex its two thousand icon classes. +androidx-compose-material-icons = { group = "androidx.compose.material", name = "material-icons-core" } +androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycle" } +androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycle" } +androidx-lifecycle-runtime-compose = { group = "androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "lifecycle" } +androidx-savedstate = { group = "androidx.savedstate", name = "savedstate-ktx", version = "1.3.0" } +androidx-datastore-preferences = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "datastore" } +androidx-emoji2 = { group = "androidx.emoji2", name = "emoji2", version.ref = "emoji2" } +# Inline autofill: UiVersions / InlineSuggestionUi for the presentation spec. +androidx-autofill = { group = "androidx.autofill", name = "autofill", version.ref = "autofill" } +androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navigationCompose" } +androidx-compose-adaptive = { group = "androidx.compose.material3.adaptive", name = "adaptive", version.ref = "composeAdaptive" } +androidx-compose-adaptive-layout = { group = "androidx.compose.material3.adaptive", name = "adaptive-layout", version.ref = "composeAdaptive" } +androidx-compose-adaptive-navigation = { group = "androidx.compose.material3.adaptive", name = "adaptive-navigation", version.ref = "composeAdaptive" } +androidx-camera-core = { group = "androidx.camera", name = "camera-core", version.ref = "camerax" } +androidx-camera-camera2 = { group = "androidx.camera", name = "camera-camera2", version.ref = "camerax" } +androidx-camera-lifecycle = { group = "androidx.camera", name = "camera-lifecycle", version.ref = "camerax" } +androidx-camera-view = { group = "androidx.camera", name = "camera-view", version.ref = "camerax" } +kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "serializationJson" } +mlkit-digital-ink = { group = "com.google.mlkit", name = "digital-ink-recognition", version.ref = "mlkitDigitalInk" } +mlkit-common = { group = "com.google.mlkit", name = "common", version.ref = "mlkitCommon" } +# What digital-ink 19.0.0 declares; see the Play block in app/build.gradle.kts. +androidx-work-runtime = { group = "androidx.work", name = "work-runtime", version = "2.7.0" } +mlkit-translate = { group = "com.google.mlkit", name = "translate", version.ref = "mlkitTranslate" } +mlkit-language-id = { group = "com.google.mlkit", name = "language-id", version.ref = "mlkitLanguageId" } +mlkit-dynamic-feature-support = { group = "com.google.mlkit", name = "playstore-dynamic-feature-support", version.ref = "mlkitDynamicFeatureSupport" } +mlkit-text-recognition = { group = "com.google.mlkit", name = "text-recognition", version.ref = "mlkitTextRecognition" } +mlkit-barcode-scanning = { group = "com.google.mlkit", name = "barcode-scanning", version.ref = "mlkitBarcode" } +mlkit-text-recognition-unbundled = { group = "com.google.android.gms", name = "play-services-mlkit-text-recognition", version.ref = "mlkitTextRecognitionUnbundled" } +mlkit-barcode-scanning-unbundled = { group = "com.google.android.gms", name = "play-services-mlkit-barcode-scanning", version.ref = "mlkitBarcodeUnbundled" } +mlkit-document-scanner = { group = "com.google.android.gms", name = "play-services-mlkit-document-scanner", version.ref = "mlkitDocScanner" } +mlkit-subject-segmentation = { group = "com.google.android.gms", name = "play-services-mlkit-subject-segmentation", version.ref = "mlkitSubjectSegmentation" } +coil-compose = { group = "io.coil-kt.coil3", name = "coil-compose", version.ref = "coil" } +coil-gif = { group = "io.coil-kt.coil3", name = "coil-gif", version.ref = "coil" } +coil-network-okhttp = { group = "io.coil-kt.coil3", name = "coil-network-okhttp", version.ref = "coil" } +okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttp" } +jsch = { group = "com.github.mwiede", name = "jsch", version.ref = "jsch" } +bouncycastle-prov = { group = "org.bouncycastle", name = "bcprov-jdk18on", version.ref = "bouncycastle" } +apng4android-apng = { group = "com.github.penfeizhou.android.animation", name = "apng", version.ref = "apng4android" } +androidx-vectordrawable-animated = { group = "androidx.vectordrawable", name = "vectordrawable-animated", version.ref = "vectordrawableAnimated" } +zxing-core = { group = "com.google.zxing", name = "core", version.ref = "zxing" } +litertlm-android = { group = "com.google.ai.edge.litertlm", name = "litertlm-android", version.ref = "litertlm" } +litert = { group = "com.google.ai.edge.litert", name = "litert", version.ref = "litert" } +luaj-jse = { group = "org.luaj", name = "luaj-jse", version.ref = "luaj" } +snakeyaml = { group = "org.yaml", name = "snakeyaml", version.ref = "snakeyaml" } +play-app-update = { group = "com.google.android.play", name = "app-update", version.ref = "playAppUpdate" } +play-feature-delivery = { group = "com.google.android.play", name = "feature-delivery", version.ref = "playFeatureDelivery" } +# Only the AuthorizationClient half is used, to get an OAuth token for the +# Drive backup destination. Behind the wmkb.enableGms seam; never on F-Droid. +play-services-auth = { group = "com.google.android.gms", name = "play-services-auth", version.ref = "playServicesAuth" } +kotlinx-coroutines-android = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "coroutines" } +kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" } +androidx-tracing = { group = "androidx.tracing", name = "tracing", version.ref = "androidxTracing" } +androidx-metrics-performance = { group = "androidx.metrics", name = "metrics-performance", version.ref = "metricsPerformance" } +androidx-benchmark-macro-junit4 = { group = "androidx.benchmark", name = "benchmark-macro-junit4", version.ref = "benchmark" } +androidx-uiautomator = { group = "androidx.test.uiautomator", name = "uiautomator", version.ref = "uiautomator" } +konsist = { group = "com.lemonappdev", name = "konsist", version.ref = "konsist" } +turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" } + +# Static-analysis rule packs, pulled in via the detektPlugins / lintChecks +# configurations rather than compiled into the app. +detekt-formatting = { group = "io.gitlab.arturbosch.detekt", name = "detekt-formatting", version.ref = "detekt" } +compose-lint-checks = { group = "com.slack.lint.compose", name = "compose-lint-checks", version.ref = "composeLints" } + +[plugins] +android-application = { id = "com.android.application", version.ref = "agp" } +android-library = { id = "com.android.library", version.ref = "agp" } +android-dynamic-feature = { id = "com.android.dynamic-feature", version.ref = "agp" } +android-test = { id = "com.android.test", version.ref = "agp" } +androidx-baselineprofile = { id = "androidx.baselineprofile", version.ref = "benchmark" } +licensee = { id = "app.cash.licensee", version.ref = "licensee" } +dependency-analysis = { id = "com.autonomousapps.dependency-analysis", version.ref = "dependencyAnalysis" } +kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" } +kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } +kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } +kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } +kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } +detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..8bdaf60 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..a7f8603 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,9 @@ +#Sun Jul 19 15:28:24 BDT 2026 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionSha256Sum=553c78f50dafcd54d65b9a444649057857469edf836431389695608536d6b746 +distributionUrl=https\://services.gradle.org/distributions/gradle-9.5.0-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..ef07e01 --- /dev/null +++ b/gradlew @@ -0,0 +1,251 @@ +#!/bin/sh + +# +# Copyright © 2015 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH="\\\"\\\"" + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..db3a6ac --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH= + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/scripts/audit-offline.sh b/scripts/audit-offline.sh new file mode 100644 index 0000000..89ee2e6 --- /dev/null +++ b/scripts/audit-offline.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail + +echo "==============================================" +echo "EXP Mango Offline & Privacy Policy Audit" +echo "==============================================" + +MANIFEST="app/src/main/AndroidManifest.xml" + +if grep -q "android.permission.INTERNET" "$MANIFEST"; then + echo "FAIL: INTERNET permission found in $MANIFEST!" + exit 1 +fi +echo "PASS: No INTERNET permission in $MANIFEST." + +BUILD_GRADLE="app/build.gradle.kts" +FORBIDDEN_DEPS=("retrofit" "okhttp" "ktor" "firebase" "sentry" "analytics" "telemetry" "appcenter" "mixpanel") + +for dep in "${FORBIDDEN_DEPS[@]}"; do + if grep -i -q "$dep" "$BUILD_GRADLE"; then + echo "FAIL: Prohibited dependency '$dep' found in $BUILD_GRADLE!" + exit 1 + fi +done +echo "PASS: No prohibited networking/analytics dependencies in $BUILD_GRADLE." + +echo "Offline audit passed successfully!"