]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/AboutWebViewFragment.java
Add share, copy, and save options to About > Version. https://redmine.stoutner.com...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / AboutWebViewFragment.java
1 /*
2  * Copyright © 2016-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.res.Configuration;
23 import android.os.Bundle;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.webkit.WebView;
28
29 import androidx.annotation.NonNull;
30 import androidx.fragment.app.Fragment;
31
32 import com.stoutner.privacybrowser.R;
33
34 public class AboutWebViewFragment extends Fragment {
35     // Declare the class constants.
36     final static String TAB_NUMBER = "tab_number";
37
38     // Declare the class variables.
39     private int tabNumber;
40
41     // Declare the class views.
42     private View aboutWebViewLayout;
43
44     public static AboutWebViewFragment createTab(int tabNumber) {
45         // Create an arguments bundle.
46         Bundle argumentsBundle = new Bundle();
47
48         // Store the arguments in the bundle.
49         argumentsBundle.putInt(TAB_NUMBER, tabNumber);
50
51         // Create a new instance of the tab fragment.
52         AboutWebViewFragment aboutWebViewFragment = new AboutWebViewFragment();
53
54         // Add the arguments bundle to the fragment.
55         aboutWebViewFragment.setArguments(argumentsBundle);
56
57         // Return the new fragment.
58         return aboutWebViewFragment;
59     }
60
61     @Override
62     public void onCreate(Bundle savedInstanceState) {
63         // Run the default commands.
64         super.onCreate(savedInstanceState);
65
66         // Get a handle for the arguments.
67         Bundle arguments = getArguments();
68
69         // Remove the incorrect lint warning below that arguments might be null.
70         assert arguments != null;
71
72         // Store the arguments in class variables.
73         tabNumber = arguments.getInt(TAB_NUMBER);
74     }
75
76     @Override
77     public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
78         // Get the current theme status.
79         int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
80
81         // Inflate the layout.  Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container.  The fragment will take care of attaching the root automatically.
82         aboutWebViewLayout = layoutInflater.inflate(R.layout.bare_webview, container, false);
83
84         // Get a handle for tab WebView.
85         WebView tabWebView = (WebView) aboutWebViewLayout;
86
87         // Load the tabs according to the theme.
88         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {  // The light theme is applied.
89             switch (tabNumber) {
90                 case 1:
91                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_permissions_light.html");
92                     break;
93
94                 case 2:
95                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_privacy_policy_light.html");
96                     break;
97
98                 case 3:
99                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_changelog_light.html");
100                     break;
101
102                 case 4:
103                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_licenses_light.html");
104                     break;
105
106                 case 5:
107                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_contributors_light.html");
108                     break;
109
110                 case 6:
111                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_links_light.html");
112                     break;
113             }
114         } else {  // The dark theme is applied.
115             // Set the background color.  The deprecated `.getColor()` must be used until the minimum API >= 23.
116             tabWebView.setBackgroundColor(getResources().getColor(R.color.gray_850));
117
118             // Tab numbers start at 0, with the WebView tabs starting at 1.
119             switch (tabNumber) {
120                 case 1:
121                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_permissions_dark.html");
122                     break;
123
124                 case 2:
125                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_privacy_policy_dark.html");
126                     break;
127
128                 case 3:
129                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_changelog_dark.html");
130                     break;
131
132                 case 4:
133                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_licenses_dark.html");
134                     break;
135
136                 case 5:
137                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_contributors_dark.html");
138                     break;
139
140                 case 6:
141                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/about_links_dark.html");
142                     break;
143             }
144         }
145
146         // Scroll the tab if the saved instance state is not null.
147         if (savedInstanceState != null) {
148             aboutWebViewLayout.post(() -> {
149                 aboutWebViewLayout.setScrollX(savedInstanceState.getInt("scroll_x"));
150                 aboutWebViewLayout.setScrollY(savedInstanceState.getInt("scroll_y"));
151             });
152         }
153
154         // Return the tab layout.
155         return aboutWebViewLayout;
156     }
157
158     @Override
159     public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
160         // Run the default commands.
161         super.onSaveInstanceState(savedInstanceState);
162
163         // Save the scroll positions if the layout is not null, which can happen if a tab is not currently selected.
164         if (aboutWebViewLayout != null) {
165             savedInstanceState.putInt("scroll_x", aboutWebViewLayout.getScrollX());
166             savedInstanceState.putInt("scroll_y", aboutWebViewLayout.getScrollY());
167         }
168     }
169 }