/* 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; 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 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; } public 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.getApplicationContext()); } return requestQueue; } public void addToRequestQueue(Request request) { getRequestQueue().add(request); } public void cancelRequest(String tag) { getRequestQueue().cancelAll(tag); } public ImageLoader getImageLoader() { return imageLoader; } }