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")
|
||||
// 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")
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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" />
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -23,4 +23,8 @@ public class LocationLoggingServiceBinder extends Binder {
|
|||
public List<LocationEntity> 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 {
|
||||
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.");
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
Loading…
Reference in a new issue