Search in sources :

Example 41 with Callback

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();
                }
            }
        }
    };
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 42 with Callback

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();
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 43 with Callback

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);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 44 with Callback

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;
            }
        }
    }
}
Also used : Callback(com.codename1.util.Callback) PushCallback(com.codename1.push.PushCallback) BackgroundFetch(com.codename1.background.BackgroundFetch) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Example 45 with Callback

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();
    }
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Aggregations

ActionListener (com.codename1.ui.events.ActionListener)14 ActionEvent (com.codename1.ui.events.ActionEvent)12 IOException (java.io.IOException)11 ConnectionRequest (com.codename1.io.ConnectionRequest)10 EventDispatcher (com.codename1.ui.util.EventDispatcher)8 NetworkEvent (com.codename1.io.NetworkEvent)6 InputStream (java.io.InputStream)5 Intent (android.content.Intent)4 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)4 ArrayList (java.util.ArrayList)4 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)3 Uri (android.net.Uri)3 IntentResultListener (com.codename1.impl.android.IntentResultListener)3 AccessToken (com.codename1.io.AccessToken)3 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)3 EncodedImage (com.codename1.ui.EncodedImage)3 Image (com.codename1.ui.Image)3 File (java.io.File)3 OutputStream (java.io.OutputStream)3 RandomAccessFile (java.io.RandomAccessFile)3