Search in sources :

Example 11 with AsyncResource

use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.

the class LoadingTextAnimationSample method showForm.

private void showForm() {
    Form f = new Form("Hello", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_SCALE));
    Form prev = CN.getCurrentForm();
    Toolbar tb = new Toolbar();
    f.setToolbar(tb);
    tb.addCommandToLeftBar("Back", null, evt -> {
        prev.showBack();
    });
    SpanLabel profileText = new SpanLabel();
    profileText.setText("placeholder");
    f.add(BorderLayout.CENTER, profileText);
    // Replace the label by a CircleProgress to indicate that it is loading.
    LoadingTextAnimation.markComponentLoading(profileText);
    Button next = new Button("Next");
    next.addActionListener(e -> {
        showLabelTest();
    });
    f.add(BorderLayout.SOUTH, next);
    AsyncResource<MyData> request = fetchDataAsync();
    request.ready(data -> {
        profileText.setText(data.getProfileText());
        // Replace the progress with the nameLabel now that
        // it is ready, using a fade transition
        LoadingTextAnimation.markComponentReady(profileText, CommonTransitions.createFade(300));
    });
    f.show();
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) Form(com.codename1.ui.Form) Button(com.codename1.ui.Button) SpanLabel(com.codename1.components.SpanLabel) Toolbar(com.codename1.ui.Toolbar)

Example 12 with AsyncResource

use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.

the class BillingSupport method runWithConnection.

private AsyncResource runWithConnection(final Runnable r) {
    final AsyncResource out = new AsyncResource();
    requireConnection().ready(new SuccessCallback() {

        @Override
        public void onSucess(Object arg0) {
            r.run();
            out.complete(true);
        }
    }).except(new SuccessCallback() {

        public void onSucess(Object arg0) {
            out.error((Throwable) arg0);
        }
    });
    return out;
}
Also used : SuccessCallback(com.codename1.util.SuccessCallback) JSONObject(org.json.JSONObject) AsyncResource(com.codename1.util.AsyncResource)

Example 13 with AsyncResource

use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.

the class CircleProgressSample method showForm.

private void showForm() {
    Form f = new Form("Hello", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
    Form prev = CN.getCurrentForm();
    Toolbar tb = new Toolbar();
    f.setToolbar(tb);
    tb.addCommandToLeftBar("Back", null, evt -> {
        prev.showBack();
    });
    Label nameLabel = new Label();
    $(nameLabel).setAlignment(CENTER);
    nameLabel.setText("placeholder");
    f.add(BorderLayout.CENTER, nameLabel);
    // Replace the label by a CircleProgress to indicate that it is loading.
    CircleProgress.markComponentLoading(nameLabel).getStyle().setFgColor(0xff0000);
    AsyncResource<MyData> request = fetchDataAsync();
    request.ready(data -> {
        nameLabel.setText(data.getName());
        // Replace the progress with the nameLabel now that
        // it is ready, using a fade transition
        CircleProgress.markComponentReady(nameLabel, CommonTransitions.createFade(300));
    });
    f.show();
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) Form(com.codename1.ui.Form) Label(com.codename1.ui.Label) Toolbar(com.codename1.ui.Toolbar)

Example 14 with AsyncResource

use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.

the class AudioRecorderComponentSample method recordAudio.

private AsyncResource<String> recordAudio() {
    AsyncResource<String> out = new AsyncResource<>();
    String mime = MediaManager.getAvailableRecordingMimeTypes()[0];
    String ext = mime.indexOf("mp3") != -1 ? "mp3" : mime.indexOf("wav") != -1 ? "wav" : mime.indexOf("aiff") != -1 ? "aiff" : "aac";
    MediaRecorderBuilder builder = new MediaRecorderBuilder().path(new File("myaudio." + ext).getAbsolutePath()).mimeType(mime);
    final AudioRecorderComponent cmp = new AudioRecorderComponent(builder);
    final Sheet sheet = new Sheet(null, "Record Audio");
    sheet.getContentPane().setLayout(new com.codename1.ui.layouts.BorderLayout());
    sheet.getContentPane().add(com.codename1.ui.layouts.BorderLayout.CENTER, cmp);
    cmp.addActionListener(new com.codename1.ui.events.ActionListener() {

        @Override
        public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
            switch(cmp.getState()) {
                case Accepted:
                    CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                        public void run() {
                            sheet.back();
                            sheet.addCloseListener(new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent evt) {
                                    sheet.removeCloseListener(this);
                                    out.complete(builder.getPath());
                                }
                            });
                        }
                    });
                    break;
                case Canceled:
                    FileSystemStorage fs = FileSystemStorage.getInstance();
                    if (fs.exists(builder.getPath())) {
                        FileSystemStorage.getInstance().delete(builder.getPath());
                    }
                    CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                        public void run() {
                            sheet.back();
                            sheet.addCloseListener(new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent evt) {
                                    sheet.removeCloseListener(this);
                                    out.complete(null);
                                }
                            });
                        }
                    });
                    break;
            }
        }
    });
    sheet.addCloseListener(new com.codename1.ui.events.ActionListener() {

        @Override
        public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
            if (cmp.getState() != AudioRecorderComponent.RecorderState.Accepted && cmp.getState() != AudioRecorderComponent.RecorderState.Canceled) {
                FileSystemStorage fs = FileSystemStorage.getInstance();
                if (fs.exists(builder.getPath())) {
                    FileSystemStorage.getInstance().delete(builder.getPath());
                }
                CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                    public void run() {
                        out.complete(null);
                    }
                });
            }
        }
    });
    sheet.show();
    return out;
}
Also used : MediaRecorderBuilder(com.codename1.media.MediaRecorderBuilder) ActionEvent(com.codename1.ui.events.ActionEvent) FileSystemStorage(com.codename1.io.FileSystemStorage) ActionEvent(com.codename1.ui.events.ActionEvent) ActionListener(com.codename1.ui.events.ActionListener) ActionListener(com.codename1.ui.events.ActionListener) AudioRecorderComponent(com.codename1.components.AudioRecorderComponent) AsyncResource(com.codename1.util.AsyncResource) File(com.codename1.io.File) Sheet(com.codename1.ui.Sheet)

