From 89f57243cfd542e48551690f96788fb5f4aab183 Mon Sep 17 00:00:00 2001 From: Gabriele Monaco Date: Sun, 20 Aug 2023 11:14:03 +0200 Subject: [PATCH] Added loyalty cards support for the banglejs --- .../devices/banglejs/BangleJSCoordinator.java | 3 ++ .../banglejs/BangleJSDeviceSupport.java | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/BangleJSCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/BangleJSCoordinator.java index ee54bd9d4..704ac1c25 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/BangleJSCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/banglejs/BangleJSCoordinator.java @@ -211,6 +211,9 @@ public class BangleJSCoordinator extends AbstractBLEDeviceCoordinator { settings.add(R.xml.devicesettings_banglejs_activity); + settings.add(R.xml.devicesettings_header_apps); + settings.add(R.xml.devicesettings_loyalty_cards); + settings.add(R.xml.devicesettings_header_developer); settings.add(R.xml.devicesettings_banglejs_apploader); settings.add(R.xml.devicesettings_device_intents); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java index 7ecb6bbda..abde7e1b5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java @@ -94,6 +94,8 @@ import io.wax911.emojify.EmojiUtils; import nodomain.freeyourgadget.gadgetbridge.BuildConfig; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.capabilities.loyaltycards.BarcodeFormat; +import nodomain.freeyourgadget.gadgetbridge.capabilities.loyaltycards.LoyaltyCard; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo; @@ -1427,6 +1429,55 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { transmitActivityStatus(); } + private List filterSupportedCards(final List cards) { + final List ret = new ArrayList<>(); + for (final LoyaltyCard card : cards) { + // we hardcode here what is supported + if (card.getBarcodeFormat() == BarcodeFormat.CODE_39 || + card.getBarcodeFormat() == BarcodeFormat.CODABAR || + card.getBarcodeFormat() == BarcodeFormat.QR_CODE) { + ret.add(card); + } + } + return ret; + } + + @Override + public void onSetLoyaltyCards(final ArrayList cards) { + final List supportedCards = filterSupportedCards(cards); + try { + JSONObject encoded_cards = new JSONObject(); + JSONArray a = new JSONArray(); + for (final LoyaltyCard card : supportedCards) { + JSONObject o = new JSONObject(); + o.put("id", card.getId()); + o.put("name", renderUnicodeAsImage(cropToLength(card.getName(),40))); + o.put("value", card.getCardId()); + o.put("type", card.getBarcodeFormat().toString()); + if (card.getExpiry() != null) + o.put("expiration", card.getExpiry().getTime()/1000); + o.put("color", card.getColor()); + // we somehow cannot distinguish no balance defined with 0 P + if (card.getBalance() != null && card.getBalance().signum() != 0 + || card.getBalanceType() != null) { + // if currency is points it is not reported + String balanceType = card.getBalanceType() != null ? + card.getBalanceType().toString() : "P"; + o.put("balance", renderUnicodeAsImage(cropToLength(card.getBalance() + + " " + balanceType, 20))); + } + if (card.getNote() != "") + o.put("note", renderUnicodeAsImage(cropToLength(card.getNote(),200))); + a.put(o); + } + encoded_cards.put("t", "cards"); + encoded_cards.put("d", a); + uartTxJSON("onSetLoyaltyCards", encoded_cards); + } catch (JSONException e) { + LOG.info("JSONException: " + e.getLocalizedMessage()); + } + } + @Override public void onAddCalendarEvent(CalendarEventSpec calendarEventSpec) { try {