feat(driver-app): add continuous 5s periodic uploader timer fallback in TrackingService
This commit is contained in:
@@ -2,7 +2,7 @@ package com.rit.driver;
|
|||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
public static final String BACKEND_URL = "https://api.rit-services.in";
|
public static final String BACKEND_URL = "https://rit-services.in";
|
||||||
|
|
||||||
|
|
||||||
public static final String DEFAULT_PIN = "RITDRIVER";
|
public static final String DEFAULT_PIN = "RITDRIVER";
|
||||||
|
|||||||
@@ -97,6 +97,35 @@ public class TrackingService extends Service {
|
|||||||
createNotificationChannel();
|
createNotificationChannel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private android.os.Handler handler = new android.os.Handler(android.os.Looper.getMainLooper());
|
||||||
|
private Runnable periodicUploader = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
Location loc = null;
|
||||||
|
if (locationManager != null) {
|
||||||
|
try {
|
||||||
|
Location gpsLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||||
|
Location netLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
||||||
|
loc = (gpsLoc != null) ? gpsLoc : netLoc;
|
||||||
|
} catch (SecurityException ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loc != null) {
|
||||||
|
locationListener.onLocationChanged(loc);
|
||||||
|
} else {
|
||||||
|
Intent statusIntent = new Intent(ACTION_LOCATION_BROADCAST);
|
||||||
|
statusIntent.putExtra(EXTRA_STATUS, "Waiting for initial GPS location fix...");
|
||||||
|
sendBroadcast(statusIntent);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Error in periodic uploader: " + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
handler.postDelayed(this, 5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@SuppressLint("InvalidWakeLockTag")
|
@SuppressLint("InvalidWakeLockTag")
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
@@ -137,39 +166,22 @@ public class TrackingService extends Service {
|
|||||||
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||||
try {
|
try {
|
||||||
if (locationManager != null) {
|
if (locationManager != null) {
|
||||||
// Request GPS updates
|
|
||||||
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||||
locationManager.requestLocationUpdates(
|
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0.0f, locationListener);
|
||||||
LocationManager.GPS_PROVIDER,
|
|
||||||
5000,
|
|
||||||
0.0f,
|
|
||||||
locationListener
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
// Request Network/Wi-Fi location updates (essential indoors)
|
|
||||||
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
||||||
locationManager.requestLocationUpdates(
|
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0.0f, locationListener);
|
||||||
LocationManager.NETWORK_PROVIDER,
|
|
||||||
5000,
|
|
||||||
0.0f,
|
|
||||||
locationListener
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Immediately trigger initial fix if available
|
|
||||||
Location lastGps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
|
||||||
Location lastNet = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
|
||||||
Location bestLocation = (lastGps != null) ? lastGps : lastNet;
|
|
||||||
if (bestLocation != null) {
|
|
||||||
locationListener.onLocationChanged(bestLocation);
|
|
||||||
}
|
|
||||||
Log.d(TAG, "Location Listeners registered successfully");
|
|
||||||
}
|
}
|
||||||
} catch (SecurityException e) {
|
} catch (SecurityException e) {
|
||||||
Log.e(TAG, "SecurityException: Location permissions not granted", e);
|
Log.e(TAG, "SecurityException: Location permissions not granted", e);
|
||||||
stopSelf();
|
stopSelf();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 4. Start periodic 5-second uploader
|
||||||
|
handler.removeCallbacks(periodicUploader);
|
||||||
|
handler.post(periodicUploader);
|
||||||
|
|
||||||
return START_REDELIVER_INTENT;
|
return START_REDELIVER_INTENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,6 +189,11 @@ public class TrackingService extends Service {
|
|||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
Log.d(TAG, "onDestroy: Stopping service cleanups");
|
Log.d(TAG, "onDestroy: Stopping service cleanups");
|
||||||
|
|
||||||
|
// Stop periodic uploader timer
|
||||||
|
if (handler != null && periodicUploader != null) {
|
||||||
|
handler.removeCallbacks(periodicUploader);
|
||||||
|
}
|
||||||
|
|
||||||
// 1. Remove GPS Listener
|
// 1. Remove GPS Listener
|
||||||
if (locationManager != null) {
|
if (locationManager != null) {
|
||||||
locationManager.removeUpdates(locationListener);
|
locationManager.removeUpdates(locationListener);
|
||||||
|
|||||||
Reference in New Issue
Block a user