// Path: app/src/main/java/party/minge/reddit/client/Manager.java
// @EBean(scope = EBean.Scope.Singleton)
// public class Manager {
//     @Bean(ResFactory.class)
//     protected UserAgentFactory userAgentFactory;
// 
//     @Bean(ResFactory.class)
//     protected CredentialFactory credentialFactory;
// 
//     @Bean(UserlessCredentialFactory.class)
//     protected CredentialFactory anonymousCredentialFactory;
// 
//     protected RedditClient redditClient;
// 
//     protected AuthenticationManager authenticationManager;
// 
//     protected AccountManager accountManager;
// 
//     @Bean(PreferenceTokenStore.class)
//     protected TokenStore tokenStore;
// 
//     @AfterInject
//     public void afterInject() {
//         this.redditClient = new RedditClient(this.userAgentFactory.getUserAgent());
//         this.authenticationManager = AuthenticationManager.get();
//         this.authenticationManager.init(this.redditClient, new RefreshTokenHandler(this.tokenStore, this.redditClient));
//         this.accountManager = new AccountManager(this.redditClient);
//     }
// 
//     /**
//      * Anonymously authenticates if required.
//      * If already authenticated, does not re-authenticate.
//      *
//      * @param authenticationCallback Methods to call on success or failure.
//      */
//     public void authenticateIfRequired(AuthenticationCallback authenticationCallback) {
//         AuthenticationState state = AuthenticationManager.get().checkAuthState();
// 
//         if (state == AuthenticationState.READY) {
//             authenticationCallback.onSuccessfulAuthentication();
//             return;
//         }
// 
//         if (state == AuthenticationState.NEED_REFRESH) {
//             try {
//                 this.authenticationManager.refreshAccessToken(this.getCredentials());
//                 authenticationCallback.onSuccessfulAuthentication();
//             } catch (Exception ex) {
//                 authenticationCallback.onFailedAuthentication(ex);
//             }
// 
//             return;
//         }
// 
//         try {
//             OAuthData authData = this.getClient().getOAuthHelper().easyAuth(this.getAnonymousCredentials());
//             this.getClient().authenticate(authData);
// 
//             authenticationCallback.onSuccessfulAuthentication();
//         } catch (Exception ex) {
//             authenticationCallback.onFailedAuthentication(ex);
//         }
//     }
// 
//     public RedditClient getClient() {
//         return this.redditClient;
//     }
// 
//     public AccountManager getAccountManager() {
//         return this.accountManager;
//     }
// 
//     public AuthenticationManager getAuthenticationManager() {
//         return this.authenticationManager;
//     }
// 
//     public Credentials getCredentials() {
//         return this.credentialFactory.getCredentials();
//     }
// 
//     public Credentials getAnonymousCredentials() {
//         return this.anonymousCredentialFactory.getCredentials();
//     }
// }


