forked from Blendman974/faceclaw
Merge branch 'main' into ios-port
This commit is contained in:
commit
a31ddae4d2
49 files changed
+2068
-212
No files matched your search
@@ -43,8 +43,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private static final long FACECLAW_WAKE_LEASE_RENEW_MS = 45_000;
|
||||
private static final long FACECLAW_WAKE_CONTROL_WAIT_MS = 1_500;
|
||||
private static final long CFW_CLEANUP_WAIT_MS = 4_000;
|
||||
private static final int COMPASS_REPORT_INTERVAL_MS = 250;
|
||||
private static final int COMPASS_MIN_CHANGE_DEGREES = 1;
|
||||
private static final int COMPASS_REPORT_INTERVAL_MS = 100;
|
||||
private static final int COMPASS_MIN_CHANGE_DEGREES = 0;
|
||||
|
||||
private final Context appContext;
|
||||
private final PowerManager powerManager;
|
||||
@@ -1567,7 +1567,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
emitWearState(decodedWearState > 0);
|
||||
}
|
||||
if (compassEvent != null) {
|
||||
emitCompassEvent(compassEvent.command, compassEvent.headingDegrees);
|
||||
emitCompassEvent(compassEvent);
|
||||
}
|
||||
if (event != null) {
|
||||
if (event.hasImu) {
|
||||
@@ -3456,11 +3456,22 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
});
|
||||
}
|
||||
|
||||
private void emitCompassEvent(int command, int headingDegrees) {
|
||||
private void emitCompassEvent(BleProtocol.CompassEvent event) {
|
||||
if (event.diagnosticFlags >= 0) {
|
||||
String[] sources = { "unknown", "GRV", "GMRV", "RV" };
|
||||
Log.i("FaceclawCompass", "heading=" + event.headingDegrees
|
||||
+ " magneticAccuracy=" + event.magneticAccuracy
|
||||
+ " magneticAnomalies=" + event.magneticAnomalies
|
||||
+ " orientationSource=" + sources[event.orientationSource]
|
||||
+ " flags=0x" + Integer.toHexString(event.diagnosticFlags)
|
||||
+ " sampleTimeMs=" + event.sampleTimeMs);
|
||||
}
|
||||
for (CompassSubscription subscription : compassSubscriptions) {
|
||||
subscription.handler.post(() -> {
|
||||
try {
|
||||
subscription.listener.onCompassEvent(command, headingDegrees);
|
||||
subscription.listener.onCompassEvent(event.command, event.headingDegrees,
|
||||
event.magneticAccuracy, event.magneticAnomalies, event.orientationSource,
|
||||
event.diagnosticFlags, event.sampleTimeMs);
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "listener onCompassEvent failed", t);
|
||||
}
|
||||
|
||||
@@ -2,5 +2,6 @@ package com.faceclaw.app;
|
||||
|
||||
/** Receives stock compass heading and calibration notifications on the thread that subscribed. */
|
||||
public interface FaceclawCompassListener {
|
||||
void onCompassEvent(int command, int headingDegrees);
|
||||
void onCompassEvent(int command, int headingDegrees, int magneticAccuracy, int magneticAnomalies,
|
||||
int orientationSource, int diagnosticFlags, long sampleTimeMs);
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
+1
-1
@@ -210,7 +210,7 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
|
||||
public static String getActiveNotificationsJson(int maxNotifications) {
|
||||
FaceclawMediaNotificationListenerService service = activeService;
|
||||
int limit = Math.max(0, Math.min(100, maxNotifications));
|
||||
int limit = Math.max(0, maxNotifications);
|
||||
if (service == null || limit == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
@@ -772,7 +772,23 @@ public class BleProtocol {
|
||||
byte[] compass = readFieldBytes(root, 10);
|
||||
if (compass == null) return null;
|
||||
int heading = readVarintFieldValue(compass, 1, -1);
|
||||
return heading >= 0 ? new CompassEvent(command, heading) : null;
|
||||
if (heading < 0 || heading >= 360) return null;
|
||||
byte[] diagnostic = readFieldBytes(root, 100);
|
||||
if (diagnostic != null && diagnostic.length == 12
|
||||
&& diagnostic[0] == 'C' && diagnostic[1] == 'M' && diagnostic[2] == 1
|
||||
&& diagnostic[7] == 0
|
||||
&& ((diagnostic[3] & 0xff) <= 3 || diagnostic[3] == (byte) 255)
|
||||
&& ((diagnostic[4] & 0xff) <= 2 || diagnostic[4] == (byte) 255)
|
||||
&& (diagnostic[5] & 0xff) <= 3) {
|
||||
long sampleTimeMs = 0;
|
||||
for (int i = 0; i < 4; i++) sampleTimeMs |= (long) (diagnostic[8 + i] & 0xff) << (8 * i);
|
||||
return new CompassEvent(command, heading,
|
||||
diagnostic[3] == (byte) 255 ? -1 : diagnostic[3] & 0xff,
|
||||
diagnostic[4] == (byte) 255 ? -1 : diagnostic[4] & 0xff,
|
||||
diagnostic[5] & 0xff, diagnostic[6] & 0xff, sampleTimeMs);
|
||||
}
|
||||
// Stock/older CFW and unknown extensions still supply usable headings.
|
||||
return new CompassEvent(command, heading);
|
||||
}
|
||||
if (command == NAV_CMD_COMPASS_CALIBRATION_STARTED
|
||||
|| command == NAV_CMD_COMPASS_CALIBRATION_COMPLETE) {
|
||||
@@ -1014,10 +1030,23 @@ public class BleProtocol {
|
||||
public static final class CompassEvent {
|
||||
public final int command;
|
||||
public final int headingDegrees;
|
||||
/** -1 means unavailable; source: 0 unknown, 1 GRV, 2 GMRV, 3 RV. */
|
||||
public final int magneticAccuracy, magneticAnomalies, orientationSource, diagnosticFlags;
|
||||
public final long sampleTimeMs;
|
||||
|
||||
CompassEvent(int command, int headingDegrees) {
|
||||
this(command, headingDegrees, -1, -1, -1, -1, -1);
|
||||
}
|
||||
|
||||
CompassEvent(int command, int headingDegrees, int magneticAccuracy, int magneticAnomalies,
|
||||
int orientationSource, int diagnosticFlags, long sampleTimeMs) {
|
||||
this.command = command;
|
||||
this.headingDegrees = headingDegrees;
|
||||
this.magneticAccuracy = magneticAccuracy;
|
||||
this.magneticAnomalies = magneticAnomalies;
|
||||
this.orientationSource = orientationSource;
|
||||
this.diagnosticFlags = diagnosticFlags;
|
||||
this.sampleTimeMs = sampleTimeMs;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user