Compare commits

...

13 commits

Author SHA1 Message Date
Filip Strajnar eb8dc83846 Added a button that sends last known location to telegram. 2024-10-27 13:31:24 +01:00
Filip Strajnar 137290e6d3 Added method sendLastKnownLocationToTelegram to service binder. 2024-10-27 13:28:45 +01:00
Filip Strajnar c65ead7bad Added method sendLastKnownLocationToTelegram. 2024-10-27 13:28:07 +01:00
Filip Strajnar 44a01332ae Method sendLocationToTelegram is now responsible for checking if internet is available. 2024-10-27 13:25:18 +01:00
Filip Strajnar 32a4467bce Added getLastKnownLocation method to LocationLoggingService. 2024-10-27 13:22:03 +01:00
Filip Strajnar 231d2fc835 Merge branch 'dev' into telegram 2024-10-27 13:12:37 +01:00
Filip Strajnar c2787d179a Checking if internet is available, before sending location to telegram. 2024-10-26 14:56:06 +02:00
Filip Strajnar 6cefb98c9b Cleaner telegram update configuration.
Distance between updates has also been set to only 1000 meters.
2024-10-26 14:02:35 +02:00
Filip Strajnar 4734104bd2 Only update location at most every 6 minutes. 2024-10-26 13:55:58 +02:00
Filip Strajnar 75a73d97fd Ignoring highly inaccurate location updates. 2024-10-26 12:44:58 +02:00
Filip Strajnar 71a1456611 Updating location only if it's more than 2 kilometers away from last sent location. 2024-10-26 12:40:17 +02:00
Filip Strajnar 7bdd8fc0b3 Created methods to send Location to telegram. 2024-10-26 12:25:55 +02:00
Filip Strajnar 79fd58d419 Added telegram bot API as dependency. 2024-10-26 11:48:49 +02:00
6 changed files with 141 additions and 3 deletions

View file

@ -54,5 +54,6 @@ dependencies {
implementation("io.jenetics:jpx:3.1.0")
// https://mvnrepository.com/artifact/org.codehaus.woodstox/woodstox-core-asl
implementation("org.codehaus.woodstox:woodstox-core-asl:4.4.1")
implementation("com.github.pengrad:java-telegram-bot-api:7.9.1")
}
}

View file

@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

View file

