Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

[ensu] Reduce lint suppressions

setalp5dd0c9d57dbc
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/AppViewModel.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/AppViewModel.kt
index f775249cb9..8b8be3bb00 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/AppViewModel.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/AppViewModel.kt
@@ -3,6 +3,7 @@ package io.ente.ensu
import android.app.Application
import android.content.pm.PackageManager
import android.os.Build
+import androidx.core.content.pm.PackageInfoCompat
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import io.ente.ensu.chat.ChatRepository
@@ -101,7 +102,6 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
super.onCleared()
}
- @Suppress("DEPRECATION")
private fun getAppVersion(application: Application): String {
val packageManager = application.packageManager
val packageName = application.packageName
@@ -109,15 +109,10 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0))
} else {
- packageManager.getPackageInfo(packageName, 0)
+ @Suppress("DEPRECATION") packageManager.getPackageInfo(packageName, 0)
}
val versionName = packageInfo.versionName ?: "unknown"
- val versionCode =
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
- packageInfo.longVersionCode
- } else {
- packageInfo.versionCode.toLong()
- }
+ val versionCode = PackageInfoCompat.getLongVersionCode(packageInfo)
return "$versionName+$versionCode"
}
}
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt
index 599d384af5..4ca6cda41d 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt
@@ -2,7 +2,6 @@
package io.ente.ensu
-import android.annotation.SuppressLint
import android.content.ComponentCallbacks2
import android.content.Context
import android.content.Intent
@@ -508,12 +507,9 @@ private fun buildAttachmentFromUri(
FileOutputStream(destination).use { output -> output.write(compressedBytes) }
finalName = normalizedJpegAttachmentName(name ?: safeName)
} else {
- // Lint misses the close performed by use.
- @SuppressLint("Recycle")
- val inputStream = resolver.openInputStream(uri) ?: return@runCatching null
- inputStream.use { input ->
+ resolver.openInputStream(uri)?.use { input ->
FileOutputStream(destination).use { output -> input.copyTo(output) }
- }
+ } ?: return@runCatching null
finalName = name ?: safeName
}
@@ -584,26 +580,23 @@ private fun openAttachment(context: Context, attachment: Attachment) {
}
}
-// Lint misses the close performed by use.
-@SuppressLint("Recycle")
private fun queryDisplayName(resolver: android.content.ContentResolver, uri: Uri): String? {
- resolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use { cursor ->
+ val cursor =
+ resolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null) ?: return null
+ return cursor.use {
if (cursor.moveToFirst()) {
val index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
- if (index >= 0) return cursor.getString(index)
- }
+ if (index >= 0) cursor.getString(index) else null
+ } else null
}
- return null
}
-// Lint misses the close performed by use.
-@SuppressLint("Recycle")
private fun querySize(resolver: android.content.ContentResolver, uri: Uri): Long? {
- resolver.query(uri, arrayOf(OpenableColumns.SIZE), null, null, null)?.use { cursor ->
+ val cursor = resolver.query(uri, arrayOf(OpenableColumns.SIZE), null, null, null) ?: return null
+ return cursor.use {
if (cursor.moveToFirst()) {
val index = cursor.getColumnIndex(OpenableColumns.SIZE)
- if (index >= 0) return cursor.getLong(index)
- }
+ if (index >= 0) cursor.getLong(index) else null
+ } else null
}
- return null
}
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt
index 5ba4f96e50..ed7ce0e8ad 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt
@@ -1,7 +1,8 @@
package io.ente.ensu.chat
-import android.annotation.SuppressLint
+import android.Manifest
import android.content.Context
+import android.content.pm.PackageManager
import android.media.AudioFormat
import android.media.AudioManager
import android.media.AudioRecord
@@ -9,6 +10,7 @@ import android.media.MediaRecorder
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
+import androidx.core.content.ContextCompat
import io.ente.ensu.assets.AssetStore
import io.ente.ensu.bindings.AssetDownloadException
import io.ente.ensu.bindings.DictationException
@@ -103,6 +105,10 @@ internal class VoiceTranscriptionController(
private val onVoiceInputJob: (Job) -> Unit,
private val onInsert: (DictationInsertion, String) -> Boolean,
) {
+ private fun microphoneFailure(error: Exception): VoiceInputFailure =
+ if (error is SecurityException) VoiceInputFailure.Permission
+ else VoiceInputFailure.MicrophoneUnavailable
+
private fun logFailure(error: Exception) =
logRepository.log(
LogLevel.Error,
@@ -111,6 +117,7 @@ internal class VoiceTranscriptionController(
throwable = error,
)
+ private val applicationContext = context.applicationContext
private val audioManager = context.getSystemService(AudioManager::class.java)
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
private val usesLiveDictation =
@@ -296,11 +303,11 @@ internal class VoiceTranscriptionController(
val hardwareReady =
async(Dispatchers.IO) {
try {
- microphone.prepare()
+ microphone.prepare(applicationContext)
} catch (error: Exception) {
withContext(Dispatchers.Main.immediate) {
if (token == generation && failure == null)
- failure = VoiceInputFailure.MicrophoneUnavailable
+ failure = microphoneFailure(error)
}
throw error
}
@@ -331,9 +338,7 @@ internal class VoiceTranscriptionController(
} catch (error: Exception) {
if (token == generation) {
logFailure(error)
- interrupt(
- VoiceInputFailure.MicrophoneUnavailable
- )
+ interrupt(microphoneFailure(error))
}
}
}
@@ -525,8 +530,7 @@ private class LiveMicrophone(
@Volatile private var recorder: AudioRecord? = null
private var readJob: Job? = null
- @SuppressLint("MissingPermission")
- fun prepare() {
+ fun prepare(context: Context) {
if (stopped.get() || recorder != null) return
val minimum =
AudioRecord.getMinBufferSize(
@@ -535,6 +539,12 @@ private class LiveMicrophone(
AudioFormat.ENCODING_PCM_16BIT,
)
if (minimum <= 0) throw IOException("Microphone format unavailable")
+ if (
+ ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) !=
+ PackageManager.PERMISSION_GRANTED
+ ) {
+ throw SecurityException("Microphone permission is required")
+ }
val active =
AudioRecord.Builder()
.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION)
@@ -554,7 +564,6 @@ private class LiveMicrophone(
recorder = active
}
- @SuppressLint("MissingPermission")
fun start(scope: CoroutineScope) {
if (stopped.get()) return
val active = recorder ?: return
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt
index 7955be0d9f..606e017271 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt
@@ -1,6 +1,5 @@
package io.ente.ensu.notes
-import android.annotation.SuppressLint
import android.content.ContentResolver
import android.net.Uri
import android.os.Build
@@ -265,14 +264,9 @@ internal class AndroidNotesSource(
}
private fun bytes(id: String): ByteArray = access { request ->
- // Lint misses the close performed by use.
- @SuppressLint("Recycle")
- val descriptor =
- resolver.openAssetFileDescriptor(uri(id), "r", request)
- ?: throw NotesException.SourceRead("Could not read the note")
- descriptor.use {
+ resolver.openAssetFileDescriptor(uri(id), "r", request)?.use { descriptor ->
cancellation.check()
- it.createInputStream().use { input ->
+ descriptor.createInputStream().use { input ->
val channel = input.channel
activeRead.set(channel)
try {
@@ -302,7 +296,7 @@ internal class AndroidNotesSource(
activeRead.compareAndSet(channel, null)
}
}
- }
+ } ?: throw NotesException.SourceRead("Could not read the note")
}
private fun <T> access(block: (CancellationSignal) -> T): T {
diff --git a/android/checks/lint-exceptions/suppressions.json b/android/checks/lint-exceptions/suppressions.json
index e0ab5d6501..90790c6650 100644
--- a/android/checks/lint-exceptions/suppressions.json
+++ b/android/checks/lint-exceptions/suppressions.json
@@ -2,12 +2,5 @@
"android/apps/ensu/app/src/main/AndroidManifest.xml": ["UnusedAttribute"],
"android/apps/ensu/app/src/main/java/io/ente/ensu/AppViewModel.kt": [
"DEPRECATION"
- ],
- "android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt": ["Recycle"],
- "android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt": [
- "MissingPermission"
- ],
- "android/apps/ensu/app/src/main/java/io/ente/ensu/notes/AndroidNotesSource.kt": [
- "Recycle"
]
}
diff --git a/rust/checks/lint-exceptions/suppressions.json b/rust/checks/lint-exceptions/suppressions.json
index efef732c6e..16acee529e 100644
--- a/rust/checks/lint-exceptions/suppressions.json
+++ b/rust/checks/lint-exceptions/suppressions.json
@@ -29,11 +29,9 @@
"rust/crates/core/src/crypto/secretbox.rs": ["clippy::expect_used"],
"rust/crates/core/src/crypto/types.rs": ["clippy::expect_used"],
"rust/crates/ensu/src/config.rs": ["clippy::expect_used"],
- "rust/crates/ensu/src/config/knowledge.rs": ["clippy::expect_used"],
"rust/crates/ensu/src/llm/generate.rs": ["clippy::expect_used"],
"rust/crates/ensu/src/model/mod.rs": ["clippy::expect_used"],
"rust/crates/ensu/src/platform.rs": ["unsafe_code"],
- "rust/crates/ensu/src/transcription/text.rs": ["clippy::expect_used"],
"rust/crates/exif/src/isobmff.rs": ["clippy::expect_used"],
"rust/crates/image/src/color_management.rs": ["clippy::expect_used"],
"rust/crates/location/src/binary.rs": ["clippy::expect_used"],
diff --git a/rust/crates/ensu/src/config/knowledge.rs b/rust/crates/ensu/src/config/knowledge.rs
index 66e92a0e0b..74e506d5b5 100644
--- a/rust/crates/ensu/src/config/knowledge.rs
+++ b/rust/crates/ensu/src/config/knowledge.rs
@@ -14,7 +14,6 @@ pub(crate) const KNOWLEDGE_ARTIFACT_FILENAMES: [&str; 4] = [
const EMBEDDING_MODEL_IDENTITY: &str =
"ggml-org/embeddinggemma-300M-GGUF:embeddinggemma-300M-Q8_0.gguf";
-const DOCUMENT_PROMPT: &str = "title: {title} | text: {text}";
const QUERY_PROMPT: &str = "task: search result | query: {query}";
const EMBEDDING_MODEL_URL: &str = "https://huggingface.co/ente-ai/embeddinggemma-300m-gguf/resolve/957b55764bd672f51240ac026e3a23ac9459ee3c/embeddinggemma-300M-Q8_0.gguf";
const EMBEDDING_MODEL_SHA256: &str =
@@ -86,7 +85,7 @@ pub(crate) fn knowledge_index_contract() -> KnowledgeIndexContract {
model: EMBEDDING_MODEL_IDENTITY.to_owned(),
source_dim: SOURCE_DIM,
matryoshka: true,
- doc_prompt: DOCUMENT_PROMPT.to_owned(),
+ doc_prompt: format_knowledge_document_prompt("{title}", "{text}"),
query_prompt: QUERY_PROMPT.to_owned(),
dim: RETRIEVAL_DIM,
quant: "int8".to_owned(),
@@ -97,21 +96,7 @@ pub(crate) fn knowledge_index_contract() -> KnowledgeIndexContract {
}
pub(crate) fn format_knowledge_document_prompt(title: &str, text: &str) -> String {
- #[expect(
- clippy::expect_used,
- reason = "The fixed prompt contains the title placeholder"
- )]
- let (prefix, remainder) = DOCUMENT_PROMPT
- .split_once("{title}")
- .expect("knowledge document prompt contains a title placeholder");
- #[expect(
- clippy::expect_used,
- reason = "The fixed prompt contains the text placeholder after the title"
- )]
- let (middle, suffix) = remainder
- .split_once("{text}")
- .expect("knowledge document prompt contains a text placeholder");
- [prefix, title, middle, text, suffix].concat()
+ format!("title: {title} | text: {text}")
}
pub(crate) fn knowledge_embedding_config() -> KnowledgeEmbeddingConfig {
@@ -234,6 +219,18 @@ mod tests {
use super::*;
+ #[test]
+ fn document_prompt_preserves_literal_placeholders_in_content() {
+ assert_eq!(
+ format_knowledge_document_prompt("A {text} title", "Text with {title} and {text}"),
+ "title: A {text} title | text: Text with {title} and {text}"
+ );
+ assert_eq!(
+ knowledge_index_contract().doc_prompt,
+ "title: {title} | text: {text}"
+ );
+ }
+
#[test]
fn artifact_urls_append_only_fixed_names() {
let urls = knowledge_artifact_urls(&knowledge_datasets()[0]);
diff --git a/rust/crates/ensu/src/transcription/text.rs b/rust/crates/ensu/src/transcription/text.rs
index 781fbbd952..2530688d42 100644
--- a/rust/crates/ensu/src/transcription/text.rs
+++ b/rust/crates/ensu/src/transcription/text.rs
@@ -1,31 +1,10 @@
-use regex::{Regex, regex};
-use std::sync::LazyLock;
-
-const FILLER_WORDS: &[&str] = &[
- "uh", "um", "uhm", "umm", "uhh", "uhhh", "ah", "eh", "hmm", "hm", "mmm", "mm", "mh", "ha",
- "ehh",
-];
-
-#[expect(
- clippy::expect_used,
- reason = "Filler words are escaped before insertion into the fixed regex"
-)]
-static FILLER_PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {
- FILLER_WORDS
- .iter()
- .map(|word| {
- Regex::new(&format!(r"(?i)\b{}\b[,.]?", regex::escape(word)))
- .expect("escaped filler word forms a valid regex")
- })
- .collect()
-});
+use regex::regex;
pub fn filter_transcription_output(text: &str) -> String {
- let mut filtered = text.to_string();
-
- for pattern in FILLER_PATTERNS.iter() {
- filtered = pattern.replace_all(&filtered, "").to_string();
- }
+ let mut filtered =
+ regex!(r"(?i)\b(?:uh|um|uhm|umm|uhh|uhhh|ah|eh|hmm|hm|mmm|mm|mh|ha|ehh)\b[,.]?")
+ .replace_all(text, "")
+ .to_string();
filtered = collapse_stutters(&filtered);
filtered = regex!(r"\s{2,}").replace_all(&filtered, " ").to_string();
@@ -74,6 +53,24 @@ mod tests {
);
}
+ #[test]
+ fn removes_all_filler_variants_with_case_and_punctuation() {
+ assert_eq!(
+ filter_transcription_output(
+ "UH, Um. uhm umm uhh uhhh ah eh hmm hm mmm mm mh ha ehh hello"
+ ),
+ "hello"
+ );
+ }
+
+ #[test]
+ fn preserves_filler_substrings_inside_words() {
+ assert_eq!(
+ filter_transcription_output("umbrella humming hammer Muhammad ähm uhé"),
+ "umbrella humming hammer Muhammad ähm uhé"
+ );
+ }
+
#[test]
fn collapses_repeated_short_stutters() {
assert_eq!(filter_transcription_output("I I I think"), "I think");