@Override protected void onSuccess(TokenInfo tokenInfo) { super.onSuccess(tokenInfo); if (tokenInfo != null && account.getUser().getIdstr().equalsIgnoreCase(AppContext.getUser().getIdstr())) { long validSecond = Long.parseLong(tokenInfo.getCreate_at()) + Long.parseLong(tokenInfo.getExpire_in()); if (System.currentTimeMillis() > validSecond * 1000) { new AlertDialogWrapper.Builder(MainActivity.this) .setTitle(R.string.remind) .setMessage(R.string.account_expired) .setNegativeButton(R.string.no, null) .setPositiveButton( R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { LoginFragment.launch( MainActivity.this, account.getAccount(), account.getPassword(), 2000); } }) .show(); } } }
@Override public TokenInfo workInBackground(AccountBean... params) throws TaskException { Logger.w("run CheckAccountValidTask"); TokenInfo token = null; TokenInfo adToken = null; try { AccountBean account = params[0]; // Aisen授权 try { token = SinaSDK.getInstance(account.getAccessToken()) .getTokenInfo(account.getAccessToken().getToken()); } catch (TaskException e) { e.printStackTrace(); if ("21327".equals(e.getCode()) || "21317".equals(e.getCode())) { token = new TokenInfo(); token.setExpire_in(0); } } if (token != null) account.getAccessToken().setExpires_in(token.getExpire_in()); // Weico授权 try { if (account.getAdvancedToken() != null) adToken = SinaSDK.getInstance(account.getAdvancedToken()) .getTokenInfo(account.getAdvancedToken().getToken()); else { adToken = new TokenInfo(); adToken.setExpire_in(0); } } catch (TaskException e) { e.printStackTrace(); if ("21327".equals(e.getCode()) || "21317".equals(e.getCode())) { adToken = new TokenInfo(); adToken.setExpire_in(0); } } if (account.getAdvancedToken() != null && adToken != null) account.getAdvancedToken().setExpires_in(adToken.getExpire_in()); } catch (Throwable e) { } if (token != null) { token.setUid(params[0].getUid()); } return token; }
@Override public TokenInfo workInBackground(Void... params) throws TaskException { TokenInfo tokenInfo = null; try { tokenInfo = SinaSDK.getInstance(account.getToken()).getTokenInfo(account.get_token()); } catch (TaskException e) { e.printStackTrace(); if ("21327".equals(e.getCode()) || "21317".equals(e.getCode())) { tokenInfo = new TokenInfo(); tokenInfo.setCreate_at("0"); tokenInfo.setExpire_in("0"); } } if (tokenInfo != null) { return tokenInfo; } return null; }
@Override public WeiBoUser workInBackground(String... params) throws TaskException { // 1、请求授权 AccessToken accessToken = SinaSDK.getInstance(null).getAccessToken(params[0]); AccountBean account = new AccountBean(); account.setToken(accessToken); // 2、加载用户信息 publishProgress(R.string.account_load_userinfo); WeiBoUser user = SinaSDK.getInstance(accessToken).userShow(accessToken.getUid(), null); // 3、加载分组信息 publishProgress(R.string.account_load_groups); Groups groups = SinaSDK.getInstance(accessToken).friendshipGroups(); // 4、更新新账户到DB account.setUser(user); account.setGroups(groups); account.setAccount(mAccount); account.setPassword(mPassword); account.setUserId(user.getIdstr()); // 2014-09-18 移除了所有账户信息,包括LoggedIn,所以需要调用AccountFragment.login() AccountDB.remove(account.getUserId()); AccountDB.newAccount(account); if (AppContext.isLogedin() && // 2014-09-18 仅更新登录用户数据 AppContext.getUser().getIdstr().equals(user.getIdstr())) { AccountFragment.login(account, false); } if (getActivity() != null) { Intent data = new Intent(); data.putExtra("token", accessToken); getActivity().setResult(Activity.RESULT_OK, data); } return user; }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_AUTH) { if (resultCode == Activity.RESULT_OK) { AccountBean accountBean = (AccountBean) data.getSerializableExtra("account"); AppContext.getAccount().setAccessToken(accountBean.getAccessToken()); if (accountBean.getUser() != null) { AppContext.getAccount().setUser(accountBean.getUser()); } if (accountBean.getGroups() != null) { AppContext.getAccount().setGroups(accountBean.getGroups()); } AccountUtils.newAccount(AppContext.getAccount()); AccountUtils.setLogedinAccount(AppContext.getAccount()); login(); } } }
public static void runCheckAccountTask(AccountBean account) { // 已经过期了就不用检查了 if (!account.getAccessToken().isExpired()) { new CheckAccountValidTask().execute(account); } }