package com.haicaojie.android.util;
|
|
import android.Manifest;
|
import android.app.Activity;
|
import android.app.Dialog;
|
import android.app.ProgressDialog;
|
import android.content.ClipData;
|
import android.content.ClipboardManager;
|
import android.content.ComponentName;
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.net.Uri;
|
import android.os.Build;
|
import android.view.Gravity;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup.LayoutParams;
|
import android.view.WindowManager;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.haicaojie.android.R;
|
import com.haicaojie.android.ui.main.MainActivity;
|
import com.lcjian.library.widget.RatioLayout;
|
import com.sina.weibo.sdk.api.MultiImageObject;
|
import com.sina.weibo.sdk.api.TextObject;
|
import com.sina.weibo.sdk.api.WeiboMultiMessage;
|
import com.umeng.socialize.ShareAction;
|
import com.umeng.socialize.UMShareListener;
|
import com.umeng.socialize.bean.SHARE_MEDIA;
|
import com.umeng.socialize.media.UMImage;
|
import com.umeng.socialize.media.UMWeb;
|
|
import java.io.File;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class ShareGoodsImgDialog extends Dialog {
|
|
public ShareGoodsImgDialog(Context context) {
|
super(context);
|
}
|
|
public ShareGoodsImgDialog(Context context, int theme) {
|
super(context, theme);
|
}
|
|
public static class Builder {
|
private Activity context;
|
private String title;
|
private String message;
|
private String sinaMessage;
|
private String url;
|
private String sinaUrl;
|
private String imageurl;
|
private UMImage image;
|
private List<String> imgs = new ArrayList<>();
|
private boolean isGoods = false;
|
private String positiveButtonText;
|
private String negativeButtonText;
|
private View contentView;
|
private OnClickListener positiveButtonClickListener;
|
private OnClickListener negativeButtonClickListener;
|
|
private PermissionsChecker mPermissionsChecker; // 权限检测器
|
|
static final String[] PERMISSIONS = new String[]{
|
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
|
private static final int REQUEST_CODE = 0; // 请求码
|
|
private void startPermissionsActivity() {
|
PermissionsActivity.startActivityForResult(context, REQUEST_CODE, PERMISSIONS);
|
}
|
|
public Builder(Activity context) {
|
this.context = context;
|
}
|
|
public Builder setMessage(String message) {
|
this.message = message;
|
return this;
|
}
|
|
public Builder setSinaMessage(String message) {
|
this.sinaMessage = message;
|
return this;
|
}
|
|
public Builder setUrl(String Url) {
|
this.url = Url;
|
return this;
|
}
|
|
public Builder setSinaUrl(String Url) {
|
this.sinaUrl = Url;
|
return this;
|
}
|
|
/**
|
* Set the Dialog message from resource
|
*
|
* @return
|
*/
|
public Builder setMessage(int message) {
|
this.message = (String) context.getText(message);
|
return this;
|
}
|
|
public Builder isGoods(boolean isGoods) {
|
this.isGoods = isGoods;
|
return this;
|
}
|
|
/**
|
* Set the Dialog title from resource
|
*
|
* @param title
|
* @return
|
*/
|
public Builder setTitle(int title) {
|
this.title = (String) context.getText(title);
|
return this;
|
}
|
|
/**
|
* Set the Dialog title from String
|
*
|
* @param title
|
* @return
|
*/
|
|
public Builder setTitle(String title) {
|
this.title = title;
|
return this;
|
}
|
|
public Builder setContentView(View v) {
|
this.contentView = v;
|
return this;
|
}
|
|
public Builder setContentImage(String imageurl) {
|
this.imageurl = imageurl;
|
this.image = new UMImage(context, imageurl);
|
return this;
|
}
|
|
public Builder setImgs(List<String> imgs) {
|
this.imgs = imgs;
|
return this;
|
}
|
|
/**
|
* Set the positive button resource and it's listener
|
*
|
* @param positiveButtonText
|
* @return
|
*/
|
public Builder setPositiveButton(int positiveButtonText,
|
OnClickListener listener) {
|
this.positiveButtonText = (String) context
|
.getText(positiveButtonText);
|
this.positiveButtonClickListener = listener;
|
return this;
|
}
|
|
public Builder setPositiveButton(String positiveButtonText,
|
OnClickListener listener) {
|
this.positiveButtonText = positiveButtonText;
|
this.positiveButtonClickListener = listener;
|
return this;
|
}
|
|
public Builder setNegativeButton(int negativeButtonText,
|
OnClickListener listener) {
|
this.negativeButtonText = (String) context
|
.getText(negativeButtonText);
|
this.negativeButtonClickListener = listener;
|
return this;
|
}
|
|
public Builder setNegativeButton(String negativeButtonText,
|
OnClickListener listener) {
|
this.negativeButtonText = negativeButtonText;
|
this.negativeButtonClickListener = listener;
|
return this;
|
}
|
|
private List<File> files = new ArrayList<>();
|
|
public ShareGoodsImgDialog create() {
|
mPermissionsChecker = new PermissionsChecker(context);
|
LayoutInflater inflater = (LayoutInflater) context
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
// instantiate the dialog with the custom Theme
|
WindowManager wm = (WindowManager) context
|
.getSystemService(Context.WINDOW_SERVICE);
|
|
int width = wm.getDefaultDisplay().getWidth();
|
final ShareGoodsImgDialog dialog = new ShareGoodsImgDialog(context,
|
R.style.Dialog);
|
|
View layout = inflater.inflate(R.layout.custom_share_bottom_dialog,
|
null);
|
RatioLayout rl_qq = (RatioLayout) layout.findViewById(R.id.rl_qq);
|
RatioLayout rl_qqzone = (RatioLayout) layout.findViewById(R.id.rl_qqzone);
|
RatioLayout rl_sina = (RatioLayout) layout.findViewById(R.id.rl_sina);
|
RatioLayout rl_friends = (RatioLayout) layout.findViewById(R.id.rl_friends);
|
RatioLayout rl_wxchat = (RatioLayout) layout.findViewById(R.id.rl_wxchat);
|
dialog.addContentView(layout, new LayoutParams(
|
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
|
if (isGoods) {
|
rl_friends.setRatio(1);
|
rl_wxchat.setRatio(1);
|
rl_qq.setVisibility(View.VISIBLE);
|
rl_qqzone.setVisibility(View.VISIBLE);
|
rl_sina.setVisibility(View.VISIBLE);
|
} else {
|
rl_friends.setRatio(0.4f);
|
rl_wxchat.setRatio(0.4f);
|
rl_qq.setVisibility(View.GONE);
|
rl_qqzone.setVisibility(View.GONE);
|
rl_sina.setVisibility(View.GONE);
|
}
|
rl_friends.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
public void onClick(View arg0) {
|
shareImgs(1);
|
// UMWeb web = new UMWeb(url);
|
// web.setDescription(message);
|
// web.setTitle(title);
|
// web.setThumb(image);
|
// new ShareAction(context).withMedia(web).withText(message)
|
// .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(shareListener).share();
|
}
|
});
|
rl_qq.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
public void onClick(View arg0) {
|
shareImgs(2);
|
|
// android.util.Log.i("mResult:", "url" + url + "--message:" + message + "--title:" + title + "--imageurl" + imageurl);
|
// if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
|
// startPermissionsActivity();
|
// } else {
|
// UMWeb web = new UMWeb(url);
|
// web.setDescription(message);
|
// web.setTitle(title);
|
// web.setThumb(image);
|
// new ShareAction(context).withMedia(web).withText(message)
|
// .setPlatform(SHARE_MEDIA.QQ).setCallback(shareListener).share();
|
// }
|
}
|
});
|
rl_qqzone.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
public void onClick(View arg0) {
|
if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
|
startPermissionsActivity();
|
} else {
|
UMWeb web = new UMWeb(url);
|
web.setDescription(message);
|
web.setTitle(title);
|
UMImage shareImage = new UMImage(context, imageurl);//网络图片
|
web.setThumb(shareImage);
|
new ShareAction(context).withText(message).withMedia(web)
|
.setPlatform(SHARE_MEDIA.QZONE).setCallback(shareListener).share();
|
}
|
}
|
});
|
rl_sina.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
public void onClick(View arg0) {
|
shareImgs(3);
|
}
|
});
|
rl_wxchat.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
public void onClick(View arg0) {
|
shareImgs(0);
|
dialog.dismiss();
|
// UMWeb web = new UMWeb(url);
|
// web.setDescription(message);
|
// web.setTitle(title);
|
// web.setThumb(image);
|
// new ShareAction(context).withText(message).withMedia(web)
|
// .setPlatform(SHARE_MEDIA.WEIXIN).setCallback(shareListener).share();
|
}
|
});
|
((RatioLayout) layout.findViewById(R.id.rl_copy_link))
|
.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
public void onClick(View arg0) {
|
ClipData myClip;
|
ClipboardManager manager = (ClipboardManager) context
|
.getSystemService(context.CLIPBOARD_SERVICE);
|
myClip = ClipData.newPlainText("text", url);
|
manager.setPrimaryClip(myClip);
|
Toast.makeText(context, "复制链接成功",
|
Toast.LENGTH_SHORT).show();
|
}
|
});
|
// set the cancel button
|
if (negativeButtonText != null) {
|
((TextView) layout.findViewById(R.id.tv_dialog_cancel))
|
.setText(negativeButtonText);
|
if (negativeButtonClickListener != null) {
|
((TextView) layout.findViewById(R.id.tv_dialog_cancel))
|
.setOnClickListener(new View.OnClickListener() {
|
public void onClick(View v) {
|
negativeButtonClickListener.onClick(dialog,
|
DialogInterface.BUTTON_NEGATIVE);
|
}
|
});
|
}
|
} else {
|
// if no confirm button just set the visibility to GONE
|
layout.findViewById(R.id.tv_dialog_cancel).setVisibility(
|
View.GONE);
|
}
|
dialog.setContentView(layout);
|
|
WindowManager.LayoutParams params = dialog.getWindow()
|
.getAttributes();
|
params.gravity = Gravity.BOTTOM;
|
params.width = width;
|
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
|
dialog.getWindow().setAttributes(params);
|
dialog.setCanceledOnTouchOutside(true);
|
return dialog;
|
}
|
|
/**
|
* 创建文本消息对象。
|
*
|
* @return 文本消息对象。
|
*/
|
private TextObject getTextObj() {
|
TextObject textObject = new TextObject();
|
textObject.text = message + url;
|
textObject.title = title;
|
textObject.actionUrl = url;
|
return textObject;
|
}
|
|
/**
|
* 多张图片分享 platform--0 微信好友
|
* platform--1 朋友圈
|
* platform--2 QQ
|
* platform--3 sina
|
*
|
* @param platfrom
|
*/
|
private void shareImgs(final int platfrom) {
|
int avilibleValue = 0;
|
if (platfrom == 0 || platfrom == 1) {
|
avilibleValue = Tools.isWeixinAvilible(context);
|
} else if (platfrom == 2) {
|
avilibleValue = Tools.isQQAvilible(context);
|
} else if (platfrom == 3) {
|
avilibleValue = Tools.isSinaAvilible(context);
|
}
|
|
if ((platfrom == 0 || platfrom == 1) && (avilibleValue == 1 || avilibleValue == 2)) {
|
if (avilibleValue == 2)
|
Toast.makeText(context, "您还没有安装微信", Toast.LENGTH_SHORT).show();
|
} else if (platfrom == 2 && (avilibleValue == 1 || avilibleValue == 2)) {
|
if (avilibleValue == 2)
|
Toast.makeText(context, "您还没有安装QQ", Toast.LENGTH_SHORT).show();
|
} else if (platfrom == 3 && (avilibleValue == 1 || avilibleValue == 2)) {
|
if (avilibleValue == 2)
|
Toast.makeText(context, "您还没有安装微博", Toast.LENGTH_SHORT).show();
|
} else {
|
new Thread(new Runnable() {
|
@Override
|
public void run() {
|
for (int i = 0; i < imgs.size(); i++) {
|
File file = Tools.saveImageToSdCard(context, imgs.get(i));
|
files.add(file);
|
}
|
Intent intent = new Intent();
|
ComponentName comp;
|
if (platfrom == 0) {
|
comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
|
} else if (platfrom == 1) {
|
comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
|
intent.putExtra("Kdescription", title);
|
} else {
|
comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
|
}
|
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
|
intent.setType("image/*");
|
|
ArrayList<Uri> imageUris = new ArrayList<Uri>();
|
for (File f : files) {
|
imageUris.add(Uri.fromFile(f));
|
}
|
|
if (platfrom == 3) {
|
WeiboMultiMessage weiboMessage = new WeiboMultiMessage();
|
MultiImageObject multiImageObject = new MultiImageObject();
|
weiboMessage.textObject = getTextObj();
|
multiImageObject.setImageList(imageUris);
|
weiboMessage.multiImageObject = multiImageObject;
|
MainActivity.shareHandler.shareMessage(weiboMessage, true);
|
} else {
|
// 判断版本大于等于7.0
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);//增加读写权限
|
}
|
intent.setComponent(comp);
|
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
|
context.startActivity(intent);
|
}
|
|
intent.setComponent(comp);
|
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
|
context.startActivity(intent);
|
|
}
|
}).start();
|
}
|
|
}
|
|
UMShareListener shareListener = new UMShareListener() {
|
@Override
|
public void onStart(SHARE_MEDIA share_media) {
|
|
}
|
|
@Override
|
public void onResult(SHARE_MEDIA share_media) {
|
Toast.makeText(context, "分享成功!", Toast.LENGTH_LONG).show();
|
}
|
|
@Override
|
public void onError(SHARE_MEDIA share_media, Throwable throwable) {
|
Toast.makeText(context, "分享出错!", Toast.LENGTH_LONG).show();
|
}
|
|
@Override
|
public void onCancel(SHARE_MEDIA share_media) {
|
Toast.makeText(context, "分享取消!", Toast.LENGTH_LONG).show();
|
}
|
};
|
private ProgressDialog mDialog;
|
|
}
|
|
}
|