admin
2020-08-12 7087e87b75cc4408eee9cace87b4ddd9b7c5a8ce
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
package com.weikou.beibeivideo.ui.media;
 
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
 
import com.bumptech.glide.Glide;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lcjian.library.RetainViewFragment;
import com.lcjian.library.util.SingleToast;
import com.lcjian.library.util.common.DimenUtils;
import com.lcjian.library.util.common.StringUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.qq.e.ads.cfg.VideoOption;
import com.qq.e.ads.nativ.ADSize;
import com.qq.e.ads.nativ.NativeExpressAD;
import com.qq.e.ads.nativ.NativeExpressADView;
import com.qq.e.comm.util.AdError;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMWeb;
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
import com.weikou.beibeivideo.BeibeiVideoAPI;
import com.weikou.beibeivideo.db.WatchHistoryTable;
import com.weikou.beibeivideo.entity.Follow;
import com.weikou.beibeivideo.entity.Play;
import com.weikou.beibeivideo.entity.PlayUrl;
import com.weikou.beibeivideo.entity.Playlocation;
import com.weikou.beibeivideo.entity.VideoDetailInfo;
import com.weikou.beibeivideo.entity.VideoInfo;
import com.weikou.beibeivideo.entity.VideoResource;
import com.weikou.beibeivideo.ui.login.LoginActivity;
import com.weikou.beibeivideo.util.BeibeiConstant;
import com.yeshi.buwanshequ.R;
 
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.List;
 
import de.greenrobot.event.EventBus;
 
/**
 * 节目选集列表,可设置电视剧6列;电影,综艺1列
 */
public class EpisodeFragment extends RetainViewFragment implements View.OnClickListener {
    private int mStart;
    private int mEnd;
    private int mPlayingPosition;
    private VideoInfo mVideoInfo = null;
    private static GridView gv_episode;
    private static EpisodeAdapter episodeAdapter;
    private static EpisodeAdapter2 episodeAdapter2;
 
    private TextView tv_add_to_favourite;
    private LinearLayout fl_add_to_favourite;
    private LinearLayout fl_report;
    private ImageView iv_report;
    private ImageView iv_favourite;
 
    private TextView tv_offline_cache;
 
    private LinearLayout fl_offline_cache;
 
    private ImageView iv_moive_img;
 
    private TextView tv_star_name, tv_update_time, tv_cancle_follow;
 
    private LinearLayout ll_add_attention;
 
    private FrameLayout fl_native_ad1;
 
