Skip to content

Commit e4e2225

Browse files
fix: Add missing SCHEDULE_EXACT_ALARM permission
1 parent 597e43a commit e4e2225

5 files changed

Lines changed: 48 additions & 10 deletions

File tree

‎build.gradle‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ if (!ignoreGit) {
105105
}
106106

107107
// RE specific version name and code
108-
def ourGmsVersionCode = 255070212
109-
def ourGmsVersionName = "7.2.1-dev.2"
108+
def ourGmsVersionCode = 255070213
109+
def ourGmsVersionName = "7.2.1-dev.3"
110110
def ourVendingVersionCode = ourGmsVersionCode
111111
def ourVendingVersionName = ourGmsVersionName
112112

‎play-services-core/src/main/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
143143
tools:ignore="QueryAllPackagesPermission" />
144144
<uses-permission android:name="android.permission.USE_EXACT_ALARM" tools:ignore="ExactAlarm" />
145+
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" tools:ignore="ExactAlarm" />
145146
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
146147
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
147148
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

‎play-services-core/src/main/java/org/microg/gms/gcm/McsService.java‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,17 @@ public static void scheduleReconnect(Context context) {
270270
logd(context, "Scheduling reconnect in " + delay / 1000 + " seconds...");
271271
PendingIntent pi = PendingIntentCompat.getBroadcast(context, 1, new Intent(ACTION_RECONNECT, null, context, TriggerReceiver.class), 0, false);
272272
if (SDK_INT >= 23) {
273-
alarmManager.setExactAndAllowWhileIdle(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delay, pi);
273+
// RE changes start
274+
try {
275+
if (SDK_INT < 31 || alarmManager.canScheduleExactAlarms()) {
276+
alarmManager.setExactAndAllowWhileIdle(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delay, pi);
277+
return;
278+
}
279+
} catch (SecurityException e) {
280+
Log.w(TAG, "Failed to schedule exact alarm for reconnect", e);
281+
}
282+
alarmManager.setAndAllowWhileIdle(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delay, pi);
283+
// RE changes end
274284
} else {
275285
alarmManager.set(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delay, pi);
276286
}
@@ -286,7 +296,15 @@ public void scheduleHeartbeat(Context context) {
286296
logd(context, "Scheduling heartbeat in " + heartbeatMs / 1000 + " seconds...");
287297
if (SDK_INT >= 23) {
288298
// This is supposed to work even when running in idle and without battery optimization disabled
289-
alarmManager.setExactAndAllowWhileIdle(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + heartbeatMs, heartbeatIntent);
299+
try {
300+
if (SDK_INT < 31 || alarmManager.canScheduleExactAlarms()) {
301+
alarmManager.setExactAndAllowWhileIdle(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + heartbeatMs, heartbeatIntent);
302+
return;
303+
}
304+
} catch (SecurityException e) {
305+
Log.w(TAG, "Failed to schedule exact alarm for heartbeat", e);
306+
}
307+
alarmManager.setAndAllowWhileIdle(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + heartbeatMs, heartbeatIntent);
290308
} else if (SDK_INT >= 19) {
291309
// With KitKat, the alarms become inexact by default, but with the newly available setWindow we can get inexact alarms with guarantees.
292310
// Schedule the alarm to fire within the interval [heartbeatMs/3*4, heartbeatMs]

‎play-services-nearby/core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/AdvertiserService.kt‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,21 @@ class AdvertiserService : LifecycleService() {
199199
private fun scheduleRestartAdvertising(nextSend: Long) {
200200
val intent = Intent(this, AdvertiserService::class.java).apply { action = ACTION_RESTART_ADVERTISING }
201201
val pendingIntent = PendingIntentCompat.getService(this, ACTION_RESTART_ADVERTISING.hashCode(), intent, FLAG_ONE_SHOT or FLAG_UPDATE_CURRENT, false)!!
202-
when {
203-
SDK_INT >= 23 ->
204-
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + nextSend, pendingIntent)
205-
else ->
206-
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + nextSend, pendingIntent)
202+
// RE changes start
203+
if (SDK_INT >= 23) {
204+
try {
205+
if (SDK_INT < 31 || alarmManager.canScheduleExactAlarms()) {
206+
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + nextSend, pendingIntent)
207+
return
208+
}
209+
} catch (e: SecurityException) {
210+
Log.w(TAG, "Failed to schedule exact alarm", e)
211+
}
212+
alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + nextSend, pendingIntent)
213+
} else {
214+
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + nextSend, pendingIntent)
207215
}
216+
// RE changes end
208217
}
209218

210219
@Synchronized

‎play-services-nearby/core/src/main/kotlin/org/microg/gms/nearby/exposurenotification/ScannerService.kt‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,17 @@ class ScannerService : LifecycleService() {
163163
val pendingIntent = PendingIntentCompat.getService(this, ScannerService::class.java.hashCode(), intent, FLAG_ONE_SHOT or FLAG_UPDATE_CURRENT, false)!!
164164
if (Build.VERSION.SDK_INT >= 23) {
165165
// Note: there is no setWindowAndAllowWhileIdle()
166-
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + nextScan, pendingIntent)
166+
// RE changes start
167+
try {
168+
if (Build.VERSION.SDK_INT < 31 || alarmManager.canScheduleExactAlarms()) {
169+
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + nextScan, pendingIntent)
170+
return
171+
}
172+
} catch (e: SecurityException) {
173+
Log.w(TAG, "Failed to schedule exact alarm", e)
174+
}
175+
alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + nextScan, pendingIntent)
176+
// RE changes end
167177
} else {
168178
alarmManager.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + nextScan - SCANNING_TIME_MS / 2, SCANNING_TIME_MS, pendingIntent)
169179
}

0 commit comments

Comments
 (0)