Disable notification bundling; add default question

pull/542/head
Alinson S. Xavier 6 years ago
parent 7c742e1016
commit 58ed759224

@ -25,7 +25,6 @@ import android.graphics.*
import android.graphics.BitmapFactory.*
import android.os.*
import android.os.Build.VERSION.*
import android.support.annotation.*
import android.support.v4.app.*
import android.support.v4.app.NotificationCompat.*
import android.util.*
@ -38,9 +37,6 @@ import org.isoron.uhabits.core.ui.*
import org.isoron.uhabits.intents.*
import javax.inject.*
@AppScope
class AndroidNotificationTray
@Inject constructor(
@ -48,7 +44,7 @@ class AndroidNotificationTray
private val pendingIntents: PendingIntentFactory,
private val preferences: Preferences,
private val ringtoneManager: RingtoneManager
) : NotificationTray.SystemTray {
) : NotificationTray.SystemTray {
private var active = HashSet<Int>()
override fun log(msg: String) {
@ -62,16 +58,15 @@ class AndroidNotificationTray
active.remove(id)
// Clear the group summary notification
if(active.isEmpty()) manager.cancelAll()
if (active.isEmpty()) manager.cancelAll()
}
override fun showNotification(habit: Habit,
notificationId: Int,
timestamp: Timestamp,
reminderTime: Long)
{
reminderTime: Long) {
val notificationManager = NotificationManagerCompat.from(context)
val summary = buildSummary(reminderTime)
val summary = buildSummary(habit, reminderTime)
notificationManager.notify(Int.MAX_VALUE, summary)
val notification = buildNotification(habit, reminderTime, timestamp)
createAndroidNotificationChannel(context)
@ -79,20 +74,22 @@ class AndroidNotificationTray
notificationManager.notify(notificationId, notification)
} catch (e: RuntimeException) {
// Some Xiaomi phones produce a RuntimeException if custom notification sounds are used.
Log.i("AndroidNotificationTray", "Failed to show notification. Retrying without sound.")
val n = buildNotification(habit, reminderTime, timestamp, disableSound = true)
Log.i("AndroidNotificationTray",
"Failed to show notification. Retrying without sound.")
val n = buildNotification(habit,
reminderTime,
timestamp,
disableSound = true)
notificationManager.notify(notificationId, n)
}
active.add(notificationId)
}
@NonNull
fun buildNotification(@NonNull habit: Habit,
@NonNull reminderTime: Long,
@NonNull timestamp: Timestamp,
disableSound: Boolean = false) : Notification
{
fun buildNotification(habit: Habit,
reminderTime: Long,
timestamp: Timestamp,
disableSound: Boolean = false): Notification {
val addRepetitionAction = Action(
R.drawable.ic_action_check,
@ -114,10 +111,11 @@ class AndroidNotificationTray
.addAction(addRepetitionAction)
.addAction(removeRepetitionAction)
val defaultText = context.getString(R.string.default_reminder_question)
val builder = NotificationCompat.Builder(context, REMINDERS_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(habit.name)
.setContentText(habit.description)
.setContentText(if(habit.description.isBlank()) defaultText else habit.description)
.setContentIntent(pendingIntents.showHabit(habit))
.setDeleteIntent(pendingIntents.dismissNotification(habit))
.addAction(addRepetitionAction)
@ -126,7 +124,7 @@ class AndroidNotificationTray
.setWhen(reminderTime)
.setShowWhen(true)
.setOngoing(preferences.shouldMakeNotificationsSticky())
.setGroup("default")
.setGroup("group" + habit.getId())
if (!disableSound)
builder.setSound(ringtoneManager.getURI())
@ -134,7 +132,7 @@ class AndroidNotificationTray
if (preferences.shouldMakeNotificationsLed())
builder.setLights(Color.RED, 1000, 1000)
if(SDK_INT < Build.VERSION_CODES.O) {
if (SDK_INT < Build.VERSION_CODES.O) {
val snoozeAction = Action(R.drawable.ic_action_snooze,
context.getString(R.string.snooze),
pendingIntents.snoozeNotification(habit))
@ -146,26 +144,24 @@ class AndroidNotificationTray
return builder.build()
}
@NonNull
private fun buildSummary(@NonNull reminderTime: Long) : Notification
{
private fun buildSummary(habit: Habit,
reminderTime: Long): Notification {
return NotificationCompat.Builder(context, REMINDERS_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.app_name))
.setWhen(reminderTime)
.setShowWhen(true)
.setGroup("default")
.setGroup("group" + habit.getId())
.setGroupSummary(true)
.build()
}
companion object {
private val REMINDERS_CHANNEL_ID = "REMINDERS"
private const val REMINDERS_CHANNEL_ID = "REMINDERS"
fun createAndroidNotificationChannel(context: Context) {
val notificationManager = context.getSystemService(Activity.NOTIFICATION_SERVICE)
as NotificationManager
if (SDK_INT >= Build.VERSION_CODES.O)
{
if (SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(REMINDERS_CHANNEL_ID,
context.resources.getString(R.string.reminder),
NotificationManager.IMPORTANCE_DEFAULT)

@ -243,5 +243,6 @@
<string name="widget_opacity_title">Widget opacity</string>
<string name="widget_opacity_description">Makes widgets more transparent or more opaque in your home screen.</string>
<string name="first_day_of_the_week">First day of the week</string>
<string name="default_reminder_question">Have you completed this habit today?</string>
</resources>
Loading…
Cancel
Save