    public static DisplayImageOptions option = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.from_other)
            .showImageOnFail(R.drawable.from_other)
            .showImageOnLoading(R.drawable.from_other)
            .resetViewBeforeLoading(false) // default
            .cacheInMemory(true) // default
            .cacheOnDisk(true) // default
            .considerExifParams(false) // default
            .imageScaleType(ImageScaleType.EXACTLY) // default
            .bitmapConfig(Bitmap.Config.ARGB_8888) // default
            .handler(new Handler()) // default
            .build();
 
    public static EpisodeFragment newInstance(VideoInfo videoInfo,
                                              int playingPosition, int start, int end) {
        EpisodeFragment episodeFragment = new EpisodeFragment();
        Bundle args = new Bundle();
        args.putSerializable("video_info", videoInfo);
        args.putInt("playing_position", playingPosition);
        args.putInt("start", start);
        args.putInt("end", end);
        episodeFragment.setArguments(args);
        return episodeFragment;
    }
 
    @Override
    public int getContentResource() {
        return R.layout.fragment_episode;
    }
 
    @Override
    public void onCreateView(View contentView, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        mVideoInfo = (VideoInfo) getArguments()
                .getSerializable("video_info");
        mPlayingPosition = getArguments().getInt("playing_position");
        mStart = getArguments().getInt("start");
        mEnd = getArguments().getInt("end");
 
        ll_add_attention = (LinearLayout) contentView.findViewById(R.id.ll_add_attention);
        if (mVideoInfo.getAttention() == null) {
            ll_add_attention.setVisibility(View.GONE);
        } else {
            ll_add_attention.setVisibility(View.VISIBLE);
            iv_moive_img = (ImageView) contentView.findViewById(R.id.iv_moive_img);
            tv_star_name = (TextView) contentView.findViewById(R.id.tv_star_name);
            tv_update_time = (TextView) contentView.findViewById(R.id.tv_update_time);
            tv_cancle_follow = (TextView) contentView.findViewById(R.id.tv_cancle_follow);
            try {
                Glide.with(getActivity().getApplicationContext()).load(mVideoInfo.getAttention().getMoviePicture()).error(R.drawable.ic_default).placeholder(R.drawable.ic_default).centerCrop().into(iv_moive_img);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            tv_star_name.setText(mVideoInfo.getAttention().getMovieName() + "");
            tv_update_time.setText(mVideoInfo.getAttention().getUpdateInfo() + "");
            if (mVideoInfo.getAttention().isAttention()) {
                tv_cancle_follow.setText("已关注");
                tv_cancle_follow.setBackgroundResource(R.color.gray);
            } else {
                tv_cancle_follow.setText("关注");
                tv_cancle_follow.setBackgroundResource(R.color.yellow);
            }
 
            tv_cancle_follow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedPreferences sp = getContext().getSharedPreferences("user", Context.MODE_PRIVATE);
                    String uid = sp.getString("uid", "");
                    String loginid = sp.getString("LoginUid", "");
                    if (StringUtils.isEmpty(loginid)) {
                        Toast.makeText(ll_add_attention.getContext(), "登录成功后才能进行关注!", Toast.LENGTH_LONG).show();
                        getContext().startActivity(new Intent(ll_add_attention.getContext(), LoginActivity.class));
                        return;
                    }
                    if (mVideoInfo.getAttention().isAttention()) {//取消关注
                        cancleAttention(uid, loginid);
                    } else {//关注
                        addAttention(uid, loginid);
                    }
                }
            });
        }
 
        fl_native_ad1 = (FrameLayout) contentView
                .findViewById(R.id.fl_native_ad_1);
        gv_episode = (GridView) contentView.findViewById(R.id.gv_fragment_episode);
        gv_episode.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        int padding = DimenUtils.dipToPixels(8, gv_episode.getContext());
        gv_episode.setPadding(padding, padding, padding, padding);
        if (mVideoInfo.getShowType() == 1) {// 综艺或者电影
            gv_episode.setNumColumns(1);
        } else if (mVideoInfo.getShowType() == 2) {
            // 电视剧什么的
            gv_episode.setNumColumns(5);
        }
        gv_episode.setHorizontalSpacing(padding);
        gv_episode.setVerticalSpacing(padding);
        gv_episode.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        gv_episode.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                VideoDetailInfo videoDetailInfo = (VideoDetailInfo) parent
                        .getItemAtPosition(position);
                if (selectedUrl == null) {
                    return;
                }
                mPlayingPosition = position;
                episodeAdapter = new EpisodeAdapter(mVideoInfo,
                        mPlayingPosition);
                episodeAdapter2 = new EpisodeAdapter2(mVideoInfo,
                        mPlayingPosition);
                refresh();
                Playlocation playlocation = new Playlocation();
                playlocation.setPosition(position);
                EventBus.getDefault().post(playlocation);
                getUrl(videoDetailInfo);
            }
        });
        fl_add_to_favourite = (LinearLayout) contentView
                .findViewById(R.id.fl_add_to_favourite);
        tv_add_to_favourite = (TextView) contentView
                .findViewById(R.id.tv_add_to_favourite);
        fl_add_to_favourite.setOnClickListener(this);
        contentView.findViewById(R.id.fl_share).setOnClickListener(this);
        fl_report = (LinearLayout) contentView.findViewById(R.id.fl_report);
        iv_report = (ImageView) contentView.findViewById(R.id.iv_report_more);
        iv_favourite = (ImageView) contentView.findViewById(R.id.iv_add_to_favourite);
        fl_report.setOnClickListener(this);
        tv_offline_cache = (TextView) contentView.findViewById(R.id.tv_offline_cache);
        fl_offline_cache = (LinearLayout) contentView
                .findViewById(R.id.fl_offline_cache);
        fl_offline_cache.setOnClickListener(this);
 
        episodeAdapter = new EpisodeAdapter(mVideoInfo, mPlayingPosition);
        episodeAdapter2 = new EpisodeAdapter2(mVideoInfo, mPlayingPosition);
        refresh();
        isCollect();
        loadAD1();
        setFrom(mVideoInfo);
    }
 
 
    public void onEventMainThread(Playlocation playlocation) {
        mPlayingPosition = playlocation.getPosition();
        episodeAdapter = new EpisodeAdapter(mVideoInfo,
                mPlayingPosition);
        episodeAdapter2 = new EpisodeAdapter2(mVideoInfo,
                mPlayingPosition);
        refresh();
    }
 
    private void addAttention(String uid, String loginId) {
        BeibeiVideoAPI.addAttention(ll_add_attention.getContext(), uid, loginId, mVideoInfo.getId(), new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optBoolean("IsPost")) {
                    SingleToast.showToast(ll_add_attention.getContext(), "添加关注成功!");
                    mVideoInfo.getAttention().setAttention(true);
                    tv_cancle_follow.setText("已关注");
                    tv_cancle_follow.setBackgroundColor(getResources().getColor(R.color.gray));
                }
            }
        });
    }
 
    private void cancleAttention(String uid, String loginId) {
        BeibeiVideoAPI.cancelAttention(ll_add_attention.getContext(), uid, loginId, mVideoInfo.getId(), new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optBoolean("IsPost")) {
                    SingleToast.showToast(ll_add_attention.getContext(), "取消关注成功!");
                    mVideoInfo.getAttention().setAttention(false);
                    tv_cancle_follow.setText("关注");
                    tv_cancle_follow.setBackgroundColor(getResources().getColor(R.color.yellow));
                }
            }
        });
    }
 
    @Override
    public void onResume() {
        super.onResume();
    }
 
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
 
    @Override
    public void onPause() {
        super.onPause();
    }
 
    private void getUrl(VideoDetailInfo info) {
        SharedPreferences preferences = iv_report.getContext().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        String uid = preferences.getString("uid", "");
        BeibeiVideoAPI.getPlayUrl(iv_report.getContext(), uid, info.getType(), mVideoInfo.getId(),
                info.getId(), selectedUrl.getId(), info.geteId(),
                new BasicTextHttpResponseHandler() {
 
                    @Override
                    public void onSuccessPerfect(int statusCode,
                                                 Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.getBoolean("IsPost")) {
                            Gson gson = new GsonBuilder().setFieldNamingPolicy(
                                    FieldNamingPolicy.UPPER_CAMEL_CASE)
                                    .create();
                            VideoResource resource = gson.fromJson(
                                    jsonObject.getJSONObject("Data")
                                            .getJSONObject("Resource")
                                            .toString(),
                                    new TypeToken<VideoResource>() {
                                    }.getType());
                            PlayUrl info = new PlayUrl();
                            info.setResource(resource);
                            info.setPlayType(Integer.parseInt(jsonObject
                                    .getJSONObject("Data")
                                    .optString("PlayType")));
                            info.setUrl(jsonObject.getJSONObject("Data")
                                    .optString("Url"));
                            info.setParams(jsonObject.getJSONObject("Data")
                                    .optString("Params"));
                            Play play = new Play();
                            play.setPlayUrl(info);
                            EventBus.getDefault().post(play);
                        }
                    }
 
                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                        super.onFailure(statusCode, headers, responseString, throwable);
 
                    }
 
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        if (pd != null && pd.isShowing())
                            pd.dismiss();
                    }
                });
    }
 
    public boolean isPlaying() {
        return mStart <= mPlayingPosition && mPlayingPosition < mEnd;
    }
 
    /**
     * 刷新adapter
     */
    public static void refresh() {
        if (VideoEpisodeFragment.isShowMore && episodeAdapter != null) {// 展示更多
            gv_episode.setAdapter(episodeAdapter);
        } else if ((!VideoEpisodeFragment.isShowMore) && episodeAdapter2 != null) {
            gv_episode.setAdapter(episodeAdapter2);
        }
 
    }
 
    // 加载播放页第一条广告,先设置加载上下文环境和条件
    private void loadAD1() {
        NativeExpressAD nativeExpressAD = new NativeExpressAD(ll_add_attention.getContext(), new ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT),
                BeibeiConstant.GDT_ID, BeibeiConstant.GDT_PLAYER_NATIVE1, new NativeExpressAD.NativeExpressADListener() {
            @Override
            public void onADLoaded(final List<NativeExpressADView> list) {
                tv_add_to_favourite.post(new Runnable() {
                    @Override
                    public void run() {
                        if (null != list && list.size() > 0) {
                            NativeExpressADView adView = list.get(0);
                            adView.render();
                            FrameLayout parent = (FrameLayout) adView.getParent();
                            if (parent != null) {
                                parent.removeAllViews();
                            }
                            fl_native_ad1.addView(adView);
                        }
                    }
                });
 
            }
 
            @Override
            public void onRenderFail(NativeExpressADView nativeExpressADView) {
 
            }
 
            @Override
            public void onRenderSuccess(NativeExpressADView nativeExpressADView) {
 
            }
 
            @Override
            public void onADExposure(NativeExpressADView nativeExpressADView) {
            }
 
            @Override
            public void onADClicked(NativeExpressADView nativeExpressADView) {
 
            }
 
            @Override
            public void onADClosed(NativeExpressADView nativeExpressADView) {
 
            }
 
            @Override
            public void onADLeftApplication(NativeExpressADView nativeExpressADView) {
 
            }
 
            @Override
            public void onADOpenOverlay(NativeExpressADView nativeExpressADView) {
 
            }
 
            @Override
            public void onADCloseOverlay(NativeExpressADView nativeExpressADView) {
 
            }
 
            @Override
            public void onNoAD(AdError adError) {
            }
        }); // 传入Activity
        // 注意:如果您在联盟平台上新建原生模板广告位时,选择了支持视频,那么可以进行个性化设置(可选)
        nativeExpressAD.setVideoOption(new VideoOption.Builder()
                .setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // WIFI 环境下可以自动播放视频
                .setAutoPlayMuted(true) // 自动播放时为静音
                .build()); //
        nativeExpressAD.loadAD(2);
    }
 
 
    @Override
    public void onClick(View v) {
        SharedPreferences preferences = iv_report.getContext()
                .getSharedPreferences("user", Context.MODE_PRIVATE);
        switch (v.getId()) {
            case R.id.fl_add_to_favourite:
                final String loginUid = preferences.getString("LoginUid", "");
                if (StringUtils.isEmpty(loginUid)) {
                    SingleToast.showToast(ll_add_attention.getContext(), "登录后才能收藏");
                    startActivity(new Intent(ll_add_attention.getContext(), LoginActivity.class));
                    break;
                }
                String uid = preferences.getString("uid", "");
                BeibeiVideoAPI.isCollect(iv_report.getContext(), uid, mVideoInfo.getId(),
                        mVideoInfo.getThirdType(),
                        new BasicTextHttpResponseHandler() {
 
                            @Override
                            public void onSuccessPerfect(int statusCode,
                                                         Header[] headers, JSONObject jsonObject)
                                    throws Exception {
                                if (jsonObject.getBoolean("IsPost")) {
                                    getScoreCollect(loginUid, "0", mVideoInfo.getThirdType());
                                } else {
                                    getScoreCollect(loginUid, "1", mVideoInfo.getThirdType());
                                }
                            }
                        });
                break;
            case R.id.fl_report:
                if (popupWindow.isShowing()) {
                    // bug:listview 中的isShowing()可以正确获取而到了这里就一直不进入,原因是pop没设置焦点获取
                    popupWindow.dismiss();
                } else {// 出现在线下,并且居右
                    if (selectedUrl == null || selectedUrl.getPicture() == null) {
 
                    } else {
                        popupWindow.showAsDropDown(v, 0, 0);
                    }
                }
                break;
            case R.id.fl_offline_cache:
                Toast.makeText(ll_add_attention.getContext(), "暂时无法缓存!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.fl_share:
                String shareContent = getShareContent();
                UMWeb web = new UMWeb(getShareUrl());
                String shareTitle = getResources().getString(R.string.app_name);
                web.setTitle(shareTitle);
                web.setDescription(shareContent);
                new ShareAction(getActivity()).withText(shareContent).withMedia(web)
                        .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
                        .setCallback(shareListener).open();
        }
    }
 
    UMShareListener shareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
 
        }
 
        @Override
        public void onResult(SHARE_MEDIA share_media) {
            Toast.makeText(iv_favourite.getContext(), "分享成功!", Toast.LENGTH_LONG).show();
        }
 
        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            if (!UMShareAPI.get(ll_add_attention.getContext()).isInstall(getActivity(), share_media)) {
                Toast.makeText(iv_favourite.getContext(), share_media == SHARE_MEDIA.QQ ? "没有安装QQ" : "没有安装微信", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(iv_favourite.getContext(), "分享出错!", Toast.LENGTH_LONG).show();
            }
        }
 
        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            Toast.makeText(iv_favourite.getContext(), "分享取消!", Toast.LENGTH_LONG).show();
        }
    };
 
    private String getShareContent() {
        return "我在" + iv_favourite.getContext().getResources().getString(R.string.app_name)
                + "看了《" + mVideoInfo.getName()
                + "》影片,非常不错,大家一起来看吧!" + getShareUrl();
    }
 
    private String getShareUrl() {
        SharedPreferences preferences = iv_favourite.getContext().getSharedPreferences("user",
                Context.MODE_PRIVATE);
        return preferences.getString("share_url", "http://yy.umgotv.com");
    }
 
    List<VideoResource> urlList;
    VideoResource selectedUrl = null;
 
    public void onEventMainThread(VideoInfo videoInfo) {
        mVideoInfo = videoInfo;
        setFrom(videoInfo);
        if (mVideoInfo.isCanSave()) {
//            fl_offline_cache.setEnabled(true);
            // tv_offline_cache.setText("缓存");
        } else {
//            fl_offline_cache.setEnabled(false);
            // tv_offline_cache.setText("无法缓存");
        }
        fl_add_to_favourite.setEnabled(true);
        fl_report.setEnabled(true);
        if (VideoEpisodeFragment.isShowMore) {// 展示更多
            gv_episode.setAdapter(episodeAdapter);
        } else {
            gv_episode.setAdapter(episodeAdapter2);
        }
//        isCollect();
    }
 
    private void setFrom(VideoInfo info) {
        if (urlList == null)
            urlList = new ArrayList<VideoResource>();
        urlList.clear();
        for (int i = 0; i < info.getResourceList().size(); i++) {
            if (info.getResourceList().get(i).isChecked()) {
                selectedUrl = info.getResourceList().get(i);
            }
        }
        EventBus.getDefault().post(selectedUrl);
        tv_add_to_favourite.postDelayed(new Runnable() {
 
            @Override
            public void run() {
                EventBus.getDefault().post(selectedUrl);
            }
        }, 1000);
        urlList.addAll(info.getResourceList());
        initResource();
    }
 
    private PopupWindow popupWindow;
 
    private ProgressDialog pd;
 
    private void initResource() {
 
        if (mVideoInfo.getResourceList() != null
                && mVideoInfo.getResourceList().size() > 0) {
            // tv_report.setText("来源:");
            iv_report.setVisibility(View.VISIBLE);
            if (selectedUrl == null || selectedUrl.getPicture() == null) {
                Glide.with(getActivity().getApplicationContext()).load(R.drawable.from_other).into(iv_report);
            } else {
//                Glide.with(getActivity().getApplicationContext()).load(selectedUrl.getPicture()).placeholder(R.drawable.from_other).error(R.drawable.from_other).into(iv_report);
                ImageLoader.getInstance().displayImage(
                        selectedUrl.getPicture(), iv_report, option);
            }
            popupWindow = new PopupWindow(
                    com.weikou.beibeivideo.util.DimenUtils.dip2px(iv_report.getContext(), 120),
                    android.view.WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setOutsideTouchable(true);// 点击外部可点击
            popupWindow.setBackgroundDrawable(new ColorDrawable(0));// 设置背景
            popupWindow.setAnimationStyle(R.style.PopupAnimation);
            popupWindow.setFocusable(true);// 获取焦点
 
            ListView lv = new ListView(iv_report.getContext());
            ListView.LayoutParams param = new ListView.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            lv.setLayoutParams(param);
            lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    if (popupWindow.isShowing()) {
                        popupWindow.dismiss();
                    }
                    if (selectedUrl.getId() != urlList.get(position).getId()) {
                        pd = new ProgressDialog(ll_add_attention.getContext());
                        pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
                        pd.setCancelable(true);// 设置是否可以通过点击Back键取消
                        pd.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
                        pd.setMessage("网络加载中·······");
                        pd.show();
                        getVideoDetail(mVideoInfo.getId(), urlList
                                .get(position).getId(), mVideoInfo
                                .getThirdType(), true);
                    }
                    selectedUrl = urlList.get(position);
                    iv_report.setVisibility(View.VISIBLE);
                    if (selectedUrl == null || selectedUrl.getPicture() == null) {
//                        ImageLoader.getInstance().displayImage(
//                                "drawable://" + R.drawable.from_other,
//                                iv_report);
                        Glide.with(getActivity().getApplicationContext()).load(R.drawable.from_other).into(iv_report);
                    } else {
                        Glide.with(getActivity().getApplicationContext()).load(selectedUrl.getPicture()).placeholder(R.drawable.from_other).error(R.drawable.from_other).into(iv_report);
//                        ImageLoader.getInstance().displayImage(
//                                selectedUrl.getPicture(), iv_report, option);
                    }
 
                }
            });
            lv.setAdapter(new ResourceAdapter(urlList));
            lv.setDividerHeight(0);
            popupWindow.setContentView(lv);
            /*
             * Window dialogWindow = dialog.getWindow(); dialogWindow.get
             */
        } else {
            iv_report.setVisibility(View.GONE);
            // tv_report.setText("其它");
        }
 
    }
 
    private class ResourceAdapter extends BaseAdapter {
        List<VideoResource> reList;
 
        public ResourceAdapter(List<VideoResource> list) {
            reList = list;
        }
 
        @Override
        public int getCount() {
            return reList.size();
        }
 
        @Override
        public Object getItem(int position) {
            return reList.get(position);
        }
 
        @Override
        public long getItemId(int position) {
            return position;
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(ll_add_attention.getContext()).inflate(
                    R.layout.item_resource, null);
            ImageView iv = (ImageView) view.findViewById(R.id.iv_resource);
            TextView tv = (TextView) view.findViewById(R.id.tv_resource);
            // if (reList.get(position) == null
            // || reList.get(position).getPicture() == null) {
            // ImageLoader.getInstance().displayImage(
            // "drawable://" + R.drawable.from_other, iv);
            // } else {
            // ImageLoader.getInstance().displayImage(
            // reList.get(position).getPicture(), iv, option);
            // }
            try {
                Glide.with(getActivity().getApplicationContext())
                        .load(reList.get(position).getPicture().contains("http://") ? reList
                                .get(position).getPicture() : BeibeiConstant.HOST
                                + "/BuWan/" + reList.get(position).getPicture())
                        .placeholder(R.drawable.from_other)
                        .error(R.drawable.from_other).crossFade().into(iv);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            if (reList.get(position) == null
                    || StringUtils.isEmpty(reList.get(position).getName())) {
                tv.setText("其它");
            } else {
                tv.setText(reList.get(position).getName());
            }
            return view;
        }
    }
 
    private void getVideoDetail(String videoId, final String resourceId,
                                String videoThirdType, final boolean isSetup) {
        SharedPreferences preferences = iv_report.getContext().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        String uid = preferences.getString("uid", "");
        String loginid = preferences.getString("LoginUid", "");
        BeibeiVideoAPI.getVideoDetail(iv_report.getContext(), uid, resourceId, videoId, loginid,
                videoThirdType, new BasicTextHttpResponseHandler() {
 
                    @Override
                    public void onStart() {
                        super.onStart();
                    }
 
                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                                          String responseString, Throwable throwable) {
                        super.onFailure(statusCode, headers, responseString,
                                throwable);
 
                    }
 
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        if (pd != null && pd.isShowing())
                            pd.dismiss();
                    }
 
                    @Override
                    public void onSuccessPerfect(int statusCode,
                                                 Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        try {
                            if (getActivity().isDestroyed()) {
                                return;
                            }
                        } catch (NoSuchMethodError e) {
                            e.printStackTrace();
                        }
                        if (jsonObject.getBoolean("IsPost")) {
                            Gson gson = new GsonBuilder().setFieldNamingPolicy(
                                    FieldNamingPolicy.UPPER_CAMEL_CASE)
                                    .create();
 
                            final VideoInfo videoInfo = gson
                                    .fromJson(jsonObject.getJSONObject("Data")
                                                    .toString(),
                                            new TypeToken<VideoInfo>() {
                                            }.getType());
                            // 附加字段
                            if (jsonObject.optJSONObject("Extra1") != null) {
                                if (jsonObject.optJSONObject("Extra1").optJSONObject("Attention") != null) {
                                    JSONObject obj = jsonObject.optJSONObject("Extra1").optJSONObject("Attention");
                                    Follow attention = new Follow();
                                    attention.setMovieName(obj.optString("Name"));
                                    attention.setMoviePicture(obj.optString("Picture"));
                                    attention.setUpdateInfo(obj.optString("UpdateInfo"));
                                    attention.setAttention(Boolean.parseBoolean(obj.optString("Name")));
                                    videoInfo.setAttention(attention);
                                }
                            }
                            JSONArray extraData = jsonObject
                                    .optJSONArray("Extra");
                            if (extraData != null)
                                videoInfo.setExtraData(extraData.toString());
 
                            if (videoInfo.getVideoDetailList() == null
                                    || videoInfo.getVideoDetailList().isEmpty()) {
                                Toast.makeText(iv_report.getContext(), "影片已删除",
                                        Toast.LENGTH_LONG).show();
                                getActivity().finish();
                                return;
                            }
                            // videoInfo.setSave(jsonObject.getJSONObject("Data").optBoolean("Save"));
                            if (isSetup) {
                                mVideoInfo.getVideoDetailList().clear();
                                mVideoInfo.setVideoDetailList(videoInfo
                                        .getVideoDetailList());
                                EventBus.getDefault().post(videoInfo);
                                int position = 0;
//                                boolean isFromWatchHistory = false;
//                                isFromWatchHistory = getArguments().getBoolean("isFromWatchHistory", false);// 是否从观看记录点击过来
                                Cursor cursor = gv_episode.getContext().getContentResolver().query(
                                        WatchHistoryTable.CONTENT_URI, null,
                                        WatchHistoryTable.VIDEO_ID + " = ? ",
                                        new String[]{videoInfo.getId()}, null);
//                                Cursor cursor = getActivity().getConentResolver().query(WatchHistoryTable.CONTENT_URI, null, WatchHistoryTable.VIDEO_ID + " = ? ", new String[]{videoInfo.getId()}, null);
                                cursor.moveToFirst();
                                String cResoureId = cursor.getString(cursor.getColumnIndex(WatchHistoryTable.VIDEO_RESOURCE_ID));
                                cursor.close();
                                if (resourceId.equalsIgnoreCase(cResoureId) && getActivity() != null) {
                                    position = getActivity().getIntent().getIntExtra(
                                            "playing_position", 0);
                                }
//                                if (isFromWatchHistory) {
                                getUrl(videoInfo.getVideoDetailList().get(position));
                                EventBus.getDefault()
                                        .post(videoInfo
                                                .getVideoDetailList()
                                                .get(position));
//                                } else {// 不是从观看记录点击过来 --跳到当前播放的集数
//                                    getUrl(videoInfo.getVideoDetailList().get(position));
//                                    EventBus.getDefault().post(
//                                            videoInfo.getVideoDetailList().get(
//                                                    position));
//                                }
                            } else {
                                tv_offline_cache.postDelayed(new Runnable() {
 
                                    @Override
                                    public void run() {
                                        EventBus.getDefault().post(videoInfo);
                                        EventBus.getDefault().post(
                                                videoInfo.getVideoDetailList()
                                                        .get(0));
                                    }
                                }, 200);
                            }
                        }
                    }
                });
    }
 
    /**
     * 收藏
     *
     * @param type
     * @param thirdType
     */
    private void getScoreCollect(String loginUid, final String type, String thirdType) {
        SharedPreferences preferences = iv_report.getContext().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        String uid = preferences.getString("uid", "");
        BeibeiVideoAPI.getScoreCollect(iv_report.getContext(), uid, loginUid, mVideoInfo.getId(),
                thirdType, type, new BasicTextHttpResponseHandler() {
 
                    @Override
                    public void onSuccessPerfect(int statusCode,
                                                 Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.getBoolean("IsPost")) {
                            if (type.equals("1")) {
                                SingleToast.showToast(iv_report.getContext(), "收藏成功");
                            } else {
                                SingleToast.showToast(iv_report.getContext(), "取消收藏成功");
                            }
                        } else {
                            if (type.equals("1")) {
                                SingleToast.showToast(iv_report.getContext(), "收藏失败");
                            } else {
                                SingleToast.showToast(iv_report.getContext(), "取消收藏失败");
                            }
                        }
                        isCollect();
                    }
                });
    }
 
    private void isCollect() {
        SharedPreferences preferences = tv_add_to_favourite.getContext().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        String uid = preferences.getString("uid", "");
        BeibeiVideoAPI.isCollect(tv_add_to_favourite.getContext(), uid, mVideoInfo.getId(),
                mVideoInfo.getThirdType(), new BasicTextHttpResponseHandler() {
 
                    @Override
                    public void onSuccessPerfect(int statusCode,
                                                 Header[] headers, JSONObject jsonObject)
                            throws Exception {
                        if (jsonObject.getBoolean("IsPost")) {
                            iv_favourite.setImageResource(R.drawable.ic_video_detail_favourite2);
                        } else {
                            iv_favourite.setImageResource(R.drawable.ic_video_detail_favourite1);
                        }
                    }
                });
    }
}