org.catnut.core.CatnutApp.java Source code

Java tutorial

Introduction

Here is the source code for org.catnut.core.CatnutApp.java

Source

/*
 * The MIT License (MIT)
 * Copyright (c) 2014 longkai
 * The software shall be used for good, not evil.
 */
package org.catnut.core;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.text.format.DateUtils;
import android.util.Log;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import org.catnut.metadata.AccessToken;
import org.catnut.support.OkHttpStack;
import org.json.JSONObject;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.catnut.metadata.AccessToken.ACCESS_TOKEN;
import static org.catnut.metadata.AccessToken.EXPIRES_IN;
import static org.catnut.metadata.AccessToken.UID;

/**
 * ?
 *
 * @author longkai
 */
public class CatnutApp extends Application {

    public static final String TAG = "CatnutApp";

    /** singleton */
    private static CatnutApp sApp;
    /** http request header for weibo' s access token */
    private static Map<String, String> sAuthHeaders;

    private RequestQueue mRequestQueue;
    private SharedPreferences mPreferences;
    private AccessToken mAccessToken;

    @Override
    public void onCreate() {
        super.onCreate();
        sApp = this;
        mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        mRequestQueue = Volley.newRequestQueue(this, new OkHttpStack());
        checkAccessToken();
    }

    /** ?????? */
    private void checkAccessToken() {
        if (mAccessToken == null) {
            mAccessToken = getAccessToken();
        }

        if (mAccessToken != null) {
            long now = System.currentTimeMillis();
            if (now > mAccessToken.expires_in) {
                // ?uid????uid???
                Log.d(TAG, "???...");
                invalidateAccessToken();
            } else {
                Log.d(TAG, "?" + DateUtils.getRelativeTimeSpanString(mAccessToken.expires_in)
                        + "?");
            }
        }
    }

    public SharedPreferences getPreferences() {
        return mPreferences;
    }

    /**
     * ????
     *
     * @param json ?access tokenjson
     */
    public void saveAccessToken(JSONObject json) {
        Log.d(TAG, "save access token...");
        mPreferences.edit().putLong(UID, json.optLong(UID)).putString(ACCESS_TOKEN, json.optString(ACCESS_TOKEN))
                .putLong(EXPIRES_IN, json.optLong(EXPIRES_IN) * 1000 + System.currentTimeMillis()).commit();
    }

    /**
     * 
     */
    public void invalidateAccessToken() {
        Log.d(TAG, "invalidate access token...");
        mPreferences.edit().remove(UID).remove(EXPIRES_IN).remove(ACCESS_TOKEN).commit();
        mAccessToken = null;
        if (sAuthHeaders != null) {
            sAuthHeaders.clear();
        }
        sAuthHeaders = null;
    }

    /**
     * ?????
     *
     * @return AccessToken
     */
    public AccessToken getAccessToken() {
        if (mAccessToken == null) {
            long uid = mPreferences.getLong(UID, 0L);
            long expiresIn = mPreferences.getLong(EXPIRES_IN, 0L);
            String accessToken = mPreferences.getString(ACCESS_TOKEN, null);
            // ?????
            if (uid != 0L && expiresIn != 0L && accessToken != null) {
                mAccessToken = new AccessToken(uid, expiresIn, accessToken);
            }
        }
        return mAccessToken;
    }

    /**
     * ?http
     *
     * @return RequestQueue
     */
    public RequestQueue getRequestQueue() {
        return mRequestQueue;
    }

    /**
     * boolean??
     *
     * @param key
     * @param defaultValue
     * @return a boolean with default res bool value
     */
    public static boolean getBoolean(int key, int defaultValue) {
        SharedPreferences pref = sApp.getPreferences();
        Resources res = sApp.getResources();
        return sApp.getPreferences().getBoolean(sApp.getString(key), res.getBoolean(defaultValue));
    }

    /**
     * ????
     *
     * @return CatnutApp
     */
    public static CatnutApp getTingtingApp() {
        return sApp;
    }

    /**
     * access tokenmap???uriappendaccess token
     * <p/>
     * ?map
     *
     * @return headers with access token
     */
    public static Map<String, String> getAuthHeaders() {
        // ?null
        AccessToken accessToken = sApp.getAccessToken();
        if (accessToken == null) {
            return Collections.emptyMap();
        }

        if (sAuthHeaders == null) {
            sAuthHeaders = new HashMap<String, String>(1);
            sAuthHeaders.put("Authorization", "OAuth2 " + accessToken.access_token);
        }
        return sAuthHeaders;
    }
}