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=43594860a3d16508e4f0fe2b98035b44c3de8c74;hp=b478d726f69d83da1c64475dc5455f054bbb97dc;hb=4ce562261f47e06c454504262a24f61f46bb393d;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..43594860 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 . * @@ -22,13 +22,13 @@ package com.stoutner.privacybrowser.dialogs; import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; -import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; +import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; import android.os.Environment; @@ -41,14 +41,18 @@ 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.appcompat.app.AlertDialog; 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; @@ -73,36 +77,20 @@ public class OpenDialog extends DialogFragment { @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { // Get a handle for the activity and the context. - Activity activity = getActivity(); - Context context = getContext(); - - // Remove the incorrect lint warnings below that the activity and the context might be null. - assert activity != null; - assert context != null; - - // Get a handle for the shared preferences. - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); - - // Get the screenshot and theme preferences. - boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false); - boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false); + Activity activity = requireActivity(); + Context context = requireContext(); // Use an alert dialog builder to create the alert dialog. - AlertDialog.Builder dialogBuilder; + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.PrivacyBrowserAlertDialog); - // Set the style and icon according to the theme. - if (darkTheme) { - // Set the style. - dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogDark); + // Get the current theme status. + int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; - // Set the icon. - dialogBuilder.setIcon(R.drawable.proxy_enabled_dark); + // Set the icon according to the theme. + if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) { + dialogBuilder.setIcon(R.drawable.proxy_enabled_night); } else { - // Set the style. - dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogLight); - - // Set the icon. - dialogBuilder.setIcon(R.drawable.proxy_enabled_light); + dialogBuilder.setIcon(R.drawable.proxy_enabled_day); } // Set the title. @@ -126,6 +114,12 @@ public class OpenDialog extends DialogFragment { // Remove the incorrect lint warning below that the window might be null. assert alertDialog.getWindow() != null; + // Get a handle for the shared preferences. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + + // Get the screenshot preference. + boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false); + // Disable screenshots if not allowed. if (!allowScreenshots) { alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); @@ -137,26 +131,15 @@ 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()); + // Remove the incorrect lint warnings below that the views might be null. + assert fileNameEditText != null; + assert browseButton != null; + assert fileDoesNotExistTextView != null; + assert storagePermissionTextView != null; // Update the status of the open button when the file name changes. fileNameEditText.addTextChangedListener(new TextWatcher() { @@ -172,11 +155,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 +212,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; }