├── example
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── colors.xml
│ │ │ └── styles.xml
│ │ ├── mipmap-xxhdpi
│ │ │ ├── share_qq.png
│ │ │ ├── ic_launcher.png
│ │ │ ├── share_qzone.png
│ │ │ ├── share_weibo.png
│ │ │ ├── share_moment.png
│ │ │ ├── share_wechat.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ └── layout
│ │ │ ├── activity_main.xml
│ │ │ ├── activity_share.xml
│ │ │ ├── activity_login.xml
│ │ │ └── layout_bottom_share.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── younglive
│ │ └── livestreaming
│ │ ├── MainActivity.java
│ │ ├── ShareActivity.java
│ │ ├── ShareBottomDialog.java
│ │ └── LoginActivity.java
├── proguard-rules.pro
└── build.gradle
├── shareutil
├── .gitignore
├── libs
│ ├── libammsdk.jar
│ ├── weiboSDKCore_3.1.4.jar
│ └── open_sdk_r5756_lite.jar
├── src
│ └── main
│ │ ├── jniLibs
│ │ ├── mips
│ │ │ └── libweibosdkcore.so
│ │ ├── x86
│ │ │ └── libweibosdkcore.so
│ │ ├── mips64
│ │ │ └── libweibosdkcore.so
│ │ ├── x86_64
│ │ │ └── libweibosdkcore.so
│ │ ├── arm64-v8a
│ │ │ └── libweibosdkcore.so
│ │ ├── armeabi
│ │ │ └── libweibosdkcore.so
│ │ └── armeabi-v7a
│ │ │ └── libweibosdkcore.so
│ │ ├── res
│ │ └── values
│ │ │ └── strings.xml
│ │ ├── java
│ │ └── me
│ │ │ └── shaohui
│ │ │ └── shareutil
│ │ │ ├── ShareManager.java
│ │ │ ├── login
│ │ │ ├── LoginListener.java
│ │ │ ├── result
│ │ │ │ ├── QQToken.java
│ │ │ │ ├── BaseToken.java
│ │ │ │ ├── WxToken.java
│ │ │ │ ├── WeiboToken.java
│ │ │ │ ├── BaseUser.java
│ │ │ │ ├── QQUser.java
│ │ │ │ ├── WxUser.java
│ │ │ │ └── WeiboUser.java
│ │ │ ├── LoginPlatform.java
│ │ │ ├── instance
│ │ │ │ ├── LoginInstance.java
│ │ │ │ ├── WeiboLoginInstance.java
│ │ │ │ ├── QQLoginInstance.java
│ │ │ │ └── WxLoginInstance.java
│ │ │ └── LoginResult.java
│ │ │ ├── share
│ │ │ ├── SharePlatform.java
│ │ │ ├── ShareImageObject.java
│ │ │ ├── instance
│ │ │ │ ├── ShareInstance.java
│ │ │ │ ├── DefaultShareInstance.java
│ │ │ │ ├── WeiboShareInstance.java
│ │ │ │ ├── WxShareInstance.java
│ │ │ │ └── QQShareInstance.java
│ │ │ ├── ShareListener.java
│ │ │ └── ImageDecoder.java
│ │ │ ├── ShareConfig.java
│ │ │ ├── _ShareActivity.java
│ │ │ ├── ShareLogger.java
│ │ │ ├── LoginUtil.java
│ │ │ └── ShareUtil.java
│ │ └── AndroidManifest.xml
├── project.properties
├── build.gradle
├── proguard-rules.pro
└── bintrayUpload.gradle
├── settings.gradle
├── preview
├── shareutil_login.gif
└── shareutil_share.gif
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── README.md
├── gradlew
└── LICENSE
/example/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/shareutil/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':shareutil', ':sicilly', ':example'
2 |
--------------------------------------------------------------------------------
/preview/shareutil_login.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/preview/shareutil_login.gif
--------------------------------------------------------------------------------
/preview/shareutil_share.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/preview/shareutil_share.gif
--------------------------------------------------------------------------------
/shareutil/libs/libammsdk.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/shareutil/libs/libammsdk.jar
--------------------------------------------------------------------------------
/example/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LiveStreaming
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/shareutil/libs/weiboSDKCore_3.1.4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/shareutil/libs/weiboSDKCore_3.1.4.jar
--------------------------------------------------------------------------------
/shareutil/libs/open_sdk_r5756_lite.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/shareutil/libs/open_sdk_r5756_lite.jar
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxhdpi/share_qq.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-xxhdpi/share_qq.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxhdpi/share_qzone.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-xxhdpi/share_qzone.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxhdpi/share_weibo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-xxhdpi/share_weibo.png
--------------------------------------------------------------------------------
/shareutil/src/main/jniLibs/mips/libweibosdkcore.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/shareutil/src/main/jniLibs/mips/libweibosdkcore.so
--------------------------------------------------------------------------------
/shareutil/src/main/jniLibs/x86/libweibosdkcore.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/shareutil/src/main/jniLibs/x86/libweibosdkcore.so
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxhdpi/share_moment.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-xxhdpi/share_moment.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxhdpi/share_wechat.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-xxhdpi/share_wechat.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/shareutil/src/main/jniLibs/mips64/libweibosdkcore.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/shareutil/src/main/jniLibs/mips64/libweibosdkcore.so
--------------------------------------------------------------------------------
/shareutil/src/main/jniLibs/x86_64/libweibosdkcore.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/shareutil/src/main/jniLibs/x86_64/libweibosdkcore.so
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/shareutil/src/main/jniLibs/arm64-v8a/libweibosdkcore.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/shareutil/src/main/jniLibs/arm64-v8a/libweibosdkcore.so
--------------------------------------------------------------------------------
/shareutil/src/main/jniLibs/armeabi/libweibosdkcore.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/shareutil/src/main/jniLibs/armeabi/libweibosdkcore.so
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/example/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/shareutil/src/main/jniLibs/armeabi-v7a/libweibosdkcore.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shaohui10086/ShareUtil/HEAD/shareutil/src/main/jniLibs/armeabi-v7a/libweibosdkcore.so
--------------------------------------------------------------------------------
/shareutil/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ShareUtil
3 | 分享
4 |
5 |
--------------------------------------------------------------------------------
/example/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3A4349
4 | #3A4349
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/shareutil/project.properties:
--------------------------------------------------------------------------------
1 | #project
2 | project.name=ShareUtil
3 | project.groupId=me.shaohui.shareutil
4 | project.artifactId=shareutil
5 | project.packaging=aar
6 | project.siteUrl=https://github.com/shaohui10086/ShareUtil
7 | project.gitUrl=https://github.com/shaohui10086/ShareUtil.git
8 |
9 | #javadoc
10 | javadoc.name=ShareUtil
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/ShareManager.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil;
2 |
3 | /**
4 | * Created by shaohui on 2016/12/5.
5 | */
6 |
7 | public class ShareManager {
8 |
9 | private static boolean isInit = false;
10 |
11 | public static ShareConfig CONFIG;
12 |
13 | public static void init(ShareConfig config) {
14 | isInit = true;
15 | CONFIG = config;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/example/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/LoginListener.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login;
2 |
3 | import me.shaohui.shareutil.login.result.BaseToken;
4 |
5 | /**
6 | * Created by shaohui on 2016/12/2.
7 | */
8 |
9 | public abstract class LoginListener {
10 |
11 | public abstract void loginSuccess(LoginResult result);
12 |
13 | public void beforeFetchUserInfo(BaseToken token) {
14 | }
15 |
16 | public abstract void loginFailure(Exception e);
17 |
18 | public abstract void loginCancel();
19 | }
20 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/result/QQToken.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.result;
2 |
3 | import org.json.JSONException;
4 | import org.json.JSONObject;
5 |
6 | /**
7 | * Created by shaohui on 2016/12/3.
8 | */
9 |
10 | public class QQToken extends BaseToken {
11 |
12 | public static QQToken parse(JSONObject jsonObject) throws JSONException {
13 | QQToken token = new QQToken();
14 | token.setAccessToken(jsonObject.getString("access_token"));
15 | token.setOpenid(jsonObject.getString("openid"));
16 | return token;
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/result/BaseToken.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.result;
2 |
3 | /**
4 | * Created by shaohui on 2016/12/3.
5 | */
6 |
7 | public class BaseToken {
8 |
9 | private String access_token;
10 |
11 | private String openid;
12 |
13 | public String getAccessToken() {
14 | return access_token;
15 | }
16 |
17 | public void setAccessToken(String access_token) {
18 | this.access_token = access_token;
19 | }
20 |
21 | public String getOpenid() {
22 | return openid;
23 | }
24 |
25 | public void setOpenid(String openid) {
26 | this.openid = openid;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /.idea/libraries
2 | .DS_Store
3 | /captures
4 | .externalNativeBuild
5 |
6 | # Built application files
7 | *.apk
8 | *.ap_
9 |
10 | # Files for the ART/Dalvik VM
11 | *.dex
12 |
13 | # Java class files
14 | *.class
15 |
16 | # Generated files
17 | bin/
18 | gen/
19 | out/
20 |
21 | # Gradle files
22 | .gradle/
23 | build/
24 |
25 | # Local configuration file (sdk path, etc)
26 | local.properties
27 |
28 | # Proguard folder generated by Eclipse
29 | proguard/
30 |
31 | # Log Files
32 | *.log
33 |
34 | # Android Studio Navigation editor temp files
35 | .navigation/
36 |
37 | # Android Studio captures folder
38 | captures/
39 |
40 | # Intellij
41 | *.iml
42 | .idea/workspace.xml
43 | .idea/
44 |
45 |
46 | # Keystore files
47 | *.jks
48 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/share/SharePlatform.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.share;
2 |
3 | import android.support.annotation.IntDef;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 |
7 | /**
8 | * Created by shaohui on 2016/11/18.
9 | */
10 |
11 | public class SharePlatform {
12 |
13 | @IntDef({ DEFAULT, QQ, QZONE, WEIBO, WX, WX_TIMELINE })
14 | @Retention(RetentionPolicy.SOURCE)
15 | public @interface Platform{}
16 |
17 | public static final int DEFAULT = 0;
18 | public static final int QQ = 1;
19 | public static final int QZONE = 2;
20 | public static final int WX = 3;
21 | public static final int WX_TIMELINE = 4;
22 | public static final int WEIBO = 5;
23 |
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/example/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/LoginPlatform.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login;
2 |
3 | import android.support.annotation.IntDef;
4 | import java.lang.annotation.Documented;
5 | import java.lang.annotation.ElementType;
6 | import java.lang.annotation.Retention;
7 | import java.lang.annotation.RetentionPolicy;
8 | import java.lang.annotation.Target;
9 |
10 | /**
11 | * Created by shaohui on 2016/12/1.
12 | */
13 |
14 | public class LoginPlatform {
15 |
16 | @Documented
17 | @IntDef({QQ, WX, WEIBO})
18 | @Retention(RetentionPolicy.SOURCE)
19 | @Target(ElementType.PARAMETER)
20 | public @interface Platform {
21 |
22 | }
23 |
24 | public static final int QQ = 1;
25 |
26 | public static final int WX = 3;
27 |
28 | public static final int WEIBO = 5;
29 | }
30 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/share/ShareImageObject.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.share;
2 |
3 | import android.graphics.Bitmap;
4 |
5 | /**
6 | * Created by shaohui on 2016/11/19.
7 | */
8 |
9 | public class ShareImageObject {
10 |
11 | private Bitmap mBitmap;
12 |
13 | private String mPathOrUrl;
14 |
15 | public ShareImageObject(Bitmap bitmap) {
16 | mBitmap = bitmap;
17 | }
18 |
19 | public ShareImageObject(String pathOrUrl) {
20 | mPathOrUrl = pathOrUrl;
21 | }
22 |
23 | public Bitmap getBitmap() {
24 | return mBitmap;
25 | }
26 |
27 | public void setBitmap(Bitmap bitmap) {
28 | mBitmap = bitmap;
29 | }
30 |
31 | public String getPathOrUrl() {
32 | return mPathOrUrl;
33 | }
34 |
35 | public void setPathOrUrl(String pathOrUrl) {
36 | mPathOrUrl = pathOrUrl;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/shareutil/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 9
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.3.8"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | compile 'com.android.support:appcompat-v7:25.1.0'
23 | compile 'io.reactivex:rxjava:1.2.3'
24 | compile 'io.reactivex:rxandroid:1.2.1'
25 | compile 'com.squareup.okhttp3:okhttp:3.4.2'
26 | compile files('libs/open_sdk_r5756_lite.jar')
27 | compile files('libs/weiboSDKCore_3.1.4.jar')
28 | compile files('libs/libammsdk.jar')
29 | }
30 |
31 | apply from: 'bintrayUpload.gradle'
32 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/result/WxToken.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.result;
2 |
3 | import org.json.JSONException;
4 | import org.json.JSONObject;
5 |
6 | /**
7 | * Created by shaohui on 2016/12/3.
8 | */
9 |
10 | public class WxToken extends BaseToken {
11 |
12 | private String refresh_token;
13 |
14 | public static WxToken parse(JSONObject jsonObject) throws JSONException {
15 | WxToken wxToken = new WxToken();
16 | wxToken.setOpenid(jsonObject.getString("openid"));
17 | wxToken.setAccessToken(jsonObject.getString("access_token"));
18 | wxToken.setRefreshToken(jsonObject.getString("refresh_token"));
19 | return wxToken;
20 | }
21 |
22 | public String getRefreshToken() {
23 | return refresh_token;
24 | }
25 |
26 | public void setRefreshToken(String refresh_token) {
27 | this.refresh_token = refresh_token;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/share/instance/ShareInstance.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.share.instance;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import me.shaohui.shareutil.share.ShareImageObject;
7 | import me.shaohui.shareutil.share.ShareListener;
8 |
9 | /**
10 | * Created by shaohui on 2016/11/18.
11 | */
12 |
13 | public interface ShareInstance {
14 |
15 | void shareText(int platform, String text, Activity activity, ShareListener listener);
16 |
17 | void shareMedia(int platform, String title, String targetUrl, String summary,
18 | ShareImageObject shareImageObject, Activity activity, ShareListener listener);
19 |
20 | void shareImage(int platform, ShareImageObject shareImageObject, Activity activity,
21 | ShareListener listener);
22 |
23 | void handleResult(Intent data);
24 |
25 | boolean isInstall(Context context);
26 |
27 | void recycle();
28 | }
29 |
--------------------------------------------------------------------------------
/example/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/shaohui/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/instance/LoginInstance.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.instance;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import com.tencent.mm.sdk.modelbase.BaseResp;
7 | import com.tencent.mm.sdk.modelmsg.SendAuth;
8 | import me.shaohui.shareutil.login.LoginListener;
9 | import me.shaohui.shareutil.login.result.BaseToken;
10 |
11 | /**
12 | * Created by shaohui on 2016/12/1.
13 | */
14 |
15 | public abstract class LoginInstance {
16 |
17 | public LoginInstance(Activity activity, LoginListener listener, boolean fetchUserInfo) {
18 |
19 | }
20 |
21 | public abstract void doLogin(Activity activity, LoginListener listener, boolean fetchUserInfo);
22 |
23 | public abstract void fetchUserInfo(BaseToken token);
24 |
25 | public abstract void handleResult(int requestCode, int resultCode, Intent data);
26 |
27 | public abstract boolean isInstall(Context context);
28 |
29 | public abstract void recycle();
30 | }
31 |
--------------------------------------------------------------------------------
/shareutil/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/shaohui/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/example/src/main/res/layout/activity_share.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
19 |
20 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/result/WeiboToken.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.result;
2 |
3 | import com.sina.weibo.sdk.auth.Oauth2AccessToken;
4 |
5 | /**
6 | * Created by shaohui on 2016/12/3.
7 | */
8 |
9 | public class WeiboToken extends BaseToken {
10 |
11 | private String refreshToken;
12 |
13 | private String phoneNum;
14 |
15 | public static WeiboToken parse(Oauth2AccessToken token) {
16 | WeiboToken target = new WeiboToken();
17 | target.setOpenid(token.getUid());
18 | target.setAccessToken(token.getToken());
19 | target.setRefreshToken(token.getRefreshToken());
20 | target.setPhoneNum(token.getPhoneNum());
21 | return target;
22 | }
23 |
24 | public String getRefreshToken() {
25 | return refreshToken;
26 | }
27 |
28 | public void setRefreshToken(String refreshToken) {
29 | this.refreshToken = refreshToken;
30 | }
31 |
32 | public String getPhoneNum() {
33 | return phoneNum;
34 | }
35 |
36 | public void setPhoneNum(String phoneNum) {
37 | this.phoneNum = phoneNum;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/LoginResult.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login;
2 |
3 | import me.shaohui.shareutil.login.result.BaseToken;
4 | import me.shaohui.shareutil.login.result.BaseUser;
5 |
6 | /**
7 | * Created by shaohui on 2016/12/3.
8 | */
9 |
10 | public class LoginResult {
11 |
12 | private BaseToken mToken;
13 |
14 | private BaseUser mUserInfo;
15 |
16 | private int mPlatform;
17 |
18 | public LoginResult(int platform, BaseToken token) {
19 | mPlatform = platform;
20 | mToken = token;
21 | }
22 |
23 | public LoginResult(int platform, BaseToken token, BaseUser userInfo) {
24 | mPlatform = platform;
25 | mToken = token;
26 | mUserInfo = userInfo;
27 | }
28 |
29 | public int getPlatform() {
30 | return mPlatform;
31 | }
32 |
33 | public void setPlatform(int platform) {
34 | this.mPlatform = platform;
35 | }
36 |
37 | public BaseToken getToken() {
38 | return mToken;
39 | }
40 |
41 | public void setToken(BaseToken token) {
42 | mToken = token;
43 | }
44 |
45 | public BaseUser getUserInfo() {
46 | return mUserInfo;
47 | }
48 |
49 | public void setUserInfo(BaseUser userInfo) {
50 | mUserInfo = userInfo;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/example/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
16 |
17 |
18 |
19 |
20 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/result/BaseUser.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.result;
2 |
3 | /**
4 | * Created by shaohui on 2016/12/1.
5 | */
6 |
7 | public class BaseUser {
8 |
9 | /**
10 | * sex
11 | * 0. 未知
12 | * 1. 男
13 | * 2. 女
14 | */
15 |
16 | private String openId;
17 |
18 | private String nickname;
19 |
20 | private int sex;
21 |
22 | private String headImageUrl;
23 |
24 | private String headImageUrlLarge;
25 |
26 | public String getOpenId() {
27 | return openId;
28 | }
29 |
30 | public void setOpenId(String openId) {
31 | this.openId = openId;
32 | }
33 |
34 | public String getNickname() {
35 | return nickname;
36 | }
37 |
38 | public void setNickname(String nickname) {
39 | this.nickname = nickname;
40 | }
41 |
42 | public int getSex() {
43 | return sex;
44 | }
45 |
46 | public void setSex(int sex) {
47 | this.sex = sex;
48 | }
49 |
50 | public String getHeadImageUrl() {
51 | return headImageUrl;
52 | }
53 |
54 | public void setHeadImageUrl(String headImageUrl) {
55 | this.headImageUrl = headImageUrl;
56 | }
57 |
58 | public String getHeadImageUrlLarge() {
59 | return headImageUrlLarge;
60 | }
61 |
62 | public void setHeadImageUrlLarge(String headImageUrlLarge) {
63 | this.headImageUrlLarge = headImageUrlLarge;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/result/QQUser.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.result;
2 |
3 | import android.text.TextUtils;
4 | import org.json.JSONException;
5 | import org.json.JSONObject;
6 |
7 | /**
8 | * Created by shaohui on 2016/12/1.
9 | */
10 |
11 | public class QQUser extends BaseUser {
12 |
13 | private String qZoneHeadImage;
14 |
15 | private String qZoneHeadImageLarge;
16 |
17 | public static QQUser parse(String openId, JSONObject jsonObject) throws JSONException {
18 | QQUser user = new QQUser();
19 | user.setNickname(jsonObject.getString("nickname"));
20 | user.setOpenId(openId);
21 | user.setSex(TextUtils.equals("男", jsonObject.getString("gender")) ? 1 : 2);
22 | user.setHeadImageUrl(jsonObject.getString("figureurl_qq_1"));
23 | user.setHeadImageUrlLarge(jsonObject.getString("figureurl_qq_2"));
24 | user.setqZoneHeadImage(jsonObject.getString("figureurl_1"));
25 | user.setqZoneHeadImageLarge(jsonObject.getString("figureurl_2"));
26 |
27 | return user;
28 | }
29 |
30 | public String getqZoneHeadImage() {
31 | return qZoneHeadImage;
32 | }
33 |
34 | public void setqZoneHeadImage(String qZoneHeadImage) {
35 | this.qZoneHeadImage = qZoneHeadImage;
36 | }
37 |
38 | public String getqZoneHeadImageLarge() {
39 | return qZoneHeadImageLarge;
40 | }
41 |
42 | public void setqZoneHeadImageLarge(String qZoneHeadImageLarge) {
43 | this.qZoneHeadImageLarge = qZoneHeadImageLarge;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/example/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.1"
6 |
7 | signingConfigs {
8 | config {
9 | keyAlias 'XXXXXX'
10 | keyPassword 'XXXXXX'
11 | storeFile file('XXXXXX')
12 | storePassword 'XXXXXX'
13 | }
14 | }
15 |
16 | defaultConfig {
17 | applicationId "com.younglive.livestreaming"
18 | minSdkVersion 15
19 | targetSdkVersion 25
20 | versionCode 1
21 | versionName "1.0"
22 |
23 | manifestPlaceholders = [
24 | qq_id: "XXXXXX"
25 | ]
26 |
27 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
28 | }
29 | buildTypes {
30 | release {
31 | minifyEnabled false
32 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
33 | }
34 | debug {
35 | signingConfig signingConfigs.config
36 | }
37 | }
38 | }
39 |
40 | dependencies {
41 | compile fileTree(include: ['*.jar'], dir: 'libs')
42 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
43 | exclude group: 'com.android.support', module: 'support-annotations'
44 | })
45 | compile project(':shareutil')
46 | compile 'me.shaohui:bottomdialog:1.1.9'
47 | compile 'com.android.support:appcompat-v7:25.0.0'
48 | compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
49 | testCompile 'junit:junit:4.12'
50 | }
51 |
--------------------------------------------------------------------------------
/example/src/main/res/layout/activity_login.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
18 |
25 |
32 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/share/ShareListener.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.share;
2 |
3 | import com.sina.weibo.sdk.api.share.BaseResponse;
4 | import com.sina.weibo.sdk.api.share.IWeiboHandler;
5 | import com.sina.weibo.sdk.constant.WBConstants;
6 | import com.tencent.tauth.IUiListener;
7 | import com.tencent.tauth.UiError;
8 |
9 | import static me.shaohui.shareutil.ShareLogger.INFO;
10 |
11 | /**
12 | * Created by shaohui on 2016/11/18.
13 | */
14 |
15 | public abstract class ShareListener implements IUiListener, IWeiboHandler.Response {
16 | @Override
17 | public final void onComplete(Object o) {
18 | shareSuccess();
19 | }
20 |
21 | @Override
22 | public final void onError(UiError uiError) {
23 | shareFailure(
24 | new Exception(uiError == null ? INFO.DEFAULT_QQ_SHARE_ERROR : uiError.errorDetail));
25 | }
26 |
27 | @Override
28 | public final void onCancel() {
29 | shareCancel();
30 | }
31 |
32 | @Override
33 | public final void onResponse(BaseResponse baseResponse) {
34 | switch (baseResponse.errCode) {
35 | case WBConstants.ErrorCode.ERR_OK:
36 | shareSuccess();
37 | break;
38 | case WBConstants.ErrorCode.ERR_FAIL:
39 | shareFailure(new Exception(baseResponse.errMsg));
40 | break;
41 | case WBConstants.ErrorCode.ERR_CANCEL:
42 | shareCancel();
43 | break;
44 | default:
45 | shareFailure(new Exception(baseResponse.errMsg));
46 | }
47 | }
48 |
49 | public abstract void shareSuccess();
50 |
51 | public abstract void shareFailure(Exception e);
52 |
53 | public abstract void shareCancel();
54 |
55 | // 用于缓解用户焦虑
56 | public void shareRequest() {
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/result/WxUser.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.result;
2 |
3 | import org.json.JSONException;
4 | import org.json.JSONObject;
5 |
6 | /**
7 | * Created by shaohui on 2016/12/1.
8 | */
9 |
10 | public class WxUser extends BaseUser {
11 |
12 | private String city;
13 |
14 | private String country;
15 |
16 | private String province;
17 |
18 | private String unionid;
19 |
20 | public static WxUser parse(JSONObject jsonObject) throws JSONException {
21 | WxUser user = new WxUser();
22 | user.setOpenId(jsonObject.getString("openid"));
23 | user.setNickname(jsonObject.getString("nickname"));
24 | user.setSex(jsonObject.getInt("sex"));
25 | user.setHeadImageUrl(jsonObject.getString("headimgurl"));
26 | user.setHeadImageUrlLarge(jsonObject.getString("headimgurl")); // 重复
27 | user.setProvince(jsonObject.getString("province"));
28 | user.setCity(jsonObject.getString("city"));
29 | user.setCountry(jsonObject.getString("country"));
30 | user.setUnionid(jsonObject.getString("unionid"));
31 |
32 | return user;
33 | }
34 |
35 | public String getCity() {
36 | return city;
37 | }
38 |
39 | public void setCity(String city) {
40 | this.city = city;
41 | }
42 |
43 | public String getCountry() {
44 | return country;
45 | }
46 |
47 | public void setCountry(String country) {
48 | this.country = country;
49 | }
50 |
51 | public String getProvince() {
52 | return province;
53 | }
54 |
55 | public void setProvince(String province) {
56 | this.province = province;
57 | }
58 |
59 | public String getUnionid() {
60 | return unionid;
61 | }
62 |
63 | public void setUnionid(String unionid) {
64 | this.unionid = unionid;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/ShareConfig.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil;
2 |
3 | /**
4 | * Created by shaohui on 2016/12/7.
5 | */
6 |
7 | public class ShareConfig {
8 |
9 | private String wxId;
10 |
11 | private String wxSecret;
12 |
13 | private String qqId;
14 |
15 | private String weiboId;
16 |
17 | private String weiboRedirectUrl = "https://api.weibo.com/oauth2/default.html";
18 |
19 | private String weiboScope = "email";
20 |
21 | private boolean debug;
22 |
23 | public static ShareConfig instance() {
24 | return new ShareConfig();
25 | }
26 |
27 | public ShareConfig wxId(String id) {
28 | wxId = id;
29 | return this;
30 | }
31 |
32 | public ShareConfig wxSecret(String id) {
33 | wxSecret = id;
34 | return this;
35 | }
36 |
37 | public ShareConfig qqId(String id) {
38 | qqId = id;
39 | return this;
40 | }
41 |
42 | public ShareConfig weiboId(String id) {
43 | weiboId = id;
44 | return this;
45 | }
46 |
47 | public ShareConfig weiboRedirectUrl(String url) {
48 | weiboRedirectUrl = url;
49 | return this;
50 | }
51 |
52 | public ShareConfig weiboScope(String scope) {
53 | weiboScope = scope;
54 | return this;
55 | }
56 |
57 | public ShareConfig debug(boolean isDebug) {
58 | debug = isDebug;
59 | return this;
60 | }
61 |
62 | public String getWxId() {
63 | return wxId;
64 | }
65 |
66 | public String getWxSecret() {
67 | return wxSecret;
68 | }
69 |
70 | public String getQqId() {
71 | return qqId;
72 | }
73 |
74 | public String getWeiboId() {
75 | return weiboId;
76 | }
77 |
78 | public String getWeiboRedirectUrl() {
79 | return weiboRedirectUrl;
80 | }
81 |
82 | public String getWeiboScope() {
83 | return weiboScope;
84 | }
85 |
86 | public boolean isDebug() {
87 | return debug;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/example/src/main/java/com/younglive/livestreaming/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.younglive.livestreaming;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.util.Log;
6 | import android.view.View;
7 | import me.shaohui.shareutil.ShareConfig;
8 | import me.shaohui.shareutil.ShareManager;
9 | import me.shaohui.shareutil.login.LoginListener;
10 | import me.shaohui.shareutil.login.LoginPlatform;
11 | import me.shaohui.shareutil.login.LoginResult;
12 | import me.shaohui.shareutil.LoginUtil;
13 | import me.shaohui.shareutil.login.result.BaseToken;
14 |
15 | public class MainActivity extends AppCompatActivity {
16 |
17 | String APP_ID = "XXXXXX";
18 | String APP_SECRET = "XXXXXXX";
19 |
20 | @Override
21 | protected void onCreate(Bundle savedInstanceState) {
22 | super.onCreate(savedInstanceState);
23 | setContentView(R.layout.activity_main);
24 |
25 | ShareConfig config = ShareConfig.instance().wxId(APP_ID).wxSecret(APP_SECRET);
26 | ShareManager.init(config);
27 |
28 | findViewById(R.id.action_login).setOnClickListener(new View.OnClickListener() {
29 | @Override
30 | public void onClick(View view) {
31 | LoginUtil.login(MainActivity.this, LoginPlatform.WX, new LoginListener() {
32 | @Override
33 | public void loginSuccess(LoginResult result) {
34 | Log.i("TAG", result.getUserInfo().getNickname());
35 | Log.i("TAG", "登录成功");
36 | }
37 |
38 | @Override
39 | public void beforeFetchUserInfo(BaseToken token) {
40 | Log.i("TAG", "获取用户信息");
41 | }
42 |
43 | @Override
44 | public void loginFailure(Exception e) {
45 | e.printStackTrace();
46 | Log.i("TAG", "登录失败");
47 | }
48 |
49 | @Override
50 | public void loginCancel() {
51 | Log.i("TAG", "登录取消");
52 | }
53 | });
54 | }
55 | });
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/example/src/main/java/com/younglive/livestreaming/ShareActivity.java:
--------------------------------------------------------------------------------
1 | package com.younglive.livestreaming;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 | import android.view.View;
7 | import me.shaohui.shareutil.ShareConfig;
8 | import me.shaohui.shareutil.ShareManager;
9 | import me.shaohui.shareutil.ShareUtil;
10 | import me.shaohui.shareutil.share.ShareListener;
11 | import me.shaohui.shareutil.share.SharePlatform;
12 |
13 | public class ShareActivity extends AppCompatActivity {
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | setContentView(R.layout.activity_share);
19 |
20 | findViewById(R.id.action_share).setOnClickListener(new View.OnClickListener() {
21 | @Override
22 | public void onClick(View view) {
23 | ShareBottomDialog dialog = new ShareBottomDialog();
24 | dialog.show(getSupportFragmentManager());
25 | }
26 | });
27 |
28 | findViewById(R.id.action_share_qq).setOnClickListener(new View.OnClickListener() {
29 | @Override
30 | public void onClick(View view) {
31 | ShareUtil.shareImage(ShareActivity.this, SharePlatform.QQ,
32 | "http://shaohui.me/images/avatar.gif", new ShareListener() {
33 | @Override
34 | public void shareSuccess() {
35 | Log.i("TAG", "分享成功");
36 | }
37 |
38 | @Override
39 | public void shareFailure(Exception e) {
40 | Log.i("TAG", "分享失败");
41 | }
42 |
43 | @Override
44 | public void shareCancel() {
45 | Log.i("TAG", "分享取消");
46 | }
47 | });
48 | }
49 | });
50 |
51 | // 初始化shareUtil
52 | ShareConfig config = ShareConfig.instance()
53 | .qqId("XXXXXX")
54 | .weiboId("XXXXXX")
55 | .wxId("xXXXXX");
56 | ShareManager.init(config);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/shareutil/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
7 |
8 |
9 |
14 |
20 |
21 |
22 |
29 |
30 |
31 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
46 |
47 |
48 |
49 |
50 |
51 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/_ShareActivity.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil;
2 |
3 | import android.app.Activity;
4 | import android.app.Application;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.os.Bundle;
8 | import android.support.annotation.Nullable;
9 |
10 | import static me.shaohui.shareutil.ShareLogger.INFO;
11 |
12 | /**
13 | * Created by shaohui on 2016/11/19.
14 | */
15 |
16 | public class _ShareActivity extends Activity {
17 |
18 | private int mType;
19 |
20 | private boolean isNew;
21 |
22 | private static final String TYPE = "share_activity_type";
23 |
24 | public static Intent newInstance(Context context, int type) {
25 | Intent intent = new Intent(context, _ShareActivity.class);
26 | if (context instanceof Application) {
27 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
28 | }
29 | intent.putExtra(TYPE, type);
30 | return intent;
31 | }
32 |
33 | @Override
34 | protected void onCreate(@Nullable Bundle savedInstanceState) {
35 | super.onCreate(savedInstanceState);
36 | ShareLogger.i(INFO.ACTIVITY_CREATE);
37 | isNew = true;
38 |
39 | // init data
40 | mType = getIntent().getIntExtra(TYPE, 0);
41 | if (mType == ShareUtil.TYPE) {
42 | // 分享
43 | ShareUtil.action(this);
44 | } else if (mType == LoginUtil.TYPE) {
45 | // 登录
46 | LoginUtil.action(this);
47 | } else {
48 | // handle 微信回调
49 | LoginUtil.handleResult(-1, -1, getIntent());
50 | ShareUtil.handleResult(getIntent());
51 | finish();
52 | }
53 | }
54 |
55 | @Override
56 | protected void onResume() {
57 | super.onResume();
58 | ShareLogger.i(INFO.ACTIVITY_RESUME);
59 | if (isNew) {
60 | isNew = false;
61 | } else {
62 | finish();
63 | }
64 | }
65 |
66 | @Override
67 | protected void onNewIntent(Intent intent) {
68 | super.onNewIntent(intent);
69 | ShareLogger.i(INFO.ACTIVITY_NEW_INTENT);
70 | // 处理回调
71 | if (mType == LoginUtil.TYPE) {
72 | LoginUtil.handleResult(0, 0, intent);
73 | } else if (mType == ShareUtil.TYPE) {
74 | ShareUtil.handleResult(intent);
75 | }
76 | finish();
77 | }
78 |
79 | @Override
80 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
81 | super.onActivityResult(requestCode, resultCode, data);
82 | ShareLogger.i(INFO.ACTIVITY_RESULT);
83 | // 处理回调
84 | if (mType == LoginUtil.TYPE) {
85 | LoginUtil.handleResult(requestCode, resultCode, data);
86 | } else if (mType == ShareUtil.TYPE) {
87 | ShareUtil.handleResult(data);
88 | }
89 | finish();
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/example/src/main/java/com/younglive/livestreaming/ShareBottomDialog.java:
--------------------------------------------------------------------------------
1 | package com.younglive.livestreaming;
2 |
3 | import android.util.Log;
4 | import android.view.View;
5 | import android.widget.Toast;
6 | import me.shaohui.bottomdialog.BaseBottomDialog;
7 | import me.shaohui.shareutil.ShareUtil;
8 | import me.shaohui.shareutil.share.ShareListener;
9 | import me.shaohui.shareutil.share.SharePlatform;
10 |
11 | /**
12 | * Created by shaohui on 2016/12/10.
13 | */
14 |
15 | public class ShareBottomDialog extends BaseBottomDialog implements View.OnClickListener {
16 |
17 | private ShareListener mShareListener;
18 |
19 | @Override
20 | public int getLayoutRes() {
21 | return R.layout.layout_bottom_share;
22 | }
23 |
24 | @Override
25 | public void bindView(final View v) {
26 | v.findViewById(R.id.share_qq).setOnClickListener(this);
27 | v.findViewById(R.id.share_qzone).setOnClickListener(this);
28 | v.findViewById(R.id.share_weibo).setOnClickListener(this);
29 | v.findViewById(R.id.share_wx).setOnClickListener(this);
30 | v.findViewById(R.id.share_wx_timeline).setOnClickListener(this);
31 |
32 | mShareListener = new ShareListener() {
33 | @Override
34 | public void shareSuccess() {
35 | Toast.makeText(v.getContext(), "分享成功", Toast.LENGTH_SHORT).show();
36 | }
37 |
38 | @Override
39 | public void shareFailure(Exception e) {
40 | Toast.makeText(v.getContext(), "分享失败", Toast.LENGTH_SHORT).show();
41 | }
42 |
43 | @Override
44 | public void shareCancel() {
45 | Toast.makeText(v.getContext(), "取消分享", Toast.LENGTH_SHORT).show();
46 |
47 | }
48 | };
49 | }
50 |
51 | @Override
52 | public void onClick(View view) {
53 | switch (view.getId()) {
54 | case R.id.share_qq:
55 | ShareUtil.shareImage(getContext(), SharePlatform.QQ,
56 | "http://shaohui.me/images/avatar.gif", mShareListener);
57 | break;
58 | case R.id.share_qzone:
59 | ShareUtil.shareMedia(getContext(), SharePlatform.QZONE, "Title", "summary",
60 | "http://www.google.com", "http://shaohui.me/images/avatar.gif",
61 | mShareListener);
62 | break;
63 | case R.id.share_weibo:
64 | ShareUtil.shareImage(getContext(), SharePlatform.WEIBO,
65 | "http://shaohui.me/images/avatar.gif", mShareListener);
66 | break;
67 | case R.id.share_wx_timeline:
68 | ShareUtil.shareText(getContext(), SharePlatform.WX_TIMELINE, "测试分享文字",
69 | mShareListener);
70 | break;
71 | case R.id.share_wx:
72 | ShareUtil.shareMedia(getContext(), SharePlatform.WX, "Title", "summary",
73 | "http://www.google.com", "http://shaohui.me/images/avatar.gif",
74 | mShareListener);
75 | break;
76 | }
77 | dismiss();
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/ShareLogger.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil;
2 |
3 | import android.util.Log;
4 |
5 | /**
6 | * Created by shaohui on 2016/12/8.
7 | */
8 |
9 | public class ShareLogger {
10 |
11 | private static final String TAG = "share_util_log";
12 |
13 | public static void i(String info) {
14 | if (ShareManager.CONFIG.isDebug()) {
15 | Log.i(TAG, info);
16 | }
17 | }
18 |
19 | public static void e(String error) {
20 | if (ShareManager.CONFIG.isDebug()) {
21 | Log.e(TAG, error);
22 | }
23 | }
24 |
25 | public static class INFO {
26 | public static final String SHARE_SUCCESS = "call share success";
27 | public static final String SHARE_FAILURE = "call share failure";
28 | public static final String SHARE_CANCEL = "call share cancel";
29 | public static final String SHARE_REQUEST = "call share request";
30 |
31 | // for share
32 | public static final String HANDLE_DATA_NULL = "Handle the result, but the data is null, please check you app id";
33 | public static final String UNKNOWN_ERROR = "Unknown error";
34 | public static final String NOT_INSTALL = "The application is not install";
35 | public static final String DEFAULT_QQ_SHARE_ERROR = "QQ share failed";
36 | public static final String QQ_NOT_SUPPORT_SHARE_TXT = "QQ not support share text";
37 | public static final String IMAGE_FETCH_ERROR = "Image fetch error";
38 | public static final String SD_CARD_NOT_AVAILABLE = "The sd card is not available";
39 |
40 | // for login
41 | public static final String LOGIN_SUCCESS = "call login success";
42 | public static final String LOGIN_FAIl = "call login failed";
43 | public static final String LOGIN_CANCEL = "call login cancel";
44 | public static final String LOGIN_AUTH_SUCCESS = "call before fetch user info";
45 | public static final String ILLEGAL_TOKEN = "Illegal token, please check your config";
46 | public static final String QQ_LOGIN_ERROR = "QQ login error";
47 | public static final String QQ_AUTH_SUCCESS = "QQ auth success";
48 | public static final String WEIBO_AUTH_ERROR = "weibo auth error";
49 | public static final String UNKNOW_PLATFORM = "unknown platform";
50 |
51 | public static final String WX_ERR_SENT_FAILED = "Wx sent failed";
52 | public static final String WX_ERR_UNSUPPORT = "Wx UnSupport";
53 | public static final String WX_ERR_AUTH_DENIED = "Wx auth denied";
54 | public static final String WX_ERR_AUTH_ERROR = "Wx auth error";
55 |
56 | public static final String AUTH_CANCEL = "auth cancel";
57 | public static final String FETCH_USER_INOF_ERROR = "Fetch user info error";
58 |
59 | // for shareActivity
60 | public static final String ACTIVITY_CREATE = "ShareActivity onCreate";
61 | public static final String ACTIVITY_RESUME = "ShareActivity onResume";
62 | public static final String ACTIVITY_RESULT = "ShareActivity onActivityResult";
63 | public static final String ACTIVITY_NEW_INTENT = "ShareActivity onNewIntent";
64 |
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/example/src/main/java/com/younglive/livestreaming/LoginActivity.java:
--------------------------------------------------------------------------------
1 | package com.younglive.livestreaming;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.view.View;
6 | import android.widget.Toast;
7 | import me.shaohui.shareutil.LoginUtil;
8 | import me.shaohui.shareutil.ShareConfig;
9 | import me.shaohui.shareutil.ShareManager;
10 | import me.shaohui.shareutil.login.LoginListener;
11 | import me.shaohui.shareutil.login.LoginPlatform;
12 | import me.shaohui.shareutil.login.LoginResult;
13 | import me.shaohui.shareutil.login.result.QQToken;
14 | import me.shaohui.shareutil.login.result.QQUser;
15 |
16 | public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
17 |
18 | private LoginListener mLoginListener;
19 |
20 | @Override
21 | protected void onCreate(Bundle savedInstanceState) {
22 | super.onCreate(savedInstanceState);
23 | setContentView(R.layout.activity_login);
24 |
25 | ShareConfig config = ShareConfig.instance()
26 | .qqId("XXXXXX")
27 | .weiboId("XXXXX")
28 | .wxId("XXXXX")
29 | .weiboRedirectUrl("XXXXXX")
30 | .wxSecret("XXXXXX");
31 | ShareManager.init(config);
32 |
33 | findViewById(R.id.login_qq).setOnClickListener(this);
34 | findViewById(R.id.login_weibo).setOnClickListener(this);
35 | findViewById(R.id.login_wx).setOnClickListener(this);
36 |
37 | mLoginListener = new LoginListener() {
38 | @Override
39 | public void loginSuccess(LoginResult result) {
40 | Toast.makeText(LoginActivity.this,
41 | result.getUserInfo() != null ? result.getUserInfo().getNickname()
42 | : "" + "登录成功", Toast.LENGTH_SHORT).show();
43 |
44 | // 处理result
45 | switch (result.getPlatform()) {
46 | case LoginPlatform.QQ:
47 | QQUser user = (QQUser) result.getUserInfo();
48 | QQToken token = (QQToken) result.getToken();
49 | break;
50 | case LoginPlatform.WEIBO:
51 | // 处理信息
52 | break;
53 | case LoginPlatform.WX:
54 |
55 | break;
56 | }
57 | }
58 |
59 | @Override
60 | public void loginFailure(Exception e) {
61 | Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
62 | }
63 |
64 | @Override
65 | public void loginCancel() {
66 | Toast.makeText(LoginActivity.this, "登录取消", Toast.LENGTH_SHORT).show();
67 | }
68 | };
69 | }
70 |
71 | @Override
72 | public void onClick(View view) {
73 | switch (view.getId()) {
74 | case R.id.login_qq:
75 | LoginUtil.login(this, LoginPlatform.QQ, mLoginListener);
76 | break;
77 | case R.id.login_weibo:
78 | LoginUtil.login(this, LoginPlatform.WEIBO, mLoginListener, false);
79 | break;
80 | case R.id.login_wx:
81 | LoginUtil.login(this, LoginPlatform.WX, mLoginListener);
82 | break;
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/shareutil/bintrayUpload.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.github.dcendents.android-maven'
2 | apply plugin: 'com.jfrog.bintray'
3 |
4 | // load properties
5 | Properties properties = new Properties()
6 | File localPropertiesFile = project.file("local.properties");
7 | if(localPropertiesFile.exists()){
8 | properties.load(localPropertiesFile.newDataInputStream())
9 | }
10 | File projectPropertiesFile = project.file("project.properties");
11 | if(projectPropertiesFile.exists()){
12 | properties.load(projectPropertiesFile.newDataInputStream())
13 | }
14 |
15 | // read properties
16 | def projectName = properties.getProperty("project.name")
17 | def projectGroupId = properties.getProperty("project.groupId")
18 | def projectArtifactId = properties.getProperty("project.artifactId")
19 | def projectVersionName = android.defaultConfig.versionName
20 | def projectPackaging = properties.getProperty("project.packaging")
21 | def projectSiteUrl = properties.getProperty("project.siteUrl")
22 | def projectGitUrl = properties.getProperty("project.gitUrl")
23 |
24 | def developerId = properties.getProperty("developer.id")
25 | def developerName = properties.getProperty("developer.name")
26 | def developerEmail = properties.getProperty("developer.email")
27 |
28 | def bintrayUser = properties.getProperty("bintray.user")
29 | def bintrayApikey = properties.getProperty("bintray.apikey")
30 |
31 | def javadocName = properties.getProperty("javadoc.name")
32 |
33 | group = projectGroupId
34 |
35 | // This generates POM.xml with proper parameters
36 | install {
37 | repositories.mavenInstaller {
38 | pom {
39 | project {
40 | name projectName
41 | groupId projectGroupId
42 | artifactId projectArtifactId
43 | version projectVersionName
44 | packaging projectPackaging
45 | url projectSiteUrl
46 | licenses {
47 | license {
48 | name 'The Apache Software License, Version 2.0'
49 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
50 | }
51 | }
52 | developers {
53 | developer {
54 | id developerId
55 | name developerName
56 | email developerEmail
57 | }
58 | }
59 | scm {
60 | connection projectGitUrl
61 | developerConnection projectGitUrl
62 | url projectSiteUrl
63 | }
64 | }
65 | }
66 | }
67 | }
68 |
69 | // This generates sources.jar
70 | task sourcesJar(type: Jar) {
71 | from android.sourceSets.main.java.srcDirs
72 | classifier = 'sources'
73 | }
74 |
75 | task javadoc(type: Javadoc) {
76 | source = android.sourceSets.main.java.srcDirs
77 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
78 | }
79 |
80 | // This generates javadoc.jar
81 | task javadocJar(type: Jar, dependsOn: javadoc) {
82 | classifier = 'javadoc'
83 | from javadoc.destinationDir
84 | }
85 |
86 | artifacts {
87 | archives javadocJar
88 | archives sourcesJar
89 | }
90 |
91 | // javadoc configuration
92 | javadoc {
93 | options{
94 | encoding "UTF-8"
95 | charSet 'UTF-8'
96 | author true
97 | version projectVersionName
98 | links "http://docs.oracle.com/javase/7/docs/api"
99 | title javadocName
100 | }
101 | }
102 |
103 | // bintray configuration
104 | bintray {
105 | user = bintrayUser
106 | key = bintrayApikey
107 | configurations = ['archives']
108 | pkg {
109 | repo = "maven"
110 | name = projectName
111 | websiteUrl = projectSiteUrl
112 | vcsUrl = projectGitUrl
113 | licenses = ["Apache-2.0"]
114 | publish = true
115 | }
116 | }
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/result/WeiboUser.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.result;
2 |
3 | import android.text.TextUtils;
4 | import org.json.JSONException;
5 | import org.json.JSONObject;
6 |
7 | /**
8 | * Created by shaohui on 2016/12/1.
9 | */
10 |
11 | public class WeiboUser extends BaseUser {
12 |
13 | private String language;
14 |
15 | private String city;
16 |
17 | private String province;
18 |
19 | private String headimgurl_hd;
20 |
21 | /**
22 | * 示例
23 | * {
24 | * "id": 1404376560,
25 | * "screen_name": "zaku",
26 | * "name": "zaku",
27 | * "province": "11",
28 | * "city": "5",
29 | * "location": "北京 朝阳区",
30 | * "description": "人生五十年,乃如梦如幻;有生斯有死,壮士复何憾。",
31 | * "url": "http://blog.sina.com.cn/zaku",
32 | * "profile_image_url": "http://tp1.sinaimg.cn/1404376560/50/0/1",
33 | * "domain": "zaku",
34 | * "gender": "m",
35 | * "followers_count": 1204,
36 | * "friends_count": 447,
37 | * "statuses_count": 2908,
38 | * "favourites_count": 0,
39 | * "created_at": "Fri Aug 28 00:00:00 +0800 2009",
40 | * "following": false,
41 | * "allow_all_act_msg": false,
42 | * "geo_enabled": true,
43 | * "verified": false,
44 | * "status": {
45 | * "created_at": "Tue May 24 18:04:53 +0800 2011",
46 | * "id": 11142488790,
47 | * "text": "我的相机到了。",
48 | * "favorited": false,
49 | * "truncated": false,
50 | * "in_reply_to_status_id": "",
51 | * "in_reply_to_user_id": "",
52 | * "in_reply_to_screen_name": "",
53 | * "geo": null,
54 | * "mid": "5610221544300749636",
55 | * "annotations": [],
56 | * "reposts_count": 5,
57 | * "comments_count": 8
58 | * },
59 | * "allow_all_comment": true,
60 | * "avatar_large": "http://tp1.sinaimg.cn/1404376560/180/0/1",
61 | * "verified_reason": "",
62 | * "follow_me": false,
63 | * "online_status": 0,
64 | * "bi_followers_count": 215
65 | * }
66 | */
67 |
68 | public static WeiboUser parse(JSONObject jsonObject) throws JSONException {
69 | WeiboUser user = new WeiboUser();
70 | user.setOpenId(String.valueOf(jsonObject.getInt("id")));
71 | user.setNickname(jsonObject.getString("screen_name"));
72 |
73 | // 性别
74 | String gender = jsonObject.getString("gender");
75 | if (TextUtils.equals(gender, "m")) {
76 | user.setSex(1);
77 | } else if (TextUtils.equals(gender, "f")) {
78 | user.setSex(2);
79 | } else {
80 | user.setSex(0);
81 | }
82 |
83 | user.setHeadImageUrl(jsonObject.getString("profile_image_url"));
84 | user.setHeadImageUrlLarge(jsonObject.getString("avatar_large"));
85 | user.setHeadimgurl_hd(jsonObject.getString("avatar_hd"));
86 | user.setCity(jsonObject.getString("city"));
87 | user.setProvince(jsonObject.getString("province"));
88 |
89 | return user;
90 | }
91 |
92 | public String getLanguage() {
93 | return language;
94 | }
95 |
96 | public void setLanguage(String language) {
97 | this.language = language;
98 | }
99 |
100 | public String getCity() {
101 | return city;
102 | }
103 |
104 | public void setCity(String city) {
105 | this.city = city;
106 | }
107 |
108 | public String getProvince() {
109 | return province;
110 | }
111 |
112 | public void setProvince(String province) {
113 | this.province = province;
114 | }
115 |
116 | public String getHeadimgurl_hd() {
117 | return headimgurl_hd;
118 | }
119 |
120 | public void setHeadimgurl_hd(String headimgurl_hd) {
121 | this.headimgurl_hd = headimgurl_hd;
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/LoginUtil.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import me.shaohui.shareutil.login.LoginListener;
7 | import me.shaohui.shareutil.login.LoginPlatform;
8 | import me.shaohui.shareutil.login.LoginResult;
9 | import me.shaohui.shareutil.login.instance.LoginInstance;
10 | import me.shaohui.shareutil.login.instance.QQLoginInstance;
11 | import me.shaohui.shareutil.login.instance.WeiboLoginInstance;
12 | import me.shaohui.shareutil.login.instance.WxLoginInstance;
13 | import me.shaohui.shareutil.login.result.BaseToken;
14 |
15 | import static me.shaohui.shareutil.ShareLogger.INFO;
16 |
17 | /**
18 | * Created by shaohui on 2016/12/3.
19 | */
20 |
21 | public class LoginUtil {
22 |
23 | private static LoginInstance mLoginInstance;
24 |
25 | private static LoginListener mLoginListener;
26 |
27 | private static int mPlatform;
28 |
29 | private static boolean isFetchUserInfo;
30 |
31 | static final int TYPE = 799;
32 |
33 | public static void login(Context context, @LoginPlatform.Platform int platform,
34 | LoginListener listener) {
35 | login(context, platform, listener, true);
36 | }
37 |
38 | public static void login(Context context, @LoginPlatform.Platform int platform,
39 | LoginListener listener, boolean fetchUserInfo) {
40 | mPlatform = platform;
41 | mLoginListener = new LoginListenerProxy(listener);
42 | isFetchUserInfo = fetchUserInfo;
43 | context.startActivity(_ShareActivity.newInstance(context, TYPE));
44 | }
45 |
46 | static void action(Activity activity) {
47 | switch (mPlatform) {
48 | case LoginPlatform.QQ:
49 | mLoginInstance = new QQLoginInstance(activity, mLoginListener, isFetchUserInfo);
50 | break;
51 | case LoginPlatform.WEIBO:
52 | mLoginInstance = new WeiboLoginInstance(activity, mLoginListener, isFetchUserInfo);
53 | break;
54 | case LoginPlatform.WX:
55 | mLoginInstance = new WxLoginInstance(activity, mLoginListener, isFetchUserInfo);
56 | break;
57 | default:
58 | mLoginListener.loginFailure(new Exception(INFO.UNKNOW_PLATFORM));
59 | activity.finish();
60 | }
61 | mLoginInstance.doLogin(activity, mLoginListener, isFetchUserInfo);
62 | }
63 |
64 | static void handleResult(int requestCode, int resultCode, Intent data) {
65 | if (mLoginInstance != null) {
66 | mLoginInstance.handleResult(requestCode, resultCode, data);
67 | }
68 | }
69 |
70 | public static void recycle() {
71 | if (mLoginInstance != null) {
72 | mLoginInstance.recycle();
73 | }
74 | mLoginInstance = null;
75 | mLoginListener = null;
76 | mPlatform = 0;
77 | isFetchUserInfo = false;
78 | }
79 |
80 | private static class LoginListenerProxy extends LoginListener {
81 |
82 | private LoginListener mListener;
83 |
84 | LoginListenerProxy(LoginListener listener) {
85 | mListener = listener;
86 | }
87 |
88 | @Override
89 | public void loginSuccess(LoginResult result) {
90 | ShareLogger.i(INFO.LOGIN_SUCCESS);
91 | mListener.loginSuccess(result);
92 | recycle();
93 | }
94 |
95 | @Override
96 | public void loginFailure(Exception e) {
97 | ShareLogger.i(INFO.LOGIN_FAIl);
98 | mListener.loginFailure(e);
99 | recycle();
100 | }
101 |
102 | @Override
103 | public void loginCancel() {
104 | ShareLogger.i(INFO.LOGIN_CANCEL);
105 | mListener.loginCancel();
106 | recycle();
107 | }
108 |
109 | @Override
110 | public void beforeFetchUserInfo(BaseToken token) {
111 | ShareLogger.i(INFO.LOGIN_AUTH_SUCCESS);
112 | mListener.beforeFetchUserInfo(token);
113 | }
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ShareUtil
2 | `ShareUtil`是一个综合性的分享及登录工具库,支持微信分享,微博分享,QQ分享,QQ空间分享以及Android系统默认分享,支持微信登录,微博登录以及QQ登录并获取用户信息。
3 |
4 | ## Preview
5 | 
6 | 
7 | ## Feature
8 |
9 | 1. 多种分享方式:
10 |
11 | 2. 支持分享图片本地链接,网络链接或者Bitmap, 不需要考虑各个平台的不一致性。
12 |
13 | 3. 支持微信、QQ、微博登录并获取登录用户信息
14 |
15 | ## Usage
16 |
17 | ### 添加依赖
18 |
19 | 免第三方Jar包配置
20 |
21 | compile 'me.shaohui.shareutil:shareutil:1.3.8'
22 |
23 | 手动添加第三方jar
24 |
25 | compile 'me.shaohui.shareutil:shareutil:1.3.8.no_jar'
26 |
27 | ### 使用配置
28 |
29 | 1. build.gradle 配置
30 | 在defaultConfig节点下增加你的qq id信息
31 |
32 | defaultConfig {
33 | ...
34 |
35 | manifestPlaceholders = [
36 | // 替换成你的qq_id
37 | qq_id: "123456789"
38 | ]
39 |
40 | }
41 | 2. 在使用之前设置在各个平台申请的Id,以及分享的回调(推荐放在Application的onCreate方法中)
42 |
43 | // init
44 | ShareConfig config = ShareConfig.instance()
45 | .qqId(QQ_ID)
46 | .wxId(WX_ID)
47 | .weiboId(WEIBO_ID)
48 | // 下面两个,如果不需要登录功能,可不填写
49 | .weiboRedirectUrl(REDIRECT_URL)
50 | .wxSecret(WX_ID);
51 | ShareManager.init(config);
52 |
53 | ### 分享使用
54 |
55 | ShareUtil.shareImage(this, SharePlatform.QQ, "http://image.com", shareListener);
56 | ShareUtil.shareText(this, SharePlatform.WX, "分享文字", shareListener);
57 | ShareUtil.shareMedia(this, SharePlatform.QZONE, "title", "summary", "targetUrl", "thumb", shareListener);
58 |
59 |
60 | ### 登录使用
61 |
62 | // LoginPlatform.WEIBO 微博登录
63 | // LoginPlatform.WX 微信登录
64 | // LoginPlatform.QQ QQ登录
65 | final LoginListener listener = new LoginListener() {
66 | @Override
67 | public void loginSuccess(LoginResult result) {
68 | //登录成功, 如果你选择了获取用户信息,可以通过
69 | }
70 |
71 | @Override
72 | public void loginFailure(Exception e) {
73 | Log.i("TAG", "登录失败");
74 | }
75 |
76 | @Override
77 | public void loginCancel() {
78 | Log.i("TAG", "登录取消");
79 | }
80 | };
81 | LoginUtil.login(MainActivity.this, LoginPlatform.WEIBO, mLoginListener, isFetchUserInfo);
82 |
83 |
84 | ## 使用说明
85 |
86 | 1. QQ不支持纯文字分享,会直接分享失败
87 | 2. 使用Jar文件的版本如下:
88 |
89 | 微信版本:3.1.1
90 | QQ版本:3.1.0 lite版
91 | 微博版本: 3.1.4
92 | 3. 分享的bitmap,会在分享之后被回收掉,所以分享之后最好不要再对该bitmap做任何操作。
93 | 4. example 中的代码可以参考,但是不可运行,因为需要保证包名以及签名文件和你申请各个平台id所填写信息保持一致
94 | 5. ShareListener的回调结果仅供参考,不可当做分享是否返回的依据,它并不是那么完全可靠,因为某些操作,例如微博分享取消,但是用户选择了保存草稿,这时候客户端并不会收到回调,所以也就不会调用ShareListener的onCancel
95 |
96 | ## ChangeLog
97 |
98 | #### 1.3.8
99 | - 修复在低端设备上,可能导致的 `NullPointException` 问题
100 |
101 | #### 1.3.7
102 | - 修复微信分享大图没反应bug
103 | - 修复在5.0以下设备使用登录功能会意外收到登录取消回调
104 |
105 | #### 1.3.6
106 | - `ShareUtil`新增`isInstalled`方法,方便检查客户端是否安装
107 |
108 | #### 1.3.5
109 | - 修复QQ分享media类型错误
110 | - 完善log
111 | - 开放Application Context调用
112 | - 增加检查外部存储
113 | - 重构部分代码
114 |
115 | #### 1.3.3
116 | - 增加Debug模式
117 |
118 | #### 1.3.2
119 | - 修复若干分享相关的bug
120 |
121 | #### 1.3.1
122 | - 修复微博分享的bug
123 |
124 | #### 1.3.0
125 | - 重构使用方式,minSdkVersion - > 9
126 |
127 | #### 1.2.8
128 | - 增加分享失败Exception
129 |
130 | #### 1.2.7
131 | - 解决内存泄露的问题
132 |
133 | ## TODO
134 |
135 | 1. 微信内存泄露
136 | 2. 图片加载失败,关闭Activity
137 |
138 | ## Thanks
139 |
140 | - https://github.com/tianzhijiexian/ShareLoginLib
141 |
142 | ## License
143 |
144 | Copyright 2016 shaohui10086
145 |
146 | Licensed under the Apache License, Version 2.0 (the "License");
147 | you may not use this file except in compliance with the License.
148 | You may obtain a copy of the License at
149 |
150 | http://www.apache.org/licenses/LICENSE-2.0
151 |
152 | Unless required by applicable law or agreed to in writing, software
153 | distributed under the License is distributed on an "AS IS" BASIS,
154 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
155 | See the License for the specific language governing permissions and
156 | limitations under the License.
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/share/instance/DefaultShareInstance.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.share.instance;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.pm.PackageManager;
7 | import android.net.Uri;
8 | import java.io.File;
9 | import me.shaohui.shareutil.R;
10 | import me.shaohui.shareutil.share.ImageDecoder;
11 | import me.shaohui.shareutil.share.ShareImageObject;
12 | import me.shaohui.shareutil.share.ShareListener;
13 | import rx.Emitter;
14 | import rx.Observable;
15 | import rx.android.schedulers.AndroidSchedulers;
16 | import rx.functions.Action1;
17 | import rx.schedulers.Schedulers;
18 |
19 | /**
20 | * Created by shaohui on 2016/11/18.
21 | */
22 |
23 | public class DefaultShareInstance implements ShareInstance {
24 |
25 | @Override
26 | public void shareText(int platform, String text, Activity activity, ShareListener listener) {
27 | Intent sendIntent = new Intent();
28 | sendIntent.setAction(Intent.ACTION_SEND);
29 | sendIntent.putExtra(Intent.EXTRA_TEXT, text);
30 | sendIntent.setType("text/plain");
31 | activity.startActivity(Intent.createChooser(sendIntent,
32 | activity.getResources().getString(R.string.vista_share_title)));
33 | }
34 |
35 | @Override
36 | public void shareMedia(int platform, String title, String targetUrl, String summary,
37 | ShareImageObject shareImageObject, Activity activity, ShareListener listener) {
38 | Intent sendIntent = new Intent();
39 | sendIntent.setAction(Intent.ACTION_SEND);
40 | sendIntent.putExtra(Intent.EXTRA_TEXT, String.format("%s %s", title, targetUrl));
41 | sendIntent.setType("text/plain");
42 | activity.startActivity(Intent.createChooser(sendIntent,
43 | activity.getResources().getString(R.string.vista_share_title)));
44 | }
45 |
46 | @Override
47 | public void shareImage(int platform, final ShareImageObject shareImageObject,
48 | final Activity activity, final ShareListener listener) {
49 | Observable.fromEmitter(new Action1>() {
50 | @Override
51 | public void call(Emitter emitter) {
52 | try {
53 | Uri uri =
54 | Uri.fromFile(new File(ImageDecoder.decode(activity, shareImageObject)));
55 | emitter.onNext(uri);
56 | emitter.onCompleted();
57 | } catch (Exception e) {
58 | emitter.onError(e);
59 | }
60 | }
61 | }, Emitter.BackpressureMode.BUFFER)
62 | .subscribeOn(Schedulers.io())
63 | .observeOn(AndroidSchedulers.mainThread())
64 | .doOnRequest(new Action1() {
65 | @Override
66 | public void call(Long aLong) {
67 | listener.shareRequest();
68 | }
69 | })
70 | .subscribe(new Action1() {
71 | @Override
72 | public void call(Uri uri) {
73 | Intent shareIntent = new Intent();
74 | shareIntent.setAction(Intent.ACTION_SEND);
75 | shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
76 | shareIntent.setType("image/jpeg");
77 | activity.startActivity(Intent.createChooser(shareIntent,
78 | activity.getResources().getText(R.string.vista_share_title)));
79 | }
80 | }, new Action1() {
81 | @Override
82 | public void call(Throwable throwable) {
83 | listener.shareFailure(new Exception(throwable));
84 | }
85 | });
86 | }
87 |
88 | @Override
89 | public void handleResult(Intent data) {
90 | // Default share, do nothing
91 | }
92 |
93 | @Override
94 | public boolean isInstall(Context context) {
95 | Intent shareIntent = new Intent();
96 | shareIntent.setAction(Intent.ACTION_SEND);
97 | return context.getPackageManager()
98 | .resolveActivity(shareIntent, PackageManager.MATCH_DEFAULT_ONLY) != null;
99 | }
100 |
101 | @Override
102 | public void recycle() {
103 |
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/share/ImageDecoder.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.share;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.os.Environment;
7 | import android.text.TextUtils;
8 | import java.io.ByteArrayOutputStream;
9 | import java.io.File;
10 | import java.io.FileInputStream;
11 | import java.io.FileOutputStream;
12 | import java.io.IOException;
13 | import java.io.InputStream;
14 | import java.io.OutputStream;
15 | import me.shaohui.shareutil.ShareLogger;
16 | import okhttp3.HttpUrl;
17 | import okhttp3.OkHttpClient;
18 | import okhttp3.Request;
19 | import okhttp3.Response;
20 | import okio.BufferedSink;
21 | import okio.Okio;
22 |
23 | import static me.shaohui.shareutil.ShareLogger.INFO;
24 |
25 | /**
26 | * Created by shaohui on 2016/11/19.
27 | */
28 |
29 | public class ImageDecoder {
30 |
31 | private static final String FILE_NAME = "share_image.jpg";
32 |
33 | public static String decode(Context context, ShareImageObject imageObject) throws Exception {
34 | File resultFile = cacheFile(context);
35 |
36 | if (!TextUtils.isEmpty(imageObject.getPathOrUrl())) {
37 | return decode(context, imageObject.getPathOrUrl());
38 | } else if (imageObject.getBitmap() != null) {
39 | // save bitmap to file
40 | FileOutputStream outputStream = new FileOutputStream(resultFile);
41 | imageObject.getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
42 | outputStream.close();
43 | return resultFile.getAbsolutePath();
44 | } else {
45 | throw new IllegalArgumentException();
46 | }
47 | }
48 |
49 | private static String decode(Context context, String pathOrUrl) throws Exception {
50 | File resultFile = cacheFile(context);
51 |
52 | if (new File(pathOrUrl).exists()) {
53 | // copy file
54 | return decodeFile(new File(pathOrUrl), resultFile);
55 | } else if (HttpUrl.parse(pathOrUrl) != null) {
56 | // download image
57 | return downloadImageToUri(pathOrUrl, resultFile);
58 | } else {
59 | throw new IllegalArgumentException("Please input a file path or http url");
60 | }
61 | }
62 |
63 | private static String downloadImageToUri(String url, File resultFile) throws IOException {
64 | OkHttpClient client = new OkHttpClient();
65 | Request request = new Request.Builder().url(url).build();
66 | Response response = client.newCall(request).execute();
67 | BufferedSink sink = Okio.buffer(Okio.sink(resultFile));
68 | sink.writeAll(response.body().source());
69 |
70 | sink.close();
71 | response.close();
72 |
73 | return resultFile.getAbsolutePath();
74 | }
75 |
76 | private static File cacheFile(Context context) throws Exception {
77 | String state = Environment.getExternalStorageState();
78 | if (state != null && state.equals(Environment.MEDIA_MOUNTED)) {
79 | return new File(context.getExternalFilesDir(""), FILE_NAME);
80 | } else {
81 | throw new Exception(INFO.SD_CARD_NOT_AVAILABLE);
82 | }
83 | }
84 |
85 | private static void copyFile(InputStream inputStream, OutputStream outputStream)
86 | throws IOException {
87 | byte[] buffer = new byte[4096];
88 | while (-1 != inputStream.read(buffer)) {
89 | outputStream.write(buffer);
90 | }
91 |
92 | outputStream.flush();
93 | inputStream.close();
94 | outputStream.close();
95 | }
96 |
97 | private static String decodeFile(File origin, File result) throws IOException {
98 | copyFile(new FileInputStream(origin), new FileOutputStream(result, false));
99 | return result.getAbsolutePath();
100 | }
101 |
102 | public static byte[] compress2Byte(String imagePath, int size, int length) {
103 | BitmapFactory.Options options = new BitmapFactory.Options();
104 | options.inJustDecodeBounds = true;
105 | BitmapFactory.decodeFile(imagePath, options);
106 |
107 | int outH = options.outHeight;
108 | int outW = options.outWidth;
109 | int inSampleSize = 1;
110 |
111 | while (outH / inSampleSize > size || outW / inSampleSize > size) {
112 | inSampleSize *= 2;
113 | }
114 |
115 | options.inSampleSize = inSampleSize;
116 | options.inJustDecodeBounds = false;
117 |
118 | Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
119 |
120 | ByteArrayOutputStream result = new ByteArrayOutputStream();
121 | int quality = 100;
122 | bitmap.compress(Bitmap.CompressFormat.JPEG, quality, result);
123 | if (result.size() > length) {
124 | result.reset();
125 | quality -= 10;
126 | bitmap.compress(Bitmap.CompressFormat.JPEG, quality, result);
127 | }
128 |
129 | bitmap.recycle();
130 | return result.toByteArray();
131 | }
132 | }
133 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/instance/WeiboLoginInstance.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.instance;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.os.Bundle;
7 | import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
8 | import com.sina.weibo.sdk.api.share.WeiboShareSDK;
9 | import com.sina.weibo.sdk.auth.AuthInfo;
10 | import com.sina.weibo.sdk.auth.Oauth2AccessToken;
11 | import com.sina.weibo.sdk.auth.WeiboAuthListener;
12 | import com.sina.weibo.sdk.auth.sso.SsoHandler;
13 | import com.sina.weibo.sdk.exception.WeiboException;
14 | import java.io.IOException;
15 | import me.shaohui.shareutil.ShareLogger;
16 | import me.shaohui.shareutil.ShareManager;
17 | import me.shaohui.shareutil.login.LoginListener;
18 | import me.shaohui.shareutil.login.LoginPlatform;
19 | import me.shaohui.shareutil.login.LoginResult;
20 | import me.shaohui.shareutil.login.result.BaseToken;
21 | import me.shaohui.shareutil.login.result.WeiboToken;
22 | import me.shaohui.shareutil.login.result.WeiboUser;
23 | import okhttp3.OkHttpClient;
24 | import okhttp3.Request;
25 | import okhttp3.Response;
26 | import org.json.JSONException;
27 | import org.json.JSONObject;
28 | import rx.Emitter;
29 | import rx.Observable;
30 | import rx.android.schedulers.AndroidSchedulers;
31 | import rx.functions.Action1;
32 | import rx.schedulers.Schedulers;
33 |
34 | import static me.shaohui.shareutil.ShareLogger.INFO;
35 |
36 | /**
37 | * Created by shaohui on 2016/12/1.
38 | */
39 |
40 | public class WeiboLoginInstance extends LoginInstance {
41 |
42 | private static final String USER_INFO = "https://api.weibo.com/2/users/show.json";
43 |
44 | private SsoHandler mSsoHandler;
45 |
46 | private LoginListener mLoginListener;
47 |
48 | public WeiboLoginInstance(Activity activity, LoginListener listener, boolean fetchUserInfo) {
49 | super(activity, listener, fetchUserInfo);
50 | AuthInfo authInfo = new AuthInfo(activity, ShareManager.CONFIG.getWeiboId(),
51 | ShareManager.CONFIG.getWeiboRedirectUrl(), ShareManager.CONFIG.getWeiboScope());
52 | mSsoHandler = new SsoHandler(activity, authInfo);
53 | mLoginListener = listener;
54 | }
55 |
56 | @Override
57 | public void doLogin(Activity activity, final LoginListener listener,
58 | final boolean fetchUserInfo) {
59 | mSsoHandler.authorize(new WeiboAuthListener() {
60 | @Override
61 | public void onComplete(Bundle bundle) {
62 | Oauth2AccessToken accessToken = Oauth2AccessToken.parseAccessToken(bundle);
63 | WeiboToken weiboToken = WeiboToken.parse(accessToken);
64 | if (fetchUserInfo) {
65 | listener.beforeFetchUserInfo(weiboToken);
66 | fetchUserInfo(weiboToken);
67 | } else {
68 | listener.loginSuccess(new LoginResult(LoginPlatform.WEIBO, weiboToken));
69 | }
70 | }
71 |
72 | @Override
73 | public void onWeiboException(WeiboException e) {
74 | ShareLogger.i(INFO.WEIBO_AUTH_ERROR);
75 | listener.loginFailure(e);
76 | }
77 |
78 | @Override
79 | public void onCancel() {
80 | ShareLogger.i(INFO.AUTH_CANCEL);
81 | listener.loginCancel();
82 | }
83 | });
84 | }
85 |
86 | @Override
87 | public void fetchUserInfo(final BaseToken token) {
88 | Observable.fromEmitter(new Action1>() {
89 | @Override
90 | public void call(Emitter weiboUserEmitter) {
91 | OkHttpClient client = new OkHttpClient();
92 | Request request =
93 | new Request.Builder().url(buildUserInfoUrl(token, USER_INFO)).build();
94 | try {
95 | Response response = client.newCall(request).execute();
96 | JSONObject jsonObject = new JSONObject(response.body().string());
97 | WeiboUser user = WeiboUser.parse(jsonObject);
98 | weiboUserEmitter.onNext(user);
99 | } catch (IOException | JSONException e) {
100 | ShareLogger.e(INFO.FETCH_USER_INOF_ERROR);
101 | weiboUserEmitter.onError(e);
102 | }
103 | }
104 | }, Emitter.BackpressureMode.DROP)
105 | .subscribeOn(Schedulers.io())
106 | .observeOn(AndroidSchedulers.mainThread())
107 | .subscribe(new Action1() {
108 | @Override
109 | public void call(WeiboUser weiboUser) {
110 | mLoginListener.loginSuccess(
111 | new LoginResult(LoginPlatform.WEIBO, token, weiboUser));
112 | }
113 | }, new Action1() {
114 | @Override
115 | public void call(Throwable throwable) {
116 | mLoginListener.loginFailure(new Exception(throwable));
117 | }
118 | });
119 | }
120 |
121 | private String buildUserInfoUrl(BaseToken token, String baseUrl) {
122 | return baseUrl + "?access_token=" + token.getAccessToken() + "&uid=" + token.getOpenid();
123 | }
124 |
125 | @Override
126 | public void handleResult(int requestCode, int resultCode, Intent data) {
127 | mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
128 | }
129 |
130 | @Override
131 | public boolean isInstall(Context context) {
132 | IWeiboShareAPI shareAPI =
133 | WeiboShareSDK.createWeiboAPI(context, ShareManager.CONFIG.getWeiboId());
134 | return shareAPI.isWeiboAppInstalled();
135 | }
136 |
137 | @Override
138 | public void recycle() {
139 | mSsoHandler = null;
140 | mLoginListener = null;
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/instance/QQLoginInstance.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.instance;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.pm.PackageInfo;
7 | import android.content.pm.PackageManager;
8 | import android.text.TextUtils;
9 | import com.tencent.tauth.IUiListener;
10 | import com.tencent.tauth.Tencent;
11 | import com.tencent.tauth.UiError;
12 | import java.io.IOException;
13 | import java.util.List;
14 | import me.shaohui.shareutil.ShareLogger;
15 | import me.shaohui.shareutil.ShareManager;
16 | import me.shaohui.shareutil.login.LoginListener;
17 | import me.shaohui.shareutil.login.LoginPlatform;
18 | import me.shaohui.shareutil.login.LoginResult;
19 | import me.shaohui.shareutil.login.result.BaseToken;
20 | import me.shaohui.shareutil.login.result.QQToken;
21 | import me.shaohui.shareutil.login.result.QQUser;
22 | import okhttp3.OkHttpClient;
23 | import okhttp3.Request;
24 | import okhttp3.Response;
25 | import org.json.JSONException;
26 | import org.json.JSONObject;
27 | import rx.Emitter;
28 | import rx.Observable;
29 | import rx.android.schedulers.AndroidSchedulers;
30 | import rx.functions.Action1;
31 | import rx.schedulers.Schedulers;
32 |
33 | import static me.shaohui.shareutil.ShareLogger.INFO;
34 |
35 | /**
36 | * Created by shaohui on 2016/12/1.
37 | */
38 |
39 | public class QQLoginInstance extends LoginInstance {
40 |
41 | private static final String SCOPE = "get_simple_userinfo";
42 |
43 | private static final String URL = "https://graph.qq.com/user/get_user_info";
44 |
45 | private Tencent mTencent;
46 |
47 | private IUiListener mIUiListener;
48 |
49 | private LoginListener mLoginListener;
50 |
51 | public QQLoginInstance(Activity activity, final LoginListener listener,
52 | final boolean fetchUserInfo) {
53 | super(activity, listener, fetchUserInfo);
54 | mTencent = Tencent.createInstance(ShareManager.CONFIG.getQqId(),
55 | activity.getApplicationContext());
56 | mLoginListener = listener;
57 | mIUiListener = new IUiListener() {
58 | @Override
59 | public void onComplete(Object o) {
60 | ShareLogger.i(INFO.QQ_AUTH_SUCCESS);
61 | try {
62 | QQToken token = QQToken.parse((JSONObject) o);
63 | if (fetchUserInfo) {
64 | listener.beforeFetchUserInfo(token);
65 | fetchUserInfo(token);
66 | } else {
67 | listener.loginSuccess(new LoginResult(LoginPlatform.QQ, token));
68 | }
69 | } catch (JSONException e) {
70 | ShareLogger.i(INFO.ILLEGAL_TOKEN);
71 | mLoginListener.loginFailure(e);
72 | }
73 | }
74 |
75 | @Override
76 | public void onError(UiError uiError) {
77 | ShareLogger.i(INFO.QQ_LOGIN_ERROR);
78 | listener.loginFailure(
79 | new Exception("QQError: " + uiError.errorCode + uiError.errorDetail));
80 | }
81 |
82 | @Override
83 | public void onCancel() {
84 | ShareLogger.i(INFO.AUTH_CANCEL);
85 | listener.loginCancel();
86 | }
87 | };
88 | }
89 |
90 | @Override
91 | public void doLogin(Activity activity, final LoginListener listener, boolean fetchUserInfo) {
92 | mTencent.login(activity, SCOPE, mIUiListener);
93 | }
94 |
95 | @Override
96 | public void fetchUserInfo(final BaseToken token) {
97 | Observable.fromEmitter(new Action1>() {
98 | @Override
99 | public void call(Emitter qqUserEmitter) {
100 | OkHttpClient client = new OkHttpClient();
101 | Request request = new Request.Builder().url(buildUserInfoUrl(token, URL)).build();
102 |
103 | try {
104 | Response response = client.newCall(request).execute();
105 | JSONObject jsonObject = new JSONObject(response.body().string());
106 | QQUser user = QQUser.parse(token.getOpenid(), jsonObject);
107 | qqUserEmitter.onNext(user);
108 | } catch (IOException | JSONException e) {
109 | ShareLogger.e(INFO.FETCH_USER_INOF_ERROR);
110 | qqUserEmitter.onError(e);
111 | }
112 | }
113 | }, Emitter.BackpressureMode.DROP)
114 | .subscribeOn(Schedulers.io())
115 | .observeOn(AndroidSchedulers.mainThread())
116 | .subscribe(new Action1() {
117 | @Override
118 | public void call(QQUser qqUser) {
119 | mLoginListener.loginSuccess(
120 | new LoginResult(LoginPlatform.QQ, token, qqUser));
121 | }
122 | }, new Action1() {
123 | @Override
124 | public void call(Throwable throwable) {
125 | mLoginListener.loginFailure(new Exception(throwable));
126 | }
127 | });
128 | }
129 |
130 | private String buildUserInfoUrl(BaseToken token, String base) {
131 | return base
132 | + "?access_token="
133 | + token.getAccessToken()
134 | + "&oauth_consumer_key="
135 | + ShareManager.CONFIG.getQqId()
136 | + "&openid="
137 | + token.getOpenid();
138 | }
139 |
140 | @Override
141 | public void handleResult(int requestCode, int resultCode, Intent data) {
142 | Tencent.handleResultData(data, mIUiListener);
143 | }
144 |
145 | @Override
146 | public boolean isInstall(Context context) {
147 | PackageManager pm = context.getPackageManager();
148 | if (pm == null) {
149 | return false;
150 | }
151 |
152 | List packageInfos = pm.getInstalledPackages(0);
153 | for (PackageInfo info : packageInfos) {
154 | if (TextUtils.equals(info.packageName.toLowerCase(), "com.tencent.mobileqq")) {
155 | return true;
156 | }
157 | }
158 | return false;
159 | }
160 |
161 | @Override
162 | public void recycle() {
163 | mTencent.releaseResource();
164 | mIUiListener = null;
165 | mLoginListener = null;
166 | mTencent = null;
167 | }
168 | }
169 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/share/instance/WeiboShareInstance.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.share.instance;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.graphics.Bitmap;
7 | import android.media.Image;
8 | import android.text.TextUtils;
9 | import android.util.Pair;
10 | import com.sina.weibo.sdk.api.ImageObject;
11 | import com.sina.weibo.sdk.api.TextObject;
12 | import com.sina.weibo.sdk.api.WeiboMultiMessage;
13 | import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
14 | import com.sina.weibo.sdk.api.share.SendMessageToWeiboResponse;
15 | import com.sina.weibo.sdk.api.share.SendMultiMessageToWeiboRequest;
16 | import com.sina.weibo.sdk.api.share.WeiboShareSDK;
17 | import com.sina.weibo.sdk.constant.WBConstants;
18 | import me.shaohui.shareutil.ShareUtil;
19 | import me.shaohui.shareutil.share.ImageDecoder;
20 | import me.shaohui.shareutil.share.ShareImageObject;
21 | import me.shaohui.shareutil.share.ShareListener;
22 | import rx.Emitter;
23 | import rx.Observable;
24 | import rx.android.schedulers.AndroidSchedulers;
25 | import rx.functions.Action1;
26 | import rx.schedulers.Schedulers;
27 |
28 | /**
29 | * Created by shaohui on 2016/11/18.
30 | */
31 |
32 | public class WeiboShareInstance implements ShareInstance {
33 | /**
34 | * 微博分享限制thumb image必须小于2097152,否则点击分享会没有反应
35 | */
36 |
37 | private IWeiboShareAPI mWeiboShareAPI;
38 |
39 | private static final int TARGET_SIZE = 1024;
40 |
41 | private static final int TARGET_LENGTH = 2097152;
42 |
43 | public WeiboShareInstance(Context context, String appId) {
44 | mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(context, appId);
45 | mWeiboShareAPI.registerApp();
46 | }
47 |
48 | @Override
49 | public void shareText(int platform, String text, Activity activity, ShareListener listener) {
50 | TextObject textObject = new TextObject();
51 | textObject.text = text;
52 | WeiboMultiMessage message = new WeiboMultiMessage();
53 | message.textObject = textObject;
54 |
55 | sendRequest(activity, message);
56 | }
57 |
58 | @Override
59 | public void shareMedia(int platform, final String title, final String targetUrl, String summary,
60 | ShareImageObject shareImageObject, final Activity activity,
61 | final ShareListener listener) {
62 | String content = String.format("%s %s", title, targetUrl);
63 | shareTextOrImage(shareImageObject, content, activity, listener);
64 | }
65 |
66 | @Override
67 | public void shareImage(int platform, ShareImageObject shareImageObject, Activity activity,
68 | ShareListener listener) {
69 | shareTextOrImage(shareImageObject, null, activity, listener);
70 | }
71 |
72 | @Override
73 | public void handleResult(Intent intent) {
74 | SendMessageToWeiboResponse baseResponse =
75 | new SendMessageToWeiboResponse(intent.getExtras());
76 |
77 | switch (baseResponse.errCode) {
78 | case WBConstants.ErrorCode.ERR_OK:
79 | ShareUtil.mShareListener.shareSuccess();
80 | break;
81 | case WBConstants.ErrorCode.ERR_FAIL:
82 | ShareUtil.mShareListener.shareFailure(new Exception(baseResponse.errMsg));
83 | break;
84 | case WBConstants.ErrorCode.ERR_CANCEL:
85 | ShareUtil.mShareListener.shareCancel();
86 | break;
87 | default:
88 | ShareUtil.mShareListener.shareFailure(new Exception(baseResponse.errMsg));
89 | }
90 | }
91 |
92 | @Override
93 | public boolean isInstall(Context context) {
94 | return mWeiboShareAPI.isWeiboAppInstalled();
95 | }
96 |
97 | @Override
98 | public void recycle() {
99 | mWeiboShareAPI = null;
100 | }
101 |
102 | private void shareTextOrImage(final ShareImageObject shareImageObject, final String text,
103 | final Activity activity, final ShareListener listener) {
104 |
105 | Observable.fromEmitter(new Action1>>() {
106 | @Override
107 | public void call(Emitter> emitter) {
108 | try {
109 | String path = ImageDecoder.decode(activity, shareImageObject);
110 | emitter.onNext(Pair.create(path,
111 | ImageDecoder.compress2Byte(path, TARGET_SIZE, TARGET_LENGTH)));
112 | emitter.onCompleted();
113 | } catch (Exception e) {
114 | emitter.onError(e);
115 | }
116 | }
117 | }, Emitter.BackpressureMode.DROP)
118 | .subscribeOn(Schedulers.io())
119 | .observeOn(AndroidSchedulers.mainThread())
120 | .doOnRequest(new Action1() {
121 | @Override
122 | public void call(Long aLong) {
123 | listener.shareRequest();
124 | }
125 | })
126 | .subscribe(new Action1>() {
127 | @Override
128 | public void call(Pair pair) {
129 | ImageObject imageObject = new ImageObject();
130 | imageObject.imageData = pair.second;
131 | imageObject.imagePath = pair.first;
132 |
133 | WeiboMultiMessage message = new WeiboMultiMessage();
134 | message.imageObject = imageObject;
135 | if (!TextUtils.isEmpty(text)) {
136 | TextObject textObject = new TextObject();
137 | textObject.text = text;
138 |
139 | message.textObject = textObject;
140 | }
141 |
142 | sendRequest(activity, message);
143 | }
144 | }, new Action1() {
145 | @Override
146 | public void call(Throwable throwable) {
147 | activity.finish();
148 | listener.shareFailure(new Exception(throwable));
149 | }
150 | });
151 | }
152 |
153 | private void sendRequest(Activity activity, WeiboMultiMessage message) {
154 | SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest();
155 | request.transaction = String.valueOf(System.currentTimeMillis());
156 | request.multiMessage = message;
157 | mWeiboShareAPI.sendRequest(activity, request);
158 | }
159 | }
160 |
--------------------------------------------------------------------------------
/example/src/main/res/layout/layout_bottom_share.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
24 |
25 |
30 |
31 |
37 |
38 |
45 |
46 |
56 |
57 |
58 |
64 |
65 |
72 |
73 |
83 |
84 |
85 |
91 |
92 |
99 |
100 |
110 |
111 |
112 |
118 |
119 |
126 |
127 |
137 |
138 |
139 |
145 |
146 |
153 |
154 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/share/instance/WxShareInstance.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.share.instance;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.graphics.Bitmap;
7 | import android.graphics.BitmapFactory;
8 | import android.media.Image;
9 | import android.text.TextUtils;
10 | import android.util.Log;
11 | import android.util.Pair;
12 | import com.tencent.mm.sdk.modelbase.BaseReq;
13 | import com.tencent.mm.sdk.modelbase.BaseResp;
14 | import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
15 | import com.tencent.mm.sdk.modelmsg.WXImageObject;
16 | import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
17 | import com.tencent.mm.sdk.modelmsg.WXTextObject;
18 | import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
19 | import com.tencent.mm.sdk.openapi.IWXAPI;
20 | import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
21 | import com.tencent.mm.sdk.openapi.WXAPIFactory;
22 | import me.shaohui.shareutil.ShareUtil;
23 | import me.shaohui.shareutil.share.ImageDecoder;
24 | import me.shaohui.shareutil.share.ShareImageObject;
25 | import me.shaohui.shareutil.share.ShareListener;
26 | import me.shaohui.shareutil.share.SharePlatform;
27 | import rx.Emitter;
28 | import rx.Observable;
29 | import rx.android.schedulers.AndroidSchedulers;
30 | import rx.functions.Action1;
31 | import rx.schedulers.Schedulers;
32 |
33 | /**
34 | * Created by shaohui on 2016/11/18.
35 | */
36 |
37 | public class WxShareInstance implements ShareInstance {
38 |
39 | /**
40 | * 微信分享限制thumb image必须小于32Kb,否则点击分享会没有反应
41 | */
42 |
43 | private IWXAPI mIWXAPI;
44 |
45 | private static final int THUMB_SIZE = 32 * 1024 * 8;
46 |
47 | private static final int TARGET_SIZE = 200;
48 |
49 | public WxShareInstance(Context context, String appId) {
50 | mIWXAPI = WXAPIFactory.createWXAPI(context, appId, true);
51 | mIWXAPI.registerApp(appId);
52 | }
53 |
54 | @Override
55 | public void shareText(int platform, String text, Activity activity, ShareListener listener) {
56 | WXTextObject textObject = new WXTextObject();
57 | textObject.text = text;
58 |
59 | WXMediaMessage message = new WXMediaMessage();
60 | message.mediaObject = textObject;
61 | message.description = text;
62 |
63 | sendMessage(platform, message, buildTransaction("text"));
64 | }
65 |
66 | @Override
67 | public void shareMedia(
68 | final int platform, final String title, final String targetUrl, final String summary,
69 | final ShareImageObject shareImageObject, final Activity activity, final ShareListener listener) {
70 | Observable.fromEmitter(new Action1>() {
71 |
72 | @Override
73 | public void call(Emitter emitter) {
74 | try {
75 | String imagePath = ImageDecoder.decode(activity, shareImageObject);
76 | emitter.onNext(ImageDecoder.compress2Byte(imagePath, TARGET_SIZE, THUMB_SIZE));
77 | } catch (Exception e) {
78 | emitter.onError(e);
79 | }
80 | }
81 | }, Emitter.BackpressureMode.DROP)
82 | .subscribeOn(Schedulers.io())
83 | .observeOn(AndroidSchedulers.mainThread())
84 | .doOnRequest(new Action1() {
85 | @Override
86 | public void call(Long aLong) {
87 | listener.shareRequest();
88 | }
89 | })
90 | .subscribe(new Action1() {
91 | @Override
92 | public void call(byte[] bytes) {
93 | WXWebpageObject webpageObject = new WXWebpageObject();
94 | webpageObject.webpageUrl = targetUrl;
95 |
96 | WXMediaMessage message = new WXMediaMessage(webpageObject);
97 | message.title = title;
98 | message.description = summary;
99 | message.thumbData = bytes;
100 |
101 | sendMessage(platform, message, buildTransaction("webPage"));
102 | }
103 | }, new Action1() {
104 | @Override
105 | public void call(Throwable throwable) {
106 | activity.finish();
107 | listener.shareFailure(new Exception(throwable));
108 | }
109 | });
110 | }
111 |
112 | @Override
113 | public void shareImage(final int platform, final ShareImageObject shareImageObject,
114 | final Activity activity, final ShareListener listener) {
115 | Observable.fromEmitter(new Action1>>() {
116 | @Override
117 | public void call(Emitter> emitter) {
118 | try {
119 | String imagePath = ImageDecoder.decode(activity, shareImageObject);
120 | emitter.onNext(Pair.create(BitmapFactory.decodeFile(imagePath),
121 | ImageDecoder.compress2Byte(imagePath, TARGET_SIZE, THUMB_SIZE)));
122 | } catch (Exception e) {
123 | emitter.onError(e);
124 | }
125 | }
126 | }, Emitter.BackpressureMode.BUFFER)
127 | .subscribeOn(Schedulers.io())
128 | .observeOn(AndroidSchedulers.mainThread())
129 | .doOnRequest(new Action1() {
130 | @Override
131 | public void call(Long aLong) {
132 | listener.shareRequest();
133 | }
134 | })
135 | .subscribe(new Action1>() {
136 | @Override
137 | public void call(Pair pair) {
138 | WXImageObject imageObject = new WXImageObject(pair.first);
139 |
140 | WXMediaMessage message = new WXMediaMessage();
141 | message.mediaObject = imageObject;
142 | message.thumbData = pair.second;
143 |
144 | sendMessage(platform, message, buildTransaction("image"));
145 | }
146 | }, new Action1() {
147 | @Override
148 | public void call(Throwable throwable) {
149 | activity.finish();
150 | listener.shareFailure(new Exception(throwable));
151 | }
152 | });
153 | }
154 |
155 | @Override
156 | public void handleResult(Intent data) {
157 | mIWXAPI.handleIntent(data, new IWXAPIEventHandler() {
158 | @Override
159 | public void onReq(BaseReq baseReq) {
160 | }
161 |
162 | @Override
163 | public void onResp(BaseResp baseResp) {
164 | switch (baseResp.errCode) {
165 | case BaseResp.ErrCode.ERR_OK:
166 | ShareUtil.mShareListener.shareSuccess();
167 | break;
168 | case BaseResp.ErrCode.ERR_USER_CANCEL:
169 | ShareUtil.mShareListener.shareCancel();
170 | break;
171 | default:
172 | ShareUtil.mShareListener.shareFailure(new Exception(baseResp.errStr));
173 | }
174 | }
175 | });
176 | }
177 |
178 | @Override
179 | public boolean isInstall(Context context) {
180 | return mIWXAPI.isWXAppInstalled();
181 | }
182 |
183 | @Override
184 | public void recycle() {
185 | mIWXAPI.detach();
186 | }
187 |
188 | private void sendMessage(int platform, WXMediaMessage message, String transaction) {
189 | SendMessageToWX.Req req = new SendMessageToWX.Req();
190 | req.transaction = transaction;
191 | req.message = message;
192 | req.scene = platform == SharePlatform.WX_TIMELINE ? SendMessageToWX.Req.WXSceneTimeline
193 | : SendMessageToWX.Req.WXSceneSession;
194 | mIWXAPI.sendReq(req);
195 | }
196 |
197 | private String buildTransaction(String type) {
198 | return System.currentTimeMillis() + type;
199 | }
200 |
201 | }
202 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/login/instance/WxLoginInstance.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.login.instance;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import com.tencent.mm.sdk.modelbase.BaseReq;
7 | import com.tencent.mm.sdk.modelbase.BaseResp;
8 | import com.tencent.mm.sdk.modelmsg.SendAuth;
9 | import com.tencent.mm.sdk.openapi.IWXAPI;
10 | import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
11 | import com.tencent.mm.sdk.openapi.WXAPIFactory;
12 | import java.io.IOException;
13 | import me.shaohui.shareutil.ShareManager;
14 | import me.shaohui.shareutil.login.LoginListener;
15 | import me.shaohui.shareutil.login.LoginPlatform;
16 | import me.shaohui.shareutil.login.LoginResult;
17 | import me.shaohui.shareutil.login.result.BaseToken;
18 | import me.shaohui.shareutil.login.result.WxToken;
19 | import me.shaohui.shareutil.login.result.WxUser;
20 | import okhttp3.OkHttpClient;
21 | import okhttp3.Request;
22 | import okhttp3.Response;
23 | import org.json.JSONException;
24 | import org.json.JSONObject;
25 | import rx.Emitter;
26 | import rx.Observable;
27 | import rx.android.schedulers.AndroidSchedulers;
28 | import rx.functions.Action1;
29 | import rx.schedulers.Schedulers;
30 |
31 | import static me.shaohui.shareutil.ShareLogger.INFO;
32 |
33 | /**
34 | * Created by shaohui on 2016/12/1.
35 | */
36 |
37 | public class WxLoginInstance extends LoginInstance {
38 |
39 | public static final String SCOPE_USER_INFO = "snsapi_userinfo";
40 | private static final String SCOPE_BASE = "snsapi_base";
41 |
42 | private static final String BASE_URL = "https://api.weixin.qq.com/sns/";
43 |
44 | private IWXAPI mIWXAPI;
45 |
46 | private LoginListener mLoginListener;
47 |
48 | private OkHttpClient mClient;
49 |
50 | private boolean fetchUserInfo;
51 |
52 | public WxLoginInstance(Activity activity, LoginListener listener, boolean fetchUserInfo) {
53 | super(activity, listener, fetchUserInfo);
54 | mLoginListener = listener;
55 | mIWXAPI = WXAPIFactory.createWXAPI(activity, ShareManager.CONFIG.getWxId());
56 | mClient = new OkHttpClient();
57 | this.fetchUserInfo = fetchUserInfo;
58 | }
59 |
60 | @Override
61 | public void doLogin(Activity activity, LoginListener listener, boolean fetchUserInfo) {
62 | final SendAuth.Req req = new SendAuth.Req();
63 | req.scope = SCOPE_USER_INFO;
64 | req.state = String.valueOf(System.currentTimeMillis());
65 | mIWXAPI.sendReq(req);
66 | }
67 |
68 | private void getToken(final String code) {
69 | Observable.fromEmitter(new Action1>() {
70 | @Override
71 | public void call(Emitter wxTokenEmitter) {
72 | Request request = new Request.Builder().url(buildTokenUrl(code)).build();
73 | try {
74 | Response response = mClient.newCall(request).execute();
75 | JSONObject jsonObject = new JSONObject(response.body().string());
76 | WxToken token = WxToken.parse(jsonObject);
77 | wxTokenEmitter.onNext(token);
78 | } catch (IOException | JSONException e) {
79 | wxTokenEmitter.onError(e);
80 | }
81 | }
82 | }, Emitter.BackpressureMode.DROP)
83 | .subscribeOn(Schedulers.io())
84 | .observeOn(AndroidSchedulers.mainThread())
85 | .subscribe(new Action1() {
86 | @Override
87 | public void call(WxToken wxToken) {
88 | if (fetchUserInfo) {
89 | mLoginListener.beforeFetchUserInfo(wxToken);
90 | fetchUserInfo(wxToken);
91 | } else {
92 | mLoginListener.loginSuccess(new LoginResult(LoginPlatform.WX, wxToken));
93 | }
94 | }
95 | }, new Action1() {
96 | @Override
97 | public void call(Throwable throwable) {
98 | mLoginListener.loginFailure(new Exception(throwable.getMessage()));
99 | }
100 | });
101 | }
102 |
103 | @Override
104 | public void fetchUserInfo(final BaseToken token) {
105 | Observable.fromEmitter(new Action1>() {
106 | @Override
107 | public void call(Emitter wxUserEmitter) {
108 | Request request = new Request.Builder().url(buildUserInfoUrl(token)).build();
109 | try {
110 | Response response = mClient.newCall(request).execute();
111 | JSONObject jsonObject = new JSONObject(response.body().string());
112 | WxUser user = WxUser.parse(jsonObject);
113 | wxUserEmitter.onNext(user);
114 | } catch (IOException | JSONException e) {
115 | wxUserEmitter.onError(e);
116 | }
117 | }
118 | }, Emitter.BackpressureMode.DROP)
119 | .subscribeOn(Schedulers.io())
120 | .observeOn(AndroidSchedulers.mainThread())
121 | .subscribe(new Action1() {
122 | @Override
123 | public void call(WxUser wxUser) {
124 | mLoginListener.loginSuccess(
125 | new LoginResult(LoginPlatform.WX, token, wxUser));
126 | }
127 | }, new Action1() {
128 | @Override
129 | public void call(Throwable throwable) {
130 | mLoginListener.loginFailure(new Exception(throwable));
131 | }
132 | });
133 | }
134 |
135 | @Override
136 | public void handleResult(int requestCode, int resultCode, Intent data) {
137 | mIWXAPI.handleIntent(data, new IWXAPIEventHandler() {
138 | @Override
139 | public void onReq(BaseReq baseReq) {
140 | }
141 |
142 | @Override
143 | public void onResp(BaseResp baseResp) {
144 | if (baseResp instanceof SendAuth.Resp && baseResp.getType() == 1) {
145 | SendAuth.Resp resp = (SendAuth.Resp) baseResp;
146 | switch (resp.errCode) {
147 | case BaseResp.ErrCode.ERR_OK:
148 | getToken(resp.code);
149 | break;
150 | case BaseResp.ErrCode.ERR_USER_CANCEL:
151 | mLoginListener.loginCancel();
152 | break;
153 | case BaseResp.ErrCode.ERR_SENT_FAILED:
154 | mLoginListener.loginFailure(new Exception(INFO.WX_ERR_SENT_FAILED));
155 | break;
156 | case BaseResp.ErrCode.ERR_UNSUPPORT:
157 | mLoginListener.loginFailure(new Exception(INFO.WX_ERR_UNSUPPORT));
158 | break;
159 | case BaseResp.ErrCode.ERR_AUTH_DENIED:
160 | mLoginListener.loginFailure(new Exception(INFO.WX_ERR_AUTH_DENIED));
161 | break;
162 | default:
163 | mLoginListener.loginFailure(new Exception(INFO.WX_ERR_AUTH_ERROR));
164 | }
165 | }
166 | }
167 | });
168 | }
169 |
170 | @Override
171 | public boolean isInstall(Context context) {
172 | return mIWXAPI.isWXAppInstalled();
173 | }
174 |
175 | @Override
176 | public void recycle() {
177 | if (mIWXAPI != null) {
178 | mIWXAPI.detach();
179 | }
180 | }
181 |
182 | private String buildTokenUrl(String code) {
183 | return BASE_URL
184 | + "oauth2/access_token?appid="
185 | + ShareManager.CONFIG.getWxId()
186 | + "&secret="
187 | + ShareManager.CONFIG.getWxSecret()
188 | + "&code="
189 | + code
190 | + "&grant_type=authorization_code";
191 | }
192 |
193 | private String buildUserInfoUrl(BaseToken token) {
194 | return BASE_URL
195 | + "userinfo?access_token="
196 | + token.getAccessToken()
197 | + "&openid="
198 | + token.getOpenid();
199 | }
200 | }
201 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/share/instance/QQShareInstance.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil.share.instance;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.pm.PackageInfo;
7 | import android.content.pm.PackageManager;
8 | import android.os.Bundle;
9 | import android.text.TextUtils;
10 | import com.tencent.connect.share.QQShare;
11 | import com.tencent.connect.share.QzonePublish;
12 | import com.tencent.connect.share.QzoneShare;
13 | import com.tencent.tauth.Tencent;
14 | import java.util.ArrayList;
15 | import java.util.List;
16 | import java.util.concurrent.Callable;
17 | import me.shaohui.shareutil.ShareUtil;
18 | import me.shaohui.shareutil.share.ImageDecoder;
19 | import me.shaohui.shareutil.share.ShareImageObject;
20 | import me.shaohui.shareutil.share.ShareListener;
21 | import me.shaohui.shareutil.share.SharePlatform;
22 | import rx.Emitter;
23 | import rx.Observable;
24 | import rx.android.schedulers.AndroidSchedulers;
25 | import rx.functions.Action0;
26 | import rx.functions.Action1;
27 | import rx.schedulers.Schedulers;
28 |
29 | import static me.shaohui.shareutil.ShareLogger.INFO;
30 |
31 | /**
32 | * Created by shaohui on 2016/11/18.
33 | */
34 |
35 | public class QQShareInstance implements ShareInstance {
36 |
37 | private Tencent mTencent;
38 |
39 | public QQShareInstance(Context context, String app_id) {
40 | mTencent = Tencent.createInstance(app_id, context.getApplicationContext());
41 | }
42 |
43 | @Override
44 | public void shareText(int platform, String text, Activity activity, ShareListener listener) {
45 | if (platform == SharePlatform.QZONE) {
46 | shareToQZoneForText(text, activity, listener);
47 | } else {
48 | activity.finish();
49 | listener.shareFailure(new Exception(INFO.QQ_NOT_SUPPORT_SHARE_TXT));
50 | }
51 | }
52 |
53 | @Override
54 | public void shareMedia(final int platform, final String title, final String targetUrl,
55 | final String summary, final ShareImageObject shareImageObject, final Activity activity,
56 | final ShareListener listener) {
57 | Observable.fromEmitter(new Action1>() {
58 | @Override
59 | public void call(Emitter emitter) {
60 | try {
61 | emitter.onNext(ImageDecoder.decode(activity, shareImageObject));
62 | emitter.onCompleted();
63 | } catch (Exception e) {
64 | emitter.onError(e);
65 | }
66 | }
67 | }, Emitter.BackpressureMode.DROP)
68 | .subscribeOn(Schedulers.io())
69 | .observeOn(AndroidSchedulers.mainThread())
70 | .doOnRequest(new Action1() {
71 | @Override
72 | public void call(Long aLong) {
73 | listener.shareRequest();
74 | }
75 | })
76 | .subscribe(new Action1() {
77 | @Override
78 | public void call(String s) {
79 | if (platform == SharePlatform.QZONE) {
80 | shareToQZoneForMedia(title, targetUrl, summary, s, activity,
81 | listener);
82 | } else {
83 | shareToQQForMedia(title, summary, targetUrl, s, activity, listener);
84 | }
85 | }
86 | }, new Action1() {
87 | @Override
88 | public void call(Throwable throwable) {
89 | activity.finish();
90 | listener.shareFailure(new Exception(throwable));
91 | }
92 | });
93 | }
94 |
95 | @Override
96 | public void shareImage(final int platform, final ShareImageObject shareImageObject,
97 | final Activity activity, final ShareListener listener) {
98 | Observable.fromEmitter(new Action1>() {
99 | @Override
100 | public void call(Emitter emitter) {
101 | try {
102 | emitter.onNext(ImageDecoder.decode(activity, shareImageObject));
103 | emitter.onCompleted();
104 | } catch (Exception e) {
105 | emitter.onError(e);
106 | }
107 | }
108 | }, Emitter.BackpressureMode.DROP)
109 | .subscribeOn(Schedulers.io())
110 | .observeOn(AndroidSchedulers.mainThread())
111 | .doOnRequest(new Action1() {
112 | @Override
113 | public void call(Long aLong) {
114 | listener.shareRequest();
115 | }
116 | })
117 | .subscribe(new Action1() {
118 | @Override
119 | public void call(String localPath) {
120 | if (platform == SharePlatform.QZONE) {
121 | shareToQzoneForImage(localPath, activity, listener);
122 | } else {
123 | shareToQQForImage(localPath, activity, listener);
124 | }
125 | }
126 | }, new Action1() {
127 | @Override
128 | public void call(Throwable throwable) {
129 | activity.finish();
130 | listener.shareFailure(new Exception(throwable));
131 | }
132 | });
133 | }
134 |
135 | @Override
136 | public void handleResult(Intent data) {
137 | Tencent.handleResultData(data, ShareUtil.mShareListener);
138 | }
139 |
140 | @Override
141 | public boolean isInstall(Context context) {
142 | PackageManager pm = context.getPackageManager();
143 | if (pm == null) {
144 | return false;
145 | }
146 |
147 | List packageInfos = pm.getInstalledPackages(0);
148 | for (PackageInfo info : packageInfos) {
149 | if (TextUtils.equals(info.packageName.toLowerCase(), "com.tencent.mobileqq")) {
150 | return true;
151 | }
152 | }
153 | return false;
154 | }
155 |
156 | @Override
157 | public void recycle() {
158 | if (mTencent != null) {
159 | mTencent.releaseResource();
160 | mTencent = null;
161 | }
162 | }
163 |
164 | private void shareToQQForMedia(String title, String summary, String targetUrl, String thumbUrl,
165 | Activity activity, ShareListener listener) {
166 | final Bundle params = new Bundle();
167 | params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
168 | params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
169 | params.putString(QQShare.SHARE_TO_QQ_SUMMARY, summary);
170 | params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, targetUrl);
171 | params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, thumbUrl);
172 | mTencent.shareToQQ(activity, params, listener);
173 | }
174 |
175 | private void shareToQQForImage(String localUrl, Activity activity, ShareListener listener) {
176 | Bundle params = new Bundle();
177 | params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
178 | params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, localUrl);
179 | mTencent.shareToQQ(activity, params, listener);
180 | }
181 |
182 | private void shareToQZoneForText(String text, Activity activity, ShareListener listener) {
183 | final Bundle params = new Bundle();
184 | params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,
185 | QzonePublish.PUBLISH_TO_QZONE_TYPE_PUBLISHMOOD);
186 | params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, text);
187 | mTencent.publishToQzone(activity, params, listener);
188 | }
189 |
190 | private void shareToQZoneForMedia(String title, String targetUrl, String summary,
191 | String imageUrl, Activity activity, ShareListener listener) {
192 | final Bundle params = new Bundle();
193 | final ArrayList image = new ArrayList<>();
194 | image.add(imageUrl);
195 | params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,
196 | QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
197 | params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);
198 | params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, summary);
199 | params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, targetUrl);
200 | params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, image);
201 | mTencent.shareToQzone(activity, params, listener);
202 | }
203 |
204 | private void shareToQzoneForImage(String imagePath, Activity activity, ShareListener listener) {
205 | final Bundle params = new Bundle();
206 | final ArrayList image = new ArrayList<>();
207 | image.add(imagePath);
208 | params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,
209 | QzonePublish.PUBLISH_TO_QZONE_TYPE_PUBLISHMOOD);
210 | params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, image);
211 | mTencent.publishToQzone(activity, params, listener);
212 | }
213 | }
214 |
--------------------------------------------------------------------------------
/shareutil/src/main/java/me/shaohui/shareutil/ShareUtil.java:
--------------------------------------------------------------------------------
1 | package me.shaohui.shareutil;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.pm.PackageInfo;
7 | import android.content.pm.PackageManager;
8 | import android.graphics.Bitmap;
9 | import android.support.annotation.NonNull;
10 | import android.text.TextUtils;
11 | import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
12 | import com.sina.weibo.sdk.api.share.WeiboShareSDK;
13 | import com.tencent.mm.sdk.openapi.IWXAPI;
14 | import com.tencent.mm.sdk.openapi.WXAPIFactory;
15 | import java.util.List;
16 | import java.util.Locale;
17 |
18 | import me.shaohui.shareutil.login.LoginPlatform;
19 | import me.shaohui.shareutil.share.ShareImageObject;
20 | import me.shaohui.shareutil.share.ShareListener;
21 | import me.shaohui.shareutil.share.SharePlatform;
22 | import me.shaohui.shareutil.share.instance.DefaultShareInstance;
23 | import me.shaohui.shareutil.share.instance.QQShareInstance;
24 | import me.shaohui.shareutil.share.instance.ShareInstance;
25 | import me.shaohui.shareutil.share.instance.WeiboShareInstance;
26 | import me.shaohui.shareutil.share.instance.WxShareInstance;
27 | import static me.shaohui.shareutil.ShareLogger.INFO;
28 |
29 | /**
30 | * Created by shaohui on 2016/11/18.
31 | */
32 |
33 | public class ShareUtil {
34 | /**
35 | * 测试case
36 | *
37 | * 1. 本地图片 vs 网络图片
38 | * 2. 图片大小限制
39 | * 3. 文字长度限制
40 | */
41 |
42 | public static final int TYPE = 798;
43 |
44 | public static ShareListener mShareListener;
45 |
46 | private static ShareInstance mShareInstance;
47 |
48 | private final static int TYPE_IMAGE = 1;
49 | private final static int TYPE_TEXT = 2;
50 | private final static int TYPE_MEDIA = 3;
51 |
52 | private static int mType;
53 | private static int mPlatform;
54 | private static String mText;
55 | private static ShareImageObject mShareImageObject;
56 | private static String mTitle;
57 | private static String mSummary;
58 | private static String mTargetUrl;
59 |
60 | static void action(Activity activity) {
61 | mShareInstance = getShareInstance(mPlatform, activity);
62 |
63 | // 防止之后调用 NullPointException
64 | if (mShareListener == null) {
65 | activity.finish();
66 | return;
67 | }
68 |
69 | if (!mShareInstance.isInstall(activity)) {
70 | mShareListener.shareFailure(new Exception(INFO.NOT_INSTALL));
71 | activity.finish();
72 | return;
73 | }
74 |
75 | switch (mType) {
76 | case TYPE_TEXT:
77 | mShareInstance.shareText(mPlatform, mText, activity, mShareListener);
78 | break;
79 | case TYPE_IMAGE:
80 | mShareInstance.shareImage(mPlatform, mShareImageObject, activity, mShareListener);
81 | break;
82 | case TYPE_MEDIA:
83 | mShareInstance.shareMedia(mPlatform, mTitle, mTargetUrl, mSummary,
84 | mShareImageObject, activity, mShareListener);
85 | break;
86 | }
87 | }
88 |
89 | public static void shareText(Context context, @SharePlatform.Platform int platform, String text,
90 | ShareListener listener) {
91 | mType = TYPE_TEXT;
92 | mText = text;
93 | mPlatform = platform;
94 | mShareListener = buildProxyListener(listener);
95 |
96 | context.startActivity(_ShareActivity.newInstance(context, TYPE));
97 | }
98 |
99 | public static void shareImage(Context context, @SharePlatform.Platform final int platform,
100 | final String urlOrPath, ShareListener listener) {
101 | mType = TYPE_IMAGE;
102 | mPlatform = platform;
103 | mShareImageObject = new ShareImageObject(urlOrPath);
104 | mShareListener = buildProxyListener(listener);
105 |
106 | context.startActivity(_ShareActivity.newInstance(context, TYPE));
107 | }
108 |
109 | public static void shareImage(Context context, @SharePlatform.Platform final int platform,
110 | final Bitmap bitmap, ShareListener listener) {
111 | mType = TYPE_IMAGE;
112 | mPlatform = platform;
113 | mShareImageObject = new ShareImageObject(bitmap);
114 | mShareListener = buildProxyListener(listener);
115 |
116 | context.startActivity(_ShareActivity.newInstance(context, TYPE));
117 | }
118 |
119 | public static void shareMedia(Context context, @SharePlatform.Platform int platform,
120 | String title, String summary, String targetUrl, Bitmap thumb, ShareListener listener) {
121 | mType = TYPE_MEDIA;
122 | mPlatform = platform;
123 | mShareImageObject = new ShareImageObject(thumb);
124 | mSummary = summary;
125 | mTargetUrl = targetUrl;
126 | mTitle = title;
127 | mShareListener = buildProxyListener(listener);
128 |
129 | context.startActivity(_ShareActivity.newInstance(context, TYPE));
130 | }
131 |
132 | public static void shareMedia(Context context, @SharePlatform.Platform int platform,
133 | String title, String summary, String targetUrl, String thumbUrlOrPath,
134 | ShareListener listener) {
135 | mType = TYPE_MEDIA;
136 | mPlatform = platform;
137 | mShareImageObject = new ShareImageObject(thumbUrlOrPath);
138 | mSummary = summary;
139 | mTargetUrl = targetUrl;
140 | mTitle = title;
141 | mShareListener = buildProxyListener(listener);
142 |
143 | context.startActivity(_ShareActivity.newInstance(context, TYPE));
144 | }
145 |
146 | private static ShareListener buildProxyListener(ShareListener listener) {
147 | return new ShareListenerProxy(listener);
148 | }
149 |
150 | public static void handleResult(Intent data) {
151 | // 微博分享会同时回调onActivityResult和onNewIntent, 而且前者返回的intent为null
152 | if (mShareInstance != null && data != null) {
153 | mShareInstance.handleResult(data);
154 | } else if (data == null) {
155 | if (mPlatform != SharePlatform.WEIBO) {
156 | ShareLogger.e(INFO.HANDLE_DATA_NULL);
157 | }
158 | } else {
159 | ShareLogger.e(INFO.UNKNOWN_ERROR);
160 | }
161 | }
162 |
163 | private static ShareInstance getShareInstance(@SharePlatform.Platform int platform,
164 | Context context) {
165 | switch (platform) {
166 | case SharePlatform.WX:
167 | case SharePlatform.WX_TIMELINE:
168 | return new WxShareInstance(context, ShareManager.CONFIG.getWxId());
169 | case SharePlatform.QQ:
170 | case SharePlatform.QZONE:
171 | return new QQShareInstance(context, ShareManager.CONFIG.getQqId());
172 | case SharePlatform.WEIBO:
173 | return new WeiboShareInstance(context, ShareManager.CONFIG.getWeiboId());
174 | case SharePlatform.DEFAULT:
175 | default:
176 | return new DefaultShareInstance();
177 | }
178 | }
179 |
180 | public static void recycle() {
181 | mTitle = null;
182 | mSummary = null;
183 | mShareListener = null;
184 |
185 | // bitmap recycle
186 | if (mShareImageObject != null
187 | && mShareImageObject.getBitmap() != null
188 | && !mShareImageObject.getBitmap().isRecycled()) {
189 | mShareImageObject.getBitmap().recycle();
190 | }
191 | mShareImageObject = null;
192 |
193 | if (mShareInstance != null) {
194 | mShareInstance.recycle();
195 | }
196 | mShareInstance = null;
197 | }
198 |
199 | /**
200 | * 检查客户端是否安装
201 | */
202 |
203 | public static boolean isInstalled(@SharePlatform.Platform int platform, Context context) {
204 | switch (platform) {
205 | case SharePlatform.QQ:
206 | case SharePlatform.QZONE:
207 | return isQQInstalled(context);
208 | case SharePlatform.WEIBO:
209 | return isWeiBoInstalled(context);
210 | case SharePlatform.WX:
211 | case SharePlatform.WX_TIMELINE:
212 | return isWeiXinInstalled(context);
213 | case SharePlatform.DEFAULT:
214 | return true;
215 | default:
216 | return false;
217 | }
218 | }
219 |
220 | @Deprecated
221 | public static boolean isQQInstalled(@NonNull Context context) {
222 | PackageManager pm = context.getPackageManager();
223 | if (pm == null) {
224 | return false;
225 | }
226 |
227 | List packageInfos = pm.getInstalledPackages(0);
228 | for (PackageInfo info : packageInfos) {
229 | if (TextUtils.equals(info.packageName.toLowerCase(Locale.getDefault()),
230 | "com.tencent.mobileqq")) {
231 | return true;
232 | }
233 | }
234 | return false;
235 | }
236 |
237 | @Deprecated
238 | public static boolean isWeiBoInstalled(@NonNull Context context) {
239 | IWeiboShareAPI shareAPI =
240 | WeiboShareSDK.createWeiboAPI(context, ShareManager.CONFIG.getWeiboId());
241 | return shareAPI.isWeiboAppInstalled();
242 | }
243 |
244 | @Deprecated
245 | public static boolean isWeiXinInstalled(Context context) {
246 | IWXAPI api = WXAPIFactory.createWXAPI(context, ShareManager.CONFIG.getWxId(), true);
247 | return api.isWXAppInstalled();
248 | }
249 |
250 | private static class ShareListenerProxy extends ShareListener {
251 |
252 | private final ShareListener mShareListener;
253 |
254 | ShareListenerProxy(ShareListener listener) {
255 | mShareListener = listener;
256 | }
257 |
258 | @Override
259 | public void shareSuccess() {
260 | ShareLogger.i(INFO.SHARE_SUCCESS);
261 | ShareUtil.recycle();
262 | mShareListener.shareSuccess();
263 | }
264 |
265 | @Override
266 | public void shareFailure(Exception e) {
267 | ShareLogger.i(INFO.SHARE_FAILURE);
268 | ShareUtil.recycle();
269 | mShareListener.shareFailure(e);
270 | }
271 |
272 | @Override
273 | public void shareCancel() {
274 | ShareLogger.i(INFO.SHARE_CANCEL);
275 | ShareUtil.recycle();
276 | mShareListener.shareCancel();
277 | }
278 |
279 | @Override
280 | public void shareRequest() {
281 | ShareLogger.i(INFO.SHARE_REQUEST);
282 | mShareListener.shareRequest();
283 | }
284 | }
285 | }
286 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------