Search in sources :

Example 96 with ActionListener

use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.

the class BlackBerryImplementation method capturePhoto.

public void capturePhoto(ActionListener response) {
    EventLog.getInstance().logInformationEvent("capturePhoto");
    captureCallback = new EventDispatcher();
    captureCallback.addListener(response);
    UiApplication.getUiApplication().addFileSystemJournalListener(new FileSystemJournalListener() {

        private long lastUSN;

        public void fileJournalChanged() {
            long USN = FileSystemJournal.getNextUSN();
            for (long i = USN - 1; i >= lastUSN; --i) {
                FileSystemJournalEntry entry = FileSystemJournal.getEntry(i);
                if (entry != null) {
                    if (entry.getEvent() == FileSystemJournalEntry.FILE_ADDED) {
                        if (entry.getPath().indexOf(".jpg") != -1) {
                            lastUSN = USN;
                            String path = entry.getPath();
                            // close the camera
                            UiApplication.getUiApplication().removeFileSystemJournalListener(this);
                            try {
                                EventInjector.KeyEvent inject = new EventInjector.KeyEvent(EventInjector.KeyEvent.KEY_DOWN, Characters.ESCAPE, 0, 200);
                                inject.post();
                                inject.post();
                            } catch (Exception e) {
                            // try to close the camera
                            }
                            EventLog.getInstance().logInformationEvent("path " + path);
                            captureCallback.fireActionEvent(new ActionEvent("file://" + path));
                            captureCallback = null;
                        }
                    }
                }
            }
            lastUSN = USN;
        }
    });
    app.setWaitingForReply(true);
    synchronized (UiApplication.getEventLock()) {
        Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, new CameraArguments());
    }
}
Also used : FileSystemJournalEntry(net.rim.device.api.io.file.FileSystemJournalEntry) CameraArguments(net.rim.blackberry.api.invoke.CameraArguments) EventDispatcher(com.codename1.ui.util.EventDispatcher) FileSystemJournalListener(net.rim.device.api.io.file.FileSystemJournalListener) ActionEvent(com.codename1.ui.events.ActionEvent) IOException(java.io.IOException) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException)

Example 97 with ActionListener

use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.

the class BlackBerryImplementation method captureAudio.

