From 58ed7592241d3bad87b3b70517147807052056bc Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Fri, 3 Jan 2020 17:11:42 -0600 Subject: [PATCH] Disable notification bundling; add default question --- .../notifications/AndroidNotificationTray.kt | 60 +++++++++---------- .../src/main/res/values/strings.xml | 1 + 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/notifications/AndroidNotificationTray.kt b/android/uhabits-android/src/main/java/org/isoron/uhabits/notifications/AndroidNotificationTray.kt index 3b45e14cc..d3cd51e0a 100644 --- a/android/uhabits-android/src/main/java/org/isoron/uhabits/notifications/AndroidNotificationTray.kt +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/notifications/AndroidNotificationTray.kt @@ -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() 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,41 +132,39 @@ 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)) + context.getString(R.string.snooze), + pendingIntents.snoozeNotification(habit)) wearableExtender.addAction(snoozeAction) builder.addAction(snoozeAction) } builder.extend(wearableExtender) - return builder.build() + 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) + context.resources.getString(R.string.reminder), + NotificationManager.IMPORTANCE_DEFAULT) notificationManager.createNotificationChannel(channel) } } diff --git a/android/uhabits-android/src/main/res/values/strings.xml b/android/uhabits-android/src/main/res/values/strings.xml index 2a724c7da..af088952a 100644 --- a/android/uhabits-android/src/main/res/values/strings.xml +++ b/android/uhabits-android/src/main/res/values/strings.xml @@ -243,5 +243,6 @@ Widget opacity Makes widgets more transparent or more opaque in your home screen. First day of the week + Have you completed this habit today? \ No newline at end of file