com.mobimvp.cliques.service.RequestManager.java Source code

Java tutorial

Introduction

Here is the source code for com.mobimvp.cliques.service.RequestManager.java

Source

/*
 *
 *  * Copyright 2015 Van Shu
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *    http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

package com.mobimvp.cliques.service;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.widget.ImageView;

import com.android.volley.Cache;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.ImageLoader;
import com.mobimvp.cliques.AppData;
import com.mobimvp.cliques.util.BitmapLruCache;
import com.mobimvp.cliques.util.StethoInterceptor;
import com.squareup.okhttp.OkHttpClient;

import java.io.File;

public class RequestManager {
    private static final int MEM_CACHE_SIZE = 1024 * 1024
            * ((ActivityManager) AppData.getContext().getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass()
            / 3;
    public static RequestQueue mRequestQueue = newRequestQueue();
    private static ImageLoader mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(MEM_CACHE_SIZE));

    private static DiskBasedCache mDiskCache = (DiskBasedCache) mRequestQueue.getCache();

    private RequestManager() {
    }

    private static Cache openCache() {
        return new DiskBasedCache(AppData.getContext().getExternalCacheDir(), 10 * 1024 * 1024);
    }

    private static RequestQueue newRequestQueue() {
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.networkInterceptors().add(new StethoInterceptor());
        RequestQueue requestQueue = new RequestQueue(openCache(), new BasicNetwork(new OkHttpStack(okHttpClient)));
        requestQueue.start();
        return requestQueue;
    }

    public static void addRequest(Request<?> request, Object tag) {
        if (tag != null) {
            request.setTag(tag);
        }
        mRequestQueue.add(request);
    }

    public static void addRequest(Request<?> request) {
        mRequestQueue.add(request);
    }

    public static void cancelAll(Object tag) {
        mRequestQueue.cancelAll(tag);
    }

    public static File getCachedImageFile(String url) {
        return mDiskCache.getFileForKey(url);
    }

    public static ImageLoader.ImageContainer loadImage(String requestUrl, ImageLoader.ImageListener imageListener) {
        return loadImage(requestUrl, imageListener, 0, 0);
    }

    public static ImageLoader.ImageContainer loadImage(String requestUrl, ImageLoader.ImageListener imageListener,
            int maxWidth, int maxHeight) {
        return mImageLoader.get(requestUrl, imageListener, maxWidth, maxHeight);
    }

    public static ImageLoader.ImageListener getImageListener(final ImageView view,
            final Drawable defaultImageDrawable, final Drawable errorImageDrawable) {
        return new ImageLoader.ImageListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (errorImageDrawable != null) {
                    view.setImageDrawable(errorImageDrawable);
                }
            }

            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (response.getBitmap() != null) {
                    if (!isImmediate && defaultImageDrawable != null) {
                        TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[] {
                                defaultImageDrawable,
                                new BitmapDrawable(AppData.getContext().getResources(), response.getBitmap()) });
                        transitionDrawable.setCrossFadeEnabled(true);
                        view.setImageDrawable(transitionDrawable);
                        transitionDrawable.startTransition(100);
                    } else {
                        view.setImageBitmap(response.getBitmap());
                    }
                } else if (defaultImageDrawable != null) {
                    view.setImageDrawable(defaultImageDrawable);
                }
            }
        };
    }

}