8 | * 自定义Application
9 | */
10 | public class SocialSDKApplication extends Application {
11 |
12 | @Override
13 | public void onCreate() {
14 |
15 | initBugHD();
16 | super.onCreate();
17 | }
18 |
19 | /**
20 | * 初始化BugHD,实时监控APP的崩溃
21 | */
22 | private void initBugHD() {
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/sample-app/src/main/java/com/encore/actionnow/wxapi/WXEntryActivity.java:
--------------------------------------------------------------------------------
1 | package com.encore.actionnow.wxapi;
2 |
3 |
4 | import com.elbbbird.android.socialsdk.sso.wechat.WXCallbackActivity;
5 |
6 | public class WXEntryActivity extends WXCallbackActivity {
7 |
8 | }
--------------------------------------------------------------------------------
/sample-app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
21 |
22 |
23 |
24 |
25 |
26 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/sample-app/src/main/res/layout/activity_share.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
21 |
22 |
23 |
24 |
25 |
26 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/sample-app/src/main/res/layout/activity_share_all.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
21 |
22 |
23 |
24 |
25 |
26 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/sample-app/src/main/res/layout/activity_sso.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
21 |
22 |
23 |
24 |
25 |
26 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/sample-app/src/main/res/layout/activity_sso_all.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
21 |
22 |
23 |
24 |
25 |
26 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/sample-app/src/main/res/layout/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
15 |
16 |
22 |
23 |
28 |
29 |
35 |
36 |
41 |
42 |
--------------------------------------------------------------------------------
/sample-app/src/main/res/layout/content_share.xml:
--------------------------------------------------------------------------------
1 |
2 |
15 |
16 |
17 |
23 |
24 |
29 |
30 |
31 |
36 |
37 |
42 |
43 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/sample-app/src/main/res/layout/content_share_all.xml:
--------------------------------------------------------------------------------
1 |
2 |
14 |
15 |
20 |
21 |
--------------------------------------------------------------------------------
/sample-app/src/main/res/layout/content_sso.xml:
--------------------------------------------------------------------------------
1 |
2 |
15 |
16 |
19 |
20 |
25 |
26 |
31 |
32 |
33 |
36 |
37 |
42 |
43 |
48 |
49 |
50 |
53 |
54 |
59 |
60 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/sample-app/src/main/res/layout/content_sso_all.xml:
--------------------------------------------------------------------------------
1 |
2 |
15 |
16 |
17 |
22 |
23 |
28 |
29 |
--------------------------------------------------------------------------------
/sample-app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
{
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/socialsdk/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
14 |
15 |
20 |
21 |
25 |
26 |
31 |
32 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/SocialUtils.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk;
2 |
3 | import android.content.Context;
4 | import android.content.pm.PackageInfo;
5 | import android.content.pm.PackageManager;
6 | import android.graphics.Bitmap;
7 | import android.graphics.BitmapFactory;
8 |
9 | import java.io.ByteArrayOutputStream;
10 | import java.io.IOException;
11 | import java.io.InputStream;
12 | import java.net.HttpURLConnection;
13 | import java.net.MalformedURLException;
14 | import java.net.URL;
15 | import java.net.URLConnection;
16 |
17 | /**
18 | * Created by zhanghailong-ms on 2015/4/7.
19 | */
20 | public class SocialUtils {
21 |
22 | /**
23 | * 从网络获取图片数据
24 | *
25 | * @param url 地址
26 | * @return 图片数据
27 | */
28 | public static byte[] getHtmlByteArray(final String url) {
29 | URL htmlUrl = null;
30 | InputStream inStream = null;
31 | try {
32 | htmlUrl = new URL(url);
33 | URLConnection connection = htmlUrl.openConnection();
34 | HttpURLConnection httpConnection = (HttpURLConnection) connection;
35 | int responseCode = httpConnection.getResponseCode();
36 | if (responseCode == HttpURLConnection.HTTP_OK) {
37 | inStream = httpConnection.getInputStream();
38 | }
39 | } catch (MalformedURLException e) {
40 | e.printStackTrace();
41 | } catch (IOException e) {
42 | e.printStackTrace();
43 | }
44 | byte[] data = inputStreamToByte(inStream);
45 |
46 | return data;
47 | }
48 |
49 | /**
50 | * 从输入流获取数据
51 | *
52 | * @param is 输入流
53 | * @return 数据
54 | */
55 | public static byte[] inputStreamToByte(InputStream is) {
56 | try {
57 | ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
58 | int ch;
59 | while ((ch = is.read()) != -1) {
60 | bytestream.write(ch);
61 | }
62 | byte imgdata[] = bytestream.toByteArray();
63 | bytestream.close();
64 | return imgdata;
65 | } catch (Exception e) {
66 | e.printStackTrace();
67 | }
68 |
69 | return null;
70 | }
71 |
72 | /**
73 | * 生成时间戳
74 | *
75 | * @param type 类型
76 | * @return 时间戳
77 | */
78 | public static String buildTransaction(final String type) {
79 | return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
80 | }
81 |
82 | /**
83 | * 压缩数据
84 | *
85 | * @param data 数据
86 | * @param size 最大值
87 | * @return 压缩后数据
88 | */
89 | public static byte[] compressBitmap(byte[] data, float size) {
90 | Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
91 | if (bitmap == null || getSizeOfBitmap(bitmap) <= size) {
92 | return data;
93 | }
94 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
95 | bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
96 | int quality = 100;
97 | while ((baos.toByteArray().length / 1024f) > size) {
98 | quality -= 5;
99 | baos.reset();
100 | if (quality <= 0) {
101 | break;
102 | }
103 | bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
104 | }
105 | byte[] byteData = baos.toByteArray();
106 | return byteData;
107 | }
108 |
109 | /**
110 | * 获取bitmap长度
111 | *
112 | * @param bitmap bitmap
113 | * @return 长度
114 | */
115 | private static long getSizeOfBitmap(Bitmap bitmap) {
116 |
117 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
118 | bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
119 | long length = baos.toByteArray().length / 1024;
120 | return length;
121 | }
122 |
123 | /**
124 | * 是否安装微信
125 | *
126 | * @param context context
127 | * @return 是否安装
128 | */
129 | public static boolean isInstalledWeChat(Context context) {
130 | return isPackageInstalled(context, "com.tencent.mm");
131 | }
132 |
133 | /**
134 | * 是否安装QQ
135 | *
136 | * @param context context
137 | * @return 是否安装
138 | */
139 | public static boolean isInstalledQQ(Context context) {
140 | return isPackageInstalled(context, "com.tencent.mobileqq");
141 | }
142 |
143 | /**
144 | * 是否安装了该包名的app
145 | *
146 | * @param context context
147 | * @param pkgName 包名
148 | * @return 是否安装
149 | */
150 | private static boolean isPackageInstalled(Context context, String pkgName) {
151 | PackageManager pm = context.getPackageManager();
152 | try {
153 | PackageInfo pkgInfo = pm.getPackageInfo(pkgName, 0);
154 | return pkgInfo != null && pkgInfo.applicationInfo.enabled;
155 | } catch (PackageManager.NameNotFoundException e) {
156 | }
157 | return false;
158 | }
159 |
160 | private static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
161 | ByteArrayOutputStream output = new ByteArrayOutputStream();
162 | bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
163 | if (needRecycle) {
164 | bmp.recycle();
165 | }
166 |
167 | byte[] result = output.toByteArray();
168 | try {
169 | output.close();
170 | } catch (Exception e) {
171 | }
172 |
173 | return result;
174 | }
175 |
176 | /**
177 | * 获取微信,朋友圈分享默认icon
178 | *
179 | * @param context context
180 | * @return 数据
181 | */
182 | public static byte[] getDefaultShareImage(Context context) {
183 |
184 | return bmpToByteArray(BitmapFactory.decodeResource(context.getResources(), R.drawable.es_icon_default), true);
185 | }
186 | }
187 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/WeChat.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk;
2 |
3 | import android.content.Context;
4 |
5 | import com.tencent.mm.sdk.openapi.IWXAPI;
6 | import com.tencent.mm.sdk.openapi.WXAPIFactory;
7 |
8 | /**
9 | * 之所以抽取WeChat类,是因为IWXAPI需要在SSO授权和分享同时用到
10 | * Created by zhanghailong-ms on 2015/11/24.
11 | */
12 | public class WeChat {
13 |
14 | private static IWXAPI api;
15 |
16 | public static IWXAPI getIWXAPIInstance(Context context, String appId) {
17 | if (null == api) {
18 | api = WXAPIFactory.createWXAPI(context, appId, true);
19 | api.registerApp(appId);
20 | }
21 |
22 | return api;
23 | }
24 |
25 | public static IWXAPI getIWXAPIInstance() {
26 | return api;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/model/SocialInfo.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.model;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 社交平台信息
7 | * Created by zhanghailong-ms on 2015/11/13.
8 | */
9 | public class SocialInfo implements Serializable {
10 |
11 | private boolean debugMode = false;
12 | private String wechatAppId = "";
13 | private String weChatAppSecret = "";
14 | private String weChatScope = "snsapi_userinfo";
15 | private String weiboAppKey = "";
16 | private String weiboRedirectrUrl = "http://www.sina.com";
17 | private String weiboScope = "email,direct_messages_read,direct_messages_write,"
18 | + "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
19 | + "follow_app_official_microblog," + "invitation_write";
20 | private String qqAppId = "";
21 | private String qqScope = "all";
22 |
23 | public boolean isDebugMode() {
24 | return debugMode;
25 | }
26 |
27 | public void setDebugMode(boolean debugMode) {
28 | this.debugMode = debugMode;
29 | }
30 |
31 | public String getWechatAppId() {
32 | return wechatAppId;
33 | }
34 |
35 | public void setWechatAppId(String wechatAppId) {
36 | this.wechatAppId = wechatAppId;
37 | }
38 |
39 | public String getWeChatAppSecret() {
40 | return weChatAppSecret;
41 | }
42 |
43 | public void setWeChatAppSecret(String weChatAppSecret) {
44 | this.weChatAppSecret = weChatAppSecret;
45 | }
46 |
47 | public String getWeChatScope() {
48 | return weChatScope;
49 | }
50 |
51 | public void setWeChatScope(String weChatScope) {
52 | this.weChatScope = weChatScope;
53 | }
54 |
55 | public String getWeiboAppKey() {
56 | return weiboAppKey;
57 | }
58 |
59 | public void setWeiboAppKey(String weiboAppKey) {
60 | this.weiboAppKey = weiboAppKey;
61 | }
62 |
63 | public String getWeiboRedirectrUrl() {
64 | return weiboRedirectrUrl;
65 | }
66 |
67 | public void setWeiboRedirectrUrl(String weiboRedirectrUrl) {
68 | this.weiboRedirectrUrl = weiboRedirectrUrl;
69 | }
70 |
71 | public String getWeiboScope() {
72 | return weiboScope;
73 | }
74 |
75 | public void setWeiboScope(String weiboScope) {
76 | this.weiboScope = weiboScope;
77 | }
78 |
79 | public String getQqAppId() {
80 | return qqAppId;
81 | }
82 |
83 | public void setQqAppId(String qqAppId) {
84 | this.qqAppId = qqAppId;
85 | }
86 |
87 | public String getQqScope() {
88 | return qqScope;
89 | }
90 |
91 | public void setQqScope(String qqScope) {
92 | this.qqScope = qqScope;
93 | }
94 |
95 | public String getUrlForWeChatToken() {
96 | return "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
97 | + getWechatAppId()
98 | + "&secret="
99 | + getWeChatAppSecret()
100 | + "&code=%s&grant_type=authorization_code";
101 | }
102 |
103 | public String getUrlForWeChatUserInfo() {
104 | return "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s";
105 | }
106 |
107 | public String getUrlForWeChatRefreshToken() {
108 | return "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="
109 | + getWechatAppId()
110 | + "&grant_type=refresh_token&refresh_token=%s";
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/model/SocialShareScene.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.model;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 社会化分享数据类
7 | * Created by zhanghailong-ms on 2015/11/23.
8 | */
9 | public class SocialShareScene implements Serializable {
10 |
11 | public static final int SHARE_TYPE_DEFAULT = 0;
12 | public static final int SHARE_TYPE_WEIBO = 1;
13 | public static final int SHARE_TYPE_WECHAT = 2;
14 | public static final int SHARE_TYPE_WECHAT_TIMELINE = 3;
15 | public static final int SHARE_TYPE_QQ = 4;
16 | public static final int SHARE_TYPE_QZONE = 5;
17 |
18 | private int id;
19 | private int type;
20 | private String appName;
21 | private String title;
22 | private String desc;
23 | private String thumbnail;
24 | private String url;
25 |
26 | /**
27 | * @param id 分享唯一标识符,可随意指定,会在分享结果ShareBusEvent中返回
28 | * @param appName 分享到QQ时需要指定,会在分享弹窗中显示该字段
29 | * @param type 分享类型,会在分享结果ShareBusEvent中作为platform返回
30 | * @param title 标题
31 | * @param desc 简短描述
32 | * @param thumbnail 缩略图网址
33 | * @param url WEB网址
34 | */
35 | public SocialShareScene(int id, String appName, int type, String title, String desc, String thumbnail, String url) {
36 | this.id = id;
37 | this.appName = appName;
38 | this.type = type;
39 | this.title = title;
40 | this.desc = desc;
41 | this.thumbnail = thumbnail;
42 | this.url = url;
43 | }
44 |
45 | public SocialShareScene(int id, String appName, String title, String desc, String thumbnail, String url) {
46 | this.id = id;
47 | this.appName = appName;
48 | this.title = title;
49 | this.desc = desc;
50 | this.thumbnail = thumbnail;
51 | this.url = url;
52 | }
53 |
54 | public String getAppName() {
55 | return appName;
56 | }
57 |
58 | public void setAppName(String appName) {
59 | this.appName = appName;
60 | }
61 |
62 | public int getId() {
63 | return id;
64 | }
65 |
66 | public void setId(int id) {
67 | this.id = id;
68 | }
69 |
70 | public int getType() {
71 | return type;
72 | }
73 |
74 | public void setType(int type) {
75 | this.type = type;
76 | }
77 |
78 | public String getTitle() {
79 | return title;
80 | }
81 |
82 | public void setTitle(String title) {
83 | this.title = title;
84 | }
85 |
86 | public String getDesc() {
87 | return desc;
88 | }
89 |
90 | public void setDesc(String desc) {
91 | this.desc = desc;
92 | }
93 |
94 | public String getThumbnail() {
95 | return thumbnail;
96 | }
97 |
98 | public void setThumbnail(String thumbnail) {
99 | this.thumbnail = thumbnail;
100 | }
101 |
102 | public String getUrl() {
103 | return url;
104 | }
105 |
106 | public void setUrl(String url) {
107 | this.url = url;
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/model/SocialToken.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.model;
2 |
3 | /**
4 | * oauth token信息
5 | *
6 | * Created by zhanghailong-ms on 2015/11/16.
7 | */
8 | public class SocialToken {
9 |
10 | private String openId;
11 | private String token;
12 | private String refreshToken;
13 | private long expiresTime;
14 |
15 | public SocialToken() {
16 | }
17 |
18 | public SocialToken(String openId, String token, String refreshToken, long expiresTime) {
19 | this.openId = openId;
20 | this.token = token;
21 | this.refreshToken = refreshToken;
22 | this.expiresTime = System.currentTimeMillis() + expiresTime * 1000L;
23 | }
24 |
25 | public String getOpenId() {
26 | return openId;
27 | }
28 |
29 | public void setOpenId(String openId) {
30 | this.openId = openId;
31 | }
32 |
33 | public String getToken() {
34 | return token;
35 | }
36 |
37 | public void setToken(String token) {
38 | this.token = token;
39 | }
40 |
41 | public String getRefreshToken() {
42 | return refreshToken;
43 | }
44 |
45 | public void setRefreshToken(String refreshToken) {
46 | this.refreshToken = refreshToken;
47 | }
48 |
49 | public long getExpiresTime() {
50 | return expiresTime;
51 | }
52 |
53 | public void setExpiresTime(long expiresTime) {
54 | this.expiresTime = expiresTime;
55 | }
56 |
57 | @Override
58 | public String toString() {
59 | return "SocialToken# openId=" + openId + ", token=" + token + ", refreshToken=" + refreshToken + ", expiresTime=" + expiresTime;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/model/SocialUser.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.model;
2 |
3 | /**
4 | * oauth用户信息
5 | *
6 | * Created by zhanghailong-ms on 2015/11/16.
7 | */
8 | public class SocialUser {
9 |
10 | public static final int TYPE_DEFAULT = 0;
11 | public static final int TYPE_WEIBO = 1;
12 | public static final int TYPE_WECHAT = 2;
13 | public static final int TYPE_QQ = 3;
14 |
15 | public static final int GENDER_UNKNOWN = 0;
16 | public static final int GENDER_MALE = 1;
17 | public static final int GENDER_FEMALE = 2;
18 |
19 | private int type;
20 | private String name;
21 | private String avatar;
22 | private int gender;
23 | private String desc;
24 | private SocialToken token;
25 |
26 | public SocialUser() {
27 | }
28 |
29 | public SocialUser(int type, String name, String avatar, int gender, SocialToken token) {
30 | this.type = type;
31 | this.name = name;
32 | this.avatar = avatar;
33 | this.gender = gender;
34 | this.token = token;
35 | }
36 |
37 | public SocialUser(int type, String name, String avatar, int gender, String desc, SocialToken token) {
38 | this.type = type;
39 | this.name = name;
40 | this.avatar = avatar;
41 | this.gender = gender;
42 | this.desc = desc;
43 | this.token = token;
44 | }
45 |
46 | public int getType() {
47 | return type;
48 | }
49 |
50 | public void setType(int type) {
51 | this.type = type;
52 | }
53 |
54 | public String getName() {
55 | return name;
56 | }
57 |
58 | public void setName(String name) {
59 | this.name = name;
60 | }
61 |
62 | public String getAvatar() {
63 | return avatar;
64 | }
65 |
66 | public void setAvatar(String avatar) {
67 | this.avatar = avatar;
68 | }
69 |
70 | public int getGender() {
71 | return gender;
72 | }
73 |
74 | public void setGender(int gender) {
75 | this.gender = gender;
76 | }
77 |
78 | public String getDesc() {
79 | return desc;
80 | }
81 |
82 | public void setDesc(String desc) {
83 | this.desc = desc;
84 | }
85 |
86 | public SocialToken getToken() {
87 | return token;
88 | }
89 |
90 | public void setToken(SocialToken token) {
91 | this.token = token;
92 | }
93 |
94 | public boolean isTokenValid() {
95 | return getToken().getToken() != null && System.currentTimeMillis() < getToken().getExpiresTime();
96 | }
97 |
98 | @Override
99 | public String toString() {
100 | return "SocialUser: type=" + type + ", name=" + name + ", avatar=" + avatar + ", gender=" + gender + ", desc=" + desc
101 | + ", token=" + token.getToken();
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/otto/BusProvider.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.otto;
2 |
3 | /**
4 | * Otto Bus Provider
5 | * Created by zhanghailong-ms on 2015/11/20.
6 | */
7 | public class BusProvider {
8 |
9 | private static MainThreadBus bus;
10 |
11 | public static MainThreadBus getInstance() {
12 | if (bus == null)
13 | bus = new MainThreadBus();
14 |
15 | return bus;
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/otto/MainThreadBus.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.otto;
2 |
3 | import android.os.Handler;
4 | import android.os.Looper;
5 |
6 | import com.squareup.otto.Bus;
7 |
8 | /**
9 | * To post from any thread (main or background) and receive on the main thread
10 | * Created by zhanghailong-ms on 2015/11/24.
11 | */
12 | public class MainThreadBus extends Bus {
13 |
14 | private final Handler mHandler = new Handler(Looper.getMainLooper());
15 |
16 | @Override
17 | public void post(final Object event) {
18 | if (Looper.myLooper() == Looper.getMainLooper()) {
19 | super.post(event);
20 | } else {
21 | mHandler.post(new Runnable() {
22 | @Override
23 | public void run() {
24 | MainThreadBus.super.post(event);
25 | }
26 | });
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/otto/SSOBusEvent.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.otto;
2 |
3 | import com.elbbbird.android.socialsdk.model.SocialToken;
4 | import com.elbbbird.android.socialsdk.model.SocialUser;
5 |
6 | /**
7 | * Otto Bus Event
8 | * Created by zhanghailong-ms on 2015/11/20.
9 | */
10 | public class SSOBusEvent {
11 |
12 | public static final int PLATFORM_DEFAULT = 0;
13 | public static final int PLATFORM_WEIBO = 1;
14 | public static final int PLATFORM_WECHAT = 2;
15 | public static final int PLATFORM_QQ = 3;
16 |
17 | public static final int TYPE_GET_TOKEN = 0;
18 | public static final int TYPE_GET_USER = 1;
19 | public static final int TYPE_FAILURE = 2;
20 | public static final int TYPE_CANCEL = 3;
21 |
22 | private int type;
23 | private int platform;
24 | private SocialUser user;
25 | private SocialToken token;
26 | private Exception exception;
27 |
28 | public SSOBusEvent(int type, int platform) {
29 | this.type = type;
30 | this.platform = platform;
31 | }
32 |
33 | public SSOBusEvent(int type, int platform, SocialUser user) {
34 | this.type = type;
35 | this.platform = platform;
36 | this.user = user;
37 | }
38 |
39 | public SSOBusEvent(int type, int platform, SocialToken token) {
40 | this.type = type;
41 | this.platform = platform;
42 | this.token = token;
43 | }
44 |
45 | public SSOBusEvent(int type, int platform, Exception exception) {
46 | this.type = type;
47 | this.platform = platform;
48 | this.exception = exception;
49 | }
50 |
51 | public int getType() {
52 | return type;
53 | }
54 |
55 | public void setType(int type) {
56 | this.type = type;
57 | }
58 |
59 | public SocialUser getUser() {
60 | return user;
61 | }
62 |
63 | public void setUser(SocialUser user) {
64 | this.user = user;
65 | }
66 |
67 | public SocialToken getToken() {
68 | return token;
69 | }
70 |
71 | public void setToken(SocialToken token) {
72 | this.token = token;
73 | }
74 |
75 | public Exception getException() {
76 | return exception;
77 | }
78 |
79 | public void setException(Exception exception) {
80 | this.exception = exception;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/otto/ShareBusEvent.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.otto;
2 |
3 | /**
4 | * Created by zhanghailong-ms on 2015/11/23.
5 | */
6 | public class ShareBusEvent {
7 |
8 | public static final int TYPE_SUCCESS = 0;
9 | public static final int TYPE_FAILURE = 1;
10 | public static final int TYPE_CANCEL = 2;
11 |
12 | private int type;
13 | private int id;
14 | private int platform;
15 | private Exception exception;
16 |
17 | public ShareBusEvent(int type, int platform) {
18 | this.type = type;
19 | this.platform = platform;
20 | }
21 |
22 | public ShareBusEvent(int type, int platform, int id) {
23 | this.type = type;
24 | this.platform = platform;
25 | this.id = id;
26 | }
27 |
28 | public ShareBusEvent(int type, int platform, Exception exception) {
29 | this.type = type;
30 | this.exception = exception;
31 | }
32 |
33 | public Exception getException() {
34 | return exception;
35 | }
36 |
37 | public void setException(Exception exception) {
38 | this.exception = exception;
39 | }
40 |
41 | public int getId() {
42 | return id;
43 | }
44 |
45 | public void setId(int id) {
46 | this.id = id;
47 | }
48 |
49 | public int getPlatform() {
50 | return platform;
51 | }
52 |
53 | public void setPlatform(int platform) {
54 | this.platform = platform;
55 | }
56 |
57 | public int getType() {
58 | return type;
59 | }
60 |
61 | public void setType(int type) {
62 | this.type = type;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/share/SocialShareActivity.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.share;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.view.Gravity;
7 | import android.view.View;
8 |
9 | import com.elbbbird.android.socialsdk.R;
10 | import com.elbbbird.android.socialsdk.SocialSDK;
11 | import com.elbbbird.android.socialsdk.model.SocialInfo;
12 | import com.elbbbird.android.socialsdk.model.SocialShareScene;
13 | import com.elbbbird.android.socialsdk.otto.BusProvider;
14 | import com.elbbbird.android.socialsdk.otto.ShareBusEvent;
15 | import com.elbbbird.android.socialsdk.view.ShareButton;
16 | import com.sina.weibo.sdk.api.share.BaseResponse;
17 | import com.sina.weibo.sdk.api.share.IWeiboHandler;
18 | import com.sina.weibo.sdk.constant.WBConstants;
19 | import com.tencent.connect.common.Constants;
20 |
21 | /**
22 | * 一键社会化分享
23 | * Created by zhanghailong-ms on 2015/11/23.
24 | */
25 | public class SocialShareActivity extends Activity implements IWeiboHandler.Response {
26 |
27 | private SocialInfo info;
28 | private SocialShareScene scene;
29 |
30 | private ShareButton sbWechat;
31 | private ShareButton sbWeChatTimeline;
32 | private ShareButton sbWeibo;
33 | private ShareButton sbQQ;
34 | private ShareButton sbQZone;
35 | private ShareButton sbMore;
36 |
37 | @Override
38 | protected void onCreate(Bundle savedInstanceState) {
39 | super.onCreate(savedInstanceState);
40 |
41 | setContentView(R.layout.es_activity_social_share);
42 |
43 | getWindow().setGravity(Gravity.BOTTOM);
44 |
45 | info = (SocialInfo) getIntent().getExtras().getSerializable("info");
46 | scene = (SocialShareScene) getIntent().getExtras().getSerializable("scene");
47 |
48 | initViews();
49 | }
50 |
51 | private void initViews() {
52 | sbWechat = (ShareButton) findViewById(R.id.social_share_sb_wechat);
53 | sbWechat.setOnClickListener(new View.OnClickListener() {
54 | @Override
55 | public void onClick(View v) {
56 | scene.setType(SocialShareScene.SHARE_TYPE_WECHAT);
57 | SocialSDK.shareToWeChat(SocialShareActivity.this, info.getWechatAppId(), scene);
58 | }
59 | });
60 | sbWeChatTimeline = (ShareButton) findViewById(R.id.social_share_sb_wechat_timeline);
61 | sbWeChatTimeline.setOnClickListener(new View.OnClickListener() {
62 | @Override
63 | public void onClick(View v) {
64 | SocialSDK.shareToWeChatTimeline(SocialShareActivity.this, info.getWechatAppId(), scene);
65 | scene.setType(SocialShareScene.SHARE_TYPE_WECHAT_TIMELINE);
66 | }
67 | });
68 | sbWeibo = (ShareButton) findViewById(R.id.social_share_sb_weibo);
69 | sbWeibo.setOnClickListener(new View.OnClickListener() {
70 | @Override
71 | public void onClick(View v) {
72 | scene.setType(SocialShareScene.SHARE_TYPE_WEIBO);
73 | SocialSDK.shareToWeibo(SocialShareActivity.this, info.getWeiboAppKey(), scene);
74 | }
75 | });
76 | sbQQ = (ShareButton) findViewById(R.id.social_share_sb_qq);
77 | sbQQ.setOnClickListener(new View.OnClickListener() {
78 | @Override
79 | public void onClick(View v) {
80 | scene.setType(SocialShareScene.SHARE_TYPE_QQ);
81 | SocialSDK.shareToQQ(SocialShareActivity.this, info.getQqAppId(), scene);
82 | }
83 | });
84 | sbQZone = (ShareButton) findViewById(R.id.social_share_sb_qzone);
85 | sbQZone.setOnClickListener(new View.OnClickListener() {
86 | @Override
87 | public void onClick(View v) {
88 | scene.setType(SocialShareScene.SHARE_TYPE_QZONE);
89 | SocialSDK.shareToQZone(SocialShareActivity.this, info.getQqAppId(), scene);
90 | }
91 | });
92 | sbMore = (ShareButton) findViewById(R.id.social_share_sb_more);
93 | sbMore.setOnClickListener(new View.OnClickListener() {
94 | @Override
95 | public void onClick(View v) {
96 | scene.setType(SocialShareScene.SHARE_TYPE_DEFAULT);
97 | Intent share = new Intent(android.content.Intent.ACTION_SEND);
98 | share.setType("text/plain");
99 | share.putExtra(Intent.EXTRA_TEXT, scene.getTitle() + "\n\r" + scene.getUrl());
100 | share.putExtra(Intent.EXTRA_TITLE, scene.getTitle());
101 | share.putExtra(Intent.EXTRA_SUBJECT, scene.getDesc());
102 | startActivity(share);
103 | }
104 | });
105 | }
106 |
107 | @Override
108 | public void finish() {
109 | super.finish();
110 | overridePendingTransition(0, R.anim.es_snack_out);
111 | }
112 |
113 | @Override
114 | protected void onNewIntent(Intent intent) {
115 | super.onNewIntent(intent);
116 | if (scene.getType() == SocialShareScene.SHARE_TYPE_WEIBO) {
117 | SocialSDK.shareToWeiboCallback(intent, this);
118 | finish();
119 | }
120 | }
121 |
122 | @Override
123 | public void onResponse(BaseResponse baseResponse) {
124 | switch (baseResponse.errCode) {
125 | case WBConstants.ErrorCode.ERR_OK:
126 | BusProvider.getInstance().post(new ShareBusEvent(ShareBusEvent.TYPE_SUCCESS, scene.getType(), scene.getId()));
127 | break;
128 | case WBConstants.ErrorCode.ERR_CANCEL:
129 | BusProvider.getInstance().post(new ShareBusEvent(ShareBusEvent.TYPE_CANCEL, scene.getType()));
130 | break;
131 | case WBConstants.ErrorCode.ERR_FAIL:
132 | BusProvider.getInstance().post(new ShareBusEvent(ShareBusEvent.TYPE_FAILURE, scene.getType(), new Exception("WBConstants.ErrorCode.ERR_FAIL: "
133 | + baseResponse.errMsg)));
134 | break;
135 | }
136 | }
137 |
138 | @Override
139 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
140 | super.onActivityResult(requestCode, resultCode, data);
141 |
142 | if (requestCode == Constants.REQUEST_QZONE_SHARE || requestCode == Constants.REQUEST_QQ_SHARE) {
143 | SocialSDK.shareToQCallback(requestCode, resultCode, data);
144 | finish();
145 | }
146 |
147 | }
148 |
149 | @Override
150 | protected void onRestart() {
151 | super.onRestart();
152 | if (scene.getType() == SocialShareScene.SHARE_TYPE_WECHAT || scene.getType() == SocialShareScene.SHARE_TYPE_WECHAT_TIMELINE) {
153 | finish();
154 | }
155 | }
156 |
157 | }
158 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/share/qq/QQShareProxy.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.share.qq;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.os.Bundle;
6 |
7 | import com.tencent.connect.share.QQShare;
8 | import com.tencent.connect.share.QzoneShare;
9 | import com.tencent.tauth.IUiListener;
10 | import com.tencent.tauth.Tencent;
11 |
12 | import java.util.ArrayList;
13 |
14 | /**
15 | * QQ分享Proxy
16 | * Created by zhanghailong-ms on 2015/11/24.
17 | */
18 | public class QQShareProxy {
19 |
20 | private static Tencent tencent;
21 |
22 | private static Tencent getInstance(Context context, String appId) {
23 | if (tencent == null)
24 | tencent = Tencent.createInstance(appId, context);
25 |
26 | return tencent;
27 | }
28 |
29 | public static void shareToQQ(Context context, String appId, String title, String summary, String url,
30 | String imageUrl, String appName, IUiListener listener) {
31 | Bundle params = new Bundle();
32 | params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
33 | params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
34 | params.putString(QQShare.SHARE_TO_QQ_SUMMARY, summary);
35 | params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
36 | params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, imageUrl);
37 | params.putString(QQShare.SHARE_TO_QQ_APP_NAME, appName);
38 | Tencent tencent = getInstance(context, appId);
39 | tencent.shareToQQ((Activity) context, params, listener);
40 | }
41 |
42 | public static void shareToQZone(Context context, String appId, String title, String summary, String url,
43 | String imageUrl, IUiListener listener) {
44 | Bundle params = new Bundle();
45 | params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
46 | params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);
47 | params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, summary);
48 | params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, url);
49 | ArrayList imgs = new ArrayList();
50 | imgs.add(imageUrl);
51 | params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imgs);
52 | Tencent tencent = getInstance(context, appId);
53 | tencent.shareToQzone((Activity) context, params, listener);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/share/wechat/IWXShareCallback.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.share.wechat;
2 |
3 | /**
4 | * Created by zhanghailong-ms on 2015/7/21.
5 | */
6 | public interface IWXShareCallback {
7 |
8 | void onSuccess();
9 |
10 | void onCancel();
11 |
12 | void onFailure(Exception e);
13 | }
14 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/share/wechat/WeChatShareProxy.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.share.wechat;
2 |
3 | import android.content.Context;
4 |
5 | import com.elbbbird.android.socialsdk.SocialUtils;
6 | import com.elbbbird.android.socialsdk.WeChat;
7 | import com.tencent.mm.sdk.modelbase.BaseResp;
8 | import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
9 | import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
10 | import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
11 |
12 | /**
13 | * 微信分享Proxy
14 | * Created by zhanghailong-ms on 2015/11/23.
15 | */
16 | public class WeChatShareProxy {
17 |
18 | private static IWXShareCallback mCallback;
19 |
20 | public static void shareToWeChat(final Context context, final String appId, final String title, final String desc,
21 | final String url, final String thumbnail, final IWXShareCallback callback) {
22 | new Thread(new Runnable() {
23 | @Override
24 | public void run() {
25 | WeChatShareProxy.mCallback = callback;
26 | WXWebpageObject webpage = new WXWebpageObject();
27 | webpage.webpageUrl = url;
28 | WXMediaMessage msg = new WXMediaMessage(webpage);
29 | msg.title = title;
30 | msg.description = desc;
31 | byte[] thumb = SocialUtils.getHtmlByteArray(thumbnail);
32 | if (null != thumb)
33 | msg.thumbData = SocialUtils.compressBitmap(thumb, 32);
34 | else
35 | msg.thumbData = SocialUtils.compressBitmap(SocialUtils.getDefaultShareImage(context), 32);
36 |
37 | SendMessageToWX.Req req = new SendMessageToWX.Req();
38 | req.transaction = SocialUtils.buildTransaction("webpage");
39 | req.message = msg;
40 | req.scene = SendMessageToWX.Req.WXSceneSession;
41 | WeChat.getIWXAPIInstance(context, appId).sendReq(req);
42 | }
43 | }).start();
44 |
45 | }
46 |
47 | public static void shareToWeChatTimeline(final Context context, final String appId, final String title, final String url,
48 | final String thumbnail, final IWXShareCallback callback) {
49 | new Thread(new Runnable() {
50 | @Override
51 | public void run() {
52 | WeChatShareProxy.mCallback = callback;
53 | WXWebpageObject webpage = new WXWebpageObject();
54 | webpage.webpageUrl = url;
55 | WXMediaMessage msg = new WXMediaMessage(webpage);
56 | msg.title = title;
57 | byte[] thumb = SocialUtils.getHtmlByteArray(thumbnail);
58 | if (null != thumb)
59 | msg.thumbData = SocialUtils.compressBitmap(thumb, 32);
60 | else
61 | msg.thumbData = SocialUtils.compressBitmap(SocialUtils.getDefaultShareImage(context), 32);
62 |
63 | SendMessageToWX.Req req = new SendMessageToWX.Req();
64 | req.transaction = SocialUtils.buildTransaction("webpage");
65 | req.message = msg;
66 | req.scene = SendMessageToWX.Req.WXSceneTimeline;
67 | WeChat.getIWXAPIInstance(context, appId).sendReq(req);
68 | }
69 | }).start();
70 |
71 | }
72 |
73 | public static void shareComplete(SendMessageToWX.Resp resp) {
74 | if (null != mCallback) {
75 | switch (resp.errCode) {
76 | case BaseResp.ErrCode.ERR_OK:
77 | mCallback.onSuccess();
78 | break;
79 | case BaseResp.ErrCode.ERR_USER_CANCEL:
80 | mCallback.onCancel();
81 | break;
82 | case BaseResp.ErrCode.ERR_AUTH_DENIED:
83 | default:
84 | mCallback.onFailure(new Exception("BaseResp.ErrCode.ERR_AUTH_DENIED"));
85 | break;
86 | }
87 | }
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/share/weibo/AccessTokenKeeper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.elbbbird.android.socialsdk.share.weibo;
18 |
19 | import android.content.Context;
20 | import android.content.SharedPreferences;
21 | import android.content.SharedPreferences.Editor;
22 |
23 | import com.sina.weibo.sdk.auth.Oauth2AccessToken;
24 |
25 | /**
26 | * 该类定义了微博授权时所需要的参数。
27 | *
28 | * @author SINA
29 | * @since 2013-10-07
30 | */
31 | public class AccessTokenKeeper {
32 | private static final String PREFERENCES_NAME = "com_weibo_sdk_android";
33 |
34 | private static final String KEY_UID = "uid";
35 | private static final String KEY_ACCESS_TOKEN = "access_token";
36 | private static final String KEY_EXPIRES_IN = "expires_in";
37 | private static final String KEY_REFRESH_TOKEN = "refresh_token";
38 |
39 | /**
40 | * 保存 Token 对象到 SharedPreferences。
41 | *
42 | * @param context 应用程序上下文环境
43 | * @param token Token 对象
44 | */
45 | public static void writeAccessToken(Context context, Oauth2AccessToken token) {
46 | if (null == context || null == token) {
47 | return;
48 | }
49 |
50 | SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
51 | Editor editor = pref.edit();
52 | editor.putString(KEY_UID, token.getUid());
53 | editor.putString(KEY_ACCESS_TOKEN, token.getToken());
54 | editor.putString(KEY_REFRESH_TOKEN, token.getRefreshToken());
55 | editor.putLong(KEY_EXPIRES_IN, token.getExpiresTime());
56 | editor.commit();
57 | }
58 |
59 | /**
60 | * 从 SharedPreferences 读取 Token 信息。
61 | *
62 | * @param context 应用程序上下文环境
63 | * @return 返回 Token 对象
64 | */
65 | public static Oauth2AccessToken readAccessToken(Context context) {
66 | if (null == context) {
67 | return null;
68 | }
69 |
70 | Oauth2AccessToken token = new Oauth2AccessToken();
71 | SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
72 | token.setUid(pref.getString(KEY_UID, ""));
73 | token.setToken(pref.getString(KEY_ACCESS_TOKEN, ""));
74 | token.setRefreshToken(pref.getString(KEY_REFRESH_TOKEN, ""));
75 | token.setExpiresTime(pref.getLong(KEY_EXPIRES_IN, 0));
76 |
77 | return token;
78 | }
79 |
80 | /**
81 | * 清空 SharedPreferences 中 Token信息。
82 | *
83 | * @param context 应用程序上下文环境
84 | */
85 | public static void clear(Context context) {
86 | if (null == context) {
87 | return;
88 | }
89 |
90 | SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
91 | Editor editor = pref.edit();
92 | editor.clear();
93 | editor.commit();
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/share/weibo/WeiboShareProxy.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.share.weibo;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.text.TextUtils;
6 |
7 | import com.elbbbird.android.socialsdk.SocialUtils;
8 | import com.sina.weibo.sdk.api.TextObject;
9 | import com.sina.weibo.sdk.api.WebpageObject;
10 | import com.sina.weibo.sdk.api.WeiboMultiMessage;
11 | import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
12 | import com.sina.weibo.sdk.api.share.SendMultiMessageToWeiboRequest;
13 | import com.sina.weibo.sdk.api.share.WeiboShareSDK;
14 | import com.sina.weibo.sdk.auth.AuthInfo;
15 | import com.sina.weibo.sdk.auth.Oauth2AccessToken;
16 | import com.sina.weibo.sdk.auth.WeiboAuthListener;
17 | import com.sina.weibo.sdk.utils.LogUtil;
18 |
19 | /**
20 | * Created by zhanghailong-ms on 2015/11/24.
21 | */
22 | public class WeiboShareProxy {
23 |
24 | private static IWeiboShareAPI api;
25 |
26 | public static IWeiboShareAPI getInstance(Context context, String appKey) {
27 | LogUtil.enableLog();
28 | if (null == api) {
29 | api = WeiboShareSDK.createWeiboAPI(context, appKey);
30 | api.registerApp();
31 | }
32 |
33 | return api;
34 | }
35 |
36 | public static IWeiboShareAPI getInstance() {
37 | return api;
38 | }
39 |
40 | private static void shareTo(final Context context, final String appKey, final String redirectUrl, final String scop, final String title, final String desc,
41 | final String imageUrl, final String shareUrl, final WeiboAuthListener listener) {
42 | new Thread(new Runnable() {
43 | @Override
44 | public void run() {
45 | WeiboMultiMessage msg = new WeiboMultiMessage();
46 | TextObject text = new TextObject();
47 | text.text = desc;
48 | msg.textObject = text;
49 | WebpageObject web = new WebpageObject();
50 | web.description = desc;
51 | byte[] thumb = SocialUtils.getHtmlByteArray(imageUrl);
52 | if (null != thumb)
53 | web.thumbData = SocialUtils.compressBitmap(thumb, 32);
54 | else
55 | web.thumbData = SocialUtils.compressBitmap(SocialUtils.getDefaultShareImage(context), 32);
56 | web.actionUrl = shareUrl;
57 | web.identify = imageUrl;
58 | web.title = title;
59 | msg.mediaObject = web;
60 |
61 | SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest();
62 | request.transaction = String.valueOf(System.currentTimeMillis());
63 | request.multiMessage = msg;
64 |
65 | AuthInfo authInfo = new AuthInfo(context, appKey, redirectUrl, scop);
66 | Oauth2AccessToken accessToken = AccessTokenKeeper.readAccessToken(context);
67 | String token = "";
68 | if (accessToken != null) {
69 | token = accessToken.getToken();
70 | }
71 | getInstance(context, appKey).sendRequest((Activity) context, request, authInfo, token, listener);
72 | }
73 | }).start();
74 |
75 | }
76 |
77 | public static void shareTo(final Context context, final String appKey, final String redirectUrl, final String title, final String desc,
78 | final String imageUrl, final String shareUrl, final WeiboAuthListener listener) {
79 | if (TextUtils.isEmpty(redirectUrl)) {
80 | shareTo(context, appKey, title, desc, imageUrl, shareUrl, listener);
81 | } else {
82 | shareTo(context, appKey, redirectUrl, "email,direct_messages_read,direct_messages_write,"
83 | + "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
84 | + "follow_app_official_microblog," + "invitation_write", title, desc, imageUrl, shareUrl, listener);
85 | }
86 |
87 | }
88 |
89 | private static void shareTo(final Context context, final String appKey, final String title, final String desc,
90 | final String imageUrl, final String shareUrl, final WeiboAuthListener listener) {
91 |
92 | shareTo(context, appKey, "http://www.sina.com", "email,direct_messages_read,direct_messages_write,"
93 | + "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
94 | + "follow_app_official_microblog," + "invitation_write", title, desc, imageUrl, shareUrl, listener);
95 |
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/SocialInfoKeeper.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.sso;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 |
6 | import com.elbbbird.android.socialsdk.model.SocialInfo;
7 |
8 | /**
9 | * 社交平台信息持久化
10 | * Created by zhanghailong-ms on 2015/11/20.
11 | */
12 | public class SocialInfoKeeper {
13 |
14 | private static final String PREFERENCE_NAME = "es_social_info";
15 |
16 | private static final String KEY_DEBUG = "debug";
17 | private static final String KEY_WECHAT_APP_ID = "wechat_app_id";
18 | private static final String KEY_WECHAT_APP_SECRET = "wechat_app_secret";
19 | private static final String KEY_WECHAT_SCOPE = "wechat_scope";
20 | private static final String KEY_WEIBO_APP_KEY = "weibo_app_key";
21 | private static final String KEY_WEIBO_REDIRECT_URL = "weibo_redirect_url";
22 | private static final String KEY_WEIBO_SCOPE = "weibo_scope";
23 | private static final String KEY_QQ_APP_ID = "qq_app_id";
24 | private static final String KEY_QQ_SCOPE = "qq_scope";
25 |
26 | /**
27 | * 持久化社交平台信息
28 | *
29 | * @param context context
30 | * @param info 社交平台信息
31 | */
32 | public static void writeSocialInfo(Context context, SocialInfo info) {
33 | if (info == null || context == null)
34 | return;
35 |
36 | SharedPreferences.Editor editor = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).edit();
37 | editor.putBoolean(KEY_DEBUG, info.isDebugMode());
38 | editor.putString(KEY_WECHAT_APP_ID, info.getWechatAppId());
39 | editor.putString(KEY_WECHAT_APP_SECRET, info.getWeChatAppSecret());
40 | editor.putString(KEY_WECHAT_SCOPE, info.getWeChatScope());
41 | editor.putString(KEY_WEIBO_APP_KEY, info.getWeiboAppKey());
42 | editor.putString(KEY_WEIBO_REDIRECT_URL, info.getWeiboRedirectrUrl());
43 | editor.putString(KEY_WEIBO_SCOPE, info.getWeiboScope());
44 | editor.putString(KEY_QQ_SCOPE, info.getQqScope());
45 | editor.putString(KEY_QQ_APP_ID, info.getQqAppId());
46 | editor.commit();
47 | }
48 |
49 | /**
50 | * 读取社交平台信息
51 | *
52 | * @param context context
53 | * @return 社交平台信息
54 | */
55 | public static SocialInfo readSocialInfo(Context context) {
56 | if (context == null)
57 | return null;
58 |
59 | SocialInfo info = new SocialInfo();
60 | SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
61 | info.setDebugMode(preferences.getBoolean(KEY_DEBUG, false));
62 | info.setWechatAppId(preferences.getString(KEY_WECHAT_APP_ID, ""));
63 | info.setWeChatAppSecret(preferences.getString(KEY_WECHAT_APP_SECRET, ""));
64 | info.setWeChatScope(preferences.getString(KEY_WECHAT_SCOPE, "snsapi_userinfo"));
65 | info.setWeiboAppKey(preferences.getString(KEY_WEIBO_APP_KEY, ""));
66 | info.setWeiboRedirectrUrl(preferences.getString(KEY_WEIBO_REDIRECT_URL, "http://www.sina.com"));
67 | info.setWeiboScope(preferences.getString(KEY_WEIBO_SCOPE, "email,direct_messages_read,direct_messages_write,"
68 | + "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
69 | + "follow_app_official_microblog," + "invitation_write"));
70 | info.setQqAppId(preferences.getString(KEY_QQ_APP_ID, ""));
71 | info.setQqScope(preferences.getString(KEY_QQ_SCOPE, "all"));
72 |
73 | return info;
74 | }
75 |
76 | /**
77 | * 清除社交平台信息
78 | *
79 | * @param context context
80 | */
81 | public static void clear(Context context) {
82 | if (null == context)
83 | return;
84 |
85 | SharedPreferences.Editor editor = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).edit();
86 | editor.clear();
87 | editor.commit();
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/SocialOauthActivity.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.sso;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.view.View;
7 |
8 | import com.elbbbird.android.socialsdk.R;
9 | import com.elbbbird.android.socialsdk.SocialSDK;
10 | import com.elbbbird.android.socialsdk.model.SocialInfo;
11 | import com.elbbbird.android.socialsdk.view.ShareButton;
12 | import com.tencent.connect.common.Constants;
13 |
14 | /**
15 | * 社交授权activity
16 | * Created by zhanghailong-ms on 2015/11/20.
17 | */
18 | public class SocialOauthActivity extends Activity {
19 |
20 | private static final String TAG = "SocialOauthActivity";
21 | private static boolean DEBUG = SocialSDK.isDebugModel();
22 |
23 | private SocialInfo info;
24 | private ShareButton llWeibo;
25 | private ShareButton llWeChat;
26 | private ShareButton llQQ;
27 |
28 | /**
29 | * type=0, 用户选择QQ或者微博登录
30 | * type=1,用户选择微信登录
31 | */
32 | private int type = 0;
33 |
34 | @Override
35 | protected void onCreate(Bundle savedInstanceState) {
36 | super.onCreate(savedInstanceState);
37 |
38 | setContentView(R.layout.es_activity_social_oauth);
39 |
40 | info = (SocialInfo) getIntent().getExtras().getSerializable("info");
41 |
42 | llWeibo = (ShareButton) findViewById(R.id.social_oauth_sb_weibo);
43 | llWeibo.setOnClickListener(new View.OnClickListener() {
44 | @Override
45 | public void onClick(View v) {
46 | SocialSSOProxy.loginWeibo(SocialOauthActivity.this, info);
47 | }
48 | });
49 | llWeChat = (ShareButton) findViewById(R.id.social_oauth_sb_wechat);
50 | llWeChat.setOnClickListener(new View.OnClickListener() {
51 | @Override
52 | public void onClick(View v) {
53 | SocialSSOProxy.loginWeChat(SocialOauthActivity.this, info);
54 | type = 1;
55 | }
56 | });
57 | llQQ = (ShareButton) findViewById(R.id.social_oauth_sb_qq);
58 | llQQ.setOnClickListener(new View.OnClickListener() {
59 | @Override
60 | public void onClick(View v) {
61 | SocialSSOProxy.loginQQ(SocialOauthActivity.this, info);
62 | }
63 | });
64 |
65 | }
66 |
67 | @Override
68 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
69 | super.onActivityResult(requestCode, resultCode, data);
70 | SocialSDK.oauthWeiboCallback(SocialOauthActivity.this, requestCode, resultCode, data);
71 | if (requestCode == Constants.REQUEST_LOGIN || requestCode == Constants.REQUEST_APPBAR) {
72 | SocialSDK.oauthQQCallback(requestCode, resultCode, data);
73 | }
74 |
75 | if (type == 0) {
76 | finish();
77 | }
78 | }
79 |
80 | @Override
81 | protected void onRestart() {
82 | super.onRestart();
83 |
84 | if (type == 1) {
85 | finish();
86 | }
87 | }
88 |
89 | @Override
90 | public void finish() {
91 | super.finish();
92 | overridePendingTransition(0, R.anim.es_snack_out);
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/SocialUserKeeper.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.sso;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 |
6 | import com.elbbbird.android.socialsdk.model.SocialToken;
7 | import com.elbbbird.android.socialsdk.model.SocialUser;
8 |
9 | /**
10 | * 用户信息持久化
11 | *
12 | * Created by zhanghailong-ms on 2015/11/16.
13 | */
14 | public class SocialUserKeeper {
15 |
16 | private static final String PREFERENCE_NAME = "es_social_user";
17 |
18 | private static final String KEY_TYPE = "type";
19 | private static final String KEY_OPENID = "open_id";
20 | private static final String KEY_NAME = "name";
21 | private static final String KEY_AVATAR = "avatar";
22 | private static final String KEY_GENDER = "gender";
23 | private static final String KEY_TOKEN = "token";
24 | private static final String KEY_EXPIRES_TIME = "expires_time";
25 | private static final String KEY_REFRESH_TOKEN = "refresh_token";
26 | private static final String KEY_SIGNATURE = "signature";
27 |
28 | /**
29 | * 持久化用户信息
30 | *
31 | * @param context context
32 | * @param user 用户信息
33 | */
34 | protected static void writeSocialUser(Context context, SocialUser user) {
35 | if (user == null || context == null)
36 | return;
37 |
38 | SharedPreferences.Editor editor = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).edit();
39 | editor.putInt(KEY_TYPE, user.getType());
40 | editor.putString(KEY_OPENID, user.getToken().getOpenId());
41 | editor.putString(KEY_NAME, user.getName());
42 | editor.putString(KEY_AVATAR, user.getAvatar());
43 | editor.putInt(KEY_GENDER, user.getGender());
44 | editor.putString(KEY_TOKEN, user.getToken().getToken());
45 | editor.putString(KEY_REFRESH_TOKEN, user.getToken().getRefreshToken());
46 | editor.putLong(KEY_EXPIRES_TIME, user.getToken().getExpiresTime());
47 | editor.putString(KEY_SIGNATURE, user.getDesc());
48 | editor.commit();
49 | }
50 |
51 | /**
52 | * 读取用户信息
53 | *
54 | * @param context context
55 | * @return 用户信息
56 | */
57 | protected static SocialUser readSocialUser(Context context) {
58 | if (context == null)
59 | return null;
60 |
61 | SocialUser user = new SocialUser();
62 | SocialToken token = new SocialToken();
63 | SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
64 | user.setType(preferences.getInt(KEY_TYPE, 0));
65 | user.setName(preferences.getString(KEY_NAME, ""));
66 | user.setAvatar(preferences.getString(KEY_AVATAR, ""));
67 | user.setGender(preferences.getInt(KEY_GENDER, 0));
68 | user.setDesc(preferences.getString(KEY_SIGNATURE, ""));
69 | token.setOpenId(preferences.getString(KEY_OPENID, ""));
70 | token.setToken(preferences.getString(KEY_TOKEN, ""));
71 | token.setRefreshToken(preferences.getString(KEY_REFRESH_TOKEN, ""));
72 | token.setExpiresTime(preferences.getLong(KEY_EXPIRES_TIME, 0));
73 | user.setToken(token);
74 |
75 | return user;
76 | }
77 |
78 | /**
79 | * 清除用户信息
80 | *
81 | * @param context context
82 | */
83 | protected static void clear(Context context) {
84 | if (null == context)
85 | return;
86 |
87 | SharedPreferences.Editor editor = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).edit();
88 | editor.clear();
89 | editor.commit();
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/qq/QQSSOProxy.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.sso.qq;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 |
6 | import com.elbbbird.android.socialsdk.model.SocialToken;
7 | import com.elbbbird.android.socialsdk.sso.SocialSSOProxy;
8 | import com.tencent.connect.UserInfo;
9 | import com.tencent.connect.auth.QQToken;
10 | import com.tencent.tauth.IUiListener;
11 | import com.tencent.tauth.Tencent;
12 |
13 | /**
14 | * QQ授权proxy
15 | *
16 | * Created by zhanghailong-ms on 2015/11/17.
17 | */
18 | public class QQSSOProxy {
19 |
20 | private static Tencent tencent;
21 |
22 | public static Tencent getTencentInstance(Context context, String appId) {
23 | if (tencent == null) {
24 | tencent = Tencent.createInstance(appId, context);
25 | }
26 |
27 | return tencent;
28 | }
29 |
30 | public static void login(Context context, String appId, String scope, IUiListener listener) {
31 | Tencent tencent = getTencentInstance(context, appId);
32 | if (!SocialSSOProxy.isTokenValid(context)) {
33 | tencent.login((Activity) context, scope, listener);
34 | }
35 | }
36 |
37 | public static void logout(Context context, String appId) {
38 | Tencent tencent = getTencentInstance(context, appId);
39 | if (SocialSSOProxy.isTokenValid(context)) {
40 | tencent.logout(context);
41 | }
42 |
43 | QQSSOProxy.tencent = null;
44 | }
45 |
46 | public static void getUserInfo(Context context, String appId, SocialToken token, IUiListener listener) {
47 | //这里只是为了在刷新用户信息的时候,设置一下全局变量。场景:APP升级安装,没有重新登录过程,调用该接口刷新用户信息,
48 | //如果没有该过程刷新Global.getContext()等常量,会出现空指针
49 | getTencentInstance(context, appId);
50 |
51 | if (SocialSSOProxy.isTokenValid(context)) {
52 | UserInfo info = new UserInfo(context, parseToken(appId, token));
53 | info.getUserInfo(listener);
54 | }
55 | }
56 |
57 | private static QQToken parseToken(String appId, SocialToken socialToken) {
58 | QQToken token = new QQToken(appId);
59 | //3600是随意定义的,不影响token的使用
60 | token.setAccessToken(socialToken.getToken(), "3600");
61 | token.setOpenId(socialToken.getOpenId());
62 |
63 | return token;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/wechat/IWXCallback.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.sso.wechat;
2 |
3 | import com.elbbbird.android.socialsdk.model.SocialToken;
4 | import com.elbbbird.android.socialsdk.model.SocialUser;
5 |
6 | /**
7 | * 微信授权回调接口
8 | *
9 | * Created by zhanghailong-ms on 2015/7/11.
10 | */
11 | public interface IWXCallback {
12 | void onGetCodeSuccess(String code);
13 |
14 | void onGetTokenSuccess(SocialToken token);
15 |
16 | void onGetUserInfoSuccess(SocialUser user);
17 |
18 | void onFailure(Exception e);
19 |
20 | void onCancel();
21 | }
22 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/wechat/WXCallbackActivity.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.sso.wechat;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 |
7 | import com.elbbbird.android.socialsdk.WeChat;
8 | import com.elbbbird.android.socialsdk.share.wechat.WeChatShareProxy;
9 | import com.tencent.mm.sdk.modelbase.BaseReq;
10 | import com.tencent.mm.sdk.modelbase.BaseResp;
11 | import com.tencent.mm.sdk.modelmsg.SendAuth;
12 | import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
13 | import com.tencent.mm.sdk.openapi.IWXAPI;
14 | import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
15 |
16 | /**
17 | * 微信授权,分享回调activity
18 | * Created by zhanghailong-ms on 2015/7/11.
19 | */
20 |
21 | public class WXCallbackActivity extends Activity implements IWXAPIEventHandler {
22 |
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 |
27 | handleIntent(getIntent());
28 | }
29 |
30 | private void handleIntent(Intent intent) {
31 | IWXAPI api = WeChat.getIWXAPIInstance();
32 | if (null != api)
33 | api.handleIntent(intent, this);
34 | }
35 |
36 | @Override
37 | protected void onNewIntent(Intent intent) {
38 | super.onNewIntent(intent);
39 |
40 | setIntent(intent);
41 | IWXAPI api = WeChat.getIWXAPIInstance();
42 | if (null != api)
43 | api.handleIntent(intent, this);
44 | }
45 |
46 | @Override
47 | public void onReq(BaseReq baseReq) {
48 | }
49 |
50 | @Override
51 | public void onResp(BaseResp resp) {
52 | if (resp instanceof SendAuth.Resp)
53 | WeChatSSOProxy.authComplete((SendAuth.Resp) resp);
54 | else if (resp instanceof SendMessageToWX.Resp)
55 | WeChatShareProxy.shareComplete((SendMessageToWX.Resp) resp);
56 |
57 | finish();
58 | }
59 |
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/wechat/WeChatSSOProxy.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.sso.wechat;
2 |
3 | import android.content.Context;
4 |
5 | import com.elbbbird.android.socialsdk.WeChat;
6 | import com.elbbbird.android.socialsdk.model.SocialInfo;
7 | import com.elbbbird.android.socialsdk.model.SocialToken;
8 | import com.elbbbird.android.socialsdk.model.SocialUser;
9 | import com.elbbbird.android.socialsdk.sso.SocialSSOProxy;
10 | import com.tencent.mm.sdk.modelbase.BaseResp;
11 | import com.tencent.mm.sdk.modelmsg.SendAuth;
12 |
13 | import org.json.JSONException;
14 | import org.json.JSONObject;
15 |
16 | import java.io.BufferedInputStream;
17 | import java.io.ByteArrayOutputStream;
18 | import java.io.IOException;
19 | import java.io.InputStream;
20 | import java.net.HttpURLConnection;
21 | import java.net.URL;
22 |
23 | /**
24 | * 微信授权proxy
25 | *
26 | * Created by zhanghailong-ms on 2015/11/17.
27 | */
28 | public class WeChatSSOProxy {
29 |
30 | private static IWXCallback callback;
31 |
32 | public static void login(Context context, IWXCallback callback, SocialInfo info) {
33 | if (!SocialSSOProxy.isTokenValid(context)) {
34 | WeChatSSOProxy.callback = callback;
35 | SendAuth.Req req = new SendAuth.Req();
36 | req.scope = info.getWeChatScope();
37 | WeChat.getIWXAPIInstance(context, info.getWechatAppId()).sendReq(req);
38 | }
39 | }
40 |
41 | public static void authComplete(SendAuth.Resp resp) {
42 |
43 | switch (resp.errCode) {
44 | case BaseResp.ErrCode.ERR_OK:
45 | callback.onGetCodeSuccess(resp.code);
46 | break;
47 | case BaseResp.ErrCode.ERR_USER_CANCEL:
48 | callback.onCancel();
49 | break;
50 | case BaseResp.ErrCode.ERR_AUTH_DENIED:
51 | callback.onFailure(new Exception("BaseResp.ErrCode.ERR_AUTH_DENIED"));
52 | break;
53 | }
54 | }
55 |
56 | public static void getToken(final String code, final String getTokenUrl) {
57 | new Thread(new Runnable() {
58 | @Override
59 | public void run() {
60 | try {
61 | URL url = new URL(String.format(getTokenUrl, code));
62 | HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
63 | InputStream in = new BufferedInputStream(urlConn.getInputStream());
64 |
65 | String result = inputStream2String(in);
66 | try {
67 | JSONObject info = new JSONObject(result);
68 | //当前token的有效市场只有7200s,需要利用refresh_token去获取新token,考虑当前需要利用token的只有获取用户信息,手动设置token超时为30天
69 | SocialToken token = new SocialToken(info.getString("openid"), info.getString("access_token"), info.getString("refresh_token"), /*info.getLong("expires_in")*/ 30 * 24 * 60 * 60);
70 | callback.onGetTokenSuccess(token);
71 | } catch (JSONException e) {
72 | callback.onFailure(e);
73 | }
74 | } catch (Exception e) {
75 | callback.onFailure(e);
76 | }
77 | }
78 | }).start();
79 |
80 | }
81 |
82 | public static void getUserInfo(final Context context, final String getUserInfoUrl, final SocialToken token) {
83 | if (SocialSSOProxy.isTokenValid(context)) {
84 | new Thread(new Runnable() {
85 | @Override
86 | public void run() {
87 | try {
88 | URL url = new URL(String.format(getUserInfoUrl, token.getToken(), token.getOpenId()));
89 | HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
90 | InputStream in = new BufferedInputStream(urlConn.getInputStream());
91 |
92 | String result = inputStream2String(in);
93 | try {
94 | JSONObject info = new JSONObject(result);
95 | String name = info.getString("nickname");
96 | int gender = info.getInt("sex");
97 | String icon = info.getString("headimgurl");
98 |
99 | SocialUser user = new SocialUser(SocialUser.TYPE_WECHAT,
100 | name, icon, gender, token);
101 |
102 | callback.onGetUserInfoSuccess(user);
103 | } catch (JSONException e) {
104 | callback.onFailure(e);
105 | }
106 | } catch (Exception e) {
107 | callback.onFailure(e);
108 | }
109 | }
110 | }).start();
111 | }
112 | }
113 |
114 | private static String inputStream2String(InputStream is) throws IOException {
115 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
116 | int i;
117 | while ((i = is.read()) != -1) {
118 | baos.write(i);
119 | }
120 | return baos.toString();
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/weibo/AbsOpenAPI.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.elbbbird.android.socialsdk.sso.weibo;
18 |
19 | import android.content.Context;
20 | import android.text.TextUtils;
21 |
22 | import com.sina.weibo.sdk.auth.Oauth2AccessToken;
23 | import com.sina.weibo.sdk.net.AsyncWeiboRunner;
24 | import com.sina.weibo.sdk.net.RequestListener;
25 | import com.sina.weibo.sdk.net.WeiboParameters;
26 | import com.sina.weibo.sdk.utils.LogUtil;
27 |
28 | /**
29 | *
30 | * @author SINA
31 | * @since 2013-11-05
32 | */
33 | public abstract class AbsOpenAPI {
34 | private static final String TAG = AbsOpenAPI.class.getName();
35 |
36 | /**
37 | */
38 | protected static final String API_SERVER = "https://api.weibo.com/2";
39 | /**
40 | */
41 | protected static final String HTTPMETHOD_POST = "POST";
42 | /**
43 | */
44 | protected static final String HTTPMETHOD_GET = "GET";
45 | /**
46 | */
47 | protected static final String KEY_ACCESS_TOKEN = "access_token";
48 |
49 | /**
50 | */
51 | protected Oauth2AccessToken mAccessToken;
52 | protected Context mContext;
53 | protected String mAppKey;
54 |
55 | /**
56 | */
57 | public AbsOpenAPI(Context context, String appKey, Oauth2AccessToken accessToken) {
58 | mContext = context;
59 | mAppKey = appKey;
60 | mAccessToken = accessToken;
61 | }
62 |
63 | /**
64 | */
65 | protected void requestAsync(String url, WeiboParameters params, String httpMethod, RequestListener listener) {
66 | if (null == mAccessToken
67 | || TextUtils.isEmpty(url)
68 | || null == params
69 | || TextUtils.isEmpty(httpMethod)
70 | || null == listener) {
71 | LogUtil.e(TAG, "Argument error!");
72 | return;
73 | }
74 |
75 | params.put(KEY_ACCESS_TOKEN, mAccessToken.getToken());
76 | new AsyncWeiboRunner(mContext).requestAsync(url, params, httpMethod, listener);
77 | }
78 |
79 | /**
80 | */
81 | protected String requestSync(String url, WeiboParameters params, String httpMethod) {
82 | if (null == mAccessToken
83 | || TextUtils.isEmpty(url)
84 | || null == params
85 | || TextUtils.isEmpty(httpMethod)) {
86 | LogUtil.e(TAG, "Argument error!");
87 | return "";
88 | }
89 |
90 | params.put(KEY_ACCESS_TOKEN, mAccessToken.getToken());
91 | return new AsyncWeiboRunner(mContext).request(url, params, httpMethod);
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/weibo/Geo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.elbbbird.android.socialsdk.sso.weibo;
18 |
19 | import android.text.TextUtils;
20 |
21 | import org.json.JSONException;
22 | import org.json.JSONObject;
23 |
24 | /**
25 | *
26 | * @author SINA
27 | * @since 2013-11-24
28 | */
29 | public class Geo {
30 |
31 | public String longitude;
32 | public String latitude;
33 | public String city;
34 | public String province;
35 | public String city_name;
36 | public String province_name;
37 | public String address;
38 | public String pinyin;
39 | public String more;
40 |
41 | public static Geo parse(String jsonString) {
42 | if (TextUtils.isEmpty(jsonString)) {
43 | return null;
44 | }
45 |
46 | Geo geo = null;
47 | try {
48 | JSONObject jsonObject = new JSONObject(jsonString);
49 | geo = parse(jsonObject);
50 | } catch (JSONException e) {
51 | e.printStackTrace();
52 | }
53 |
54 | return geo;
55 | }
56 |
57 | public static Geo parse(JSONObject jsonObject) {
58 | if (null == jsonObject) {
59 | return null;
60 | }
61 |
62 | Geo geo = new Geo();
63 | geo.longitude = jsonObject.optString("longitude");
64 | geo.latitude = jsonObject.optString("latitude");
65 | geo.city = jsonObject.optString("city");
66 | geo.province = jsonObject.optString("province");
67 | geo.city_name = jsonObject.optString("city_name");
68 | geo.province_name = jsonObject.optString("province_name");
69 | geo.address = jsonObject.optString("address");
70 | geo.pinyin = jsonObject.optString("pinyin");
71 | geo.more = jsonObject.optString("more");
72 |
73 | return geo;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/weibo/LogoutAPI.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.elbbbird.android.socialsdk.sso.weibo;
18 |
19 | import android.content.Context;
20 |
21 | import com.sina.weibo.sdk.auth.Oauth2AccessToken;
22 | import com.sina.weibo.sdk.net.RequestListener;
23 | import com.sina.weibo.sdk.net.WeiboParameters;
24 |
25 | /**
26 | *
27 | * @author SINA
28 | * @since 2013-11-05
29 | */
30 | public class LogoutAPI extends AbsOpenAPI {
31 | /**
32 | */
33 | private static final String REVOKE_OAUTH_URL = "https://api.weibo.com/oauth2/revokeoauth2";
34 |
35 | /**
36 | *
37 | */
38 | public LogoutAPI(Context context, String appKey, Oauth2AccessToken accessToken) {
39 | super(context, appKey, accessToken);
40 | }
41 |
42 | /**
43 | */
44 | public void logout(RequestListener listener) {
45 | requestAsync(REVOKE_OAUTH_URL, new WeiboParameters(mAppKey), HTTPMETHOD_POST, listener);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/weibo/Status.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.elbbbird.android.socialsdk.sso.weibo;
18 |
19 | import org.json.JSONArray;
20 | import org.json.JSONException;
21 | import org.json.JSONObject;
22 |
23 | import java.util.ArrayList;
24 |
25 | /**
26 | *
27 | * @author SINA
28 | * @since 2013-11-22
29 | */
30 | public class Status {
31 |
32 | public String created_at;
33 | public String id;
34 | public String mid;
35 | public String idstr;
36 | public String text;
37 | public String source;
38 | public boolean favorited;
39 | public boolean truncated;
40 | public String in_reply_to_status_id;
41 | public String in_reply_to_user_id;
42 | public String in_reply_to_screen_name;
43 | public String thumbnail_pic;
44 | public String bmiddle_pic;
45 | public String original_pic;
46 | public Geo geo;
47 | public User user;
48 | public Status retweeted_status;
49 | public int reposts_count;
50 | public int comments_count;
51 | public int attitudes_count;
52 | public int mlevel;
53 | /**
54 | */
55 | public Visible visible;
56 | public ArrayList pic_urls;
57 | //public Ad ad;
58 |
59 | public static Status parse(String jsonString) {
60 | try {
61 | JSONObject jsonObject = new JSONObject(jsonString);
62 | return Status.parse(jsonObject);
63 | } catch (JSONException e) {
64 | e.printStackTrace();
65 | }
66 |
67 | return null;
68 | }
69 |
70 | public static Status parse(JSONObject jsonObject) {
71 | if (null == jsonObject) {
72 | return null;
73 | }
74 |
75 | Status status = new Status();
76 | status.created_at = jsonObject.optString("created_at");
77 | status.id = jsonObject.optString("id");
78 | status.mid = jsonObject.optString("mid");
79 | status.idstr = jsonObject.optString("idstr");
80 | status.text = jsonObject.optString("text");
81 | status.source = jsonObject.optString("source");
82 | status.favorited = jsonObject.optBoolean("favorited", false);
83 | status.truncated = jsonObject.optBoolean("truncated", false);
84 |
85 | // Have NOT supported
86 | status.in_reply_to_status_id = jsonObject.optString("in_reply_to_status_id");
87 | status.in_reply_to_user_id = jsonObject.optString("in_reply_to_user_id");
88 | status.in_reply_to_screen_name = jsonObject.optString("in_reply_to_screen_name");
89 |
90 | status.thumbnail_pic = jsonObject.optString("thumbnail_pic");
91 | status.bmiddle_pic = jsonObject.optString("bmiddle_pic");
92 | status.original_pic = jsonObject.optString("original_pic");
93 | status.geo = Geo.parse(jsonObject.optJSONObject("geo"));
94 | status.user = User.parse(jsonObject.optJSONObject("user"));
95 | status.retweeted_status = Status.parse(jsonObject.optJSONObject("retweeted_status"));
96 | status.reposts_count = jsonObject.optInt("reposts_count");
97 | status.comments_count = jsonObject.optInt("comments_count");
98 | status.attitudes_count = jsonObject.optInt("attitudes_count");
99 | status.mlevel = jsonObject.optInt("mlevel", -1); // Have NOT supported
100 | status.visible = Visible.parse(jsonObject.optJSONObject("visible"));
101 |
102 | JSONArray picUrlsArray = jsonObject.optJSONArray("pic_urls");
103 | if (picUrlsArray != null && picUrlsArray.length() > 0) {
104 | int length = picUrlsArray.length();
105 | status.pic_urls = new ArrayList(length);
106 | JSONObject tmpObject = null;
107 | for (int ix = 0; ix < length; ix++) {
108 | tmpObject = picUrlsArray.optJSONObject(ix);
109 | if (tmpObject != null) {
110 | status.pic_urls.add(tmpObject.optString("thumbnail_pic"));
111 | }
112 | }
113 | }
114 |
115 | //status.ad = jsonObject.optString("ad", "");
116 |
117 | return status;
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/weibo/StatusesAPI.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.elbbbird.android.socialsdk.sso.weibo;
18 |
19 | import android.content.Context;
20 | import android.graphics.Bitmap;
21 | import android.text.TextUtils;
22 | import android.util.SparseArray;
23 |
24 | import com.sina.weibo.sdk.auth.Oauth2AccessToken;
25 | import com.sina.weibo.sdk.net.RequestListener;
26 | import com.sina.weibo.sdk.net.WeiboParameters;
27 |
28 | /**
29 | * @author SINA
30 | * @since 2014-03-03
31 | */
32 | public class StatusesAPI extends AbsOpenAPI {
33 |
34 | public static final int FEATURE_ALL = 0;
35 | public static final int FEATURE_ORIGINAL = 1;
36 | public static final int FEATURE_PICTURE = 2;
37 | public static final int FEATURE_VIDEO = 3;
38 | public static final int FEATURE_MUSICE = 4;
39 |
40 | public static final int AUTHOR_FILTER_ALL = 0;
41 | public static final int AUTHOR_FILTER_ATTENTIONS = 1;
42 | public static final int AUTHOR_FILTER_STRANGER = 2;
43 |
44 | public static final int SRC_FILTER_ALL = 0;
45 | public static final int SRC_FILTER_WEIBO = 1;
46 | public static final int SRC_FILTER_WEIQUN = 2;
47 |
48 | public static final int TYPE_FILTER_ALL = 0;
49 | public static final int TYPE_FILTER_ORIGAL = 1;
50 |
51 | /**
52 | * API URL
53 | */
54 | private static final String API_BASE_URL = API_SERVER + "/statuses";
55 |
56 | private static final int READ_API_FRIENDS_TIMELINE = 0;
57 | private static final int READ_API_MENTIONS = 1;
58 | private static final int WRITE_API_UPDATE = 2;
59 | private static final int WRITE_API_REPOST = 3;
60 | private static final int WRITE_API_UPLOAD = 4;
61 | private static final int WRITE_API_UPLOAD_URL_TEXT = 5;
62 |
63 | private static final SparseArray sAPIList = new SparseArray();
64 |
65 | static {
66 | sAPIList.put(READ_API_FRIENDS_TIMELINE, API_BASE_URL + "/friends_timeline.json");
67 | sAPIList.put(READ_API_MENTIONS, API_BASE_URL + "/mentions.json");
68 | sAPIList.put(WRITE_API_REPOST, API_BASE_URL + "/repost.json");
69 | sAPIList.put(WRITE_API_UPDATE, API_BASE_URL + "/update.json");
70 | sAPIList.put(WRITE_API_UPLOAD, API_BASE_URL + "/upload.json");
71 | sAPIList.put(WRITE_API_UPLOAD_URL_TEXT, API_BASE_URL + "/upload_url_text.json");
72 | }
73 |
74 | public StatusesAPI(Context context, String appKey, Oauth2AccessToken accessToken) {
75 | super(context, appKey, accessToken);
76 | }
77 |
78 | public void friendsTimeline(long since_id, long max_id, int count, int page, boolean base_app,
79 | int featureType, boolean trim_user, RequestListener listener) {
80 | WeiboParameters params =
81 | buildTimeLineParamsBase(since_id, max_id, count, page, base_app, trim_user, featureType);
82 | requestAsync(sAPIList.get(READ_API_FRIENDS_TIMELINE), params, HTTPMETHOD_GET, listener);
83 | }
84 |
85 | public void mentions(long since_id, long max_id, int count, int page, int authorType, int sourceType,
86 | int filterType, boolean trim_user, RequestListener listener) {
87 | WeiboParameters params = buildMentionsParams(since_id, max_id, count, page, authorType, sourceType, filterType, trim_user);
88 | requestAsync(sAPIList.get(READ_API_MENTIONS), params, HTTPMETHOD_GET, listener);
89 | }
90 |
91 | public void update(String content, String lat, String lon, RequestListener listener) {
92 | WeiboParameters params = buildUpdateParams(content, lat, lon);
93 | requestAsync(sAPIList.get(WRITE_API_UPDATE), params, HTTPMETHOD_POST, listener);
94 | }
95 |
96 | public void upload(String content, Bitmap bitmap, String lat, String lon, RequestListener listener) {
97 | WeiboParameters params = buildUpdateParams(content, lat, lon);
98 | params.put("pic", bitmap);
99 | requestAsync(sAPIList.get(WRITE_API_UPLOAD), params, HTTPMETHOD_POST, listener);
100 | }
101 |
102 | public void uploadUrlText(String status, String imageUrl, String pic_id, String lat, String lon,
103 | RequestListener listener) {
104 | WeiboParameters params = buildUpdateParams(status, lat, lon);
105 | params.put("url", imageUrl);
106 | params.put("pic_id", pic_id);
107 | requestAsync(sAPIList.get(WRITE_API_UPLOAD_URL_TEXT), params, HTTPMETHOD_POST, listener);
108 | }
109 |
110 | public String friendsTimelineSync(long since_id, long max_id, int count, int page, boolean base_app, int featureType,
111 | boolean trim_user) {
112 | WeiboParameters params = buildTimeLineParamsBase(since_id, max_id, count, page, base_app,
113 | trim_user, featureType);
114 | return requestSync(sAPIList.get(READ_API_FRIENDS_TIMELINE), params, HTTPMETHOD_GET);
115 | }
116 |
117 | public String mentionsSync(long since_id, long max_id, int count, int page,
118 | int authorType, int sourceType, int filterType, boolean trim_user) {
119 | WeiboParameters params = buildMentionsParams(since_id, max_id, count, page, authorType, sourceType, filterType, trim_user);
120 | return requestSync(sAPIList.get(READ_API_MENTIONS), params, HTTPMETHOD_GET);
121 | }
122 |
123 | public String updateSync(String content, String lat, String lon) {
124 | WeiboParameters params = buildUpdateParams(content, lat, lon);
125 | return requestSync(sAPIList.get(WRITE_API_UPDATE), params, HTTPMETHOD_POST);
126 | }
127 |
128 | public String uploadSync(String content, Bitmap bitmap, String lat, String lon) {
129 | WeiboParameters params = buildUpdateParams(content, lat, lon);
130 | params.put("pic", bitmap);
131 | return requestSync(sAPIList.get(WRITE_API_UPLOAD), params, HTTPMETHOD_POST);
132 | }
133 |
134 | public String uploadUrlTextSync(String status, String imageUrl, String pic_id, String lat, String lon) {
135 | WeiboParameters params = buildUpdateParams(status, lat, lon);
136 | params.put("url", imageUrl);
137 | params.put("pic_id", pic_id);
138 | return requestSync(sAPIList.get(WRITE_API_UPLOAD_URL_TEXT), params, HTTPMETHOD_POST);
139 | }
140 |
141 | private WeiboParameters buildTimeLineParamsBase(long since_id, long max_id, int count, int page,
142 | boolean base_app, boolean trim_user, int featureType) {
143 | WeiboParameters params = new WeiboParameters(mAppKey);
144 | params.put("since_id", since_id);
145 | params.put("max_id", max_id);
146 | params.put("count", count);
147 | params.put("page", page);
148 | params.put("base_app", base_app ? 1 : 0);
149 | params.put("trim_user", trim_user ? 1 : 0);
150 | params.put("feature", featureType);
151 | return params;
152 | }
153 |
154 | private WeiboParameters buildUpdateParams(String content, String lat, String lon) {
155 | WeiboParameters params = new WeiboParameters(mAppKey);
156 | params.put("status", content);
157 | if (!TextUtils.isEmpty(lon)) {
158 | params.put("long", lon);
159 | }
160 | if (!TextUtils.isEmpty(lat)) {
161 | params.put("lat", lat);
162 | }
163 | return params;
164 | }
165 |
166 | private WeiboParameters buildMentionsParams(long since_id, long max_id, int count, int page,
167 | int authorType, int sourceType, int filterType, boolean trim_user) {
168 | WeiboParameters params = new WeiboParameters(mAppKey);
169 | params.put("since_id", since_id);
170 | params.put("max_id", max_id);
171 | params.put("count", count);
172 | params.put("page", page);
173 | params.put("filter_by_author", authorType);
174 | params.put("filter_by_source", sourceType);
175 | params.put("filter_by_type", filterType);
176 | params.put("trim_user", trim_user ? 1 : 0);
177 |
178 | return params;
179 | }
180 | }
181 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/weibo/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.elbbbird.android.socialsdk.sso.weibo;
18 |
19 | import org.json.JSONException;
20 | import org.json.JSONObject;
21 |
22 | /**
23 | *
24 | * @author SINA
25 | * @since 2013-11-24
26 | */
27 | public class User {
28 |
29 | public String id;
30 | public String idstr;
31 | public String screen_name;
32 | public String name;
33 | public int province;
34 | public int city;
35 | public String location;
36 | public String description;
37 | public String url;
38 | public String profile_image_url;
39 | public String profile_url;
40 | public String domain;
41 | public String weihao;
42 | public String gender;
43 | public int followers_count;
44 | public int friends_count;
45 | public int statuses_count;
46 | public int favourites_count;
47 | public String created_at;
48 | public boolean following;
49 | public boolean allow_all_act_msg;
50 | public boolean geo_enabled;
51 | public boolean verified;
52 | public int verified_type;
53 | public String remark;
54 | public Status status;
55 | public boolean allow_all_comment;
56 | public String avatar_large;
57 | public String avatar_hd;
58 | public String verified_reason;
59 | public boolean follow_me;
60 | public int online_status;
61 | public int bi_followers_count;
62 | public String lang;
63 |
64 | public String star;
65 | public String mbtype;
66 | public String mbrank;
67 | public String block_word;
68 |
69 | public static User parse(String jsonString) {
70 | try {
71 | JSONObject jsonObject = new JSONObject(jsonString);
72 | return User.parse(jsonObject);
73 | } catch (JSONException e) {
74 | e.printStackTrace();
75 | }
76 |
77 | return null;
78 | }
79 |
80 | public static User parse(JSONObject jsonObject) {
81 | if (null == jsonObject) {
82 | return null;
83 | }
84 |
85 | User user = new User();
86 | user.id = jsonObject.optString("id", "");
87 | user.idstr = jsonObject.optString("idstr", "");
88 | user.screen_name = jsonObject.optString("screen_name", "");
89 | user.name = jsonObject.optString("name", "");
90 | user.province = jsonObject.optInt("province", -1);
91 | user.city = jsonObject.optInt("city", -1);
92 | user.location = jsonObject.optString("location", "");
93 | user.description = jsonObject.optString("description", "");
94 | user.url = jsonObject.optString("url", "");
95 | user.profile_image_url = jsonObject.optString("avatar_hd", "");
96 | user.profile_url = jsonObject.optString("profile_url", "");
97 | user.domain = jsonObject.optString("domain", "");
98 | user.weihao = jsonObject.optString("weihao", "");
99 | user.gender = jsonObject.optString("gender", "");
100 | user.followers_count = jsonObject.optInt("followers_count", 0);
101 | user.friends_count = jsonObject.optInt("friends_count", 0);
102 | user.statuses_count = jsonObject.optInt("statuses_count", 0);
103 | user.favourites_count = jsonObject.optInt("favourites_count", 0);
104 | user.created_at = jsonObject.optString("created_at", "");
105 | user.following = jsonObject.optBoolean("following", false);
106 | user.allow_all_act_msg = jsonObject.optBoolean("allow_all_act_msg", false);
107 | user.geo_enabled = jsonObject.optBoolean("geo_enabled", false);
108 | user.verified = jsonObject.optBoolean("verified", false);
109 | user.verified_type = jsonObject.optInt("verified_type", -1);
110 | user.remark = jsonObject.optString("remark", "");
111 | //user.status = jsonObject.optString("status", ""); // XXX: NO Need ?
112 | user.allow_all_comment = jsonObject.optBoolean("allow_all_comment", true);
113 | user.avatar_large = jsonObject.optString("avatar_large", "");
114 | user.avatar_hd = jsonObject.optString("avatar_hd", "");
115 | user.verified_reason = jsonObject.optString("verified_reason", "");
116 | user.follow_me = jsonObject.optBoolean("follow_me", false);
117 | user.online_status = jsonObject.optInt("online_status", 0);
118 | user.bi_followers_count = jsonObject.optInt("bi_followers_count", 0);
119 | user.lang = jsonObject.optString("lang", "");
120 |
121 | user.star = jsonObject.optString("star", "");
122 | user.mbtype = jsonObject.optString("mbtype", "");
123 | user.mbrank = jsonObject.optString("mbrank", "");
124 | user.block_word = jsonObject.optString("block_word", "");
125 |
126 | return user;
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/weibo/UsersAPI.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.elbbbird.android.socialsdk.sso.weibo;
18 |
19 | import android.content.Context;
20 | import android.util.SparseArray;
21 |
22 | import com.sina.weibo.sdk.auth.Oauth2AccessToken;
23 | import com.sina.weibo.sdk.net.RequestListener;
24 | import com.sina.weibo.sdk.net.WeiboParameters;
25 |
26 | /**
27 | * @author SINA
28 | * @since 2014-03-03
29 | */
30 | public class UsersAPI extends AbsOpenAPI {
31 |
32 | private static final int READ_USER = 0;
33 | private static final int READ_USER_BY_DOMAIN = 1;
34 | private static final int READ_USER_COUNT = 2;
35 |
36 | private static final String API_BASE_URL = API_SERVER + "/users";
37 |
38 | private static final SparseArray sAPIList = new SparseArray();
39 |
40 | static {
41 | sAPIList.put(READ_USER, API_BASE_URL + "/show.json");
42 | sAPIList.put(READ_USER_BY_DOMAIN, API_BASE_URL + "/domain_show.json");
43 | sAPIList.put(READ_USER_COUNT, API_BASE_URL + "/counts.json");
44 | }
45 |
46 | public UsersAPI(Context context, String appKey, Oauth2AccessToken accessToken) {
47 | super(context, appKey, accessToken);
48 | }
49 |
50 | public void show(long uid, RequestListener listener) {
51 | WeiboParameters params = new WeiboParameters(mAppKey);
52 | params.put("uid", uid);
53 | requestAsync(sAPIList.get(READ_USER), params, HTTPMETHOD_GET, listener);
54 | }
55 |
56 | public void show(String screen_name, RequestListener listener) {
57 | WeiboParameters params = new WeiboParameters(mAppKey);
58 | params.put("screen_name", screen_name);
59 | requestAsync(sAPIList.get(READ_USER), params, HTTPMETHOD_GET, listener);
60 | }
61 |
62 | public void domainShow(String domain, RequestListener listener) {
63 | WeiboParameters params = new WeiboParameters(mAppKey);
64 | params.put("domain", domain);
65 | requestAsync(sAPIList.get(READ_USER_BY_DOMAIN), params, HTTPMETHOD_GET, listener);
66 | }
67 |
68 | public void counts(long[] uids, RequestListener listener) {
69 | WeiboParameters params = buildCountsParams(uids);
70 | requestAsync(sAPIList.get(READ_USER_COUNT), params, HTTPMETHOD_GET, listener);
71 | }
72 |
73 | public String showSync(long uid) {
74 | WeiboParameters params = new WeiboParameters(mAppKey);
75 | params.put("uid", uid);
76 | return requestSync(sAPIList.get(READ_USER), params, HTTPMETHOD_GET);
77 | }
78 |
79 | public String showSync(String screen_name) {
80 | WeiboParameters params = new WeiboParameters(mAppKey);
81 | params.put("screen_name", screen_name);
82 | return requestSync(sAPIList.get(READ_USER), params, HTTPMETHOD_GET);
83 | }
84 |
85 | public String domainShowSync(String domain) {
86 | WeiboParameters params = new WeiboParameters(mAppKey);
87 | params.put("domain", domain);
88 | return requestSync(sAPIList.get(READ_USER_BY_DOMAIN), params, HTTPMETHOD_GET);
89 | }
90 |
91 | public String countsSync(long[] uids) {
92 | WeiboParameters params = buildCountsParams(uids);
93 | return requestSync(sAPIList.get(READ_USER_COUNT), params, HTTPMETHOD_GET);
94 | }
95 |
96 | private WeiboParameters buildCountsParams(long[] uids) {
97 | WeiboParameters params = new WeiboParameters(mAppKey);
98 | StringBuilder strb = new StringBuilder();
99 | for (long cid : uids) {
100 | strb.append(cid).append(",");
101 | }
102 | strb.deleteCharAt(strb.length() - 1);
103 | params.put("uids", strb.toString());
104 | return params;
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/weibo/Visible.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.elbbbird.android.socialsdk.sso.weibo;
18 |
19 | import org.json.JSONObject;
20 |
21 | /**
22 | *
23 | * @author SINA
24 | * @since 2013-11-24
25 | */
26 | public class Visible {
27 |
28 | public static final int VISIBLE_NORMAL = 0;
29 | public static final int VISIBLE_PRIVACY = 1;
30 | public static final int VISIBLE_GROUPED = 2;
31 | public static final int VISIBLE_FRIEND = 3;
32 |
33 | public int type;
34 | public int list_id;
35 |
36 | public static Visible parse(JSONObject jsonObject) {
37 | if (null == jsonObject) {
38 | return null;
39 | }
40 |
41 | Visible visible = new Visible();
42 | visible.type = jsonObject.optInt("type", 0);
43 | visible.list_id = jsonObject.optInt("list_id", 0);
44 |
45 | return visible;
46 | }
47 | }
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/sso/weibo/WeiboSSOProxy.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.sso.weibo;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.util.Log;
6 |
7 | import com.elbbbird.android.socialsdk.SocialSDK;
8 | import com.elbbbird.android.socialsdk.model.SocialInfo;
9 | import com.elbbbird.android.socialsdk.model.SocialToken;
10 | import com.elbbbird.android.socialsdk.sso.SocialSSOProxy;
11 | import com.sina.weibo.sdk.auth.AuthInfo;
12 | import com.sina.weibo.sdk.auth.Oauth2AccessToken;
13 | import com.sina.weibo.sdk.auth.WeiboAuthListener;
14 | import com.sina.weibo.sdk.auth.sso.SsoHandler;
15 | import com.sina.weibo.sdk.net.RequestListener;
16 |
17 | /**
18 | * 微博授权proxy
19 | * Created by zhanghailong-ms on 2015/11/16.
20 | */
21 | public class WeiboSSOProxy {
22 |
23 | private static final boolean DEBUG = SocialSDK.isDebugModel();
24 | private static final String TAG = "WeiboSSOProxy";
25 |
26 | private static SsoHandler ssoHandler;
27 | private static AuthInfo authInfo;
28 |
29 | public static SsoHandler getSsoHandler(Context context, SocialInfo info) {
30 | if (ssoHandler == null)
31 | ssoHandler = new SsoHandler((Activity) context, getAuthInfo(context, info.getWeiboAppKey(), info.getWeiboRedirectrUrl(), info.getWeiboScope()));
32 |
33 | return ssoHandler;
34 | }
35 |
36 | private static AuthInfo getAuthInfo(Context context, String key, String redirectUrl, String scope) {
37 | if (authInfo == null || !key.equals(authInfo.getAppKey()))
38 | authInfo = new AuthInfo(context, key, redirectUrl, scope);
39 |
40 | return authInfo;
41 | }
42 |
43 | public static void login(Context context, SocialInfo info, WeiboAuthListener listener) {
44 | if (!SocialSSOProxy.isTokenValid(context)) {
45 | getSsoHandler(context, info).authorize(listener);
46 | }
47 | }
48 |
49 | public static void logout(Context context, SocialInfo info, SocialToken token, RequestListener listener) {
50 | if (SocialSSOProxy.isTokenValid(context)) {
51 | LogoutAPI logout = new LogoutAPI(context, info.getWeiboAppKey(), parseToken(token));
52 | logout.logout(listener);
53 | }
54 |
55 | ssoHandler = null;
56 | authInfo = null;
57 | }
58 |
59 | public static void getUserInfo(Context context, SocialInfo info, SocialToken token, RequestListener listener) {
60 | if (DEBUG)
61 | Log.i(TAG, "getUserInfo");
62 | if (SocialSSOProxy.isTokenValid(context)) {
63 | if (DEBUG)
64 | Log.i(TAG, "getUserInfo#isTokenValid true");
65 | UsersAPI usersAPI = new UsersAPI(context, info.getWeiboAppKey(), parseToken(token));
66 | usersAPI.show(Long.parseLong(token.getOpenId()), listener);
67 | } else {
68 | if (DEBUG)
69 | Log.i(TAG, "getUserInfo#isTokenValid false");
70 | }
71 | }
72 |
73 | private static Oauth2AccessToken parseToken(SocialToken socialToken) {
74 | Oauth2AccessToken token = new Oauth2AccessToken();
75 | token.setUid(socialToken.getOpenId());
76 | token.setExpiresTime(socialToken.getExpiresTime());
77 | token.setToken(socialToken.getToken());
78 |
79 | return token;
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/view/OverlayImageView.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.view;
2 |
3 | import android.content.Context;
4 | import android.graphics.PorterDuff;
5 | import android.util.AttributeSet;
6 | import android.widget.ImageView;
7 |
8 | /**
9 | * Created by zhanghailong-ms on 2015/12/11.
10 | */
11 | public class OverlayImageView extends ImageView {
12 | public OverlayImageView(Context context) {
13 | super(context);
14 | }
15 |
16 | public OverlayImageView(Context context, AttributeSet attrs) {
17 | super(context, attrs);
18 | }
19 |
20 | public OverlayImageView(Context context, AttributeSet attrs, int defStyleAttr) {
21 | super(context, attrs, defStyleAttr);
22 | }
23 |
24 | @Override
25 | public void setPressed(boolean pressed) {
26 | super.setPressed(pressed);
27 | if (this.getDrawable() != null) {
28 | if (pressed) {
29 | this.getDrawable().setColorFilter(1140850688, PorterDuff.Mode.SRC_ATOP);
30 | this.invalidate();
31 | } else {
32 | this.getDrawable().clearColorFilter();
33 | this.invalidate();
34 | }
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/socialsdk/src/main/java/com/elbbbird/android/socialsdk/view/ShareButton.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk.view;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.drawable.Drawable;
6 | import android.util.AttributeSet;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.widget.ImageView;
10 | import android.widget.LinearLayout;
11 | import android.widget.TextView;
12 |
13 | import com.elbbbird.android.socialsdk.R;
14 |
15 | /**
16 | * Created by zhanghailong-ms on 2015/12/11.
17 | */
18 | public class ShareButton extends LinearLayout {
19 |
20 | private ImageView a;
21 | private TextView b;
22 | private Drawable c;
23 | private String d;
24 |
25 | public ShareButton(Context context) {
26 | this(context, null);
27 | }
28 |
29 | public ShareButton(Context context, AttributeSet attrs) {
30 | this(context, attrs, 0);
31 | }
32 |
33 | public ShareButton(Context context, AttributeSet attrs, int defStyleAttr) {
34 | super(context, attrs, defStyleAttr);
35 | TypedArray v1 = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShareButton, defStyleAttr, 0);
36 | this.d = v1.getString(0);
37 | this.c = v1.getDrawable(1);
38 | LayoutInflater v2 = LayoutInflater.from(context);
39 | View view = v2.inflate(R.layout.es_view_btn_share, null);
40 | this.a = (ImageView) view.findViewById(R.id.view_btn_share_iv);
41 | this.b = (TextView) view.findViewById(R.id.view_btn_share_tv);
42 | this.b.setText(this.d);
43 | this.a.setImageDrawable(this.c);
44 | this.addView(view);
45 | v1.recycle();
46 | }
47 |
48 | @Override
49 | public void setOnClickListener(OnClickListener l) {
50 | this.a.setOnClickListener(l);
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/socialsdk/src/main/res/anim/es_snack_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/socialsdk/src/main/res/anim/es_snack_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/socialsdk/src/main/res/drawable-xhdpi/es_icon_default.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangjie127/ESSocialSDK-master/bc27f42e3855109dbab52c8fb3513f6cd6cea62e/socialsdk/src/main/res/drawable-xhdpi/es_icon_default.png
--------------------------------------------------------------------------------
/socialsdk/src/main/res/drawable-xhdpi/es_icon_more.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangjie127/ESSocialSDK-master/bc27f42e3855109dbab52c8fb3513f6cd6cea62e/socialsdk/src/main/res/drawable-xhdpi/es_icon_more.png
--------------------------------------------------------------------------------
/socialsdk/src/main/res/drawable-xhdpi/es_icon_qq.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangjie127/ESSocialSDK-master/bc27f42e3855109dbab52c8fb3513f6cd6cea62e/socialsdk/src/main/res/drawable-xhdpi/es_icon_qq.png
--------------------------------------------------------------------------------
/socialsdk/src/main/res/drawable-xhdpi/es_icon_qzone.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangjie127/ESSocialSDK-master/bc27f42e3855109dbab52c8fb3513f6cd6cea62e/socialsdk/src/main/res/drawable-xhdpi/es_icon_qzone.png
--------------------------------------------------------------------------------
/socialsdk/src/main/res/drawable-xhdpi/es_icon_wechat.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangjie127/ESSocialSDK-master/bc27f42e3855109dbab52c8fb3513f6cd6cea62e/socialsdk/src/main/res/drawable-xhdpi/es_icon_wechat.png
--------------------------------------------------------------------------------
/socialsdk/src/main/res/drawable-xhdpi/es_icon_wechat_timeline.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangjie127/ESSocialSDK-master/bc27f42e3855109dbab52c8fb3513f6cd6cea62e/socialsdk/src/main/res/drawable-xhdpi/es_icon_wechat_timeline.png
--------------------------------------------------------------------------------
/socialsdk/src/main/res/drawable-xhdpi/es_icon_weibo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangjie127/ESSocialSDK-master/bc27f42e3855109dbab52c8fb3513f6cd6cea62e/socialsdk/src/main/res/drawable-xhdpi/es_icon_weibo.png
--------------------------------------------------------------------------------
/socialsdk/src/main/res/layout/es_activity_social_oauth.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
18 |
19 |
26 |
27 |
34 |
35 |
36 |
44 |
45 |
53 |
54 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/socialsdk/src/main/res/layout/es_activity_social_share.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
18 |
19 |
26 |
27 |
34 |
35 |
43 |
44 |
52 |
53 |
61 |
62 |
63 |
64 |
65 |
72 |
73 |
81 |
82 |
90 |
91 |
99 |
100 |
--------------------------------------------------------------------------------
/socialsdk/src/main/res/layout/es_view_btn_share.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
15 |
16 |
24 |
25 |
--------------------------------------------------------------------------------
/socialsdk/src/main/res/values/es_attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/socialsdk/src/main/res/values/es_strings.xml:
--------------------------------------------------------------------------------
1 |
2 | socialsdk
3 | 社交平台
4 | 微博登录
5 | 微信登录
6 | QQ登录
7 | 分享到
8 | 新浪微博
9 | 微信
10 | 朋友圈
11 | QQ好友
12 | QQ空间
13 | 更多
14 |
--------------------------------------------------------------------------------
/socialsdk/src/main/res/values/es_styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
12 |
13 |
19 |
20 |
--------------------------------------------------------------------------------
/socialsdk/src/test/java/com/elbbbird/android/socialsdk/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.elbbbird.android.socialsdk;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------