forked from Blendman974/faceclaw
Add a Navigate app (requires a Mapbox API token)
This commit is contained in:
1 parent
3c2f53ed75
commit
bde70ac380
16 files changed
+2096
-4
No files matched your search
@@ -111,6 +111,13 @@ public class FaceclawForegroundService extends Service {
|
||||
if (hasRecordAudioPermission()) {
|
||||
type |= ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE;
|
||||
}
|
||||
// The location type keeps while-in-use location flowing to the
|
||||
// Navigate app when the phone screen locks. Only claimed once the
|
||||
// permission exists: on API 34+ claiming it without the permission
|
||||
// makes startForeground throw.
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && hasFineLocationPermission()) {
|
||||
type |= ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION;
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
@@ -118,4 +125,9 @@ public class FaceclawForegroundService extends Service {
|
||||
return ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
|
||||
== PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
private boolean hasFineLocationPermission() {
|
||||
return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
== PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
/**
|
||||
* Continuous location updates for turn-by-turn navigation. Unlike
|
||||
* FaceclawLocationProvider (one-shot, coarse, for Weather), this streams GPS
|
||||
* fixes with bearing and speed until stopped.
|
||||
*
|
||||
* Callbacks are delivered on the Looper of the thread that constructed the
|
||||
* tracker, so a worker isolate receives them on its own thread (the same
|
||||
* convention as FaceclawSseRequest).
|
||||
*/
|
||||
public final class FaceclawLocationTracker implements LocationListener {
|
||||
private final Context context;
|
||||
private final LocationManager locationManager;
|
||||
private final Handler callbackHandler;
|
||||
|
||||
private FaceclawLocationTrackerListener listener;
|
||||
private boolean running;
|
||||
|
||||
public FaceclawLocationTracker(Context context) {
|
||||
this.context = context.getApplicationContext();
|
||||
this.locationManager = (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE);
|
||||
Looper looper = Looper.myLooper();
|
||||
this.callbackHandler = new Handler(looper != null ? looper : Looper.getMainLooper());
|
||||
}
|
||||
|
||||
public void setListener(FaceclawLocationTrackerListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
/** Begin streaming fixes at roughly the requested interval. */
|
||||
public void start(long intervalMs) {
|
||||
callbackHandler.post(() -> startOnCallbackThread(intervalMs));
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
callbackHandler.post(() -> {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
running = false;
|
||||
try {
|
||||
locationManager.removeUpdates(this);
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void startOnCallbackThread(long intervalMs) {
|
||||
if (running) {
|
||||
return;
|
||||
}
|
||||
if (locationManager == null) {
|
||||
deliverError("Android location service is unavailable.");
|
||||
return;
|
||||
}
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
deliverError("Precise location permission is required for navigation.");
|
||||
return;
|
||||
}
|
||||
|
||||
String provider = chooseProvider();
|
||||
if (provider == null) {
|
||||
deliverError("Turn on Location on your phone, then retry.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
running = true;
|
||||
locationManager.requestLocationUpdates(provider, Math.max(500L, intervalMs), 0f, this,
|
||||
callbackHandler.getLooper());
|
||||
// Seed with the freshest cached fix so the UI has a position
|
||||
// before the first live fix (GPS cold starts can take a while).
|
||||
Location cached = locationManager.getLastKnownLocation(provider);
|
||||
if (cached == null) {
|
||||
cached = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
||||
}
|
||||
if (cached != null) {
|
||||
deliverLocation(cached);
|
||||
}
|
||||
} catch (SecurityException error) {
|
||||
running = false;
|
||||
deliverError("Precise location permission is required for navigation.");
|
||||
} catch (Throwable error) {
|
||||
running = false;
|
||||
deliverError("Unable to start location updates.");
|
||||
}
|
||||
}
|
||||
|
||||
private String chooseProvider() {
|
||||
try {
|
||||
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||
return LocationManager.GPS_PROVIDER;
|
||||
}
|
||||
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
||||
return LocationManager.NETWORK_PROVIDER;
|
||||
}
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
deliverLocation(location);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderDisabled(String provider) {
|
||||
deliverError("Location was turned off on the phone.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderEnabled(String provider) {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||
}
|
||||
|
||||
private void deliverLocation(Location location) {
|
||||
FaceclawLocationTrackerListener current = listener;
|
||||
if (current == null || location == null) {
|
||||
return;
|
||||
}
|
||||
current.onLocation(
|
||||
location.getLatitude(),
|
||||
location.getLongitude(),
|
||||
location.hasAccuracy() ? location.getAccuracy() : -1f,
|
||||
location.hasBearing() ? location.getBearing() : -1f,
|
||||
location.hasSpeed() ? location.getSpeed() : -1f,
|
||||
location.getTime());
|
||||
}
|
||||
|
||||
private void deliverError(String message) {
|
||||
FaceclawLocationTrackerListener current = listener;
|
||||
if (current != null) {
|
||||
current.onError(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Continuous location callbacks for the TypeScript navigation bridge. */
|
||||
public interface FaceclawLocationTrackerListener {
|
||||
/**
|
||||
* A new fix. bearingDeg/speedMps are -1 when the fix doesn't carry them
|
||||
* (common when stationary); accuracyMeters is -1 when unknown.
|
||||
*/
|
||||
void onLocation(double latitude, double longitude, float accuracyMeters,
|
||||
float bearingDeg, float speedMps, long timestampMs);
|
||||
|
||||
void onError(String message);
|
||||
}
|
||||
@@ -50,6 +50,34 @@ public final class ImageFileLoader {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode an in-memory image file (PNG/JPEG/WebP bytes, e.g. a fetched
|
||||
* map tile) and downscale to fit within maxWidth x maxHeight. Takes a
|
||||
* ByteBuffer because NativeScript marshals a JS ArrayBuffer to one.
|
||||
*/
|
||||
public static byte[] loadGrayFromBytes(java.nio.ByteBuffer buffer, int maxWidth, int maxHeight) {
|
||||
if (buffer == null || maxWidth <= 0 || maxHeight <= 0) {
|
||||
return new byte[0];
|
||||
}
|
||||
try {
|
||||
java.nio.ByteBuffer source = buffer.duplicate();
|
||||
source.rewind();
|
||||
byte[] data = new byte[source.remaining()];
|
||||
source.get(data);
|
||||
if (data.length == 0) {
|
||||
return new byte[0];
|
||||
}
|
||||
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
|
||||
if (bitmap == null) {
|
||||
return new byte[0];
|
||||
}
|
||||
return bitmapToGrayPacket(bitmap, maxWidth, maxHeight);
|
||||
} catch (Exception | OutOfMemoryError e) {
|
||||
Log.w(TAG, "byte-array image decode failed", e);
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale a bitmap to fit within maxWidth x maxHeight (preserving aspect,
|
||||
* never upscaling) and convert it to the grayscale packet format.
|
||||
|
||||
Reference in new issue
Block a user