|  | @@ -2,11 +2,17 @@ package com.citycloud.androidweb.activy;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import androidx.appcompat.app.AppCompatActivity;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import android.app.Activity;
 | 
	
		
			
				|  |  | +import android.app.DownloadManager;
 | 
	
		
			
				|  |  | +import android.content.Context;
 | 
	
		
			
				|  |  |  import android.content.Intent;
 | 
	
		
			
				|  |  |  import android.graphics.Bitmap;
 | 
	
		
			
				|  |  | +import android.net.Uri;
 | 
	
		
			
				|  |  |  import android.os.Bundle;
 | 
	
		
			
				|  |  | +import android.os.Environment;
 | 
	
		
			
				|  |  |  import android.util.Log;
 | 
	
		
			
				|  |  |  import android.view.KeyEvent;
 | 
	
		
			
				|  |  | +import android.webkit.DownloadListener;
 | 
	
		
			
				|  |  |  import android.webkit.ValueCallback;
 | 
	
		
			
				|  |  |  import android.webkit.WebResourceRequest;
 | 
	
		
			
				|  |  |  import android.webkit.WebSettings;
 | 
	
	
		
			
				|  | @@ -20,9 +26,18 @@ import com.citycloud.androidweb.common.Constants;
 | 
	
		
			
				|  |  |  import com.citycloud.androidweb.web.CoolIndicatorLayout;
 | 
	
		
			
				|  |  |  import com.citycloud.androidweb.R;
 | 
	
		
			
				|  |  |  import com.citycloud.androidweb.common.AndroidInterface;
 | 
	
		
			
				|  |  | +import com.download.library.DownloadImpl;
 | 
	
		
			
				|  |  | +import com.download.library.DownloadListenerAdapter;
 | 
	
		
			
				|  |  | +import com.download.library.DownloadingListener;
 | 
	
		
			
				|  |  | +import com.download.library.Extra;
 | 
	
		
			
				|  |  | +import com.download.library.ResourceRequest;
 | 
	
		
			
				|  |  | +import com.just.agentweb.AbsAgentWebSettings;
 | 
	
		
			
				|  |  |  import com.just.agentweb.AgentWeb;
 | 
	
		
			
				|  |  | +import com.just.agentweb.DefaultDownloadImpl;
 | 
	
		
			
				|  |  |  import com.just.agentweb.DefaultWebClient;
 | 
	
		
			
				|  |  | +import com.just.agentweb.IAgentWebSettings;
 | 
	
		
			
				|  |  |  import com.just.agentweb.WebChromeClient;
 | 
	
		
			
				|  |  | +import com.just.agentweb.WebListenerManager;
 | 
	
		
			
				|  |  |  import com.just.agentweb.WebViewClient;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import org.json.JSONObject;
 | 
	
	
		
			
				|  | @@ -35,7 +50,7 @@ import cn.bingoogolapple.bgabanner.BGABanner;
 | 
	
		
			
				|  |  |   * 实现了回退、退出功能
 | 
	
		
			
				|  |  |   * @author Yc
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  | -public class MainActivity extends AppCompatActivity {
 | 
	
		
			
				|  |  | +public class MainActivity extends AppCompatActivity implements DownloadListener {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private ImageView ivback;
 | 
	
		
			
				|  |  |      private TextView receiveTitle;
 | 
	
	
		
			
				|  | @@ -71,6 +86,7 @@ public class MainActivity extends AppCompatActivity {
 | 
	
		
			
				|  |  |          mAgentWeb = AgentWeb.with(this)
 | 
	
		
			
				|  |  |                  .setAgentWebParent(mLinearLayout, new LinearLayout.LayoutParams(-1, -1))
 | 
	
		
			
				|  |  |                  .setCustomIndicator(mCoolIndicatorLayout)
 | 
	
		
			
				|  |  | +                .setAgentWebWebSettings(getSettings())
 | 
	
		
			
				|  |  |                  //.useDefaultIndicator(0,0)
 | 
	
		
			
				|  |  |                  //.useDefaultIndicator()
 | 
	
		
			
				|  |  |                  .setWebChromeClient(webChromeClient)
 | 
	
	
		
			
				|  | @@ -125,6 +141,72 @@ public class MainActivity extends AppCompatActivity {
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * @return IAgentWebSettings
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public IAgentWebSettings getSettings() {
 | 
	
		
			
				|  |  | +        return new AbsAgentWebSettings() {
 | 
	
		
			
				|  |  | +            private AgentWeb mAgentWeb;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            @Override
 | 
	
		
			
				|  |  | +            protected void bindAgentWebSupport(AgentWeb agentWeb) {
 | 
	
		
			
				|  |  | +                this.mAgentWeb = agentWeb;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            /**
 | 
	
		
			
				|  |  | +             * AgentWeb 4.0.0 内部删除了 DownloadListener 监听 ,以及相关API ,将 Download 部分完全抽离出来独立一个库,
 | 
	
		
			
				|  |  | +             * 如果你需要使用 AgentWeb Download 部分 , 请依赖上 compile 'com.download.library:Downloader:4.1.1' ,
 | 
	
		
			
				|  |  | +             * 如果你需要监听下载结果,请自定义 AgentWebSetting , New 出 DefaultDownloadImpl
 | 
	
		
			
				|  |  | +             * 实现进度或者结果监听,例如下面这个例子,如果你不需要监听进度,或者下载结果,下面 setDownloader 的例子可以忽略。
 | 
	
		
			
				|  |  | +             * @param webView
 | 
	
		
			
				|  |  | +             * @param downloadListener
 | 
	
		
			
				|  |  | +             * @return WebListenerManager
 | 
	
		
			
				|  |  | +             */
 | 
	
		
			
				|  |  | +            @Override
 | 
	
		
			
				|  |  | +            public WebListenerManager setDownloader(WebView webView, android.webkit.DownloadListener downloadListener) {
 | 
	
		
			
				|  |  | +                return super.setDownloader(webView,
 | 
	
		
			
				|  |  | +                        new DefaultDownloadImpl(MainActivity.this,
 | 
	
		
			
				|  |  | +                                webView,
 | 
	
		
			
				|  |  | +                                this.mAgentWeb.getPermissionInterceptor()) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                            @Override
 | 
	
		
			
				|  |  | +                            protected ResourceRequest createResourceRequest(String url) {
 | 
	
		
			
				|  |  | +                                return DownloadImpl.getInstance()
 | 
	
		
			
				|  |  | +                                        .with(getApplicationContext())
 | 
	
		
			
				|  |  | +                                        .url(url)
 | 
	
		
			
				|  |  | +                                        .quickProgress()
 | 
	
		
			
				|  |  | +                                        .addHeader("", "")
 | 
	
		
			
				|  |  | +                                        .setEnableIndicator(true)
 | 
	
		
			
				|  |  | +                                        .autoOpenIgnoreMD5()
 | 
	
		
			
				|  |  | +                                        .setRetry(5)
 | 
	
		
			
				|  |  | +                                        .setBlockMaxTime(100000L);
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                            @Override
 | 
	
		
			
				|  |  | +                            protected void taskEnqueue(ResourceRequest resourceRequest) {
 | 
	
		
			
				|  |  | +                                resourceRequest.enqueue(new DownloadListenerAdapter() {
 | 
	
		
			
				|  |  | +                                    @Override
 | 
	
		
			
				|  |  | +                                    public void onStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength, Extra extra) {
 | 
	
		
			
				|  |  | +                                        super.onStart(url, userAgent, contentDisposition, mimetype, contentLength, extra);
 | 
	
		
			
				|  |  | +                                    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                                    @DownloadingListener.MainThread
 | 
	
		
			
				|  |  | +                                    @Override
 | 
	
		
			
				|  |  | +                                    public void onProgress(String url, long downloaded, long length, long usedTime) {
 | 
	
		
			
				|  |  | +                                        super.onProgress(url, downloaded, length, usedTime);
 | 
	
		
			
				|  |  | +                                    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                                    @Override
 | 
	
		
			
				|  |  | +                                    public boolean onResult(Throwable throwable, Uri path, String url, Extra extra) {
 | 
	
		
			
				|  |  | +                                        return super.onResult(throwable, path, url, extra);
 | 
	
		
			
				|  |  | +                                    }
 | 
	
		
			
				|  |  | +                                });
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                        });
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        };
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      private String getJson(){
 | 
	
		
			
				|  |  |          String result="";
 | 
	
		
			
				|  |  |          try {
 | 
	
	
		
			
				|  | @@ -189,4 +271,23 @@ public class MainActivity extends AppCompatActivity {
 | 
	
		
			
				|  |  |          return super.onKeyDown(keyCode, event);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
 | 
	
		
			
				|  |  | +        startDownload(url);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void startDownload(String url) {
 | 
	
		
			
				|  |  | +        DownloadManager dm = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
 | 
	
		
			
				|  |  | +        DownloadManager.Request request = new DownloadManager.Request(
 | 
	
		
			
				|  |  | +                Uri.parse(url));
 | 
	
		
			
				|  |  | +        request.setMimeType("application/vnd.android.package-archive");
 | 
	
		
			
				|  |  | +        request.setVisibleInDownloadsUi(true);
 | 
	
		
			
				|  |  | +        request.setDestinationInExternalFilesDir(this,
 | 
	
		
			
				|  |  | +                Environment.DIRECTORY_DOWNLOADS,"fileName");
 | 
	
		
			
				|  |  | +        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
 | 
	
		
			
				|  |  | +        dm.enqueue(request);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  }
 |