ShareSdkUtil.java 11.2 KB
package com.cnlive.goldenline.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.cnlive.goldenline.R;
import com.cnlive.goldenline.ui.widget.SharePopupWindow;
import com.mob.tools.utils.UIHandler;

import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import static com.cnlive.goldenline.util.LogUtils.LOGD;
import static com.cnlive.goldenline.util.LogUtils.LOGE;


public class ShareSdkUtil {
    public static final int SHARE_VIDEO = 1;
    public static final int SHARE_IMAGE = 2;
    public static final int SHARE_CONTENT = 3;
    private static final int MSG_ACTION_CCALLBACK = 4;
    @Nullable
    private static SharePopupWindow popupWindow;
    private static Context mContext;
    @NonNull
    private static Callback callback = new Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.arg1) {
                case 1:
                    // 成功  用tostutil微信回调不起作用,所以改用tost
                    Toast.makeText(mContext, mContext.getString(R.string.toast_msg_share_success), Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    // 失败
                    Toast.makeText(mContext, mContext.getString(R.string.toast_msg_share_fail), Toast.LENGTH_LONG).show();
                    break;
                case 3:
                    // 取消
                    Toast.makeText(mContext, mContext.getString(R.string.toast_msg_share_cancel), Toast.LENGTH_LONG).show();
                    break;
            }
            return false;
        }
    };
    private static Platform sPlat;

    //获取第三方平台用户资料
    public static void loginThirdPlatform(Context context, String platformName, PlatformActionListener paListener) {
        initShareSdk(context);
        Platform plat = ShareSDK.getPlatform(context, platformName);
        if (plat == null) {
            return;
        }
        if (plat.isValid()) {//先清除缓存
            plat.removeAccount();
        }
        plat.setPlatformActionListener(paListener);
        //使用SSO授权,通过客户单授权
        plat.SSOSetting(false);
        plat.showUser(null);//获取登陆用户资料
    }


    public static void selectSharePlatform(@Nullable Activity a, int showAtLocationId, int type, String title, String content, String img, String targetUrl) {
        if (a == null)
            return;
        if (popupWindow != null)
            popupWindow.dismiss();
        popupWindow = new SharePopupWindow(a.findViewById(showAtLocationId), a, type, title, content, img, targetUrl);
    }

    public static void sharePlatform(@NonNull Context context, @NonNull String platformName, int type, String title, String content, String img, String targetUrl) {
        mContext = context;
        if (popupWindow != null)
            popupWindow.dismiss();
        initShareSdk(context);
        if (type == SHARE_VIDEO) {
            LOGE("   share   视频 ");
            shareMediaVideo(context, platformName, title, content, img, targetUrl);
        } else if (type == SHARE_IMAGE) {
            LOGE("   share   图片 ");
            shareMediaImage(context, platformName, title, content, img, targetUrl);
        } else if (type == SHARE_CONTENT) {
            LOGE("   share   视频1 ");
            shareMediaContent(context, platformName, title, content, img, targetUrl);
        }
    }

    private static void shareMediaContent(@NonNull final Context context, @NonNull String platformName, String title, String content, String img, String targetUrl) {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setTitleUrl(targetUrl);
        LOGE("share  img   ==" + img);
        sp.setImagePath(img);

        if (platformName.equals(SinaWeibo.NAME)) {
            sp.setTitle(title);
            sp.setText(content + targetUrl);
        } else if (platformName.equals(Wechat.NAME)) {
            sp.setUrl(targetUrl);
            sp.setTitle(title);
            sp.setText(content);
        } else if (platformName.equals(WechatMoments.NAME)) {
            sp.setUrl(targetUrl);
            sp.setTitle(content);
            sp.setText(content);
        } else if (platformName.equals(QZone.NAME)) {
            sp.setTitle(title);
            sp.setText(content);
            sp.setSite("正在上演");
            sp.setSiteUrl(context.getString(R.string.share_program_download_targetUrl));
        }

        directShare(context, platformName, sp);
    }

    private static void shareMediaImage(@NonNull final Context context, @NonNull String platformName, String title, String content, String img, String targetUrl) {
        Platform.ShareParams sp = new Platform.ShareParams();
        LOGD("platformName= " + platformName);
        sp.setShareType(Platform.SHARE_IMAGE);
        //        sp.setTitle(title);
        sp.setImageUrl(img);
        if (platformName.equals(SinaWeibo.NAME)) {
            sp.setText(title);
        } else if (platformName.equals(Wechat.NAME)) {
            sp.setSite("正在上演");
            sp.setSiteUrl(context.getString(R.string.share_program_download_targetUrl));
            LOGD("targetUrl= " + targetUrl);
        } else if (platformName.equals(WechatMoments.NAME)) {
            sp.setSite("正在上演");
            sp.setSiteUrl(context.getString(R.string.share_program_download_targetUrl));
        } else if (platformName.equals(QZone.NAME)) {
            //            sp.setTitleUrl(targetUrl);

            sp.setText(content);
            sp.setSite("正在上演");
            sp.setSiteUrl(context.getString(R.string.share_program_download_targetUrl));
        }

        directShare(context, platformName, sp);
    }

    private static void shareMediaVideo(@NonNull Context context, @NonNull String platformName, String title, String content, String img, String targetUrl) {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setShareType(Platform.SHARE_VIDEO);
        sp.setTitle("【正在上演】"+title);
        LogUtils.LOGD("share   img=" + img);
        sp.setImageUrl(img);
        //        sp.setImagePath(img);

        if (platformName.equals(SinaWeibo.NAME)) {
            sp.setText("【正在上演】"+"《" + title + "》" + content + targetUrl);
        } else if (platformName.equals(Wechat.NAME)) {
            sp.setUrl(targetUrl);
            sp.setText(content);
            Log.e("111111111111111111",targetUrl+"       "+content);
        } else if (platformName.equals(WechatMoments.NAME)) {
            sp.setUrl(targetUrl);
            sp.setText(content);
            Log.e("22222222222",targetUrl+"       "+content);
        } else if (platformName.equals(QZone.NAME)) {
            sp.setTitleUrl(targetUrl);
            sp.setText(content);
            sp.setSite("正在上演");
            sp.setSiteUrl(context.getString(R.string.share_program_download_targetUrl));
        }
        //        Platform.ShareParams wechat = new Platform.ShareParams();
        //        wechat.setTitle("分享标题");
        //        wechat.setText("分享文本   http://www.baidu.com");
        //        wechat.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
        //                wechat.setUrl("http://www.baidu.com");
        //        wechat.setShareType(Platform. SHARE_WEBPAGE);
        directShare(context, platformName, sp);

    }

    private static void shareComplete(Activity a, String platformName) {
        //        if (platformName.equals(SinaWeibo.NAME)) {
        //            HttpRequest.appProbe(a, Probe.eventId_share, Probe.uri_share_sina);
        //        } else if (platformName.equals(Wechat.NAME)) {
        //            HttpRequest.appProbe(a, Probe.eventId_share, Probe.uri_share_weixin);
        //        } else if (platformName.equals(WechatMoments.NAME)) {
        //            HttpRequest.appProbe(a, Probe.eventId_share, Probe.uri_share_weixin_circle);
        //        } else if (platformName.equals(QZone.NAME)) {
        //            HttpRequest.appProbe(a, Probe.eventId_share, Probe.uri_share_qzone);
        //        }
    }

    private static void directShare(final Context context, @NonNull final String platformName, Platform.ShareParams sp) {
        Log.e("TAGTAGTAG=====",platformName);
        Platform platform = ShareSDK.getPlatform(platformName);
        Log.e("222222222222222=====",platform.toString());
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int action, HashMap<String, Object> stringObjectHashMap) {
                // 成功
                LOGD("shareonComplete" + action + "      " + platformName.toString());
                Message msg = new Message();
                msg.what = MSG_ACTION_CCALLBACK;
                msg.arg1 = 1;
                msg.arg2 = action;
                msg.obj = platform;
                UIHandler.sendMessage(msg, callback);

            }

            @Override
            public void onError(Platform platform, int action, @NonNull Throwable throwable) {
                // 失敗
                //打印错误信息,print the error msg
                throwable.printStackTrace();
                //错误监听,handle the error msg
                Message msg = new Message();
                msg.what = MSG_ACTION_CCALLBACK;
                msg.arg1 = 2;
                msg.arg2 = action;
                msg.obj = throwable;
                LogUtils.LOGD("share    " + msg.toString());
                //                UIHandler.sendMessage(msg, callback);
            }

            @Override
            public void onCancel(Platform platform, int action) {
                // 取消
                Message msg = new Message();
                msg.what = MSG_ACTION_CCALLBACK;
                msg.arg1 = 3;
                msg.arg2 = action;
                msg.obj = platform;
                //                UIHandler.sendMessage(msg, callback);
            }
        });  // 设置分享事件回调
        // 执行图文分享
        platform.share(sp);
    }

    public static void initShareSdk(Context context) {
        //初始化shareSdk  参数appkey
        ShareSDK.initSDK(context);
    }

    public static boolean isAppAvilible(@NonNull Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        for (int i = 0; i < pinfo.size(); i++) {
            if (pinfo.get(i).packageName.equalsIgnoreCase(packageName)) {
                return true;
            }
        }
        return false;
    }

}