Register for Darwin Notification and send event details via Callback - Codename One

Viewed 538

I am trying to create a test App with Codename One that listens out for displayStatus change via Darwin Notifications and sends the events via callback to the Java side. I have very very little knowledge of C and almost no knowledge of objective C so most of the code i have for that part was lifted and joined from several places in the web. I have followed the developer guide but the build fails on the cloud. So far i have done the following:

In my start method i have this:

public void start() {
    if(current != null){
        current.show();
        return;
    }
    Form hi = new Form("Xerveur", new LayeredLayout());

    hi.add(buildRootContainer()).add(buildRootChildContainer());

    Display.getInstance().callSerially(hi::show);
    registerForNativeCallback();
}

This registers for the native interface:

private void registerForNativeCallback(){
    NativeListener listener = NativeLookup.create(NativeListener.class);
    if( listener != null && listener.isSupported() ){
        Log.p("Setup Event Listener returned: " + listener.setupEventListener());
    }
}

The above code snippets are in the main class file. The NativeListener interface is simple:

public interface NativeListener extends NativeInterface {
    public boolean setupEventListener();
}

Now i have a simple callback class which is supposed to receive a string, having the required information from the native side:

public class NativeCallback {
    public static void receive(String payload){
        Log.p(payload);
    }
}

These are the contents of the ".m" file that was generated (the ".h" file is unmodified from what was auto-generated) and i edited:

#import "ca_ratelsoft_testing_testapp2_NativeListenerImpl.h"
#include "ca_ratelsoft_testing_testapp2_NativeCallback.h"
#include "CodenameOne_GLViewController.h"
#include <unistd.h>       // good idea in general
#include <stdlib.h>       // good idea in general

#include <CoreFoundation/CoreFoundation.h>
#include <notify.h>       // for all notifications


@implementation ca_ratelsoft_testing_testapp2_NativeListenerImpl

-(BOOL)setupEventListener{
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                        NULL, // observer
                                        displayStatusChanged, // callback
                                        CFSTR("com.apple.springboard.displayStatus"), // event name
                                        NULL, // object
                                        CFNotificationSuspensionBehaviorDeliverImmediately);

    return YES;
}

static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
    NSLog(@"event received!");
    // you might try inspecting the `userInfo` dictionary, to see
    //  if it contains any useful info
    if (userInfo != nil) {
        const void * keys;
        const void * values;
        NSString *payload = @"displayStatus$$$";    //delimeter: $$$
        CFDictionaryGetKeysAndValues(userInfo, &keys, &values);

        //key1=value1;key2=value2;

        for (int i = 0; i < CFDictionaryGetCount(userInfo); i++) {
            const char * keyStr = CFStringGetCStringPtr((CFStringRef)&keys[i], CFStringGetSystemEncoding());
            const char * valStr = CFStringGetCStringPtr((CFStringRef)&values[i], CFStringGetSystemEncoding());
            if( i > 0 )
                payload = [payload stringByAppendingString:@";"];
            payload = [payload stringByAppendingString:@(keyStr)];
            payload = [payload stringByAppendingString:@"="];
            payload = [payload stringByAppendingString:@(valStr)];
        }

        ca_ratelsoft_testing_testapp2_NativeCallback_receive___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG fromNSString(CN1_THREAD_GET_STATE_PASS_ARG payload));
    }
}

-(BOOL)isSupported{
    return YES;
}

@end

I get the following error when building a debug iOS app: https://www.dropbox.com/s/sq8f00nzf445gp0/f9e35511-c43f-4bb6-854a-f513ec8e3820-1500397464685-error.txt?dl=0

1 Answers
Related