@ -9,7 +9,10 @@ import android.content.pm.ServiceInfo;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
@ -17,14 +20,28 @@ import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.ServiceCompat;
import androidx.core.location.LocationManagerCompat;
import androidx.room.Room;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.request.SendLocation;
import java.time.Duration;
import java.util.List;
public class LocationLoggingService extends Service implements LocationListener {
private final String TAG = LocationLoggingService.class.getName();
private LocationDao locationDao;
private Location lastLocation;
Duration durationBetweenTelegramUpdates = Duration.ofMinutes(6);
long metersDistanceBetweenTelegramUpdates = 1000;
private boolean internetAvailable = false;
NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.build();
public LocationLoggingService() {
}
@ -61,10 +78,31 @@ public class LocationLoggingService extends Service implements LocationListener
locationDao = database.locationDao();
subscribeToLocationUpdates(this);
subscribeToNetworkUpdates();
return super.onStartCommand(intent, flags, startId);
}
public void subscribeToNetworkUpdates(){
ConnectivityManager connectivityManager =
(ConnectivityManager) getSystemService(ConnectivityManager.class);
connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback(){
@Override
public void onLost(@NonNull Network network) {
super.onLost(network);
Log.i(TAG, "Internet connection lost.");
internetAvailable = false;
}
@Override
public void onAvailable(@NonNull Network network) {
super.onAvailable(network);
Log.i(TAG, "Internet connection obtained.");
internetAvailable = true;
}
});
}
public List<LocationEntity> allLocations(){
return locationDao.getAll();
}
@ -76,6 +114,18 @@ public class LocationLoggingService extends Service implements LocationListener
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
@SuppressLint("MissingPermission")
public Location getLastKnownLocation(){
LocationManager locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
public void sendLastKnownLocationToTelegram(){
Location lastKnownLocation = getLastKnownLocation();
this.lastLocation = lastKnownLocation;
sendLocationToTelegram(lastKnownLocation);
}
@Override
public void onDestroy() {
Log.d(TAG, "Service has been destroyed.");
@ -88,8 +138,72 @@ public class LocationLoggingService extends Service implements LocationListener
super.onCreate();
}
private void updateLocation(Location newLocation){
if(lastLocation != null)
{
if(newLocation.distanceTo(lastLocation) < metersDistanceBetweenTelegramUpdates) {
Log.d(TAG, "Distance to last location too short. Ignoring.");
return;
}
long oldTime = lastLocation.getTime();
long newTime = newLocation.getTime();
if(oldTime > newTime)
{
Log.w(TAG, "Last location time was somehow after new location's.");
return;
}
long millisSinceLastUpdate = newTime - oldTime;
if(durationBetweenTelegramUpdates.toMillis() > millisSinceLastUpdate)
{
Log.d(TAG, "Location update is too recent.");
return;
}
}
if(!newLocation.hasAccuracy())
{
Log.d(TAG, "New location does not have accuracy. Ignoring it.");
return;
}
if(newLocation.getAccuracy() > 100)
{
Log.d(TAG, "New location's accuracy is above 100 meters. Ignoring it.");
return;
}
lastLocation = newLocation;
sendLocationToTelegram(lastLocation);
}
public void sendLocationToTelegram(Location location) {
if(!internetAvailable)
{
Log.i(TAG, "Internet is not available.");
return;
}
String botToken = getResources().getString(R.string.telegramToken);
String telegramChat = getResources().getString(R.string.telegramChat);
TelegramBot bot = new TelegramBot(botToken);
Log.i(TAG, "Sending to telegram.");
new Thread(()->{
bot.execute(new SendLocation(telegramChat,
Double.valueOf(location.getLatitude()).floatValue(),
Double.valueOf(location.getLongitude()).floatValue()
));
}).start();
}
@Override
public void onLocationChanged(@NonNull Location location) {
updateLocation(location);
new Thread(()->{
Log.d(TAG, String.format(
"New location. Latitude: %s Longitude: %s Altitude: %s",

View file

@ -23,4 +23,8 @@ public class LocationLoggingServiceBinder extends Binder {
public List<LocationEntity> allLocations(){
return this.service.allLocations();
}
public void sendLastKnownLocationToTelegram(){
this.service.sendLastKnownLocationToTelegram();
}
}

View file

@ -32,6 +32,7 @@ import io.jenetics.jpx.WayPoint;
public class MainActivity extends AppCompatActivity implements LocationListener, ServiceConnection {
private final String TAG = MainActivity.class.getName();
private Button exportButton;
private Button sendTelegramButton;
private WriteToFile writeToFile;
private static boolean includeComments = false;
private static Double accuracyThreshold = 2.0;
@ -54,6 +55,7 @@ public class MainActivity extends AppCompatActivity implements LocationListener,
});
exportButton = findViewById(R.id.buttonExport);
sendTelegramButton = findViewById(R.id.buttonSendTelegram);
}
@Override
@ -89,6 +91,12 @@ public class MainActivity extends AppCompatActivity implements LocationListener,
LocationLoggingServiceBinder binder = (LocationLoggingServiceBinder) iBinder;
binder.subscribeToLocationUpdates(this);
if(sendTelegramButton != null){
sendTelegramButton.setOnClickListener(v -> {
binder.sendLastKnownLocationToTelegram();
});
}
if(exportButton != null){
exportButton.setOnClickListener(v -> {
Log.d(TAG, "Export button clicked.");

View file

@ -26,5 +26,13 @@
android:text="Export"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonSendTelegram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/buttonExport"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:text="Send to telegram" />
</androidx.constraintlayout.widget.ConstraintLayout>