public void captureAudio(ActionListener response) {
    int h = CodeModuleManager.getModuleHandle("net_rim_bb_voicenotesrecorder");
    if (h == 0) {
        throw new RuntimeException("capture audio works only if Voice Notes is installed");
    }
    captureCallback = new EventDispatcher();
    captureCallback.addListener(response);
    UiApplication.getUiApplication().addFileSystemJournalListener(new FileSystemJournalListener() {

        private long lastUSN;

        public void fileJournalChanged() {
            long USN = FileSystemJournal.getNextUSN();
            for (long i = USN - 1; i >= lastUSN; --i) {
                FileSystemJournalEntry entry = FileSystemJournal.getEntry(i);
                if (entry != null) {
                    String path = entry.getPath();
                    if (entry.getEvent() == FileSystemJournalEntry.FILE_ADDED && path.endsWith(".amr")) {
                        UiApplication.getUiApplication().removeFileSystemJournalListener(this);
                        try {
                            EventInjector.KeyEvent inject = new EventInjector.KeyEvent(EventInjector.KeyEvent.KEY_DOWN, Characters.ESCAPE, 0, 200);
                            inject.post();
                        } catch (Exception e) {
                        // try to close the voicenotesrecorder
                        }
                        captureCallback.fireActionEvent(new ActionEvent("file://" + path));
                        captureCallback = null;
                        break;
                    }
                }
            }
            lastUSN = USN;
        }
    });
    app.setWaitingForReply(true);
    ApplicationDescriptor desc = new ApplicationDescriptor(CodeModuleManager.getApplicationDescriptors(h)[0], null);
    try {
        ApplicationManager.getApplicationManager().runApplication(desc, true);
    } catch (ApplicationManagerException e) {
        EventLog.getInstance().logErrorEvent("err " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : FileSystemJournalListener(net.rim.device.api.io.file.FileSystemJournalListener) ActionEvent(com.codename1.ui.events.ActionEvent) IOException(java.io.IOException) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) FileSystemJournalEntry(net.rim.device.api.io.file.FileSystemJournalEntry) EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 98 with ActionListener

use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.

the class TestComponent method testBrowserComponent2267.

// Test for https://github.com/codenameone/CodenameOne/issues/2267
private void testBrowserComponent2267() {
    Form hi = new Form();
    String formName = "testBrowserComponent2267";
    hi.setName(formName);
    hi.setLayout(new BorderLayout());
    BrowserComponent browserComponent = new BrowserComponent();
    hi.add(BorderLayout.CENTER, browserComponent);
    final Throwable[] ex = new Throwable[1];
    final boolean[] complete = new boolean[1];
    Button loadButton = new Button("setUrl");
    String buttonName = "setUrl";
    loadButton.setName(buttonName);
    loadButton.addActionListener((ev) -> {
        ActionListener errorHandler = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                e.consume();
                ex[0] = (Throwable) e.getSource();
                complete[0] = true;
                Display.getInstance().removeEdtErrorHandler(this);
            }
        };
        try {
            Display.getInstance().addEdtErrorHandler(errorHandler);
            browserComponent.addWebEventListener("onLoad", e -> {
                Display.getInstance().removeEdtErrorHandler(errorHandler);
                complete[0] = true;
            });
            browserComponent.setURL("https://www.google.es");
        } catch (Throwable t) {
            ex[0] = t;
            complete[0] = true;
            Display.getInstance().removeEdtErrorHandler(errorHandler);
        } finally {
        // complete[0] = true;
        }
    });
    hi.add(BorderLayout.SOUTH, loadButton);
    hi.show();
    TestUtils.waitForFormName(formName, 2000);
    TestUtils.clickButtonByName(buttonName);
    while (!complete[0]) {
        Display.getInstance().invokeAndBlock(() -> {
            Util.sleep(50);
        });
    }
    String message = null;
    if (ex[0] != null) {
        message = ex[0].getMessage();
    }
    TestUtils.assertBool(ex[0] == null, "We received an exception setting the browserComponent URL: " + message);
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 99 with ActionListener

use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.

the class TestComponent method testOverflowMenuNPE.

/**
 * https://github.com/codenameone/CodenameOne/issues/2255
 */
public void testOverflowMenuNPE() {
    Form hi = new Form();
    hi.setName("testOverflowMenuNPE");
    hi.getToolbar().addCommandToOverflowMenu("Test", FontImage.createMaterial(FontImage.MATERIAL_3D_ROTATION, new Style()), new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            ToastBar.showMessage("menu", FontImage.MATERIAL_3D_ROTATION);
            hi.revalidate();
        }
    });
    hi.show();
    TestUtils.waitForFormName("testOverflowMenuNPE", 2000);
    hi.getToolbar().closeSideMenu();
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) Style(com.codename1.ui.plaf.Style)

Example 100 with ActionListener

use of com.codename1.ui.events.ActionListener in project codenameone-google-maps by codenameone.

the class MapContainer method initBrowserComponent.

