Search in sources :

Example 1 with WebToken

use of com.mxt.anitrend.model.entity.anilist.WebToken in project anitrend-app by AniTrend.

the class WebTokenRequest method checkTokenState.

/**
 * Double checks to assure that multiple threads attempting to access
 * the token don't invoke multiple refresh token requests all at once
 */
private static void checkTokenState(Context context, BasePresenter presenter) {
    if (token == null || token.getExpires() < (System.currentTimeMillis() / 1000L)) {
        WebToken response = WebFactory.requestCodeTokenSync(presenter.getDatabase().getAuthCode().getCode());
        if (response != null) {
            createNewTokenReference(response);
            presenter.getDatabase().saveWebToken(response);
            Log.d("WebTokenRequest", "Token refreshed & saved at time stamp: " + System.currentTimeMillis() / 1000L);
        } else
            Log.e("WebTokenRequest", "Token had an invalid instance from context: " + context);
    }
}
Also used : WebToken(com.mxt.anitrend.model.entity.anilist.WebToken)

Example 2 with WebToken

use of com.mxt.anitrend.model.entity.anilist.WebToken in project anitrend-app by AniTrend.

the class WebTokenRequest method getToken.

/**
 * Request a new access token using access code for authenticated content,
 * and replace the current token with the new one from the server after authentication
 */
public static synchronized boolean getToken(Context context, String code) throws ExecutionException, InterruptedException {
    WebToken authenticatedToken = new AuthenticationCodeAsync().execute(code).get();
    if (authenticatedToken != null) {
        DatabaseHelper databaseHelper = new DatabaseHelper(context);
        databaseHelper.saveWebToken(authenticatedToken);
        databaseHelper.saveAuthCode(new AuthBase(code, authenticatedToken.getRefresh_token()));
        createNewTokenReference(authenticatedToken);
        return true;
    }
    return false;
}
Also used : DatabaseHelper(com.mxt.anitrend.data.DatabaseHelper) AuthBase(com.mxt.anitrend.model.entity.base.AuthBase) WebToken(com.mxt.anitrend.model.entity.anilist.WebToken)

Aggregations

WebToken (com.mxt.anitrend.model.entity.anilist.WebToken)2 DatabaseHelper (com.mxt.anitrend.data.DatabaseHelper)1 AuthBase (com.mxt.anitrend.model.entity.base.AuthBase)1