Skip to content

Commit

Permalink
精简了api,更加语义化
Browse files Browse the repository at this point in the history
  • Loading branch information
Kale committed Sep 14, 2018
1 parent 42d8fcf commit a2fbe8e
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/liulishuo/engzo/AppApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void onCreate() {
WeiBoPlatform.KEY_REDIRECT_URL, weibo_redirect_url,

WeiXinPlatform.KEY_APP_ID, weixin_app_id,
WeiXinPlatform.KEY_SECRET_KEY, weixin_secret
WeiXinPlatform.KEY_SECRET, weixin_secret
),
Arrays.asList(
QQPlatform.class,
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/liulishuo/engzo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void onClick(View v) {
ShareLoginLib.doShare(this, QQPlatform.ZONE, mShareContent, shareListener);
break;
case R.id.分享到微博:
ShareLoginLib.doShare(this, WeiBoPlatform.TIME_LINE, mShareContent, shareListener);
ShareLoginLib.doShare(this, WeiBoPlatform.TIMELINE, mShareContent, shareListener);
break;
case R.id.分享到微博故事:
ShareLoginLib.doShare(this, WeiBoPlatform.STORY, mShareContent, shareListener);
Expand All @@ -128,16 +128,16 @@ public void onClick(View v) {
null, // url
BitmapFactory.decodeByteArray(mShareContent.getThumbBmpBytes(), 0, mShareContent.getThumbBmpBytes().length));

ShareLoginLib.doShare(this, WeiBoPlatform.TIME_LINE, webPage, shareListener);
ShareLoginLib.doShare(this, WeiBoPlatform.TIMELINE, webPage, shareListener);
} else {
ShareLoginLib.doShare(this, WeiBoPlatform.TIME_LINE, mShareContent, shareListener);
ShareLoginLib.doShare(this, WeiBoPlatform.TIMELINE, mShareContent, shareListener);
}
break;
case R.id.分享给微信好友:
ShareLoginLib.doShare(this, WeiXinPlatform.FRIEND, mShareContent, shareListener);
break;
case R.id.分享到微信朋友圈:
ShareLoginLib.doShare(this, WeiXinPlatform.FRIEND_ZONE, mShareContent, shareListener);
ShareLoginLib.doShare(this, WeiXinPlatform.TIMELINE, mShareContent, shareListener);
break;
case R.id.分享到微信收藏:
ShareLoginLib.doShare(this, WeiXinPlatform.FAVORITE, mShareContent, shareListener);
Expand Down
7 changes: 6 additions & 1 deletion lib/src/main/java/kale/sharelogin/IPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface IPlatform {
/**
* 检查当前环境,如果异常则直接终止
*
* @param type 当前平台支持的操作类型
* @param type 当前平台支持的操作类型
* @param shareContentType 分享时传入的分享类型,如果是登录则会传{@link ShareContent#NO_CONTENT}
*/
void checkEnvironment(Context context, @NonNull String type, @ShareContentType int shareContentType);
Expand All @@ -48,4 +48,9 @@ public interface IPlatform {
*/
void onResponse(@NonNull Activity activity, @Nullable Intent data);

/**
* 得到用户的信息,会在{@link LoginListener#onReceiveUserInfo(OAuthUserInfo)}中进行回调
*/
void getUserInfo(Context context, String accessToken, String uid, final LoginListener listener);

}
36 changes: 21 additions & 15 deletions lib/src/main/java/kale/sharelogin/ShareLoginLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,22 @@ public static void initPlatforms(Map<String, String> keyValue, List<Class<? exte
}

public static void doLogin(@NonNull final Activity activity, String type, @Nullable LoginListener listener) {
if (type == null) {
if (listener != null) {
listener.onError("type is null");
}
return;
}
doAction(activity, true, type, null, listener, null);
}

public static void doShare(@NonNull final Activity activity, String type, @NonNull ShareContent shareContent, @Nullable ShareListener listener) {
if (type == null || shareContent == null) {
if (listener != null) {
listener.onError("type or shareContent is null");
}
return;
}
doAction(activity, false, type, shareContent, null, listener);
}

Expand All @@ -71,13 +83,7 @@ private static void doAction(Activity activity, boolean isLoginAction, @NonNull
ArrayList<IPlatform> platforms = new ArrayList<>();

for (Class<? extends IPlatform> platformClz : supportPlatforms) {
try {
platforms.add(platformClz.newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
platforms.add(SlUtils.createPlatform(platformClz));
}

// 2. 根据type匹配出一个目标平台
Expand Down Expand Up @@ -156,14 +162,14 @@ static void destroy() {
*/
@CheckResult
public static boolean isAppInstalled(Context context, Class<? extends IPlatform> platformClz) {
try {
return platformClz.newInstance().isAppInstalled(context.getApplicationContext());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
return SlUtils.createPlatform(platformClz).isAppInstalled(context.getApplicationContext());
}

/**
* 得到用户的信息,会在{@link LoginListener#onReceiveUserInfo(OAuthUserInfo)}中进行回调
*/
public static void getUserInfo(Context context, Class<? extends IPlatform> platformClz, String accessToken, String uid, LoginListener listener) {
SlUtils.createPlatform(platformClz).getUserInfo(context, accessToken, uid, listener);
}

public static String getValue(String key) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/kale/sharelogin/qq/LoginHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static void parseLoginResp(Activity activity, Object object, @NonNull LoginListe
* "is_yellow_year_vip":"1"
* }
*/
private static void getUserInfo(Context context, final String accessToken, final String userId, LoginListener listener) {
static void getUserInfo(Context context, final String accessToken, final String userId, LoginListener listener) {
LinkedHashMap<String, Object> params = new LinkedHashMap<>();
params.put("access_token", accessToken);
params.put("openid", userId);
Expand Down
12 changes: 9 additions & 3 deletions lib/src/main/java/kale/sharelogin/qq/QQPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;

import kale.sharelogin.IPlatform;
import kale.sharelogin.LoginListener;
import kale.sharelogin.ShareListener;
import kale.sharelogin.ShareLoginLib;
import kale.sharelogin.content.ShareContent;
import kale.sharelogin.content.ShareContentType;
import kale.sharelogin.IPlatform;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;

/**
* @author Kale
Expand Down Expand Up @@ -128,6 +129,11 @@ public void onResponse(@NonNull Activity activity, @Nullable Intent data) {
}
}

@Override
public void getUserInfo(Context context, String accessToken, String uid, LoginListener listener) {
LoginHelper.getUserInfo(context, accessToken, uid, listener);
}

/**
* 传入应用程序的全局context,可通过activity的getApplicationContext方法获取
*/
Expand Down
13 changes: 13 additions & 0 deletions lib/src/main/java/kale/sharelogin/utils/SlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.widget.Toast;

import kale.sharelogin.EventHandlerActivity;
import kale.sharelogin.IPlatform;
import kale.sharelogin.ShareLoginLib;
import kale.sharelogin.content.ShareContent;

Expand Down Expand Up @@ -167,6 +168,18 @@ public static void checkLeak(Activity activity) {
}, 1000);
}

public static IPlatform createPlatform(Class<? extends IPlatform> platformClz) {
try {
return platformClz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

throw new RuntimeException("platform create error");
}

public interface Function<T> {

T apply(ShareContent content);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/kale/sharelogin/weibo/LoginHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static String data2Json(@NonNull Oauth2AccessToken data) {
*
* @see "http://open.weibo.com/wiki/2/users/show"
*/
public static void getUserInfo(Context context, String accessToken, String uid, LoginListener listener) {
static void getUserInfo(Context context, String accessToken, String uid, LoginListener listener) {
LinkedHashMap<String, Object> params = new LinkedHashMap<>();
params.put("access_token", accessToken);
params.put("uid", uid);
Expand Down
11 changes: 8 additions & 3 deletions lib/src/main/java/kale/sharelogin/weibo/WeiBoPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class WeiBoPlatform implements IPlatform {

public static final String LOGIN = "weibo_login";

public static final String TIME_LINE = "weibo_time_line";
public static final String TIMELINE = "weibo_timeline";

public static final String STORY = "weibo_story";

Expand All @@ -50,7 +50,7 @@ public class WeiBoPlatform implements IPlatform {

@Override
public String[] getSupportedTypes() {
return new String[]{LOGIN, TIME_LINE, STORY};
return new String[]{LOGIN, TIMELINE, STORY};
}

@Override
Expand Down Expand Up @@ -137,7 +137,7 @@ public void onWbShareFail() {
WbShareHandler shareHandler = new WbShareHandler(activity);
shareHandler.registerApp();

if (shareType.equals(TIME_LINE)) {
if (shareType.equals(TIMELINE)) {
shareHandler.shareMessage(ShareHelper.shareMessage(shareContent), false);
} else if (shareType.equals(STORY)) {
shareHandler.shareToStory(ShareHelper.storyMessage(shareContent));
Expand Down Expand Up @@ -170,4 +170,9 @@ public void onResponse(@NonNull Activity activity, @Nullable Intent data) {
}
}

@Override
public void getUserInfo(Context context, String accessToken, String uid, LoginListener listener) {
LoginHelper.getUserInfo(context, accessToken, uid, listener);
}

}
4 changes: 2 additions & 2 deletions lib/src/main/java/kale/sharelogin/weixin/LoginHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static void parseLoginResp(final Activity activity, BaseResp baseResp, @NonNull
*/
private static void code2Token(Context context, String code, @NonNull final LoginListener listener) {
String appId = ShareLoginLib.getValue(WeiXinPlatform.KEY_APP_ID);
String secret = ShareLoginLib.getValue(WeiXinPlatform.KEY_SECRET_KEY);
String secret = ShareLoginLib.getValue(WeiXinPlatform.KEY_SECRET);

WeiboParameters params = new WeiboParameters(null);
params.put("appid", appId);
Expand Down Expand Up @@ -117,7 +117,7 @@ public void onWeiboException(WeiboException e) {
* "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
* }
*/
public static void getUserInfo(Context context, String accessToken, String uid, final LoginListener listener) {
static void getUserInfo(Context context, String accessToken, String uid, final LoginListener listener) {
LinkedHashMap<String, Object> params = new LinkedHashMap<>();
params.put("access_token", accessToken);
params.put("openid", uid);
Expand Down
24 changes: 15 additions & 9 deletions lib/src/main/java/kale/sharelogin/weixin/WeiXinPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
import android.support.annotation.Nullable;
import android.text.TextUtils;

import kale.sharelogin.LoginListener;
import kale.sharelogin.ShareListener;
import kale.sharelogin.ShareLoginLib;
import kale.sharelogin.content.ShareContent;
import kale.sharelogin.content.ShareContentType;
import kale.sharelogin.IPlatform;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
Expand All @@ -21,6 +15,13 @@
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import kale.sharelogin.IPlatform;
import kale.sharelogin.LoginListener;
import kale.sharelogin.ShareListener;
import kale.sharelogin.ShareLoginLib;
import kale.sharelogin.content.ShareContent;
import kale.sharelogin.content.ShareContentType;

/**
* @author Kale
* @date 2018/9/10
Expand All @@ -29,23 +30,23 @@ public class WeiXinPlatform implements IPlatform {

public static final String KEY_APP_ID = "weixin_key_app_id";

public static final String KEY_SECRET_KEY = "weixin_key_secret_key";
public static final String KEY_SECRET = "weixin_key_secret";

// ---------------------------------------------------------------

public static final String LOGIN = "weixin_login";

public static final String FRIEND = "weixin_friend" + SendMessageToWX.Req.WXSceneSession, // 好友

FRIEND_ZONE = "weixin_friend_zone" + SendMessageToWX.Req.WXSceneTimeline, // 朋友圈
TIMELINE = "weixin_timeline" + SendMessageToWX.Req.WXSceneTimeline, // 朋友圈

FAVORITE = "weixin_favorite" + SendMessageToWX.Req.WXSceneFavorite; // 收藏

private IWXAPIEventHandler wxEventHandler;

@Override
public String[] getSupportedTypes() {
return new String[]{LOGIN, FRIEND, FRIEND_ZONE, FAVORITE};
return new String[]{LOGIN, FRIEND, TIMELINE, FAVORITE};
}

@Override
Expand Down Expand Up @@ -116,6 +117,11 @@ public void onResponse(@NonNull Activity activity, @Nullable Intent data) {
getApi(activity).handleIntent(data, wxEventHandler);
}

@Override
public void getUserInfo(Context context, String accessToken, String uid, LoginListener listener) {
LoginHelper.getUserInfo(context, accessToken, uid, listener);
}

private static IWXAPI getApi(Context context) {
return WXAPIFactory.createWXAPI(context.getApplicationContext(), ShareLoginLib.getValue(KEY_APP_ID), true);
}
Expand Down

0 comments on commit a2fbe8e

Please sign in to comment.