]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java
Migrate to AndroidX from the Android Support Library. https://redmine.stoutner.com...
[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.os.Bundle;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.AdapterView;
27 import android.widget.ListView;
28
29 import androidx.annotation.NonNull;
30 import androidx.fragment.app.Fragment;  // The AndroidX fragment must be used until minimum API >= 23.  Otherwise `getContext()` does not work.
31 import androidx.fragment.app.FragmentManager;
32
33 import com.google.android.material.floatingactionbutton.FloatingActionButton;
34
35 import com.stoutner.privacybrowser.R;
36 import com.stoutner.privacybrowser.activities.DomainsActivity;
37 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
38
39 public class DomainsListFragment extends Fragment {
40     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
41         // Inflate `domains_list_fragment`.  `false` does not attach it to the root `container`.
42         View domainsListFragmentView = inflater.inflate(R.layout.domains_list_fragment, container, false);
43
44         // Initialize `domainsListView`.
45         ListView domainsListView = domainsListFragmentView.findViewById(R.id.domains_listview);
46
47         // Remove the incorrect lint error below that `.getSupportFragmentManager()` might be null.
48         assert getActivity() != null;
49
50         // Get a handle for `supportFragmentManager`.
51         final FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
52
53         domainsListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
54             // Dismiss `undoDeleteSnackbar` if it is currently displayed (because a domain has just been deleted).
55             if ((DomainsActivity.undoDeleteSnackbar != null) && (DomainsActivity.undoDeleteSnackbar.isShown())) {
56                 DomainsActivity.dismissingSnackbar = true;
57
58                 DomainsActivity.undoDeleteSnackbar.dismiss();
59             }
60
61             // Save the current domain settings if operating in two-paned mode and a domain is currently selected.
62             if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled()) {
63                 // Get a handle for the domain settings fragment.
64                 Fragment domainSettingsFragment = supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container);
65
66                 // Remove the incorrect lint error below that the domain settings fragment might be null.
67                 assert domainSettingsFragment != null;
68
69                 // Get a handle for the domain settings fragment view.
70                 View domainSettingsFragmentView = domainSettingsFragment.getView();
71
72                 // Get a handle for the domains activity.
73                 DomainsActivity domainsActivity = new DomainsActivity();
74
75                 // Save the domain settings.
76                 domainsActivity.saveDomainSettings(domainSettingsFragmentView, getResources());
77             }
78
79             // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database.
80             DomainsActivity.currentDomainDatabaseId = (int) id;
81
82             // Add `currentDomainDatabaseId` to `argumentsBundle`.
83             Bundle argumentsBundle = new Bundle();
84             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId);
85
86             // Add the arguments bundle to the domain settings fragment.
87             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
88             domainSettingsFragment.setArguments(argumentsBundle);
89
90             // Display the domain settings fragment.
91             if (DomainsActivity.twoPanedMode) {  // The device in in two-paned mode.
92                 // enable `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
93                 if (!DomainsActivity.dismissingSnackbar) {
94                     // Enable the delete menu item.
95                     DomainsActivity.deleteMenuItem.setEnabled(true);
96
97                     // Set the delete icon according to the theme.
98                     if (MainWebViewActivity.darkTheme) {
99                         DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_dark);
100                     } else {
101                         DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_light);
102                     }
103                 }
104
105                 // Display the domain settings fragment.
106                 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
107             } else { // The device in in single-paned mode
108                 // Show `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
109                 if (!DomainsActivity.dismissingSnackbar) {
110                     DomainsActivity.deleteMenuItem.setVisible(true);
111                 }
112
113                 // Hide the add domain FAB.
114                 FloatingActionButton addDomainFAB = getActivity().findViewById(R.id.add_domain_fab);
115                 addDomainFAB.hide();
116
117                 // Display the domain settings fragment.
118                 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
119             }
120         });
121
122         return domainsListFragmentView;
123     }
124 }