handleNotification method

void handleNotification(
  1. NotificationEvent event
)

Extract the details from the notification and send to Frame

Implementation

void handleNotification(NotificationEvent event) async {
  _log.fine('onData: $event');

  // filter notifications for Maps
  if (event.packageName != null &&
      event.packageName == "com.google.android.apps.maps") {
    setState(() {
      _lastEvent = event;
    });

    try {
      // send text to Frame
      String text = '${event.title}\n${event.text}\n${event.raw!["subText"]}';
      if (text != _prevText) {
        String wrappedText = TextUtils.wrapText(text, 500, 4).join('\n');
        await frame
            ?.sendMessage(TxPlainText(msgCode: 0x0a, text: wrappedText));
        _prevText = text;
      }

      if (event.hasLargeIcon!) {
        Uint8List iconBytes = event.largeIcon!;
        _log.finest('Icon bytes: ${iconBytes.length}: $iconBytes');

        if (!listEquals(iconBytes, _prevIcon)) {
          _prevIcon = iconBytes;
          // TODO if the maps icons are all 2-color bitmaps even though they're RGB(A?) bitmaps,
          // maybe we can pack them and send as an indexed file more easily than having to do quantize()? Or using Image() at all.
          final img.Image? image = img.decodeImage(iconBytes);

          // Ensure the image is loaded correctly
          if (image != null) {
            _log.fine(
                'Image: ${image.width}x${image.height}, ${image.format}, ${image.hasAlpha}, ${image.hasPalette}, ${image.length}');
            _log.finest('Image bytes: ${image.toUint8List()}');

            // quantize the image for pack/send/display to frame
            final qImage = img.quantize(image,
                numberOfColors: 4,
                method: img.QuantizeMethod.binary,
                dither: img.DitherKernel.none,
                ditherSerpentine: false);
            Uint8List qImageBytes = qImage.toUint8List();
            _log.fine(
                'QuantizedImage: ${qImage.width}x${qImage.height}, ${qImage.format}, ${qImage.hasAlpha}, ${qImage.hasPalette}, ${qImage.palette!.toUint8List()}, ${qImage.length}');
            _log.finest('QuantizedImage bytes: $qImageBytes');

            // send image message (header and image data) to Frame
            await frame?.sendMessage(TxSprite(
                msgCode: 0x0d,
                width: qImage.width,
                height: qImage.height,
                numColors: qImage.palette!.lengthInBytes ~/ 3,
                paletteData: qImage.palette!.toUint8List(),
                pixelData: qImageBytes));
          }
        }
      }
    } catch (e) {
      _log.severe('Error processing notification: $e');
    }
  }
}