From 2a8e30c26122be7dd1bf956d98da645ad58f74df Mon Sep 17 00:00:00 2001 From: Pratistha Sinha Date: Sun, 15 Sep 2024 22:28:12 +0530 Subject: [PATCH] fix-990: runtime permission for alarm services in android 12+ devices --- omniNotes/src/main/AndroidManifest.xml | 1 + .../omninotes/utils/ReminderHelper.java | 22 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/omniNotes/src/main/AndroidManifest.xml b/omniNotes/src/main/AndroidManifest.xml index f360e00fb..e4894199d 100644 --- a/omniNotes/src/main/AndroidManifest.xml +++ b/omniNotes/src/main/AndroidManifest.xml @@ -36,6 +36,7 @@ + diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/ReminderHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/ReminderHelper.java index 02cea5784..720f9511b 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/ReminderHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/ReminderHelper.java @@ -27,7 +27,9 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.os.Build; import android.os.Handler; +import android.provider.Settings; import android.text.TextUtils; import android.widget.Toast; import it.feio.android.omninotes.OmniNotes; @@ -54,15 +56,31 @@ public static void addReminder(Context context, Note note) { public static void addReminder(Context context, Note note, long reminder) { if (DateUtils.isFuture(reminder)) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { // Android 12 (API 31) and above + AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + + if (!alarmManager.canScheduleExactAlarms()) { + Intent intent = new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM); + context.startActivity(intent); + return; + } + } + Intent intent = new Intent(context, AlarmReceiver.class); intent.putExtra(INTENT_NOTE, ParcelableUtil.marshall(note)); - PendingIntent sender = PendingIntent.getBroadcast(context, getRequestCode(note), intent, - immutablePendingIntentFlag(FLAG_CANCEL_CURRENT)); + PendingIntent sender = PendingIntent.getBroadcast( + context, + getRequestCode(note), + intent, + immutablePendingIntentFlag(PendingIntent.FLAG_CANCEL_CURRENT) + ); + AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); am.setExact(AlarmManager.RTC_WAKEUP, reminder, sender); } } + /** * Checks if exists any reminder for given note */