Compare commits
13 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb8dc83846 | ||
|
|
137290e6d3 | ||
|
|
c65ead7bad | ||
|
|
44a01332ae | ||
|
|
32a4467bce | ||
|
|
231d2fc835 | ||
|
|
c2787d179a | ||
|
|
6cefb98c9b | ||
|
|
4734104bd2 | ||
|
|
75a73d97fd | ||
|
|
71a1456611 | ||
|
|
7bdd8fc0b3 | ||
|
|
79fd58d419 |
|
|
@ -54,5 +54,6 @@ dependencies {
|
||||||
implementation("io.jenetics:jpx:3.1.0")
|
implementation("io.jenetics:jpx:3.1.0")
|
||||||
// https://mvnrepository.com/artifact/org.codehaus.woodstox/woodstox-core-asl
|
// https://mvnrepository.com/artifact/org.codehaus.woodstox/woodstox-core-asl
|
||||||
implementation("org.codehaus.woodstox:woodstox-core-asl:4.4.1")
|
implementation("org.codehaus.woodstox:woodstox-core-asl:4.4.1")
|
||||||
|
implementation("com.github.pengrad:java-telegram-bot-api:7.9.1")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -2,6 +2,9 @@
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
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_COARSE_LOCATION" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,10 @@ import android.content.pm.ServiceInfo;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.location.LocationListener;
|
import android.location.LocationListener;
|
||||||
import android.location.LocationManager;
|
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.Build;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
@ -17,14 +20,28 @@ import android.util.Log;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.core.app.ServiceCompat;
|
import androidx.core.app.ServiceCompat;
|
||||||
import androidx.core.location.LocationManagerCompat;
|
|
||||||
import androidx.room.Room;
|
import androidx.room.Room;
|
||||||
|
|
||||||
|
import com.pengrad.telegrambot.TelegramBot;
|
||||||
|
import com.pengrad.telegrambot.request.SendLocation;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class LocationLoggingService extends Service implements LocationListener {
|
public class LocationLoggingService extends Service implements LocationListener {
|
||||||
private final String TAG = LocationLoggingService.class.getName();
|
private final String TAG = LocationLoggingService.class.getName();
|
||||||
private LocationDao locationDao;
|
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() {
|
public LocationLoggingService() {
|
||||||
}
|
}
|
||||||
|
|
@ -61,10 +78,31 @@ public class LocationLoggingService extends Service implements LocationListener
|
||||||
locationDao = database.locationDao();
|
locationDao = database.locationDao();
|
||||||
|
|
||||||
subscribeToLocationUpdates(this);
|
subscribeToLocationUpdates(this);
|
||||||
|
subscribeToNetworkUpdates();
|
||||||
|
|
||||||
return super.onStartCommand(intent, flags, startId);
|
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(){
|
public List<LocationEntity> allLocations(){
|
||||||
return locationDao.getAll();
|
return locationDao.getAll();
|
||||||
}
|
}
|
||||||
|
|
@ -76,6 +114,18 @@ public class LocationLoggingService extends Service implements LocationListener
|
||||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 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
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
Log.d(TAG, "Service has been destroyed.");
|
Log.d(TAG, "Service has been destroyed.");
|
||||||
|
|
@ -88,8 +138,72 @@ public class LocationLoggingService extends Service implements LocationListener
|
||||||
super.onCreate();
|
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
|
@Override
|
||||||
public void onLocationChanged(@NonNull Location location) {
|
public void onLocationChanged(@NonNull Location location) {
|
||||||
|
updateLocation(location);
|
||||||
|
|
||||||
new Thread(()->{
|
new Thread(()->{
|
||||||
Log.d(TAG, String.format(
|
Log.d(TAG, String.format(
|
||||||
"New location. Latitude: %s Longitude: %s Altitude: %s",
|
"New location. Latitude: %s Longitude: %s Altitude: %s",
|
||||||
|
|
|
||||||
|
|
@ -23,4 +23,8 @@ public class LocationLoggingServiceBinder extends Binder {
|
||||||
public List<LocationEntity> allLocations(){
|
public List<LocationEntity> allLocations(){
|
||||||
return this.service.allLocations();
|
return this.service.allLocations();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendLastKnownLocationToTelegram(){
|
||||||
|
this.service.sendLastKnownLocationToTelegram();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ import io.jenetics.jpx.WayPoint;
|
||||||
public class MainActivity extends AppCompatActivity implements LocationListener, ServiceConnection {
|
public class MainActivity extends AppCompatActivity implements LocationListener, ServiceConnection {
|
||||||
private final String TAG = MainActivity.class.getName();
|
private final String TAG = MainActivity.class.getName();
|
||||||
private Button exportButton;
|
private Button exportButton;
|
||||||
|
private Button sendTelegramButton;
|
||||||
private WriteToFile writeToFile;
|
private WriteToFile writeToFile;
|
||||||
private static boolean includeComments = false;
|
private static boolean includeComments = false;
|
||||||
private static Double accuracyThreshold = 2.0;
|
private static Double accuracyThreshold = 2.0;
|
||||||
|
|
@ -54,6 +55,7 @@ public class MainActivity extends AppCompatActivity implements LocationListener,
|
||||||
});
|
});
|
||||||
|
|
||||||
exportButton = findViewById(R.id.buttonExport);
|
exportButton = findViewById(R.id.buttonExport);
|
||||||
|
sendTelegramButton = findViewById(R.id.buttonSendTelegram);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -89,6 +91,12 @@ public class MainActivity extends AppCompatActivity implements LocationListener,
|
||||||
LocationLoggingServiceBinder binder = (LocationLoggingServiceBinder) iBinder;
|
LocationLoggingServiceBinder binder = (LocationLoggingServiceBinder) iBinder;
|
||||||
binder.subscribeToLocationUpdates(this);
|
binder.subscribeToLocationUpdates(this);
|
||||||
|
|
||||||
|
if(sendTelegramButton != null){
|
||||||
|
sendTelegramButton.setOnClickListener(v -> {
|
||||||
|
binder.sendLastKnownLocationToTelegram();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if(exportButton != null){
|
if(exportButton != null){
|
||||||
exportButton.setOnClickListener(v -> {
|
exportButton.setOnClickListener(v -> {
|
||||||
Log.d(TAG, "Export button clicked.");
|
Log.d(TAG, "Export button clicked.");
|
||||||
|
|
|
||||||
|
|
@ -26,5 +26,13 @@
|
||||||
android:text="Export"
|
android:text="Export"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="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>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
Loading…
Reference in a new issue