Search in sources :

Example 6 with ImageListener

use of com.android.volley.toolbox.ImageLoader.ImageListener in project WordPress-Android by wordpress-mobile.

the class WPNetworkImageViewTest method testVolleyImageLoaderGetNullHost.

// https://github.com/wordpress-mobile/WordPress-Android/issues/1549
public void testVolleyImageLoaderGetNullHost() throws InterruptedException {
    Handler mainLooperHandler = new Handler(WordPress.getContext().getMainLooper());
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final boolean[] success = new boolean[1];
    Runnable getImage = new Runnable() {

        @Override
        public void run() {
            try {
                // This call crash on old volley versions
                WordPress.sImageLoader.get("http;///hello/null/host", new ImageListener() {

                    @Override
                    public void onResponse(ImageContainer imageContainer, boolean b) {
                    }

                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                    }
                }, 1, 1);
                success[0] = true;
            } catch (Exception e) {
                AppLog.e(T.TESTS, e);
                success[0] = false;
            } finally {
                countDownLatch.countDown();
            }
        }
    };
    mainLooperHandler.post(getImage);
    countDownLatch.await(1, TimeUnit.SECONDS);
    assertTrue("Invalid Volley library version", success[0]);
}
Also used : VolleyError(com.android.volley.VolleyError) ImageListener(com.android.volley.toolbox.ImageLoader.ImageListener) Handler(android.os.Handler) CountDownLatch(java.util.concurrent.CountDownLatch) ImageContainer(com.android.volley.toolbox.ImageLoader.ImageContainer)

Example 7 with ImageListener

use of com.android.volley.toolbox.ImageLoader.ImageListener in project SimplifyReader by chentao0707.

the class NetworkImageView method loadImageIfNecessary.

/**
 * Loads the image for the view if it isn't already loaded.
 * @param isInLayoutPass True if this was invoked from a layout pass, false otherwise.
 */
void loadImageIfNecessary(final boolean isInLayoutPass) {
    int width = getWidth();
    int height = getHeight();
    ScaleType scaleType = getScaleType();
    boolean wrapWidth = false, wrapHeight = false;
    if (getLayoutParams() != null) {
        wrapWidth = getLayoutParams().width == LayoutParams.WRAP_CONTENT;
        wrapHeight = getLayoutParams().height == LayoutParams.WRAP_CONTENT;
    }
    // if the view's bounds aren't known yet, and this is not a wrap-content/wrap-content
    // view, hold off on loading the image.
    boolean isFullyWrapContent = wrapWidth && wrapHeight;
    if (width == 0 && height == 0 && !isFullyWrapContent) {
        return;
    }
    // currently loaded image.
    if (TextUtils.isEmpty(mUrl)) {
        if (mImageContainer != null) {
            mImageContainer.cancelRequest();
            mImageContainer = null;
        }
        setDefaultImageOrNull();
        return;
    }
    // if there was an old request in this view, check if it needs to be canceled.
    if (mImageContainer != null && mImageContainer.getRequestUrl() != null) {
        if (mImageContainer.getRequestUrl().equals(mUrl)) {
            // if the request is from the same URL, return.
            return;
        } else {
            // if there is a pre-existing request, cancel it if it's fetching a different URL.
            mImageContainer.cancelRequest();
            setDefaultImageOrNull();
        }
    }
    // Calculate the max image width / height to use while ignoring WRAP_CONTENT dimens.
    int maxWidth = wrapWidth ? 0 : width;
    int maxHeight = wrapHeight ? 0 : height;
    // The pre-existing content of this view didn't match the current URL. Load the new image
    // from the network.
    ImageContainer newContainer = mImageLoader.get(mUrl, new ImageListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (mErrorImageId != 0) {
                setImageResource(mErrorImageId);
            }
        }

        @Override
        public void onResponse(final ImageContainer response, boolean isImmediate) {
            // the main thread.
            if (isImmediate && isInLayoutPass) {
                post(new Runnable() {

                    @Override
                    public void run() {
                        onResponse(response, false);
                    }
                });
                return;
            }
            if (response.getBitmap() != null) {
                setImageBitmap(response.getBitmap());
            } else if (mDefaultImageId != 0) {
                setImageResource(mDefaultImageId);
            }
        }
    }, maxWidth, maxHeight, scaleType);
    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
