Migrate to AndroidX from the Android Support Library. https://redmine.stoutner.com...
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / GuideTabFragment.java
1 /*
2  * Copyright © 2016-2019 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.annotation.SuppressLint;
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 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
34
35 public class GuideTabFragment extends Fragment {
36     // `tabNumber` is used in `onCreate()` and `onCreateView()`.
37     private int tabNumber;
38
39     // Store the tab number in the arguments bundle.
40     public static GuideTabFragment createTab (int tab) {
41         // Create a bundle.
42         Bundle bundle = new Bundle();
43
44         // Store the tab number in the bundle.
45         bundle.putInt("Tab", tab);
46
47         // Add the bundle to the fragment.
48         GuideTabFragment guideTabFragment = new GuideTabFragment();
49         guideTabFragment.setArguments(bundle);
50
51         // Return the new fragment.
52         return guideTabFragment;
53     }
54
55     @Override
56     public void onCreate(Bundle savedInstanceState) {
57         // Run the default commands.
58         super.onCreate(savedInstanceState);
59
60         // Remove the lint warning that `getArguments()` might be null.
61         assert getArguments() != null;
62
63         // Store the tab number in a class variable.
64         tabNumber = getArguments().getInt("Tab");
65     }
66
67     @SuppressLint("SetJavaScriptEnabled")
68     @Override
69     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
70         // 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.
71         View tabLayout = inflater.inflate(R.layout.bare_webview, container, false);
72
73         // Get a handle for `tabWebView`.
74         WebView tabWebView = (WebView) tabLayout;
75
76         // Load the tabs according to the theme.
77         if (MainWebViewActivity.darkTheme) {  // The dark theme is applied.
78             // Set the background color.  The deprecated `.getColor()` must be used until API >= 23.
79             //noinspection deprecation
80             tabWebView.setBackgroundColor(getResources().getColor(R.color.gray_850));
81
82             // Tab numbers start at 0.
83             switch (tabNumber) {
84                 case 0:
85                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_overview_dark.html");
86                     break;
87
88                 case 1:
89                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_javascript_dark.html");
90                     break;
91
92                 case 2:
93                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_local_storage_dark.html");
94                     break;
95
96                 case 3:
97                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_user_agent_dark.html");
98                     break;
99
100                 case 4:
101                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_requests_dark.html");
102                     break;
103
104                 case 5:
105                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_domain_settings_dark.html");
106                     break;
107
108                 case 6:
109                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates_dark.html");
110                     break;
111
112                 case 7:
113                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tor_dark.html");
114                     break;
115
116                 case 8:
117                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tracking_ids_dark.html");
118                     break;
119
120                 case 9:
121                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_bookmarks_dark.html");
122                     break;
123             }
124         } else {  // The light theme is applied.
125             // Tab numbers start at 0.
126             switch (tabNumber) {
127                 case 0:
128                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_overview_light.html");
129                     break;
130
131                 case 1:
132                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_javascript_light.html");
133                     break;
134
135                 case 2:
136                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_local_storage_light.html");
137                     break;
138
139                 case 3:
140                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_user_agent_light.html");
141                     break;
142
143                 case 4:
144                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_requests_light.html");
145                     break;
146
147                 case 5:
148                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_domain_settings_light.html");
149                     break;
150
151                 case 6:
152                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_ssl_certificates_light.html");
153                     break;
154
155                 case 7:
156                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tor_light.html");
157                     break;
158
159                 case 8:
160                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tracking_ids_light.html");
161                     break;
162
163                 case 9:
164                     tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_bookmarks_light.html");
165             }
166         }
167
168         // Return the formatted `tabLayout`.
169         return tabLayout;
170     }
171 }