- // Create a file name string.
- String fileName = "";
-
- // Set the file name according to the type.
- switch (saveType) {
- case StoragePermissionDialog.SAVE_URL:
- // Convert the URL to a URI.
- Uri uri = Uri.parse(url);
-
- // Get the last path segment.
- String lastPathSegment = uri.getLastPathSegment();
-
- // Use a default file name if the last path segment is null.
- if (lastPathSegment == null) {
- lastPathSegment = getString(R.string.file);
- }
-
- // Use the last path segment as the file name.
- fileName = lastPathSegment;
- break;
-
- case StoragePermissionDialog.SAVE_AS_ARCHIVE:
- fileName = getString(R.string.webpage_mht);
- break;
-
- case StoragePermissionDialog.SAVE_AS_IMAGE:
- fileName = getString(R.string.webpage_png);
- break;
- }
-
- // Save the file name as the default file name. This must be final to be used in the lambda below.
- final String defaultFileName = fileName;
-
- // Instantiate the download location helper.
- DownloadLocationHelper downloadLocationHelper = new DownloadLocationHelper();
-
- // Get the default file path.
- String defaultFilePath = downloadLocationHelper.getDownloadLocation(context) + "/" + defaultFileName;
-
- // Hide the storage permission text view if the permission has already been granted.
- if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
- storagePermissionTextView.setVisibility(View.GONE);
- }
-
- // Populate the edit texts.
- urlEditText.setText(url);
- fileNameEditText.setText(defaultFilePath);
-
- // Move the cursor to the end of the default file path.
- fileNameEditText.setSelection(defaultFilePath.length());
-