477 lines
17 KiB
Java
477 lines
17 KiB
Java
package com.gh.common.util;
|
|
|
|
import android.app.Activity;
|
|
import android.content.ComponentName;
|
|
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.Color;
|
|
import android.graphics.Matrix;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import android.os.Environment;
|
|
import android.os.Handler;
|
|
import android.support.v7.widget.GridLayoutManager;
|
|
import android.support.v7.widget.RecyclerView;
|
|
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.gamecenter.R;
|
|
import com.tencent.connect.share.QQShare;
|
|
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.WXImageObject;
|
|
import com.tencent.mm.sdk.openapi.WXMediaMessage;
|
|
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.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Created by khy on 2016/11/8.
|
|
*/
|
|
public class MessageShareUtils {
|
|
private static MessageShareUtils instance;
|
|
|
|
private Bitmap shareBm; //分享截图
|
|
|
|
private String picName;
|
|
|
|
private boolean isPlugin = false;
|
|
|
|
private static IWXAPI api;
|
|
private static Tencent mTencent;
|
|
|
|
private Handler handler;
|
|
|
|
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};
|
|
private String[] arrLabel = {"微信好友", "朋友圈", "QQ好友", "QQ空间", "保存"};
|
|
|
|
private PopupWindow popupWindow;
|
|
|
|
private Context context;
|
|
|
|
private Activity activity; // 用来关闭分享页面
|
|
|
|
// 适配快传成绩单分享
|
|
private int contentSize;
|
|
private int paddTop;
|
|
private int picSize;
|
|
private int itemSize;
|
|
private int gridCount;
|
|
private int marImg;
|
|
private int shareType;
|
|
|
|
public static MessageShareUtils getInstance(Context context) {
|
|
if (instance == null) {
|
|
instance = new MessageShareUtils();
|
|
mTencent = Tencent.createInstance("1104659243", context); //初始化QQ分享
|
|
api = WXAPIFactory.createWXAPI(context, "wx3ffd0785fad18396"); //初始化微信分享
|
|
}
|
|
instance.context = context;
|
|
return instance;
|
|
}
|
|
|
|
public void showShareWindows(View view, Bitmap bitmap, String picName, int shareType){
|
|
this.shareBm = bitmap;
|
|
this.picName = picName;
|
|
this.shareType = shareType;
|
|
this.activity= (Activity) context;
|
|
|
|
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(context);
|
|
contentView.setBackgroundColor(0x8c000000);
|
|
contentView.setFocusable(true);
|
|
contentView.setFocusableInTouchMode(true);
|
|
|
|
RecyclerView shareRecyclerView = new RecyclerView(context);
|
|
shareRecyclerView.setPadding(DisplayUtils.dip2px(context, 10), DisplayUtils.dip2px(context, paddTop), DisplayUtils.dip2px(context, 10), 0);
|
|
shareRecyclerView.setBackgroundColor(Color.WHITE);
|
|
|
|
//RecyclerView禁止滑动
|
|
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 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(context, contentSize);
|
|
llBottom.addView(shareRecyclerView);
|
|
return;
|
|
}
|
|
|
|
RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
|
|
, DisplayUtils.dip2px(context, 106));
|
|
rlParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
|
|
contentView.addView(shareRecyclerView,rlParams);
|
|
|
|
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;
|
|
}
|
|
});
|
|
}
|
|
|
|
public class ShareRecyclerViewAdapter extends RecyclerView.Adapter<ShareRecyclerViewAdapter.ViewHolder>{
|
|
|
|
@Override
|
|
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
LinearLayout linearLayout = new LinearLayout(context);
|
|
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, DisplayUtils.dip2px(context, itemSize)));
|
|
linearLayout.setOrientation(LinearLayout.VERTICAL);
|
|
linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
|
|
linearLayout.setBackgroundResource(R.drawable.cardview_item_style);
|
|
|
|
ImageView shareLogo = new ImageView(context);
|
|
LinearLayout.LayoutParams logoParams = new LinearLayout.LayoutParams(DisplayUtils.dip2px(context, picSize), DisplayUtils.dip2px(context, picSize));
|
|
logoParams.setMargins(0, DisplayUtils.dip2px(context, 10), 0, 0);
|
|
shareLogo.setLayoutParams(logoParams);
|
|
|
|
TextView shareLabel = new TextView(context);
|
|
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
layoutParams.setMargins(0, DisplayUtils.dip2px(context,10), 0, 0);
|
|
shareLabel.setLayoutParams(layoutParams);
|
|
shareLabel.setGravity(Gravity.CENTER);
|
|
shareLabel.setTextColor(Color.parseColor("#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(arrLabel[position]);
|
|
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View 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 (popupWindow == null) return;
|
|
popupWindow.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);
|
|
}
|
|
}
|
|
}
|
|
|
|
//QQ分享
|
|
private void qqSahre(){
|
|
Utils.toast(context,"分享跳转中...");
|
|
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, context.getExternalCacheDir().getPath() + "/ShareImg/" + picName);
|
|
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "光环助手");
|
|
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,
|
|
QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE);
|
|
mTencent.shareToQQ(
|
|
(Activity) context, params,QqShareListener);
|
|
if (popupWindow == null) return;
|
|
popupWindow.dismiss();
|
|
}
|
|
|
|
//QQ空间分享
|
|
private void qZoneSahre(){
|
|
Utils.toast(context,"分享跳转中...");
|
|
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, context.getExternalCacheDir().getPath() + "/ShareImg/" + picName);
|
|
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "光环助手");
|
|
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,
|
|
QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
|
|
mTencent.shareToQQ(
|
|
(Activity) context, params,QqShareListener);
|
|
if (popupWindow == null) return;
|
|
popupWindow.dismiss();
|
|
}
|
|
|
|
//微信好友分享
|
|
private void wechatSahre(){
|
|
Utils.toast(context,"分享跳转中...");
|
|
|
|
//官方分享
|
|
// WXImageObject imgObj = new WXImageObject();
|
|
// imgObj.setImagePath(context.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 = Util.bmpToByteArray(compressBp, true);
|
|
// api.sendReq(req);
|
|
|
|
//调用手机系统分享
|
|
try {
|
|
Intent intent = new Intent(Intent.ACTION_SEND);
|
|
intent.setType("image/*");
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + context.getExternalCacheDir().getPath() + "/ShareImg/" + picName));
|
|
intent.setComponent(new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI"));
|
|
context.startActivity(intent);
|
|
} catch (Exception e) {
|
|
Utils.toast(context,"分享失败!请检查是否已安装微信");
|
|
e.printStackTrace();
|
|
}
|
|
|
|
if (popupWindow == null) return;
|
|
popupWindow.dismiss();
|
|
}
|
|
|
|
//微信朋友圈分享
|
|
private void wechatMomentsSahre(){
|
|
Utils.toast(context,"分享跳转中...");
|
|
|
|
WXImageObject imgObj = new WXImageObject();
|
|
imgObj.setImagePath(context.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 = Util.bmpToByteArray(compressBp, true);
|
|
api.sendReq(req);
|
|
|
|
if (popupWindow == null) return;
|
|
popupWindow.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;
|
|
}
|
|
|
|
//QQ或者QQ空间分享回调处理
|
|
public IUiListener QqShareListener = new IUiListener() {
|
|
@Override
|
|
public void onComplete(Object o) {
|
|
activity.finish();
|
|
activity.overridePendingTransition(0, 0);//禁止退出Activity 动画
|
|
Utils.toast(context, "分享成功");
|
|
}
|
|
|
|
@Override
|
|
public void onError(UiError uiError) {
|
|
Utils.toast(context, "分享失败");
|
|
}
|
|
|
|
@Override
|
|
public void onCancel() {
|
|
activity.finish();
|
|
activity.overridePendingTransition(0, 0);//禁止退出Activity 动画
|
|
Utils.toast(context, "分享已取消");
|
|
}
|
|
};
|
|
|
|
//检查是否安装手机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;
|
|
}
|
|
|
|
//写到存储卡中
|
|
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(context,"成功保存到相册");
|
|
//刷新手机图片库
|
|
refreshImage(_file, context);
|
|
}
|
|
}
|
|
}
|
|
} catch (FileNotFoundException e) {
|
|
Utils.log("消息分享异常" + e.toString());
|
|
e.printStackTrace();
|
|
} finally {
|
|
if (fos != null) {
|
|
try {
|
|
fos.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void refreshImage(File _file, Context context) {
|
|
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
|
|
Uri uri = Uri.fromFile(_file);
|
|
intent.setData(uri);
|
|
context.sendBroadcast(intent);
|
|
Utils.log("保存分享图片路径:" + _file.getAbsolutePath());
|
|
}
|
|
|
|
}
|