admin
6 天以前 7f0825f8195a522ed7e8bcdb6347f3a719e06c74
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
package com.weikou.beibeivideo.ui.media;
 
import java.io.File;
import java.util.List;
 
import android.app.Dialog;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
 
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lcjian.library.dialog.SlidingDialog;
import com.lcjian.library.util.SingleToast;
import com.umeng.analytics.MobclickAgent;
import com.weikou.beibeivideo.db.DownloadTable;
import com.weikou.beibeivideo.entity.VideoDetailInfo;
import com.weikou.beibeivideo.entity.VideoInfo;
import com.weikou.beibeivideo.util.DownloadUtils;
import com.weikou.beibeivideo.util.GetDownLoadTask;
import com.weikou.beibeivideo.util.GetRealPathTask;
import com.weikou.beibeivideo.util.SDCardUtil;
import com.weikou.beibeivideo.R;
import com.weikou.beibeivideo.util.VideoUtil;
 
import org.greenrobot.eventbus.EventBus;
 
/**
 * 全屏播放-下载页面
 * 
 * @author weikou2015
 * 
 */
public class VideoEpisodeFragment4 extends DialogFragment {
 
    private VideoInfo mVideoInfo;
 
    private GridView gv_episode4;
 
    private Button btn_offline_cache;
 
    private GridEpisodeAdapter4 mGridEpisodeAdapter4;
 
    public static VideoEpisodeFragment4 newInstance(VideoInfo videoInfo) {
        VideoEpisodeFragment4 videoEpisodeFragment4 = new VideoEpisodeFragment4();
        Bundle args = new Bundle();
        args.putSerializable("video_info", videoInfo);
        videoEpisodeFragment4.setArguments(args);
        return videoEpisodeFragment4;
    }
 
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new SlidingDialog(gv_episode4.getContext());
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return dialog;
    }
 
    @Override
    public void onResume() {
        super.onResume();
    }
 
    @Override
    public void onPause() {
        super.onPause();
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mVideoInfo = (VideoInfo) getArguments().getSerializable("video_info");
 
        View view = inflater.inflate(R.layout.video_episode_fragment4,
                container, false);
        gv_episode4 = (GridView) view.findViewById(R.id.gv_episode4);
        btn_offline_cache = (Button) view.findViewById(R.id.btn_offline_cache);
        if (mVideoInfo.getShowType() == 1) {
            gv_episode4.setNumColumns(1);
        } else if (mVideoInfo.getShowType() == 2) {
            gv_episode4.setNumColumns(5);
        }
 
        if (VideoUtil.videoEpisodeList != null
                && !VideoUtil.videoEpisodeList.isEmpty()) {
            mGridEpisodeAdapter4 = new GridEpisodeAdapter4(mVideoInfo);
            gv_episode4.setAdapter(mGridEpisodeAdapter4);
        } else {
            gv_episode4.setVisibility(View.GONE);
        }
 
        btn_offline_cache.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                if (!mVideoInfo.isCanSave()) {
                    Toast.makeText(gv_episode4.getContext(), "该视频暂不提供缓存",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                final Context context = v.getContext();
                List<VideoDetailInfo> selectedToDownLoads = mGridEpisodeAdapter4
                        .getSelectedToDownLoads();
                if (selectedToDownLoads != null
                        && !selectedToDownLoads.isEmpty()) {
                    for (final VideoDetailInfo videoDetailInfo : selectedToDownLoads) {
                        if (!DownloadUtils.isOffline(gv_episode4.getContext(),
                                mVideoInfo.getId(), videoDetailInfo.getId())) {
//                            final DownloadManager downloadManager = new DownloadManager(
//                                    getActivity().getContentResolver(),
//                                    getActivity().getPackageName());
                            String url = "";
                            // VideoPlayUrlUtil.getVideoUrl(
                            // videoDetailInfo).getUrl();
 
                            (new GetDownLoadTask(gv_episode4.getContext()) {
                                protected void onPostExecute(String[] result) {
                                    DownloadManager.Request request = new Request(
                                            Uri.parse(result[0]));
                                    try {
                                        // request.setDestinationFromBase(
                                        // new File(
                                        // SDCardUtil
                                        // .getDownLoadPath(getActivity())),
                                        // "/");
                                        Uri uri = Uri.fromFile(new File(
                                                SDCardUtil
                                                        .getDownLoadPath(context)));
                                        request.setDestinationUri(uri);
//                                        request.setUserAgent(result[1]);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                    // request.setDestinationInExternalPublicDir(
                                    // Environment.DIRECTORY_DOWNLOADS,
                                    // "/");
 
                                    Cursor cursor = getContext()
                                            .getContentResolver()
                                            .query(DownloadTable.CONTENT_URI,
                                                    new String[] {
                                                            DownloadTable.VIDEO_ID,
                                                            DownloadTable.VIDEO_DETAIL_ID },
                                                    DownloadTable.VIDEO_DETAIL_ID
                                                            + "=?",
                                                    new String[] { videoDetailInfo
                                                            .getId() + "" },
                                                    DownloadTable.VIDEO_DETAIL_ID
                                                            + " ASC");// 查询cursor
                                    if (cursor.getCount() > 0) {
                                        cursor.close();
                                        return;
                                    }
                                    cursor.close();
 
                                    if (getContext().getSharedPreferences(
                                            "settings", Context.MODE_PRIVATE)
                                            .getBoolean("only_wifi_download",
                                                    true)) {
                                        request.setAllowedNetworkTypes(Request.NETWORK_WIFI);
                                    }
                                    request.setTitle(mVideoInfo.getName());
                                    request.setDescription(TextUtils
                                            .isEmpty(videoDetailInfo.getTag()) ? videoDetailInfo
                                            .getName() : videoDetailInfo
                                            .getTag());
//                                    long taskId = downloadManager
//                                            .enqueue(request);
 
                                    ContentValues values = new ContentValues();
//                                    values.put(DownloadTable.TASK_ID, taskId);
                                    values.put(DownloadTable.VIDEO_ID,
                                            mVideoInfo.getId());
                                    values.put(DownloadTable.VIDEO_DETAIL_ID,
                                            videoDetailInfo.getId());
                                    values.put(DownloadTable.VIDEO_THIRD_TYPE,
                                            mVideoInfo.getThirdType());
                                    Gson gson = new GsonBuilder()
                                            .setFieldNamingPolicy(
                                                    FieldNamingPolicy.UPPER_CAMEL_CASE)
                                            .create();
                                    values.put(DownloadTable.VIDEO_DETAIL,
                                            gson.toJson(mVideoInfo));
                                    getContext().getContentResolver().insert(
                                            DownloadTable.CONTENT_URI, values);
                                    EventBus.getDefault().post(values);
                                    // Toast.makeText(getContext(), "已加入下载列表",
                                    // Toast.LENGTH_LONG).show();
                                    SingleToast.showToast(getContext(),
                                            "已加入下载列表");
                                };
                            }).execute(
                                    url,
                                    videoDetailInfo.getId(),
                                    GetRealPathTask
                                            .getNeedSecondConnectServerUrl(mVideoInfo
                                                    .getExtraData()));
                        }
                    }
                } else {
                    SingleToast.showToast(gv_episode4.getContext(), "已加请添加未入下载剧集");
                }
            }
        });
        return view;
    }
}