1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 12:56:49 +01:00

Huami: Use system time for GPS timestamps

This commit is contained in:
José Rebelo 2022-08-27 14:12:47 +01:00 committed by Gitea
parent 643f8ef931
commit 9f0169542e
4 changed files with 26 additions and 8 deletions

View File

@ -16,21 +16,15 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.externalevents.gps; package nodomain.freeyourgadget.gadgetbridge.externalevents.gps;
import android.content.Context;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.SystemClock;
import android.os.Looper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.devices.EventHandler; import nodomain.freeyourgadget.gadgetbridge.devices.EventHandler;
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.webview.CurrentPosition;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
/** /**
* An implementation of a {@link LocationListener} that forwards the location updates to the * An implementation of a {@link LocationListener} that forwards the location updates to the
@ -51,6 +45,9 @@ public class GBLocationListener implements LocationListener {
public void onLocationChanged(final Location location) { public void onLocationChanged(final Location location) {
LOG.info("Location changed: {}", location); LOG.info("Location changed: {}", location);
// Correct the location time
location.setTime(getLocationTimestamp(location));
// The location usually doesn't contain speed, compute it from the previous location // The location usually doesn't contain speed, compute it from the previous location
if (previousLocation != null && !location.hasSpeed()) { if (previousLocation != null && !location.hasSpeed()) {
long timeInterval = (location.getTime() - previousLocation.getTime()) / 1000L; long timeInterval = (location.getTime() - previousLocation.getTime()) / 1000L;
@ -75,6 +72,11 @@ public class GBLocationListener implements LocationListener {
@Override @Override
public void onStatusChanged(final String provider, final int status, final Bundle extras) { public void onStatusChanged(final String provider, final int status, final Bundle extras) {
LOG.info("onStatusChanged: {}", provider, status); LOG.info("onStatusChanged: {}, {}", provider, status);
}
private static long getLocationTimestamp(final Location location) {
long nanosSinceLocation = SystemClock.elapsedRealtimeNanos() - location.getElapsedRealtimeNanos();
return System.currentTimeMillis() - (nanosSinceLocation / 100_000L);
} }
} }

View File

@ -21,6 +21,7 @@ import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.SystemClock;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -63,6 +64,7 @@ public class MockLocationProvider extends AbstractLocationProvider {
final Location newLocation = new Location(previousLocation); final Location newLocation = new Location(previousLocation);
newLocation.setLatitude(previousLocation.getLatitude() + coordDiff); newLocation.setLatitude(previousLocation.getLatitude() + coordDiff);
newLocation.setTime(System.currentTimeMillis()); newLocation.setTime(System.currentTimeMillis());
newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
getLocationListener().onLocationChanged(newLocation); getLocationListener().onLocationChanged(newLocation);

View File

@ -16,15 +16,19 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.externalevents.gps; package nodomain.freeyourgadget.gadgetbridge.externalevents.gps;
import android.Manifest;
import android.content.Context; import android.content.Context;
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.os.Looper; import android.os.Looper;
import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
/** /**
* A location provider that uses the phone GPS, using {@link LocationManager}. * A location provider that uses the phone GPS, using {@link LocationManager}.
*/ */
@ -42,6 +46,11 @@ public class PhoneGpsLocationProvider extends AbstractLocationProvider {
void start(final Context context) { void start(final Context context) {
LOG.info("Starting phone gps location provider"); LOG.info("Starting phone gps location provider");
if (!GB.checkPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) && !GB.checkPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)) {
GB.toast("Location permission not granted", Toast.LENGTH_SHORT, GB.ERROR);
return;
}
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(getLocationListener()); locationManager.removeUpdates(getLocationListener());
locationManager.requestLocationUpdates( locationManager.requestLocationUpdates(

View File

@ -35,6 +35,7 @@ import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -634,4 +635,8 @@ public class GB {
Intent intent = new Intent(GBApplication.ACTION_NEW_DATA); Intent intent = new Intent(GBApplication.ACTION_NEW_DATA);
LocalBroadcastManager.getInstance(GBApplication.getContext()).sendBroadcast(intent); LocalBroadcastManager.getInstance(GBApplication.getContext()).sendBroadcast(intent);
} }
public static boolean checkPermission(final Context context, final String permission) {
return ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}
} }