reformat code & optimize import & reorder entries

This commit is contained in:
CsHeng
2017-04-27 14:27:11 +08:00
parent 153f33f44b
commit 9432e34571
671 changed files with 33616 additions and 34861 deletions

View File

@ -1,46 +1,24 @@
package com.gh.common.util;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.*;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.*;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
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 android.view.*;
import android.widget.*;
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.openapi.*;
import com.tencent.mm.sdk.platformtools.Util;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.tencent.tauth.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.util.Date;
import java.util.List;
@ -49,16 +27,11 @@ import java.util.List;
*/
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 Bitmap shareBm; //分享截图
private String picName;
private boolean isPlugin = false;
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空间", "保存"};
@ -68,7 +41,27 @@ public class MessageShareUtils {
private Context context;
private Activity activity; // 用来关闭分享页面
//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, "分享已取消");
}
};
// 适配快传成绩单分享
private int contentSize;
private int paddTop;
@ -88,11 +81,34 @@ public class MessageShareUtils {
return instance;
}
public void showShareWindows(View view, Bitmap bitmap, String picName, int shareType){
//检查是否安装手机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 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());
}
public void showShareWindows(View view, Bitmap bitmap, String picName, int shareType) {
this.shareBm = bitmap;
this.picName = picName;
this.shareType = shareType;
this.activity= (Activity) context;
this.activity = (Activity) context;
if (shareType == 2) {
contentSize = 75;
@ -122,7 +138,7 @@ public class MessageShareUtils {
shareRecyclerView.setBackgroundColor(Color.WHITE);
//RecyclerView禁止滑动
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, gridCount){
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, gridCount) {
@Override
public boolean canScrollVertically() {
return false;
@ -144,7 +160,7 @@ public class MessageShareUtils {
RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
, DisplayUtils.dip2px(context, 106));
rlParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
contentView.addView(shareRecyclerView,rlParams);
contentView.addView(shareRecyclerView, rlParams);
popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT
, LinearLayout.LayoutParams.MATCH_PARENT, true);
@ -172,7 +188,173 @@ public class MessageShareUtils {
});
}
public class ShareRecyclerViewAdapter extends RecyclerView.Adapter<ShareRecyclerViewAdapter.ViewHolder>{
//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;
}
//写到存储卡中
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 class ShareRecyclerViewAdapter extends RecyclerView.Adapter<ShareRecyclerViewAdapter.ViewHolder> {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
@ -189,7 +371,7 @@ public class MessageShareUtils {
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);
layoutParams.setMargins(0, DisplayUtils.dip2px(context, 10), 0, 0);
shareLabel.setLayoutParams(layoutParams);
shareLabel.setGravity(Gravity.CENTER);
shareLabel.setTextColor(Color.parseColor("#3a3a3a"));
@ -208,7 +390,7 @@ public class MessageShareUtils {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (holder.getPosition()){
switch (holder.getPosition()) {
case 0:
wechatSahre();
if (shareType != 2) {
@ -234,7 +416,7 @@ public class MessageShareUtils {
if (popupWindow == null) return;
popupWindow.dismiss();
activity.finish();
break;
break;
}
}
@ -246,7 +428,7 @@ public class MessageShareUtils {
return gridCount;
}
public class ViewHolder extends RecyclerView.ViewHolder{
public class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
TextView shareLabel;
ImageView shareLogo;
@ -260,216 +442,4 @@ public class MessageShareUtils {
}
}
//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());
}
}