WIP: Add led-activation blocks.
This commit is contained in:
parent
c60ec32e05
commit
ec3b6b58cc
@ -24,6 +24,8 @@ import com.programaker.api.ProgramakerApi;
|
|||||||
public class ProgramakerBridgeService extends Service {
|
public class ProgramakerBridgeService extends Service {
|
||||||
public static final String BridgeUserNotificationChannel = "PROGRAMAKER_BRIDGE_USER_NOTIFICATION";
|
public static final String BridgeUserNotificationChannel = "PROGRAMAKER_BRIDGE_USER_NOTIFICATION";
|
||||||
public static final CharSequence BridgeUserNotificationChannelName = "User notifications";
|
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 String BridgeStatusNotificationChannel = "PROGRAMAKER_BRIDGE_STATUS_NOTIFICATION";
|
||||||
private static CharSequence BridgeStatusNotificationChannelName = "Programaker bridge status";
|
private static CharSequence BridgeStatusNotificationChannelName = "Programaker bridge status";
|
||||||
|
@ -4,11 +4,14 @@ import android.app.Notification;
|
|||||||
import android.app.NotificationChannel;
|
import android.app.NotificationChannel;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
|
|
||||||
import com.codigoparallevar.minicards.R;
|
import com.codigoparallevar.minicards.R;
|
||||||
import com.codigoparallevar.minicards.bridge.ProgramakerBridgeService;
|
import com.codigoparallevar.minicards.bridge.ProgramakerBridgeService;
|
||||||
|
import com.codigoparallevar.minicards.types.functional.Box;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@ -24,17 +27,34 @@ public class DefaultAndroidBlocks {
|
|||||||
assert notificationManager != null;
|
assert notificationManager != null;
|
||||||
|
|
||||||
Random notificationRandom = new Random();
|
Random notificationRandom = new Random();
|
||||||
|
int ledsNotificationId = notificationRandom.nextInt();
|
||||||
|
Box<Integer> currentLedArg = new Box<>(0x00000000);
|
||||||
|
|
||||||
|
NotificationChannel channelPreparation = null;
|
||||||
|
NotificationChannel ledsChannelPreparation = null;
|
||||||
|
|
||||||
String notificationChannelId = ProgramakerBridgeService.BridgeUserNotificationChannel;
|
String notificationChannelId = ProgramakerBridgeService.BridgeUserNotificationChannel;
|
||||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
String ledsChannelId = ProgramakerBridgeService.BridgeUserLedsNotificationChannel;
|
||||||
NotificationChannel channel = new NotificationChannel(
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
channelPreparation = new NotificationChannel(
|
||||||
ProgramakerBridgeService.BridgeUserNotificationChannel,
|
ProgramakerBridgeService.BridgeUserNotificationChannel,
|
||||||
ProgramakerBridgeService.BridgeUserNotificationChannelName,
|
ProgramakerBridgeService.BridgeUserNotificationChannelName,
|
||||||
NotificationManager.IMPORTANCE_DEFAULT);
|
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()
|
return new BridgeBlockListBuilder()
|
||||||
// Notifications
|
// Notifications
|
||||||
.addOperation(
|
.addOperation(
|
||||||
@ -70,6 +90,49 @@ public class DefaultAndroidBlocks {
|
|||||||
(List<?> params) -> {
|
(List<?> params) -> {
|
||||||
notificationManager.cancelAll();
|
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);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -25,4 +25,5 @@
|
|||||||
<string name="bridge_service_failed_stopping">Programaker bridge stopped</string>
|
<string name="bridge_service_failed_stopping">Programaker bridge stopped</string>
|
||||||
<string name="stop_bridge">Stop bridge</string>
|
<string name="stop_bridge">Stop bridge</string>
|
||||||
<string name="start_bridge">Start bridge</string>
|
<string name="start_bridge">Start bridge</string>
|
||||||
|
<string name="leds_notification">LED activated</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user