Skip to content

Commit

Permalink
AlertService: Switch to FOREGROUND_SERVICE_SYSTEM_EXEMPTED on Android…
Browse files Browse the repository at this point in the history
… 14+

* New FGS type on Android 14+, needs new permission
* We should migrate to work manager in future which is now recommended to handle background jobs
* Ref: https://developer.android.com/about/versions/14/changes/fgs-types-required#system-exempted

Signed-off-by: Aayush Gupta <[email protected]>
  • Loading branch information
theimpulson authored and jspricke committed Oct 17, 2023
1 parent 1e2021e commit 3036cd6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
Expand Down Expand Up @@ -267,7 +268,7 @@

<service android:name="com.android.calendar.alerts.AlertService"
android:exported="false"
android:foregroundServiceType="dataSync" />
android:foregroundServiceType="dataSync|systemExempted" />

<service android:name="com.android.calendar.alerts.DismissAlarmsService" />

Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/com/android/calendar/AllInOneActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;

import android.Manifest;
import android.app.AlarmManager;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
Expand Down Expand Up @@ -597,9 +596,8 @@ protected void onResume() {
mController.registerFirstEventHandler(HANDLER_KEY, this);
mOnSaveInstanceStateCalled = false;

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
if (!alarmManager.canScheduleExactAlarms()) {
if (!Utils.canScheduleAlarms(this)) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM);
startActivity(intent);
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/android/calendar/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.Manifest;
import android.accounts.Account;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.SearchManager;
import android.content.BroadcastReceiver;
Expand Down Expand Up @@ -54,6 +55,7 @@
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.ColorUtils;
Expand Down Expand Up @@ -205,6 +207,19 @@ public class Utils {
private static boolean mAllowWeekForDetailView = false;
private static String sVersion = null;

@RequiresApi(api = Build.VERSION_CODES.S)
public static boolean canScheduleAlarms(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
return alarmManager.canScheduleExactAlarms();
}

/**
* Returns whether the SDK is the UpsideDownCake release or later.
*/
public static boolean isUpsideDownCakeOrLater() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE;
}

/**
* Returns whether the SDK is the Q release or later.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public static void beginStartingService(Context context, Intent intent) {
if (Utils.isMOrLater()) {
if (pm.isIgnoringBatteryOptimizations(context.getPackageName())) {
if (Utils.isOreoOrLater()) {
if (Utils.isUpsideDownCakeOrLater() && !Utils.canScheduleAlarms(context)) {
return;
}
context.startForegroundService(intent);
} else {
context.startService(intent);
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/com/android/calendar/alerts/AlertService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.android.calendar.alerts;

import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC;
import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED;

import android.Manifest;
import android.annotation.TargetApi;
Expand Down Expand Up @@ -934,12 +935,13 @@ public int onStartCommand(Intent intent, int flags, int startId) {
.setShowWhen(false)
.build();
if (Utils.isQOrLater()) {
ServiceCompat.startForeground(
this,
1337,
notification,
FOREGROUND_SERVICE_TYPE_DATA_SYNC
);
int serviceType;
if (Utils.isUpsideDownCakeOrLater()) {
serviceType = FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED;
} else {
serviceType = FOREGROUND_SERVICE_TYPE_DATA_SYNC;
}
ServiceCompat.startForeground(this, 1337, notification, serviceType);
} else {
startForeground(1337, notification);
}
Expand Down

0 comments on commit 3036cd6

Please sign in to comment.