Implemented the Android notification-privacy part of Phase 1:
This commit is contained in:
@@ -56,6 +56,16 @@ internal object AndroidAccountVault {
|
||||
}
|
||||
|
||||
fun accounts(): JSONArray = read().optJSONArray("accounts") ?: JSONArray()
|
||||
|
||||
fun notificationDetailsEnabled(): Boolean =
|
||||
read().optBoolean("showNotificationDetails", NotificationPrivacyPolicy.DEFAULT_SHOW_DETAILS)
|
||||
|
||||
@Synchronized
|
||||
fun setNotificationDetailsEnabled(enabled: Boolean): Boolean {
|
||||
val store = read().put("showNotificationDetails", enabled)
|
||||
write(store)
|
||||
return enabled
|
||||
}
|
||||
|
||||
fun account(id: String? = null): JSONObject? {
|
||||
val store = read()
|
||||
|
||||
@@ -54,7 +54,28 @@ class EpMailPlugin : Plugin() {
|
||||
@PluginMethod
|
||||
fun getAppVersion(call: PluginCall) {
|
||||
call.resolve(JSObject().put("version", BuildConfig.VERSION_NAME))
|
||||
}
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun getNotificationPrivacy(call: PluginCall) {
|
||||
try {
|
||||
call.resolve(JSObject().put("showDetails", AndroidAccountVault.notificationDetailsEnabled()))
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not read notification privacy settings.")
|
||||
}
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun setNotificationDetails(call: PluginCall) {
|
||||
try {
|
||||
val enabled = call.getBoolean("enabled")
|
||||
?: throw IllegalArgumentException("Choose whether notifications may show details.")
|
||||
AndroidAccountVault.setNotificationDetailsEnabled(enabled)
|
||||
call.resolve(JSObject().put("showDetails", enabled))
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not save notification privacy settings.")
|
||||
}
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun getAccount(call: PluginCall) {
|
||||
|
||||
@@ -136,6 +136,7 @@ class MailIdleService : Service() {
|
||||
.setContentIntent(appPendingIntent())
|
||||
.setOngoing(true)
|
||||
.setPriority(NotificationCompat.PRIORITY_LOW)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
||||
.build()
|
||||
|
||||
private fun appPendingIntent(): PendingIntent {
|
||||
@@ -219,14 +220,21 @@ class MailIdleService : Service() {
|
||||
appContext, 0, launchIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
val title = if (count == 1) "New mail" else "$count new messages"
|
||||
val notification = NotificationCompat.Builder(appContext, MAIL_CHANNEL_ID)
|
||||
.setSmallIcon(android.R.drawable.ic_dialog_email)
|
||||
.setContentTitle(title)
|
||||
.setContentText("Open ep-mail to check your inbox")
|
||||
.setAutoCancel(true)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setContentIntent(pendingIntent)
|
||||
val showDetails = runCatching { AndroidAccountVault.notificationDetailsEnabled() }.getOrDefault(false)
|
||||
val notification = NotificationCompat.Builder(appContext, MAIL_CHANNEL_ID)
|
||||
.setSmallIcon(android.R.drawable.ic_dialog_email)
|
||||
.setContentTitle("New mail")
|
||||
.setContentText(NotificationPrivacyPolicy.newMailText(showDetails, email, count))
|
||||
.setAutoCancel(true)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
||||
.setPublicVersion(NotificationCompat.Builder(appContext, MAIL_CHANNEL_ID)
|
||||
.setSmallIcon(android.R.drawable.ic_dialog_email)
|
||||
.setContentTitle("ep-mail")
|
||||
.setContentText("New mail")
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
.build())
|
||||
.setContentIntent(pendingIntent)
|
||||
.build()
|
||||
try {
|
||||
NotificationManagerCompat.from(appContext).notify(email.hashCode(), notification)
|
||||
@@ -256,13 +264,21 @@ class MailIdleService : Service() {
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
))
|
||||
}
|
||||
val notification = NotificationCompat.Builder(appContext, MAIL_CHANNEL_ID)
|
||||
.setSmallIcon(android.R.drawable.ic_dialog_email)
|
||||
.setContentTitle("Mail reminder")
|
||||
.setContentText("A snoozed message is due")
|
||||
.setAutoCancel(true)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setContentIntent(appPendingIntentFor(appContext))
|
||||
val showDetails = runCatching { AndroidAccountVault.notificationDetailsEnabled() }.getOrDefault(false)
|
||||
val notification = NotificationCompat.Builder(appContext, MAIL_CHANNEL_ID)
|
||||
.setSmallIcon(android.R.drawable.ic_dialog_email)
|
||||
.setContentTitle("Mail reminder")
|
||||
.setContentText(NotificationPrivacyPolicy.reminderText(showDetails, reminder.optString("subject")))
|
||||
.setAutoCancel(true)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
||||
.setPublicVersion(NotificationCompat.Builder(appContext, MAIL_CHANNEL_ID)
|
||||
.setSmallIcon(android.R.drawable.ic_dialog_email)
|
||||
.setContentTitle("ep-mail")
|
||||
.setContentText("A mail reminder is due")
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
.build())
|
||||
.setContentIntent(appPendingIntentFor(appContext))
|
||||
.build()
|
||||
try {
|
||||
NotificationManagerCompat.from(appContext).notify(
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package studio.daybreak.mail
|
||||
|
||||
internal object NotificationPrivacyPolicy {
|
||||
const val DEFAULT_SHOW_DETAILS = false
|
||||
|
||||
fun newMailText(showDetails: Boolean, email: String, count: Int): String =
|
||||
if (showDetails) {
|
||||
if (count == 1) email else "$count new messages · $email"
|
||||
} else {
|
||||
"Open ep-mail to check your inbox"
|
||||
}
|
||||
|
||||
fun reminderText(showDetails: Boolean, subject: String): String =
|
||||
if (showDetails && subject.isNotBlank()) subject else "A snoozed message is due"
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package studio.daybreak.mail
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Test
|
||||
|
||||
class NotificationPrivacyPolicyTest {
|
||||
@Test
|
||||
fun notificationDetailsAreDisabledByDefault() {
|
||||
assertFalse(NotificationPrivacyPolicy.DEFAULT_SHOW_DETAILS)
|
||||
assertEquals(
|
||||
"Open ep-mail to check your inbox",
|
||||
NotificationPrivacyPolicy.newMailText(NotificationPrivacyPolicy.DEFAULT_SHOW_DETAILS, "person@example.test", 1)
|
||||
)
|
||||
assertEquals(
|
||||
"A snoozed message is due",
|
||||
NotificationPrivacyPolicy.reminderText(NotificationPrivacyPolicy.DEFAULT_SHOW_DETAILS, "Private subject")
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun detailsAppearOnlyAfterExplicitOptIn() {
|
||||
assertEquals("person@example.test", NotificationPrivacyPolicy.newMailText(true, "person@example.test", 1))
|
||||
assertEquals(
|
||||
"3 new messages · person@example.test",
|
||||
NotificationPrivacyPolicy.newMailText(true, "person@example.test", 3)
|
||||
)
|
||||
assertEquals("Private subject", NotificationPrivacyPolicy.reminderText(true, "Private subject"))
|
||||
assertEquals("A snoozed message is due", NotificationPrivacyPolicy.reminderText(true, " "))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user