Prevent crash on devices without GPS

This commit is contained in:
José Rebelo
2026-05-16 18:09:57 +01:00
parent 37ad178dac
commit d1f5531217
7 changed files with 28 additions and 14 deletions
@@ -93,6 +93,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst; import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.externalevents.gps.GBLocationService;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryConfig; import nodomain.freeyourgadget.gadgetbridge.model.BatteryConfig;
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec; import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
@@ -683,6 +684,12 @@ public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment i
addPreferenceHandlerFor(PREF_CAMERA_REMOTE); addPreferenceHandlerFor(PREF_CAMERA_REMOTE);
addPreferenceHandlerFor(PREF_SCREEN_LIFT_WRIST); addPreferenceHandlerFor(PREF_SCREEN_LIFT_WRIST);
final Preference sendGpsToBandPref = findPreference(PREF_WORKOUT_SEND_GPS_TO_BAND);
if (sendGpsToBandPref != null && !GBLocationService.isGpsSupportedAndEnabled()) {
sendGpsToBandPref.setEnabled(false);
sendGpsToBandPref.setSummary(R.string.phone_gps_not_available);
}
addPreferenceHandlerFor(PREF_BATTERY_POLLING_ENABLE); addPreferenceHandlerFor(PREF_BATTERY_POLLING_ENABLE);
addPreferenceHandlerFor(PREF_BATTERY_POLLING_INTERVAL); addPreferenceHandlerFor(PREF_BATTERY_POLLING_INTERVAL);
addPreferenceHandlerFor(PREF_TIME_SYNC); addPreferenceHandlerFor(PREF_TIME_SYNC);
@@ -21,6 +21,7 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.location.LocationManager;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@@ -37,6 +38,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import nodomain.freeyourgadget.gadgetbridge.BuildConfig; import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
@@ -86,18 +88,17 @@ public class GBLocationService extends BroadcastReceiver {
LOG.debug("Starting location provider {} for {}", providerType, device.getAliasOrName()); LOG.debug("Starting location provider {} for {}", providerType, device.getAliasOrName());
if (!providersByDevice.containsKey(device)) {
providersByDevice.put(device, new ArrayList<>());
}
updateNotification();
final List<GBLocationProvider> existingProviders = providersByDevice.get(device);
final GBLocationListener locationListener = new GBLocationListener(device); final GBLocationListener locationListener = new GBLocationListener(device);
final GBLocationProvider locationProvider = providerType.newInstance(context, locationListener); final GBLocationProvider locationProvider = providerType.newInstance(context, locationListener);
try {
locationProvider.start(updateInterval); locationProvider.start(updateInterval);
} catch (final Exception e) {
LOG.error("Failed to start location provider {} for {}", providerType, device.getAliasOrName(), e);
return;
}
final List<GBLocationProvider> existingProviders = providersByDevice.computeIfAbsent(device, ignored -> new ArrayList<>());
Objects.requireNonNull(existingProviders).add(locationProvider); Objects.requireNonNull(existingProviders).add(locationProvider);
updateNotification();
return; return;
case ACTION_STOP: case ACTION_STOP:
if (device != null) { if (device != null) {
@@ -144,6 +145,11 @@ public class GBLocationService extends BroadcastReceiver {
updateNotification(); updateNotification();
} }
public static boolean isGpsSupportedAndEnabled() {
final LocationManager locationManager = (LocationManager) GBApplication.getContext().getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
public static void start(final Context context, public static void start(final Context context,
@NonNull final GBDevice device, @NonNull final GBDevice device,
final GBLocationProviderType providerType, final GBLocationProviderType providerType,
@@ -1,4 +1,4 @@
/* Copyright (C) 2022-2024 José Rebelo, LukasEdl /* Copyright (C) 2022-2026 José Rebelo, LukasEdl
This file is part of Gadgetbridge. This file is part of Gadgetbridge.
@@ -47,7 +47,7 @@ public class PhoneLocationProvider extends GBLocationProvider {
@Override @Override
public void start(final int interval) { public void start(final int interval) {
LOG.info("Starting phone gps location provider"); LOG.info("Starting phone location provider ({})", provider);
if (!GB.checkPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) && !GB.checkPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION)) { 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); GB.toast("Location permission not granted", Toast.LENGTH_SHORT, GB.ERROR);
@@ -1859,7 +1859,7 @@ public abstract class HuamiSupport extends AbstractBTLESingleDeviceSupport
final boolean sendGpsToBand = HuamiCoordinator.getWorkoutSendGpsToBand(getDevice().getAddress()); final boolean sendGpsToBand = HuamiCoordinator.getWorkoutSendGpsToBand(getDevice().getAddress());
if (workoutNeedsGps) { if (workoutNeedsGps) {
if (sendGpsToBand) { if (sendGpsToBand && GBLocationService.isGpsSupportedAndEnabled()) {
lastPhoneGpsSent = 0; lastPhoneGpsSent = 0;
sendPhoneGps(HuamiPhoneGpsStatus.SEARCHING, null); sendPhoneGps(HuamiPhoneGpsStatus.SEARCHING, null);
GBLocationService.start(getContext(), getDevice(), GBLocationProviderType.GPS, 1000); GBLocationService.start(getContext(), getDevice(), GBLocationProviderType.GPS, 1000);
@@ -155,7 +155,7 @@ public class ZeppOsWorkoutService extends AbstractZeppOsService {
final boolean sendGpsToBand = getDevicePrefs().getBoolean(DeviceSettingsPreferenceConst.PREF_WORKOUT_SEND_GPS_TO_BAND, false); final boolean sendGpsToBand = getDevicePrefs().getBoolean(DeviceSettingsPreferenceConst.PREF_WORKOUT_SEND_GPS_TO_BAND, false);
if (workoutNeedsGps) { if (workoutNeedsGps) {
if (sendGpsToBand) { if (sendGpsToBand && GBLocationService.isGpsSupportedAndEnabled()) {
lastPhoneGpsSent = 0; lastPhoneGpsSent = 0;
sendPhoneGps(HuamiPhoneGpsStatus.SEARCHING, null); sendPhoneGps(HuamiPhoneGpsStatus.SEARCHING, null);
GBLocationService.start(getContext(), getSupport().getDevice(), GBLocationProviderType.GPS, 1000); GBLocationService.start(getContext(), getSupport().getDevice(), GBLocationProviderType.GPS, 1000);
@@ -656,7 +656,7 @@ public class XiaomiHealthService extends AbstractXiaomiService {
); );
final boolean sendGpsToBand = getDevicePrefs().getBoolean(DeviceSettingsPreferenceConst.PREF_WORKOUT_SEND_GPS_TO_BAND, false); final boolean sendGpsToBand = getDevicePrefs().getBoolean(DeviceSettingsPreferenceConst.PREF_WORKOUT_SEND_GPS_TO_BAND, false);
if (!sendGpsToBand) { if (!sendGpsToBand || !GBLocationService.isGpsSupportedAndEnabled()) {
getSupport().sendCommand( getSupport().sendCommand(
"send location disabled", "send location disabled",
XiaomiProto.Command.newBuilder() XiaomiProto.Command.newBuilder()
+1
View File
@@ -78,6 +78,7 @@
<string name="all_satellites">All Satellites</string> <string name="all_satellites">All Satellites</string>
<string name="speed_first">Speed first</string> <string name="speed_first">Speed first</string>
<string name="accuracy_first">Accuracy First</string> <string name="accuracy_first">Accuracy First</string>
<string name="phone_gps_not_available">Phone GPS is not available</string>
<string name="device_card_activity_card_title">Activity info on device card</string> <string name="device_card_activity_card_title">Activity info on device card</string>
<string name="device_card_activity_card_title_summary">Choose what activity details are displayed on device card</string> <string name="device_card_activity_card_title_summary">Choose what activity details are displayed on device card</string>