use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class AndroidImplementation method openImageGallery.
/**
* Opens the device image gallery
*
* @param response callback for the resulting image
*/
public void openImageGallery(ActionListener response) {
if (getActivity() == null) {
throw new RuntimeException("Cannot open image gallery in background mode");
}
if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to browse the photos")) {
return;
}
if (editInProgress()) {
stopEditing(true);
}
callback = new EventDispatcher();
callback.addListener(response);
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
this.getActivity().startActivityForResult(galleryIntent, OPEN_GALLERY);
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class AndroidImplementation method captureAudio.
public void captureAudio(final ActionListener response) {
if (!checkForPermission(Manifest.permission.RECORD_AUDIO, "This is required to record the audio")) {
return;
}
try {
final Form current = Display.getInstance().getCurrent();
final File temp = File.createTempFile("mtmp", ".3gpp");
temp.deleteOnExit();
if (recorder != null) {
recorder.release();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
recorder.setOutputFile(temp.getAbsolutePath());
final Form recording = new Form("Recording");
recording.setTransitionInAnimator(CommonTransitions.createEmpty());
recording.setTransitionOutAnimator(CommonTransitions.createEmpty());
recording.setLayout(new BorderLayout());
recorder.prepare();
recorder.start();
final Label time = new Label("00:00");
time.getAllStyles().setAlignment(Component.CENTER);
Font f = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
f = f.derive(getDisplayHeight() / 10, Font.STYLE_PLAIN);
time.getAllStyles().setFont(f);
recording.addComponent(BorderLayout.CENTER, time);
recording.registerAnimated(new Animation() {
long current = System.currentTimeMillis();
long zero = current;
int sec = 0;
public boolean animate() {
long now = System.currentTimeMillis();
if (now - current > 1000) {
current = now;
sec++;
return true;
}
return false;
}
public void paint(Graphics g) {
int seconds = sec % 60;
int minutes = sec / 60;
String secStr = seconds < 10 ? "0" + seconds : "" + seconds;
String minStr = minutes < 10 ? "0" + minutes : "" + minutes;
String txt = minStr + ":" + secStr;
time.setText(txt);
}
});
Container south = new Container(new com.codename1.ui.layouts.GridLayout(1, 2));
Command cancel = new Command("Cancel") {
@Override
public void actionPerformed(ActionEvent evt) {
if (recorder != null) {
recorder.stop();
recorder.release();
recorder = null;
}
current.showBack();
response.actionPerformed(null);
}
};
recording.setBackCommand(cancel);
south.add(new com.codename1.ui.Button(cancel));
south.add(new com.codename1.ui.Button(new Command("Save") {
@Override
public void actionPerformed(ActionEvent evt) {
if (recorder != null) {
recorder.stop();
recorder.release();
recorder = null;
}
current.showBack();
response.actionPerformed(new ActionEvent(temp.getAbsolutePath()));
}
}));
recording.addComponent(BorderLayout.SOUTH, south);
recording.show();
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException("failed to start audio recording");
}
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class AndroidImplementation method stopTextEditing.
@Override
public void stopTextEditing(final Runnable onFinish) {
final Form f = Display.getInstance().getCurrent();
f.addSizeChangedListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
f.removeSizeChangedListener(this);
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
onFinish.run();
}
});
}
});
stopEditing(true);
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class AndroidImplementation method execute.
public void execute(String url, ActionListener response) {
if (response != null) {
callback = new EventDispatcher();
callback.addListener(response);
}
try {
Intent intent = createIntentForURL(url);
if (intent == null) {
return;
}
if (response != null && getActivity() != null) {
getActivity().startActivityForResult(intent, IntentResultListener.URI_SCHEME);
} else {
getContext().startActivity(intent);
}
return;
} catch (Exception ex) {
com.codename1.io.Log.e(ex);
}
try {
if (editInProgress()) {
stopEditing(true);
}
getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class AndroidImplementation method captureVideo.
@Override
public void captureVideo(ActionListener response) {
if (getActivity() == null) {
throw new RuntimeException("Cannot capture video in background mode");
}
if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to take a video")) {
return;
}
callback = new EventDispatcher();
callback.addListener(response);
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
File newFile = getOutputMediaFile(true);
Uri videoUri = Uri.fromFile(newFile);
Storage.getInstance().writeObject("videoUri", newFile.getAbsolutePath());
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, videoUri);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.getActivity().startActivityForResult(intent, CAPTURE_VIDEO);
}
Aggregations