Browse Source

增加文件下载功能

yc100 4 years atrás
parent
commit
9d9e1a127a

+ 2 - 2
app/build.gradle

@@ -6,7 +6,7 @@ android {
 
     defaultConfig {
         applicationId 'com.citycloud.androidweb'
-        minSdkVersion 29
+        minSdkVersion 17
         targetSdkVersion 29
         versionCode 1
         versionName "1.0"
@@ -34,7 +34,7 @@ dependencies {
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
     implementation 'com.just.agentweb:agentweb:4.1.4' // (必选)
     implementation 'com.just.agentweb:filechooser:4.1.4'// (可选)
-    //implementation 'com.download.library:Downloader:4.1.4'// (可选)
+    compile 'com.download.library:Downloader:4.1.2'// (可选)
     implementation 'com.lcodecorex:tkrefreshlayout:1.0.7'
     implementation 'com.coolindicator.sdk:coolindicator:1.0.0-beta'
     //noinspection GradleCompatible

+ 102 - 1
app/src/main/java/com/citycloud/androidweb/activy/MainActivity.java

@@ -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);
+    }
+
+
+
 }

+ 3 - 1
app/src/main/java/com/citycloud/androidweb/common/Constants.java

@@ -2,7 +2,9 @@ package com.citycloud.androidweb.common;
 
 public class Constants {
     /*project*/
-    public static final String TARGET_URL="http://ecweb.aiyangniu.net/";/*测试环境*/
+    //public static final String TARGET_URL="http://ecweb.aiyangniu.net/";/*测试环境*/
+    //public static final String TARGET_URL="http://ecadmin.aiyangniu.net/#/login";/*测试环境*/
+    public static final String TARGET_URL="http://test.aiyangniu.net/";/*测试环境*/
 
 
 

+ 6 - 4
build.gradle

@@ -3,11 +3,13 @@
 buildscript {
     
     repositories {
-        google()
         jcenter()
-        mavenCentral()
-        maven { url 'https://jitpack.io' }
-        
+        google()
+        maven { url "https://jitpack.io" }
+        maven {
+            url 'https://maven.google.com/'
+            name 'Google'
+        }
     }
     dependencies {
         classpath 'com.android.tools.build:gradle:3.6.2'