From 521dca09a134085d6bef1ccd2be92b85e8832f42 Mon Sep 17 00:00:00 2001 From: yehunxin <348226830@qq.com> Date: Sat, 19 Jun 2021 23:33:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=86=85=E7=BD=AE=E6=B5=8F=E8=A7=88?= =?UTF-8?q?=E5=99=A8=E6=94=AF=E6=8C=81=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/caches/build_file_checksums.ser | Bin 585 -> 585 bytes .../myreader/ui/dialog/MyAlertDialog.java | 14 ++++ .../download/DownloadCompleteReceiver.java | 41 ++++++++++ .../myreader/util/download/DownloadUtil.java | 74 ++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 app/src/main/java/xyz/fycz/myreader/util/download/DownloadCompleteReceiver.java create mode 100644 app/src/main/java/xyz/fycz/myreader/util/download/DownloadUtil.java diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 5cd7f7f835355055cdbc1f400a68aca0a57c223f..03ca4b96c56aca894a51ec1c75ac9c360d75e6fd 100644 GIT binary patch delta 35 tcmV+;0Nnq{1jz)Dm;@wXiztzt{SY^vn5vgR6#cMTMW(}_LH?7e0iYPK5UKzG delta 35 tcmV+;0Nnq{1jz)Dm;@h6sV0$}{SbtRw_u(-dDDMq9^B}8nQ4=$0iYYg5TO77 diff --git a/app/src/main/java/xyz/fycz/myreader/ui/dialog/MyAlertDialog.java b/app/src/main/java/xyz/fycz/myreader/ui/dialog/MyAlertDialog.java index af8af82..258b6bc 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/dialog/MyAlertDialog.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/dialog/MyAlertDialog.java @@ -2,6 +2,7 @@ package xyz.fycz.myreader.ui.dialog; import android.app.Activity; import android.app.Dialog; +import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.DialogInterface; @@ -9,6 +10,7 @@ import android.content.Intent; import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Build; +import android.os.Environment; import android.text.Editable; import android.text.InputType; import android.text.SpannableString; @@ -17,12 +19,15 @@ import android.text.TextPaint; import android.text.TextWatcher; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; +import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; +import android.webkit.DownloadListener; import android.webkit.JsResult; +import android.webkit.URLUtil; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; @@ -52,10 +57,13 @@ import xyz.fycz.myreader.greendao.service.BookGroupService; import xyz.fycz.myreader.util.CyptoUtils; import xyz.fycz.myreader.util.SharedPreUtils; import xyz.fycz.myreader.util.StatusBarUtil; +import xyz.fycz.myreader.util.download.DownloadUtil; import xyz.fycz.myreader.util.help.StringHelper; import xyz.fycz.myreader.util.ToastUtils; import xyz.fycz.myreader.util.utils.FingerprintUtils; +import static android.content.Context.DOWNLOAD_SERVICE; + /** * @author fengyue * @date 2020/9/20 9:48 @@ -446,6 +454,10 @@ public class MyAlertDialog { }); + webView.setDownloadListener((url1, userAgent, contentDisposition, mimetype, contentLength) -> { + DownloadUtil.downloadBySystem(context, url1, contentDisposition, mimetype); + }); + webView.loadUrl(url); } }).setOnBackPressedListener(() -> { @@ -470,4 +482,6 @@ public class MyAlertDialog { public interface OnInputFinishListener { void finish(String text); } + + } diff --git a/app/src/main/java/xyz/fycz/myreader/util/download/DownloadCompleteReceiver.java b/app/src/main/java/xyz/fycz/myreader/util/download/DownloadCompleteReceiver.java new file mode 100644 index 0000000..8ca2bc8 --- /dev/null +++ b/app/src/main/java/xyz/fycz/myreader/util/download/DownloadCompleteReceiver.java @@ -0,0 +1,41 @@ +package xyz.fycz.myreader.util.download; + +import android.app.DownloadManager; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.text.TextUtils; +import android.util.Log; + +import static android.content.Context.DOWNLOAD_SERVICE; + +public class DownloadCompleteReceiver extends BroadcastReceiver { + private static final String TAG = DownloadCompleteReceiver.class.getSimpleName(); + + @Override + public void onReceive(Context context, Intent intent) { + Log.d(TAG, String.format("onReceive. intent:%s", intent != null ? intent.toUri(0) : null)); + if (intent != null) { + if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) { + long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); + Log.d(TAG, String.format("downloadId:%s", downloadId)); + DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE); + String type = downloadManager.getMimeTypeForDownloadedFile(downloadId); + Log.d(TAG, String.format("getMimeTypeForDownloadedFile:%s", type)); + if (TextUtils.isEmpty(type)) { + type = "*/*"; + } + Uri uri = downloadManager.getUriForDownloadedFile(downloadId); + Log.d(TAG, String.format("UriForDownloadedFile:%s", uri)); + if (uri != null) { + Intent handlerIntent = new Intent(Intent.ACTION_VIEW); + handlerIntent.setDataAndType(uri, type); + context.startActivity(handlerIntent); + } + context.unregisterReceiver(this); + } + } + } +} + diff --git a/app/src/main/java/xyz/fycz/myreader/util/download/DownloadUtil.java b/app/src/main/java/xyz/fycz/myreader/util/download/DownloadUtil.java new file mode 100644 index 0000000..69b2fe2 --- /dev/null +++ b/app/src/main/java/xyz/fycz/myreader/util/download/DownloadUtil.java @@ -0,0 +1,74 @@ +package xyz.fycz.myreader.util.download; + +import android.app.DownloadManager; +import android.content.Context; +import android.content.IntentFilter; +import android.net.Uri; +import android.os.Environment; +import android.util.Log; +import android.webkit.URLUtil; + +import com.hjq.permissions.OnPermissionCallback; + +import java.util.List; + +import xyz.fycz.myreader.ui.dialog.DialogCreator; +import xyz.fycz.myreader.util.utils.StoragePermissionUtils; + +import static android.content.Context.DOWNLOAD_SERVICE; + +public class DownloadUtil { + + private static final String TAG = DownloadUtil.class.getSimpleName(); + + public static void downloadBySystem(Context context, String url, String contentDisposition, String mimeType) { + StoragePermissionUtils.request(context, (permissions, all) -> { + String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType); + Log.d(TAG, String.format("fileName:%s", fileName)); + DialogCreator.createCommonDialog(context, "添加下载", + "确定要下载文件[" + fileName + "]吗?", true, (dialog, which) -> { + startDownload(context, url, contentDisposition, mimeType); + }, null); + }); + } + + private static void startDownload(Context context, String url, String contentDisposition, String mimeType) { + registerReceiver(context); + // 指定下载地址 + DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); + // 允许媒体扫描,根据下载的文件类型被加入相册、音乐等媒体库 + request.allowScanningByMediaScanner(); + // 设置通知的显示类型,下载进行时和完成后显示通知 + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); + // 设置通知栏的标题,如果不设置,默认使用文件名 +// request.setTitle("This is title"); + // 设置通知栏的描述 +// request.setDescription("This is description"); + // 允许在计费流量下下载 + request.setAllowedOverMetered(true); + // 允许该记录在下载管理界面可见 + request.setVisibleInDownloadsUi(true); + // 允许漫游时下载 + request.setAllowedOverRoaming(true); + // 允许下载的网路类型 + request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); + // 设置下载文件保存的路径和文件名 + String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType); + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); +// 另外可选一下方法,自定义下载路径 +// request.setDestinationUri() +// request.setDestinationInExternalFilesDir() + final DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE); + // 添加一个下载任务 + long downloadId = downloadManager.enqueue(request); + Log.d(TAG, String.format("downloadId:%s", downloadId)); + } + + public static void registerReceiver(Context context) { + // 使用 + DownloadCompleteReceiver receiver = new DownloadCompleteReceiver(); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE); + context.registerReceiver(receiver, intentFilter); + } +} From 2ee354cf3742e06ce90435d141fd4a73fd7baec1 Mon Sep 17 00:00:00 2001 From: yehunxin <348226830@qq.com> Date: Sun, 20 Jun 2021 17:08:45 +0800 Subject: [PATCH 2/3] fix bug --- .../main/java/xyz/fycz/myreader/util/download/DownloadUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/xyz/fycz/myreader/util/download/DownloadUtil.java b/app/src/main/java/xyz/fycz/myreader/util/download/DownloadUtil.java index 69b2fe2..26f1065 100644 --- a/app/src/main/java/xyz/fycz/myreader/util/download/DownloadUtil.java +++ b/app/src/main/java/xyz/fycz/myreader/util/download/DownloadUtil.java @@ -51,7 +51,7 @@ public class DownloadUtil { // 允许漫游时下载 request.setAllowedOverRoaming(true); // 允许下载的网路类型 - request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); + //request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // 设置下载文件保存的路径和文件名 String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); From 50093e4469fb331303bf841fe48c61e51320fbad Mon Sep 17 00:00:00 2001 From: yehunxin <348226830@qq.com> Date: Sun, 20 Jun 2021 17:11:23 +0800 Subject: [PATCH 3/3] fix bug --- .idea/caches/build_file_checksums.ser | Bin 585 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .idea/caches/build_file_checksums.ser diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser deleted file mode 100644 index 03ca4b96c56aca894a51ec1c75ac9c360d75e6fd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 585 zcmZ4UmVvdnh`~NNKUXg?FQq6yGexf?KR>5fFEb@IQ7^qHF(oHeub?PDD>b=9F91S2 zm1gFoxMk*~I%lLNXBU^|7Q2L-Ts|(GuF1r}QPHR4k|6tJEKG6k(WgMYiW=Kf@-LmkpdV-u!T?D`61Cqk7W9dc|2q`%JDK l-V}N1