内置浏览器支持下载文件

pull/11/head
yehunxin 3 years ago
parent e19f7cc51b
commit 521dca09a1
  1. BIN
      .idea/caches/build_file_checksums.ser
  2. 14
      app/src/main/java/xyz/fycz/myreader/ui/dialog/MyAlertDialog.java
  3. 41
      app/src/main/java/xyz/fycz/myreader/util/download/DownloadCompleteReceiver.java
  4. 74
      app/src/main/java/xyz/fycz/myreader/util/download/DownloadUtil.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);
}
}

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

@ -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);
}
}
Loading…
Cancel
Save