Compare commits
6 commits
507878e10a
...
6cefb98c9b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cefb98c9b | ||
|
|
4734104bd2 | ||
|
|
75a73d97fd | ||
|
|
71a1456611 | ||
|
|
7bdd8fc0b3 | ||
|
|
79fd58d419 |
|
|
@ -50,4 +50,6 @@ dependencies {
|
||||||
|
|
||||||
// optional - Guava support for Room, including Optional and ListenableFuture
|
// optional - Guava support for Room, including Optional and ListenableFuture
|
||||||
implementation("androidx.room:room-guava:$room_version")
|
implementation("androidx.room:room-guava:$room_version")
|
||||||
|
|
||||||
|
implementation("com.github.pengrad:java-telegram-bot-api:7.9.1")
|
||||||
}
|
}
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
<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_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" />
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,20 @@ import androidx.core.app.ServiceCompat;
|
||||||
import androidx.core.location.LocationManagerCompat;
|
import androidx.core.location.LocationManagerCompat;
|
||||||
import androidx.room.Room;
|
import androidx.room.Room;
|
||||||
|
|
||||||
|
import com.pengrad.telegrambot.TelegramBot;
|
||||||
|
import com.pengrad.telegrambot.model.Chat;
|
||||||
|
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;
|
||||||
|
|
||||||
public LocationLoggingService() {
|
public LocationLoggingService() {
|
||||||
}
|
}
|
||||||
|
|
@ -88,8 +97,65 @@ 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){
|
||||||
|
String botToken = getResources().getString(R.string.telegramToken);
|
||||||
|
String telegramChat = getResources().getString(R.string.telegramChat);
|
||||||
|
TelegramBot bot = new TelegramBot(botToken);
|
||||||
|
|
||||||
|
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",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue