- currentWebView.findNext(true);
- }
-
- public void closeFindOnPage(View view) {
- // Get a handle for the views.
- Toolbar toolbar = findViewById(R.id.toolbar);
- LinearLayout findOnPageLinearLayout = findViewById(R.id.find_on_page_linearlayout);
- EditText findOnPageEditText = findViewById(R.id.find_on_page_edittext);
-
- // Delete the contents of `find_on_page_edittext`.
- findOnPageEditText.setText(null);
-
- // Clear the highlighted phrases if the WebView is not null.
- if (currentWebView != null) {
- currentWebView.clearMatches();
- }
-
- // Hide the find on page linear layout.
- findOnPageLinearLayout.setVisibility(View.GONE);
-
- // Show the toolbar.
- toolbar.setVisibility(View.VISIBLE);
-
- // Get a handle for the input method manager.
- InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
-
- // Remove the lint warning below that the input method manager might be null.
- assert inputMethodManager != null;
-
- // Hide the keyboard.
- inputMethodManager.hideSoftInputFromWindow(toolbar.getWindowToken(), 0);
- }
-
- @Override
- public void onApplyNewFontSize(DialogFragment dialogFragment) {
- // Get the dialog.
- Dialog dialog = dialogFragment.getDialog();
-
- // Remove the incorrect lint warning below tha the dialog might be null.
- assert dialog != null;
-
- // Get a handle for the font size edit text.
- EditText fontSizeEditText = dialog.findViewById(R.id.font_size_edittext);
-
- // Initialize the new font size variable with the current font size.
- int newFontSize = currentWebView.getSettings().getTextZoom();
-
- // Get the font size from the edit text.
- try {
- newFontSize = Integer.valueOf(fontSizeEditText.getText().toString());
- } catch (Exception exception) {
- // If the edit text does not contain a valid font size do nothing.
- }
-
- // Apply the new font size.
- currentWebView.getSettings().setTextZoom(newFontSize);
- }
-
- @Override
- public void onOpen(DialogFragment dialogFragment) {
- // Get the dialog.
- Dialog dialog = dialogFragment.getDialog();
-
- // Remove the incorrect lint warning below that the dialog might be null.
- assert dialog != null;
-
- // Get a handle for the file name edit text.
- EditText fileNameEditText = dialog.findViewById(R.id.file_name_edittext);
-
- // Get the file path string.
- openFilePath = fileNameEditText.getText().toString();
-
- // Check to see if the storage permission is needed.
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted.
- // Open the file.
- currentWebView.loadUrl("file://" + openFilePath);
- } else { // The storage permission has not been granted.
- // Get the external private directory file.
- File externalPrivateDirectoryFile = getExternalFilesDir(null);
-
- // Remove the incorrect lint error below that the file might be null.
- assert externalPrivateDirectoryFile != null;
-
- // Get the external private directory string.
- String externalPrivateDirectory = externalPrivateDirectoryFile.toString();
-
- // Check to see if the file path is in the external private directory.
- if (openFilePath.startsWith(externalPrivateDirectory)) { // the file path is in the external private directory.
- // Open the file.
- currentWebView.loadUrl("file://" + openFilePath);
- } else { // The file path is in a public directory.
- // Check if the user has previously denied the storage permission.
- if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Show a dialog explaining the request first.
- // Instantiate the storage permission alert dialog.
- DialogFragment storagePermissionDialogFragment = StoragePermissionDialog.displayDialog(StoragePermissionDialog.OPEN);
-
- // Show the storage permission alert dialog. The permission will be requested the the dialog is closed.
- storagePermissionDialogFragment.show(getSupportFragmentManager(), getString(R.string.storage_permission));
- } else { // Show the permission request directly.
- // Request the write external storage permission. The file will be opened when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_OPEN_REQUEST_CODE);
- }
- }
- }
- }
-
- @Override
- public void onSaveWebpage(int saveType, DialogFragment dialogFragment) {
- // Get the dialog.
- Dialog dialog = dialogFragment.getDialog();
-
- // Remove the incorrect lint warning below that the dialog might be null.
- assert dialog != null;
-
- // Get a handle for the file name edit text.
- EditText fileNameEditText = dialog.findViewById(R.id.file_name_edittext);
-
- // Get the file path string.
- saveWebpageFilePath = fileNameEditText.getText().toString();
-
- // Check to see if the storage permission is needed.
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted.
- //Save the webpage according to the save type.
- switch (saveType) {
- case StoragePermissionDialog.SAVE_ARCHIVE:
- // Save the webpage archive.
- currentWebView.saveWebArchive(saveWebpageFilePath);
- break;
-
- case StoragePermissionDialog.SAVE_IMAGE:
- // Save the webpage image.
- new SaveWebpageImage(this, currentWebView).execute(saveWebpageFilePath);
- break;
- }
- } else { // The storage permission has not been granted.
- // Get the external private directory file.
- File externalPrivateDirectoryFile = getExternalFilesDir(null);
-
- // Remove the incorrect lint error below that the file might be null.
- assert externalPrivateDirectoryFile != null;
-
- // Get the external private directory string.
- String externalPrivateDirectory = externalPrivateDirectoryFile.toString();
-
- // Check to see if the file path is in the external private directory.
- if (saveWebpageFilePath.startsWith(externalPrivateDirectory)) { // The file path is in the external private directory.
- // Save the webpage according to the save type.
- switch (saveType) {
- case StoragePermissionDialog.SAVE_ARCHIVE:
- // Save the webpage archive.
- currentWebView.saveWebArchive(saveWebpageFilePath);
- break;
-
- case StoragePermissionDialog.SAVE_IMAGE:
- // Save the webpage image.
- new SaveWebpageImage(this, currentWebView).execute(saveWebpageFilePath);
- break;
- }
- } else { // The file path is in a public directory.
- // Check if the user has previously denied the storage permission.
- if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Show a dialog explaining the request first.
- // Instantiate the storage permission alert dialog.
- DialogFragment storagePermissionDialogFragment = StoragePermissionDialog.displayDialog(saveType);
-
- // Show the storage permission alert dialog. The permission will be requested when the dialog is closed.
- storagePermissionDialogFragment.show(getSupportFragmentManager(), getString(R.string.storage_permission));
- } else { // Show the permission request directly.
- switch (saveType) {
- case StoragePermissionDialog.SAVE_ARCHIVE:
- // Request the write external storage permission. The webpage archive will be saved when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_SAVE_WEBPAGE_ARCHIVE_REQUEST_CODE);
- break;
-
- case StoragePermissionDialog.SAVE_IMAGE:
- // Request the write external storage permission. The webpage image will be saved when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_SAVE_WEBPAGE_IMAGE_REQUEST_CODE);
- break;
- }
- }
- }
- }
- }
-
- @Override
- public void onCloseStoragePermissionDialog(int requestType) {
- switch (requestType) {
- case StoragePermissionDialog.OPEN:
- // Request the write external storage permission. The file will be opened when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_OPEN_REQUEST_CODE);
- break;
-
- case StoragePermissionDialog.SAVE_ARCHIVE:
- // Request the write external storage permission. The webpage archive will be saved when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_SAVE_WEBPAGE_ARCHIVE_REQUEST_CODE);
- break;
-
- case StoragePermissionDialog.SAVE_IMAGE:
- // Request the write external storage permission. The webpage image will be saved when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_SAVE_WEBPAGE_IMAGE_REQUEST_CODE);
- break;
- }