import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import net.dean.jraw.http.oauth.Credentials;
import net.dean.jraw.http.oauth.OAuthData;
import net.dean.jraw.http.oauth.OAuthException;
import net.dean.jraw.http.oauth.OAuthHelper;
import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import java.net.URL;
import party.minge.reddit.client.Manager;
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                if (url.contains("code=")) {
                    LoginActivity.this.handleOAuthSuccess(url);
                } else if (url.contains("error=")) {
                    LoginActivity.this.handleOAuthError(url);
                }
            }
        });
    }

    @Background
    protected void handleOAuthSuccess(String url) {
        try {
            OAuthData resp = this.helper.onUserChallenge(url, this.credentials);
            this.manager.getClient().authenticate(resp);
            this.handleSuccess();
        } catch (OAuthException ex) {
            this.handleError(ex.getMessage());
        }
    }

    @Background
    protected void handleOAuthError(String error) {
        Log.d("login", error);
        this.handleError("OAuth failed");
    }

    @UiThread
    protected void handleSuccess() {
// Path: app/src/main/java/party/minge/reddit/client/Manager.java
// @EBean(scope = EBean.Scope.Singleton)
// public class Manager {
//     @Bean(ResFactory.class)
//     protected UserAgentFactory userAgentFactory;
// 
//     @Bean(ResFactory.class)
//     protected CredentialFactory credentialFactory;
// 
//     @Bean(UserlessCredentialFactory.class)
//     protected CredentialFactory anonymousCredentialFactory;
// 
//     protected RedditClient redditClient;
// 
//     protected AuthenticationManager authenticationManager;
// 
//     protected AccountManager accountManager;
// 
//     @Bean(PreferenceTokenStore.class)
//     protected TokenStore tokenStore;
// 
//     @AfterInject
//     public void afterInject() {
//         this.redditClient = new RedditClient(this.userAgentFactory.getUserAgent());
//         this.authenticationManager = AuthenticationManager.get();
//         this.authenticationManager.init(this.redditClient, new RefreshTokenHandler(this.tokenStore, this.redditClient));
//         this.accountManager = new AccountManager(this.redditClient);
//     }
// 
//     /**
//      * Anonymously authenticates if required.
//      * If already authenticated, does not re-authenticate.
//      *
//      * @param authenticationCallback Methods to call on success or failure.
//      */
//     public void authenticateIfRequired(AuthenticationCallback authenticationCallback) {
//         AuthenticationState state = AuthenticationManager.get().checkAuthState();
// 
//         if (state == AuthenticationState.READY) {
//             authenticationCallback.onSuccessfulAuthentication();
//             return;
//         }
// 
//         if (state == AuthenticationState.NEED_REFRESH) {
//             try {
//                 this.authenticationManager.refreshAccessToken(this.getCredentials());
//                 authenticationCallback.onSuccessfulAuthentication();
//             } catch (Exception ex) {
//                 authenticationCallback.onFailedAuthentication(ex);
//             }
// 
//             return;
//         }
// 
//         try {
//             OAuthData authData = this.getClient().getOAuthHelper().easyAuth(this.getAnonymousCredentials());
//             this.getClient().authenticate(authData);
// 
//             authenticationCallback.onSuccessfulAuthentication();
//         } catch (Exception ex) {
//             authenticationCallback.onFailedAuthentication(ex);
//         }
//     }
// 
//     public RedditClient getClient() {
//         return this.redditClient;
//     }
// 
//     public AccountManager getAccountManager() {
//         return this.accountManager;
//     }
// 
//     public AuthenticationManager getAuthenticationManager() {
//         return this.authenticationManager;
//     }
// 
//     public Credentials getCredentials() {
//         return this.credentialFactory.getCredentials();
//     }
// 
//     public Credentials getAnonymousCredentials() {
//         return this.anonymousCredentialFactory.getCredentials();
//     }
// }


// Path: app/src/main/java/party/minge/reddit/LoginActivity.java
import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import net.dean.jraw.http.oauth.Credentials;
import net.dean.jraw.http.oauth.OAuthData;
import net.dean.jraw.http.oauth.OAuthException;
import net.dean.jraw.http.oauth.OAuthHelper;
import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import java.net.URL;
import party.minge.reddit.client.Manager;

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                if (url.contains("code=")) {
                    LoginActivity.this.handleOAuthSuccess(url);
                } else if (url.contains("error=")) {
                    LoginActivity.this.handleOAuthError(url);
                }
            }
        });
    }

    @Background
    protected void handleOAuthSuccess(String url) {
        try {
            OAuthData resp = this.helper.onUserChallenge(url, this.credentials);
            this.manager.getClient().authenticate(resp);
            this.handleSuccess();
        } catch (OAuthException ex) {
            this.handleError(ex.getMessage());
        }
    }

    @Background
    protected void handleOAuthError(String error) {
        Log.d("login", error);
        this.handleError("OAuth failed");
    }

    @UiThread
    protected void handleSuccess() {
        String loginMessage = "Logged in as " + this.manager.getClient().getAuthenticatedUser();