com.codename1.system.NativeLookup Java Examples

The following examples show how to use com.codename1.system.NativeLookup. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestNatives.java    From CodenameOne with GNU General Public License v2.0 7 votes vote down vote up
public void start() {
    if(current != null){
        current.show();
        return;
    }
    Form hi = new Form("Hi World", BoxLayout.y());
    hi.add(new Label("Hi World"));
    Button test = new Button("Test");
    test.addActionListener(e->{
        MyNativeInterface ni = NativeLookup.create(MyNativeInterface.class);
        System.out.println(Arrays.toString(ni.getBytes()));
        System.out.println(Arrays.toString(ni.getInts()));
        System.out.println(Arrays.toString(ni.getDouble()));
        ni.setBytes(new byte[]{3,2,1});
        ni.setInts(new int[]{6,5,4});
        ni.setDoubles(new double[]{9, 8, 7});
    });
    hi.add(test);
    hi.show();
}
 
Example #2
Source File: ParseInstallation.java    From parse4cn1 with Apache License 2.0 6 votes vote down vote up
/**
 * (iOS only) Sets the app batch that is shown on the app icon to the specified
 * count for this installation.
 * <p>If invoked on other platforms, the badge will still be set via the REST API
 * but will not have the desired effect of badging the app icon.
 * @param count The badge count to be set
 * @throws ParseException if anything goes wrong.
 */
public void setBadge(final int count) throws ParseException {
    if (Parse.getPlatform() == Parse.EPlatform.IOS) {
        final ParsePushNative nativePush
                = (ParsePushNative) NativeLookup.create(ParsePushNative.class);
        if (nativePush != null && nativePush.isSupported()) {
            try {
                nativePush.setBadge(count);
            } catch (Exception ex) {
                throw new ParseException("Resetting badge failed."
                        + (ex != null ? " Error: " + ex.getMessage() : ""), ex);
            }
        }
    } else {
        Logger.getInstance().warn("App icon badging is an iOS-only feature. On this platform, "
                + "the badge will simply be set via the REST API");
        put(KEY_BADGE, count);
        save();
    }
}
 
