Updated on 2026-08-14

This commit is contained in:
Tangem 2018-07-18 12:14:21 +03:00
parent ff6efa8f37
commit 6cf2cd2afb
13 changed files with 239 additions and 111 deletions

View file

@ -2,9 +2,21 @@ package com.tangem;
import android.app.Application;
import android.support.v7.app.AppCompatDelegate;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
private static final String TAG = "." + AppController.class.getSimpleName();
public static final String TAG = "fragment." + AppController.class.getSimpleName();
/**
* Global request queue for Volley
*/
private RequestQueue mRequestQueue;
/**
* A singleton instance of the application class for easy access in other places
@ -22,20 +34,64 @@ public class AppController extends Application {
@Override
public void onCreate() {
super.onCreate();
// init the singleton
// initialize the singleton
sInstance = this;
}
/**
* @return singleton instance
* @return SeoPult singleton instance
*/
public static synchronized AppController getInstance() {
return sInstance;
}
@Override
public void onTerminate() {
super.onTerminate();
/**
* @return The Volley Request queue, the queue will be created if it is null
*/
public RequestQueue getRequestQueue() {
// lazy initialize the request queue, the queue instance will be created when it is accessed for the first time
if (mRequestQueue == null)
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
return mRequestQueue;
}
/**
* Adds the specified request to the global queue, if tag is specified
* then it is used else Default TAG is used.
*
* @param req
* @param tag
*/
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
VolleyLog.d("Adding request to queue: %s", req.getUrl());
getRequestQueue().add(req);
}
/**
* Adds the specified request to the global queue using the Default TAG.
*
* @param req
*/
public <T> void addToRequestQueue(Request<T> req) {
// set the default tag if tag is empty
req.setTag(TAG);
getRequestQueue().add(req);
}
/**
* Cancels all pending requests by the specified TAG, it is important
* to specify a TAG so that the pending/ongoing requests can be cancelled.
*
* @param tag
*/
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}