Search in sources :

Example 86 with ActionListener

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

the class AndroidImplementation method capturePhoto.

@Override
public void capturePhoto(ActionListener response) {
    if (getActivity() == null) {
        throw new RuntimeException("Cannot capture photo in background mode");
    }
    if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to take a picture")) {
        return;
    }
    callback = new EventDispatcher();
    callback.addListener(response);
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File newFile = getOutputMediaFile(false);
    Uri imageUri = Uri.fromFile(newFile);
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
    String lastImageID = getLastImageId();
    Storage.getInstance().writeObject("imageUri", newFile.getAbsolutePath() + ";" + lastImageID);
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    this.getActivity().startActivityForResult(intent, CAPTURE_IMAGE);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Uri(android.net.Uri)

Example 87 with ActionListener

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

the class CodenameOneView method handleSizeChange.

public void handleSizeChange(int w, int h) {
    if (!drawing) {
        if ((this.width != w && (this.width < w || this.height < h)) || (bitmap.getHeight() < h)) {
            this.initBitmaps(w, h);
        }
    }
    if (this.width == w && this.height == h) {
        return;
    }
    this.width = w;
    this.height = h;
    Log.d("Codename One", "sizechanged: " + width + " " + height + " " + this);
    if (this.implementation.getCurrentForm() == null) {
        /**
         * make sure a form has been set before we can send events to the
         * EDT. if we send events before the form has been set we might
         * deadlock!
         */
        return;
    }
    if (InPlaceEditView.isEditing()) {
        final Form f = this.implementation.getCurrentForm();
        ActionListener sizeChanged = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                CodenameOneView.this.implementation.getActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        InPlaceEditView.reLayoutEdit();
                        InPlaceEditView.scrollActiveTextfieldToVisible();
                    }
                });
                f.removeSizeChangedListener(this);
            }
        };
        f.addSizeChangedListener(sizeChanged);
    }
    Display.getInstance().sizeChanged(w, h);
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 88 with ActionListener

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

the class AutoCompleteTextField method addPopup.

private void addPopup() {
    final Form f = getComponentForm();
    popup.removeAll();
    popup.setVisible(false);
    popup.setEnabled(false);
    filter(getText());
    final com.codename1.ui.List l = new com.codename1.ui.List(getSuggestionModel());
    if (getMinimumElementsShownInPopup() > 0) {
        l.setMinElementHeight(getMinimumElementsShownInPopup());
    }
    l.setScrollToSelected(false);
    l.setItemGap(0);
    for (ActionListener al : listeners) {
        l.addActionListener(al);
    }
    if (completionRenderer == null) {
        ((DefaultListCellRenderer<String>) l.getRenderer()).setShowNumbers(false);
    } else {
        l.setRenderer(completionRenderer);
    }
    l.setUIID("AutoCompleteList");
    l.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            pickedText = (String) l.getSelectedItem();
            setParentText(pickedText);
            fireActionEvent();
            // relaunch text editing if we are still editing
            if (Display.getInstance().isTextEditing(AutoCompleteTextField.this)) {
                Display.getInstance().editString(AutoCompleteTextField.this, getMaxSize(), getConstraint(), (String) l.getSelectedItem());
            }
            popup.setVisible(false);
            popup.setEnabled(false);
            f.repaint();
        }
    });
    byte[] units = popup.getStyle().getMarginUnit();
    if (units != null) {
        units[Component.LEFT] = Style.UNIT_TYPE_PIXELS;
        units[Component.TOP] = Style.UNIT_TYPE_PIXELS;
        popup.getAllStyles().setMarginUnit(units);
    }
    popup.getAllStyles().setMargin(LEFT, Math.max(0, getAbsoluteX()));
    int popupHeight = calcPopuupHeight(l);
    popup.setPreferredW(getWidth());
    popup.setHeight(popupHeight);
    popup.setWidth(getWidth());
    popup.addComponent(l);
    popup.layoutContainer();
    // block the reflow of this popup, which can cause painting problems
    dontCalcSize = true;
    if (f != null) {
        if (popup.getParent() == null) {
            Container lay = f.getLayeredPane(AutoCompleteTextField.this.getClass(), true);
            lay.setLayout(new LayeredLayout());
            Container wrapper = new Container();
            wrapper.add(popup);
            lay.addComponent(wrapper);
        }
        f.revalidate();
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) ActionListener(com.codename1.ui.events.ActionListener) ArrayList(java.util.ArrayList) LayeredLayout(com.codename1.ui.layouts.LayeredLayout)

Example 89 with ActionListener

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

the class SMSShare method share.

/**
 * {@inheritDoc}
 */
