Register for Darwin Notice and Submit Event Details via Callback - Codename One

I'm trying to create a test application with Codename One that listens for displayStatus change via Darwin Notifications and dispatches events via callback to the Java side. I know very little about C and have almost no knowledge of objective C, so most of the code I have for this part has been stripped and linked to several places on the internet. I followed the developer guide but the build doesn't work in the cloud. So far I have done the following:

In my startup method, I have the following:

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 is the register 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 that should receive a string that has the required information from the inside:

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

      

This is the content of the generated ".m" file (the ".h" file has not been modified from what was automatically 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

      

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

+3


source to share


1 answer


Let's start with an error first, if you scroll down the page you will see NativeListenerImpl.o

. If you search NativeListenerImpl

in the file, you will see the compilation code for this and the actual error right below it:

src/ca_ratelsoft_testing_testapp2_NativeListenerImpl.m:2:10: fatal error: 'ca_ratelsoft_testing_testapp2_NativeCallback.h' file not found
#include "ca_ratelsoft_testing_testapp2_NativeCallback.h"
         ^
1 error generated.

      

This is because our optimizer wants to remove unused code anymore and cannot find use of a callback. You can solve this problem by adding the following code to your main class:



boolean fakeVariable;

public void init(Object o) {
  // ... rest of code

  if(fakeVariable) {
      NativeCallback.receive(null);
  }
}

      

It is important. Don't make the variable private !!!

The variable is protected by the package and will always be false so that the code never appears. In theory, some code can change this flag so that the optimizer cannot detect it and will be forced to leave this code in place.

+1


source







All Articles