Compare commits

..

No commits in common. "6cefb98c9bb298bf78efbd61c9ec01a8797f0b1a" and "507878e10a18643229aa9f0b660f534db3f5a8c5" have entirely different histories.

3 changed files with 0 additions and 69 deletions

View file

@ -50,6 +50,4 @@ 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")
} }

View file

@ -2,7 +2,6 @@
<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" />

View file

@ -20,20 +20,11 @@ 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() {
} }
@ -97,65 +88,8 @@ 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",