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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ATTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ Generated-in-code art is original and has no file to list: the UI icon set (`cra
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_notifications_settings_notifications_dark_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android Settings (MB3.14) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_notifications_settings_notifications_dark_200.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android Settings (MB3.14) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_notifications_settings_notifications_light_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android Settings (MB3.14) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_notificationButtons_settings_notification_buttons_light_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android Settings' notification buttons (#1992) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_notificationButtons_settings_notification_buttons_dark_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android Settings' notification buttons (#1992) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_notificationButtons_settings_notification_buttons_light_200.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android Settings' notification buttons (#1992) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_notificationButtons_settings_notification_buttons_dark_200.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android Settings' notification buttons (#1992) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_notifications_settings_notifications_light_200.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android Settings (MB3.14) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_pictures_settings_pictures_dark_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android Settings (MB3.14) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_pictures_settings_pictures_dark_200.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android Settings (MB3.14) |
Expand Down
13 changes: 13 additions & 0 deletions apps/mailcraft-android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
</intent-filter>
</receiver>

<!-- A new-mail notification's buttons that act without opening the app (Delete, Archive,
Mark as read, Flag, Junk). Not exported: only the app's own notifications reach it. -->
<receiver
android:name=".NoticeActionReceiver"
android:exported="false" />

<activity
android:name=".MainActivity"
android:exported="true"
Expand Down Expand Up @@ -116,6 +122,13 @@
</intent-filter>
</activity>

<!-- A new-mail notification's Reply opens the window through this alias, which no other app
can reach: the window takes a reply to a message from it alone. -->
<activity-alias
android:name=".NoticeReply"
android:exported="false"
android:targetActivity=".MainActivity" />

<!-- The provider's page sends the owner back to the app's own scheme (RFC 8252 7.1). The
scheme is the reverse of a domain the project owns and the host is the registered one
(Entra takes only the `//` form); the whole address is checked in the app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,16 @@ internal class ComposeHost(
/**
* Another app asked for a message: a `mailto:` link (the core reads it, and the message is only ever
* started, never sent), or something shared (text, links, files, read off the main thread under the caps).
* With a message already open nothing is taken in, and the owner is told so.
* With a message already open nothing is taken in, and the owner is told so. Whether it was taken.
*/
fun beginIncoming(incoming: Incoming, reader: ShareReader, ownAuthority: String) {
fun beginIncoming(incoming: Incoming, reader: ShareReader, ownAuthority: String): Boolean {
if (controller != null) {
busy()
return false
}
when (incoming) {
is Incoming.Mailto -> if (controller == null) begin(ComposeStart.New(0uL, incoming.link)) else busy()
is Incoming.Mailto, is Incoming.Reply -> incoming.start?.let(::begin)
is Incoming.Share -> {
if (controller != null) return busy()
scope.launch {
reading = true
val load = try {
Expand All @@ -122,6 +125,7 @@ internal class ComposeHost(
}
}
}
return true
}

private fun busy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import android.app.Application
import android.os.Bundle
import android.util.Log
import java.io.File
import java.util.concurrent.atomic.AtomicBoolean
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeoutOrNull
import uniffi.mailcraft_mobile.Core
import uniffi.mailcraft_mobile.CoreException
import uniffi.mailcraft_mobile.Event
Expand Down Expand Up @@ -116,6 +119,54 @@ class MailCraftApplication : Application() {
return reminders.refresh(ReminderWords.from(opened.core::t)) { askSchedule(opened.core, it) }
}

/**
* A new-mail notification's [button] ([NoticeActionReceiver]): [triage] made to [message] with the
* one core, opened when no window has (waited for no longer than [NOTICE_CORE_WAIT_MS]), in turn
* with the list's commands, and only while the message is still in the [folder] it was told of
* in (the core looks and acts in one step: one deleted twice would be gone for good). The mailbox
* is saved before the notification says it is done, so that the change outlives the process; the
* next Send/Receive takes it to the server, as a change made in the list. The notification is
* not taken away (the shade would close with its last one): a short word takes its place, what
* was done or that nothing could be ([confirmMailNotice]). [finished] is called when it is over,
* or after [NOTICE_BUDGET_MS] at the latest (the system gives a receiver little time; what is
* under way then goes on while the process lasts).
*/
internal fun runNoticeButton(
button: NoticeButton,
triage: Triage,
message: ULong,
folder: ULong,
finished: () -> Unit,
) {
val over = AtomicBoolean(false)
val finish = { if (over.compareAndSet(false, true)) finished() }
val work = scope.launch {
var outcome = NoticeOutcome.Failed
var texts: Texts? = null
try {
begin()
val phase = withTimeoutOrNull(NOTICE_CORE_WAIT_MS) { started.first { it !is Phase.Loading } }
val opened = (phase as? Phase.Ready)?.value ?: return@launch
texts = CoreTexts(opened.core)
val acted = try {
opened.mail.inTurn({ opened.core.noticeCommand(message, folder, triage.command()) }) { it }
} catch (_: CoreException) {
return@launch
}
if (opened.hub.saveNow()) outcome = if (acted) NoticeOutcome.Done else NoticeOutcome.Moved
} finally {
val said = noticeConfirmation(button, outcome, texts ?: EnglishTexts)
confirmMailNotice(applicationContext, message, said)
finish()
}
}
val budget = scope.launch {
delay(NOTICE_BUDGET_MS)
finish()
}
work.invokeOnCompletion { budget.cancel() }
}

/**
* The mailbox is written when no window is on screen: the system may end the process after it.
* (A core still opening has nothing of the owner's to write yet.)
Expand Down Expand Up @@ -193,7 +244,8 @@ class MailCraftApplication : Application() {
events.register(opened.hub::handle)
val onlyNewMail = { DeviceSwitches(applicationContext).load().newMailOnly }
val remindersOn = { DeviceSwitches(applicationContext).load().reminders }
events.register(MailNotifier(applicationContext, opened.core::t, onlyNewMail, remindersOn)::handle)
val buttons = { DeviceNoticeButtons(applicationContext).load() }
events.register(MailNotifier(applicationContext, opened.core::t, onlyNewMail, remindersOn, buttons)::handle)
// The words of what a background round says (a notification's "New mail") are the
// window's language even when no window has opened (the owner's saved choice first).
opened.core.setLanguage(LanguageChoice.inUse(this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,14 @@ internal class MailListController(
}

/**
* Marks message [id] read, as opening it does: a command made in turn like the owner's, so that an
* undo offered before it is withdrawn (undo would now take back the mark). Whether it was done.
* Runs [command], made outside the list (opening a message marks it read; a notification's button
* deletes it), in turn with the list's own commands, so that an undo offered before it is
* withdrawn once it [made] a change (undo would now take back that change instead).
*/
suspend fun markRead(id: ULong): Boolean {
val done = commandLock.withLock {
val mark = Triage.MarkRead(read = true)
source.run(mark.command(), jsonText(mark.params(listOf(id)))).also { if (it) commands.incrementAndGet() }
}
if (done) state.update { if (it.notice?.notice?.undoable == true) it.copy(notice = null) else it }
return done
suspend fun <T> inTurn(command: suspend () -> T, made: (T) -> Boolean): T {
val result = commandLock.withLock { command().also { if (made(it)) commands.incrementAndGet() } }
if (made(result)) state.update { if (it.notice?.notice?.undoable == true) it.copy(notice = null) else it }
return result
}

/**
Expand Down Expand Up @@ -273,6 +271,10 @@ internal class MailListController(
}
}

/** Makes [triage] to message [id] alone, in turn ([MailListController.inTurn]); whether it was done. */
internal suspend fun MailListController.runOn(triage: Triage, id: ULong): Boolean =
inTurn({ source.run(triage.command(), jsonText(triage.params(listOf(id)))) }) { it }

/** The categories mail can be given, with their colours. */
internal suspend fun MailListController.categories(): List<CategoryItem> = source.categories()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ internal fun AskForNotificationsOnce(hasAccount: Boolean, answered: () -> Unit)
* switch for the app and for the channel). The notification is private: where the system hides
* private content on the lock screen (its sensitive-content setting), only "New mail" shows there
* and what the message says waits until the phone is unlocked; where the owner lets the lock
* screen show all of it, the sender and subject show there too. Tapping one opens the app.
* screen show all of it, the sender and subject show there too. Tapping one opens the app; a
* notification about one message has the buttons the owner chose ([NoticeButtons]), which Android
* 12 and later let be used only once the phone is unlocked.
*/
internal class MailNotifier(
private val context: Context,
Expand All @@ -107,6 +109,8 @@ internal class MailNotifier(
private val onlyNewMail: () -> Boolean = { false },
/** The owner's Reminders switch, read as each one is shown: off, no calendar reminder is posted. */
private val remindersOn: () -> Boolean = { true },
/** The buttons the owner chose for new mail, read as each one is shown. */
private val buttons: () -> NoticeButtons = { NoticeButtons(emptyList()) },
) {
fun handle(event: Event) {
if (event !is Event.Notification) return
Expand All @@ -132,7 +136,7 @@ internal class MailNotifier(
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(if (reminder) t("Reminders") else t("New mail"))
.build()
val notification = NotificationCompat.Builder(context, channel)
val builder = NotificationCompat.Builder(context, channel)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(record.title)
.setContentText(record.body)
Expand All @@ -141,7 +145,20 @@ internal class MailNotifier(
.setPublicVersion(hidden)
.setContentIntent(open)
.setAutoCancel(true)
.build()
// A notification about one message has the owner's buttons; they carry its number alone, and
// the version a locked screen shows (the hidden one) has none.
val about = record.message.takeIf { !reminder }
if (about != null) {
buttons().chosen.forEach { button ->
val press = noticePendingIntent(context, button, about)
val action = NotificationCompat.Action.Builder(0, button.label(t), press)
// Nothing is done from a locked phone (Android 12 and later ask for it to be unlocked first).
.setAuthenticationRequired(true)
.build()
builder.addAction(action)
}
}
val notification = builder.build()
try {
val tag = if (reminder) REMINDER_NOTIFICATION_TAG else MAIL_NOTIFICATION_TAG
NotificationManagerCompat.from(context).notify(tag, notificationId(record.key), notification)
Expand All @@ -163,4 +180,4 @@ internal fun postsNotification(topic: NotificationTopic, remindersOn: Boolean):
* [newMail] and nothing of the message, whatever the lock screen shows.
*/
internal fun privateRecord(record: NotificationRecord, onlyNewMail: Boolean, newMail: String): NotificationRecord =
if (onlyNewMail) record.copy(title = newMail, body = "") else record
if (onlyNewMail) record.copy(title = newMail, body = "", message = null) else record
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ class MainActivity : AppCompatActivity() {
*/
internal fun incomingOf(intent: Intent?): Incoming? {
if (intent == null || intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0) return null
// A notification's Reply comes only through the alias no other app can reach.
if (intent.component?.className == NOTICE_REPLY_ALIAS) {
return noticeAskOf(intent)?.takeIf { it.button == NoticeButton.Reply }?.let { Incoming.Reply(it.message) }
}
return try {
routeIncoming(
action = intent.action,
Expand Down Expand Up @@ -164,7 +168,10 @@ private fun TakeIncoming(texts: Texts, asked: Asked, writing: ComposeHost, conte
LaunchedEffect(wanted) {
if (wanted != null) {
asked.handled()
writing.beginIncoming(wanted, shareReader, OpenedFiles.authority(context))
val taken = writing.beginIncoming(wanted, shareReader, OpenedFiles.authority(context))
// A notification's Reply takes its notification away once the reply is open; while
// another message is being written it stays, to be pressed again.
if (taken && wanted is Incoming.Reply) cancelMailNotice(context, wanted.message)
}
}
IncomingLayers(texts, writing)
Expand Down Expand Up @@ -233,6 +240,8 @@ private fun CoreShell(
val appContext = LocalContext.current.applicationContext
val prefs = remember { DeviceSwipePrefs(appContext) }
var swipe by remember { mutableStateOf(prefs.load()) }
val noticeStore = remember { DeviceNoticeButtons(appContext) }
var noticeButtons by remember { mutableStateOf(noticeStore.load()) }
val readingStore = remember { DeviceReadingPrefs(appContext) }
var readingPrefs by remember { mutableStateOf(readingStore.load()) }
// (A folder of 0 is the first Inbox.)
Expand Down Expand Up @@ -279,6 +288,7 @@ private fun CoreShell(
categories = categories,
nowMillis = now,
swipe = swipe,
noticeButtons = noticeButtons,
readingPrefs = readingPrefs,
reading = pictured.reading,
pictures = pictured.settings,
Expand All @@ -300,6 +310,10 @@ private fun CoreShell(
swipe = it
prefs.save(it)
},
onNoticeButtons = {
noticeButtons = it
noticeStore.save(it)
},
onReadingPrefs = {
readingPrefs = it
readingStore.save(it)
Expand Down Expand Up @@ -369,7 +383,9 @@ private class Pictured(

@Composable
private fun rememberPictures(core: Core, list: MailListController, scope: CoroutineScope, context: Context): Pictured {
val reader = remember(core, list) { ReadingStack(core, context, scope, list::markRead) }
val reader = remember(core, list) {
ReadingStack(core, context, scope) { id -> list.runOn(Triage.MarkRead(read = true), id) }
}
val stack = reader.pictures
val attachments by reader.attachments.ui.collectAsState()
val reading by stack.reading.ui.collectAsState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal enum class SettingsPage {
Accounts,
MailOffline,
Notifications,
NotificationButtons,
SwipeActions,
Reading,
Pictures,
Expand All @@ -42,6 +43,7 @@ internal enum class SettingsPage {
Accounts -> texts.t("Accounts")
MailOffline -> texts.t("Mail to keep offline")
Notifications -> texts.t("Notifications")
NotificationButtons -> texts.t("Notification buttons")
SwipeActions -> texts.t("Swipe actions")
Reading -> texts.t("Reading")
Pictures -> texts.t("Pictures")
Expand Down
Loading
Loading