package com.ushahidi.android.presentation.account;

import com.google.gson.Gson;

import com.ushahidi.android.presentation.UshahidiApplication;
import com.ushahidi.android.presentation.state.NoAccessTokenEvent;

import android.content.SharedPreferences;

import de.rheinfabrik.heimdall.OAuth2AccessToken;
import de.rheinfabrik.heimdall.OAuth2AccessTokenStorage;
import rx.Observable;

/**
 * A simple storage that saves the access token as plain text in the passed shared preferences.
 * It is recommend to set the access mode to MODE_PRIVATE.
 *
 * @param <TAccessToken> The access token type.
 * @author Ushahidi Team <team@ushahidi.com>
 */
public class AccessTokenStorageManager<TAccessToken extends OAuth2AccessToken> implements
        OAuth2AccessTokenStorage<TAccessToken> {
    // Constants

    private static final String ACCESS_TOKEN_PREFERENCES_KEY = "OAuth2AccessToken";

    // Members

    private final SharedPreferences mSharedPreferences;

    private final Class mTokenClass;

    // Constructor

    /**
     * Designated constructor.
     *
     * @param sharedPreferences The shared preferences used for saving the access token.
     * @param tokenClass        The actual class of the access token.
     */
    public AccessTokenStorageManager(SharedPreferences sharedPreferences,
            Class tokenClass) {
        super();

        if (tokenClass == null) {
            throw new RuntimeException("TokenClass MUST NOT be null.");
        }

        if (sharedPreferences == null) {
            throw new RuntimeException("SharedPreferences MUST NOT be null.");
        }

        mTokenClass = tokenClass;
        mSharedPreferences = sharedPreferences;
    }

    // OAuth2AccessTokenStorage

    @SuppressWarnings("unchecked")
    @Override
    public Observable<TAccessToken> getStoredAccessToken() {
        return Observable
                .just(mSharedPreferences.getString(ACCESS_TOKEN_PREFERENCES_KEY, null))
                .filter(accessToken -> {
                    if (accessToken == null) {
                        // Send an event that there is no valid access token
                        UshahidiApplication.getRxEventBusInstance().send(new NoAccessTokenEvent());
                    }

                    return true;
                })
                .map(json -> (TAccessToken) new Gson().fromJson(json, mTokenClass));
    }

    @Override
    public void storeAccessToken(TAccessToken accessToken) {
        mSharedPreferences
                .edit()
                .putString(ACCESS_TOKEN_PREFERENCES_KEY, new Gson().toJson(accessToken))
                .apply();
    }

    @Override
    public Observable<Boolean> hasAccessToken() {
        return Observable.just(mSharedPreferences.contains(ACCESS_TOKEN_PREFERENCES_KEY));
    }

    @Override
    public void removeAccessToken() {
        mSharedPreferences
                .edit()
                .remove(ACCESS_TOKEN_PREFERENCES_KEY)
                .apply();
    }
}

@@ -552,27 +552,16 @@
 param %3CT
-AccessToken
 %3E The ac
@@ -666,27 +666,16 @@
 anager%3CT
-AccessToken
  extends
@@ -739,27 +739,16 @@
 torage%3CT
-AccessToken
 %3E %7B%0A    
@@ -1756,27 +1756,16 @@
 rvable%3CT
-AccessToken
 %3E getSto
@@ -2176,33 +2176,32 @@
               %7D%0A
-%0A
                 
@@ -2267,27 +2267,16 @@
 on -%3E (T
-AccessToken
 ) new Gs
@@ -2365,27 +2365,16 @@
 sToken(T
-AccessToken
  accessT

Fix checkstyle