Skip to content

Commit

Permalink
优化appId为空
Browse files Browse the repository at this point in the history
为空时不抛异常,只打赢log和回调callback的socialError方法。
  • Loading branch information
liujinwei committed Jun 28, 2018
1 parent 689ab65 commit e7c2284
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.v7.app.AppCompatActivity;

import net.arvin.socialhelper.SocialHelper;
import net.arvin.socialhelper.sample.utils.SocialUtil;

public class MainActivity extends AppCompatActivity {

Expand All @@ -15,14 +16,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

socialHelper = new SocialHelper.Builder()
.setQqAppId("")
.setWxAppId("")
.setWxAppSecret("")
.setWbAppId("")
.setWbRedirectUrl("")
.build();

socialHelper = SocialUtil.getInstance().socialHelper();
}

@Override
Expand Down
23 changes: 22 additions & 1 deletion socialhelper/src/main/java/net/arvin/socialhelper/QQHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.tencent.connect.UserInfo;
Expand All @@ -27,6 +28,7 @@
final class QQHelper implements ISocial {
private Activity activity;
private Tencent tencent;
private String appId;

private SocialLoginCallback loginCallback;
private IUiListener loginListener;
Expand All @@ -38,8 +40,10 @@ final class QQHelper implements ISocial {

QQHelper(Activity activity, String appId) {
this.activity = activity;
this.appId = appId;
if (TextUtils.isEmpty(appId)) {
throw new RuntimeException("QQ's appId is empty!");
Log.w("QQHelper", "QQ's appId is empty!");
return;
}
tencent = Tencent.createInstance(appId, activity.getApplicationContext());
}
Expand Down Expand Up @@ -76,6 +80,9 @@ public void onComplete(Object o) {
@Override
public void login(SocialLoginCallback callback) {
this.loginCallback = callback;
if (baseVerify(callback)) {
return;
}
initLoginListener();
if (!tencent.isSessionValid()) {
tencent.login(activity, "all", loginListener);
Expand Down Expand Up @@ -139,6 +146,9 @@ public ThirdInfoEntity createThirdInfo() {
@Override
public void share(SocialShareCallback callback, ShareEntity shareInfo) {
this.shareCallback = callback;
if (baseVerify(callback)) {
return;
}
if (!SocialUtil.isQQInstalled(activity)) {
if (callback != null) {
callback.socialError(activity.getString(R.string.social_qq_uninstall));
Expand All @@ -153,6 +163,17 @@ public void share(SocialShareCallback callback, ShareEntity shareInfo) {
}
}

/*基本信息验证*/
private boolean baseVerify(SocialCallback callback) {
if (TextUtils.isEmpty(appId)) {
if (callback != null) {
callback.socialError(activity.getString(R.string.social_error_appid_empty));
}
return true;
}
return false;
}

@Override
public void onDestroy() {
if (activity != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,6 @@ public void shareWB(Activity activity, ShareEntity shareInfo, SocialShareCallbac
wbHelper.share(callback, shareInfo);
}

@Deprecated
public void shareQQ(Activity activity, SocialShareCallback callback, ShareEntity shareInfo) {
clear();
qqHelper = new QQHelper(activity, builder.getQqAppId());
qqHelper.share(callback, shareInfo);
}

@Deprecated
public void shareWX(Activity activity, SocialShareCallback callback, ShareEntity shareInfo) {
clear();
wxHelper = new WXHelper(activity, builder.getWxAppId(), builder.getWxAppSecret());
wxHelper.share(callback, shareInfo);
}

@Deprecated
public void shareWB(Activity activity, SocialShareCallback callback, ShareEntity shareInfo) {
clear();
wbHelper = new WBHelper(activity, builder.getWbAppId(), builder.getWbRedirectUrl());
wbHelper.share(callback, shareInfo);
}

/**
* qq登录和分享以及微博登录都需要在其当前的activity的onActivityResult中调用该方法
*/
Expand Down
24 changes: 23 additions & 1 deletion socialhelper/src/main/java/net/arvin/socialhelper/WBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.sina.weibo.sdk.WbSdk;
Expand All @@ -30,6 +31,7 @@
import com.sina.weibo.sdk.share.WbShareHandler;
import com.sina.weibo.sdk.utils.Utility;

import net.arvin.socialhelper.callback.SocialCallback;
import net.arvin.socialhelper.callback.SocialLoginCallback;
import net.arvin.socialhelper.callback.SocialShareCallback;
import net.arvin.socialhelper.entities.ShareEntity;
Expand All @@ -53,6 +55,7 @@ final class WBHelper implements ISocial {

private Activity activity;
private String appId;
private String redirectUrl;
private SsoHandler mSsoHandler;

private SocialLoginCallback loginCallback;
Expand All @@ -66,8 +69,10 @@ final class WBHelper implements ISocial {
WBHelper(Activity activity, String appId, String redirectUrl) {
this.activity = activity;
this.appId = appId;
this.redirectUrl = redirectUrl;
if (TextUtils.isEmpty(appId) || TextUtils.isEmpty(redirectUrl)) {
throw new RuntimeException("WeBo's appId or redirectUrl is empty!");
Log.w("WBHelper", "WeBo's appId or redirectUrl is empty!");
return;
}
WbSdk.install(activity.getApplicationContext(), new AuthInfo(activity.getApplicationContext(), appId, redirectUrl, SCOPE));
}
Expand All @@ -78,11 +83,25 @@ final class WBHelper implements ISocial {
@Override
public void login(SocialLoginCallback callback) {
this.loginCallback = callback;
if (baseVerify(callback)) {
return;
}
initLoginListener();
mSsoHandler = new SsoHandler(activity);
mSsoHandler.authorize(wbAuthCallback);
}

/*基本信息验证*/
private boolean baseVerify(SocialCallback callback) {
if (TextUtils.isEmpty(appId) || TextUtils.isEmpty(redirectUrl)) {
if (callback != null) {
callback.socialError(activity.getString(R.string.social_error_appid_empty));
}
return true;
}
return false;
}

/**
* 2、被调用,回调到{@link #wbAuthCallback},然后调用{@link #wbAuthCallback}的onSuccess方法
*/
Expand Down Expand Up @@ -174,6 +193,9 @@ public ThirdInfoEntity createThirdInfo() {
@Override
public void share(SocialShareCallback callback, ShareEntity shareInfo) {
this.shareCallback = callback;
if (baseVerify(callback)) {
return;
}
if (!WbSdk.isWbInstall(activity)) {
if (callback != null) {
callback.socialError(activity.getString(R.string.social_wb_uninstall));
Expand Down
32 changes: 23 additions & 9 deletions socialhelper/src/main/java/net/arvin/socialhelper/WXHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
Expand All @@ -25,6 +26,7 @@
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import net.arvin.socialhelper.callback.SocialCallback;
import net.arvin.socialhelper.callback.SocialLoginCallback;
import net.arvin.socialhelper.callback.SocialShareCallback;
import net.arvin.socialhelper.entities.ShareEntity;
Expand Down Expand Up @@ -63,7 +65,8 @@ final class WXHelper implements ISocial {
this.appSecret = appSecret;

if (TextUtils.isEmpty(appId) || TextUtils.isEmpty(appSecret)) {
throw new RuntimeException("Wechat's appId or appSecret is empty!");
Log.w("WXHelper", "Wechat's appId or appSecret is empty!");
return;
}

api = WXAPIFactory.createWXAPI(activity, appId, true);
Expand All @@ -77,10 +80,7 @@ final class WXHelper implements ISocial {
@Override
public void login(SocialLoginCallback callback) {
this.loginCallback = callback;
if (!api.isWXAppInstalled()) {
if (callback != null) {
callback.socialError(activity.getString(R.string.social_wx_uninstall));
}
if (baseVerify(callback)) {
return;
}
initLoginReceiver();
Expand Down Expand Up @@ -179,10 +179,7 @@ public ThirdInfoEntity createThirdInfo() {
@Override
public void share(SocialShareCallback callback, ShareEntity shareInfo) {
this.shareCallback = callback;
if (!api.isWXAppInstalled()) {
if (callback != null) {
callback.socialError(activity.getString(R.string.social_wx_uninstall));
}
if (baseVerify(callback)) {
return;
}
//是否分享到朋友圈,微信4.2以下不支持朋友圈
Expand Down Expand Up @@ -346,6 +343,23 @@ private boolean addTitleSummaryAndThumb(WXMediaMessage msg, Bundle params) {
return false;
}

/*基本信息验证*/
private boolean baseVerify(SocialCallback callback) {
if (TextUtils.isEmpty(appId) || TextUtils.isEmpty(appSecret)) {
if (callback != null) {
callback.socialError(activity.getString(R.string.social_error_appid_empty));
}
return true;
}
if (!api.isWXAppInstalled()) {
if (callback != null) {
callback.socialError(activity.getString(R.string.social_wx_uninstall));
}
return true;
}
return false;
}

private boolean notFoundFile(String filePath) {
if (!TextUtils.isEmpty(filePath)) {
File file = new File(filePath);
Expand Down
1 change: 1 addition & 0 deletions socialhelper/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<string name="social_wb_uninstall">微博未安装</string>
<string name="social_img_not_found">分享图片未找到</string>
<string name="social_wx_version_low_error">微信4.2以上才支持分享朋友圈,请升级微信</string>
<string name="social_error_appid_empty">appId或appSecret或redirectUrl为空</string>
</resources>

0 comments on commit e7c2284

Please sign in to comment.