diff --git a/server/src/main/java/com/genymobile/scrcpy/CleanUp.java b/server/src/main/java/com/genymobile/scrcpy/CleanUp.java index d84ce5e9..89bc9095 100644 --- a/server/src/main/java/com/genymobile/scrcpy/CleanUp.java +++ b/server/src/main/java/com/genymobile/scrcpy/CleanUp.java @@ -7,7 +7,6 @@ import com.genymobile.scrcpy.util.SettingsException; import com.genymobile.scrcpy.wrappers.ServiceManager; import android.app.NotificationManager; -import android.content.Context; import android.os.BatteryManager; import android.os.Looper; import android.system.ErrnoException; @@ -82,14 +81,13 @@ public final class CleanUp { } } - int previousDoNotDisturbMode = NotificationManager.INTERRUPTION_FILTER_NONE; + int previousDoNotDisturbMode = -1; if (options.getDoNotDisturb()) { try { - FakeContext context = FakeContext.get(); - NotificationManager notificationManager = (NotificationManager) context - .getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager notificationManager = ServiceManager.getNotificationManager(); previousDoNotDisturbMode = notificationManager.getCurrentInterruptionFilter(); notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALARMS); + } catch (Exception e) { Ln.e("Could not set dnd mode", e); } @@ -284,6 +282,11 @@ public final class CleanUp { } } + if(previousDoNotDisturbMode != -1) { + Ln.i("Restoring DoNotDisturbMode"); + ServiceManager.getNotificationManager().setInterruptionFilter(previousDoNotDisturbMode); + } + System.exit(0); } } diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/ServiceManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/ServiceManager.java index b1123b55..246b321b 100644 --- a/server/src/main/java/com/genymobile/scrcpy/wrappers/ServiceManager.java +++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/ServiceManager.java @@ -2,6 +2,7 @@ package com.genymobile.scrcpy.wrappers; import com.genymobile.scrcpy.FakeContext; +import android.app.NotificationManager; import android.annotation.SuppressLint; import android.content.Context; import android.hardware.camera2.CameraManager; @@ -32,6 +33,7 @@ public final class ServiceManager { private static ClipboardManager clipboardManager; private static ActivityManager activityManager; private static CameraManager cameraManager; + private static NotificationManager notificationManager; private ServiceManager() { /* not instantiable */ @@ -109,4 +111,17 @@ public final class ServiceManager { } return cameraManager; } + + public static NotificationManager getNotificationManager() { + if (notificationManager == null) { + try { + // a similar method like CameraManager is only available for Android >= 16 + // https://android.googlesource.com/platform/frameworks/base/+blame/refs/tags/android-16.0.0_r4/core/java/android/app/NotificationManager.java#718 + notificationManager = (NotificationManager) FakeContext.get().getSystemService(Context.NOTIFICATION_SERVICE); + } catch (Exception e) { + throw new AssertionError(e); + } + } + return notificationManager; + } }