WIP: Add led-activation blocks.

This commit is contained in:
Sergio Martínez Portela 2020-05-27 19:56:32 +02:00
parent c60ec32e05
commit ec3b6b58cc
4 changed files with 84 additions and 5 deletions

View File

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

View File

@ -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<Integer> 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<BlockArgumentDefinition>() {{
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);
}
)
;
}
}

View File

@ -0,0 +1,13 @@
package com.codigoparallevar.minicards.types.functional;
public class Box<T> {
private T value;
public Box(T value) {
this.value = value;
}
public void set(T value) {
this.value = value;
}
}

View File

@ -25,4 +25,5 @@
<string name="bridge_service_failed_stopping">Programaker bridge stopped</string>
<string name="stop_bridge">Stop bridge</string>
<string name="start_bridge">Start bridge</string>
<string name="leds_notification">LED activated</string>
</resources>