]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/GuideTabFragment.java
Create a dark theme for `GuideActivity`.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / GuideTabFragment.java
1 /*
2  * Copyright © 2016-2017 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.graphics.ColorMatrixColorFilter;
23 import android.graphics.Paint;
24 import android.os.Bundle;
25 import android.support.v4.app.Fragment;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.webkit.WebView;
30
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
33
34 public class GuideTabFragment extends Fragment {
35     // `tabNumber` is used in `onCreate()` and `onCreateView()`.
36     private int tabNumber;
37
38     // GuideTabFragment.createTab stores the tab number in the bundle arguments so it can be referenced from onCreate().
39     public static GuideTabFragment createTab (int tab) {
40         Bundle thisTabArguments = new Bundle();
41         thisTabArguments.putInt("Tab", tab);
42
43         GuideTabFragment thisTab = new GuideTabFragment();
44         thisTab.setArguments(thisTabArguments);
45         return thisTab;
46     }
47
48     @Override
49     public void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51
52         // Store the tab number in `tabNumber`.
53         tabNumber = getArguments().getInt("Tab");
54     }
55
56     @Override
57     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
58         // 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.
59         View tabLayout = inflater.inflate(R.layout.bare_webview, container, false);
60
61         // Get a handle for `tabWebView`.
62         WebView tabWebView = (WebView) tabLayout;
63
64         // Filter the colors if `darkTheme` is `true`.
65         if (MainWebViewActivity.darkTheme) {
66             // Initialize `darkPaint`.
67             Paint darkPaint = new Paint();
68
69             // Setup a float array that inverts and tempers the colors (no hard whites or blacks).
70             float[] darkFilterFloatArray = {
71                     -.8f, 0, 0, 0, 255,  // Red.
72                     0, -.8f, 0, 0, 255,  // Green.
73                     0, 0, -.8f, 0, 255,  // Blue.
74                     0, 0, 0, .8f, 0      // Alpha.
75             };
76
77             // Set `darkPaint` to use `darkFilterFloatArray`.
78             darkPaint.setColorFilter(new ColorMatrixColorFilter(darkFilterFloatArray));
79
80             // Apply `darkPaint` to `tabWebView`.
81             tabWebView.setLayerType(View.LAYER_TYPE_HARDWARE, darkPaint);
82         } else {
83             // Reset `tabWebView` to use the normal colors.
84             tabWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
85         }
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.html");
91                 break;
92
93             case 1:
94                 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_javascript.html");
95                 break;
96
97             case 2:
98                 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_local_storage.html");
99                 break;
100
101             case 3:
102                 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_user_agent.html");
103                 break;
104
105             case 4:
106                 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_domain_settings.html");
107                 break;
108
109             case 5:
110                 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tor.html");
111                 break;
112
113             case 6:
114                 tabWebView.loadUrl("file:///android_asset/" + getString(R.string.android_asset_path) + "/guide_tracking_ids.html");
115                 break;
116         }
117
118         return tabLayout;
119     }
120 }