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