X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Ffragments%2FDomainsListFragment.java;h=92d4b885eaa2d123883cca94058e612f616cb4cb;hp=34349d79a413b933ab2f12127c8fdbde1262c7f6;hb=1b27ac6f2b7c046945fc97e2aff9adbde8a152ce;hpb=5fb34c1fa70b7c42a0fc3c0b5af8e856d3af2695 diff --git a/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java b/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java index 34349d79..92d4b885 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java @@ -1,185 +1,139 @@ /* - * Copyright © 2017 Soren Stoutner . + * Copyright © 2017-2020,2022 Soren Stoutner . * - * This file is part of Privacy Browser . + * This file is part of Privacy Browser Android . * - * Privacy Browser is free software: you can redistribute it and/or modify + * Privacy Browser Android is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * Privacy Browser is distributed in the hope that it will be useful, + * Privacy Browser Android is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with Privacy Browser. If not, see . + * along with Privacy Browser Android. If not, see . */ package com.stoutner.privacybrowser.fragments; -import android.net.http.SslCertificate; +import android.content.Context; import android.os.Bundle; -import android.support.design.widget.FloatingActionButton; -// We have to use `android.support.v4.app.Fragment` until minimum API >= 23. Otherwise we cannot call `getContext()`. -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; -import android.widget.EditText; import android.widget.ListView; -import android.widget.RadioButton; -import android.widget.Spinner; -import android.widget.Switch; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; + +import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.activities.DomainsActivity; -import com.stoutner.privacybrowser.activities.MainWebViewActivity; -import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper; public class DomainsListFragment extends Fragment { - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + // Instantiate the dismiss snackbar interface. + private DismissSnackbarInterface dismissSnackbarInterface; + + // Define the public dismiss snackbar interface. + public interface DismissSnackbarInterface { + void dismissSnackbar(); + } + + public void onAttach(@NonNull Context context) { + // Run the default commands. + super.onAttach(context); + + // Populate the dismiss snackbar interface. + dismissSnackbarInterface = (DismissSnackbarInterface) context; + } + + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate `domains_list_fragment`. `false` does not attach it to the root `container`. View domainsListFragmentView = inflater.inflate(R.layout.domains_list_fragment, container, false); - // Initialize `domainsListView`. - ListView domainsListView = (ListView) domainsListFragmentView.findViewById(R.id.domains_listview); + // Get a handle for the domains listview. + ListView domainsListView = domainsListFragmentView.findViewById(R.id.domains_listview); - // Initialize the database handler. The two `nulls` do not specify the database name or a `CursorFactory`. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`. - final DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0); + // Remove the incorrect lint error below that `.getSupportFragmentManager()` might be null. + assert getActivity() != null; - // Get a handle for `supportFragmentManager`. + // Get a handle for the support fragment manager. final FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager(); - domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - // Dismiss `undoDeleteSnackbar` if it is currently displayed (because a domain has just been deleted). - if ((DomainsActivity.undoDeleteSnackbar != null) && (DomainsActivity.undoDeleteSnackbar.isShown())) { - DomainsActivity.dismissingSnackbar = true; + domainsListView.setOnItemClickListener((AdapterView parent, View view, int position, long id) -> { + // Dismiss the snackbar if it is visible. + dismissSnackbarInterface.dismissSnackbar(); - DomainsActivity.undoDeleteSnackbar.dismiss(); - } + // Save the current domain settings if operating in two-paned mode and a domain is currently selected. + if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled()) { + // Get a handle for the domain settings fragment. + Fragment domainSettingsFragment = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container); - // Save the current domain settings if operating in two-paned mode and a domain is currently selected. - if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled()) { - View domainSettingsFragmentView = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container).getView(); - assert domainSettingsFragmentView != null; - - // Get handles for the domain settings. - EditText domainNameEditText = (EditText) domainSettingsFragmentView.findViewById(R.id.domain_settings_name_edittext); - Switch javaScriptEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_javascript_switch); - Switch firstPartyCookiesEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_first_party_cookies_switch); - Switch thirdPartyCookiesEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_third_party_cookies_switch); - Switch domStorageEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_dom_storage_switch); - Switch formDataEnabledSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_form_data_switch); - Spinner userAgentSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_user_agent_spinner); - EditText customUserAgentEditText = (EditText) domainSettingsFragmentView.findViewById(R.id.domain_settings_custom_user_agent_edittext); - Spinner fontSizeSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_font_size_spinner); - Spinner displayWebpageImagesSpinner = (Spinner) domainSettingsFragmentView.findViewById(R.id.domain_settings_display_webpage_images_spinner); - Switch pinnedSslCertificateSwitch = (Switch) domainSettingsFragmentView.findViewById(R.id.domain_settings_pinned_ssl_certificate_switch); - RadioButton savedSslCertificateRadioButton = (RadioButton) domainSettingsFragmentView.findViewById(R.id.saved_ssl_certificate_radiobutton); - RadioButton currentWebsiteCertificateRadioButton = (RadioButton) domainSettingsFragmentView.findViewById(R.id.current_website_certificate_radiobutton); - - // Extract the data for the domain settings. - String domainNameString = domainNameEditText.getText().toString(); - boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked(); - boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked(); - boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked(); - boolean domStorageEnabledEnabledBoolean = domStorageEnabledSwitch.isChecked(); - boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked(); - int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition(); - int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition(); - int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition(); - boolean pinnedSslCertificate = pinnedSslCertificateSwitch.isChecked(); - - // Get the data for the `Spinners` from the entry values string arrays. - String userAgentString = getResources().getStringArray(R.array.domain_settings_user_agent_entry_values)[userAgentPositionInt]; - int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePositionInt]); - - // Check to see if we are using a custom user agent. - if (userAgentString.equals("Custom user agent")) { - // Set `userAgentString` to the custom user agent string. - userAgentString = customUserAgentEditText.getText().toString(); - } - - // Save the domain settings. - if (savedSslCertificateRadioButton.isChecked()) { // The current certificate is being used. - // Update the database except for the certificate. - domainsDatabaseHelper.updateDomainExceptCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, - formDataEnabledBoolean, userAgentString, fontSizeInt, displayWebpageImagesInt, pinnedSslCertificate); - } else if (currentWebsiteCertificateRadioButton.isChecked()) { // The certificate is being updated with the current website certificate. - // Get the current website SSL certificate. - SslCertificate currentWebsiteSslCertificate = MainWebViewActivity.sslCertificate; - - // Store the values from the SSL certificate. - String issuedToCommonName = currentWebsiteSslCertificate.getIssuedTo().getCName(); - String issuedToOrganization = currentWebsiteSslCertificate.getIssuedTo().getOName(); - String issuedToOrganizationalUnit = currentWebsiteSslCertificate.getIssuedTo().getUName(); - String issuedByCommonName = currentWebsiteSslCertificate.getIssuedBy().getCName(); - String issuedByOrganization = currentWebsiteSslCertificate.getIssuedBy().getOName(); - String issuedByOrganizationalUnit = currentWebsiteSslCertificate.getIssuedBy().getUName(); - long startDateLong = currentWebsiteSslCertificate.getValidNotBeforeDate().getTime(); - long endDateLong = currentWebsiteSslCertificate.getValidNotAfterDate().getTime(); - - // Update the database. - domainsDatabaseHelper.updateDomainWithCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, - formDataEnabledBoolean, userAgentString, fontSizeInt, displayWebpageImagesInt, pinnedSslCertificate, issuedToCommonName, issuedToOrganization, issuedToOrganizationalUnit, issuedByCommonName, issuedByOrganization, - issuedByOrganizationalUnit, startDateLong, endDateLong); - } else { // No certificate is selected. - // Update the database, with PINNED_SSL_CERTIFICATE set to false. - domainsDatabaseHelper.updateDomainExceptCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, - formDataEnabledBoolean, userAgentString, fontSizeInt, displayWebpageImagesInt, false); - } - } + // Remove the incorrect lint warning below that the domain settings fragment might be null. + assert domainSettingsFragment != null; - // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database. - DomainsActivity.currentDomainDatabaseId = (int) id; + // Get a handle for the domain settings fragment view. + View domainSettingsFragmentView = domainSettingsFragment.getView(); - // Add `currentDomainDatabaseId` to `argumentsBundle`. - Bundle argumentsBundle = new Bundle(); - argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId); + // Remove the incorrect lint warning below that the domain settings fragment view might be null. + assert domainSettingsFragmentView != null; - // Add `argumentsBundle` to `domainSettingsFragment`. - DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment(); - domainSettingsFragment.setArguments(argumentsBundle); + // Get a handle for the domains activity. + DomainsActivity domainsActivity = new DomainsActivity(); + + // Save the domain settings. + domainsActivity.saveDomainSettings(domainSettingsFragmentView, getResources()); + } + + // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database. + DomainsActivity.currentDomainDatabaseId = (int) id; + + // Add `currentDomainDatabaseId` to `argumentsBundle`. + Bundle argumentsBundle = new Bundle(); + argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId); + + // Add the arguments bundle to the domain settings fragment. + DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment(); + domainSettingsFragment.setArguments(argumentsBundle); + + // Check to see if the device is in two paned mode. + if (DomainsActivity.twoPanedMode) { // The device in in two-paned mode. + // enable `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed. + if (!DomainsActivity.dismissingSnackbar) { + // Enable the delete menu item. + DomainsActivity.deleteMenuItem.setEnabled(true); + + // Set the delete icon. + DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_enabled); + } // Display the domain settings fragment. - if (DomainsActivity.twoPanedMode) { // The device in in two-paned mode. - // enable `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed. - if (!DomainsActivity.dismissingSnackbar) { - // Enable `deleteMenuItem`. - DomainsActivity.deleteMenuItem.setEnabled(true); - - // Set the delete icon according to the theme. - if (MainWebViewActivity.darkTheme) { - DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_dark); - } else { - DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_light); - } - } - - // Display `domainSettingsFragment`. - supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit(); - } else { // The device in in single-paned mode - // Show `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed. - if (!DomainsActivity.dismissingSnackbar) { - DomainsActivity.deleteMenuItem.setVisible(true); - } - - // Hide `add_domain_fab`. - FloatingActionButton addDomainFAB = (FloatingActionButton) getActivity().findViewById(R.id.add_domain_fab); - addDomainFAB.setVisibility(View.GONE); - - // Display `domainSettingsFragment`. - supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit(); + supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit(); + } else { // The device in in single-paned mode + // Save the domains listview position. + DomainsActivity.domainsListViewPosition = domainsListView.getFirstVisiblePosition(); + + // Show `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed. + if (!DomainsActivity.dismissingSnackbar) { + DomainsActivity.deleteMenuItem.setVisible(true); } + + // Hide the add domain FAB. + FloatingActionButton addDomainFAB = getActivity().findViewById(R.id.add_domain_fab); + addDomainFAB.hide(); + + // Display the domain settings fragment. + supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit(); } }); + // Return the domains list fragment. return domainsListFragmentView; } -} +} \ No newline at end of file