66import android .content .Intent ;
77import android .graphics .Color ;
88import android .graphics .drawable .Drawable ;
9+ import android .net .Uri ;
910import android .os .Build ;
1011import android .os .Bundle ;
1112import android .text .Html ;
1213import android .text .TextUtils ;
1314import android .util .TypedValue ;
1415import android .view .View ;
1516import android .view .WindowManager ;
16- import android .webkit .WebChromeClient ;
1717import android .webkit .WebView ;
1818
19+ import androidx .appcompat .app .AlertDialog ;
20+ import androidx .core .content .ContextCompat ;
21+
1922import com .aries .library .fast .demo .R ;
2023import com .aries .library .fast .demo .module .main .MainActivity ;
21- import com .aries .library .fast .demo .util .NotificationUtil ;
2224import com .aries .library .fast .i .IFastRefreshView ;
2325import com .aries .library .fast .manager .LoggerManager ;
2426import com .aries .library .fast .manager .RxJavaManager ;
3840import com .aries .ui .util .RomUtil ;
3941import com .aries .ui .view .title .TitleBarView ;
4042import 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 ;
4147import com .just .agentweb .AbsAgentWebSettings ;
4248import com .just .agentweb .AgentWeb ;
49+ import com .just .agentweb .AgentWebSettingsImpl ;
50+ import com .just .agentweb .DefaultDownloadImpl ;
4351import com .just .agentweb .IAgentWebSettings ;
4452import com .just .agentweb .IVideo ;
45- import com .just .agentweb .LogUtils ;
53+ import com .just .agentweb .MiddlewareWebChromeBase ;
4654import com .just .agentweb .VideoImpl ;
4755import 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 ;
5256import com .scwang .smartrefresh .header .StoreHouseHeader ;
5357import com .scwang .smartrefresh .layout .SmartRefreshLayout ;
5458import com .scwang .smartrefresh .layout .api .RefreshLayout ;
5559
5660import java .io .File ;
5761
58- import androidx .appcompat .app .AlertDialog ;
59- import androidx .core .content .ContextCompat ;
60-
6162/**
6263 * @Author: AriesHoo on 2018/7/30 11:04
6364 * @E-Mail: AriesHoo@126.com
@@ -109,21 +110,16 @@ public void setTitleBar(TitleBarView titleBar) {
109110 .setVisibility (View .GONE );
110111 }
111112 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- // })
118113 .setDividerVisible (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP );
119114 }
120115
121116 @ Override
122117 protected void setAgentWeb (AgentWeb .CommonBuilder mAgentBuilder ) {
123118 super .setAgentWeb (mAgentBuilder );
124119 //设置 IAgentWebSettings
125- mAgentBuilder .setAgentWebWebSettings (getSettings ())
126- .setWebChromeClient (new WebChromeClient () {
120+ mAgentBuilder .setAgentWebWebSettings (AgentWebSettingsImpl .getInstance ())
121+ .setAgentWebWebSettings (getSettings ())
122+ .useMiddlewareWebChrome (new MiddlewareWebChromeBase () {
127123 @ Override
128124 public void onProgressChanged (WebView view , int newProgress ) {
129125 super .onProgressChanged (view , newProgress );
@@ -135,14 +131,6 @@ public void onProgressChanged(WebView view, int newProgress) {
135131 }
136132 }
137133
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-
146134 @ Override
147135 public void onHideCustomView () {
148136 super .onHideCustomView ();
@@ -307,115 +295,6 @@ private IVideo getIVideo() {
307295 return mIVideo ;
308296 }
309297
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- };
419298
420299 /**
421300 * @return IAgentWebSettings
@@ -431,8 +310,8 @@ protected void bindAgentWebSupport(AgentWeb agentWeb) {
431310
432311 /**
433312 * 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
436315 * 实现进度或者结果监听,例如下面这个例子,如果你不需要监听进度,或者下载结果,下面 setDownloader 的例子可以忽略。
437316 * @param webView
438317 * @param downloadListener
@@ -441,12 +320,44 @@ protected void bindAgentWebSupport(AgentWeb agentWeb) {
441320 @ Override
442321 public WebListenerManager setDownloader (WebView webView , android .webkit .DownloadListener downloadListener ) {
443322 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+ });
450361 }
451362 };
452363 }
0 commit comments