From 09ad39151b652a1c6ad7cf9bb8f6151952c057ee Mon Sep 17 00:00:00 2001 From: Austin O'Neil Date: Sun, 28 Apr 2024 14:09:12 -0700 Subject: [PATCH 1/4] Update events and tasks to allow for creation when notification permission is not granted. --- .../calendar/activities/EventActivity.kt | 12 +++++++++--- .../calendar/activities/TaskActivity.kt | 12 +++++++++--- .../org/fossify/calendar/helpers/Config.kt | 18 +++++++++++++++--- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt b/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt index 61c9e5c18..0119a0c6c 100644 --- a/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt +++ b/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt @@ -1,9 +1,11 @@ package org.fossify.calendar.activities +import android.Manifest import android.app.Activity import android.app.DatePickerDialog import android.app.TimePickerDialog import android.content.Intent +import android.content.pm.PackageManager.PERMISSION_GRANTED import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable import android.graphics.drawable.LayerDrawable @@ -263,10 +265,14 @@ class EventActivity : SimpleActivity() { } } else { mEvent = Event(null) + val hasPermission = checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PERMISSION_GRANTED config.apply { - mReminder1Minutes = if (usePreviousEventReminders && lastEventReminderMinutes1 >= -1) lastEventReminderMinutes1 else defaultReminder1 - mReminder2Minutes = if (usePreviousEventReminders && lastEventReminderMinutes2 >= -1) lastEventReminderMinutes2 else defaultReminder2 - mReminder3Minutes = if (usePreviousEventReminders && lastEventReminderMinutes3 >= -1) lastEventReminderMinutes3 else defaultReminder3 + mReminder1Minutes = + if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes1 >= -1) lastEventReminderMinutes1 else defaultReminder1 + mReminder2Minutes = + if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes2 >= -1) lastEventReminderMinutes2 else defaultReminder2 + mReminder3Minutes = + if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes3 >= -1) lastEventReminderMinutes3 else defaultReminder3 } if (savedInstanceState == null) { diff --git a/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt b/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt index 5a8598a91..43bcb300c 100644 --- a/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt +++ b/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt @@ -1,8 +1,10 @@ package org.fossify.calendar.activities +import android.Manifest import android.app.DatePickerDialog import android.app.TimePickerDialog import android.content.Intent +import android.content.pm.PackageManager import android.graphics.Color import android.os.Bundle import android.view.WindowManager @@ -239,10 +241,14 @@ class TaskActivity : SimpleActivity() { } } else { mTask = Event(null) + val hasPermission = checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED config.apply { - mReminder1Minutes = if (usePreviousEventReminders && lastEventReminderMinutes1 >= -1) lastEventReminderMinutes1 else defaultReminder1 - mReminder2Minutes = if (usePreviousEventReminders && lastEventReminderMinutes2 >= -1) lastEventReminderMinutes2 else defaultReminder2 - mReminder3Minutes = if (usePreviousEventReminders && lastEventReminderMinutes3 >= -1) lastEventReminderMinutes3 else defaultReminder3 + mReminder1Minutes = + if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes1 >= -1) lastEventReminderMinutes1 else defaultReminder1 + mReminder2Minutes = + if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes2 >= -1) lastEventReminderMinutes2 else defaultReminder2 + mReminder3Minutes = + if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes3 >= -1) lastEventReminderMinutes3 else defaultReminder3 } if (savedInstanceState == null) { diff --git a/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt b/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt index 86c75ba10..8ad9bac16 100644 --- a/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt +++ b/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt @@ -1,8 +1,11 @@ package org.fossify.calendar.helpers +import android.Manifest import android.content.Context +import android.content.pm.PackageManager import android.media.AudioManager import android.media.RingtoneManager +import androidx.core.content.ContextCompat import org.fossify.calendar.R import org.fossify.calendar.extensions.config import org.fossify.calendar.extensions.scheduleCalDAVSync @@ -180,16 +183,25 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getBoolean(USE_PREVIOUS_EVENT_REMINDERS, true) set(usePreviousEventReminders) = prefs.edit().putBoolean(USE_PREVIOUS_EVENT_REMINDERS, usePreviousEventReminders).apply() + fun getDefault(reminder: String): Int { + val hasPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED + if (hasPermission) { + return prefs.getInt(reminder, 10) + } else { + return REMINDER_OFF + } + } + var defaultReminder1: Int - get() = prefs.getInt(DEFAULT_REMINDER_1, 10) + get() = getDefault(DEFAULT_REMINDER_1) set(defaultReminder1) = prefs.edit().putInt(DEFAULT_REMINDER_1, defaultReminder1).apply() var defaultReminder2: Int - get() = prefs.getInt(DEFAULT_REMINDER_2, REMINDER_OFF) + get() = getDefault(DEFAULT_REMINDER_2) set(defaultReminder2) = prefs.edit().putInt(DEFAULT_REMINDER_2, defaultReminder2).apply() var defaultReminder3: Int - get() = prefs.getInt(DEFAULT_REMINDER_3, REMINDER_OFF) + get() = getDefault(DEFAULT_REMINDER_3) set(defaultReminder3) = prefs.edit().putInt(DEFAULT_REMINDER_3, defaultReminder3).apply() var pullToRefresh: Boolean From f43dbe79646db6fbfce76d98e2e3b85c54b8cd2c Mon Sep 17 00:00:00 2001 From: Austin O'Neil Date: Sun, 28 Apr 2024 14:33:19 -0700 Subject: [PATCH 2/4] Refactor: Extracted reused logic into config --- .../org/fossify/calendar/activities/EventActivity.kt | 11 ++++------- .../org/fossify/calendar/activities/TaskActivity.kt | 11 ++++------- .../kotlin/org/fossify/calendar/helpers/Config.kt | 9 +++++++++ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt b/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt index 0119a0c6c..ea0fe7470 100644 --- a/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt +++ b/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt @@ -265,14 +265,11 @@ class EventActivity : SimpleActivity() { } } else { mEvent = Event(null) - val hasPermission = checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PERMISSION_GRANTED config.apply { - mReminder1Minutes = - if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes1 >= -1) lastEventReminderMinutes1 else defaultReminder1 - mReminder2Minutes = - if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes2 >= -1) lastEventReminderMinutes2 else defaultReminder2 - mReminder3Minutes = - if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes3 >= -1) lastEventReminderMinutes3 else defaultReminder3 + val reminders = defaultReminders + mReminder1Minutes = reminders.first + mReminder2Minutes = reminders.second + mReminder3Minutes = reminders.third } if (savedInstanceState == null) { diff --git a/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt b/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt index 43bcb300c..0af3d3b6f 100644 --- a/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt +++ b/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt @@ -241,14 +241,11 @@ class TaskActivity : SimpleActivity() { } } else { mTask = Event(null) - val hasPermission = checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED config.apply { - mReminder1Minutes = - if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes1 >= -1) lastEventReminderMinutes1 else defaultReminder1 - mReminder2Minutes = - if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes2 >= -1) lastEventReminderMinutes2 else defaultReminder2 - mReminder3Minutes = - if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes3 >= -1) lastEventReminderMinutes3 else defaultReminder3 + val reminders = defaultReminders + mReminder1Minutes = reminders.first + mReminder2Minutes = reminders.second + mReminder3Minutes = reminders.third } if (savedInstanceState == null) { diff --git a/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt b/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt index 8ad9bac16..ef843b160 100644 --- a/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt +++ b/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt @@ -192,6 +192,15 @@ class Config(context: Context) : BaseConfig(context) { } } + val defaultReminders: Triple + get() { + val hasPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED + val defaultRem1 = if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes1 >= -1) lastEventReminderMinutes1 else defaultReminder1 + val defaultRem2 = if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes1 >= -1) lastEventReminderMinutes2 else defaultReminder2 + val defaultRem3 = if (hasPermission && usePreviousEventReminders && lastEventReminderMinutes1 >= -1) lastEventReminderMinutes3 else defaultReminder3 + return Triple(defaultRem1, defaultRem2, defaultRem3) + } + var defaultReminder1: Int get() = getDefault(DEFAULT_REMINDER_1) set(defaultReminder1) = prefs.edit().putInt(DEFAULT_REMINDER_1, defaultReminder1).apply() From 2f2817464f199bff9dbca151ba2ffa29a6c7d2ce Mon Sep 17 00:00:00 2001 From: Austin O'Neil Date: Sun, 28 Apr 2024 15:04:35 -0700 Subject: [PATCH 3/4] add default param to extracted fun --- .../main/kotlin/org/fossify/calendar/helpers/Config.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt b/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt index ef843b160..539228efc 100644 --- a/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt +++ b/app/src/main/kotlin/org/fossify/calendar/helpers/Config.kt @@ -183,10 +183,10 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getBoolean(USE_PREVIOUS_EVENT_REMINDERS, true) set(usePreviousEventReminders) = prefs.edit().putBoolean(USE_PREVIOUS_EVENT_REMINDERS, usePreviousEventReminders).apply() - fun getDefault(reminder: String): Int { + fun getDefault(reminder: String, defValue: Int): Int { val hasPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED if (hasPermission) { - return prefs.getInt(reminder, 10) + return prefs.getInt(reminder, defValue) } else { return REMINDER_OFF } @@ -202,15 +202,15 @@ class Config(context: Context) : BaseConfig(context) { } var defaultReminder1: Int - get() = getDefault(DEFAULT_REMINDER_1) + get() = getDefault(DEFAULT_REMINDER_1, 10) set(defaultReminder1) = prefs.edit().putInt(DEFAULT_REMINDER_1, defaultReminder1).apply() var defaultReminder2: Int - get() = getDefault(DEFAULT_REMINDER_2) + get() = getDefault(DEFAULT_REMINDER_2, REMINDER_OFF) set(defaultReminder2) = prefs.edit().putInt(DEFAULT_REMINDER_2, defaultReminder2).apply() var defaultReminder3: Int - get() = getDefault(DEFAULT_REMINDER_3) + get() = getDefault(DEFAULT_REMINDER_3, REMINDER_OFF) set(defaultReminder3) = prefs.edit().putInt(DEFAULT_REMINDER_3, defaultReminder3).apply() var pullToRefresh: Boolean From 3dabd46bf85ff587323c82cfd8b4979e47bedcff Mon Sep 17 00:00:00 2001 From: Austin O'Neil Date: Mon, 29 Apr 2024 13:06:52 -0700 Subject: [PATCH 4/4] Request notification permission on first launch. --- .../main/kotlin/org/fossify/calendar/activities/MainActivity.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/kotlin/org/fossify/calendar/activities/MainActivity.kt b/app/src/main/kotlin/org/fossify/calendar/activities/MainActivity.kt index bfd5b87e6..01680d5fa 100644 --- a/app/src/main/kotlin/org/fossify/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/org/fossify/calendar/activities/MainActivity.kt @@ -145,6 +145,8 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { addImportIdsToTasks { refreshViewPager() } + + handlePermission(PERMISSION_POST_NOTIFICATIONS) {} } override fun onResume() {