]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java
41613170b0f59e68795191aa5bd4b223e6cce650
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / DomainsListFragment.java
1 /*
2  * Copyright © 2017-2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.fragments;
21
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;
29
30 import androidx.annotation.NonNull;
31 import androidx.fragment.app.Fragment;  // The AndroidX fragment must be used until minimum API >= 23.  Otherwise `getContext()` does not work.
32 import androidx.fragment.app.FragmentManager;
33
34 import com.google.android.material.floatingactionbutton.FloatingActionButton;
35
36 import com.stoutner.privacybrowser.R;
37 import com.stoutner.privacybrowser.activities.DomainsActivity;
38 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39
40 public class DomainsListFragment extends Fragment {
41     // Instantiate the dismiss snackbar interface handle.
42     private DismissSnackbarInterface dismissSnackbarInterface;
43
44     // Define the public dismiss snackbar interface.
45     public interface DismissSnackbarInterface {
46         void dismissSnackbar();
47     }
48
49     public void onAttach(Context context) {
50         // Run the default commands.
51         super.onAttach(context);
52
53         // Get a handle for the dismiss snackbar interface.
54         dismissSnackbarInterface = (DismissSnackbarInterface) context;
55     }
56
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);
60
61         // Initialize `domainsListView`.
62         ListView domainsListView = domainsListFragmentView.findViewById(R.id.domains_listview);
63
64         // Remove the incorrect lint error below that `.getSupportFragmentManager()` might be null.
65         assert getActivity() != null;
66
67         // Get a handle for the support fragment manager.
68         final FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
69
70         domainsListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
71             // Dismiss the snackbar if it is visible.
72             dismissSnackbarInterface.dismissSnackbar();
73
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);
78
79                 // Remove the incorrect lint error below that the domain settings fragment might be null.
80                 assert domainSettingsFragment != null;
81
82                 // Get a handle for the domain settings fragment view.
83                 View domainSettingsFragmentView = domainSettingsFragment.getView();
84
85                 // Get a handle for the domains activity.
86                 DomainsActivity domainsActivity = new DomainsActivity();
87
88                 // Save the domain settings.
89                 domainsActivity.saveDomainSettings(domainSettingsFragmentView, getResources());
90             }
91
92             // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database.
93             DomainsActivity.currentDomainDatabaseId = (int) id;
94
95             // Add `currentDomainDatabaseId` to `argumentsBundle`.
96             Bundle argumentsBundle = new Bundle();
97             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId);
98
99             // Add the arguments bundle to the domain settings fragment.
100             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
101             domainSettingsFragment.setArguments(argumentsBundle);
102
103             // Display the domain settings fragment.
104             if (DomainsActivity.twoPanedMode) {  // The device in in two-paned mode.
105                 // enable `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
106                 if (!DomainsActivity.dismissingSnackbar) {
107                     // Enable the delete menu item.
108                     DomainsActivity.deleteMenuItem.setEnabled(true);
109
110                     // Set the delete icon according to the theme.
111                     if (MainWebViewActivity.darkTheme) {
112                         DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_dark);
113                     } else {
114                         DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_light);
115                     }
116                 }
117
118                 // Display the domain settings fragment.
119                 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
120             } else { // The device in in single-paned mode
121                 // Show `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
122                 if (!DomainsActivity.dismissingSnackbar) {
123                     DomainsActivity.deleteMenuItem.setVisible(true);
124                 }
125
126                 // Hide the add domain FAB.
127                 FloatingActionButton addDomainFAB = getActivity().findViewById(R.id.add_domain_fab);
128                 addDomainFAB.hide();
129
130                 // Display the domain settings fragment.
131                 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
132             }
133         });
134
135         return domainsListFragmentView;
136     }
137 }