package com.haitang.dollprint.utils;
import net.tsz.afinal.http.AjaxParams;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import com.haitang.dollprint.thread.HttpPostTask;
import com.haitang.dollprint.thread.NewVersionDownloadTask;
import com.haitang.dollprint.thread.UserInfoUtils;
import com.haitang.dollprint.utils.TaskService.TaskHandler;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQAuth;
import com.tencent.connect.auth.QQToken;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
/**
* QQ 登录工具类
*
* @author wyd 2015.6.15
*/
public class QQLoginUtils {
private static String TAG = "QQLoginUtils";
public Activity mActivity;
public TaskHandler mHandler;
public Tencent mTencent;
private UserInfo mInfo;
public static QQLoginUtils sQQLoginUtils;
public static String TYPE_LOGIN_QQ = "QQ";
/**
* 创建QQLoginUtils 单例
*
* @param Activity
* @return
*/
public static synchronized QQLoginUtils create(Activity activity) {
if (sQQLoginUtils == null) {
sQQLoginUtils = new QQLoginUtils(activity);
}
return sQQLoginUtils;
}
/** 构造器实例 */
public QQLoginUtils(Activity activity) {
// TODO Auto-generated constructor stub
mActivity = activity;
mTencent = Tencent.createInstance(AppKeyManager.getQQAppKey(mActivity),
mActivity);
}
/** 登录方法 */
public void login(TaskHandler taskHandler) {
mHandler = taskHandler;
if (!mTencent.isSessionValid()) {
Utils.LOGE(TAG, "用户没有授权,需要授权");
mTencent.login((Activity) mActivity, "all", listener);
} else {
mTencent.reAuth((Activity) mActivity, "all", listener);
Utils.LOGE(TAG, "用户已经授权,需要重新授权");
}
}
IUiListener listener = new BaseUiListener() {
@Override
protected void doComplete(JSONObject values) {
Utils.LOGE(TAG, "监听成功 执行更新用户信息");
Utils.LOGE(
TAG,
"mTencent.getAccessToken() " + mTencent.getAccessToken()
+ "\nmTencent.getQQToken() "
+ mTencent.getQQToken() + "\n OpenID = "
+ mTencent.getOpenId() + "\n ExpiresIn = "
+ mTencent.getExpiresIn());
writeSharePref(mTencent.getAccessToken(), mTencent.getOpenId(),
mTencent.getExpiresIn(), mActivity);
updateUserInfo(mTencent.getQQToken());
}
};
/** 更新用户信息 */
public void updateUserInfo(QQToken qqToken) {
Utils.LOGE(TAG, "\nmTencent.getQQToken().isSessionValid() = "
+ mTencent.getQQToken().isSessionValid());
if (mTencent != null && mTencent.isSessionValid()) {
IUiListener listener = new BaseUiListener() {
@Override
public void onError(UiError e) {
// TODO Auto-generated method stub
}
@Override
public void onComplete(final Object response) {
new Thread() {
@Override
public void run() {
JSONObject json = (JSONObject) response;
UserInfoUtils.clearUserInfo(mActivity);
Common.setSPString(mActivity, "login_type",
TYPE_LOGIN_QQ);
UserInfoUtils.setUserAlreadyBind(mActivity, false);
UserInfoUtils.setUserAlreadyLogin(mActivity, true);
try {
if (json.has("nickname")) {
UserInfoUtils.setNickName(mActivity,
json.getString("nickname"));
} else {
UserInfoUtils.setNickName(mActivity, "无");
}
if (json.has("figureurl")) {
UserInfoUtils.setsUserIconImg(mActivity,
json.getString("figureurl_qq_2"));
}
if (null != mHandler) {
mHandler.sendSucessMessage();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
@Override
public void onCancel() {
}
};
mInfo = new UserInfo(mActivity, qqToken);
mInfo.getUserInfo(listener);
}
}
public boolean isAntoLogin = false;
/** QQ自动登录 */
public void AutoLogin() {
isAntoLogin = true;
ThridLoginInfoUtils thridLoginInfoUtils = readSharePref(mActivity);
mTencent.setAccessToken(thridLoginInfoUtils.getAccessToken(),
thridLoginInfoUtils.getExpires_In() + "");
mTencent.setOpenId(thridLoginInfoUtils.getUid());
Utils.LOGE(
TAG,
"mTencent.isSessionValid() = " + mTencent.isSessionValid()
+ "\nmTencent.getQQToken() = " + mTencent.getQQToken()
+ "\nmTencent.getAccessToken() = "
+ mTencent.getAccessToken());
if (null != mTencent && mTencent.isSessionValid()) {
Utils.LOGE(TAG, "QQ自动登录在有效期内!");
updateUserInfo(mTencent.getQQToken());
} else {
Utils.LOGE(TAG, "QQ自动登录失败!");
exitLogin(mActivity);
}
// mAccessToken.setToken(mAccessToken.getRefreshToken());
}
/** 退出QQ登录 */
public static void exitLogin(Activity activity) {
ToolUtil.clearUserLocalDate(activity);
QQLoginUtils.create(activity).mTencent.logout(activity);
// QQLoginUtils.create(activity).mQQAuth.logout(activity);
ThridLoginInfoUtils.clear(activity, ThridLoginInfoUtils.sQQlogin);
}
/** 将微博信息写入共享存储 */
public static void writeSharePref(String accessToken, String openID,
long expires_In, Activity activity) {
ThridLoginInfoUtils loginInfoUtils = new ThridLoginInfoUtils();
loginInfoUtils.setAccessToken(accessToken);
loginInfoUtils.setUid(openID);
loginInfoUtils.setExpires_In(expires_In);
loginInfoUtils.setsThridLoginType(ThridLoginInfoUtils.sQQlogin);
ThridLoginInfoUtils.writeAccessToken(activity, loginInfoUtils);
}
/** 读取共享存储中微博信息 */
public static ThridLoginInfoUtils readSharePref(Activity activity) {
ThridLoginInfoUtils loginInfoUtils = ThridLoginInfoUtils
.readThridLoginInfo(activity, ThridLoginInfoUtils.sQQlogin);
Utils.LOGE(TAG, "Token = " + loginInfoUtils.getAccessToken());
Utils.LOGE(TAG, "Uid = " + loginInfoUtils.getUid());
return loginInfoUtils;
}
/** 监听登录事件 */
public class BaseUiListener implements IUiListener {
@Override
public void onComplete(Object response) {
if (!isAntoLogin) {
ToastUtil.showToast(mActivity, "登录成功" + response.toString());
}
doComplete((JSONObject) response);
}
protected void doComplete(JSONObject values) {
}
@Override
public void onError(UiError e) {
if (!isAntoLogin) {
ToastUtil.showToast(mActivity, "登录出错!" + e.errorDetail);
}
}
@Override
public void onCancel() {
if (!isAntoLogin) {
ToastUtil.showToast(mActivity, "登录取消!");
}
}
}
}