Search in sources :

Example 1 with User

use of com.winsonchiu.reader.data.reddit.User in project Reader by TheKeeperOfPie.

the class ActivityLogin method loadAccountInfo.

private void loadAccountInfo(final String tokenAuth, final String tokenRefresh, final long timeExpire) {
    Request request = new Request.Builder().url(Reddit.OAUTH_URL + "/api/v1/me").header(Reddit.USER_AGENT, Reddit.CUSTOM_USER_AGENT).header(Reddit.AUTHORIZATION, Reddit.BEARER + tokenAuth).header(Reddit.CONTENT_TYPE, Reddit.CONTENT_TYPE_APP_JSON).get().build();
    reddit.load(request).flatMap(UtilsRx.flatMapWrapError(response -> User.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class)))).subscribe(new FinalizingSubscriber<User>() {

        @Override
        public void next(User user) {
            Account account = new Account(user.getName(), Reddit.ACCOUNT_TYPE);
            Intent result = new Intent();
            result.putExtra(AccountManager.KEY_ACCOUNT_NAME, user.getName());
            result.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Reddit.ACCOUNT_TYPE);
            result.putExtra(AccountManager.KEY_AUTHTOKEN, tokenAuth);
            if (getIntent().getBooleanExtra(KEY_IS_NEW_ACCOUNT, false)) {
                Bundle extras = new Bundle();
                extras.putString(KEY_TIME_EXPIRATION, String.valueOf(timeExpire));
                accountManager.addAccountExplicitly(account, tokenRefresh, extras);
                accountManager.setAuthToken(account, Reddit.AUTH_TOKEN_FULL_ACCESS, tokenAuth);
            }
            destroyWebView();
            setAccountAuthenticatorResult(result.getExtras());
            setResult(RESULT_OK, result);
            ActivityLogin.this.finish();
        }
    });
}
Also used : Account(android.accounts.Account) User(com.winsonchiu.reader.data.reddit.User) Bundle(android.os.Bundle) WebResourceRequest(android.webkit.WebResourceRequest) Request(okhttp3.Request) Intent(android.content.Intent)

Example 2 with User

use of com.winsonchiu.reader.data.reddit.User in project Reader by TheKeeperOfPie.

the class ControllerUser method setAccount.

public void setAccount(Account accountUser) {
    boolean accountFound = false;
    Account[] accounts = accountManager.getAccountsByType(Reddit.ACCOUNT_TYPE);
    for (Account account : accounts) {
        if (account.name.equals(accountUser.name)) {
            this.account = account;
            accountFound = true;
            reloadUser();
            break;
        }
    }
    user = new User();
    if (!accountFound) {
        account = null;
    } else {
        user.setName(account.name);
    }
}
Also used : Account(android.accounts.Account) User(com.winsonchiu.reader.data.reddit.User)

Example 3 with User

use of com.winsonchiu.reader.data.reddit.User in project Reader by TheKeeperOfPie.

the class ControllerUser method clearAccount.

public void clearAccount() {
    account = null;
    user = new User();
    for (Listener listener : listeners) {
        listener.onUserLoaded(null);
    }
}
Also used : User(com.winsonchiu.reader.data.reddit.User)

Aggregations

User (com.winsonchiu.reader.data.reddit.User)3 Account (android.accounts.Account)2 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 WebResourceRequest (android.webkit.WebResourceRequest)1 Request (okhttp3.Request)1