Add option to make upcoming lesson notification not persistent
MODIFY
app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt
app/src/main/java/io/github/wulkanowy/data/repositories/PreferencesRepository.kt
@@ -107,6 +107,14 @@ class PreferencesRepository @Inject constructor(
R.bool.pref_default_notification_upcoming_lessons_enable
)
+ val isUpcomingLessonsNotificationsPersistentKey =
+ context.getString(R.string.pref_key_notifications_upcoming_lessons_persistent)
+ val isUpcomingLessonsNotificationsPersistent: Boolean
+ get() = getBoolean(
+ isUpcomingLessonsNotificationsPersistentKey,
+ R.bool.pref_default_notification_upcoming_lessons_persistent
+ )
+
val isNotificationPiggybackEnabledKey =
context.getString(R.string.pref_key_notifications_piggyback)
val isNotificationPiggybackEnabled: Boolean

MODIFY
app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt
app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt
@@ -11,6 +11,7 @@ import androidx.core.app.NotificationManagerCompat
import dagger.hilt.android.AndroidEntryPoint
import io.github.wulkanowy.R
import io.github.wulkanowy.data.Status
+import io.github.wulkanowy.data.repositories.PreferencesRepository
import io.github.wulkanowy.data.repositories.StudentRepository
import io.github.wulkanowy.services.HiltBroadcastReceiver
import io.github.wulkanowy.services.sync.channels.UpcomingLessonsChannel.Companion.CHANNEL_ID
@@ -32,6 +33,9 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
@Inject
lateinit var studentRepository: StudentRepository
+ @Inject
+ lateinit var preferencesRepository: PreferencesRepository
+
companion object {
const val NOTIFICATION_TYPE_CURRENT = 1
const val NOTIFICATION_TYPE_UPCOMING = 2
@@ -68,6 +72,7 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
private fun prepareNotification(context: Context, intent: Intent) {
val type = intent.getIntExtra(LESSON_TYPE, 0)
val notificationId = intent.getIntExtra(NOTIFICATION_ID, MainView.Section.TIMETABLE.id)
+ val isPersistent = preferencesRepository.isUpcomingLessonsNotificationsPersistent
if (type == NOTIFICATION_TYPE_LAST_LESSON_CANCELLATION) {
return NotificationManagerCompat.from(context).cancel(notificationId)
@@ -87,20 +92,38 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
Timber.d("TimetableNotification receive: type: $type, subject: $subject, start: ${start.toLocalDateTime()}, student: $studentId")
- showNotification(context, notificationId, studentName,
+ showNotification(context, notificationId, isPersistent, studentName,
if (type == NOTIFICATION_TYPE_CURRENT) end else start, end - start,
- context.getString(if (type == NOTIFICATION_TYPE_CURRENT) R.string.timetable_now else R.string.timetable_next, "($room) $subject".removePrefix("()")),
- nextSubject?.let { context.getString(R.string.timetable_later, "($nextRoom) $nextSubject".removePrefix("()")) }
+ context.getString(
+ if (type == NOTIFICATION_TYPE_CURRENT) R.string.timetable_now else R.string.timetable_next,
+ "($room) $subject".removePrefix("()")
+ ),
+ nextSubject?.let {
+ context.getString(
+ R.string.timetable_later,
+ "($nextRoom) $nextSubject".removePrefix("()")
+ )
+ }
)
}
- private fun showNotification(context: Context, notificationId: Int, studentName: String?, countDown: Long, timeout: Long, title: String, next: String?) {
- NotificationManagerCompat.from(context).notify(notificationId, NotificationCompat.Builder(context, CHANNEL_ID)
+ private fun showNotification(
+ context: Context,
+ notificationId: Int,
+ isPersistent: Boolean,
+ studentName: String?,
+ countDown: Long,
+ timeout: Long,
+ title: String,
+ next: String?
+ ) {
+ NotificationManagerCompat.from(context)
+ .notify(notificationId, NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(title)
.setContentText(next)
.setAutoCancel(false)
- .setOngoing(true)
.setWhen(countDown)
+ .setOngoing(isPersistent)
.apply {
if (Build.VERSION.SDK_INT >= N) setUsesChronometer(true)
}
@@ -111,8 +134,14 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
it.setSummaryText(studentName)
it.addLine(next)
})
- .setContentIntent(PendingIntent.getActivity(context, MainView.Section.TIMETABLE.id,
- MainActivity.getStartIntent(context, MainView.Section.TIMETABLE, true), FLAG_UPDATE_CURRENT))
+ .setContentIntent(
+ PendingIntent.getActivity(
+ context,
+ MainView.Section.TIMETABLE.id,
+ MainActivity.getStartIntent(context, MainView.Section.TIMETABLE, true),
+ FLAG_UPDATE_CURRENT
+ )
+ )
.build()
)
}

MODIFY
app/src/main/java/io/github/wulkanowy/ui/modules/settings/notifications/NotificationsPresenter.kt
app/src/main/java/io/github/wulkanowy/ui/modules/settings/notifications/NotificationsPresenter.kt
@@ -42,7 +42,7 @@ class NotificationsPresenter @Inject constructor(
preferencesRepository.apply {
when (key) {
- isUpcomingLessonsNotificationsEnableKey -> {
+ isUpcomingLessonsNotificationsEnableKey, isUpcomingLessonsNotificationsPersistentKey -> {
if (!isUpcomingLessonsNotificationsEnable) {
timetableNotificationHelper.cancelNotification()
}

MODIFY
app/src/main/res/values/preferences_defaults.xml
app/src/main/res/values/preferences_defaults.xml
<bool name="pref_default_services_wifi_only">false</bool>
<bool name="pref_default_notifications_enable">true</bool>
<bool name="pref_default_notification_upcoming_lessons_enable">false</bool>
+ <bool name="pref_default_notification_upcoming_lessons_persistent">true</bool>
<bool name="pref_default_notification_debug">false</bool>
<string name="pref_default_grade_modifier_plus">0.33</string>
<string name="pref_default_grade_modifier_minus">0.33</string>

MODIFY
app/src/main/res/values/preferences_keys.xml
app/src/main/res/values/preferences_keys.xml
<string name="pref_key_notifications_system_settings">notifications_system_settings</string>
<string name="pref_key_notifications_enable">notifications_enable</string>
<string name="pref_key_notifications_upcoming_lessons_enable">notifications_upcoming_lessons_enable</string>
+ <string name="pref_key_notifications_upcoming_lessons_persistent">notifications_upcoming_lessons_persistent</string>
<string name="pref_key_notification_debug">notification_debug</string>
<string name="pref_key_grade_modifier_plus">grade_modifier_plus</string>
<string name="pref_key_grade_modifier_minus">grade_modifier_minus</string>

MODIFY
app/src/main/res/values/strings.xml
app/src/main/res/values/strings.xml
<string name="pref_notify_header">Notifications</string>
<string name="pref_notify_switch">Show notifications</string>
<string name="pref_notify_upcoming_lessons_switch">Show upcoming lesson notifications</string>
+ <string name="pref_notify_upcoming_lessons_persistent_switch">Make upcoming lesson notification persistent</string>
+ <string name="pref_notify_upcoming_lessons_persistent_summary">Turn off when notification is not showing in your watch/band</string>
<string name="pref_notify_open_system_settings">Open system notification settings</string>
<string name="pref_notify_fix_sync_issues">Fix synchronization &amp; notifications issues</string>
<string name="pref_notify_fix_sync_issues_message">Your device may have data synchronization issues and with notifications.\n\nTo fix them, you need to add Wulkanowy to the autostart and turn off battery optimization/saving in the phone settings.</string>

MODIFY
app/src/main/res/xml/scheme_preferences_notifications.xml
app/src/main/res/xml/scheme_preferences_notifications.xml
app:key="@string/pref_key_notifications_upcoming_lessons_enable"
app:singleLineTitle="false"
app:title="@string/pref_notify_upcoming_lessons_switch" />
+ <SwitchPreferenceCompat
+ app:defaultValue="@bool/pref_default_notification_upcoming_lessons_persistent"
+ app:iconSpaceReserved="false"
+ app:key="@string/pref_key_notifications_upcoming_lessons_persistent"
+ app:dependency="@string/pref_key_notifications_upcoming_lessons_enable"
+ app:singleLineTitle="false"
+ app:summary="@string/pref_notify_upcoming_lessons_persistent_summary"
+ app:title="@string/pref_notify_upcoming_lessons_persistent_switch" />
<SwitchPreferenceCompat
app:defaultValue="@bool/pref_default_notification_piggyback"
app:iconSpaceReserved="false"

Add option to make upcoming lesson notification not persistent (#1537)