1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-01 19:06:06 +02:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/gps/providers/PhoneLocationProvider.java

79 lines
3.1 KiB
Java
Raw Normal View History

/* Copyright (C) 2022-2024 José Rebelo, LukasEdl
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
2024-04-25 00:32:48 +02:00
package nodomain.freeyourgadget.gadgetbridge.externalevents.gps.providers;
import android.Manifest;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Looper;
import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2024-04-25 00:32:48 +02:00
import nodomain.freeyourgadget.gadgetbridge.externalevents.gps.GBLocationProvider;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
/**
* A location provider that uses the phone GPS, using {@link LocationManager}.
*/
2024-04-25 00:32:48 +02:00
public class PhoneLocationProvider extends GBLocationProvider {
private static final Logger LOG = LoggerFactory.getLogger(PhoneLocationProvider.class);
2024-04-25 00:32:48 +02:00
private final String provider;
2024-04-25 00:32:48 +02:00
private static final int INTERVAL_MIN_DISTANCE = 0;
2024-04-25 00:32:48 +02:00
public PhoneLocationProvider(final Context context, final LocationListener locationListener, final String provider) {
super(context, locationListener);
this.provider = provider;
[Banglejs] Send phone location data to banglejs, which can be used as gps data (#2992) Since the PR #2961 aswell as #2976, i pushed the changes to this pr. Original text: With this PR, the gadgetbridge app sends the current locationd data, obtained from the gps or network provider, to a connected banglejs device as an "gps" event. The bangle device can use this data instead of the internal gps data. Therefor saving battery energy, since the gps chip is one of the biggest energy consumers. Furthermore it enables the banglejs device to use the location data, based on the network with which the phone is currently connected. This would be usefull if there is no gps signal. Updates: I added a network provider so that it is possible to use the network location. I also overload the start method of GBLocationManager so that it is now possible to select which provider should be used to get the data (currently GPS or Network) and to set a interval to determine how often the update should be run. For the banglejs device i added a switch to enable the sending of gps data. I also added a setting, to set the interval on how often the gps data is being updated. This allows to throttle the updates of the gps data and therefore saving energy of the smartphone batterie. To further save energy, the app now requestes the current status of the gps from the banglejs and only sends data, if the gps of the banglejs is turned on. In the PR #2976 I also moved the settings to the device settings of the banglejs and i moved the logic to the onLocationChanged method of the GBLocationManager. Co-authored-by: Lukas <lukas.edi@gmx.net> Reviewed-on: https://codeberg.org/Freeyourgadget/Gadgetbridge/pulls/2992 Co-authored-by: LukasEdl <lukasedl@noreply.codeberg.org> Co-committed-by: LukasEdl <lukasedl@noreply.codeberg.org>
2022-12-12 08:48:19 +01:00
}
@Override
2024-04-25 00:32:48 +02:00
public void start(final int interval) {
LOG.info("Starting phone gps location provider");
2024-04-25 00:32:48 +02:00
if (!GB.checkPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) && !GB.checkPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION)) {
GB.toast("Location permission not granted", Toast.LENGTH_SHORT, GB.ERROR);
return;
}
2024-04-25 00:32:48 +02:00
final LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(getLocationListener());
locationManager.requestLocationUpdates(
2024-04-25 00:32:48 +02:00
provider,
interval > 0 ? interval : 1_000,
INTERVAL_MIN_DISTANCE,
getLocationListener(),
Looper.getMainLooper()
);
final Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
[Banglejs] Send phone location data to banglejs, which can be used as gps data (#2992) Since the PR #2961 aswell as #2976, i pushed the changes to this pr. Original text: With this PR, the gadgetbridge app sends the current locationd data, obtained from the gps or network provider, to a connected banglejs device as an "gps" event. The bangle device can use this data instead of the internal gps data. Therefor saving battery energy, since the gps chip is one of the biggest energy consumers. Furthermore it enables the banglejs device to use the location data, based on the network with which the phone is currently connected. This would be usefull if there is no gps signal. Updates: I added a network provider so that it is possible to use the network location. I also overload the start method of GBLocationManager so that it is now possible to select which provider should be used to get the data (currently GPS or Network) and to set a interval to determine how often the update should be run. For the banglejs device i added a switch to enable the sending of gps data. I also added a setting, to set the interval on how often the gps data is being updated. This allows to throttle the updates of the gps data and therefore saving energy of the smartphone batterie. To further save energy, the app now requestes the current status of the gps from the banglejs and only sends data, if the gps of the banglejs is turned on. In the PR #2976 I also moved the settings to the device settings of the banglejs and i moved the logic to the onLocationChanged method of the GBLocationManager. Co-authored-by: Lukas <lukas.edi@gmx.net> Reviewed-on: https://codeberg.org/Freeyourgadget/Gadgetbridge/pulls/2992 Co-authored-by: LukasEdl <lukasedl@noreply.codeberg.org> Co-committed-by: LukasEdl <lukasedl@noreply.codeberg.org>
2022-12-12 08:48:19 +01:00
LOG.debug("Last known gps location: {}", lastKnownLocation);
}
@Override
2024-04-25 00:32:48 +02:00
public void stop() {
LOG.info("Stopping phone gps location provider");
2024-04-25 00:32:48 +02:00
final LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(getLocationListener());
}
}