Add per-account mail rules and settings
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -200,14 +200,37 @@ class MailIdleService : Service() {
|
||||
if (count <= 0) return
|
||||
val appContext = context.applicationContext
|
||||
val email = account.optString("email")
|
||||
val notificationManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
|
||||
notificationManager.getNotificationChannel(MAIL_CHANNEL_ID) == null) {
|
||||
notificationManager.createNotificationChannel(NotificationChannel(
|
||||
MAIL_CHANNEL_ID,
|
||||
"New mail",
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
).apply { description = "Notifies you when new mail arrives." })
|
||||
if (account.optBoolean("notificationsEnabled", true)) {
|
||||
val notificationManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
|
||||
notificationManager.getNotificationChannel(MAIL_CHANNEL_ID) == null) {
|
||||
notificationManager.createNotificationChannel(NotificationChannel(
|
||||
MAIL_CHANNEL_ID,
|
||||
"New mail",
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
).apply { description = "Notifies you when new mail arrives." })
|
||||
}
|
||||
val launchIntent = appContext.packageManager.getLaunchIntentForPackage(appContext.packageName)
|
||||
?: Intent(appContext, MainActivity::class.java)
|
||||
launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
||||
val pendingIntent = PendingIntent.getActivity(
|
||||
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(email)
|
||||
.setAutoCancel(true)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setContentIntent(pendingIntent)
|
||||
.build()
|
||||
try {
|
||||
NotificationManagerCompat.from(appContext).notify(email.hashCode(), notification)
|
||||
} catch (error: SecurityException) {
|
||||
android.util.Log.w(TAG, "Notification permission is unavailable", error)
|
||||
}
|
||||
}
|
||||
val update = JSONObject()
|
||||
.put("accountId", account.optString("id"))
|
||||
@@ -216,56 +239,37 @@ class MailIdleService : Service() {
|
||||
appContext.sendBroadcast(Intent(ACTION_MAIL_SYNC)
|
||||
.setPackage(appContext.packageName)
|
||||
.putExtra(EXTRA_SYNC_UPDATE, update.toString()))
|
||||
val launchIntent = appContext.packageManager.getLaunchIntentForPackage(appContext.packageName)
|
||||
?: Intent(appContext, MainActivity::class.java)
|
||||
launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
||||
val pendingIntent = PendingIntent.getActivity(
|
||||
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(email)
|
||||
.setAutoCancel(true)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setContentIntent(pendingIntent)
|
||||
.build()
|
||||
try {
|
||||
NotificationManagerCompat.from(appContext).notify(email.hashCode(), notification)
|
||||
} catch (error: SecurityException) {
|
||||
android.util.Log.w(TAG, "Notification permission is unavailable", error)
|
||||
}
|
||||
}
|
||||
|
||||
fun notifySnoozeReminder(context: Context, account: JSONObject, reminder: JSONObject) {
|
||||
val appContext = context.applicationContext
|
||||
val email = account.optString("email")
|
||||
val manager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
|
||||
manager.getNotificationChannel(MAIL_CHANNEL_ID) == null) {
|
||||
manager.createNotificationChannel(NotificationChannel(
|
||||
MAIL_CHANNEL_ID,
|
||||
"Mail reminders",
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
))
|
||||
}
|
||||
val notification = NotificationCompat.Builder(appContext, MAIL_CHANNEL_ID)
|
||||
.setSmallIcon(android.R.drawable.ic_dialog_email)
|
||||
.setContentTitle("Mail reminder")
|
||||
.setContentText(reminder.optString("subject").ifBlank { "A snoozed message is due" })
|
||||
.setAutoCancel(true)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setContentIntent(appPendingIntentFor(appContext))
|
||||
.build()
|
||||
try {
|
||||
NotificationManagerCompat.from(appContext).notify(
|
||||
(email + reminder.optLong("uid")).hashCode(),
|
||||
notification
|
||||
)
|
||||
} catch (error: SecurityException) {
|
||||
android.util.Log.w(TAG, "Reminder notification permission is unavailable", error)
|
||||
if (account.optBoolean("notificationsEnabled", true)) {
|
||||
val manager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
|
||||
manager.getNotificationChannel(MAIL_CHANNEL_ID) == null) {
|
||||
manager.createNotificationChannel(NotificationChannel(
|
||||
MAIL_CHANNEL_ID,
|
||||
"Mail reminders",
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
))
|
||||
}
|
||||
val notification = NotificationCompat.Builder(appContext, MAIL_CHANNEL_ID)
|
||||
.setSmallIcon(android.R.drawable.ic_dialog_email)
|
||||
.setContentTitle("Mail reminder")
|
||||
.setContentText(reminder.optString("subject").ifBlank { "A snoozed message is due" })
|
||||
.setAutoCancel(true)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setContentIntent(appPendingIntentFor(appContext))
|
||||
.build()
|
||||
try {
|
||||
NotificationManagerCompat.from(appContext).notify(
|
||||
(email + reminder.optLong("uid")).hashCode(),
|
||||
notification
|
||||
)
|
||||
} catch (error: SecurityException) {
|
||||
android.util.Log.w(TAG, "Reminder notification permission is unavailable", error)
|
||||
}
|
||||
}
|
||||
val update = JSONObject()
|
||||
.put("accountId", account.optString("id"))
|
||||
|
||||
Reference in New Issue
Block a user