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.AdapterView.OnItemClickListener
29 import android.widget.ListView
31 import androidx.fragment.app.Fragment
33 import com.google.android.material.floatingactionbutton.FloatingActionButton
35 import com.stoutner.privacybrowser.R
36 import com.stoutner.privacybrowser.activities.DomainsActivity
38 class DomainsListFragment : Fragment() {
39 // Declare the class variables.
40 private lateinit var dismissSnackbarInterface: DismissSnackbarInterface
41 private lateinit var saveDomainSettingsInterface: SaveDomainSettingsInterface
43 // Define the public dismiss snackbar interface.
44 interface DismissSnackbarInterface {
48 // Define the public save domain interface.
49 interface SaveDomainSettingsInterface {
50 fun saveDomainSettings(view: View)
53 override fun onAttach(context: Context) {
54 // Run the default commands.
55 super.onAttach(context)
57 // Populate the interfaces.
58 dismissSnackbarInterface = context as DismissSnackbarInterface
59 saveDomainSettingsInterface = context as SaveDomainSettingsInterface
62 override fun onCreateView(layoutInflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
63 // Inflate the layout. The fragment will take care of attaching the root automatically.
64 val domainsListFragmentView = layoutInflater.inflate(R.layout.domains_list_fragment, container, false)
66 // Get a handle for the domains list view.
67 val domainsListView = domainsListFragmentView.findViewById<ListView>(R.id.domains_listview)
69 // Get a handle for the support fragment manager.
70 val supportFragmentManager = requireActivity().supportFragmentManager
72 // Handle clicks on the domains list view.
73 domainsListView.onItemClickListener = OnItemClickListener { _: AdapterView<*>, _: View, _: Int, id: Long ->
74 // Dismiss the snackbar if it is visible.
75 dismissSnackbarInterface.dismissSnackbar()
77 // Save the current domain settings if operating in two-paned mode and a domain is currently selected.
78 if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled) {
79 // Get a handle for the domain settings fragment.
80 val domainSettingsFragment = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container)!!
82 // Get a handle for the domain settings fragment view.
83 val domainSettingsFragmentView = domainSettingsFragment.requireView()
85 // Save the domain settings.
86 saveDomainSettingsInterface.saveDomainSettings(domainSettingsFragmentView)
89 // Store the new current domain database ID, converting it from long to int to match the format of the domains database.
90 DomainsActivity.currentDomainDatabaseId = id.toInt()
92 // Create an arguments bundle.
93 val argumentsBundle = Bundle()
95 // Add the current domain database ID to the arguments bundle.
96 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId)
98 // Create a new domains settings fragment instance.
99 val domainSettingsFragment = DomainSettingsFragment()
101 // Add the arguments bundle to the domain settings fragment.
102 domainSettingsFragment.arguments = argumentsBundle
104 // Check to see if the device is in two paned mode.
105 if (DomainsActivity.twoPanedMode) { // The device in in two-paned mode.
106 // Enable the delete menu item if the system is not waiting for a snackbar to be dismissed.
107 if (!DomainsActivity.dismissingSnackbar) {
108 // Enable the delete menu item.
109 DomainsActivity.deleteMenuItem.isEnabled = true
112 // Display the domain settings fragment.
113 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit()
114 } else { // The device in in single-paned mode
115 // Save the domains listview position.
116 DomainsActivity.domainsListViewPosition = domainsListView.firstVisiblePosition
118 // Show the delete menu item if the system is not waiting for a snackbar to be dismissed.
119 if (!DomainsActivity.dismissingSnackbar)
120 DomainsActivity.deleteMenuItem.isVisible = true
122 // Get a handle for the add domain floating action button.
123 val addDomainFab = requireActivity().findViewById<FloatingActionButton>(R.id.add_domain_fab)
125 // Hide the add domain FAB.
128 // Display the domain settings fragment.
129 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit()
133 // Return the domains list fragment.
134 return domainsListFragmentView