1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-29 05:16:51 +01:00

Added loyalty cards support for the banglejs

This commit is contained in:
Gabriele Monaco 2023-08-20 11:14:03 +02:00
parent 8c48a49d3a
commit 89f57243cf
2 changed files with 54 additions and 0 deletions

View File

@ -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);

View File

@ -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<LoyaltyCard> filterSupportedCards(final List<LoyaltyCard> cards) {
final List<LoyaltyCard> 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<LoyaltyCard> cards) {
final List<LoyaltyCard> 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 {