From 4f83eb572b8a635e02aa42fd6588e3c3612f5bb3 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 16 Jun 2021 08:34:23 +0200 Subject: [PATCH] Exact alarm improvements --- app/src/fdroid/AndroidManifest.xml | 4 ++++ app/src/github/AndroidManifest.xml | 4 ++++ app/src/main/AndroidManifest.xml | 4 ++++ .../faircode/email/AlarmManagerCompatEx.java | 17 +++++++++++----- .../eu/faircode/email/ReceiverAutoStart.java | 20 ++++++++++++++----- app/src/play/AndroidManifest.xml | 4 ++++ 6 files changed, 43 insertions(+), 10 deletions(-) diff --git a/app/src/fdroid/AndroidManifest.xml b/app/src/fdroid/AndroidManifest.xml index e77098cfd8..93a0a5d06c 100644 --- a/app/src/fdroid/AndroidManifest.xml +++ b/app/src/fdroid/AndroidManifest.xml @@ -459,6 +459,10 @@ + + + + diff --git a/app/src/github/AndroidManifest.xml b/app/src/github/AndroidManifest.xml index e77098cfd8..93a0a5d06c 100644 --- a/app/src/github/AndroidManifest.xml +++ b/app/src/github/AndroidManifest.xml @@ -459,6 +459,10 @@ + + + + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e902deecac..2c35544959 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -459,6 +459,10 @@ + + + + diff --git a/app/src/main/java/eu/faircode/email/AlarmManagerCompatEx.java b/app/src/main/java/eu/faircode/email/AlarmManagerCompatEx.java index 4ce4d656f6..a5981eca19 100644 --- a/app/src/main/java/eu/faircode/email/AlarmManagerCompatEx.java +++ b/app/src/main/java/eu/faircode/email/AlarmManagerCompatEx.java @@ -36,18 +36,25 @@ public class AlarmManagerCompatEx { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean exact_alarms = prefs.getBoolean("exact_alarms", true); + // https://developer.android.com/about/versions/12/behavior-changes-12#exact-alarm-permission if (exact_alarms && canScheduleExactAlarms(context)) - AlarmManagerCompat.setExactAndAllowWhileIdle(am, type, trigger, pi); + try { + AlarmManagerCompat.setExactAndAllowWhileIdle(am, type, trigger, pi); + } catch (SecurityException ex) { + Log.w(ex); + AlarmManagerCompat.setAndAllowWhileIdle(am, type, trigger, pi); + } else AlarmManagerCompat.setAndAllowWhileIdle(am, type, trigger, pi); } static boolean canScheduleExactAlarms(Context context) { - if (Build.VERSION.SDK_INT < 31 /* S */) + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) return true; - else + else { + //AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + //return am.canScheduleExactAlarms(); return true; - //AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); - //return am.canScheduleExactAlarms(); + } } } diff --git a/app/src/main/java/eu/faircode/email/ReceiverAutoStart.java b/app/src/main/java/eu/faircode/email/ReceiverAutoStart.java index 15f4d6cbde..60a0f3bffb 100644 --- a/app/src/main/java/eu/faircode/email/ReceiverAutoStart.java +++ b/app/src/main/java/eu/faircode/email/ReceiverAutoStart.java @@ -24,17 +24,27 @@ import android.content.Context; import android.content.Intent; public class ReceiverAutoStart extends BroadcastReceiver { + // https://developer.android.com/reference/android/app/AlarmManager#ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED + private final String ACTION_EXACT = + "android.app.action.SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED"; + @Override public void onReceive(final Context context, Intent intent) { - if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) || - Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) { + String action = intent.getAction(); + if (ACTION_EXACT.equals(action) || + Intent.ACTION_BOOT_COMPLETED.equals(action) || + Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)) { Log.i("Received " + intent); - if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) + if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)) ApplicationEx.upgrade(context); - ServiceSynchronize.boot(context); - ServiceSend.boot(context); + if (ACTION_EXACT.equals(action)) + ServiceSynchronize.reload(context, null, false, action); + else { + ServiceSynchronize.boot(context); + ServiceSend.boot(context); + } } } } diff --git a/app/src/play/AndroidManifest.xml b/app/src/play/AndroidManifest.xml index 010eda5ab8..27c513884c 100644 --- a/app/src/play/AndroidManifest.xml +++ b/app/src/play/AndroidManifest.xml @@ -460,6 +460,10 @@ + + + +