- @Override
- public void onCloseDownloadLocationPermissionDialog(int downloadType) {
- switch (downloadType) {
- case DownloadLocationPermissionDialog.DOWNLOAD_FILE:
- // Request the WRITE_EXTERNAL_STORAGE permission with a file request code.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, DOWNLOAD_FILE_REQUEST_CODE);
- break;
-
- case DownloadLocationPermissionDialog.DOWNLOAD_IMAGE:
- // Request the WRITE_EXTERNAL_STORAGE permission with an image request code.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, DOWNLOAD_IMAGE_REQUEST_CODE);
- break;
- }
- }
-
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- // Get a handle for the fragment manager.
- FragmentManager fragmentManager = getSupportFragmentManager();
-
- switch (requestCode) {
- case DOWNLOAD_FILE_REQUEST_CODE:
- // Show the download file alert dialog. When the dialog closes, the correct command will be used based on the permission status.
- DialogFragment downloadFileDialogFragment = DownloadFileDialog.fromUrl(downloadUrl, downloadContentDisposition, downloadContentLength);
-
- // On API 23, displaying the fragment must be delayed or the app will crash.
- if (Build.VERSION.SDK_INT == 23) {
- new Handler().postDelayed(() -> downloadFileDialogFragment.show(fragmentManager, getString(R.string.download)), 500);
- } else {
- downloadFileDialogFragment.show(fragmentManager, getString(R.string.download));
- }
-
- // Reset the download variables.
- downloadUrl = "";
- downloadContentDisposition = "";
- downloadContentLength = 0;
- break;
-
- case DOWNLOAD_IMAGE_REQUEST_CODE:
- // Show the download image alert dialog. When the dialog closes, the correct command will be used based on the permission status.
- DialogFragment downloadImageDialogFragment = DownloadImageDialog.imageUrl(downloadImageUrl);
-
- // On API 23, displaying the fragment must be delayed or the app will crash.
- if (Build.VERSION.SDK_INT == 23) {
- new Handler().postDelayed(() -> downloadImageDialogFragment.show(fragmentManager, getString(R.string.download)), 500);
- } else {
- downloadImageDialogFragment.show(fragmentManager, getString(R.string.download));
- }
-
- // Reset the image URL variable.
- downloadImageUrl = "";
- break;
-
- case SAVE_WEBPAGE_IMAGE_REQUEST_CODE:
- // Check to see if the storage permission was granted. If the dialog was canceled the grant result will be empty.
- if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) { // The storage permission was granted.
- // Save the webpage image.
- new SaveWebpageImage(this, currentWebView).execute(saveWebsiteImageFilePath);
- } else { // The storage permission was not granted.
- // Display an error snackbar.
- Snackbar.make(currentWebView, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
- }
-
- // Reset the save website image file path.
- saveWebsiteImageFilePath = "";
- break;
- }
- }
-
- @Override
- public void onDownloadImage(DialogFragment dialogFragment, String imageUrl) {
- // Download the image if it has an HTTP or HTTPS URI.
- if (imageUrl.startsWith("http")) {
- // Get a handle for the system `DOWNLOAD_SERVICE`.
- DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
-
- // Parse `imageUrl`.
- DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(imageUrl));
-
- // Get a handle for the cookie manager.
- CookieManager cookieManager = CookieManager.getInstance();
-
- // Pass cookies to download manager if cookies are enabled. This is required to download images from websites that require a login.
- // Code contributed 2017 Hendrik Knackstedt. Copyright assigned to Soren Stoutner <soren@stoutner.com>.
- if (cookieManager.acceptCookie()) {
- // Get the cookies for `imageUrl`.
- String cookies = cookieManager.getCookie(imageUrl);
-
- // Add the cookies to `downloadRequest`. In the HTTP request header, cookies are named `Cookie`.
- downloadRequest.addRequestHeader("Cookie", cookies);
- }
-
- // Get the dialog.
- Dialog dialog = dialogFragment.getDialog();
-
- // Remove the incorrect lint warning below that the dialog might be null.
- assert dialog != null;
-
- // Get the file name from the dialog fragment.
- EditText downloadImageNameEditText = dialog.findViewById(R.id.download_image_name);
- String imageName = downloadImageNameEditText.getText().toString();
-
- // Specify the download location.
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // External write permission granted.
- // Download to the public download directory.
- downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, imageName);
- } else { // External write permission denied.
- // Download to the app's external download directory.
- downloadRequest.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, imageName);
- }
-
- // Allow `MediaScanner` to index the download if it is a media file.
- downloadRequest.allowScanningByMediaScanner();
-
- // Add the URL as the description for the download.
- downloadRequest.setDescription(imageUrl);
-
- // Show the download notification after the download is completed.
- downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
-
- // Remove the lint warning below that `downloadManager` might be `null`.
- assert downloadManager != null;
-
- // Initiate the download.
- downloadManager.enqueue(downloadRequest);
- } else { // The image is not an HTTP or HTTPS URI.
- Snackbar.make(currentWebView, R.string.cannot_download_image, Snackbar.LENGTH_INDEFINITE).show();
- }
- }
-
- @Override
- public void onDownloadFile(DialogFragment dialogFragment, String downloadUrl) {
- // Download the file if it has an HTTP or HTTPS URI.
- if (downloadUrl.startsWith("http")) {
- // Get a handle for the system `DOWNLOAD_SERVICE`.
- DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
-
- // Parse `downloadUrl`.
- DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(downloadUrl));
-
- // Get a handle for the cookie manager.
- CookieManager cookieManager = CookieManager.getInstance();
-
- // Pass cookies to download manager if cookies are enabled. This is required to download files from websites that require a login.
- // Code contributed 2017 Hendrik Knackstedt. Copyright assigned to Soren Stoutner <soren@stoutner.com>.
- if (cookieManager.acceptCookie()) {
- // Get the cookies for `downloadUrl`.
- String cookies = cookieManager.getCookie(downloadUrl);
-
- // Add the cookies to `downloadRequest`. In the HTTP request header, cookies are named `Cookie`.
- downloadRequest.addRequestHeader("Cookie", cookies);
- }
-
- // Get the dialog.
- Dialog dialog = dialogFragment.getDialog();
-
- // Remove the incorrect lint warning below that the dialog might be null.
- assert dialog != null;
-
- // Get the file name from the dialog.
- EditText downloadFileNameEditText = dialog.findViewById(R.id.download_file_name);
- String fileName = downloadFileNameEditText.getText().toString();
-
- // Specify the download location.
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // External write permission granted.
- // Download to the public download directory.
- downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
- } else { // External write permission denied.
- // Download to the app's external download directory.
- downloadRequest.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, fileName);
- }
-
- // Allow `MediaScanner` to index the download if it is a media file.
- downloadRequest.allowScanningByMediaScanner();
-
- // Add the URL as the description for the download.
- downloadRequest.setDescription(downloadUrl);
-
- // Show the download notification after the download is completed.
- downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
-
- // Remove the lint warning below that `downloadManager` might be `null`.
- assert downloadManager != null;
-
- // Initiate the download.
- downloadManager.enqueue(downloadRequest);
- } else { // The download is not an HTTP or HTTPS URI.
- Snackbar.make(currentWebView, R.string.cannot_download_file, Snackbar.LENGTH_INDEFINITE).show();
- }
- }
-
- // Override `onBackPressed` to handle the navigation drawer and and the WebViews.