admin
2022-01-28 760aee20870a34e6130a1c12237c5b747e2b00bd
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
package com.yeshi.video.ui;
 
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
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.util.SingleToast;
import com.mozillaonline.providers.DownloadManager;
import com.umeng.analytics.MobclickAgent;
import com.yeshi.base.entity.db.DownloadTable;
import com.yeshi.base.entity.video.VideoDetailInfo;
import com.yeshi.base.entity.video.VideoInfo;
import com.yeshi.base.utils.JsonUtil;
import com.yeshi.base.utils.SDCardUtil;
import com.yeshi.base.utils.downutil.DownloadUtils;
import com.yeshi.base.utils.http.BasicTextHttpResponseHandler;
import com.yeshi.base.utils.http.GetDownLoadTask;
import com.yeshi.video.R;
import com.yeshi.video.ui.adapter.GridEpisodeAdapter3;
import com.yeshi.video.utils.VideoApiUtil;
 
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;
 
import java.io.File;
import java.util.List;
 
import androidx.fragment.app.Fragment;
import de.greenrobot.event.EventBus;
 
/**
 * 播放页小屏底部下载详情页面,也是多集下载详情页面点击下载更多 弹出页面
 *
 * @author weikou2015
 */
public class VideoEpisodeFragment3 extends Fragment {
 
    private VideoInfo mVideoInfo;
 
    private GridView gv_episode3;
 
    private Button btn_offline_cache;
 
    private GridEpisodeAdapter3 mGridEpisodeAdapter3;
 
    public static VideoEpisodeFragment3 newInstance(VideoInfo videoInfo) {
        VideoEpisodeFragment3 videoEpisodeFragment3 = new VideoEpisodeFragment3();
        Bundle args = new Bundle();
        args.putSerializable("video_info", videoInfo);
        videoEpisodeFragment3.setArguments(args);
        return videoEpisodeFragment3;
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mVideoInfo = (VideoInfo) getArguments().getSerializable("video_info");
 
        View view = inflater.inflate(R.layout.video_episode_fragment3,
                container, false);
        gv_episode3 = (GridView) view.findViewById(R.id.gv_episode3);
        btn_offline_cache = (Button) view.findViewById(R.id.btn_offline_cache);
        if (mVideoInfo.getShowType() == 1) {
            gv_episode3.setNumColumns(1);
        } else if (mVideoInfo.getShowType() == 2) {
            gv_episode3.setNumColumns(5);
        }
 
        if (mVideoInfo.getVideoDetailList() != null
                && !mVideoInfo.getVideoDetailList().isEmpty()) {
            mGridEpisodeAdapter3 = new GridEpisodeAdapter3(mVideoInfo);
            gv_episode3.setAdapter(mGridEpisodeAdapter3);
        } else {
            gv_episode3.setVisibility(View.GONE);
        }
 
        btn_offline_cache.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                final Context context = v.getContext();
                if (!mVideoInfo.isCanSave()) {
                    Toast.makeText(getActivity(), "该视频暂不提供缓存",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                List<VideoDetailInfo> selectedToDownLoads = mGridEpisodeAdapter3
                        .getSelectedToDownLoads();
                if (selectedToDownLoads != null
                        && !selectedToDownLoads.isEmpty()) {
                    for (final VideoDetailInfo videoDetailInfo : selectedToDownLoads) {
                        if (!DownloadUtils.isOffline(getActivity(),
                                mVideoInfo.getId(), videoDetailInfo.getId())) {
                            final DownloadManager downloadManager = new DownloadManager(
                                    getActivity().getContentResolver(),
                                    getActivity().getPackageName());
                            String url = "";
                            // VideoPlayUrlUtil.getVideoUrl(
                            // videoDetailInfo).getUrl();
 
                            (new GetDownLoadTask(getActivity()) {
                                protected void onPostExecute(String[] result) {
                                    DownloadManager.Request request = new DownloadManager.Request(
                                            Uri.parse(result[0]));
 
                                    try {
                                        // request.setDestinationUri(Uri.fromFile(new
                                        // File(
                                        // SDCardUtil
                                        // .getDownLoadPath(context))));
                                        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(DownloadManager.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());
                                    values.put(DownloadTable.VIDEO_DETAIL,
                                            JsonUtil.videoGson.toJson(mVideoInfo));
                                    getContext().getContentResolver().insert(
                                            DownloadTable.CONTENT_URI, values);
                                    EventBus.getDefault().post(values);
                                    SingleToast.showToast(getContext(),
                                            "已加入下载列表");
                                }
 
                                ;
                                // }).execute(
                                // url,
                                // videoDetailInfo.getId(),
                                // GetRealPathTask
                                // .getNeedSecondConnectServerUrl(mVideoInfo
                                // .getExtraData()));
                            }).execute(
                                    url,
                                    videoDetailInfo.getId(),
                                    null);
                        }
                    }
                    uploadSaveHistory(selectedToDownLoads);
                    getActivity().onBackPressed();
                } else {
                    SingleToast.showToast(getActivity(), "请添加未下载剧集");
                }
            }
        });
        return view;
    }
 
    @Override
    public void onResume() {
        super.onResume();
    }
 
    @Override
    public void onPause() {
        super.onPause();
    }
 
    // 提交缓存记录
    private void uploadSaveHistory(List<VideoDetailInfo> list) {
        JSONArray array = new JSONArray();
        if (list != null)
            for (VideoDetailInfo info : list)
                array.put(info.getId());
        SharedPreferences preferences = getActivity().getSharedPreferences(
                "user", Context.MODE_PRIVATE);
        String uid = preferences.getString("uid", "");
        VideoApiUtil.getScoreSave(getActivity(), uid, array.toString(),
                new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode,
                                                 Header[] headers, JSONObject jsonObject)
                            throws Exception {
                    }
                });
    }
}