2 * Copyright © 2017-2020 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.fragments;
22 import android.content.Context;
23 import android.content.res.Configuration;
24 import android.os.Bundle;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.AdapterView;
29 import android.widget.ListView;
31 import androidx.annotation.NonNull;
32 import androidx.fragment.app.Fragment;
33 import androidx.fragment.app.FragmentManager;
35 import com.google.android.material.floatingactionbutton.FloatingActionButton;
37 import com.stoutner.privacybrowser.R;
38 import com.stoutner.privacybrowser.activities.DomainsActivity;
40 public class DomainsListFragment extends Fragment {
41 // Instantiate the dismiss snackbar interface.
42 private DismissSnackbarInterface dismissSnackbarInterface;
44 // Define the public dismiss snackbar interface.
45 public interface DismissSnackbarInterface {
46 void dismissSnackbar();
49 public void onAttach(@NonNull Context context) {
50 // Run the default commands.
51 super.onAttach(context);
53 // Populate the dismiss snackbar interface.
54 dismissSnackbarInterface = (DismissSnackbarInterface) context;
57 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
58 // Inflate `domains_list_fragment`. `false` does not attach it to the root `container`.
59 View domainsListFragmentView = inflater.inflate(R.layout.domains_list_fragment, container, false);
61 // Get a handle for the domains listview.
62 ListView domainsListView = domainsListFragmentView.findViewById(R.id.domains_listview);
64 // Remove the incorrect lint error below that `.getSupportFragmentManager()` might be null.
65 assert getActivity() != null;
67 // Get a handle for the support fragment manager.
68 final FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
70 domainsListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
71 // Dismiss the snackbar if it is visible.
72 dismissSnackbarInterface.dismissSnackbar();
74 // Save the current domain settings if operating in two-paned mode and a domain is currently selected.
75 if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled()) {
76 // Get a handle for the domain settings fragment.
77 Fragment domainSettingsFragment = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container);
79 // Remove the incorrect lint warning below that the domain settings fragment might be null.
80 assert domainSettingsFragment != null;
82 // Get a handle for the domain settings fragment view.
83 View domainSettingsFragmentView = domainSettingsFragment.getView();
85 // Remove the incorrect lint warning below that the domain settings fragment view might be null.
86 assert domainSettingsFragmentView != null;
88 // Get a handle for the domains activity.
89 DomainsActivity domainsActivity = new DomainsActivity();
91 // Save the domain settings.
92 domainsActivity.saveDomainSettings(domainSettingsFragmentView, getResources());
95 // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database.
96 DomainsActivity.currentDomainDatabaseId = (int) id;
98 // Add `currentDomainDatabaseId` to `argumentsBundle`.
99 Bundle argumentsBundle = new Bundle();
100 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId);
102 // Add the arguments bundle to the domain settings fragment.
103 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
104 domainSettingsFragment.setArguments(argumentsBundle);
106 // Check to see if the device is in two paned mode.
107 if (DomainsActivity.twoPanedMode) { // The device in in two-paned mode.
108 // enable `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
109 if (!DomainsActivity.dismissingSnackbar) {
110 // Enable the delete menu item.
111 DomainsActivity.deleteMenuItem.setEnabled(true);
113 // Get the current theme status.
114 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
116 // Set the delete icon according to the theme.
117 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
118 DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_night);
120 DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_day);
124 // Display the domain settings fragment.
125 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
126 } else { // The device in in single-paned mode
127 // Save the domains listview position.
128 DomainsActivity.domainsListViewPosition = domainsListView.getFirstVisiblePosition();
130 // Show `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
131 if (!DomainsActivity.dismissingSnackbar) {
132 DomainsActivity.deleteMenuItem.setVisible(true);
135 // Hide the add domain FAB.
136 FloatingActionButton addDomainFAB = getActivity().findViewById(R.id.add_domain_fab);
139 // Display the domain settings fragment.
140 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
144 // Return the domains list fragment.
145 return domainsListFragmentView;