527 lines
19 KiB
Java
527 lines
19 KiB
Java
package com.gh.common.util;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
import android.graphics.Color;
|
|
import android.graphics.Matrix;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import android.os.Environment;
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
import androidx.recyclerview.widget.GridLayoutManager;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import android.util.Base64;
|
|
import android.view.Gravity;
|
|
import android.view.KeyEvent;
|
|
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.gh.common.Base64ImageHolder;
|
|
import com.gh.common.constant.Config;
|
|
import com.gh.gamecenter.R;
|
|
import com.lightgame.utils.Utils;
|
|
import com.tencent.connect.auth.QQToken;
|
|
import com.tencent.connect.share.QQShare;
|
|
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
|
|
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
|
|
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
|
|
import com.tencent.mm.opensdk.openapi.IWXAPI;
|
|
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
|
|
import com.tencent.open.TDialog;
|
|
import com.tencent.tauth.IUiListener;
|
|
import com.tencent.tauth.Tencent;
|
|
import com.tencent.tauth.UiError;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.lang.ref.WeakReference;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Created by khy on 2016/11/8.
|
|
*/
|
|
public class MessageShareUtils {
|
|
|
|
private static MessageShareUtils instance;
|
|
private IWXAPI mIWXAPI;
|
|
private Tencent mTencent;
|
|
|
|
private WeakReference<Activity> mActivity; // 用来关闭分享页面
|
|
private Context mContext; // application context
|
|
|
|
private PopupWindow mPopupWindow;
|
|
private Bitmap shareBm; //分享截图
|
|
private String picName;
|
|
|
|
|
|
private String[] mArrLabel = {"微信好友", "朋友圈", "QQ好友", "QQ空间", "保存"};
|
|
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_save
|
|
};
|
|
|
|
//QQ或者QQ空间分享回调处理
|
|
public IUiListener QqShareListener = new IUiListener() {
|
|
@Override
|
|
public void onComplete(Object o) {
|
|
Utils.toast(mContext, "分享成功");
|
|
}
|
|
|
|
@Override
|
|
public void onError(UiError uiError) {
|
|
// 单分享图片不支持显示未安装弹窗,手动调出
|
|
Activity activity = mActivity.get();
|
|
if (activity != null && !ShareUtils.isQQClientAvailable(activity.getApplication())) {
|
|
new TDialog(activity,
|
|
"",
|
|
"http://openmobile.qq.com/oauth2.0/m_jump_by_version",
|
|
null,
|
|
new QQToken("")).show();
|
|
} else {
|
|
Utils.toast(mContext, "分享失败");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onCancel() {
|
|
Utils.toast(mContext, R.string.share_cancel_hint);
|
|
}
|
|
};
|
|
|
|
// 适配快传成绩单分享
|
|
private int contentSize;
|
|
private int paddTop;
|
|
private int picSize;
|
|
private int itemSize;
|
|
private int gridCount;
|
|
private int marImg;
|
|
private int shareType;
|
|
|
|
private MessageShareUtils(Context context) {
|
|
mTencent = Tencent.createInstance(Config.TENCENT_APPID, context); //初始化QQ分享
|
|
mIWXAPI = WXAPIFactory.createWXAPI(context, Config.WECHAT_APPID); //初始化微信分享
|
|
mContext = context.getApplicationContext();
|
|
}
|
|
|
|
public static MessageShareUtils getInstance(Context context) {
|
|
if (instance == null) {
|
|
instance = new MessageShareUtils(context);
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
public Tencent getTencent() {
|
|
return mTencent;
|
|
}
|
|
|
|
public void showShareWindows(Activity activity, View view, Bitmap bitmap, String picName, int shareType) {
|
|
this.shareBm = bitmap;
|
|
this.picName = picName;
|
|
this.shareType = shareType;
|
|
this.mActivity = new WeakReference<>(activity);
|
|
|
|
if (shareType == 2) {
|
|
contentSize = 75;
|
|
paddTop = 0;
|
|
gridCount = 4;
|
|
picSize = 30;
|
|
itemSize = 75;
|
|
marImg = 10;
|
|
} else {
|
|
contentSize = 100;
|
|
paddTop = 10;
|
|
gridCount = 5;
|
|
picSize = 43;
|
|
itemSize = 90;
|
|
marImg = 13;
|
|
|
|
}
|
|
|
|
RelativeLayout contentView = new RelativeLayout(mContext);
|
|
contentView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
|
contentView.setBackgroundColor(0x8c000000);
|
|
contentView.setFocusable(true);
|
|
contentView.setFocusableInTouchMode(true);
|
|
|
|
RecyclerView shareRecyclerView = new RecyclerView(mContext);
|
|
shareRecyclerView.setPadding(DisplayUtils.dip2px(mContext, 10), DisplayUtils.dip2px(mContext, paddTop), DisplayUtils.dip2px(mContext, 10), 0);
|
|
shareRecyclerView.setBackgroundColor(Color.WHITE);
|
|
|
|
//RecyclerView禁止滑动
|
|
GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, gridCount) {
|
|
@Override
|
|
public boolean canScrollVertically() {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
shareRecyclerView.setLayoutManager(gridLayoutManager);
|
|
shareRecyclerView.setAdapter(new ShareRecyclerViewAdapter());
|
|
|
|
if (shareType == 0 || shareType == 2) {
|
|
LinearLayout llBottom = (LinearLayout) view;
|
|
ViewGroup.LayoutParams layoutParams = llBottom.getLayoutParams();
|
|
layoutParams.height = DisplayUtils.dip2px(mContext, contentSize);
|
|
shareRecyclerView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
|
llBottom.addView(shareRecyclerView);
|
|
return;
|
|
}
|
|
|
|
RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
|
|
, DisplayUtils.dip2px(mContext, 106));
|
|
rlParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
|
|
contentView.addView(shareRecyclerView, rlParams);
|
|
|
|
mPopupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT
|
|
, LinearLayout.LayoutParams.MATCH_PARENT, true);
|
|
mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
|
|
mPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
|
|
|
|
contentView.setOnClickListener(v -> mPopupWindow.dismiss());
|
|
|
|
contentView.setOnKeyListener((v, keyCode, event) -> {
|
|
if (keyCode == KeyEvent.KEYCODE_BACK
|
|
&& event.getRepeatCount() == 0
|
|
&& mPopupWindow != null
|
|
&& mPopupWindow.isShowing()) {
|
|
mPopupWindow.dismiss();
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
|
|
public void shareFromWeb(Activity activity, String type) {
|
|
// base64转bitmap
|
|
byte[] decode = Base64.decode(Base64ImageHolder.INSTANCE.getImage(), Base64.DEFAULT);
|
|
Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
|
|
// 转完后重新置位
|
|
Base64ImageHolder.INSTANCE.setImage("");
|
|
|
|
this.picName = "shareImgPic.jpg";
|
|
this.mActivity = new WeakReference<>(activity);
|
|
this.shareBm = bitmap;
|
|
|
|
// 保存图片
|
|
File file = new File(activity.getExternalCacheDir().getPath() + "/ShareImg");
|
|
if (!file.isDirectory()) {
|
|
file.delete();
|
|
file.mkdirs();
|
|
}
|
|
if (!file.exists()) {
|
|
file.mkdirs();
|
|
}
|
|
writeBitmap(file.getPath(), picName, bitmap, false);
|
|
|
|
// 分享
|
|
switch (type) {
|
|
case "qq" :
|
|
qqSahre();
|
|
break;
|
|
case "qq_zone" :
|
|
qZoneSahre();
|
|
break;
|
|
case "wechat" :
|
|
wechatSahre();
|
|
break;
|
|
case "wechat_moments" :
|
|
wechatMomentsSahre();
|
|
case "save" :
|
|
String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/ghzhushou/";
|
|
writeBitmap(savePath, "gh-" + new Date().getTime() + ".jpg", shareBm, true);
|
|
break;
|
|
}
|
|
}
|
|
|
|
//QQ分享
|
|
private void qqSahre() {
|
|
Utils.toast(mContext, "分享跳转中...");
|
|
Bundle params = new Bundle();
|
|
params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,
|
|
QQShare.SHARE_TO_QQ_TYPE_IMAGE);
|
|
params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, mContext.getExternalCacheDir().getPath() + "/ShareImg/" + picName);
|
|
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, mContext.getString(R.string.app_name));
|
|
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,
|
|
QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE);
|
|
|
|
Activity activity = mActivity.get();
|
|
if (activity != null) {
|
|
mTencent.shareToQQ(activity, params, QqShareListener);
|
|
}
|
|
|
|
if (mPopupWindow == null) return;
|
|
mPopupWindow.dismiss();
|
|
}
|
|
|
|
//QQ空间分享
|
|
private void qZoneSahre() {
|
|
Utils.toast(mContext, "分享跳转中...");
|
|
Bundle params = new Bundle();
|
|
params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,
|
|
QQShare.SHARE_TO_QQ_TYPE_IMAGE);
|
|
params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, mContext.getExternalCacheDir().getPath() + "/ShareImg/" + picName);
|
|
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, mContext.getString(R.string.app_name));
|
|
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,
|
|
QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
|
|
|
|
Activity activity = mActivity.get();
|
|
if (activity != null) {
|
|
mTencent.shareToQQ(activity, params, QqShareListener);
|
|
}
|
|
|
|
if (mPopupWindow == null) return;
|
|
mPopupWindow.dismiss();
|
|
}
|
|
|
|
//微信好友分享
|
|
private void wechatSahre() {
|
|
Utils.toast(mContext, "分享跳转中...");
|
|
|
|
if (!PackageHelper.INSTANCE.getLocalPackageNameSet().contains("com.tencent.mm")) {
|
|
Utils.toast(mContext, "没安装微信,分享失败");
|
|
return;
|
|
}
|
|
|
|
//官方分享
|
|
WXImageObject imgObj = new WXImageObject();
|
|
imgObj.setImagePath(mContext.getExternalCacheDir().getPath() + "/ShareImg/" + picName);
|
|
WXMediaMessage msg = new WXMediaMessage();
|
|
msg.mediaObject = imgObj;
|
|
|
|
SendMessageToWX.Req req = new SendMessageToWX.Req();
|
|
req.transaction = buildTransaction("img");
|
|
req.message = msg;
|
|
req.scene = SendMessageToWX.Req.WXSceneSession;
|
|
|
|
Bitmap compressBp = compressBitmap(shareBm);
|
|
msg.thumbData = ImageUtils.bmpToByteArray(compressBp, true);
|
|
mIWXAPI.sendReq(req);
|
|
|
|
// //调用手机系统分享
|
|
// try {
|
|
// mContext.startActivity(IntentUtils.getWechatShareIntent(mContext, picName));
|
|
// } catch (Exception e) {
|
|
// Utils.toast(mContext, "分享失败!请检查是否已安装微信");
|
|
// e.printStackTrace();
|
|
// }
|
|
|
|
if (mPopupWindow == null) return;
|
|
mPopupWindow.dismiss();
|
|
}
|
|
|
|
//微信朋友圈分享
|
|
private void wechatMomentsSahre() {
|
|
Utils.toast(mContext, "分享跳转中...");
|
|
|
|
if (!PackageHelper.INSTANCE.getLocalPackageNameSet().contains("com.tencent.mm")) {
|
|
Utils.toast(mContext, "没安装微信,分享失败");
|
|
return;
|
|
}
|
|
|
|
WXImageObject imgObj = new WXImageObject();
|
|
imgObj.setImagePath(mContext.getExternalCacheDir().getPath() + "/ShareImg/" + picName);
|
|
WXMediaMessage msg = new WXMediaMessage();
|
|
msg.mediaObject = imgObj;
|
|
|
|
SendMessageToWX.Req req = new SendMessageToWX.Req();
|
|
|
|
req.transaction = buildTransaction("img");
|
|
req.message = msg;
|
|
req.scene = SendMessageToWX.Req.WXSceneTimeline;
|
|
|
|
Bitmap compressBp = compressBitmap(shareBm);
|
|
msg.thumbData = ImageUtils.bmpToByteArray(compressBp, true);
|
|
mIWXAPI.sendReq(req);
|
|
|
|
if (mPopupWindow == null) return;
|
|
mPopupWindow.dismiss();
|
|
}
|
|
|
|
private String buildTransaction(final String type) {
|
|
return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
|
|
}
|
|
|
|
//压缩图片
|
|
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;
|
|
}
|
|
|
|
//写到存储卡中
|
|
public void writeBitmap(String path, String name, Bitmap bitmap, boolean isToast) {
|
|
File file = new File(path);
|
|
if (!file.exists()) {
|
|
file.mkdirs();
|
|
}
|
|
|
|
File _file = new File(path, name);
|
|
if (_file.exists()) {
|
|
_file.delete();
|
|
}
|
|
FileOutputStream fos = null;
|
|
try {
|
|
fos = new FileOutputStream(_file);
|
|
if (name != null && !"".equals(name)) {
|
|
int index = name.lastIndexOf(".");
|
|
if (index != -1 && (index + 1) < name.length()) {
|
|
String extension = name.substring(index + 1).toLowerCase();
|
|
if ("png".equals(extension)) {
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
|
|
} else if ("jpg".equals(extension)
|
|
|| "jpeg".equals(extension)) {
|
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, fos);
|
|
}
|
|
if (isToast) {
|
|
Utils.toast(mContext, "图片已保存到/Pictures/ghzhushou/");
|
|
//刷新手机图片库
|
|
refreshImage(mContext, _file);
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
Utils.log("消息分享异常" + e.toString());
|
|
e.printStackTrace();
|
|
} finally {
|
|
if (fos != null) {
|
|
try {
|
|
fos.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 注意,第二个入参需要是 file 本身而不是 folder
|
|
*/
|
|
public static void refreshImage(Context context, File imageFile) {
|
|
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
|
|
Uri uri = Uri.fromFile(imageFile);
|
|
intent.setData(uri);
|
|
context.sendBroadcast(intent);
|
|
Utils.log("保存分享图片路径:" + imageFile.getAbsolutePath());
|
|
}
|
|
|
|
class ShareRecyclerViewAdapter extends RecyclerView.Adapter<ShareRecyclerViewAdapter.ViewHolder> {
|
|
|
|
@Override
|
|
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
LinearLayout linearLayout = new LinearLayout(mContext);
|
|
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(mContext, itemSize)));
|
|
linearLayout.setOrientation(LinearLayout.VERTICAL);
|
|
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
|
|
linearLayout.setBackgroundResource(R.drawable.cardview_item_style);
|
|
|
|
ImageView shareLogo = new ImageView(mContext);
|
|
LinearLayout.LayoutParams logoParams = new LinearLayout.LayoutParams(DisplayUtils.dip2px(mContext, picSize), DisplayUtils.dip2px(mContext, picSize));
|
|
logoParams.setMargins(0, DisplayUtils.dip2px(mContext, 10), 0, 0);
|
|
shareLogo.setLayoutParams(logoParams);
|
|
|
|
TextView shareLabel = new TextView(mContext);
|
|
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
layoutParams.setMargins(0, DisplayUtils.dip2px(mContext, 10), 0, 0);
|
|
shareLabel.setLayoutParams(layoutParams);
|
|
shareLabel.setGravity(Gravity.CENTER);
|
|
shareLabel.setTextColor(ContextCompat.getColor(mContext, R.color.text_3a3a3a));
|
|
shareLabel.setTextSize(marImg);
|
|
|
|
linearLayout.addView(shareLogo);
|
|
linearLayout.addView(shareLabel);
|
|
|
|
return new ViewHolder(linearLayout);
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(final ViewHolder holder, int position) {
|
|
holder.shareLogo.setImageResource(arrLogo[position]);
|
|
holder.shareLabel.setText(mArrLabel[position]);
|
|
holder.itemView.setOnClickListener(v -> {
|
|
switch (holder.getPosition()) {
|
|
case 0:
|
|
wechatSahre();
|
|
if (shareType != 2) {
|
|
// activity.finish();
|
|
}
|
|
break;
|
|
case 1:
|
|
wechatMomentsSahre();
|
|
if (shareType != 2) {
|
|
// activity.finish();
|
|
}
|
|
break;
|
|
case 2:
|
|
qqSahre();
|
|
break;
|
|
case 3:
|
|
qZoneSahre();
|
|
break;
|
|
case 4:
|
|
String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/ghzhushou/";
|
|
writeBitmap(savePath, "gh-" + new Date().getTime() + ".jpg", shareBm, true);
|
|
|
|
if (mPopupWindow == null) return;
|
|
mPopupWindow.dismiss();
|
|
// activity.finish();
|
|
break;
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return gridCount;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|