Skip to content

Commit

Permalink
升级微博sdk,修复通过微博网页版登录时遮罩未消失的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
kale committed Mar 29, 2016
1 parent 8a153aa commit 590e9fa
Show file tree
Hide file tree
Showing 19 changed files with 90 additions and 94 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
buildToolsVersion '23.0.2'

defaultConfig {
applicationId "com.liulishuo.engzo"
Expand All @@ -22,7 +22,7 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:appcompat-v7:23.1.1'
compile project (':share')

}
4 changes: 2 additions & 2 deletions share/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
provided 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:support-annotations:22.1.1'
}

Binary file not shown.

This file was deleted.

2 changes: 0 additions & 2 deletions share/src/main/java/com/liulishuo/share/ShareBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ public class ShareBlock {
@Retention(RetentionPolicy.SOURCE)
@IntDef({WEIXIN_FRIEND, WEIXIN_FRIEND_ZONE, QQ_ZONE, QQ_FRIEND, WEIBO_TIME_LINE})
public @interface ShareType {

}


private static ShareBlock mInstance;

private ShareBlock() {
Expand Down
2 changes: 1 addition & 1 deletion share/src/main/java/com/liulishuo/share/util/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface CallBack {
/**
* 异步的Get请求
*/
public static void doGetAsyn(final String urlStr, final CallBack callBack) {
public static void doGetAsync(final String urlStr, final CallBack callBack) {
new Thread() {
public void run() {
String result = doGet(urlStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,26 @@

/**
* 该类定义了微博授权时所需要的参数。
*
*
* @author SINA
* @since 2013-10-07
*/
class AccessTokenKeeper {
private static final String PREFERENCES_NAME = "com_weibo_sdk_android";
private static final String KEY_UID = "uid";
private static final String KEY_ACCESS_TOKEN = "access_token";
private static final String KEY_EXPIRES_IN = "expires_in";

private static final String KEY_UID = "uid";
private static final String KEY_ACCESS_TOKEN = "access_token";
private static final String KEY_EXPIRES_IN = "expires_in";

/**
* 保存 Token 对象到 SharedPreferences。
*
*
* @param context 应用程序上下文环境
* @param token Token 对象
*/
protected static void writeAccessToken(Context context, Oauth2AccessToken token) {
if (null == context || null == token) {
return;
}

SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
Editor editor = pref.edit();
editor.putString(KEY_UID, token.getUid());
Expand All @@ -57,16 +55,14 @@ protected static void writeAccessToken(Context context, Oauth2AccessToken token)

/**
* 从 SharedPreferences 读取 Token 信息。
*
*
* @param context 应用程序上下文环境
*
* @return 返回 Token 对象
*/
protected static Oauth2AccessToken readAccessToken(Context context) {
if (null == context) {
return null;
}

Oauth2AccessToken token = new Oauth2AccessToken();
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
token.setUid(pref.getString(KEY_UID, ""));
Expand All @@ -77,14 +73,13 @@ protected static Oauth2AccessToken readAccessToken(Context context) {

/**
* 清空 SharedPreferences 中 Token信息。
*
*
* @param context 应用程序上下文环境
*/
protected static void clear(Context context) {
if (null == context) {
return;
}

SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
Editor editor = pref.edit();
editor.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
* @author Jack Tony
* @date 2015/10/26
*/
public class SL_WeiBoLoginActivity extends Activity{
public class SL_WeiBoLoginActivity extends Activity {

private boolean isFirstIn = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// 防止不保留活动情况下activity被重置后直接进行操作的情况
WeiboLoginManager.sendLoginMsg(this);
} else {
isFirstIn = false;
}
}

Expand All @@ -24,4 +28,15 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
WeiboLoginManager.handlerOnActivityResult(requestCode, resultCode, data);
finish();
}

@Override
protected void onResume() {
super.onResume();
if (isFirstIn) {
isFirstIn = false;
} else {
// 这里处理通过网页登录无回调的问题
finish();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,39 @@
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.widget.Toast;

/**
* @author Jack Tony
* @date 2015/10/14
*/
public class SL_WeiBoShareActivity extends Activity implements IWeiboHandler.Response {

private boolean mIsFirstTime = true;
private boolean isFirstIn = true;

@CallSuper
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// 防止不保留活动情况下activity被重置后直接进行操作的情况

if (!WeiboShareManager.isWeiBoInstalled(this)) {
Toast.makeText(getApplicationContext(), "请先安装微博", Toast.LENGTH_SHORT).show();
finish();
return;
}

WeiboShareManager.sendShareMsg(this);
}

// 当 Activity 被重新初始化时(该 Activity 处于后台时,可能会由于内存不足被杀掉了),
// 需要调用 {@link IWeiboShareAPI#handleWeiboResponse} 来接收微博客户端返回的数据。
// 执行成功,返回 true,并调用 {@link IWeiboHandler.Response#onResponse};
// 失败返回 false,不调用上述回调
if (savedInstanceState != null) {
} else {
isFirstIn = false;
/**
* 当 Activity 被重新初始化时(该 Activity 处于后台时,可能会由于内存不足被杀掉了),
* 需要调用 {@link IWeiboShareAPI#handleWeiboResponse} 来接收微博客户端返回的数据。
* 执行成功,返回 true,并调用 {@link IWeiboHandler.Response#onResponse};
* 失败返回 false,不调用上述回调
*/
IWeiboShareAPI API = WeiboShareSDK.createWeiboAPI(getApplicationContext(),
ShareBlock.getInstance().weiboAppId);

API.handleWeiboResponse(getIntent(), this);
}
}
Expand All @@ -45,31 +51,30 @@ protected void onCreate(Bundle savedInstanceState) {
* 因为微博客户端在用户取消分享后,用户点击保存到草稿箱后就不能接收到回调。
* 因此,在这里必须进行强制关闭,不能依赖回调来关闭。
*/
@CallSuper
@Override
protected void onResume() {
super.onResume();
if (mIsFirstTime) {
mIsFirstTime = false;
if (isFirstIn) {
isFirstIn = false;
} else {
// 这里处理保存到草稿箱的逻辑
WeiboShareManager.parseShareResp(WBConstants.ErrorCode.ERR_CANCEL, "weibo cancel");
finish();
}
}

@CallSuper
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// 从当前应用唤起微博并进行分享后,返回到当前应用时,需要在此处调用该函数
// 来接收微博客户端返回的数据;执行成功,返回 true,并调用
// {@link IWeiboHandler.Response#onResponse};失败返回 false,不调用上述回调
/**
* 从当前应用唤起微博并进行分享后,返回到当前应用时,需要在此处调用该函数
* 来接收微博客户端返回的数据;执行成功,返回 true,并调用
* {@link IWeiboHandler.Response#onResponse};失败返回 false,不调用上述回调
*/
IWeiboShareAPI API = WeiboShareSDK.createWeiboAPI(getApplicationContext(), ShareBlock.getInstance().weiboAppId);
API.handleWeiboResponse(intent, this); // 当前应用唤起微博分享后,返回当前应用
}

@CallSuper
@Override
public void onResponse(BaseResponse baseResponse) {
WeiboShareManager.parseShareResp(baseResponse.errCode, baseResponse.errMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ public class WeiboLoginManager implements ILoginManager {
/**
* 注意:SsoHandler 仅当 SDK 支持 SSO 时有效
*/
private static SsoHandler mSsoHandler;
private static SsoHandler ssoHandler;

private static LoginListener mLoginListener;
private static LoginListener loginListener;

@Override
public void login(@NonNull final Activity activity, @Nullable final LoginListener loginListener) {
mLoginListener = loginListener;
// 启动activity后,应该立刻调用{sendLoginMsg}方法
WeiboLoginManager.loginListener = loginListener;
/**
* 启动activity后,应该立刻调用{@link #sendLoginMsg}方法
*/
activity.startActivity(new Intent(activity, SL_WeiBoLoginActivity.class));
}

Expand All @@ -53,12 +55,12 @@ protected static void sendLoginMsg(final Activity activity) {

AccessTokenKeeper.clear(activity);

AuthInfo authInfo = new AuthInfo(activity.getApplicationContext(), appId,
AuthInfo authInfo = new AuthInfo(activity, appId,
ShareBlock.getInstance().weiboRedirectUrl,
ShareBlock.getInstance().weiboScope);

mSsoHandler = new SsoHandler(activity, authInfo);
mSsoHandler.authorize(new WeiboAuthListener() {
ssoHandler = new SsoHandler(activity, authInfo);
ssoHandler.authorize(new WeiboAuthListener() {
/*
* 1. SSO 授权时,需要在 onActivityResult 中调用 {@link SsoHandler#authorizeCallBack} 后,
* 该回调才会被执行。
Expand All @@ -69,8 +71,8 @@ public void onComplete(Bundle values) {
final Oauth2AccessToken accessToken = Oauth2AccessToken.parseAccessToken(values);
if (accessToken != null && accessToken.isSessionValid()) {
AccessTokenKeeper.writeAccessToken(activity, accessToken);
if (mLoginListener != null) {
mLoginListener.onSuccess(accessToken.getToken(),
if (loginListener != null) {
loginListener.onSuccess(accessToken.getToken(),
accessToken.getUid(),
accessToken.getExpiresTime() / 1000000,
oAuthData2Json(accessToken));
Expand All @@ -80,15 +82,15 @@ public void onComplete(Bundle values) {

@Override
public void onWeiboException(WeiboException e) {
if (mLoginListener != null) {
mLoginListener.onError(e.getMessage());
if (loginListener != null) {
loginListener.onError(e.getMessage());
}
}

@Override
public void onCancel() {
if (mLoginListener != null) {
mLoginListener.onCancel();
if (loginListener != null) {
loginListener.onCancel();
}
}
});
Expand All @@ -99,16 +101,14 @@ public void onCancel() {
* SSO 授权回调 重要:发起 SSO 登陆的 Activity 必须重写 onActivityResult
*/
protected static void handlerOnActivityResult(int requestCode, int resultCode, Intent data) {
if (mSsoHandler == null) {
if (mLoginListener != null) {
mLoginListener.onError("认证失败,请重试");
}
} else {
mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
if (ssoHandler != null) {
ssoHandler.authorizeCallBack(requestCode, resultCode, data);
}
}

private static String oAuthData2Json(@NonNull Oauth2AccessToken data) {
private static
@Nullable
String oAuthData2Json(@NonNull Oauth2AccessToken data) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("uid", data.getUid());
Expand All @@ -124,7 +124,6 @@ private static String oAuthData2Json(@NonNull Oauth2AccessToken data) {

// ---------------------------------- 得到用户信息 -------------------------------------


@Override
public void getUserInformation(@NonNull String accessToken, @NonNull String userId, @Nullable UserInfoListener listener) {
getUserInfo(accessToken, userId, listener);
Expand All @@ -150,9 +149,8 @@ protected AuthUserInfo doInBackground(Void... params) {

AuthUserInfo userInfo = null;
try {
JSONObject jsonObject = new JSONObject(respStr);

userInfo = new AuthUserInfo();
JSONObject jsonObject = new JSONObject(respStr);
userInfo.nickName = jsonObject.getString("screen_name");
userInfo.sex = jsonObject.getString("gender");
userInfo.headImgUrl = jsonObject.getString("avatar_large");
Expand All @@ -164,7 +162,7 @@ protected AuthUserInfo doInBackground(Void... params) {
}

@Override
protected void onPostExecute(AuthUserInfo userInfo) {
protected void onPostExecute(@Nullable AuthUserInfo userInfo) {
super.onPostExecute(userInfo);
if (listener != null) {
if (userInfo != null) {
Expand Down
Loading

0 comments on commit 590e9fa

Please sign in to comment.