Example 15 with AsyncResource

use of com.codename1.util.AsyncResource in project CodenameOne by codenameone.

the class JavaFXSEPort method createMediaAsync.

/**
 * Plays the sound in the given stream
 *
 * @param stream the stream containing the media data
 * @param mimeType the type of the data in the stream
 * @param onCompletion invoked when the audio file finishes playing, may be
 * null
 * @return a handle that can be used to control the playback of the audio
 * @throws java.io.IOException if the URI access fails
 */
@Override
public AsyncResource<Media> createMediaAsync(final InputStream stream, final String mimeType, final Runnable onCompletion) {
    final AsyncResource<Media> out = new AsyncResource<Media>();
    if (!checkForPermission("android.permission.READ_PHONE_STATE", "This is required to play media")) {
        out.error(new IOException("android.permission.READ_PHONE_STATE is required to play media"));
        return out;
    }
    if (!checkForPermission("android.permission.WRITE_EXTERNAL_STORAGE", "This is required to play media")) {
        out.error(new IOException("android.permission.WRITE_EXTERNAL_STORAGE is required to play media"));
        return out;
    }
    if (!fxExists) {
        String msg = "This fetaure is supported from Java version 1.7.0_06, update your Java to enable this feature. This might fail on OpenJDK as well in which case you will need to install the Oracle JDK. ";
        // System.out.println(msg);
        out.error(new IOException(msg));
        return out;
    }
    java.awt.Container cnt = canvas.getParent();
    while (!(cnt instanceof JFrame)) {
        cnt = cnt.getParent();
        if (cnt == null) {
            return null;
        }
    }
    final java.awt.Container c = cnt;
    // final Media[] media = new Media[1];
    // final Exception[] err = new Exception[1];
    final javafx.embed.swing.JFXPanel m = new CN1JFXPanel();
    // mediaContainer = m;
    Platform.runLater(new Runnable() {

        @Override
        public void run() {
            try {
                new CodenameOneMediaPlayer(stream, mimeType, (JFrame) c, m, onCompletion, out);
            } catch (Exception ex) {
                out.error(ex);
            }
        }
    });
    return out;
}
Also used : AbstractMedia(com.codename1.media.AbstractMedia) AsyncMedia(com.codename1.media.AsyncMedia) Media(com.codename1.media.Media) IOException(java.io.IOException) IOException(java.io.IOException) JFrame(javax.swing.JFrame) AsyncResource(com.codename1.util.AsyncResource)

Aggregations

AsyncResource (com.codename1.util.AsyncResource)20 IOException (java.io.IOException)6 Timer (java.util.Timer)5 AbstractMedia (com.codename1.media.AbstractMedia)4 AsyncMedia (com.codename1.media.AsyncMedia)4 Media (com.codename1.media.Media)4 Map (java.util.Map)4 JFrame (javax.swing.JFrame)4 Element (com.codename1.xml.Element)3 InputStreamReader (java.io.InputStreamReader)3 Component (com.codename1.ui.Component)2 Form (com.codename1.ui.Form)2 Toolbar (com.codename1.ui.Toolbar)2 Motion (com.codename1.ui.animations.Motion)2 ActionEvent (com.codename1.ui.events.ActionEvent)2 ActionListener (com.codename1.ui.events.ActionListener)2 BorderLayout (com.codename1.ui.layouts.BorderLayout)2 StringReader (java.io.StringReader)2 TimerTask (java.util.TimerTask)2 BillingResult (com.android.billingclient.api.BillingResult)1