use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class AsyncDocumentRequestHandlerImpl method createConnectionRequest.
protected ConnectionRequest createConnectionRequest(final DocumentInfo docInfo, final IOCallback callback, final Object[] response) {
return new ConnectionRequest() {
protected void buildRequestBody(OutputStream os) throws IOException {
if (isPost()) {
if (docInfo.getParams() != null) {
OutputStreamWriter w = new OutputStreamWriter(os, docInfo.getEncoding());
w.write(docInfo.getParams());
}
}
}
protected void handleIOException(IOException err) {
if (callback == null) {
response[0] = err;
}
super.handleIOException(err);
}
protected boolean shouldAutoCloseResponse() {
return callback != null;
}
protected void readResponse(InputStream input) throws IOException {
if (callback != null) {
callback.streamReady(input, docInfo);
} else {
response[0] = input;
synchronized (LOCK) {
LOCK.notify();
}
}
}
};
}
use of com.codename1.util.Callback in project CodenameOne by codenameone.
the class SideMenuBar method closeCurrentMenu.
/**
* Folds the current side menu if it is open, when the menu is closed it
* will invoke the runnable callback method
*
* @param callback will be invoked when the menu is actually closed
*/
public static void closeCurrentMenu(final Runnable callback) {
if (Toolbar.isOnTopSideMenu() && (Toolbar.isGlobalToolbar() || Display.getInstance().getCommandBehavior() != Display.COMMAND_BEHAVIOR_SIDE_NAVIGATION)) {
Display.getInstance().getCurrent().getToolbar().closeSideMenu();
callback.run();
return;
}
Form f = Display.getInstance().getCurrent();
final SideMenuBar b = (SideMenuBar) f.getClientProperty("cn1$sideMenuParent");
if (b != null && !b.transitionRunning) {
b.parent.addShowListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
b.parent.removeShowListener(this);
callback.run();
}
});
b.closeMenu();
} else {
callback.run();
}
}
use of com.codename1.util.Callback 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.util.Callback in project CodenameOne by codenameone.
the class AndroidImplementation method performBackgroundFetch.
/**
* Calls the background fetch callback. If the app is in teh background, this will
* check to see if the lifecycle class implements the {@link com.codename1.background.BackgroundFetch}
* interface. If it does, it will execute its {@link com.codename1.background.BackgroundFetch#performBackgroundFetch(long, com.codename1.util.Callback) }
* method.
* @param blocking True if this should block until it is complete.
*/
public static void performBackgroundFetch(boolean blocking) {
if (Display.getInstance().isMinimized()) {
// By definition, background fetch should only occur if the app is minimized.
// This keeps it consistent with the iOS implementation that doesn't have a
// choice
final boolean[] complete = new boolean[1];
final Object lock = new Object();
final BackgroundFetch bgFetchListener = instance.getBackgroundFetchListener();
final long timeout = System.currentTimeMillis() + 25000;
if (bgFetchListener != null) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
bgFetchListener.performBackgroundFetch(timeout, new Callback<Boolean>() {
@Override
public void onSucess(Boolean value) {
// So we'll just consume this.
synchronized (lock) {
complete[0] = true;
lock.notify();
}
}
@Override
public void onError(Object sender, Throwable err, int errorCode, String errorMessage) {
com.codename1.io.Log.e(err);
synchronized (lock) {
complete[0] = true;
lock.notify();
}
}
});
}
});
}
while (blocking && !complete[0]) {
synchronized (lock) {
try {
lock.wait(1000);
} catch (Exception ex) {
}
}
if (!complete[0]) {
System.out.println("Waiting for background fetch to complete. Make sure your background fetch handler calls onSuccess() or onError() in the callback when complete");
}
if (System.currentTimeMillis() > timeout) {
System.out.println("Background fetch exceeded time alotted. Not waiting for its completion");
break;
}
}
}
}
use of com.codename1.util.Callback 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();
}
}
Aggregations