Example #3
Source File: StateMachine.java    From codenameone-demos with GNU General Public License v2.0 5 votes vote down vote up
protected void onGUI1_ScanAction(Component c, ActionEvent event) {
    // If the resource file changes the names of components this call will break notifying you that you should fix the code
    super.onGUI1_ScanAction(c, event);
    System.out.println("onGUI1_ScanAction");
    final ZXingNativeCalls zx = (ZXingNativeCalls)NativeLookup.create(ZXingNativeCalls.class);
    if(zx != null && zx.isSupported()) {
        final Form f = Display.getInstance().getCurrent();
        System.out.println("zx is running");
        new Thread() {
            public void run() {
                System.out.println("scan invoked");
                zx.scan();
                while(zx.getStatus() == ZXingNativeCalls.PENDING) {
                    try {
                        sleep(300);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
                if(zx.getStatus() == ZXingNativeCalls.ERROR) {
                    f.addComponent(new Label("Got error from zxing"));
                } else {
                    f.addComponent(new TextArea("Format: " + zx.getType()));
                    f.addComponent(new TextArea("Content: " + zx.getResult()));
                }
                f.animateLayout(800);
            }
        }.start();
    }
}
 
Example #4
Source File: StateMachine.java    From codenameone-demos with GNU General Public License v2.0 5 votes vote down vote up
/**
 * this method should be used to initialize variables instead of
 * the constructor/class scope to avoid race conditions
 */
protected void initVars() {
    TestNative n = (TestNative)NativeLookup.create(TestNative.class);
    if(n != null && n.isSupported()) {
        n.hiWorld("YO!");
    }
}
 
Example #5
Source File: StateMachine.java    From codenameone-demos with GNU General Public License v2.0 5 votes vote down vote up
protected void onGUI1_AddNativeButtonAction(Component c, ActionEvent event) {
    // If the resource file changes the names of components this call will break notifying you that you should fix the code
    super.onGUI1_AddNativeButtonAction(c, event);
    try {
        NativeCalls n = (NativeCalls) NativeLookup.create(NativeCalls.class);
        if(n != null && n.isSupported()) {
            if(peer != null){
                System.out.println("Native Button already added");
                return;
            }
            PeerComponent nativeButton = n.createNativeButton(findNameForNativeButton(c.getParent()).getText());
            peer = nativeButton;
            System.out.println("onGUI1_AddNativeButtonAction got native button peer: " + nativeButton);
            if(nativeButton != null) {
                c.getComponentForm().addComponent(nativeButton);
            } else {
                c.getComponentForm().addComponent(new Label("Native interface failed to provide a button"));
            }
        } else {
            c.getComponentForm().addComponent(new Label("Native interface is not supported on this platform"));
        }
        c.getComponentForm().revalidate();
    } catch(Throwable t) {
        t.printStackTrace();
        Dialog.show("Error", "Exception during native access: " + t, "OK", null);
    }
}
 
Example #6
Source File: DrSbaitso.java    From codenameone-demos with GNU General Public License v2.0 5 votes vote down vote up
public void init(Object context) {
    Resources theme = UIManager.initFirstTheme("/theme");
    userPicture = theme.getImage("duke_iphone.png");
    tts = (TTS)NativeLookup.create(TTS.class);        
    // Pro only feature, uncomment if you have a pro subscription
    //Log.bindCrashProtection(true);
}
 
Example #7
Source File: TestNativesTest.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean runTest() throws Exception {
    MyNativeInterface ni = (MyNativeInterface)NativeLookup.create(MyNativeInterface.class);
    assertArrayEqual(new byte[]{1, 2, 3, -1}, ni.getBytes());
    assertArrayEqual(new int[]{1, 2, 3, -1}, ni.getInts());
    assertEqual(Arrays.toString(new double[]{1, 2, 3, -1}), Arrays.toString(ni.getDouble()));
    assertArrayEqual(new int[]{3, 2, 1}, ni.setInts(new int[]{3, 2, 1}));
    assertArrayEqual(new byte[]{4, 5, 6}, ni.setBytes(new byte[]{4, 5, 6}));
    assertEqual(Arrays.toString(new double[]{7, 8, 9}), Arrays.toString(ni.setDoubles(new double[]{7, 8, 9})));
    return true;
}
 
Example #8
Source File: SignIn.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
public static void doFirebase() {
    interfaceFirebase = NativeLookup.create(InterfaceFirebase.class);
    if (interfaceFirebase != null && interfaceFirebase.isSupported()) {
        Log.p("interfaceFirebase != null &* isSupported()");
        interfaceFirebase.launchFirebase();
        interfaceFirebase.logWithFirebase("good", "yes");
    }
}
 
Example #9
Source File: SimpleDateFormatTests.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean runTest() throws Exception {
    
    if (Display.getInstance().isSimulator()) {
        
        NativeTimeZoneUtil tzUtil = NativeLookup.create(NativeTimeZoneUtil.class);
        TimeZone defaultTz = TimeZone.getDefault();
        try {
            for (String tzName : new String[]{"America/Vancouver", "America/Chicago", "UTC"}) {

                tzUtil.setDefaultTimeZone(tzName);
                
                String[] data = new String[]{
                    "yyyy-MM-dd kk:mm:ss.SSS", "2018-04-26 08:04:30.511", "h:mm aa", "8:04 AM",
                    "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, PDT", "h:mm aa", "8:04 PM",
                    "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, GMT-08:00", "h:mm aa", "5:04 PM",
                    "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, GMT", "h:mm aa", "5:04 PM",
                    "dd-MMM-yy HH:mm:ss z", "26-aug-20 18:02:09 gmt"//,
                    //"EEE, dd-MMM-yy HH:mm:ss z", "wed, 26-aug-20 18:02:09 gmt"

                };
                for (int i=0; i<data.length; i+=4) {
                    java.text.DateFormat messageDateFormat0 = new java.text.SimpleDateFormat(data[i], java.util.Locale.US);
                    SimpleDateFormat messageDateFormat = new SimpleDateFormat(data[i]);
                    messageDateFormat.getDateFormatSymbols().addZoneMapping("America/Vancouver", "Pacific Standard Time", "Pacific Daylight Time", "PST", "PDT");
                    messageDateFormat.getDateFormatSymbols().addZoneMapping("America/New_York", "Eastern Standard Time", "Eastern Daylight Time", "EST", "EDT");
                    messageDateFormat.getDateFormatSymbols().addZoneMapping("America/Chicago", "Central Standard Time", "Central Daylight Time", "CST", "CDT");
                    Date when0 = messageDateFormat0.parse(data[i+1]);
                    Date when = messageDateFormat.parse(data[i+1]);
                    assertEqual(when0, when, "In timezone "+tzName+", SimpleDateFormat parse deviated from java.text version.  Parsing "+data[i+1]+" with format "+data[i]);
                    //if (!when0.equals(when)) {
                    //    throw new RuntimeException("Test "+(i/4)+" FAILED.  Expected "+when0+" but found "+when);
                    //}
                    // What is date that is associated with when?
                    DateFormat displayTimeFormat = messageDateFormat;
                    java.text.DateFormat displayTimeFormat0 = new java.text.SimpleDateFormat(data[i], java.util.Locale.US);
                    String output0 = displayTimeFormat0.format(when);
                    String output = displayTimeFormat.format(when);
                    assertEqual(output0, output, "In timezone "+tzName+" SimpleDateFormat format deviated from java.text version.  Formatting "+data[i+1]+" with format "+data[i]);

                }
            }
        } finally {
            tzUtil.setDefaultTimeZone(defaultTz.getID());
        }
    
    }
    /*
    if (true) {
        long[] timestamps = new long[]{
            1524755070511L, 1524798240000L, 1524801840000L, 1524773040000L
        };
        String[] data = new String[]{
            "yyyy-MM-dd kk:mm:ss.SSS", "2018-04-26 08:04:30.511 PDT", "h:mm aa", "8:04 AM",
            "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, PDT", "h:mm aa", "8:04 PM",
            "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, GMT-08:00", "h:mm aa", "5:04 PM",
            "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, GMT", "h:mm aa", "5:04 PM"

        };
        for (int i=0; i<data.length; i+=4) {
            //java.text.DateFormat messageDateFormat0 = new java.text.SimpleDateFormat(data[i]);
            SimpleDateFormat messageDateFormat = new SimpleDateFormat(data[i]);
            messageDateFormat.getDateFormatSymbols().addZoneMapping("America/Vancouver", "Pacific Standard Time", "Pacific Daylight Time", "PST", "PDT");
            messageDateFormat.getDateFormatSymbols().addZoneMapping("America/New_York", "Eastern Standard Time", "Eastern Daylight Time", "EST", "EDT");
            //Date when0 = messageDateFormat0.parse(data[i+1]);
            Date when = messageDateFormat.parse(data[i+1]);
            assertEqual(timestamps[i/4], when.getTime(), "SimpleDateFormat parse deviated from java.text version.  Parsing "+data[i+1]+" with format "+data[i]);
            
            // Formatting depends on the timezone, so we won't run these tests as they will turn out
            // differently depending on the timezone of the device.
            //if (!when0.equals(when)) {
            //    throw new RuntimeException("Test "+(i/4)+" FAILED.  Expected "+when0+" but found "+when);
            //}
            // What is date that is associated with when?
            //DateFormat displayTimeFormat = new SimpleDateFormat(data[i+2]);
            //java.text.DateFormat displayTimeFormat0 = new java.text.SimpleDateFormat(data[i+2]);
            //String output0 = displayTimeFormat0.format(when);
            //String output = displayTimeFormat.format(when);
            //assertEqual(output0, output, "SimpleDateFormat format deviated from java.text version.  Formatting "+data[i+1]+" with format "+data[i+2]);

        }
    }
            */
    return true;

}