mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
Add source-filtering to Music app, to exclude "media sessions" from apps that aren't really media players
This commit is contained in:
1 parent
813668f850
commit
78c56ffc4e
7 files changed
+237
-3
No files matched your search
@@ -23,6 +23,8 @@ import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class FaceclawMediaController {
|
||||
private final Context appContext;
|
||||
@@ -71,6 +73,7 @@ public class FaceclawMediaController {
|
||||
private volatile FaceclawMediaControllerListener listener;
|
||||
private MediaController activeController;
|
||||
private boolean started;
|
||||
private Set<String> ignoredPackages = new HashSet<>();
|
||||
|
||||
public FaceclawMediaController(Context context) {
|
||||
this.appContext = context.getApplicationContext();
|
||||
@@ -113,6 +116,23 @@ public class FaceclawMediaController {
|
||||
}
|
||||
}
|
||||
|
||||
/** Apply the complete preference set and immediately choose an allowed session. */
|
||||
public void setIgnoredPackagesJson(String packagesJson) {
|
||||
Set<String> packages = new HashSet<>();
|
||||
try {
|
||||
JSONArray array = new JSONArray(packagesJson);
|
||||
for (int i = 0; i < array.length(); i++) packages.add(array.getString(i));
|
||||
} catch (Exception e) {
|
||||
Log.w("FaceclawMedia", "invalid ignored packages", e);
|
||||
return;
|
||||
}
|
||||
synchronized (lock) {
|
||||
if (ignoredPackages.equals(packages)) return;
|
||||
ignoredPackages = packages;
|
||||
refreshActiveControllerLocked(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
synchronized (lock) {
|
||||
if (!started) {
|
||||
@@ -289,6 +309,7 @@ public class FaceclawMediaController {
|
||||
controllers = null;
|
||||
}
|
||||
}
|
||||
emitSessionAppsLocked(controllers);
|
||||
setActiveControllerLocked(chooseController(controllers));
|
||||
emitStateLocked();
|
||||
}
|
||||
@@ -297,8 +318,10 @@ public class FaceclawMediaController {
|
||||
if (controllers == null || controllers.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
MediaController first = controllers.get(0);
|
||||
MediaController first = null;
|
||||
for (MediaController controller : controllers) {
|
||||
if (ignoredPackages.contains(controller.getPackageName())) continue;
|
||||
if (first == null) first = controller;
|
||||
PlaybackState playbackState = controller.getPlaybackState();
|
||||
if (playbackState != null && playbackState.getState() == PlaybackState.STATE_PLAYING) {
|
||||
return controller;
|
||||
@@ -307,6 +330,27 @@ public class FaceclawMediaController {
|
||||
return first;
|
||||
}
|
||||
|
||||
private void emitSessionAppsLocked(List<MediaController> controllers) {
|
||||
FaceclawMediaControllerListener currentListener = listener;
|
||||
if (currentListener == null || controllers == null) return;
|
||||
JSONArray apps = new JSONArray();
|
||||
Set<String> seen = new HashSet<>();
|
||||
for (MediaController controller : controllers) {
|
||||
String packageName = safe(controller.getPackageName());
|
||||
if (packageName.isEmpty() || !seen.add(packageName)) continue;
|
||||
try {
|
||||
JSONObject app = new JSONObject();
|
||||
app.put("packageName", packageName);
|
||||
app.put("appName", getApplicationLabel(packageName));
|
||||
apps.put(app);
|
||||
} catch (Exception e) {
|
||||
Log.w("FaceclawMedia", "session app serialization failed", e);
|
||||
}
|
||||
}
|
||||
String json = apps.toString();
|
||||
mainHandler.post(() -> currentListener.onSessionAppsChanged(json));
|
||||
}
|
||||
|
||||
private void setActiveControllerLocked(MediaController controller) {
|
||||
if (sameController(activeController, controller)) {
|
||||
return;
|
||||
|
||||
+3
@@ -1,6 +1,9 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawMediaControllerListener {
|
||||
/** All observed session owners, before filtering or choosing the active player. */
|
||||
void onSessionAppsChanged(String appsJson);
|
||||
|
||||
void onStateChange(
|
||||
String playbackState,
|
||||
String packageName,
|
||||
|
||||
@@ -8,6 +8,7 @@ import { lineStep, tightRowHeight } from "../../ui/metrics";
|
||||
import { mediaControllerBridge, type MediaControllerState, type MediaQueueItem } from "../../native/media-controller";
|
||||
import { mediaBrowserBridge, type MediaBrowserApp } from "../../native/media-browser";
|
||||
import { MediaBrowseLayer } from "./media-browse";
|
||||
import { MusicSettingsLayer } from "./music-settings";
|
||||
import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
|
||||
@@ -480,6 +481,13 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin
|
||||
iconLetter: "M",
|
||||
icon: "music",
|
||||
closeable: true,
|
||||
menuItems: () => [{
|
||||
label: "Music settings",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.pop();
|
||||
ctx.stack.push(new MusicSettingsLayer());
|
||||
},
|
||||
}],
|
||||
actions: options.actions,
|
||||
baseLayer: new MusicRootLayer(musicLayer),
|
||||
submitFrame: options.submitFrame,
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { truncateText } from "../../graphics/textwrap";
|
||||
import { mediaBrowserBridge } from "../../native/media-browser";
|
||||
import { isMediaAppEnabled, readMediaApps, setMediaAppEnabled } from "../../native/media-apps";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||
import { drawToggleMenuItem, MenuLayer, type MenuItem } from "../../ui/menu";
|
||||
|
||||
/** Discovered apps stay visible even when ignored or no longer publishing a session. */
|
||||
export class MusicSettingsLayer implements Layer {
|
||||
private menu: MenuLayer | null = null;
|
||||
private signature = "";
|
||||
private selectedPackage = "";
|
||||
|
||||
constructor() {
|
||||
// Refresh installed browser services, remembering ignored services too.
|
||||
mediaBrowserBridge.listBrowsableApps(true);
|
||||
}
|
||||
|
||||
private currentMenu(ctx: LayerContext): MenuLayer {
|
||||
const apps = readMediaApps().sort((a, b) =>
|
||||
a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName));
|
||||
const width = ctx.stack.getBaseSize().width;
|
||||
const signature = JSON.stringify([width, apps.map((app) => [app.packageName, app.appName])]);
|
||||
if (!this.menu || signature !== this.signature) {
|
||||
this.signature = signature;
|
||||
const items: MenuItem[] = apps.map((app) => ({
|
||||
label: app.appName,
|
||||
onSelect: () => setMediaAppEnabled(app, !isMediaAppEnabled(app.packageName)),
|
||||
render: ({ image, x, y, width, selected }) => {
|
||||
if (selected) this.selectedPackage = app.packageName;
|
||||
const font = getDefaultSmallFont();
|
||||
const duplicateName = apps.some((other) => other !== app && other.appName === app.appName);
|
||||
const label = duplicateName ? `${app.appName} (${app.packageName})` : app.appName;
|
||||
drawToggleMenuItem(image, font, x, y, width, truncateText(font, label, width - 48),
|
||||
isMediaAppEnabled(app.packageName), selected);
|
||||
},
|
||||
}));
|
||||
if (!items.length) items.push({ label: "No media apps discovered yet.", disabled: true, onSelect: () => {} });
|
||||
this.menu = new MenuLayer("Music settings", items, {
|
||||
x: 8, y: 8, width: width - 16,
|
||||
opaque: true, showBorder: false,
|
||||
footer: "On = enabled Off = ignored",
|
||||
}).selectItem(Math.max(0, apps.findIndex((app) => app.packageName === this.selectedPackage)));
|
||||
}
|
||||
return this.menu;
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
return this.currentMenu(ctx).paint(ctx, paintBelow);
|
||||
}
|
||||
|
||||
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||
await this.currentMenu(ctx).handleInput(event, ctx);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { getStringSetting, setStringSetting } from "./settings-store";
|
||||
|
||||
export const MEDIA_APPS_KEY = "music.apps";
|
||||
|
||||
// Exact package IDs only: browsers, social feeds and messaging apps commonly
|
||||
// publish sessions for embedded clips. Users can explicitly enable any of them.
|
||||
export const DEFAULT_IGNORED_MEDIA_APPS: readonly string[] = [
|
||||
"com.android.chrome",
|
||||
"org.mozilla.firefox",
|
||||
"com.microsoft.emmx",
|
||||
"com.sec.android.app.sbrowser",
|
||||
"com.facebook.katana",
|
||||
"com.facebook.orca",
|
||||
"com.instagram.android",
|
||||
"com.whatsapp",
|
||||
"org.telegram.messenger",
|
||||
"com.snapchat.android",
|
||||
"com.reddit.frontpage",
|
||||
"com.twitter.android",
|
||||
"com.discord",
|
||||
];
|
||||
|
||||
export type MediaApp = {
|
||||
packageName: string;
|
||||
appName: string;
|
||||
/** Absent until the user chooses; built-in defaults can evolve independently. */
|
||||
enabled?: boolean;
|
||||
};
|
||||
|
||||
/** Keep discovered apps after their sessions end, including ignored apps. */
|
||||
export function readMediaApps(): MediaApp[] {
|
||||
try {
|
||||
const parsed = JSON.parse(getStringSetting(MEDIA_APPS_KEY, "[]"));
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
const apps = new Map<string, MediaApp>();
|
||||
for (const app of parsed) {
|
||||
if (!app || typeof app.packageName !== "string" || !app.packageName) continue;
|
||||
apps.set(app.packageName, {
|
||||
packageName: app.packageName,
|
||||
appName: typeof app.appName === "string" && app.appName ? app.appName : app.packageName,
|
||||
...(typeof app.enabled === "boolean" ? { enabled: app.enabled } : {}),
|
||||
});
|
||||
}
|
||||
return Array.from(apps.values());
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function rememberMediaApps(discovered: { packageName: string; appName: string }[]): void {
|
||||
const apps = new Map(readMediaApps().map((app) => [app.packageName, app]));
|
||||
let changed = false;
|
||||
for (const app of discovered) {
|
||||
if (!app.packageName) continue;
|
||||
const previous = apps.get(app.packageName);
|
||||
const appName = app.appName || previous?.appName || app.packageName;
|
||||
if (!previous || previous.appName !== appName) {
|
||||
apps.set(app.packageName, { ...previous, packageName: app.packageName, appName });
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed) setStringSetting(MEDIA_APPS_KEY, JSON.stringify(Array.from(apps.values())));
|
||||
}
|
||||
|
||||
export function ignoredMediaPackages(): string[] {
|
||||
const ignored = new Set(DEFAULT_IGNORED_MEDIA_APPS);
|
||||
for (const app of readMediaApps()) {
|
||||
if (app.enabled === true) ignored.delete(app.packageName);
|
||||
else if (app.enabled === false) ignored.add(app.packageName);
|
||||
}
|
||||
return Array.from(ignored).sort();
|
||||
}
|
||||
|
||||
export function isMediaAppEnabled(packageName: string): boolean {
|
||||
return !ignoredMediaPackages().includes(packageName);
|
||||
}
|
||||
|
||||
export function setMediaAppEnabled(app: MediaApp, enabled: boolean): void {
|
||||
if (!app.packageName) return;
|
||||
const apps = new Map(readMediaApps().map((item) => [item.packageName, item]));
|
||||
apps.set(app.packageName, { packageName: app.packageName, appName: app.appName || app.packageName, enabled });
|
||||
setStringSetting(MEDIA_APPS_KEY, JSON.stringify(Array.from(apps.values())));
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
import { ignoredMediaPackages, isMediaAppEnabled, MEDIA_APPS_KEY, rememberMediaApps } from "./media-apps";
|
||||
import { onSettingsStoreChanged } from "./settings-store";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
@@ -34,11 +36,12 @@ export class FaceclawMediaBrowserBridge {
|
||||
private readonly pendingConnects = new Map<number, PendingRequest<string>>();
|
||||
private readonly pendingBrowses = new Map<number, PendingRequest<MediaBrowseItem[]>>();
|
||||
private appsCache: MediaBrowserApp[] | null = null;
|
||||
private connectedPackage = "";
|
||||
|
||||
/** Installed apps exposing a media browser service; cached after the first call. */
|
||||
listBrowsableApps(refresh = false): MediaBrowserApp[] {
|
||||
if (!global.isAndroid) return [];
|
||||
if (this.appsCache && !refresh) return this.appsCache;
|
||||
if (this.appsCache && !refresh) return this.enabledApps();
|
||||
this.ensureBrowser();
|
||||
if (!this.browser) return [];
|
||||
try {
|
||||
@@ -52,11 +55,17 @@ export class FaceclawMediaBrowserBridge {
|
||||
serviceClass: String(app.serviceClass ?? ""),
|
||||
appName: String(app.appName ?? ""),
|
||||
}));
|
||||
rememberMediaApps(this.appsCache);
|
||||
} catch (error) {
|
||||
console.warn(`media browser app list parse failed: ${error}`);
|
||||
this.appsCache = [];
|
||||
}
|
||||
return this.appsCache;
|
||||
return this.enabledApps();
|
||||
}
|
||||
|
||||
private enabledApps(): MediaBrowserApp[] {
|
||||
const ignored = new Set(ignoredMediaPackages());
|
||||
return (this.appsCache ?? []).filter((app) => !ignored.has(app.packageName));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,9 +74,11 @@ export class FaceclawMediaBrowserBridge {
|
||||
*/
|
||||
connect(app: MediaBrowserApp): Promise<string> {
|
||||
if (!global.isAndroid) return Promise.reject(new Error("Android only"));
|
||||
if (!isMediaAppEnabled(app.packageName)) return Promise.reject(new Error("App is ignored in Music settings"));
|
||||
this.ensureBrowser();
|
||||
if (!this.browser) return Promise.reject(new Error("Media browser unavailable"));
|
||||
const requestId = this.nextRequestId++;
|
||||
this.connectedPackage = app.packageName;
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
this.pendingConnects.set(requestId, { resolve, reject });
|
||||
this.browser.connect(requestId, app.packageName, app.serviceClass);
|
||||
@@ -77,6 +88,10 @@ export class FaceclawMediaBrowserBridge {
|
||||
/** Fetch one level of the connected service's content tree. */
|
||||
browse(parentId: string): Promise<MediaBrowseItem[]> {
|
||||
if (!global.isAndroid) return Promise.reject(new Error("Android only"));
|
||||
if (this.connectedPackage && !isMediaAppEnabled(this.connectedPackage)) {
|
||||
this.disconnect();
|
||||
return Promise.reject(new Error("App is ignored in Music settings"));
|
||||
}
|
||||
this.ensureBrowser();
|
||||
if (!this.browser) return Promise.reject(new Error("Media browser unavailable"));
|
||||
const requestId = this.nextRequestId++;
|
||||
@@ -89,6 +104,7 @@ export class FaceclawMediaBrowserBridge {
|
||||
/** Start playback of a browsed item through the connected player's session. */
|
||||
playFromMediaId(mediaId: string): void {
|
||||
if (!global.isAndroid) return;
|
||||
if (!this.connectedPackage || !isMediaAppEnabled(this.connectedPackage)) return;
|
||||
this.browser?.playFromMediaId(mediaId);
|
||||
}
|
||||
|
||||
@@ -96,6 +112,7 @@ export class FaceclawMediaBrowserBridge {
|
||||
disconnect(): void {
|
||||
if (!global.isAndroid) return;
|
||||
this.rejectAllPending("Disconnected");
|
||||
this.connectedPackage = "";
|
||||
this.browser?.disconnect();
|
||||
}
|
||||
|
||||
@@ -115,6 +132,11 @@ export class FaceclawMediaBrowserBridge {
|
||||
throw new Error("Android application context unavailable");
|
||||
}
|
||||
this.browser = new com.faceclaw.app.FaceclawMediaBrowser(context);
|
||||
onSettingsStoreChanged((key) => {
|
||||
if (key === MEDIA_APPS_KEY && this.connectedPackage && !isMediaAppEnabled(this.connectedPackage)) {
|
||||
this.disconnect();
|
||||
}
|
||||
});
|
||||
this.listenerProxy = new com.faceclaw.app.FaceclawMediaBrowserListener({
|
||||
onConnectResult: (requestId: number, connected: boolean, rootId: string, error: string) => {
|
||||
const pending = this.pendingConnects.get(Number(requestId));
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { grayImageFromPacket, PHOTO_TONE } from "./image-files";
|
||||
import { ignoredMediaPackages, MEDIA_APPS_KEY, rememberMediaApps } from "./media-apps";
|
||||
import { onSettingsStoreChanged } from "./settings-store";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
@@ -173,7 +175,22 @@ export class FaceclawMediaControllerBridge {
|
||||
throw new Error("Android application context unavailable");
|
||||
}
|
||||
this.controller = new com.faceclaw.app.FaceclawMediaController(context);
|
||||
// Install defaults and saved overrides before starting session discovery.
|
||||
this.controller.setIgnoredPackagesJson(JSON.stringify(ignoredMediaPackages()));
|
||||
onSettingsStoreChanged((key) => {
|
||||
if (key === MEDIA_APPS_KEY) {
|
||||
this.controller.setIgnoredPackagesJson(JSON.stringify(ignoredMediaPackages()));
|
||||
}
|
||||
});
|
||||
this.listenerProxy = new com.faceclaw.app.FaceclawMediaControllerListener({
|
||||
onSessionAppsChanged: (appsJson: string) => {
|
||||
try {
|
||||
const apps = JSON.parse(String(appsJson));
|
||||
rememberMediaApps(apps);
|
||||
} catch (error) {
|
||||
console.warn(`media session apps parse failed: ${error}`);
|
||||
}
|
||||
},
|
||||
onStateChange: (
|
||||
playbackState: string,
|
||||
packageName: string,
|
||||
|
||||
Reference in new issue
Block a user