From ec3b6b58cce4d1ca47bdf48835e7e69ae6ad526e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Wed, 27 May 2020 19:56:32 +0200 Subject: [PATCH] WIP: Add led-activation blocks. --- .../bridge/ProgramakerBridgeService.java | 2 + .../bridge/blocks/DefaultAndroidBlocks.java | 73 +++++++++++++++++-- .../minicards/types/functional/Box.java | 13 ++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/functional/Box.java diff --git a/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerBridgeService.java b/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerBridgeService.java index 94b37c8..3fcbfa5 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerBridgeService.java +++ b/app/src/main/java/com/codigoparallevar/minicards/bridge/ProgramakerBridgeService.java @@ -24,6 +24,8 @@ import com.programaker.api.ProgramakerApi; public class ProgramakerBridgeService extends Service { public static final String BridgeUserNotificationChannel = "PROGRAMAKER_BRIDGE_USER_NOTIFICATION"; public static final CharSequence BridgeUserNotificationChannelName = "User notifications"; + public static String BridgeUserLedsNotificationChannel = "PROGRAMAKER_BRIDGE_USER_LEDS_NOTIFICATION"; + public static CharSequence BridgeUserLedsNotificationChannelName = "User LEDs"; private static String BridgeStatusNotificationChannel = "PROGRAMAKER_BRIDGE_STATUS_NOTIFICATION"; private static CharSequence BridgeStatusNotificationChannelName = "Programaker bridge status"; diff --git a/app/src/main/java/com/codigoparallevar/minicards/bridge/blocks/DefaultAndroidBlocks.java b/app/src/main/java/com/codigoparallevar/minicards/bridge/blocks/DefaultAndroidBlocks.java index fe61797..a6c43bf 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/bridge/blocks/DefaultAndroidBlocks.java +++ b/app/src/main/java/com/codigoparallevar/minicards/bridge/blocks/DefaultAndroidBlocks.java @@ -4,11 +4,14 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; +import android.graphics.Color; +import android.os.Build; import androidx.core.app.NotificationCompat; import com.codigoparallevar.minicards.R; import com.codigoparallevar.minicards.bridge.ProgramakerBridgeService; +import com.codigoparallevar.minicards.types.functional.Box; import java.util.Collections; import java.util.LinkedList; @@ -24,17 +27,34 @@ public class DefaultAndroidBlocks { assert notificationManager != null; Random notificationRandom = new Random(); + int ledsNotificationId = notificationRandom.nextInt(); + Box currentLedArg = new Box<>(0x00000000); + + NotificationChannel channelPreparation = null; + NotificationChannel ledsChannelPreparation = null; String notificationChannelId = ProgramakerBridgeService.BridgeUserNotificationChannel; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - NotificationChannel channel = new NotificationChannel( + String ledsChannelId = ProgramakerBridgeService.BridgeUserLedsNotificationChannel; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + channelPreparation = new NotificationChannel( ProgramakerBridgeService.BridgeUserNotificationChannel, ProgramakerBridgeService.BridgeUserNotificationChannelName, NotificationManager.IMPORTANCE_DEFAULT); - notificationManager.createNotificationChannel(channel); + notificationManager.createNotificationChannel(channelPreparation); + + ledsChannelPreparation = new NotificationChannel( + ledsChannelId, + ProgramakerBridgeService.BridgeUserLedsNotificationChannelName, + NotificationManager.IMPORTANCE_DEFAULT); + + ledsChannelPreparation.enableLights(true); + + notificationManager.createNotificationChannel(ledsChannelPreparation); } - + final NotificationChannel notificationChannel = channelPreparation; + final NotificationChannel ledsChannel = ledsChannelPreparation; return new BridgeBlockListBuilder() // Notifications .addOperation( @@ -70,6 +90,49 @@ public class DefaultAndroidBlocks { (List params) -> { notificationManager.cancelAll(); } - ); + ) + // Leds + .addOperation( + "leds_turn_on", + "Set leds to (r:%1,g:%2,b:%3)", + new LinkedList() {{ + add(new BlockArgumentDefinition(BlockArgumentDefinition.Type.INT, "255")); + add(new BlockArgumentDefinition(BlockArgumentDefinition.Type.INT, "255")); + add(new BlockArgumentDefinition(BlockArgumentDefinition.Type.INT, "255")); + }}, + params -> { + if (params.size() != 3) { + throw new Exception("Expected three (3) arguments, found " + params.size()); + } + String rStr = params.get(0).toString(); + String gStr = params.get(1).toString(); + String bStr = params.get(2).toString(); + + int r = Math.min(255, Math.max(0, Integer.parseInt(rStr))); + int g = Math.min(255, Math.max(0, Integer.parseInt(gStr))); + int b = Math.min(255, Math.max(0, Integer.parseInt(bStr))); + + int ledColor = 0x7f * (1<<24) + r * (1 << 16) + g * (1 << 8) + b; + + Notification notif = new NotificationCompat + .Builder(ctx, ledsChannelId) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setContentTitle(ctx.getString(R.string.leds_notification)) + .setSmallIcon(R.drawable.ic_center_focus_weak_black) // TODO: Change icon + .setLights(Color.RED, 0, 0) + .build(); + + notif.ledARGB = Color.RED; + notif.ledOnMS = 300; + notif.ledOffMS = 300; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + ledsChannel.setLightColor(Color.RED); + } + + notificationManager.notify(ledsNotificationId, notif); + } + ) + ; } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/functional/Box.java b/app/src/main/java/com/codigoparallevar/minicards/types/functional/Box.java new file mode 100644 index 0000000..5f1e49a --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/functional/Box.java @@ -0,0 +1,13 @@ +package com.codigoparallevar.minicards.types.functional; + +public class Box { + private T value; + + public Box(T value) { + this.value = value; + } + + public void set(T value) { + this.value = value; + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 63f8b22..0f5dac5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -25,4 +25,5 @@ Programaker bridge stopped Stop bridge Start bridge + LED activated