2 * Copyright © 2017-2020,2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
6 * Privacy Browser Android 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 Android 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 Android. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.fragments;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.AdapterView;
28 import android.widget.ListView;
30 import androidx.annotation.NonNull;
31 import androidx.fragment.app.Fragment;
32 import androidx.fragment.app.FragmentManager;
34 import com.google.android.material.floatingactionbutton.FloatingActionButton;
36 import com.stoutner.privacybrowser.R;
37 import com.stoutner.privacybrowser.activities.DomainsActivity;
39 public class DomainsListFragment extends Fragment {
40 // Instantiate the dismiss snackbar interface.
41 private DismissSnackbarInterface dismissSnackbarInterface;
43 // Define the public dismiss snackbar interface.
44 public interface DismissSnackbarInterface {
45 void dismissSnackbar();
48 public void onAttach(@NonNull Context context) {
49 // Run the default commands.
50 super.onAttach(context);
52 // Populate the dismiss snackbar interface.
53 dismissSnackbarInterface = (DismissSnackbarInterface) context;
56 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
57 // Inflate `domains_list_fragment`. `false` does not attach it to the root `container`.
58 View domainsListFragmentView = inflater.inflate(R.layout.domains_list_fragment, container, false);
60 // Get a handle for the domains listview.
61 ListView domainsListView = domainsListFragmentView.findViewById(R.id.domains_listview);
63 // Remove the incorrect lint error below that `.getSupportFragmentManager()` might be null.
64 assert getActivity() != null;
66 // Get a handle for the support fragment manager.
67 final FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
69 domainsListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
70 // Dismiss the snackbar if it is visible.
71 dismissSnackbarInterface.dismissSnackbar();
73 // Save the current domain settings if operating in two-paned mode and a domain is currently selected.
74 if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled()) {
75 // Get a handle for the domain settings fragment.
76 Fragment domainSettingsFragment = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container);
78 // Remove the incorrect lint warning below that the domain settings fragment might be null.
79 assert domainSettingsFragment != null;
81 // Get a handle for the domain settings fragment view.
82 View domainSettingsFragmentView = domainSettingsFragment.getView();
84 // Remove the incorrect lint warning below that the domain settings fragment view might be null.
85 assert domainSettingsFragmentView != null;
87 // Get a handle for the domains activity.
88 DomainsActivity domainsActivity = new DomainsActivity();
90 // Save the domain settings.
91 domainsActivity.saveDomainSettings(domainSettingsFragmentView, getResources());
94 // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database.
95 DomainsActivity.currentDomainDatabaseId = (int) id;
97 // Add `currentDomainDatabaseId` to `argumentsBundle`.
98 Bundle argumentsBundle = new Bundle();
99 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId);
101 // Add the arguments bundle to the domain settings fragment.
102 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
103 domainSettingsFragment.setArguments(argumentsBundle);
105 // Check to see if the device is in two paned mode.
106 if (DomainsActivity.twoPanedMode) { // The device in in two-paned mode.
107 // enable `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
108 if (!DomainsActivity.dismissingSnackbar) {
109 // Enable the delete menu item.
110 DomainsActivity.deleteMenuItem.setEnabled(true);
112 // Set the delete icon.
113 DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_enabled);
116 // Display the domain settings fragment.
117 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
118 } else { // The device in in single-paned mode
119 // Save the domains listview position.
120 DomainsActivity.domainsListViewPosition = domainsListView.getFirstVisiblePosition();
122 // Show `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
123 if (!DomainsActivity.dismissingSnackbar) {
124 DomainsActivity.deleteMenuItem.setVisible(true);
127 // Hide the add domain FAB.
128 FloatingActionButton addDomainFAB = getActivity().findViewById(R.id.add_domain_fab);
131 // Display the domain settings fragment.
132 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
136 // Return the domains list fragment.
137 return domainsListFragmentView;