use of com.codename1.ui.events.ActionEvent 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.ActionEvent 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.ActionEvent in project CodenameOne by codenameone.
the class AndroidImplementation method onActivityResult.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == ZOOZ_PAYMENT) {
((IntentResultListener) pur).onActivityResult(requestCode, resultCode, intent);
return;
}
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CAPTURE_IMAGE) {
try {
String imageUri = (String) Storage.getInstance().readObject("imageUri");
Vector pathandId = StringUtil.tokenizeString(imageUri, ";");
String path = (String) pathandId.get(0);
String lastId = (String) pathandId.get(1);
Storage.getInstance().deleteStorageFile("imageUri");
clearMediaDB(lastId, path);
callback.fireActionEvent(new ActionEvent(path));
return;
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == CAPTURE_VIDEO) {
String path = (String) Storage.getInstance().readObject("videoUri");
Storage.getInstance().deleteStorageFile("videoUri");
callback.fireActionEvent(new ActionEvent(path));
return;
} else if (requestCode == CAPTURE_AUDIO) {
Uri data = intent.getData();
String path = convertImageUriToFilePath(data, getContext());
callback.fireActionEvent(new ActionEvent(path));
return;
} else if (requestCode == OPEN_GALLERY) {
Uri selectedImage = intent.getData();
String scheme = intent.getScheme();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// this happens on Android devices, not exactly sure what the use case is
if (cursor == null) {
callback.fireActionEvent(null);
return;
}
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
if (filePath == null && "content".equals(scheme)) {
// locally
try {
InputStream inputStream = getContext().getContentResolver().openInputStream(selectedImage);
if (inputStream != null) {
String name = getContentName(getContext().getContentResolver(), selectedImage);
if (name != null) {
filePath = getAppHomePath() + getFileSystemSeparator() + name;
File f = new File(filePath);
OutputStream tmp = createFileOuputStream(f);
byte[] buffer = new byte[1024];
int read = -1;
while ((read = inputStream.read(buffer)) > -1) {
tmp.write(buffer, 0, read);
}
tmp.close();
inputStream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
callback.fireActionEvent(new ActionEvent(filePath));
return;
} else {
if (callback != null) {
callback.fireActionEvent(new ActionEvent("ok"));
}
return;
}
}
// clean imageUri
String imageUri = (String) Storage.getInstance().readObject("imageUri");
if (imageUri != null) {
Storage.getInstance().deleteStorageFile("imageUri");
}
if (callback != null) {
callback.fireActionEvent(null);
}
}
use of com.codename1.ui.events.ActionEvent 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);
}
use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.
the class AdsService method readResponse.
/**
* {@inheritDoc}
*/
protected void readResponse(InputStream input) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[256];
int len;
while ((len = input.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
int size = out.toByteArray().length;
if (size > 0) {
String s = new String(out.toByteArray(), 0, size, "UTF-8");
currentAd = s;
fireResponseListener(new ActionEvent(currentAd, ActionEvent.Type.Response));
}
}
Aggregations