Compare commits
No commits in common. "telegram" and "dev" have entirely different histories.
|
|
@ -54,6 +54,5 @@ 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,9 +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_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,10 +9,7 @@ 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.net.ConnectivityManager;
|
import android.location.LocationProvider;
|
||||||
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;
|
||||||
|
|
@ -20,28 +17,14 @@ 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() {
|
||||||
}
|
}
|
||||||
|
|
@ -78,31 +61,10 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
@ -114,18 +76,6 @@ 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.");
|
||||||
|
|
@ -138,72 +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) {
|
|
||||||
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,8 +23,4 @@ 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,7 +32,6 @@ 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;
|
||||||
|
|
@ -55,7 +54,6 @@ public class MainActivity extends AppCompatActivity implements LocationListener,
|
||||||
});
|
});
|
||||||
|
|
||||||
exportButton = findViewById(R.id.buttonExport);
|
exportButton = findViewById(R.id.buttonExport);
|
||||||
sendTelegramButton = findViewById(R.id.buttonSendTelegram);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -91,12 +89,6 @@ 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,13 +26,5 @@
|
||||||
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