package vn.efode.vts.application;

import android.app.Application;
import android.content.SharedPreferences;
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;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import vn.efode.vts.model.User;

/**
 * Created by Tuan on 04/04/2017.
 */

public class ApplicationController extends Application {
    public static final String TAG = "VolleyPatterns";//Log or request TAG
    private RequestQueue mRequestQueue;//Global request queue for Volley
    private static ApplicationController sInstance;//A singleton instance of the application class for easy access in other places
    public static final String USER_SESSION = "user_session";

    /**
     * Common shared preference.
     */
    public static SharedPreferences sharedPreferences;

    @Override
    public void onCreate() {
        super.onCreate();
        // initialize sharedPreferences instance
        sharedPreferences = getSharedPreferences( getPackageName() + "_storage", MODE_PRIVATE);

        // initialize the singleton
        sInstance = this;
    }

    /**
     * @return co.pixelmatter.meme.ApplicationController singleton instance
     */
    public static synchronized ApplicationController getInstance() {
        return sInstance;
    }

    /**
     * @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);
        }
    }

    /**
     * Get current user.
     * @return current user.
     */
    public static User getCurrentUser() {
        String userJson = ApplicationController.sharedPreferences.getString(USER_SESSION, null);
        User user = null;
        if (userJson != null) {
            Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                    .setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            user = gson.fromJson(userJson, User.class);
        }
        return user;
    }
}

@@ -807,16 +807,83 @@
 r places
+%0A%0A    /**%0A     * Shared preference user session key's name.%0A     */
 %0A    pub

Add java doc