From d33054ae5437db8218f81fe63414ac5eab381c53 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Sun, 20 Jul 2025 16:23:56 +0200 Subject: [PATCH] Printer: allow printing incoming text shared to Gadgetbridge Add a new NotificationType for received text. Add device specific settings to the printer coordinator, also add setting to print received text (defaults to on). --- .../activities/TextReceiverActivity.java | 2 +- .../thermalprinter/ThermalPrinterCoordinator.java | 9 +++++++++ .../gadgetbridge/model/NotificationType.java | 2 ++ .../GenericThermalPrinterSupport.java | 14 ++++++++++++++ app/src/main/res/values/strings.xml | 1 + .../res/xml/devicesettings_print_received_text.xml | 11 +++++++++++ 6 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/xml/devicesettings_print_received_text.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/TextReceiverActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/TextReceiverActivity.java index 538338cba5..89437f611d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/TextReceiverActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/TextReceiverActivity.java @@ -71,7 +71,7 @@ public class TextReceiverActivity extends AbstractGBActivity { notificationSpec.body = text; notificationSpec.sourceAppId = BuildConfig.APPLICATION_ID; notificationSpec.sourceName = appName; - notificationSpec.type = NotificationType.UNKNOWN; + notificationSpec.type = NotificationType.GADGETBRIDGE_TEXT_RECEIVER; GBApplication.deviceService().onNotification(notificationSpec); finish(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/thermalprinter/ThermalPrinterCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/thermalprinter/ThermalPrinterCoordinator.java index 9289916a59..9ed3b42003 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/thermalprinter/ThermalPrinterCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/thermalprinter/ThermalPrinterCoordinator.java @@ -12,6 +12,7 @@ import java.util.List; import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettings; import nodomain.freeyourgadget.gadgetbridge.devices.AbstractBLEDeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCardAction; import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler; @@ -79,6 +80,14 @@ public class ThermalPrinterCoordinator extends AbstractBLEDeviceCoordinator { return null; } + @Override + public DeviceSpecificSettings getDeviceSpecificSettings(GBDevice device) { + final DeviceSpecificSettings deviceSpecificSettings = new DeviceSpecificSettings(); + + deviceSpecificSettings.addRootScreen(R.xml.devicesettings_print_received_text); + return deviceSpecificSettings; + } + @Override public List getCustomActions() { return Collections.singletonList(new ControlDeviceCardAction()); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/NotificationType.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/NotificationType.java index 0efe544e8f..451545aa55 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/NotificationType.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/NotificationType.java @@ -60,6 +60,7 @@ public enum NotificationType { TUMBLR, PINTEREST, YOUTUBE, + GADGETBRIDGE_TEXT_RECEIVER, ; @@ -120,6 +121,7 @@ public enum NotificationType { case YAHOO_MAIL: return "generic_email"; case COL_REMINDER: + case GADGETBRIDGE_TEXT_RECEIVER: case UNKNOWN: default: return "generic"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/thermalprinter/GenericThermalPrinterSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/thermalprinter/GenericThermalPrinterSupport.java index dd8c8bac5c..663b238a50 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/thermalprinter/GenericThermalPrinterSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/thermalprinter/GenericThermalPrinterSupport.java @@ -2,6 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.thermalprinter; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; +import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; @@ -34,6 +35,7 @@ import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSett import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec; +import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLESingleDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.util.GB; @@ -119,6 +121,18 @@ public class GenericThermalPrinterSupport extends AbstractBTLESingleDeviceSuppor @Override public void onNotification(NotificationSpec notificationSpec) { + if(notificationSpec.type.equals(NotificationType.GADGETBRIDGE_TEXT_RECEIVER)) { + final SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress()); + if(!prefs.getBoolean("pref_printer_print_received_text", false)) { + LOG.info("Not printing received text, device preference forbids this."); + return; + } + + //title is appName + //body is the shared text + // see TextReceiverActivity.java + printImage(createBitmapFromString(notificationSpec.body), false, PrintAlignment.ALIGN_LEFT); + } // printImage(createNotificationBitmap(notificationSpec), true, PrintAlignment.ALIGN_LEFT); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 37546be1fe..0338ed3d6d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4165,4 +4165,5 @@ Restrict to specific intervals Enable the deprecated Notification Collector Monitor Service Forces the usage of a second foreground service to keep the Notification monitor alive, activate this option if Gadgetbridge stops receiving notifications.\nPlease note that it might cause a second persistent notification to be shown.\nRestart the application after toggling. + Print text shared to Gadgetbridge diff --git a/app/src/main/res/xml/devicesettings_print_received_text.xml b/app/src/main/res/xml/devicesettings_print_received_text.xml new file mode 100644 index 0000000000..670e35bd96 --- /dev/null +++ b/app/src/main/res/xml/devicesettings_print_received_text.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file