Search in sources :

Example 1 with Token

use of com.codez.collar.bean.Token in project Collar by CodeZsx.

the class AccessTokenManager method switchToken.

public void switchToken(Context context, int positionInCache, OnTokenSwitchListener onTokenSwitchListener) {
    TokenList tokenList = TokenList.parse(SDCardUtil.get(context, SDCardUtil.getSDCardPath() + TOKENLIST_CACHE_DIR, TOKENLIST_CACHE_NAME));
    if (tokenList.getTokenList().size() > 0) {
        Token token = tokenList.getTokenList().get(positionInCache);
        updateAccessToken(context, token.getToken(), token.getExpiresIn(), token.getRefreshToken(), token.getUid());
        onTokenSwitchListener.onSuccess();
    } else {
        onTokenSwitchListener.OnError("切换失败,本地token缓存为空");
    }
}
Also used : TokenList(com.codez.collar.bean.TokenList) Token(com.codez.collar.bean.Token)

Example 2 with Token

use of com.codez.collar.bean.Token in project Collar by CodeZsx.

the class AccessTokenManager method addToken.

public void addToken(Context context, String accessToken, String expiresIn, String refreshToken, String uid) {
    Gson gson = new Gson();
    Token token = new Token(accessToken, expiresIn, refreshToken, uid);
    TokenList tokenList = TokenList.parse(SDCardUtil.get(context, SDCardUtil.getSDCardPath() + TOKENLIST_CACHE_DIR, TOKENLIST_CACHE_NAME));
    if (tokenList == null || tokenList.getTokenList().size() == 0) {
        tokenList = new TokenList();
    }
    // 重复登录的话,不进行重复token的添加
    for (int i = 0; i < tokenList.getTokenList().size(); i++) {
        if (tokenList.getTokenList().get(i).getUid().equals(uid)) {
            updateAccessToken(context, accessToken, expiresIn, refreshToken, uid);
            return;
        }
    }
    tokenList.addTokenList(token);
    tokenList.setCurrUid(uid);
    SDCardUtil.put(context, SDCardUtil.getSDCardPath() + TOKENLIST_CACHE_DIR, TOKENLIST_CACHE_NAME, gson.toJson(tokenList));
    updateAccessToken(context, accessToken, expiresIn, refreshToken, uid);
}
Also used : Gson(com.google.gson.Gson) Token(com.codez.collar.bean.Token) TokenList(com.codez.collar.bean.TokenList)

Example 3 with Token

use of com.codez.collar.bean.Token in project Collar by CodeZsx.

the class AccessTokenManager method deleteToken.

public void deleteToken(Context context, String uid) {
    Gson gson = new Gson();
    TokenList tokenList = TokenList.parse(SDCardUtil.get(context, SDCardUtil.getSDCardPath() + TOKENLIST_CACHE_DIR, TOKENLIST_CACHE_NAME));
    for (int i = 0; i < tokenList.getTokenList().size(); i++) {
        if (tokenList.getTokenList().get(i).getUid().equals(uid)) {
            tokenList.removeTokenList(i);
        }
    }
    // 当前账户list为空
    if (tokenList.getTokenList().size() == 0) {
        SDCardUtil.put(context, SDCardUtil.getSDCardPath() + TOKENLIST_CACHE_DIR, TOKENLIST_CACHE_NAME, gson.toJson(tokenList));
        // 移除当前使用账户
        AccessTokenKeeper.getInstance().clear();
        return;
    }
    // 当前登录账户为删除账户
    if (AccessTokenKeeper.getInstance().readAccessToken().getUid().equals(uid)) {
        AccessTokenKeeper.getInstance().clear();
        // 当前登录账户被删除后,使用list中的第一个账户
        Token token = tokenList.getTokenList().get(0);
        tokenList.setCurrUid(tokenList.getTokenList().get(0).getUid());
        updateAccessToken(context, token.getToken(), token.getExpiresIn(), token.getRefreshToken(), token.getUid());
    }
    // 其他情况,即删除账户不为当前账户:无操作
    SDCardUtil.put(context, SDCardUtil.getSDCardPath() + TOKENLIST_CACHE_DIR, TOKENLIST_CACHE_NAME, gson.toJson(tokenList));
}
Also used : Gson(com.google.gson.Gson) TokenList(com.codez.collar.bean.TokenList) Token(com.codez.collar.bean.Token)

Example 4 with Token

use of com.codez.collar.bean.Token in project Collar by CodeZsx.

the class AccessTokenManager method switchToken.

public void switchToken(Context context, String uid) {
    TokenList tokenList = TokenList.parse(SDCardUtil.get(context, SDCardUtil.getSDCardPath() + TOKENLIST_CACHE_DIR, TOKENLIST_CACHE_NAME));
    for (int i = 0; i < tokenList.getTokenList().size(); i++) {
        if (tokenList.getTokenList().get(i).getUid().equals(uid)) {
            Token token = tokenList.getTokenList().get(i);
            updateAccessToken(context, token.getToken(), token.getExpiresIn(), token.getRefreshToken(), token.getUid());
        }
    }
}
Also used : TokenList(com.codez.collar.bean.TokenList) Token(com.codez.collar.bean.Token)

Example 5 with Token

use of com.codez.collar.bean.Token in project Collar by CodeZsx.

the class AccountActivity method loadData.

private void loadData() {
    if (!AccessTokenKeeper.getInstance().readAccessToken().isSessionsValid()) {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
    AccessTokenManager manager = new AccessTokenManager();
    TokenList tokenList = manager.getTokenList(this);
    final List<Token> lists = tokenList.getTokenList();
    for (Token token : lists) {
        HttpUtils.getInstance().getUserService().getUserInfo(token.getUid(), null).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<UserBean>() {

            @Override
            public void onCompleted() {
                L.e("onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                L.e("onError");
            }

            @Override
            public void onNext(UserBean userBean) {
                mAdapter.add(userBean);
                mAdapter.notifyDataSetChanged();
            }
        });
    }
}
Also used : AccessTokenManager(com.codez.collar.auth.AccessTokenManager) UserBean(com.codez.collar.bean.UserBean) Intent(android.content.Intent) TokenList(com.codez.collar.bean.TokenList) Token(com.codez.collar.bean.Token)

Aggregations

Token (com.codez.collar.bean.Token)5 TokenList (com.codez.collar.bean.TokenList)5 Gson (com.google.gson.Gson)2 Intent (android.content.Intent)1 AccessTokenManager (com.codez.collar.auth.AccessTokenManager)1 UserBean (com.codez.collar.bean.UserBean)1