2017-01-20 19:09:10 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is part of Tusky.
|
|
|
|
*
|
|
|
|
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License as published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2017-01-03 10:30:27 +11:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.support.v4.util.LruCache;
|
|
|
|
|
|
|
|
import com.android.volley.Request;
|
|
|
|
import com.android.volley.RequestQueue;
|
|
|
|
import com.android.volley.toolbox.ImageLoader;
|
|
|
|
import com.android.volley.toolbox.Volley;
|
|
|
|
|
|
|
|
public class VolleySingleton {
|
|
|
|
private static VolleySingleton instance;
|
|
|
|
private RequestQueue requestQueue;
|
|
|
|
private ImageLoader imageLoader;
|
|
|
|
private static Context context;
|
|
|
|
|
|
|
|
private VolleySingleton(Context context) {
|
|
|
|
VolleySingleton.context = context;
|
|
|
|
requestQueue = getRequestQueue();
|
|
|
|
imageLoader = new ImageLoader(requestQueue,
|
|
|
|
new ImageLoader.ImageCache() {
|
|
|
|
private final LruCache<String, Bitmap> cache = new LruCache<>(20);
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Bitmap getBitmap(String url) {
|
|
|
|
return cache.get(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void putBitmap(String url, Bitmap bitmap) {
|
|
|
|
cache.put(url, bitmap);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized VolleySingleton getInstance(Context context) {
|
|
|
|
if (instance == null) {
|
|
|
|
instance = new VolleySingleton(context);
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
private RequestQueue getRequestQueue() {
|
2017-01-03 10:30:27 +11:00
|
|
|
if (requestQueue == null) {
|
|
|
|
/* getApplicationContext() is key, it keeps you from leaking the
|
|
|
|
* Activity or BroadcastReceiver if someone passes one in. */
|
|
|
|
requestQueue= Volley.newRequestQueue(context.getApplicationContext());
|
|
|
|
}
|
|
|
|
return requestQueue;
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
<T> void addToRequestQueue(Request<T> request) {
|
2017-01-03 10:30:27 +11:00
|
|
|
getRequestQueue().add(request);
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
void cancelAll(String tag) {
|
2017-01-17 05:15:42 +11:00
|
|
|
getRequestQueue().cancelAll(tag);
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
ImageLoader getImageLoader() {
|
2017-01-03 10:30:27 +11:00
|
|
|
return imageLoader;
|
|
|
|
}
|
|
|
|
}
|