public void share(final String toShare) {
    final Form currentForm = Display.getInstance().getCurrent();
    final Form contactsForm = new Form("Contacts");
    contactsForm.setScrollable(false);
    contactsForm.setLayout(new BorderLayout());
    contactsForm.addComponent(BorderLayout.CENTER, new Label("Please wait..."));
    contactsForm.show();
    Display.getInstance().startThread(new Runnable() {

        public void run() {
            String[] ids = ContactsManager.getAllContacts();
            if (ids == null || ids.length == 0) {
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        Dialog.show("Failed to Share", "No Contacts Found", "Ok", null);
                        currentForm.showBack();
                    }
                });
                return;
            }
            ContactsModel model = new ContactsModel(ids);
            final List contacts = new List(model);
            contacts.setRenderer(createListRenderer());
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    contacts.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            final ShareForm[] f = new ShareForm[1];
                            final Hashtable contact = (Hashtable) contacts.getSelectedItem();
                            f[0] = new ShareForm(contactsForm, "Send SMS", (String) contact.get("phone"), toShare, new ActionListener() {

                                public void actionPerformed(ActionEvent evt) {
                                    try {
                                        Display.getInstance().sendSMS(f[0].getTo(), f[0].getMessage());
                                    } catch (IOException ex) {
                                        Log.e(ex);
                                        System.out.println("failed to send sms to " + (String) contact.get("phone"));
                                    }
                                    finish();
                                }
                            });
                            f[0].show();
                        }
                    });
                    contactsForm.addComponent(BorderLayout.CENTER, contacts);
                    Command back = new Command("Back") {

                        public void actionPerformed(ActionEvent evt) {
                            currentForm.showBack();
                        }
                    };
                    contactsForm.addCommand(back);
                    contactsForm.setBackCommand(back);
                    contactsForm.revalidate();
                }
            });
        }
    }, "SMS Thread").start();
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) Hashtable(java.util.Hashtable) IOException(java.io.IOException) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) ContactsModel(com.codename1.contacts.ContactsModel)

Example 90 with ActionListener

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

the class FacebookShare method share.

/**
 * {@inheritDoc}
 */
public void share(String text, final String image, final String mime) {
    final ShareForm[] f = new ShareForm[1];
    if (image == null) {
        f[0] = new ShareForm(getOriginal(), "Post on My Wall", null, text, new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                try {
                    InfiniteProgress inf = new InfiniteProgress();
                    final Dialog progress = inf.showInifiniteBlocking();
                    FaceBookAccess.getInstance().addResponseCodeListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            NetworkEvent ne = (NetworkEvent) evt;
                            int code = ne.getResponseCode();
                            FaceBookAccess.getInstance().removeResponseCodeListener(this);
                            progress.dispose();
                            Dialog.show("Failed to Share", "for some reason sharing has failed, try again later.", "Ok", null);
                            finish();
                        }
                    });
                    FaceBookAccess.getInstance().postOnWall("me", f[0].getMessage(), new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            progress.dispose();
                            finish();
                        }
                    });
                } catch (IOException ex) {
                    Log.e(ex);
                    System.out.println("failed to share " + ex.getMessage());
                }
            }
        });
        f[0].show();
    } else {
        f[0] = new ShareForm(getOriginal(), "Post on My Wall", null, text, image, new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                InfiniteProgress inf = new InfiniteProgress();
                final Dialog progress = inf.showInifiniteBlocking();
                FaceBookAccess.getInstance().addResponseCodeListener(new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        NetworkEvent ne = (NetworkEvent) evt;
                        int code = ne.getResponseCode();
                        FaceBookAccess.getInstance().removeResponseCodeListener(this);
                        progress.dispose();
                        Dialog.show("Failed to Share", "for some reason sharing has failed, try again later.", "Ok", null);
                        finish();
                    }
                });
                MultipartRequest req = new MultipartRequest();
                req.addResponseListener(new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        progress.dispose();
                        finish();
                    }
                });
                final String endpoint = "https://graph.facebook.com/me/photos?access_token=" + token;
                req.setUrl(endpoint);
                req.addArgumentNoEncoding("message", f[0].getMessage());
                InputStream is = null;
                try {
                    is = FileSystemStorage.getInstance().openInputStream(image);
                    req.addData("source", is, FileSystemStorage.getInstance().getLength(image), mime);
                    NetworkManager.getInstance().addToQueue(req);
                } catch (IOException ioe) {
                    Log.e(ioe);
                }
            }
        });
        f[0].show();
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) InfiniteProgress(com.codename1.components.InfiniteProgress) ActionEvent(com.codename1.ui.events.ActionEvent) Dialog(com.codename1.ui.Dialog) InputStream(java.io.InputStream) NetworkEvent(com.codename1.io.NetworkEvent) IOException(java.io.IOException) MultipartRequest(com.codename1.io.MultipartRequest)

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