6
6
import android .content .Intent ;
7
7
import android .graphics .Color ;
8
8
import android .graphics .drawable .Drawable ;
9
+ import android .net .Uri ;
9
10
import android .os .Build ;
10
11
import android .os .Bundle ;
11
12
import android .text .Html ;
12
13
import android .text .TextUtils ;
13
14
import android .util .TypedValue ;
14
15
import android .view .View ;
15
16
import android .view .WindowManager ;
16
- import android .webkit .WebChromeClient ;
17
17
import android .webkit .WebView ;
18
18
19
+ import androidx .appcompat .app .AlertDialog ;
20
+ import androidx .core .content .ContextCompat ;
21
+
19
22
import com .aries .library .fast .demo .R ;
20
23
import com .aries .library .fast .demo .module .main .MainActivity ;
21
- import com .aries .library .fast .demo .util .NotificationUtil ;
22
24
import com .aries .library .fast .i .IFastRefreshView ;
23
25
import com .aries .library .fast .manager .LoggerManager ;
24
26
import com .aries .library .fast .manager .RxJavaManager ;
38
40
import com .aries .ui .util .RomUtil ;
39
41
import com .aries .ui .view .title .TitleBarView ;
40
42
import com .aries .ui .widget .action .sheet .UIActionSheetDialog ;
43
+ import com .download .library .DownloadImpl ;
44
+ import com .download .library .DownloadListenerAdapter ;
45
+ import com .download .library .Extra ;
46
+ import com .download .library .ResourceRequest ;
41
47
import com .just .agentweb .AbsAgentWebSettings ;
42
48
import com .just .agentweb .AgentWeb ;
49
+ import com .just .agentweb .AgentWebSettingsImpl ;
50
+ import com .just .agentweb .DefaultDownloadImpl ;
43
51
import com .just .agentweb .IAgentWebSettings ;
44
52
import com .just .agentweb .IVideo ;
45
- import com .just .agentweb .LogUtils ;
53
+ import com .just .agentweb .MiddlewareWebChromeBase ;
46
54
import com .just .agentweb .VideoImpl ;
47
55
import com .just .agentweb .WebListenerManager ;
48
- import com .just .agentweb .download .AgentWebDownloader ;
49
- import com .just .agentweb .download .DefaultDownloadImpl ;
50
- import com .just .agentweb .download .DownloadListenerAdapter ;
51
- import com .just .agentweb .download .DownloadingService ;
52
56
import com .scwang .smartrefresh .header .StoreHouseHeader ;
53
57
import com .scwang .smartrefresh .layout .SmartRefreshLayout ;
54
58
import com .scwang .smartrefresh .layout .api .RefreshLayout ;
55
59
56
60
import java .io .File ;
57
61
58
- import androidx .appcompat .app .AlertDialog ;
59
- import androidx .core .content .ContextCompat ;
60
-
61
62
/**
62
63
* @Author: AriesHoo on 2018/7/30 11:04
63
64
* @E-Mail: AriesHoo@126.com
@@ -109,21 +110,16 @@ public void setTitleBar(TitleBarView titleBar) {
109
110
.setVisibility (View .GONE );
110
111
}
111
112
titleBar .setTitleMainTextMarquee (true )
112
- // .setOnRightTextClickListener(new View.OnClickListener() {
113
- // @Override
114
- // public void onClick(View v) {
115
- // WebViewActivity.start(mContext,"www.baidu.com");
116
- // }
117
- // })
118
113
.setDividerVisible (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP );
119
114
}
120
115
121
116
@ Override
122
117
protected void setAgentWeb (AgentWeb .CommonBuilder mAgentBuilder ) {
123
118
super .setAgentWeb (mAgentBuilder );
124
119
//设置 IAgentWebSettings
125
- mAgentBuilder .setAgentWebWebSettings (getSettings ())
126
- .setWebChromeClient (new WebChromeClient () {
120
+ mAgentBuilder .setAgentWebWebSettings (AgentWebSettingsImpl .getInstance ())
121
+ .setAgentWebWebSettings (getSettings ())
122
+ .useMiddlewareWebChrome (new MiddlewareWebChromeBase () {
127
123
@ Override
128
124
public void onProgressChanged (WebView view , int newProgress ) {
129
125
super .onProgressChanged (view , newProgress );
@@ -135,14 +131,6 @@ public void onProgressChanged(WebView view, int newProgress) {
135
131
}
136
132
}
137
133
138
- @ Override
139
- public void onReceivedTitle (WebView view , String title ) {
140
- super .onReceivedTitle (view , title );
141
- if (mTitleBar != null ) {
142
- mTitleBar .setTitleMainText (title );
143
- }
144
- }
145
-
146
134
@ Override
147
135
public void onHideCustomView () {
148
136
super .onHideCustomView ();
@@ -307,115 +295,6 @@ private IVideo getIVideo() {
307
295
return mIVideo ;
308
296
}
309
297
310
- /**
311
- * 更新于 AgentWeb 4.0.0
312
- */
313
- protected DownloadListenerAdapter mDownloadListenerAdapter = new DownloadListenerAdapter () {
314
-
315
- private DownloadingService mDownloadingService ;
316
-
317
- /**
318
- *
319
- * @param url 下载链接
320
- * @param userAgent UserAgent
321
- * @param contentDisposition ContentDisposition
322
- * @param mimetype 资源的媒体类型
323
- * @param contentLength 文件长度
324
- * @param extra 下载配置 , 用户可以通过 Extra 修改下载icon , 关闭进度条 , 是否强制下载。
325
- * @return true 表示用户处理了该下载事件 , false 交给 AgentWeb 下载
326
- */
327
- @ Override
328
- public boolean onStart (String url , String userAgent , String contentDisposition , String mimetype , long contentLength , AgentWebDownloader .Extra extra ) {
329
- LogUtils .i (TAG , "onStart:" + url );
330
- // 是否开启断点续传
331
- extra .setOpenBreakPointDownload (true )
332
- //下载通知的icon
333
- .setIcon (R .drawable .ic_file_download_black_24dp )
334
- // 连接最大时长
335
- .setConnectTimeOut (6000 )
336
- // 以8KB位单位,默认60s ,如果60s内无法从网络流中读满8KB数据,则抛出异常
337
- .setBlockMaxTime (10 * 60 * 1000 )
338
- // 下载最大时长
339
- .setDownloadTimeOut (Long .MAX_VALUE )
340
- // 串行下载更节省资源哦
341
- .setParallelDownload (false )
342
- // false 关闭进度通知
343
- .setEnableIndicator (true )
344
- // 自定义请求头
345
- .addHeader ("Cookie" , "xx" )
346
- // 下载完成自动打开
347
- .setAutoOpen (true )
348
- // 强制下载,不管网络网络类型
349
- .setForceDownload (true );
350
- return false ;
351
- }
352
-
353
- /**
354
- *
355
- * 不需要暂停或者停止下载该方法可以不必实现
356
- * @param url
357
- * @param downloadingService 用户可以通过 DownloadingService#shutdownNow 终止下载
358
- */
359
- @ Override
360
- public void onBindService (String url , DownloadingService downloadingService ) {
361
- super .onBindService (url , downloadingService );
362
- mDownloadingService = downloadingService ;
363
- LogUtils .i (TAG , "onBindService:" + url + " DownloadingService:" + downloadingService );
364
- }
365
-
366
- /**
367
- * 回调onUnbindService方法,让用户释放掉 DownloadingService。
368
- * @param url
369
- * @param downloadingService
370
- */
371
- @ Override
372
- public void onUnbindService (String url , DownloadingService downloadingService ) {
373
- super .onUnbindService (url , downloadingService );
374
- mDownloadingService = null ;
375
- LogUtils .i (TAG , "onUnbindService:" + url );
376
- }
377
-
378
- /**
379
- *
380
- * @param url 下载链接
381
- * @param loaded 已经下载的长度
382
- * @param length 文件的总大小
383
- * @param usedTime 耗时 ,单位ms
384
- * 注意该方法回调在子线程 ,线程名 AsyncTask #XX 或者 AgentWeb # XX
385
- */
386
- @ Override
387
- public void onProgress (String url , long loaded , long length , long usedTime ) {
388
- int mProgress = (int ) ((loaded ) / Float .valueOf (length ) * 100 );
389
- LogUtils .i (TAG , "onProgress:" + mProgress );
390
- //进度到100--主动调用关闭下载并重启下载因断点下载直接回调成功
391
- if (mProgress == 100 ) {
392
- mDownloadingService .shutdownNow ().performReDownload ();
393
- }
394
- super .onProgress (url , loaded , length , usedTime );
395
- }
396
-
397
- /**
398
- *
399
- * @param path 文件的绝对路径
400
- * @param url 下载地址
401
- * @param throwable 如果异常,返回给用户异常
402
- * @return true 表示用户处理了下载完成后续的事件 ,false 默认交给AgentWeb 处理
403
- */
404
- @ Override
405
- public boolean onResult (String path , String url , Throwable throwable ) {
406
- NotificationUtil .getInstance ().cancelAll ();
407
- //下载成功
408
- if (null == throwable ) {
409
- if (path .endsWith ("apk" )) {
410
- FastFileUtil .installApk (new File (path ), getPackageName () + ".AgentWebFileProvider" );
411
- }
412
- } else {//下载失败
413
-
414
- }
415
- // true 不会发出下载完成的通知 , 或者打开文件
416
- return path .endsWith ("apk" );
417
- }
418
- };
419
298
420
299
/**
421
300
* @return IAgentWebSettings
@@ -431,8 +310,8 @@ protected void bindAgentWebSupport(AgentWeb agentWeb) {
431
310
432
311
/**
433
312
* AgentWeb 4.0.0 内部删除了 DownloadListener 监听 ,以及相关API ,将 Download 部分完全抽离出来独立一个库,
434
- * 如果你需要使用 AgentWeb Download 部分 , 请依赖上 compile 'com.just.agentweb:download :4.0.0 ,
435
- * 如果你需要监听下载结果,请自定义 AgentWebSetting , New 出 DefaultDownloadImpl,传入DownloadListenerAdapter
313
+ * 如果你需要使用 AgentWeb Download 部分 , 请依赖上 compile 'com.download.library:Downloader :4.1.1' ,
314
+ * 如果你需要监听下载结果,请自定义 AgentWebSetting , New 出 DefaultDownloadImpl
436
315
* 实现进度或者结果监听,例如下面这个例子,如果你不需要监听进度,或者下载结果,下面 setDownloader 的例子可以忽略。
437
316
* @param webView
438
317
* @param downloadListener
@@ -441,12 +320,44 @@ protected void bindAgentWebSupport(AgentWeb agentWeb) {
441
320
@ Override
442
321
public WebListenerManager setDownloader (WebView webView , android .webkit .DownloadListener downloadListener ) {
443
322
return super .setDownloader (webView ,
444
- DefaultDownloadImpl
445
- .create ((Activity ) webView .getContext (),
446
- webView ,
447
- mDownloadListenerAdapter ,
448
- mDownloadListenerAdapter ,
449
- this .mAgentWeb .getPermissionInterceptor ()));
323
+ new DefaultDownloadImpl ((Activity ) webView .getContext (),
324
+ webView ,
325
+ this .mAgentWeb .getPermissionInterceptor ()) {
326
+
327
+ @ Override
328
+ protected ResourceRequest createResourceRequest (String url ) {
329
+ return DownloadImpl .getInstance ()
330
+ .with (getApplicationContext ())
331
+ .url (url )
332
+ .quickProgress ()
333
+ .addHeader ("" , "" )
334
+ .setEnableIndicator (true )
335
+ .autoOpenIgnoreMD5 ()
336
+ .setRetry (5 )
337
+ .setBlockMaxTime (100000L );
338
+ }
339
+
340
+ @ Override
341
+ protected void taskEnqueue (ResourceRequest resourceRequest ) {
342
+ resourceRequest .enqueue (new DownloadListenerAdapter () {
343
+ @ Override
344
+ public void onStart (String url , String userAgent , String contentDisposition , String mimetype , long contentLength , Extra extra ) {
345
+ super .onStart (url , userAgent , contentDisposition , mimetype , contentLength , extra );
346
+ }
347
+
348
+ @ MainThread
349
+ @ Override
350
+ public void onProgress (String url , long downloaded , long length , long usedTime ) {
351
+ super .onProgress (url , downloaded , length , usedTime );
352
+ }
353
+
354
+ @ Override
355
+ public boolean onResult (Throwable throwable , Uri path , String url , Extra extra ) {
356
+ return super .onResult (throwable , path , url , extra );
357
+ }
358
+ });
359
+ }
360
+ });
450
361
}
451
362
};
452
363
}
0 commit comments