admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
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
package com.weikou.beibeivideo.ui.media;
 
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.core.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.Toast;
 
import com.lcjian.library.RetainViewFragment;
import com.lcjian.library.util.ManifestDataUtil;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.sdk.QbSdk;
import com.umeng.analytics.MobclickAgent;
import com.weikou.beibeivideo.entity.AccumulateRule;
import com.weikou.beibeivideo.entity.PlayUrl;
import com.weikou.beibeivideo.entity.video.ChangeVideoEvent;
import com.weikou.beibeivideo.util.downutil.DownFiles;
import com.weikou.beibeivideo.util.downutil.DownFiles.IProgress;
import com.weikou.beibeivideo.util.x5.X5WebView;
import com.weikou.beibeivideo.R;
 
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
 
import java.util.HashMap;
 
/**
 * 网页加载页面
 *
 * @author Administrator
 */
public class IQYVideoFragment extends RetainViewFragment implements
        OnClickListener {
    private X5WebView webview;
    private FrameLayout fl_webview;
 
    @Override
    public int getContentResource() {
        return R.layout.fragment_iqiyi_video;
    }
 
    @Override
    public void onCreateView(View contentView, Bundle savedInstanceState) {
        fl_webview = contentView.findViewById(R.id.fl_webview);
        contentView.findViewById(R.id.iv_back).setOnClickListener(this);
        webview = new X5WebView(fl_webview.getContext());
        webview.setDownloadListener(new com.tencent.smtt.sdk.DownloadListener() {
 
            @Override
            public void onDownloadStart(String arg0, String arg1, String arg2,
                                        String arg3, long arg4) {
            }
 
        });
        fl_webview.addView(webview, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        com.tencent.smtt.sdk.WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
 
        webview.setDownloadListener(new MyWebViewDownLoadListener());
        webview.setWebViewClient(new com.tencent.smtt.sdk.WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(com.tencent.smtt.sdk.WebView view, String url) {
                if (url != null && url.startsWith("weixin://")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                if (url != null && url.startsWith("buwanprotocol://")) {
                    String murl = url.split("buwanprotocol://")[1];
                    String[] params = murl.split("#");
                    if (params[0].equalsIgnoreCase("playvideo")) {
                        Intent intent = new Intent(fl_webview.getContext(), VideoDetailActivity2.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("Id", params[1]);
                        bundle.putString("ResourceId", params[2]);
                        bundle.putString("DetailId", params[3]);
                        bundle.putString("Share", "0");
                        bundle.putString("ThirdType", "0");
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
 
            }
 
 
            @Override
            public void onPageFinished(com.tencent.smtt.sdk.WebView webView, String s) {
                super.onPageFinished(webView, s);
                VideoDetailActivity.hideImage();
            }
 
            @Override
            public void onReceivedSslError(com.tencent.smtt.sdk.WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
                sslErrorHandler.proceed();
            }
        });
        webview.setWebChromeClient(new com.tencent.smtt.sdk.WebChromeClient() {
 
            @Override
            public void onReceivedTitle(com.tencent.smtt.sdk.WebView view, String title) {
            }
 
            @Override
            public void onProgressChanged(com.tencent.smtt.sdk.WebView webView, int i) {
                super.onProgressChanged(webView, i);
            }
        });
        PlayUrl playUrl = (PlayUrl) getArguments().getSerializable("playUrl");
        if (playUrl != null) {
            webview.loadUrl(playUrl.getUrl());
        }
        VideoDetailActivity.hideImage();
 
    }
 
    // 文件下载监听
 
    private class MyWebViewDownLoadListener implements com.tencent.smtt.sdk.DownloadListener {
 
        @Override
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype, long contentLength) {
            startDownLoadFile(url);
            /*
             * Uri uri = Uri.parse(url); Intent intent = new
             * Intent(Intent.ACTION_VIEW, uri);
             * startActivity(intent);
             */
        }
    }
 
    private NotificationManager manager;
    private Notification notif;
    private Notification.Builder oBuilder;
    private NotificationCompat.Builder builder;
    int j = -1;
 
    private void startDownLoadFile(String url) {
 
        new DownFiles(fl_webview.getContext(), new IProgress() {
 
            @Override
            public void getProgress(int p) {
                // stub
                if (p > 99) {
                    handler.sendEmptyMessage(1);
                    Log.i("DownFiles", "getProgress下载进度:" + p);
                } else {
                    if (manager == null) {
                        Toast.makeText(fl_webview.getContext(), "文件已经开始下载",
                                Toast.LENGTH_SHORT).show();
                        manager = (NotificationManager) (fl_webview.getContext().getSystemService(Context.NOTIFICATION_SERVICE));
                        String id = "UMENG_CHANNEL";
                        String description = ManifestDataUtil.getAppMetaData(fl_webview.getContext(), id);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            int importance = NotificationManager.IMPORTANCE_HIGH;
                            NotificationChannel mChannel = new NotificationChannel(id, "123", importance);
                            mChannel.setDescription(description);
                            mChannel.enableLights(true);
                            mChannel.setLightColor(Color.RED);
                            mChannel.enableVibration(true);
                            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                            manager.createNotificationChannel(mChannel);
                            oBuilder = new Notification.Builder(fl_webview.getContext(), id);
                            oBuilder.setContentTitle("影视大全")
                                    .setSmallIcon(R.drawable.ic_launcher)
                                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
                                    .setContentText("热门应用下载")
                                    .setAutoCancel(true)
                                    .build();
                        } else {
                            builder = new NotificationCompat.Builder(fl_webview.getContext());
                            builder.setContentTitle("影视大全")
                                    .setContentText("热门应用下载")
                                    .setSmallIcon(R.drawable.ic_launcher)
                                    .setOngoing(true);//无效
                        }
                    }
 
                    Message msg = handler.obtainMessage();
                    if (j != p) {
                        msg.what = 0;
                        msg.arg1 = p;
                        handler.sendMessage(msg);
                    }
                    j = p;
                }
            }
        }).execute(url);
    }
 
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        oBuilder.setContentText(msg.arg1 + "%");
                        notif = oBuilder.build();
                    } else {
                        builder.setContentText(msg.arg1 + "%");
                        notif = builder.build();
                    }
                    manager.notify(0, notif);
                    break;
                case 1:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        oBuilder.setContentText("下载完成");
                        notif = oBuilder.build();
                    } else {
                        builder.setContentText("下载完成");
                        notif = builder.build();
                    }
                    manager.notify(0, notif);
                    j = 100;
                    manager.cancelAll();
                    break;
                default:
                    break;
            }
        }
    };
 
    @Override
    public void onResume() {
        super.onResume();
        webview.reload();
    }
 
    @Override
    public void onPause() {
        super.onPause();
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back: {
                EventBus.getDefault().post(new AccumulateRule());// 播放界面返回替代使用AccumulateRule
                // class
            }
            default:
                break;
        }
    }
 
    @Subscribe
    public void onEventMainThread(ChangeVideoEvent changeVideoEvent) {
        if ("VideoPlayerFragment".equals(changeVideoEvent.getTag())) {
            changeVideoEvent.setTag("VideoDetailActivity");
            EventBus.getDefault().post(changeVideoEvent);
        }
    }
}