]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java
Save and restore the app state. https://redmine.stoutner.com/issues/461
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / DomainsListFragment.java
1 /*
2  * Copyright © 2017-2020 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.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;
30
31 import androidx.annotation.NonNull;
32 import androidx.fragment.app.Fragment;
33 import androidx.fragment.app.FragmentManager;
34
35 import com.google.android.material.floatingactionbutton.FloatingActionButton;
36
37 import com.stoutner.privacybrowser.R;
38 import com.stoutner.privacybrowser.activities.DomainsActivity;
39
40 public class DomainsListFragment extends Fragment {
41     // Instantiate the dismiss snackbar interface.
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(@NonNull Context context) {
50         // Run the default commands.
51         super.onAttach(context);
52
53         // Populate 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         // Get a handle for the domains listview.
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 warning 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                 // Remove the incorrect lint warning below that the domain settings fragment view might be null.
86                 assert domainSettingsFragmentView != null;
87
88                 // Get a handle for the domains activity.
89                 DomainsActivity domainsActivity = new DomainsActivity();
90
91                 // Save the domain settings.
92                 domainsActivity.saveDomainSettings(domainSettingsFragmentView, getResources());
93             }
94
95             // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database.
96             DomainsActivity.currentDomainDatabaseId = (int) id;
97
98             // Add `currentDomainDatabaseId` to `argumentsBundle`.
99             Bundle argumentsBundle = new Bundle();
100             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId);
101
102             // Add the arguments bundle to the domain settings fragment.
103             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
104             domainSettingsFragment.setArguments(argumentsBundle);
105
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);
112
113                     // Get the current theme status.
114                     int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
115
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);
119                     } else {
120                         DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_day);
121                     }
122                 }
123
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();
129
130                 // Show `deleteMenuItem` if the system is not waiting for a `Snackbar` to be dismissed.
131                 if (!DomainsActivity.dismissingSnackbar) {
132                     DomainsActivity.deleteMenuItem.setVisible(true);
133                 }
134
135                 // Hide the add domain FAB.
136                 FloatingActionButton addDomainFAB = getActivity().findViewById(R.id.add_domain_fab);
137                 addDomainFAB.hide();
138
139                 // Display the domain settings fragment.
140                 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
141             }
142         });
143
144         // Return the domains list fragment.
145         return domainsListFragmentView;
146     }
147 }