X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FOpenDialog.java;h=59aa177ba945c1fa58288357987a22c69570bfa9;hp=b478d726f69d83da1c64475dc5455f054bbb97dc;hb=ba4a1c032dbffde044b70c804f9d3c1f1ba7b939;hpb=b235ba52731f112015d9058ade4c5a66abf7ed5c diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/OpenDialog.java b/app/src/main/java/com/stoutner/privacybrowser/dialogs/OpenDialog.java index b478d726..59aa177b 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/OpenDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/OpenDialog.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Soren Stoutner . + * Copyright © 2019-2020 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -41,14 +41,17 @@ import android.widget.Button; import android.widget.EditText; import android.widget.TextView; -import com.stoutner.privacybrowser.R; -import com.stoutner.privacybrowser.activities.MainWebViewActivity; - import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; import androidx.fragment.app.DialogFragment; import androidx.preference.PreferenceManager; +import com.stoutner.privacybrowser.R; +import com.stoutner.privacybrowser.activities.MainWebViewActivity; +import com.stoutner.privacybrowser.helpers.DownloadLocationHelper; + +import java.io.File; + public class OpenDialog extends DialogFragment { // Define the open listener. private OpenListener openListener; @@ -137,27 +140,10 @@ public class OpenDialog extends DialogFragment { // Get handles for the layout items. EditText fileNameEditText = alertDialog.findViewById(R.id.file_name_edittext); Button browseButton = alertDialog.findViewById(R.id.browse_button); + TextView fileDoesNotExistTextView = alertDialog.findViewById(R.id.file_does_not_exist_textview); TextView storagePermissionTextView = alertDialog.findViewById(R.id.storage_permission_textview); Button openButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); - // Create a string for the default file path. - String defaultFilePath; - - // Set the default file path according to the storage permission state. - if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted. - // Set the default file path to use the external public directory. - defaultFilePath = Environment.getExternalStorageDirectory() + "/"; - } else { // The storage permission has not been granted. - // Set the default file path to use the external private directory. - defaultFilePath = context.getExternalFilesDir(null) + "/"; - } - - // Display the default file path. - fileNameEditText.setText(defaultFilePath); - - // Move the cursor to the end of the default file path. - fileNameEditText.setSelection(defaultFilePath.length()); - // Update the status of the open button when the file name changes. fileNameEditText.addTextChangedListener(new TextWatcher() { @Override @@ -172,11 +158,46 @@ public class OpenDialog extends DialogFragment { @Override public void afterTextChanged(Editable editable) { - // Enable the open button if a file name exists. - openButton.setEnabled(!fileNameEditText.getText().toString().isEmpty()); + // Get the current file name. + String fileNameString = fileNameEditText.getText().toString(); + + // Convert the file name string to a file. + File file = new File(fileNameString); + + // Check to see if the file exists. + if (file.exists()) { // The file exists. + // Hide the notification that the file does not exist. + fileDoesNotExistTextView.setVisibility(View.GONE); + + // Enable the open button. + openButton.setEnabled(true); + } else { // The file does not exist. + // Show the notification that the file does not exist. + fileDoesNotExistTextView.setVisibility(View.VISIBLE); + + // Disable the open button. + openButton.setEnabled(false); + } } }); + // Instantiate the download location helper. + DownloadLocationHelper downloadLocationHelper = new DownloadLocationHelper(); + + // Get the default file path. + String defaultFilePath = downloadLocationHelper.getDownloadLocation(context) + "/"; + + // Display the default file path. + fileNameEditText.setText(defaultFilePath); + + // Move the cursor to the end of the default file path. + fileNameEditText.setSelection(defaultFilePath.length()); + + // 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); + } + // Handle clicks on the browse button. browseButton.setOnClickListener((View view) -> { // Create the file picker intent. @@ -194,11 +215,6 @@ public class OpenDialog extends DialogFragment { activity.startActivityForResult(browseIntent, MainWebViewActivity.BROWSE_OPEN_REQUEST_CODE); }); - // Hide the storage permission text view on API < 23 as permissions on older devices are automatically granted. - if (Build.VERSION.SDK_INT < 23 || (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) { - storagePermissionTextView.setVisibility(View.GONE); - } - // Return the alert dialog. return alertDialog; }