2 * Copyright © 2017-2018 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.fragments;
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;
34 import com.stoutner.privacybrowser.R;
35 import com.stoutner.privacybrowser.activities.DomainsActivity;
36 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
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);
43 // Initialize `domainsListView`.
44 ListView domainsListView = domainsListFragmentView.findViewById(R.id.domains_listview);
46 // Remove the incorrect lint error below that `.getSupportFragmentManager()` might be null.
47 assert getActivity() != null;
49 // Get a handle for `supportFragmentManager`.
50 final FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
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;
57 DomainsActivity.undoDeleteSnackbar.dismiss();
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();
65 // Get a handle for the domains activity.
66 DomainsActivity domainsActivity = new DomainsActivity();
68 // Save the domain settings.
69 domainsActivity.saveDomainSettings(domainSettingsFragmentView, getResources());
72 // Store the new `currentDomainDatabaseId`, converting it from `long` to `int` to match the format of the domains database.
73 DomainsActivity.currentDomainDatabaseId = (int) id;
75 // Add `currentDomainDatabaseId` to `argumentsBundle`.
76 Bundle argumentsBundle = new Bundle();
77 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, DomainsActivity.currentDomainDatabaseId);
79 // Add the arguments bundle to the domain settings fragment.
80 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
81 domainSettingsFragment.setArguments(argumentsBundle);
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 the delete menu item.
88 DomainsActivity.deleteMenuItem.setEnabled(true);
90 // Set the delete icon according to the theme.
91 if (MainWebViewActivity.darkTheme) {
92 DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_dark);
94 DomainsActivity.deleteMenuItem.setIcon(R.drawable.delete_light);
98 // Display the domain settings fragment.
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);
106 // Hide the add domain FAB.
107 FloatingActionButton addDomainFAB = getActivity().findViewById(R.id.add_domain_fab);
108 addDomainFAB.setVisibility(View.GONE);
110 // Display the domain settings fragment.
111 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
115 return domainsListFragmentView;