/*
* Created by Storm Zhang, Feb 11, 2014.
*/
package com.likebamboo.osa.android.request;
import android.app.ActivityManager;
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.likebamboo.osa.android.cache.BitmapLruCache;
/**
* volley请求管理
*/
public class RequestManager {
private static RequestQueue mRequestQueue;
private static ImageLoader mImageLoader;
private RequestManager() {
// no instances
}
/**
* 初始化
*
* @param context
*/
public static void init(Context context) {
mRequestQueue = Volley.newRequestQueue(context);
int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
// Use 1/8th of the available memory for this memory cache.
int cacheSize = 1024 * 1024 * memClass / 8;
mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(cacheSize));
}
/**
* 获取请求队列
*
* @return
*/
public static RequestQueue getRequestQueue() {
if (mRequestQueue != null) {
return mRequestQueue;
} else {
throw new IllegalStateException("RequestQueue not initialized");
}
}
/**
* 添加请求
*
* @param request
* @param tag
*/
public static void addRequest(Request<?> request, Object... tag) {
if (tag != null && tag.length > 0) {
request.setTag(tag[0]);
}
mRequestQueue.add(request);
}
/**
* 取消请求
*
* @param tag
*/
public static void cancelAll(Object tag) {
mRequestQueue.cancelAll(tag);
}
/**
* Returns instance of ImageLoader initialized with {@see FakeImageCache}
* which effectively means that no memory caching is used. This is useful
* for images that you know that will be show only once.
*
* @return
*/
public static ImageLoader getImageLoader() {
if (mImageLoader != null) {
return mImageLoader;
} else {
throw new IllegalStateException("ImageLoader not initialized");
}
}
}