From 83118c078ed1b751528572b7ac4fad1896b9af19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Wed, 24 Dec 2025 09:22:20 +0000 Subject: [PATCH] Reduce toast spam when bluetooth is disabled (#1093) It's likely that it still triggers in the background under some situations, but at least this reduces the number of toasts from 2 to 1. --- .../service/DeviceCommunicationService.java | 11 ++++- .../service/DeviceSupportFactory.java | 45 ++++++++----------- .../AutoConnectIntervalReceiver.java | 2 +- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 658a26b45b..dd3bc117f1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -27,6 +27,7 @@ import android.Manifest; import android.annotation.SuppressLint; import android.app.ActivityManager; import android.app.Service; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; @@ -751,7 +752,15 @@ public class DeviceCommunicationService extends Service implements SharedPrefere throw e; } } else { - GB.toast(this, getString(R.string.cannot_connect, "Can't create device support"), Toast.LENGTH_SHORT, GB.ERROR); + // no device found, check transport availability and warn + final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); + if (adapter == null) { + GB.toast(this, getString(R.string.bluetooth_is_not_supported_), Toast.LENGTH_SHORT, GB.WARN); + } else if (!adapter.isEnabled()) { + GB.toast(this, getString(R.string.bluetooth_is_disabled_), Toast.LENGTH_SHORT, GB.WARN); + } else { + GB.toast(this, getString(R.string.cannot_connect, "Can't create device support"), Toast.LENGTH_SHORT, GB.ERROR); + } } } catch (Exception e) { LOG.warn("exception in connectToDevice for {}", deviceAddress, e); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java index b8ac87d15c..fba8d7dd61 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceSupportFactory.java @@ -27,7 +27,6 @@ package nodomain.freeyourgadget.gadgetbridge.service; import android.bluetooth.BluetoothAdapter; import android.content.Context; -import android.widget.Toast; import androidx.annotation.Nullable; @@ -39,7 +38,6 @@ import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleSupport; -import nodomain.freeyourgadget.gadgetbridge.util.GB; import java.lang.reflect.Constructor; import java.util.EnumSet; @@ -72,13 +70,7 @@ public class DeviceSupportFactory { deviceSupport = createClassNameDeviceSupport(device); } - if (deviceSupport != null) { - return deviceSupport; - } - - // no device found, check transport availability and warn - checkBtAvailability(); - return null; + return deviceSupport; } @Nullable @@ -92,20 +84,13 @@ public class DeviceSupportFactory { support.setContext(device, null, mContext); return support; } catch (ClassNotFoundException e) { + LOG.warn("Unable to find DeviceSupport class {} for {}", className, device.getAddress()); return null; // not a class, or not known at least } catch (Exception e) { throw new GBException("Error creating DeviceSupport instance for " + className, e); } } - private void checkBtAvailability() { - if (mBtAdapter == null) { - GB.toast(mContext.getString(R.string.bluetooth_is_not_supported_), Toast.LENGTH_SHORT, GB.WARN); - } else if (!mBtAdapter.isEnabled()) { - GB.toast(mContext.getString(R.string.bluetooth_is_disabled_), Toast.LENGTH_SHORT, GB.WARN); - } - } - private ServiceDeviceSupport createServiceDeviceSupport(GBDevice device) throws GBException { final DeviceCoordinator coordinator = device.getDeviceCoordinator(); final Class supportClass = coordinator.getDeviceSupportClass(device); @@ -114,23 +99,29 @@ public class DeviceSupportFactory { final DeviceSupport supportInstance = (DeviceSupport) supportClass.newInstance(); return new ServiceDeviceSupport(supportInstance, coordinator.getInitialFlags()); } catch (ReflectiveOperationException e) { - LOG.error("error calling DeviceSupport constructor with zero arguments"); + LOG.error("error calling DeviceSupport constructor for {} with zero arguments", device.getAddress()); throw new GBException(e); } } @Nullable private DeviceSupport createBTDeviceSupport(final GBDevice gbDevice) throws GBException { - if (mBtAdapter != null && mBtAdapter.isEnabled()) { - try { - final DeviceSupport deviceSupport = createServiceDeviceSupport(gbDevice); - deviceSupport.setContext(gbDevice, mBtAdapter, mContext); - return deviceSupport; - } catch (final Exception e) { - throw new GBException(mContext.getString(R.string.cannot_connect_bt_address_invalid_), e); - } + if (mBtAdapter == null) { + LOG.warn("Unable to create bt device support for {} - no bt adapter", gbDevice.getAddress()); + return null; + } + if (!mBtAdapter.isEnabled()) { + LOG.warn("Unable to create bt device support for {} - bt is disabled", gbDevice.getAddress()); + return null; + } + + try { + final DeviceSupport deviceSupport = createServiceDeviceSupport(gbDevice); + deviceSupport.setContext(gbDevice, mBtAdapter, mContext); + return deviceSupport; + } catch (final Exception e) { + throw new GBException(mContext.getString(R.string.cannot_connect_bt_address_invalid_), e); } - return null; } private DeviceSupport createTCPDeviceSupport(GBDevice gbDevice) throws GBException { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/AutoConnectIntervalReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/AutoConnectIntervalReceiver.java index eec0f85f06..f47514c05d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/AutoConnectIntervalReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/receivers/AutoConnectIntervalReceiver.java @@ -111,7 +111,7 @@ public class AutoConnectIntervalReceiver extends BroadcastReceiver { am.setAndAllowWhileIdle( AlarmManager.RTC_WAKEUP, Calendar.getInstance(). - getTimeInMillis() + delay * 1000, + getTimeInMillis() + delay * 1000L, pendingIntent ); mScheduled = true;