573 lines
20 KiB
Java
573 lines
20 KiB
Java
package com.gh.common.util;
|
||
|
||
import android.app.Activity;
|
||
import android.content.ClipboardManager;
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.content.pm.PackageInfo;
|
||
import android.content.pm.PackageManager;
|
||
import android.graphics.Bitmap;
|
||
import android.graphics.Canvas;
|
||
import android.graphics.Color;
|
||
import android.graphics.Matrix;
|
||
import android.os.Bundle;
|
||
import android.support.v7.widget.GridLayoutManager;
|
||
import android.support.v7.widget.RecyclerView;
|
||
import android.view.Gravity;
|
||
import android.view.KeyEvent;
|
||
import android.view.LayoutInflater;
|
||
import android.view.View;
|
||
import android.view.ViewGroup;
|
||
import android.widget.ImageView;
|
||
import android.widget.LinearLayout;
|
||
import android.widget.PopupWindow;
|
||
import android.widget.RelativeLayout;
|
||
import android.widget.TextView;
|
||
|
||
import com.facebook.common.references.CloseableReference;
|
||
import com.facebook.datasource.DataSource;
|
||
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
|
||
import com.facebook.imagepipeline.image.CloseableImage;
|
||
import com.gh.common.constant.Config;
|
||
import com.gh.gamecenter.R;
|
||
import com.gh.gamecenter.WeiBoShareActivity;
|
||
import com.tencent.connect.share.QQShare;
|
||
import com.tencent.connect.share.QzoneShare;
|
||
import com.tencent.mm.sdk.openapi.IWXAPI;
|
||
import com.tencent.mm.sdk.openapi.SendMessageToWX;
|
||
import com.tencent.mm.sdk.openapi.WXAPIFactory;
|
||
import com.tencent.mm.sdk.openapi.WXMediaMessage;
|
||
import com.tencent.mm.sdk.openapi.WXWebpageObject;
|
||
import com.tencent.mm.sdk.platformtools.Util;
|
||
import com.tencent.tauth.IUiListener;
|
||
import com.tencent.tauth.Tencent;
|
||
import com.tencent.tauth.UiError;
|
||
|
||
import java.io.ByteArrayOutputStream;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* Created by khy on 2016/9/4.
|
||
*/
|
||
public class ShareUtils {
|
||
|
||
private static ShareUtils instance;
|
||
private IWXAPI mIWXAPI;
|
||
private Tencent mTencent;
|
||
private String shareUrl;
|
||
private String shareGameName;
|
||
private String shareIcon;
|
||
private String shareNewsTitle; // shareNewsTitle不为空就是新闻分享,否则是游戏分享
|
||
private boolean isPlugin = false;
|
||
private boolean ispopupWindow;
|
||
private boolean isToolsBox;
|
||
|
||
private int[] arrLogo = {
|
||
R.drawable.share_wechat_logo,
|
||
R.drawable.share_wechatmoments_logo,
|
||
R.drawable.share_qq_logo,
|
||
R.drawable.share_qzone_logo,
|
||
R.drawable.share_sinaweibo_logo,
|
||
R.drawable.share_shortmessage_logo,
|
||
R.drawable.share_copyfont_logo,
|
||
R.drawable.share_cancel_logo
|
||
};
|
||
private String[] arrLabel = {"微信好友", "朋友圈", "QQ好友", "QQ空间", "新浪微博", "短信", "复制链接", "取消"};
|
||
|
||
private PopupWindow popupWindow;
|
||
|
||
private Context mContext;
|
||
//QQ或者QQ空间分享回调处理
|
||
public IUiListener QqShareListener = new IUiListener() {
|
||
@Override
|
||
public void onComplete(Object o) {
|
||
Utils.toast(mContext, "分享成功");
|
||
}
|
||
|
||
@Override
|
||
public void onError(UiError uiError) {
|
||
Utils.toast(mContext, "分享失败");
|
||
}
|
||
|
||
@Override
|
||
public void onCancel() {
|
||
Utils.toast(mContext, "分享已取消");
|
||
}
|
||
};
|
||
|
||
private ShareUtils(Context context) {
|
||
mTencent = Tencent.createInstance(Config.TENCENT_APPID, context); //初始化QQ分享
|
||
mIWXAPI = WXAPIFactory.createWXAPI(context, Config.WECHAT_APPID); //初始化微信分享
|
||
mContext = context;
|
||
}
|
||
|
||
public static ShareUtils getInstance(Activity context) {
|
||
if (instance == null) {
|
||
instance = new ShareUtils(context);
|
||
}
|
||
return instance;
|
||
}
|
||
|
||
//检查是否安装手机QQ
|
||
public static boolean isQQClientAvailable(Context context) {
|
||
final PackageManager packageManager = context.getPackageManager();
|
||
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
|
||
if (pinfo != null) {
|
||
for (int i = 0; i < pinfo.size(); i++) {
|
||
String pn = pinfo.get(i).packageName;
|
||
if (pn.equals("com.tencent.mobileqq")) {
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* @param view ispopupWindow-true是绑定的布局, ispopupWindow-false 则是嵌套的父控件
|
||
* @param url 分享链接
|
||
* @param gameName 游戏名 与 新闻标题区分
|
||
* @param icon 分享图标
|
||
* @param newsTitle 新闻标题 与 游戏名区分
|
||
* @param isPlugin 判断游戏是否是插件
|
||
* @param ispopupWindow 判断是否是 PopupWindow false可直接嵌套进布局(分享光环),view是父控件
|
||
*/
|
||
public void showShareWindows(View view, String url, String gameName, String icon, String newsTitle,
|
||
boolean isPlugin, boolean ispopupWindow, boolean isToolsBox) {
|
||
this.shareIcon = icon;
|
||
this.shareGameName = gameName;
|
||
this.shareUrl = url;
|
||
this.shareNewsTitle = newsTitle;
|
||
this.isPlugin = isPlugin;
|
||
this.ispopupWindow = ispopupWindow;
|
||
this.isToolsBox = isToolsBox;
|
||
|
||
View contentView = View.inflate(mContext, R.layout.share_popup_layout, null);
|
||
contentView.setFocusable(true);
|
||
contentView.setFocusableInTouchMode(true);
|
||
RecyclerView shareRecyclerView = (RecyclerView) contentView.findViewById(R.id.share_rv);
|
||
|
||
shareRecyclerView.setPadding(DisplayUtils.dip2px(mContext, 20), DisplayUtils.dip2px(mContext, 10), DisplayUtils.dip2px(mContext, 20), 0);
|
||
|
||
//RecyclerView禁止滑动
|
||
GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 4) {
|
||
@Override
|
||
public boolean canScrollVertically() {
|
||
return false;
|
||
}
|
||
};
|
||
|
||
shareRecyclerView.setLayoutManager(gridLayoutManager);
|
||
shareRecyclerView.setAdapter(new ShareRecyclerViewAdapter());
|
||
|
||
if (!ispopupWindow) {
|
||
RelativeLayout layout = (RelativeLayout) view;
|
||
layout.addView(contentView);
|
||
arrLabel[6] = "邮件";
|
||
arrLogo[6] = R.drawable.share_email_logo;
|
||
arrLabel[7] = "复制链接";
|
||
arrLogo[7] = R.drawable.share_copyfont_logo;
|
||
return;
|
||
} else {
|
||
arrLabel[6] = "复制链接";
|
||
arrLogo[6] = R.drawable.share_copyfont_logo;
|
||
arrLabel[7] = "取消";
|
||
arrLogo[7] = R.drawable.share_cancel_logo;
|
||
}
|
||
|
||
popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT
|
||
, LinearLayout.LayoutParams.MATCH_PARENT, true);
|
||
popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
|
||
popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
|
||
|
||
contentView.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
popupWindow.dismiss();
|
||
}
|
||
});
|
||
|
||
contentView.setOnKeyListener(new View.OnKeyListener() {
|
||
@Override
|
||
public boolean onKey(View v, int keyCode, KeyEvent event) {
|
||
|
||
if (keyCode == KeyEvent.KEYCODE_BACK
|
||
&& event.getRepeatCount() == 0 && popupWindow != null
|
||
&& popupWindow.isShowing()) {
|
||
popupWindow.dismiss();
|
||
}
|
||
return false;
|
||
}
|
||
});
|
||
}
|
||
|
||
//QQ分享
|
||
private void qqSahre() {
|
||
Utils.toast(mContext, "分享跳转中...");
|
||
Bundle params = new Bundle();
|
||
|
||
String title;
|
||
String summary;
|
||
|
||
if (isToolsBox) {
|
||
title = shareNewsTitle;
|
||
summary = shareGameName;
|
||
} else if (ispopupWindow) {
|
||
if (shareNewsTitle != null) {
|
||
title = shareNewsTitle;
|
||
summary = "来自光环助手(最强卡牌神器)";
|
||
} else {
|
||
title = "向你推荐:";
|
||
if (isPlugin) {
|
||
summary = shareGameName + "(光环加速版)";
|
||
} else {
|
||
summary = shareGameName;
|
||
}
|
||
}
|
||
} else {
|
||
title = "玩手游不用肝的感觉真好";
|
||
summary = "绿色安全的手游加速助手";
|
||
}
|
||
params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
|
||
params.putString(QQShare.SHARE_TO_QQ_SUMMARY, summary);
|
||
params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
|
||
params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareUrl);
|
||
params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareIcon);
|
||
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "光环助手");
|
||
|
||
mTencent.shareToQQ(
|
||
(Activity) mContext, params, QqShareListener);
|
||
|
||
if (ispopupWindow) {
|
||
popupWindow.dismiss();
|
||
}
|
||
}
|
||
|
||
//微信好友分享
|
||
private void wechatSahre() {
|
||
Utils.toast(mContext, "分享跳转中...");
|
||
WXWebpageObject webpage = new WXWebpageObject();
|
||
WXMediaMessage msg = new WXMediaMessage(webpage);
|
||
webpage.webpageUrl = shareUrl;
|
||
|
||
String title;
|
||
String summary;
|
||
|
||
if (isToolsBox) {
|
||
title = shareNewsTitle;
|
||
summary = shareGameName;
|
||
} else if (ispopupWindow) {
|
||
if (shareNewsTitle != null) {
|
||
title = shareNewsTitle;
|
||
summary = "来自光环助手(最强卡牌神器)";
|
||
} else {
|
||
title = "向你推荐:";
|
||
if (isPlugin) {
|
||
summary = shareGameName + "(光环加速版)";
|
||
} else {
|
||
summary = shareGameName;
|
||
}
|
||
}
|
||
} else {
|
||
title = "玩手游不用肝的感觉真好";
|
||
summary = "绿色安全的手游加速助手";
|
||
}
|
||
|
||
msg.title = title;
|
||
msg.description = summary;
|
||
|
||
SendMessageToWX.Req req = new SendMessageToWX.Req();
|
||
req.transaction = buildTransaction("webpage");
|
||
req.message = msg;
|
||
req.scene = SendMessageToWX.Req.WXSceneSession;
|
||
|
||
loadBitMap(shareIcon, msg, req);
|
||
if (ispopupWindow) {
|
||
popupWindow.dismiss();
|
||
}
|
||
}
|
||
|
||
private String buildTransaction(final String type) {
|
||
return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
|
||
}
|
||
|
||
private void loadBitMap(final String iconUrl, final WXMediaMessage msg, final SendMessageToWX.Req req) {
|
||
ImageUtils.getInstance().display(mContext, iconUrl, new BaseBitmapDataSubscriber() {
|
||
@Override
|
||
protected void onNewResultImpl(Bitmap bitmap) {
|
||
Bitmap compressBp = compressBitmap(bitmap);
|
||
Bitmap resultBp = addBackGround(compressBp);
|
||
msg.thumbData = Util.bmpToByteArray(resultBp, true);
|
||
mIWXAPI.sendReq(req);
|
||
}
|
||
|
||
@Override
|
||
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
|
||
Utils.log("分享获取bitmap失败");
|
||
}
|
||
});
|
||
}
|
||
|
||
//压缩图片
|
||
private Bitmap compressBitmap(Bitmap bitmap) {
|
||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, bos);
|
||
float zoom = (float) Math.sqrt(10 * 1024 / (float) bos.toByteArray().length);
|
||
|
||
Matrix matrix = new Matrix();
|
||
matrix.setScale(zoom, zoom);
|
||
|
||
Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
|
||
bos.reset();
|
||
|
||
result.compress(Bitmap.CompressFormat.JPEG, 85, bos);
|
||
|
||
while (bos.toByteArray().length > 10 * 1024) {
|
||
System.out.println(bos.toByteArray().length);
|
||
matrix.setScale(0.9f, 0.9f);
|
||
result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, true);
|
||
bos.reset();
|
||
result.compress(Bitmap.CompressFormat.JPEG, 85, bos);
|
||
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
//添加背景,防止图片格式为PNG的图片分享出现黑框问题
|
||
public Bitmap addBackGround(Bitmap result) {
|
||
Bitmap bgBitmap;
|
||
int[] colors = new int[result.getWidth() * result.getHeight()];
|
||
for (int i = 0; i < colors.length; i++) {
|
||
colors[i] = Color.WHITE;
|
||
}
|
||
bgBitmap = Bitmap.createBitmap(colors, result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);
|
||
|
||
Bitmap newmap = Bitmap
|
||
.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);
|
||
Canvas canvas = new Canvas(newmap);
|
||
canvas.drawBitmap(bgBitmap, 0, 0, null);
|
||
canvas.drawBitmap(result, (result.getHeight() - result.getWidth()) / 2,
|
||
(result.getHeight() - result.getWidth()) / 2, null);
|
||
canvas.save(Canvas.ALL_SAVE_FLAG);
|
||
canvas.restore();
|
||
|
||
return newmap;
|
||
}
|
||
|
||
//QQ空间分享
|
||
private void qZoneSahre() {
|
||
Utils.toast(mContext, "分享跳转中...");
|
||
Bundle params = new Bundle();
|
||
|
||
String title;
|
||
String summary = null;
|
||
|
||
if (isToolsBox) {
|
||
title = shareNewsTitle;
|
||
summary = shareGameName;
|
||
} else if (ispopupWindow) {
|
||
if (shareNewsTitle != null) {
|
||
title = shareNewsTitle;
|
||
} else {
|
||
title = "向你推荐:";
|
||
if (isPlugin) {
|
||
summary = shareGameName + "(光环加速版)";
|
||
} else {
|
||
summary = shareGameName;
|
||
}
|
||
}
|
||
} else {
|
||
title = "玩手游不用肝的感觉真好";
|
||
summary = "绿色安全的手游加速助手";
|
||
}
|
||
|
||
ArrayList<String> imageUrls = new ArrayList<>();
|
||
imageUrls.add(shareIcon);
|
||
|
||
if (summary != null) {
|
||
params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, summary);
|
||
}
|
||
params.putString(QzoneShare.SHARE_TO_QQ_TITLE, title);
|
||
params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_NO_TYPE);
|
||
params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, shareUrl);
|
||
params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);
|
||
params.putString(QzoneShare.SHARE_TO_QQ_APP_NAME, "光环助手");
|
||
|
||
mTencent.shareToQzone(
|
||
(Activity) mContext, params, QqShareListener);
|
||
if (ispopupWindow) {
|
||
popupWindow.dismiss();
|
||
}
|
||
}
|
||
|
||
//微信朋友圈分享
|
||
private void wechatMomentsSahre() {
|
||
Utils.toast(mContext, "分享跳转中...");
|
||
WXWebpageObject webpage = new WXWebpageObject();
|
||
WXMediaMessage msg = new WXMediaMessage(webpage);
|
||
|
||
webpage.webpageUrl = shareUrl;
|
||
|
||
String title;
|
||
|
||
if (isToolsBox) {
|
||
title = shareNewsTitle;
|
||
} else if (ispopupWindow) {
|
||
if (shareNewsTitle != null) {
|
||
title = shareNewsTitle;
|
||
} else {
|
||
if (isPlugin) {
|
||
title = shareGameName + "(光环加速版)";
|
||
} else {
|
||
title = shareGameName;
|
||
}
|
||
}
|
||
} else {
|
||
title = "玩手游不用肝的感觉真好";
|
||
}
|
||
|
||
msg.title = title;
|
||
SendMessageToWX.Req req = new SendMessageToWX.Req();
|
||
|
||
req.transaction = buildTransaction("webpage");
|
||
req.message = msg;
|
||
req.scene = SendMessageToWX.Req.WXSceneTimeline;
|
||
|
||
loadBitMap(shareIcon, msg, req);
|
||
if (ispopupWindow) {
|
||
popupWindow.dismiss();
|
||
}
|
||
}
|
||
|
||
//新浪微博分享
|
||
private void sinaWeiboSahre() {
|
||
Intent intent = WeiBoShareActivity.getWeiboshareIntent(mContext,
|
||
shareNewsTitle, shareIcon, shareGameName, shareUrl, isPlugin, ispopupWindow, isToolsBox);
|
||
mContext.startActivity(intent);
|
||
|
||
if (ispopupWindow) {
|
||
popupWindow.dismiss();
|
||
}
|
||
}
|
||
|
||
//短信分享
|
||
private void shortMessageSahre() {
|
||
String smsBody;
|
||
if (isToolsBox) {
|
||
smsBody = shareNewsTitle + shareUrl;
|
||
} else if (ispopupWindow) {
|
||
if (shareNewsTitle != null) {
|
||
smsBody = shareNewsTitle + shareUrl;
|
||
} else {
|
||
if (isPlugin) {
|
||
smsBody = "向你推荐:" + shareGameName + "(光环加速版)" + shareUrl;
|
||
} else {
|
||
smsBody = "向你推荐:" + shareGameName + shareUrl;
|
||
}
|
||
}
|
||
} else {
|
||
smsBody = "这个App可以下载各种热门卡牌手游的加速版,绿色安全,超级省心,做日常效率提高3-5倍!光环助手官网地址:" + shareUrl;
|
||
}
|
||
|
||
try {
|
||
Intent sendIntent = IntentUtils.getSMSIntent(smsBody);
|
||
mContext.startActivity(sendIntent);
|
||
} catch (Exception e) {
|
||
Utils.toast(mContext, "系统异常,分享失败");
|
||
e.printStackTrace();
|
||
}
|
||
|
||
if (ispopupWindow) {
|
||
popupWindow.dismiss();
|
||
}
|
||
}
|
||
|
||
//复制文字链接
|
||
private void copyLink(String copyContent) {
|
||
ClipboardManager cmb = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
|
||
cmb.setText(copyContent);
|
||
if (ispopupWindow) {
|
||
Utils.toast(mContext, "复制成功");
|
||
popupWindow.dismiss();
|
||
} else {
|
||
Utils.toast(mContext, "复制成功,请到微信/QQ粘贴分享");
|
||
}
|
||
}
|
||
|
||
private class ShareRecyclerViewAdapter extends RecyclerView.Adapter<ShareRecyclerViewAdapter.ViewHolder> {
|
||
|
||
@Override
|
||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||
View view = LayoutInflater.from(mContext).inflate(R.layout.share_popup_item, parent, false);
|
||
return new ViewHolder(view);
|
||
}
|
||
|
||
@Override
|
||
public void onBindViewHolder(final ViewHolder holder, int position) {
|
||
holder.shareLogo.setImageResource(arrLogo[position]);
|
||
holder.shareLabel.setText(arrLabel[position]);
|
||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
switch (holder.getPosition()) {
|
||
case 0:
|
||
wechatSahre();
|
||
break;
|
||
case 1:
|
||
wechatMomentsSahre();
|
||
break;
|
||
case 2:
|
||
qqSahre();
|
||
break;
|
||
case 3:
|
||
qZoneSahre();
|
||
break;
|
||
case 4:
|
||
sinaWeiboSahre();
|
||
break;
|
||
case 5:
|
||
shortMessageSahre();
|
||
break;
|
||
case 6:
|
||
if (ispopupWindow) {
|
||
copyLink(shareUrl);
|
||
} else {
|
||
Intent data = IntentUtils.getEmailToGHIntent();
|
||
mContext.startActivity(data);
|
||
}
|
||
break;
|
||
case 7:
|
||
if (ispopupWindow) {
|
||
popupWindow.dismiss();
|
||
} else {
|
||
copyLink("推荐光环助手,绿色安全的手游加速助手:" + shareUrl);
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public int getItemCount() {
|
||
return 8;
|
||
}
|
||
|
||
public class ViewHolder extends RecyclerView.ViewHolder {
|
||
LinearLayout linearLayout;
|
||
TextView shareLabel;
|
||
ImageView shareLogo;
|
||
|
||
public ViewHolder(View itemView) {
|
||
super(itemView);
|
||
linearLayout = (LinearLayout) itemView;
|
||
shareLogo = (ImageView) linearLayout.getChildAt(0);
|
||
shareLabel = (TextView) linearLayout.getChildAt(1);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|