/* 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
* . */
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;
import java.lang.ref.WeakReference;
public class VolleySingleton {
private static VolleySingleton instance;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
/* This is a weak reference to account for the case where it might be held onto beyond the
* lifetime of the Activity/Fragment/Service, so it can be cleaned up. */
private static WeakReference context;
private VolleySingleton(Context context) {
VolleySingleton.context = new WeakReference<>(context);
requestQueue = getRequestQueue();
imageLoader = new ImageLoader(requestQueue,
new ImageLoader.ImageCache() {
private final LruCache 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;
}
private RequestQueue getRequestQueue() {
if (requestQueue == null) {
/* getApplicationContext() is key, it keeps you from leaking the
* Activity or BroadcastReceiver if someone passes one in. */
requestQueue= Volley.newRequestQueue(context.get().getApplicationContext());
}
return requestQueue;
}
void addToRequestQueue(Request request) {
getRequestQueue().add(request);
}
void cancelAll(String tag) {
getRequestQueue().cancelAll(tag);
}
ImageLoader getImageLoader() {
return imageLoader;
}
}