@@ -20,38 +20,48 @@ function createLocationPermissionError(code: string, message: string) {
2020 return error ;
2121}
2222
23- TaskManager . defineTask ( TASK_NAME , async ( { data, error } ) => {
24- console . log ( "[LOCATION] Background task triggered" ) ; //REMOVE: for testing
25- if ( error ) {
26- console . error ( "[LOCATION] Task error" , error ) ; //REMOVE: for testing
27- return ;
28- }
29-
30- const location = ( data as any ) ?. locations ?. [ 0 ] ;
31- if ( ! location ) return ;
32-
33- const { latitude, longitude, accuracy, speed, heading } = location . coords ;
34-
35- if ( accuracy != null && accuracy > 50 ) return ;
23+ if ( ! TaskManager . isTaskDefined ( TASK_NAME ) ) {
24+ TaskManager . defineTask ( TASK_NAME , async ( { data, error } ) => {
25+ console . log ( "[LOCATION] Background task triggered" ) ; //REMOVE: for testing
26+ if ( error ) {
27+ console . error ( "[LOCATION] Task error" , error ) ; //REMOVE: for testing
28+ return ;
29+ }
3630
37- addPendingLocation ( {
38- id : Crypto . randomUUID ( ) ,
39- ts : new Date ( location . timestamp ) . toISOString ( ) ,
40- latitude,
41- longitude,
42- accuracyM : accuracy ?? null ,
43- speedMps : speed ?? null ,
44- headingDeg : heading ?? null ,
45- provider : "gps" ,
46- meta : JSON . stringify ( { battery : null } ) ,
31+ const location = ( data as any ) ?. locations ?. [ 0 ] ;
32+ if ( ! location ) return ;
33+
34+ const { latitude, longitude, accuracy, speed, heading } = location . coords ;
35+
36+ if ( accuracy != null && accuracy > 50 ) return ;
37+
38+ addPendingLocation ( {
39+ id : Crypto . randomUUID ( ) ,
40+ ts : new Date ( location . timestamp ) . toISOString ( ) ,
41+ latitude,
42+ longitude,
43+ accuracyM : accuracy ?? null ,
44+ speedMps : speed ?? null ,
45+ headingDeg : heading ?? null ,
46+ provider : "gps" ,
47+ meta : JSON . stringify ( { battery : null } ) ,
48+ } ) ;
49+
50+ try {
51+ await removePendingLocations ( ) ;
52+ } catch { }
53+ const count = countPendingLocations ( ) ; //REMOVE:for testing
54+ console . log ( "[LOCATION] Pending rows in SQLite:" , count ) ;
4755 } ) ;
56+ }
57+
58+ export function getTrackingTaskName ( ) {
59+ return TASK_NAME ;
60+ }
4861
49- try {
50- await removePendingLocations ( ) ;
51- } catch { }
52- const count = countPendingLocations ( ) ; //REMOVE:for testing
53- console . log ( "[LOCATION] Pending rows in SQLite:" , count ) ;
54- } ) ;
62+ export async function isTrackingActive ( ) {
63+ return Location . hasStartedLocationUpdatesAsync ( TASK_NAME ) ;
64+ }
5565
5666export async function startTracking ( ) {
5767 const isBackgroundLocationAvailable =
@@ -103,7 +113,7 @@ export async function startTracking() {
103113 ) ;
104114 }
105115
106- const hasStarted = await Location . hasStartedLocationUpdatesAsync ( TASK_NAME ) ;
116+ const hasStarted = await isTrackingActive ( ) ;
107117 console . log ( "[LOCATION] Already started:" , hasStarted ) ; //REMOVE: for testing
108118 if ( hasStarted ) return ;
109119
@@ -113,11 +123,21 @@ export async function startTracking() {
113123 distanceInterval : 15 ,
114124 showsBackgroundLocationIndicator : true ,
115125 pausesUpdatesAutomatically : false ,
126+ ...( Platform . OS === "android"
127+ ? {
128+ foregroundService : {
129+ notificationTitle : "Duty tracking active" ,
130+ notificationBody :
131+ "Crime Link Analyzer is collecting location while you are on duty." ,
132+ notificationColor : "#0B57D0" ,
133+ } ,
134+ }
135+ : { } ) ,
116136 } ) ;
117137}
118138
119139export async function stopTracking ( ) {
120- const hasStarted = await Location . hasStartedLocationUpdatesAsync ( TASK_NAME ) ;
140+ const hasStarted = await isTrackingActive ( ) ;
121141 if ( ! hasStarted ) return ;
122142
123143 await Location . stopLocationUpdatesAsync ( TASK_NAME ) ;
0 commit comments