wpc
2018-11-27 680fbc9e73da3e11988557cf88fd935efd3e0b1e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package com.haicaojie.android.util;
 
import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
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.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
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.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.haicaojie.android.R;
import com.haicaojie.android.ShoppingApi;
import com.haicaojie.android.ui.main.MainActivity;
import com.lcjian.library.util.common.StringUtils;
import com.lcjian.library.widget.RatioLayout;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.sina.weibo.sdk.api.ImageObject;
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 ShareFriendsDialog extends Dialog {
 
    public ShareFriendsDialog(Context context) {
        super(context);
    }
 
    public ShareFriendsDialog(Context context, int theme) {
        super(context, theme);
    }
 
    public static class Builder {
        private Activity context;
        private String title;
        private String message;
        private String url;
        private String id;
        private String img;
        private String positiveButtonText;
        private String negativeButtonText;
        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 setUrl(String Url) {
            this.url = Url;
            return this;
        }
 
        public Builder setId(String id) {
            this.id = id + "";
            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 setImgs(String imgs) {
            this.img = 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<>();
 
        ShareFriendsDialog dialog;
 
        public ShareFriendsDialog 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();
            dialog = new ShareFriendsDialog(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);
            final RatioLayout rl_friends = (RatioLayout) layout.findViewById(R.id.rl_friends);
            final RatioLayout rl_wxchat = (RatioLayout) layout.findViewById(R.id.rl_wxchat);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//            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) {
                    if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
                        startPermissionsActivity();
                    } else {
                        UMWeb web = new UMWeb(url);
                        web.setDescription(message);
                        web.setTitle(title);
                        UMImage shareImage;
                        if (StringUtils.isEmpty(img)) {
                            shareImage = new UMImage(context, R.drawable.ic_launcher);//本地图片
                        } else {
                            shareImage = new UMImage(context, img);//网络图片
                        }
 
                        web.setThumb(shareImage);
                        new ShareAction(context).withText(message).withMedia(web)
                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(shareListener).share();
                        dialog.dismiss();
                    }
                }
            });
            rl_qq.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;
                        if (StringUtils.isEmpty(img)) {
                            shareImage = new UMImage(context, R.drawable.ic_launcher);//本地图片
                        } else {
                            shareImage = new UMImage(context, img);//网络图片
                        }
                        web.setThumb(shareImage);
                        new ShareAction(context).withText(message).withMedia(web)
                                .setPlatform(SHARE_MEDIA.QQ).setCallback(shareListener).share();
                    }
                    dialog.dismiss();
                }
            });
            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;
                        if (StringUtils.isEmpty(img)) {
                            shareImage = new UMImage(context, R.drawable.ic_launcher);//本地图片
                        } else {
                            shareImage = new UMImage(context, img);//网络图片
                        }
                        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) {
                    WeiboMultiMessage weiboMessage = new WeiboMultiMessage();
                    weiboMessage.textObject = getTextObj();
                    weiboMessage.imageObject = getImageObj();
                    MainActivity.shareHandler.shareMessage(weiboMessage, true);
//                    if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
//                        startPermissionsActivity();
//                    } else {
//                        UMWeb web = new UMWeb(url);
//                        web.setTitle(title);
//                        web.setDescription(message);
//                        UMImage shareImage = new UMImage(context, R.drawable.ic_launcher);//网络图片
//                        web.setThumb(shareImage);
//                        new ShareAction(context).withText(message).withMedia(web)
//                                .setPlatform(SHARE_MEDIA.SINA).setCallback(shareListener).share();
//                    }
 
                    dialog.dismiss();
                }
            });
            rl_wxchat.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;
                        if (StringUtils.isEmpty(img)) {
                            shareImage = new UMImage(context, R.drawable.ic_launcher);//本地图片
                        } else {
                            shareImage = new UMImage(context, img);//网络图片
                        }
                        web.setThumb(shareImage);
                        new ShareAction(context).withText(message).withMedia(web)
                                .setPlatform(SHARE_MEDIA.WEIXIN).setCallback(shareListener).share();
                    }
                    dialog.dismiss();
                }
            });
            ((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;
        }
 
        /**
         * 多张图片分享 platform--0 微信好友
         * platform--1 朋友圈
         * platform--2 QQ
         *
         * @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 {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        File file = Tools.saveImageToSdCard(context, img);
                        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));
                        }
                        intent.setComponent(comp);
                        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
                        context.startActivity(intent);
                        handler.sendEmptyMessage(1);
                    }
                }).start();
            }
        }
 
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (dialog.isShowing())
                    dialog.dismiss();
                ShoppingApi.addShareCount(context, id, new JsonHttpResponseHandler());
            }
        };
 
        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();
            }
        };
 
        /**
         * 创建文本消息对象。
         *
         * @return 文本消息对象。
         */
        private TextObject getTextObj() {
            TextObject textObject = new TextObject();
            textObject.text = message + url;
            textObject.title = title;
            textObject.actionUrl = url;
            return textObject;
        }
 
        /**
         * 创建图片消息对象。
         *
         * @return 图片消息对象。
         */
        Bitmap bitmap;
 
        private ImageObject getImageObj() {
            ImageObject imageObject = new ImageObject();
            //设置缩略图。 注意:最终压缩过的缩略图大小不得超过 32kbc
            Glide.with(context).load(StringUtils.isEmpty(img) ? R.drawable.ic_launcher : img).asBitmap().into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    bitmap = resource;
                }
            });
            imageObject.setImageObject(bitmap);
            return imageObject;
        }
 
    }
 
 
}