use of com.codename1.util.Callback 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);
}
use of com.codename1.util.Callback 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);
}
use of com.codename1.util.Callback 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.util.Callback in project CodenameOne by codenameone.
the class ResetableTextWatcher method showTextEditorAgain.
/**
* Shows the native text field again after it has been hidden in async edit mode.
*/
private void showTextEditorAgain() {
if (!mIsEditing || !isTextEditorHidden()) {
return;
}
textEditorHidden = false;
final TextArea ta = mEditText.mTextArea;
// changed since the native aread was hidden.
synchronized (this) {
inputBuffer = new ArrayList<TextChange>();
}
// We are probably not on the EDT. We need to be on the EDT to
// safely get text from the textarea for synchronization.
Display.getInstance().callSerially(new Runnable() {
public void run() {
// and the editing text area hasn't changed since we issued this call.
if (mIsEditing && mEditText != null && mEditText.mTextArea == ta) {
final String text = ta.getText();
final int cursorPos = ta.getCursorPosition();
// Now that we have our text from the CN1 text area, we need to be on the
// Android UI thread in order to set the text of the native text editor.
impl.getActivity().runOnUiThread(new Runnable() {
public void run() {
// and the editing text area hasn't changed since we issued this call.
if (mIsEditing && mEditText != null && mEditText.mTextArea == ta) {
// so that we don't find it in an inconsistent state.
synchronized (InPlaceEditView.this) {
// Let's record the cursor positions of the native
// text editor in case we need to use them after synchronizing
// with the CN1 textarea.
int start = cursorPos;
int end = cursorPos;
/*
if (!inputBuffer.isEmpty()) {
// If the input buffer isn't empty, then our start
// and end positions will be "wonky"
start = end = inputBuffer.get(0).atPos;
// If the first change was a delete, then the atPos
// will point to the beginning of the deleted section
// so we need to adjust the end point to be *after*
// the deleted section to begin.
if (inputBuffer.get(0).deleteLength > 0) {
end = start = end + inputBuffer.get(0).deleteLength;
}
}
*/
StringBuilder buf = new StringBuilder();
buf.append(text);
// Loop through any pending changes in the input buffer
// (I.e. key strokes that have occurred since we initiated
// this async callback hell!!)
List<TextChange> tinput = inputBuffer;
if (tinput != null) {
for (TextChange change : tinput) {
// end.
if (change.textToAppend != null) {
if (end >= 0 && end <= buf.length()) {
buf.insert(end, change.textToAppend);
end += change.textToAppend.length();
start = end;
} else {
buf.append(change.textToAppend);
end = buf.length();
start = end;
}
} else // The change is "deleted" text.
if (change.deleteLength > 0) {
if (end >= change.deleteLength && end <= buf.length()) {
buf.delete(end - change.deleteLength, end);
end -= change.deleteLength;
start = end;
} else if (end > 0 && end < change.deleteLength) {
buf.delete(0, end);
end = 0;
start = end;
}
}
}
}
// Important: Clear the input buffer so that the TextWatcher
// knows to stop filling it up. We only need the inputBuffer
// to keep input between the original showTextEditorAgain() call
// and here.
inputBuffer = null;
mEditText.setText(buf.toString());
if (start < 0 || start > mEditText.getText().length()) {
start = mEditText.getText().length();
}
if (end < 0 || end > mEditText.getText().length()) {
end = mEditText.getText().length();
}
// Update the caret in the edit text field so we can continue.
mEditText.setSelection(start, end);
}
}
}
});
}
}
});
reLayoutEdit(true);
repaintTextEditor(true);
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class Component method laidOut.
/**
* This is a callback method to inform the Component when it's been laidout
* on the parent Container
*/
protected void laidOut() {
if (!isCellRenderer()) {
CodenameOneImplementation ci = Display.impl;
if (ci.isEditingText()) {
return;
}
Form f = getComponentForm();
int ivk = getInvisibleAreaUnderVKB();
if (isScrollableY() && getScrollY() > 0 && getScrollY() + getHeight() > getScrollDimension().getHeight() + ivk) {
setScrollY(getScrollDimension().getHeight() - getHeight() + ivk);
}
if (isScrollableX() && getScrollX() > 0 && getScrollX() + getWidth() > getScrollDimension().getWidth()) {
setScrollX(getScrollDimension().getWidth() - getWidth());
}
if (!isScrollableY() && getScrollY() > 0) {
setScrollY(0);
}
if (!isScrollableX() && getScrollX() > 0) {
setScrollX(0);
}
updateNativeOverlay();
}
}
Aggregations