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();
}
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;
}
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();
}
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;
}
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;
}
Aggregations