Music app rechecks for permission if it didn't have notifications access

This commit is contained in:
James Babcock committed 2026-09-11 22:30:26 -07:00
1 parent fb89517a5a
commit d41096f336
2 files changed
+64 -22

No files matched your search

@@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.MediaDescription; import android.media.MediaDescription;
@@ -27,6 +28,8 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class FaceclawMediaController { public class FaceclawMediaController {
private static final String ENABLED_NOTIFICATION_LISTENERS = "enabled_notification_listeners";
private final Context appContext; private final Context appContext;
private final Object lock = new Object(); private final Object lock = new Object();
private final Handler mainHandler = new Handler(Looper.getMainLooper()); private final Handler mainHandler = new Handler(Looper.getMainLooper());
@@ -70,9 +73,27 @@ public class FaceclawMediaController {
} }
}; };
/**
* Fires when the user grants or revokes notification-listener access in
* system settings. Session access depends on that grant, so the sessions
* listener is (re)registered and the state re-emitted whenever it changes;
* otherwise a grant made after start() would never be noticed.
*/
private final ContentObserver notificationAccessObserver = new ContentObserver(mainHandler) {
@Override
public void onChange(boolean selfChange) {
synchronized (lock) {
if (!started) return;
syncSessionsListenerLocked();
refreshActiveControllerLocked(null);
}
}
};
private volatile FaceclawMediaControllerListener listener; private volatile FaceclawMediaControllerListener listener;
private MediaController activeController; private MediaController activeController;
private boolean started; private boolean started;
private boolean sessionsListenerRegistered;
private Set<String> ignoredPackages = new HashSet<>(); private Set<String> ignoredPackages = new HashSet<>();
public FaceclawMediaController(Context context) { public FaceclawMediaController(Context context) {
@@ -96,26 +117,48 @@ public class FaceclawMediaController {
return; return;
} }
started = true; started = true;
if (!isNotificationAccessEnabled()) { try {
emitStateLocked(); appContext.getContentResolver().registerContentObserver(
return; Settings.Secure.getUriFor(ENABLED_NOTIFICATION_LISTENERS),
} false,
if (sessionManager != null) { notificationAccessObserver
try { );
sessionManager.addOnActiveSessionsChangedListener( } catch (Exception e) {
sessionsChangedListener, Log.w("FaceclawMedia", "notification access observer registration failed", e);
listenerComponent,
mainHandler
);
} catch (SecurityException ignored) {
emitStateLocked();
return;
}
} }
syncSessionsListenerLocked();
refreshActiveControllerLocked(null); refreshActiveControllerLocked(null);
} }
} }
/**
* Keep the active-sessions listener registered exactly while notification
* access is granted. Registration throws SecurityException without the
* grant, so this is retried from the settings observer rather than only
* attempted once at start().
*/
private void syncSessionsListenerLocked() {
if (sessionManager == null) return;
boolean accessEnabled = started && isNotificationAccessEnabled();
if (accessEnabled && !sessionsListenerRegistered) {
try {
sessionManager.addOnActiveSessionsChangedListener(
sessionsChangedListener,
listenerComponent,
mainHandler
);
sessionsListenerRegistered = true;
} catch (SecurityException ignored) {
}
} else if (!accessEnabled && sessionsListenerRegistered) {
try {
sessionManager.removeOnActiveSessionsChangedListener(sessionsChangedListener);
} catch (SecurityException ignored) {
}
sessionsListenerRegistered = false;
}
}
/** Apply the complete preference set and immediately choose an allowed session. */ /** Apply the complete preference set and immediately choose an allowed session. */
public void setIgnoredPackagesJson(String packagesJson) { public void setIgnoredPackagesJson(String packagesJson) {
Set<String> packages = new HashSet<>(); Set<String> packages = new HashSet<>();
@@ -139,12 +182,11 @@ public class FaceclawMediaController {
return; return;
} }
started = false; started = false;
if (sessionManager != null) { try {
try { appContext.getContentResolver().unregisterContentObserver(notificationAccessObserver);
sessionManager.removeOnActiveSessionsChangedListener(sessionsChangedListener); } catch (Exception ignored) {
} catch (SecurityException ignored) {
}
} }
syncSessionsListenerLocked();
setActiveControllerLocked(null); setActiveControllerLocked(null);
emitStateLocked(); emitStateLocked();
} }
@@ -377,7 +419,7 @@ public class FaceclawMediaController {
private boolean isNotificationAccessEnabled() { private boolean isNotificationAccessEnabled() {
String enabledListeners = Settings.Secure.getString( String enabledListeners = Settings.Secure.getString(
appContext.getContentResolver(), appContext.getContentResolver(),
"enabled_notification_listeners" ENABLED_NOTIFICATION_LISTENERS
); );
if (enabledListeners == null || enabledListeners.isEmpty()) { if (enabledListeners == null || enabledListeners.isEmpty()) {
return false; return false;
+1 -1
View File
@@ -68,7 +68,7 @@ class MusicAppLayer implements Layer {
if (!media.accessEnabled) { if (!media.accessEnabled) {
const lines = wrapText( const lines = wrapText(
font, font,
"Notification access is required before Android exposes media sessions. Click to open settings.", "Notification access is required to control Android media sessions. Tap to open settings.",
width - 48, width - 48,
); );
for (let index = 0; index < lines.length; index++) { for (let index = 0; index < lines.length; index++) {