]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/GuideTabFragment.java
Fix incorrect pinned mismatch errors. https://redmine.stoutner.com/issues/591
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / GuideTabFragment.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 GuideTabFragment extends Fragment {
35     // Define the class variables.
36     private int tabNumber;
37     private View tabLayout;
38
39     // Store the tab number in the arguments bundle.
40     public static GuideTabFragment createTab (int tabNumber) {
41         // Create a bundle.
42         Bundle bundle = new Bundle();
43
44         // Store the tab number in the bundle.
45         bundle.putInt("tab_number", tabNumber);
46
47         // Create a new guide tab fragment.
48         GuideTabFragment guideTabFragment = new GuideTabFragment();
49
50         // Add the bundle to the fragment.
51         guideTabFragment.setArguments(bundle);
52
53         // Return the new fragment.
54         return guideTabFragment;
55     }
56
57     @Override
58     public void onCreate(Bundle savedInstanceState) {
59         // Run the default commands.
60         super.onCreate(savedInstanceState);
61
62         // Get a handle for the arguments.
63         Bundle arguments = getArguments();
64
65         // Remove the lint warning below that arguments might be null.
66         assert arguments != null;
67
68         // Store the tab number in a class variable.
69         tabNumber = arguments.getInt("tab_number");
70     }
71
72     @Override
73     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
74         // Inflate the layout.  The fragment will take care of attaching the root automatically.
75         tabLayout = inflater.inflate(R.layout.bare_webview, container, false);
76
77         // Get a handle for the tab WebView.
78         WebView tabWebView = (WebView) tabLayout;
79
80         // Get the current theme status.
81         int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
82
83         // Load the tabs according to the theme.
84         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {  // The dark theme is applied.
85             tabWebView.setBackgroundColor(getResources().getColor(R.color.gray_850));
86
87             // Tab numbers start at 0.
88             switch (tabNumber) {
89                 case 0:
90                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_overview_dark.html");
91                     break;
92
93                 case 1:
94                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_javascript_dark.html");
95                     break;
96
97                 case 2:
98                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_local_storage_dark.html");
99                     break;
100
101                 case 3:
102                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_user_agent_dark.html");
103                     break;
104
105                 case 4:
106                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_requests_dark.html");
107                     break;
108
109                 case 5:
110                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_domain_settings_dark.html");
111                     break;
112
113                 case 6:
114                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates_dark.html");
115                     break;
116
117                 case 7:
118                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_proxies_dark.html");
119                     break;
120
121                 case 8:
122                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tracking_ids_dark.html");
123                     break;
124             }
125         } else {  // The light theme is applied.
126             // Tab numbers start at 0.
127             switch (tabNumber) {
128                 case 0:
129                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_overview_light.html");
130                     break;
131
132                 case 1:
133                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_javascript_light.html");
134                     break;
135
136                 case 2:
137                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_local_storage_light.html");
138                     break;
139
140                 case 3:
141                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_user_agent_light.html");
142                     break;
143
144                 case 4:
145                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_requests_light.html");
146                     break;
147
148                 case 5:
149                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_domain_settings_light.html");
150                     break;
151
152                 case 6:
153                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates_light.html");
154                     break;
155
156                 case 7:
157                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_proxies_light.html");
158                     break;
159
160                 case 8:
161                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tracking_ids_light.html");
162                     break;
163             }
164         }
165
166         // Scroll the WebView if the saved instance state is not null.
167         if (savedInstanceState != null) {
168             tabWebView.post(() -> tabWebView.setScrollY(savedInstanceState.getInt("scroll_y")));
169         }
170
171         // Return the formatted `tabLayout`.
172         return tabLayout;
173     }
174
175     @Override
176     public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
177         // Run the default commands.
178         super.onSaveInstanceState(savedInstanceState);
179
180         // Get a handle for the tab WebView.  A class variable cannot be used because it gets out of sync when restarting.
181         WebView tabWebView = (WebView) tabLayout;
182
183         // Save the scroll Y position if the tab WebView is not null, which can happen if a tab is not currently selected.
184         if (tabWebView != null) {
185             savedInstanceState.putInt("scroll_y", tabWebView.getScrollY());
186         }
187     }
188 }