Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.
Rust androiddocument-scannerdocximage-editorjetpack-composekotlinliquid-glassofflineopen-sourcepdfpdf-compressorpdf-editorpdf-mergerpdf-readerpdf-splitterpdf-toolspdf-viewerpptxspreadsheet-editorxlsx
Encryption: one tool to set a PDF's password or remove it; Edit photo as a tool
chethan616e3cff8fc7877
diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/AppViewModel.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/AppViewModel.ktindex 8c00455..4eeca3d 100644--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/AppViewModel.kt+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/AppViewModel.kt@@ -2865,7 +2865,52 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : AndrnoteCameraShots.trySend(f)}+ /** The Tools tab's Edit photo: a picture is asked for, then opened in the photo editor. */+ private var photoToolPending = false++ /** The edited picture also goes to Downloads when the editor was opened as a tool. */+ var photoSaveCopy = false++ fun pickPhotoToEdit() {+ photoToolPending = true+ notePhotoPicks.trySend(Unit)+ }++ /** A copy of the picture just edited, saved to Downloads (only after [pickPhotoToEdit]). */+ fun savePhotoCopy(file: File) {+ if (!photoSaveCopy) return+ photoSaveCopy = false+ viewModelScope.launch {+ runCatching {+ val name = "Artefact photo ${java.text.SimpleDateFormat("yyyy-MM-dd HH-mm-ss", java.util.Locale.ROOT).format(java.util.Date())}.jpg"+ withContext(Dispatchers.IO) {+ val copy = tempFile("jpg")+ file.copyTo(copy, overwrite = true)+ try {+ files.saveToDownloads(copy, name, "image/jpeg")+ } finally {+ copy.delete()+ }+ }+ tell(text(R.string.msg_saved_downloads, name))+ }.onFailure { tell(it.userMessage) }+ }+ }+fun onNotePhoto(uri: Uri?) {+ if (photoToolPending) {+ // a picture for the editor: it is kept as a note of its own on the desk, and edited there+ photoToolPending = false+ uri ?: return+ viewModelScope.launch {+ val name = notes.importImage(uri) ?: return@launch tell(text(R.string.common_file_unreadable))+ val note = com.chethan616.clearpdf.notes.Note(java.util.UUID.randomUUID().toString(), com.chethan616.clearpdf.notes.NoteKind.Text, images = listOf(name))+ notes.add(note)+ photoSaveCopy = true+ editingPhoto = note.id to name+ }+ return+ }val id = openNote ?: returnuri ?: returnviewModelScope.launch {diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/ToolScreens.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/ToolScreens.ktindex 07e09e8..c1f854a 100644--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/ToolScreens.kt+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/ToolScreens.kt@@ -57,6 +57,7 @@ import androidx.compose.ui.focus.focusRequesterimport androidx.compose.foundation.layout.heightInimport androidx.compose.foundation.layout.widthInimport androidx.compose.ui.draw.drawBehind+import androidx.compose.ui.draw.alphaimport androidx.compose.ui.draw.clipimport androidx.compose.ui.draw.alphaimport com.chethan616.clearpdf.ui.home.chipRowFade@@ -313,40 +314,55 @@ private fun PasswordField(label: String, value: String, onValue: (String) -> Uni@Composablefun ProtectScreen(vm: AppViewModel, doc: OpenDocument) {val c = pdfColors+ val ink = com.chethan616.clearpdf.ui.theme.clear+ val locked = doc.info.encrypted+ // the way that makes sense for this file is open first: a protected one to remove, else to set+ var removing by remember { mutableStateOf(locked) }var pw by remember { mutableStateOf("") }var confirm by remember { mutableStateOf("") }var visible by remember { mutableStateOf(false) }var info by remember { mutableStateOf(false) }- val focus = remember { androidx.compose.ui.focus.FocusRequester() }- LaunchedEffect(Unit) { runCatching { focus.requestFocus() } }val bad = pw.isNotEmpty() && (pw.length !in 6..128 || pw.any { it < '!' || it > '~' })val mismatch = confirm.isNotEmpty() && confirm != pw- ToolPage(stringResource(R.string.common_tool_set_password), { vm.toolScreen = null }, stringResource(R.string.common_tool_set_password), pw.isNotEmpty() && confirm.isNotEmpty() && !bad && !mismatch, { vm.protect(doc, pw) },+ val canSet = pw.isNotEmpty() && confirm.isNotEmpty() && !bad && !mismatch+ ToolScaffold(+ stringResource(R.string.tool_encryption), { vm.toolScreen = null },+ action = stringResource(if (removing) R.string.tools_unlock_action else R.string.common_tool_set_password),+ actionEnabled = if (removing) locked else canSet,+ onAction = { if (removing) vm.removePassword(doc) else vm.protect(doc, pw) },trailing = {com.chethan616.clearpdf.ui.glass.GlassCircle({ info = true }, label = stringResource(R.string.tools_about_passwords)) {com.chethan616.clearpdf.ui.glass.GlassIcon(LucideIcons.Info, null, Modifier.size(20.dp))}- }) {- Column(Modifier.verticalScroll(rememberScrollState())) {- // a protected file: the way back out first, an unlocked copy that never asks again- if (doc.info.encrypted) {- Column(- Modifier.padding(horizontal = 16.dp).padding(bottom = 18.dp).fillMaxWidth()- .clip(RoundedCornerShape(18.dp)).background(c.label.copy(alpha = 0.06f)).padding(16.dp),- ) {- Text(stringResource(R.string.tools_unlock_title), color = c.label, fontSize = 16.sp, fontWeight = FontWeight.SemiBold)- Text(stringResource(R.string.tools_unlock_body), color = c.text, fontSize = 14.sp, modifier = Modifier.padding(top = 4.dp, bottom = 12.dp))- com.chethan616.clearpdf.ui.glass.GlassPill(onClick = { vm.removePassword(doc) }, tint = com.chethan616.clearpdf.ui.theme.Candy.Sky, height = 44.dp) {- Icon(LucideIcons.LockOpen, null, tint = Color.White, modifier = Modifier.size(18.dp))- Text(stringResource(R.string.tools_unlock_action), color = Color.White, fontSize = 15.sp, fontWeight = FontWeight.SemiBold)- }- }+ },+ ) {+ Column(Modifier.verticalScroll(rememberScrollState()).padding(bottom = 12.dp)) {+ // how the file stands now+ Row(Modifier.padding(start = 24.dp, end = 24.dp, top = 4.dp, bottom = 14.dp), verticalAlignment = Alignment.CenterVertically) {+ Icon(if (locked) LucideIcons.Lock else LucideIcons.LockOpen, null, tint = ink.inkSoft, modifier = Modifier.size(16.dp))+ Spacer(Modifier.width(8.dp))+ Text(+ stringResource(if (locked) R.string.tools_unlock_title else R.string.msg_no_password_yet),+ style = com.chethan616.clearpdf.ui.theme.ClearType.Label, color = ink.inkSoft,+ )+ }+ EncryptionChoice(+ LucideIcons.Lock, com.chethan616.clearpdf.ui.theme.Candy.Grape,+ stringResource(R.string.common_tool_set_password), stringResource(R.string.tools_set_password_body),+ chosen = !removing, enabled = true, onClick = { removing = false },+ ) {+ PasswordField(stringResource(R.string.tools_new_password), pw, { pw = it }, visible, { visible = !visible }, bad)+ if (bad) Text(stringResource(R.string.tools_password_rule), color = c.red, fontSize = 13.sp, modifier = Modifier.padding(horizontal = 24.dp, vertical = 4.dp))+ PasswordField(stringResource(R.string.tools_repeat_password), confirm, { confirm = it }, visible, { visible = !visible }, mismatch)+ if (mismatch) Text(stringResource(R.string.tools_password_mismatch), color = c.red, fontSize = 13.sp, modifier = Modifier.padding(horizontal = 24.dp, vertical = 4.dp))+ Spacer(Modifier.height(16.dp))}- Text(stringResource(R.string.tools_protect_intro), color = c.label, fontSize = 16.sp, modifier = Modifier.padding(horizontal = 24.dp))- PasswordField(stringResource(R.string.tools_new_password), pw, { pw = it }, visible, { visible = !visible }, bad, focus)- if (bad) Text(stringResource(R.string.tools_password_rule), color = c.red, fontSize = 13.sp, modifier = Modifier.padding(horizontal = 24.dp, vertical = 4.dp))- PasswordField(stringResource(R.string.tools_repeat_password), confirm, { confirm = it }, visible, { visible = !visible }, mismatch)- if (mismatch) Text(stringResource(R.string.tools_password_mismatch), color = c.red, fontSize = 13.sp, modifier = Modifier.padding(horizontal = 24.dp, vertical = 4.dp))+ Spacer(Modifier.height(12.dp))+ EncryptionChoice(+ LucideIcons.LockOpen, com.chethan616.clearpdf.ui.theme.Candy.Mint,+ stringResource(R.string.tools_unlock_action), stringResource(if (locked) R.string.tools_unlock_body else R.string.msg_no_password),+ chosen = removing, enabled = locked, onClick = { removing = true },+ ) {}}}if (info) {@@ -356,6 +372,56 @@ fun ProtectScreen(vm: AppViewModel, doc: OpenDocument) {}}+/**+ * One of Encryption's two ways, as a card: its sign in its colour, what it is called and what it+ * does, a tick when it is the chosen one (the card is then ringed, and [more] opens under it). The+ * button at the foot of the screen does the chosen one. A way that can't apply (removing a password+ * from a file that has none) is shown faint and can't be chosen.+ */+@Composable+private fun EncryptionChoice(+ icon: androidx.compose.ui.graphics.vector.ImageVector,+ accent: Color,+ title: String,+ body: String,+ chosen: Boolean,+ enabled: Boolean,+ onClick: () -> Unit,+ more: @Composable ColumnScope.() -> Unit,+) {+ val ink = com.chethan616.clearpdf.ui.theme.clear+ val shape = com.chethan616.clearpdf.ui.glass.RoundedRectangle(24.dp)+ Column(+ Modifier.padding(horizontal = 16.dp).fillMaxWidth()+ .alpha(if (enabled) 1f else 0.5f)+ .clip(shape)+ .background(ink.glass)+ .border(if (chosen && enabled) 2.dp else 1.dp, if (chosen && enabled) com.chethan616.clearpdf.ui.theme.Candy.Sky else ink.ink.copy(alpha = 0.10f), shape),+ ) {+ Row(+ Modifier.fillMaxWidth().clickable(enabled = enabled, onClick = onClick).padding(16.dp),+ verticalAlignment = Alignment.CenterVertically,+ ) {+ Box(Modifier.size(44.dp).clip(com.chethan616.clearpdf.ui.glass.RoundedRectangle(14.dp)).background(accent), contentAlignment = Alignment.Center) {+ Icon(icon, null, tint = Color.White, modifier = Modifier.size(23.dp))+ }+ Spacer(Modifier.width(14.dp))+ Column(Modifier.weight(1f)) {+ Text(title, style = com.chethan616.clearpdf.ui.theme.ClearType.BodyStrong, color = ink.ink)+ Text(body, style = com.chethan616.clearpdf.ui.theme.ClearType.Caption, color = ink.inkSoft, modifier = Modifier.padding(top = 2.dp))+ }+ Spacer(Modifier.width(10.dp))+ Box(+ Modifier.size(24.dp).clip(androidx.compose.foundation.shape.CircleShape)+ .background(if (chosen && enabled) com.chethan616.clearpdf.ui.theme.Candy.Sky else Color.Transparent)+ .border(2.dp, if (chosen && enabled) com.chethan616.clearpdf.ui.theme.Candy.Sky else ink.inkFaint, androidx.compose.foundation.shape.CircleShape),+ contentAlignment = Alignment.Center,+ ) { if (chosen && enabled) Icon(LucideIcons.Check, null, tint = Color.White, modifier = Modifier.size(14.dp)) }+ }+ androidx.compose.animation.AnimatedVisibility(chosen && enabled) { Column(content = more) }+ }+}+/** Export PDF: Document (Word, RTF, Excel, PowerPoint) or Image (JPEG, PNG) of its pages. */@Composablefun ExportScreen(vm: AppViewModel, doc: OpenDocument) {diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/desk/Common.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/desk/Common.ktindex b48d18c..46bb098 100644--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/desk/Common.kt+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/desk/Common.kt@@ -92,16 +92,17 @@ object ToolCatalog {val Crop = ToolDef("crop", LucideIcons.Crop, R.string.common_tool_crop, R.string.home_about_crop, Candy.Mint, setOf("Edit")) { it.toolForFile(FileTool.Crop) }val Compress = ToolDef("compress", LucideIcons.FileArchive, R.string.common_tool_compress, R.string.home_about_compress, Candy.Mint, setOf("Convert")) { it.toolForFile(FileTool.Compress) }val Export = ToolDef("export", LucideIcons.FileOutput, R.string.common_tool_export, R.string.home_about_export, Candy.Tangerine, setOf("Convert")) { it.toolForFile(FileTool.Export) }- val Protect = ToolDef("protect", LucideIcons.FileLock2, R.string.common_tool_set_password, R.string.home_about_protect, Candy.Grape, setOf("Edit")) { it.toolForFile(FileTool.Protect) }+ // one tool for a PDF's password: set one, or take it off for good+ val Protect = ToolDef("protect", LucideIcons.FileLock2, R.string.tool_encryption, R.string.home_about_encryption, Candy.Grape, setOf("Edit")) { it.toolForFile(FileTool.Protect) }+ val EditPhoto = ToolDef("photo", LucideIcons.Images, R.string.tool_edit_photo, R.string.home_about_edit_photo, Candy.Lemon, setOf("Edit")) { it.pickPhotoToEdit() }val Photos = ToolDef("photos", LucideIcons.Images, R.string.desk_photos_to_pdf, R.string.home_about_create, Candy.Lemon, setOf("Create")) { it.pickPhotosToPdf() }// the tools asked for on the tracker, each one tap from the Tools tabval TextToPdf = ToolDef("text2pdf", LucideIcons.Type, R.string.tool_text_to_pdf, R.string.home_about_text_to_pdf, Candy.Sky, setOf("Create", "Convert")) { it.openCreateFromText() }val PdfToText = ToolDef("pdf2text", LucideIcons.FileText, R.string.tool_pdf_to_text, R.string.home_about_pdf_to_text, Candy.Lemon, setOf("Convert")) { it.toolForFile(FileTool.ToText) }val FindFields = ToolDef("fields", LucideIcons.ScanText, R.string.tool_find_fields, R.string.home_about_find_fields, Candy.Bubblegum, setOf("Edit")) { it.toolForFile(FileTool.FindFields) }- val Unlock = ToolDef("unlock", LucideIcons.LockOpen, R.string.tools_unlock_action, R.string.home_about_unlock, Candy.Mint, setOf("Edit")) { it.toolForFile(FileTool.Unlock) }- val All = listOf(Edit, FillSign, Scan, Combine, Comment, ReadAloud, Organize, Compress, Export, Crop, Protect, Create, TextToPdf, PdfToText, FindFields, Unlock)+ val All = listOf(Edit, FillSign, Scan, Combine, Comment, ReadAloud, Organize, Compress, Export, Crop, Protect, Create, TextToPdf, PdfToText, FindFields, EditPhoto)}/**diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/notes/PhotoEditor.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/notes/PhotoEditor.ktindex faa2994..d6548de 100644--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/notes/PhotoEditor.kt+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/notes/PhotoEditor.kt@@ -113,6 +113,8 @@ private enum class Adjust(Clarity("effects", "clarity", R.string.photo_clarity, LucideIcons.Sparkles, -100f, 100f, 0),Sharpen("detail", "sharpen_amount", R.string.photo_sharpen, androidx.compose.material.icons.Icons.Rounded.Details, 0f, 150f, 0),Vignette("vignette", "amount", R.string.photo_vignette, androidx.compose.material.icons.Icons.Rounded.Vignette, -100f, 100f, 0),+ // below zero it softens the picture (the nearest LightCraft has to a blur), above it brings detail out+ Texture("effects", "texture", R.string.photo_texture, LucideIcons.Droplet, -100f, 100f, 0),}private val TURNS = listOf("Normal", "Rotate90", "Rotate180", "Rotate270")@@ -190,6 +192,8 @@ fun PhotoEditScreen(vm: AppViewModel, noteId: String, name: String, onClose: ()vm.notes.get(noteId)?.let { note ->vm.notes.put(note.copy(images = note.images.map { if (it == name) output.name else it }))}+ // opened as a tool: the edited picture is also put in Downloads+ vm.savePhotoCopy(output)} else {vm.tell(com.chethan616.clearpdf.text(R.string.photo_not_saved))}diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/viewer/QuickTools.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/viewer/QuickTools.ktindex e70c203..e2cdc66 100644--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/viewer/QuickTools.kt+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/viewer/QuickTools.kt@@ -80,7 +80,7 @@ enum class QuickTool(val id: String, @StringRes val label: Int, @StringRes val tExport("export", R.string.common_tool_export, R.string.common_tool_export, LucideIcons.FileOutput, ToolHues.export),Combine("combine", R.string.toolbar_label_combine, R.string.common_tool_combine, LucideIcons.Combine, ToolHues.combine),Compress("compress", R.string.toolbar_label_compress, R.string.common_tool_compress, LucideIcons.FileArchive, ToolHues.compress),- Protect("protect", R.string.toolbar_label_password, R.string.common_tool_set_password, LucideIcons.FileLock2, ToolHues.protect),+ Protect("protect", R.string.tool_encryption, R.string.tool_encryption, LucideIcons.FileLock2, ToolHues.protect),Organize("organize", R.string.toolbar_label_organize, R.string.common_tool_organize, LucideIcons.LayoutGrid, ToolHues.organize),Crop("crop", R.string.toolbar_label_crop, R.string.common_tool_crop, LucideIcons.Crop, ToolHues.crop),Ocr("ocr", R.string.toolbar_label_recognize, R.string.common_tool_recognize_text, LucideIcons.ScanText, ToolHues.ocr),diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xmlindex b75d5c1..2b54636 100644--- a/android/app/src/main/res/values/strings.xml+++ b/android/app/src/main/res/values/strings.xml@@ -168,6 +168,13 @@<string name="home_about_compress">Make a PDF smaller</string><string name="home_about_export">Save a PDF as a document, spreadsheet, slides or images</string><string name="home_about_protect">Protect a PDF with a password</string>+ <string name="tool_encryption">Encryption</string>+ <string name="home_about_encryption">Set a PDF\'s password, or remove it for good</string>+ <string name="tools_set_password_body">Save a copy that asks for a password before it opens</string>+ <string name="msg_no_password_yet">This PDF has no password</string>+ <string name="tool_edit_photo">Edit photo</string>+ <string name="home_about_edit_photo">Crop, straighten and adjust a picture</string>+ <string name="photo_texture">Soften / Texture</string><string name="tool_text_to_pdf">Text to PDF</string><string name="home_about_text_to_pdf">Type or paste text and save it as a PDF</string><string name="tool_pdf_to_text">PDF to text</string>