feat: Add native Android driver tracking app and real-time backend/frontend bus tracking system
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package com.rit.driver;
|
||||
|
||||
public class Config {
|
||||
// Replace this with your VPS public IP address or production domain name
|
||||
public static final String BACKEND_URL = "http://15.206.182.201:8085";
|
||||
|
||||
// Default driver security PIN
|
||||
public static final String DEFAULT_PIN = "RITDRIVER";
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package com.rit.driver;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private static final int PERMISSION_REQUEST_CODE = 888;
|
||||
|
||||
private EditText etRouteNumber;
|
||||
private EditText etDriverPin;
|
||||
private Button btnStartSharing;
|
||||
private Button btnStopSharing;
|
||||
private TextView tvStatusHeader;
|
||||
private TextView tvStatusLogs;
|
||||
private LinearLayout statusCard;
|
||||
|
||||
private boolean isServiceRunning = false;
|
||||
|
||||
// Receive tracking updates from Foreground Service
|
||||
private final BroadcastReceiver locationReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent != null) {
|
||||
if (intent.hasExtra(TrackingService.EXTRA_STATUS)) {
|
||||
String status = intent.getStringExtra(TrackingService.EXTRA_STATUS);
|
||||
tvStatusHeader.setText("STATUS: " + (isServiceRunning ? "ACTIVE" : "INACTIVE"));
|
||||
logStatus(status);
|
||||
}
|
||||
|
||||
if (intent.hasExtra(TrackingService.EXTRA_LATITUDE) && intent.hasExtra(TrackingService.EXTRA_LONGITUDE)) {
|
||||
double lat = intent.getDoubleExtra(TrackingService.EXTRA_LATITUDE, 0.0);
|
||||
double lng = intent.getDoubleExtra(TrackingService.EXTRA_LONGITUDE, 0.0);
|
||||
float accuracy = intent.getFloatExtra(TrackingService.EXTRA_ACCURACY, 0.0f);
|
||||
|
||||
String gpsInfo = String.format("GPS Coords: %.6f, %.6f\nAccuracy: ±%.1fm", lat, lng, accuracy);
|
||||
logStatus(gpsInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
// Bind Views
|
||||
etRouteNumber = findViewById(R.id.etRouteNumber);
|
||||
etDriverPin = findViewById(R.id.etDriverPin);
|
||||
etDriverPin.setText(Config.DEFAULT_PIN);
|
||||
btnStartSharing = findViewById(R.id.btnStartSharing);
|
||||
btnStopSharing = findViewById(R.id.btnStopSharing);
|
||||
tvStatusHeader = findViewById(R.id.tvStatusHeader);
|
||||
tvStatusLogs = findViewById(R.id.tvStatusLogs);
|
||||
statusCard = findViewById(R.id.statusCard);
|
||||
|
||||
btnStartSharing.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (checkAndRequestPermissions()) {
|
||||
startTracking();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
btnStopSharing.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
stopTracking();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
// Register receiver for service communication
|
||||
IntentFilter filter = new IntentFilter(TrackingService.ACTION_LOCATION_BROADCAST);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
registerReceiver(locationReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
|
||||
} else {
|
||||
registerReceiver(locationReceiver, filter);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
// Unregister receiver
|
||||
unregisterReceiver(locationReceiver);
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
private void startTracking() {
|
||||
String serverUrl = Config.BACKEND_URL;
|
||||
String routeNumber = etRouteNumber.getText().toString().trim();
|
||||
String driverPin = etDriverPin.getText().toString().trim();
|
||||
if (routeNumber.isEmpty()) {
|
||||
Toast.makeText(this, "Enter bus route number", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
if (driverPin.isEmpty()) {
|
||||
Toast.makeText(this, "Enter security driver PIN", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
Intent serviceIntent = new Intent(this, TrackingService.class);
|
||||
serviceIntent.putExtra("server_url", serverUrl);
|
||||
serviceIntent.putExtra("route_number", routeNumber);
|
||||
serviceIntent.putExtra("driver_pin", driverPin);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
startForegroundService(serviceIntent);
|
||||
} else {
|
||||
startService(serviceIntent);
|
||||
}
|
||||
|
||||
isServiceRunning = true;
|
||||
btnStartSharing.setVisibility(View.GONE);
|
||||
btnStopSharing.setVisibility(View.VISIBLE);
|
||||
tvStatusHeader.setText("STATUS: ACTIVE");
|
||||
logStatus("Foreground tracking service started.");
|
||||
}
|
||||
|
||||
private void stopTracking() {
|
||||
Intent serviceIntent = new Intent(this, TrackingService.class);
|
||||
stopService(serviceIntent);
|
||||
|
||||
isServiceRunning = false;
|
||||
btnStartSharing.setVisibility(View.VISIBLE);
|
||||
btnStopSharing.setVisibility(View.GONE);
|
||||
tvStatusHeader.setText("STATUS: INACTIVE");
|
||||
logStatus("Tracking stopped by driver.");
|
||||
}
|
||||
|
||||
private void logStatus(String msg) {
|
||||
String currentText = tvStatusLogs.getText().toString();
|
||||
tvStatusLogs.setText(msg + "\n\n" + currentText);
|
||||
}
|
||||
|
||||
private boolean checkAndRequestPermissions() {
|
||||
boolean hasFine = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
|
||||
boolean hasCoarse = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
|
||||
|
||||
List<String> listPermissionsNeeded = new ArrayList<>();
|
||||
|
||||
if (!hasFine && !hasCoarse) {
|
||||
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
|
||||
listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
|
||||
listPermissionsNeeded.add(Manifest.permission.POST_NOTIFICATIONS);
|
||||
}
|
||||
}
|
||||
|
||||
if (!listPermissionsNeeded.isEmpty()) {
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
listPermissionsNeeded.toArray(new String[0]),
|
||||
PERMISSION_REQUEST_CODE
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == PERMISSION_REQUEST_CODE) {
|
||||
boolean hasFine = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
|
||||
boolean hasCoarse = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
|
||||
|
||||
if (hasFine || hasCoarse) {
|
||||
startTracking();
|
||||
} else {
|
||||
Toast.makeText(this, "Location permission (Precise or Approximate) is required to track the bus.", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.rit.driver;
|
||||
|
||||
import android.util.Log;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class NetworkHelper {
|
||||
|
||||
private static final String TAG = "NetworkHelper";
|
||||
|
||||
public interface Callback {
|
||||
void onSuccess();
|
||||
void onFailure(String error);
|
||||
}
|
||||
|
||||
public static void postLocation(final String serverUrl, final String routeNumber, final double lat, final double lng, final String pin, final Callback callback) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
HttpURLConnection conn = null;
|
||||
try {
|
||||
// Clean URL trailing slash and route mapping
|
||||
String cleanUrl = serverUrl;
|
||||
if (cleanUrl.endsWith("/")) {
|
||||
cleanUrl = cleanUrl.substring(0, cleanUrl.length() - 1);
|
||||
}
|
||||
String targetUrl = cleanUrl + "/api/bus-locations/" + routeNumber;
|
||||
|
||||
URL url = new URL(targetUrl);
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/json");
|
||||
conn.setDoOutput(true);
|
||||
conn.setConnectTimeout(8000);
|
||||
conn.setReadTimeout(8000);
|
||||
|
||||
// Build JSON string payload
|
||||
String jsonInputString = String.format(
|
||||
"{\"latitude\": %f, \"longitude\": %f, \"pin\": \"%s\"}",
|
||||
lat, lng, pin
|
||||
);
|
||||
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
int code = conn.getResponseCode();
|
||||
if (code == 200 || code == 201) {
|
||||
Log.d(TAG, "Location uploaded successfully: " + code);
|
||||
if (callback != null) {
|
||||
callback.onSuccess();
|
||||
}
|
||||
} else {
|
||||
String errMsg = "HTTP error code " + code;
|
||||
Log.w(TAG, errMsg);
|
||||
if (callback != null) {
|
||||
callback.onFailure(errMsg);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String errMsg = "Upload failed: " + e.getMessage();
|
||||
Log.e(TAG, errMsg, e);
|
||||
if (callback != null) {
|
||||
callback.onFailure(errMsg);
|
||||
}
|
||||
} finally {
|
||||
if (conn != null) {
|
||||
conn.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package com.rit.driver;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import android.util.Log;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
public class TrackingService extends Service {
|
||||
|
||||
private static final String TAG = "TrackingService";
|
||||
private static final String CHANNEL_ID = "TrackingServiceChannel";
|
||||
private static final int NOTIFICATION_ID = 444;
|
||||
|
||||
private LocationManager locationManager;
|
||||
private PowerManager.WakeLock wakeLock;
|
||||
|
||||
private String serverUrl = "";
|
||||
private String routeNumber = "";
|
||||
private String driverPin = "";
|
||||
|
||||
// Broadcast action for MainActivity to update UI coordinates
|
||||
public static final String ACTION_LOCATION_BROADCAST = "com.rit.driver.LOCATION_BROADCAST";
|
||||
public static final String EXTRA_LATITUDE = "extra_latitude";
|
||||
public static final String EXTRA_LONGITUDE = "extra_longitude";
|
||||
public static final String EXTRA_ACCURACY = "extra_accuracy";
|
||||
public static final String EXTRA_STATUS = "extra_status";
|
||||
|
||||
private final LocationListener locationListener = new LocationListener() {
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
double lat = location.getLatitude();
|
||||
double lng = location.getLongitude();
|
||||
float accuracy = location.getAccuracy();
|
||||
|
||||
Log.d(TAG, "Location updated: " + lat + ", " + lng + " (Accuracy: " + accuracy + "m)");
|
||||
|
||||
// Broadcast updates locally to MainActivity UI
|
||||
Intent broadcastIntent = new Intent(ACTION_LOCATION_BROADCAST);
|
||||
broadcastIntent.putExtra(EXTRA_LATITUDE, lat);
|
||||
broadcastIntent.putExtra(EXTRA_LONGITUDE, lng);
|
||||
broadcastIntent.putExtra(EXTRA_ACCURACY, accuracy);
|
||||
broadcastIntent.putExtra(EXTRA_STATUS, "Broadcasting Location...");
|
||||
sendBroadcast(broadcastIntent);
|
||||
|
||||
// Upload location to backend
|
||||
NetworkHelper.postLocation(serverUrl, routeNumber, lat, lng, driverPin, new NetworkHelper.Callback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
Log.d(TAG, "Uploaded location successfully");
|
||||
Intent statusIntent = new Intent(ACTION_LOCATION_BROADCAST);
|
||||
statusIntent.putExtra(EXTRA_STATUS, "Upload Success: Location synced.");
|
||||
sendBroadcast(statusIntent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(String error) {
|
||||
Log.e(TAG, "Upload failed: " + error);
|
||||
Intent statusIntent = new Intent(ACTION_LOCATION_BROADCAST);
|
||||
statusIntent.putExtra(EXTRA_STATUS, "Upload Failed: " + error);
|
||||
sendBroadcast(statusIntent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStatusChanged(String provider, int status, Bundle extras) {}
|
||||
|
||||
@Override
|
||||
public void onProviderEnabled(String provider) {}
|
||||
|
||||
@Override
|
||||
public void onProviderDisabled(String provider) {
|
||||
Intent statusIntent = new Intent(ACTION_LOCATION_BROADCAST);
|
||||
statusIntent.putExtra(EXTRA_STATUS, "GPS Provider Disabled. Please turn on Location/GPS.");
|
||||
sendBroadcast(statusIntent);
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
Log.d(TAG, "onCreate: Service starting");
|
||||
createNotificationChannel();
|
||||
}
|
||||
|
||||
@SuppressLint("InvalidWakeLockTag")
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Log.d(TAG, "onStartCommand: Service active");
|
||||
|
||||
if (intent != null) {
|
||||
serverUrl = intent.getStringExtra("server_url");
|
||||
routeNumber = intent.getStringExtra("route_number");
|
||||
driverPin = intent.getStringExtra("driver_pin");
|
||||
}
|
||||
|
||||
// 1. Show Foreground Notification
|
||||
Intent notificationIntent = new Intent(this, MainActivity.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(
|
||||
this, 0, notificationIntent,
|
||||
PendingIntent.FLAG_IMMUTABLE
|
||||
);
|
||||
|
||||
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
.setContentTitle("RIT Driver Tracker")
|
||||
.setContentText("Active Journey: Route " + routeNumber + " is currently tracking...")
|
||||
.setSmallIcon(android.R.drawable.ic_menu_compass)
|
||||
.setContentIntent(pendingIntent)
|
||||
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||
.build();
|
||||
|
||||
startForeground(NOTIFICATION_ID, notification);
|
||||
|
||||
// 2. Acquire Power CPU WakeLock
|
||||
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||
if (pm != null) {
|
||||
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TrackingService::WakeLock");
|
||||
wakeLock.acquire();
|
||||
Log.d(TAG, "WakeLock acquired successfully");
|
||||
}
|
||||
|
||||
// 3. Register GPS location listener
|
||||
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||
try {
|
||||
if (locationManager != null) {
|
||||
// Request updates every 5 seconds (5000ms) or 2 meters
|
||||
locationManager.requestLocationUpdates(
|
||||
LocationManager.GPS_PROVIDER,
|
||||
5000,
|
||||
2.0f,
|
||||
locationListener
|
||||
);
|
||||
Log.d(TAG, "GPS Listener registered successfully");
|
||||
}
|
||||
} catch (SecurityException e) {
|
||||
Log.e(TAG, "SecurityException: Location permissions not granted", e);
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
return START_REDELIVER_INTENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
Log.d(TAG, "onDestroy: Stopping service cleanups");
|
||||
|
||||
// 1. Remove GPS Listener
|
||||
if (locationManager != null) {
|
||||
locationManager.removeUpdates(locationListener);
|
||||
}
|
||||
|
||||
// 2. Release CPU WakeLock
|
||||
if (wakeLock != null && wakeLock.isHeld()) {
|
||||
wakeLock.release();
|
||||
Log.d(TAG, "WakeLock released cleanly");
|
||||
}
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void createNotificationChannel() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
NotificationChannel serviceChannel = new NotificationChannel(
|
||||
CHANNEL_ID,
|
||||
"RIT Driver Tracker Channel",
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
);
|
||||
NotificationManager manager = getSystemService(NotificationManager.class);
|
||||
if (manager != null) {
|
||||
manager.createNotificationChannel(serviceChannel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user