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

@ -243,5 +243,6 @@
<string name="widget_opacity_title">Widget opacity</string> <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="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="first_day_of_the_week">First day of the week</string>
<string name="default_reminder_question">Have you completed this habit today?</string>
</resources> </resources>
Loading…
Cancel
Save