Android activity: Generating a string value with an flow-based node system - java

I want to create an activity in which you can insert/remove/move/connect nodes between each others and based on them to generate a string value that would be later sent through Bluetooth to an other device.
Something like this
And the resulting string should look like:
`"do[i<0-2>]:
{case[i]:
{0:"Hello ",1:"World",2:"!"}
}"
My problem is that I have no idea how to start creating the view where the nodes will be placed and the nodes themselves
I think that the "workspace" should be just a simple empty view where you can pan and zoom in/out
But for the nodes I have no idea where to start because they need to be able to have multiple inputs/outputs... maybe I need to create a custom veiw/component but like i said :( i don't know how to start
Thanks for the help in advance!
EDIT:
I have decided to use Google's Blockly to generate the string, I have customized the block the way I need to generate the string, but I can't figure it out how to get the "code" generated as a string so I can use it later... does anyone has an idea?

Blockly for Android uses a CodeGenerationRequest.CodeGeneratorCallback to pass the code string back to the application.
See this example from the TurtleActivity:
private final CodeGenerationRequest.CodeGeneratorCallback mCodeGeneratorCallback =
new CodeGenerationRequest.CodeGeneratorCallback() {
#Override
public void onFinishCodeGeneration(final String generatedCode) {
// Sample callback.
Log.i(TAG, "generatedCode:\n" + generatedCode);
Toast.makeText(getApplicationContext(), generatedCode,
Toast.LENGTH_LONG).show();
mHandler.post(new Runnable() {
#Override
public void run() {
String encoded = "Turtle.execute("
+ JavascriptUtil.makeJsString(generatedCode) + ")";
mTurtleWebview.loadUrl("javascript:" + encoded);
}
});
}
};

Related

Getting LiveStreamManager error -3 in DJI Mobile SDK when trying to stream to custom RTMP?

I'm trying to implement a app that sends live video from drone to my custom rtmp server. When I use de LiveStreamManager from DJI Mobile SDK it gives me error code -3, and the stream do not start. How can I use this API?
My app registers successfully, I can setup missions, and get telemetry from drone. But when I try to use the LiveStreamManeger it won't work no matter what. Even by implementing exactly the way it is implemented in Sample Code, it does not work. Documentation in DJI API reference seems to be missing a few methods as well.
Here is my implementation
private void setupLiveStream() {
DJISDKManager.getInstance().getLiveStreamManager().registerListener(listener);
initListener();
DJISDKManager.getInstance().getLiveStreamManager().setAudioStreamingEnabled(false);
DJISDKManager.getInstance().getLiveStreamManager().setVideoSource(LiveStreamManager.LiveStreamVideoSource.Primary);
liveURL = "rtmp://mycustomrtmp.com/drone/live_testDJI";
}
private void initListener() {
listener = new LiveStreamManager.OnLiveChangeListener() {
#Override
public void onStatusChanged(int i) {
setResultToToast("status changed : " + i);
}
};
}
private void StartStreaming(){
if (!isLiveStreamManagerOn()) {
return;
}
if (DJISDKManager.getInstance().getLiveStreamManager().isStreaming()) {
setResultToToast("already started the Stream!");
return;
}
new Thread() {
#Override
public void run() {
DJISDKManager.getInstance().getLiveStreamManager().setLiveUrl(liveURL);// + vehicleID);
int result = DJISDKManager.getInstance().getLiveStreamManager().startStream();
DJISDKManager.getInstance().getLiveStreamManager().setStartTime();
setResultToToast("LiveStream Start: " + result +
"\n isVideoStreamSpeedConfigurable:" + DJISDKManager.getInstance().getLiveStreamManager().isVideoStreamSpeedConfigurable() +
"\n isLiveAudioEnabled:" + DJISDKManager.getInstance().getLiveStreamManager().isLiveAudioEnabled());
}
}.start();
}
I always get a return code -3. When I use the sample code I can get it to work. The only diference is then I call the function isVideoStreamSpeedConfigurable(), it returns true on my code, and false on sample code. But I did not see where I can set this thing to false. How should I implement LiveStreamingManager?
Answering my own question...
I've managed to solve the issue. Apparently, to be able to use the LiveStreamManager you must first call the function VideoFeeder.getPrimaryVideoFeed() somewhere in your code or it will give error code -3.
Using the Sample Code there is a class in internal.utils.VideoFeedView that can be used to this purpose
I have first declared a private property VideoFeedView.
Then on my class constructor I call the initUI function.
private VideoFeedView primaryVideoFeed;
private void initUI() {
primaryVideoFeed.registerLiveVideo(VideoFeeder.getInstance().getPrimaryVideoFeed(),true);
startStreaming();
}
I don't know if I just was lucky, but for me, the following code solved my problem. I didn't have any need for anything more, like VideoFeedView. What is the reason for using that?
I running on a mavic 2 pro and streaming 30fps 720p to youtube.
private LiveStreamManager l;
public int live_streaming_start(String live_url){
Log.d("MavicMax", "LiveStream:live_streaming_start:" + live_url);
l = DJISDKManager.getInstance().getLiveStreamManager();
l.registerListener((x)->{Log.d("MavicMax", "LiveStream callback:" + x);});
l.setVideoSource(LiveStreamManager.LiveStreamVideoSource.Primary);
l.setVideoEncodingEnabled(true);
l.setLiveUrl(live_url);
int r = 0;
r = l.startStream();
return r;
}

How do I remove my listener after finishing what I started? (JAVA)

I'm creating a media player in JavaFX. In one of my methods, I've created a way to search for metadata in a Media-file and then display it in ImageView. Works fine first time, but as soon as I want to call it again using another Media object, the image doesn't show up. I'm a bit confused and inexperienced, but I think that perhaps I need to reset/stop the listener before going to next object in line?
So my question is! How do you remove the listener when "image" has been found, what do you type to make it happen?
If you think that there's another reason why my image wont display the second time, please let me know as well.
Thanks on purpose.
private void displayAlbumCover (){
// Will start to show a blank CD
File file = new File("src/sample/images/blank_cd.jpeg");
Image image = new Image(file.toURI().toString());
albumCoverView.setImage(image);
// However if an album cover is found in the meta-data it will be displayed
ObservableMap<String,Object> meta_data=me.getMetadata();
meta_data.addListener((MapChangeListener<String, Object>) ch -> {
if(ch.wasAdded()){
String key=ch.getKey();
Object value=ch.getValueAdded();
switch(key){
case "image":
albumCoverView.setImage((Image)value);
break;
}
}
});
}
ObservableMap has removeListner method. You can keep the listener instance to variable and then remove it later.
private MapChangeListener<String, Object> listener;
private void displayAlbumCover (){
// ...
this.listener = //...
meta_data.addListener(listener);
}
private void removeListener() {
me.getMetadata().removeListener(this.listener);
}
https://docs.oracle.com/javase/8/javafx/api/javafx/collections/ObservableMap.html#removeListener-javafx.collections.MapChangeListener-

How can i make parallel or concurent API calls?

I am building an Android app (my first app actually) and it's about getting similar tracks to the one you searched for. I am using retrofit2 with rxJava and gson for my calls.
For each track i found i add the corresponding image provided by the response in an imageview, but this image is not the actual album image, it's just an image of the band. I want to have the album image which i can get from the API if i do an album search.
So is there a way to make a API call that returns the album info for each track without losing to much time loading? I want these calls to happen in parallel with each other so as to be less visible to the "user" (me).
This is the code that i use to search for the similar tracks:
private void loadSimilarTracks() {
String mbid = selectedTrack.getMbid();
String artist = selectedTrack.getmArtist();
String track = selectedTrack.getName();
searchService = new LastFMSearchService();
Flowable<List<TrackSimilar>> fetchDataObservable = null;
if(!mbid.equals("")) {
fetchDataObservable = searchService.getSimilarTracks(mbid);
}
else{
fetchDataObservable = searchService.getSimilarTracks(artist, track);
}
mCompositeSubscription.add(fetchDataObservable
.timeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSubscriber<List<TrackSimilar>>() {
#Override
public void onNext(List<TrackSimilar> tracks) {
mTracks = tracks;
similarTrackAdapter.setTrackList(tracks);
}
#Override
public void onError(Throwable e) {
Toast.makeText(getActivity(), "API CALL ERROR", Toast.LENGTH_SHORT).show();
}
#Override
public void onComplete() {
resultsView.setVisibility(View.VISIBLE);
}
})
);
}
P.S i am using the lastFM api for my info.
Thanks in advance for any response.
Your code is good, one thing that you can do is that you can segment your code into multiple functions instead of a single one and run them as separate threads.
One function that makes api calls to search for similar tracks and other to get the image and trust me that significantly improves the response. Hope it helps..

How do I show a notification in IntelliJ?

Figured out how to show one of those little notification bubble messages in the top right of the screen, answer below.
Turns out, you have to make a NotificationGroup instance, and then use that to make a Notification, and pass the notification and the Project to Notifications.Bus.notify().
public class VoiceApplicationComponentImpl implements ApplicationComponent, VoiceApplicationComponent {
...
public static final NotificationGroup GROUP_DISPLAY_ID_INFO =
new NotificationGroup("My notification group",
NotificationDisplayType.BALLOON, true);
...
void showMyMessage(String message) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
#Override
public void run() {
Notification notification = GROUP_DISPLAY_ID_INFO.createNotification(message, NotificationType.ERROR);
Project[] projects = ProjectManager.getInstance().getOpenProjects();
Notifications.Bus.notify(notification, projects[0]);
}
});
}
Note: you'll probably have a better way to get the current Project, right now I just assume there's one open project. This means my method doesn't work on startup (projects array is empty).
Another note: you'll probably not need to wrap with the invokeLater but I did, because I was calling showMyMessage in a different thread.
this will be better!
StatusBar statusBar = WindowManager.getInstance()
.getStatusBar(DataKeys.PROJECT.getData(actionEvent.getDataContext()));
JBPopupFactory.getInstance()
.createHtmlTextBalloonBuilder(htmlText, messageType, null)
.setFadeoutTime(7500)
.createBalloon()
.show(RelativePoint.getCenterOf(statusBar.getComponent()),
Balloon.Position.atRight);
reference linkļ¼š
1. original link
2. enter link description here

Java code to handle node failure in jgroups cluster

My Jgroups config file contains the protocol/config
<FD timeout="3000" max_tries="3" />
But how do I use this in the Java code. For example, if there is a cluster and when I detect a failure I want to call an external notifier service via a REST call, like /nodeDown/nodeID
I'm not able to find any java code which does this, all I see is message receive and send, is there a way I can implement this?
Thanks
Adding some more info
I have done the step of writing a RecieverAdpater and override the start, stop, send, recieve method. Please find some code here,
public void receive(Message msg) {
JGroupsDataPacket pckt = (JGroupsDataPacket) msg.getObject();
if ( pckt.getCmd().equals("cacheUpdate") ){
int uid = pckt.getAffectedUid();
cacheUpdateRoutine(uid);
}
if ( pckt.getCmd().equals("ack") ){
System.out.println("got the mesaage!");
}
logger.log(LogLevel.ERROR, "received msg from " + msg.getSrc() + ": " + msg.getObject());
}
public void send(JGroupsDataPacket pckt){
Message msg = new Message(null, null, pckt);
msg.setFlag(Message.Flag.RSVP);
try {
channel.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
I want to know where should I add code for example to handle the TimeOutException when I'm sending a message with the RSVP flag enabled. Another requirement is to know, which is the Java callback method which is called when SUSPECT(P) is triggered. I want to catch and handle the machine's going down, timout etc.
Is the viewAccepted() the only place where I can handle this? Is there a sample code around this?
Also is http://www.jgroups.org/manual/html/user-channel.html
the section 3. APIs give all java/programmatic things we can do with JGroups.
Thanks again
I found some documentation here, I think this is the class which I'm supposed to override
public interface MembershipListener {
void viewAccepted(View new_view);
void suspect(Object suspected_mbr);
void block();
void unblock();
}
OK, first off, you have a JChannel. You need to use it to register for view callbacks, like this:
JChannel ch;
ch.setReceiver(this);
'this' extends ReceiverAdapter and overrides viewAccepted():
public void viewAccepted(View view) {
// handle new view
}
To determine the members which left between views v1 and v2:
List<Address> left_mbrs=View.leftMembers(v1,v2);

Categories