Also used : VolleyError(com.android.volley.VolleyError) ImageListener(com.android.volley.toolbox.ImageLoader.ImageListener) ImageContainer(com.android.volley.toolbox.ImageLoader.ImageContainer)

Example 8 with ImageListener

use of com.android.volley.toolbox.ImageLoader.ImageListener in project iosched by google.

the class NetworkImageView method loadImageIfNecessary.

/**
 * Loads the image for the view if it isn't already loaded.
 * @param isInLayoutPass True if this was invoked from a layout pass, false otherwise.
 */
void loadImageIfNecessary(final boolean isInLayoutPass) {
    int width = getWidth();
    int height = getHeight();
    boolean wrapWidth = false, wrapHeight = false;
    if (getLayoutParams() != null) {
        wrapWidth = getLayoutParams().width == LayoutParams.WRAP_CONTENT;
        wrapHeight = getLayoutParams().height == LayoutParams.WRAP_CONTENT;
    }
    // if the view's bounds aren't known yet, and this is not a wrap-content/wrap-content
    // view, hold off on loading the image.
    boolean isFullyWrapContent = wrapWidth && wrapHeight;
    if (width == 0 && height == 0 && !isFullyWrapContent) {
        return;
    }
    // currently loaded image.
    if (TextUtils.isEmpty(mUrl)) {
        if (mImageContainer != null) {
            mImageContainer.cancelRequest();
            mImageContainer = null;
        }
        setDefaultImageOrNull();
        return;
    }
    // if there was an old request in this view, check if it needs to be canceled.
    if (mImageContainer != null && mImageContainer.getRequestUrl() != null) {
        if (mImageContainer.getRequestUrl().equals(mUrl)) {
            // if the request is from the same URL, return.
            return;
        } else {
            // if there is a pre-existing request, cancel it if it's fetching a different URL.
            mImageContainer.cancelRequest();
            setDefaultImageOrNull();
        }
    }
    // Calculate the max image width / height to use while ignoring WRAP_CONTENT dimens.
    int maxWidth = wrapWidth ? 0 : width;
    int maxHeight = wrapHeight ? 0 : height;
    // The pre-existing content of this view didn't match the current URL. Load the new image
    // from the network.
    ImageContainer newContainer = mImageLoader.get(mUrl, new ImageListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (mErrorImageId != 0) {
                setImageResource(mErrorImageId);
            }
        }

        @Override
        public void onResponse(final ImageContainer response, boolean isImmediate) {
            // the main thread.
            if (isImmediate && isInLayoutPass) {
                post(new Runnable() {

                    @Override
                    public void run() {
                        onResponse(response, false);
                    }
                });
                return;
            }
            if (response.getBitmap() != null) {
                setImageBitmap(response.getBitmap());
            } else if (mDefaultImageId != 0) {
                setImageResource(mDefaultImageId);
            }
        }
    }, maxWidth, maxHeight);
    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
Also used : VolleyError(com.android.volley.VolleyError) ImageListener(com.android.volley.toolbox.ImageLoader.ImageListener) ImageContainer(com.android.volley.toolbox.ImageLoader.ImageContainer)

Aggregations

VolleyError (com.android.volley.VolleyError)8 ImageContainer (com.android.volley.toolbox.ImageLoader.ImageContainer)8 ImageListener (com.android.volley.toolbox.ImageLoader.ImageListener)8 Handler (android.os.Handler)1 NetworkImageView (com.android.volley.toolbox.NetworkImageView)1 CountDownLatch (java.util.concurrent.CountDownLatch)1