private void initBrowserComponent(String htmlApiKey) {
    if (debug) {
        Log.e(new RuntimeException("Initializing Browser Component.  This stack trace is just for tracking purposes.  It is NOT a real exception"));
    }
    // System.out.println("About to check location");
    Location loc = LocationManager.getLocationManager().getLastKnownLocation();
    try {
        // if (true)return;
        // System.out.println("About to load map text");
        String str = Util.readToString(Display.getInstance().getResourceAsStream(null, "/com_codename1_googlemaps_MapContainer.html"));
        // System.out.println("Map text: "+str);
        str = StringUtil.replaceAll(str, "YOUR_API_KEY", htmlApiKey);
        // System.out.println("Finished setting API key");
        str = StringUtil.replaceAll(str, "//origin = MAPCONTAINER_ORIGIN", "origin = {lat: " + loc.getLatitude() + ", lng: " + loc.getLongitude() + "};");
        // System.out.println("Finished setting origin");
        internalBrowser.setPage(str, "/");
        if (debug) {
            Log.e(new RuntimeException("Adding onLoad Listener.  This stack trace is just for tracking purposes.  It is NOT a real exception"));
        }
        internalBrowser.addWebEventListener("onLoad", new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (debug) {
                    Log.e(new RuntimeException("Inside onLoad Listener.  This stack trace is just for tracking purposes.  It is NOT a real exception"));
                }
                // JavascriptContext ctx = new JavascriptContext(internalBrowser);
                // browserBridge.ctx = ctx;
                internalBrowser.execute("com_codename1_googlemaps_MapContainer = {}");
                internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireTapEvent = function(x,y){callback.onSuccess(x+','+y)};", new SuccessCallback<BrowserComponent.JSRef>() {

                    public void onSucess(BrowserComponent.JSRef value) {
                        String[] parts = Util.split(value.getValue(), ",");
                        int x = new Double(Double.parseDouble(parts[0])).intValue();
                        int y = new Double(Double.parseDouble(parts[1])).intValue();
                        fireTapEvent(x, y);
                    }
                });
                internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireLongPressEvent = function(x,y){callback.onSuccess(x+','+y)};", new SuccessCallback<BrowserComponent.JSRef>() {

                    public void onSucess(BrowserComponent.JSRef value) {
                        String[] parts = Util.split(value.getValue(), ",");
                        int x = new Double(Double.parseDouble(parts[0])).intValue();
                        int y = new Double(Double.parseDouble(parts[1])).intValue();
                        fireLongPressEvent(x, y);
                    }
                });
                internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireMapChangeEvent = function(zoom,lat,lon){callback.onSuccess(zoom+','+lat+','+lon)};", new SuccessCallback<BrowserComponent.JSRef>() {

                    public void onSucess(BrowserComponent.JSRef value) {
                        String[] parts = Util.split(value.getValue(), ",");
                        int zoom = Integer.parseInt(parts[0]);
                        double lat = Double.parseDouble(parts[1]);
                        double lon = Double.parseDouble(parts[2]);
                        fireMapListenerEvent(zoom, lat, lon);
                    }
                });
                internalBrowser.addJSCallback("com_codename1_googlemaps_MapContainer.fireMarkerEvent = function(id){callback.onSuccess(id)};", new SuccessCallback<BrowserComponent.JSRef>() {

                    public void onSucess(BrowserComponent.JSRef value) {
                        fireMarkerEvent(value.getInt());
                    }
                });
                internalBrowser.execute("callback.onSuccess(com_codename1_googlemaps_MapContainer_bridge)", new SuccessCallback<BrowserComponent.JSRef>() {

                    public void onSucess(BrowserComponent.JSRef value) {
                        if ("null".equals(value.getValue()) || value.getJSType() == BrowserComponent.JSType.UNDEFINED) {
                            internalBrowser.execute("com_codename1_googlemaps_MapContainer_onReady=function(bridge){callback.onSuccess(bridge)};", new SuccessCallback<BrowserComponent.JSRef>() {

                                public void onSucess(BrowserComponent.JSRef value) {
                                    browserBridge.ready = true;
                                }
                            });
                        } else {
                            browserBridge.ready = true;
                        }
                    }
                });
                // /System.out.println("Bridge is ready");
                if (debug) {
                    Log.p("About to fire browserBridge.ready(null) event to kick things off");
                }
                browserBridge.ready(null);
            }
        });
        return;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : SuccessCallback(com.codename1.util.SuccessCallback) ActionEvent(com.codename1.ui.events.ActionEvent) JSRef(com.codename1.ui.BrowserComponent.JSRef) IOException(java.io.IOException) Point(com.codename1.ui.geom.Point) JSRef(com.codename1.ui.BrowserComponent.JSRef) ActionListener(com.codename1.ui.events.ActionListener) BrowserComponent(com.codename1.ui.BrowserComponent) Location(com.codename1.location.Location)

Aggregations

ActionEvent (com.codename1.ui.events.ActionEvent)61 ActionListener (com.codename1.ui.events.ActionListener)61 IOException (java.io.IOException)25 BorderLayout (com.codename1.ui.layouts.BorderLayout)20 EventDispatcher (com.codename1.ui.util.EventDispatcher)16 Hashtable (java.util.Hashtable)14 NetworkEvent (com.codename1.io.NetworkEvent)13 Form (com.codename1.ui.Form)13 Vector (java.util.Vector)11 Container (com.codename1.ui.Container)10 Button (com.codename1.ui.Button)8 ActionEvent (java.awt.event.ActionEvent)8 ActionListener (java.awt.event.ActionListener)8 Dialog (com.codename1.ui.Dialog)7 EncodedImage (com.codename1.ui.EncodedImage)6 Image (com.codename1.ui.Image)6 Label (com.codename1.ui.Label)6 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)6 MediaException (javax.microedition.media.MediaException)6 RecordStoreException (javax.microedition.rms.RecordStoreException)6