Create a dark theme for `DomainsActivity` and `DomainsSettingsActivity`.
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / DomainSettingsFragment.java
1 /*
2  * Copyright © 2017 Soren Stoutner <soren@stoutner.com>.
3  *
4  * Huawei spinner code fix contributed 2017 Thomas Jensen <lianergoist@vongriffen.dk>.  Copyright assigned to Soren Stoutner <soren@stoutner.com>.
5  *
6  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
7  *
8  * Privacy Browser is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Privacy Browser is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 package com.stoutner.privacybrowser.fragments;
23
24 import android.annotation.SuppressLint;
25 import android.content.Context;
26 import android.database.Cursor;
27 import android.os.Build;
28 import android.os.Bundle;
29 // We have to use `android.support.v4.app.Fragment` until minimum API >= 23.  Otherwise we cannot call `getContext()`.
30 import android.support.v4.app.Fragment;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.webkit.WebView;
35 import android.widget.AdapterView;
36 import android.widget.ArrayAdapter;
37 import android.widget.CompoundButton;
38 import android.widget.EditText;
39 import android.widget.ImageView;
40 import android.widget.LinearLayout;
41 import android.widget.Spinner;
42 import android.widget.Switch;
43 import android.widget.TextView;
44
45 import com.stoutner.privacybrowser.R;
46 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
47 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
48
49 public class DomainSettingsFragment extends Fragment {
50     // `DATABASE_ID` is used by activities calling this fragment.
51     public static final String DATABASE_ID = "database_id";
52
53     // `databaseId` is used in `onCreate()` and `onCreateView()`.
54     private int databaseId;
55
56     @Override
57     public void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59
60         // Store the database id in `databaseId`.
61         databaseId = getArguments().getInt(DATABASE_ID);
62     }
63
64     // We have to use the deprecated `getDrawable()` until the minimum API >= 21.
65     @SuppressWarnings("deprecation")
66     @Override
67     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
68         // Inflate `domain_settings`.  `false` does not attach it to the root `container`.
69         View domainSettingsView = inflater.inflate(R.layout.domain_settings, container, false);
70
71         // Get a handle for the `Context`.
72         Context context = getContext();
73
74         // Get handles for the views in the fragment.
75         EditText domainNameEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_name_edittext);
76         Switch javaScriptEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_javascript_switch);
77         final ImageView javaScriptImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_javascript_imageview);
78         Switch firstPartyCookiesEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_first_party_cookies_switch);
79         final ImageView firstPartyCookiesImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_first_party_cookies_imageview);
80         LinearLayout thirdPartyCookiesLinearLayout = (LinearLayout) domainSettingsView.findViewById(R.id.domain_settings_third_party_cookies_linearlayout);
81         final Switch thirdPartyCookiesEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_third_party_cookies_switch);
82         final ImageView thirdPartyCookiesImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_third_party_cookies_imageview);
83         final Switch domStorageEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_dom_storage_switch);
84         final ImageView domStorageImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_dom_storage_imageview);
85         Switch formDataEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_form_data_switch);
86         final ImageView formDataImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_form_data_imageview);
87         Spinner userAgentSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_user_agent_spinner);
88         final TextView userAgentTextView = (TextView) domainSettingsView.findViewById(R.id.domain_settings_user_agent_textview);
89         final EditText customUserAgentEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_custom_user_agent_edittext);
90         Spinner fontSizeSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_font_size_spinner);
91         final ImageView displayWebpageImagesImageView = (ImageView) domainSettingsView.findViewById(R.id.domain_settings_display_webpage_images_imageview);
92         Spinner displayWebpageImagesSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_display_webpage_images_spinner);
93
94         // Initialize the database handler.  `this` specifies the context.  The two `nulls` do not specify the database name or a `CursorFactory`.
95         // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
96         DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
97
98         // Get the database `Cursor` for this ID and move it to the first row.
99         Cursor domainCursor = domainsDatabaseHelper.getCursorForId(databaseId);
100         domainCursor.moveToFirst();
101
102         // Save the `Cursor` entries as variables.
103         String domainNameString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
104         int javaScriptEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT));
105         int firstPartyCookiesEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES));
106         int thirdPartyCookiesEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES));
107         int domStorageEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE));
108         int formDataEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA));
109         final String currentUserAgentString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT));
110         int fontSizeInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE));
111         int displayImagesInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.DISPLAY_IMAGES));
112
113         // Create `ArrayAdapters` for the `Spinners`and their `entry values`.
114         ArrayAdapter<CharSequence> userAgentArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entries, android.R.layout.simple_spinner_item);
115         final ArrayAdapter<CharSequence> userAgentEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entry_values, android.R.layout.simple_spinner_item);
116         ArrayAdapter<CharSequence> fontSizeArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entries, android.R.layout.simple_spinner_item);
117         ArrayAdapter<CharSequence> fontSizeEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entry_values, android.R.layout.simple_spinner_item);
118         final ArrayAdapter<CharSequence> displayImagesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.display_website_images_array, android.R.layout.simple_spinner_item);
119
120         // Some phones running Huawei's customized Android 7.0 have layout problems displaying a spinner with the default `simple_spinner_dropdown_item`.  The Huawei P9 Lite is known to be affected.
121         if (Build.BRAND.equals("HUAWEI") && (Build.VERSION.SDK_INT == 24)) {  // The device is manufactured by Huawei and is running Android 7.0.
122             // Use a customized `simple_spinner_dropdown_item`.  Huawei spinner code fix contributed 2017 Thomas Jensen <lianergoist@vongriffen.dk>.  Copyright assigned to Soren Stoutner <soren@stoutner.com>.
123             userAgentArrayAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item_huawei_fix);
124             fontSizeArrayAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item_huawei_fix);
125             displayImagesArrayAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item_huawei_fix);
126         } else {  // Use the standard `android.R.layout.simple_spinner_dropdown_item` on all other devices.
127             userAgentArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
128             fontSizeArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
129             displayImagesArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
130         }
131
132         // Set the `ArrayAdapters` for the `Spinners`.
133         userAgentSpinner.setAdapter(userAgentArrayAdapter);
134         fontSizeSpinner.setAdapter(fontSizeArrayAdapter);
135         displayWebpageImagesSpinner.setAdapter(displayImagesArrayAdapter);
136
137         // Set the domain name from the the database cursor.
138         domainNameEditText.setText(domainNameString);
139
140         // Set the JavaScript status.
141         if (javaScriptEnabledInt == 1) {  // JavaScript is enabled.
142             javaScriptEnabledSwitch.setChecked(true);
143             javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.javascript_enabled));
144         } else {  // JavaScript is disabled.
145             javaScriptEnabledSwitch.setChecked(false);
146             javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.privacy_mode));
147         }
148
149         // Set the first-party cookies status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
150         if (firstPartyCookiesEnabledInt == 1) {  // First-party cookies are enabled.
151             firstPartyCookiesEnabledSwitch.setChecked(true);
152             firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_enabled));
153         } else {  // First-party cookies are disabled.
154             firstPartyCookiesEnabledSwitch.setChecked(false);
155
156             // Set the icon according to the theme.
157             if (MainWebViewActivity.darkTheme) {
158                 firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_dark));
159             } else {
160                 firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
161             }
162         }
163
164         // Only display third-party cookies if SDK_INT >= 21.
165         if (Build.VERSION.SDK_INT >= 21) {  // Third-party cookies can be configured for API >= 21.
166             // Only enable third-party-cookies if first-party cookies are enabled.
167             if (firstPartyCookiesEnabledInt == 1) {  // First-party cookies are enabled.
168                 // Set the third-party cookies status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
169                 if (thirdPartyCookiesEnabledInt == 1) {  // Both first-party and third-party cookies are enabled.
170                     thirdPartyCookiesEnabledSwitch.setChecked(true);
171                     thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_warning));
172                 } else {  // First party cookies are enabled but third-party cookies are disabled.
173                     thirdPartyCookiesEnabledSwitch.setChecked(false);
174
175                     // Set the icon according to the theme.
176                     if (MainWebViewActivity.darkTheme) {
177                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_dark));
178                     } else {
179                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
180                     }
181                 }
182             } else {  // First-party cookies are disabled.
183                 // Set the status of third-party cookies.
184                 if (thirdPartyCookiesEnabledInt == 1) {
185                     thirdPartyCookiesEnabledSwitch.setChecked(true);
186                 } else {
187                     thirdPartyCookiesEnabledSwitch.setChecked(false);
188                 }
189
190                 // Disable the third-party cookies switch.
191                 thirdPartyCookiesEnabledSwitch.setEnabled(false);
192
193                 // Set the icon according to the theme.
194                 if (MainWebViewActivity.darkTheme) {
195                     thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_ghosted_dark));
196                 } else {
197                     thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_ghosted_light));
198                 }
199             }
200         } else {  // Third-party cookies cannot be configured for API <= 21.
201             // Hide the `LinearLayout` for third-party cookies.
202             thirdPartyCookiesLinearLayout.setVisibility(View.GONE);
203         }
204
205         // Only enable DOM storage if JavaScript is enabled.
206         if (javaScriptEnabledInt == 1) {  // JavaScript is enabled.
207             // Set the DOM storage status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
208             if (domStorageEnabledInt == 1) {  // Both JavaScript and DOM storage are enabled.
209                 domStorageEnabledSwitch.setChecked(true);
210                 domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_enabled));
211             } else {  // JavaScript is enabled but DOM storage is disabled.
212                 // Set the DOM storage switch to off.
213                 domStorageEnabledSwitch.setChecked(false);
214
215                 // Set the icon according to the theme.
216                 if (MainWebViewActivity.darkTheme) {
217                     domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_dark));
218                 } else {
219                     domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_light));
220                 }
221             }
222         } else {  // JavaScript is disabled.
223             // Set the checked status of DOM storage.
224             if (domStorageEnabledInt == 1) {  // DOM storage is enabled but JavaScript is disabled.
225                 domStorageEnabledSwitch.setChecked(true);
226             } else {  // Both JavaScript and DOM storage are disabled.
227                 domStorageEnabledSwitch.setChecked(false);
228             }
229
230             // Disable `domStorageEnabledSwitch`.
231             domStorageEnabledSwitch.setEnabled(false);
232
233             // Set the icon according to the theme.
234             if (MainWebViewActivity.darkTheme) {
235                 domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_ghosted_dark));
236             } else {
237                 domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_ghosted_light));
238             }
239         }
240
241         // Set the form data status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
242         if (formDataEnabledInt == 1) {  // Form data is enabled.
243             formDataEnabledSwitch.setChecked(true);
244             formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_enabled));
245         } else {  // Form data is disabled.
246             // Set the form data switch to off.
247             formDataEnabledSwitch.setChecked(false);
248
249             // Set the icon according to the theme.
250             if (MainWebViewActivity.darkTheme) {
251                 formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_disabled_dark));
252             } else {
253                 formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_disabled_light));
254             }
255         }
256
257         // We need to inflated a `WebView` to get the default user agent.
258         // `@SuppressLint("InflateParams")` removes the warning about using `null` as the `ViewGroup`, which in this case makes sense because we don't want to display `bare_webview` on the screen.  `false` does not attach the view to the root.
259         @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
260         WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
261         final String webViewDefaultUserAgentString = bareWebView.getSettings().getUserAgentString();
262
263         // Get the position of the user agent in `userAgentEntryValuesArrayAdapter`.
264         int userAgentArrayPosition = userAgentEntryValuesArrayAdapter.getPosition(currentUserAgentString);
265
266         // Set the user agent.
267         if (userAgentArrayPosition == -1) {  // We are using a custom `userAgentString`.
268             // Set `userAgentSpinner` to `Custom`.
269             userAgentSpinner.setSelection(userAgentEntryValuesArrayAdapter.getPosition("Custom user agent"));
270
271             // Hide `userAgentTextView`.
272             userAgentTextView.setVisibility(View.GONE);
273
274             // Show `customUserAgentEditText` and set `userAgentString` as the text.
275             customUserAgentEditText.setVisibility(View.VISIBLE);
276             customUserAgentEditText.setText(currentUserAgentString);
277         } else if (currentUserAgentString.equals("WebView default user agent")) {  // We are using the `WebView` default user agent.
278             // Set the `userAgentSpinner` selection.
279             userAgentSpinner.setSelection(userAgentArrayPosition);
280
281             // Show `userAgentTextView` and set the text.
282             userAgentTextView.setVisibility(View.VISIBLE);
283             userAgentTextView.setText(webViewDefaultUserAgentString);
284
285             // Hide `customUserAgentEditText`.
286             customUserAgentEditText.setVisibility(View.GONE);
287         } else {  // We are using a standard user agent.
288             // Set the `userAgentSpinner` selection.
289             userAgentSpinner.setSelection(userAgentArrayPosition);
290
291             // Show `userAgentTextView` and set the text.
292             userAgentTextView.setVisibility(View.VISIBLE);
293             userAgentTextView.setText(currentUserAgentString);
294
295             // Hide `customUserAgentEditText`.
296             customUserAgentEditText.setVisibility(View.GONE);
297         }
298
299         // Set the selected font size.
300         int fontSizeArrayPosition = fontSizeEntryValuesArrayAdapter.getPosition(String.valueOf(fontSizeInt));
301         fontSizeSpinner.setSelection(fontSizeArrayPosition);
302
303         // Set the selected display website images mode.
304         displayWebpageImagesSpinner.setSelection(displayImagesInt);
305
306         // Set the display website images icon.
307         switch (displayImagesInt) {
308             case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_SYSTEM_DEFAULT:
309                 if (MainWebViewActivity.displayWebpageImagesBoolean) {
310                     // Set the icon according to the theme.
311                     if (MainWebViewActivity.darkTheme) {
312                         displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_dark));
313                     } else {
314                         displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
315                     }
316                 } else {
317                     // Set the icon according to the theme.
318                     if (MainWebViewActivity.darkTheme) {
319                         displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_dark));
320                     } else {
321                         displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_light));
322                     }
323                 }
324                 break;
325
326             case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_ENABLED:
327                 // Set the icon according to the theme.
328                 if (MainWebViewActivity.darkTheme) {
329                     displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_dark));
330                 } else {
331                     displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
332                 }
333                 break;
334
335             case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_DISABLED:
336                 // Set the icon according to the theme.
337                 if (MainWebViewActivity.darkTheme) {
338                     displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_dark));
339                 } else {
340                     displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_light));
341                 }
342                 break;
343         }
344
345
346         // Set the `javaScriptEnabledSwitch` `OnCheckedChangeListener()`.
347         javaScriptEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
348             @Override
349             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
350                 if (isChecked) {  // JavaScript is enabled.
351                     // Update the JavaScript icon.
352                     javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.javascript_enabled));
353
354                     // Enable the DOM storage `Switch`.
355                     domStorageEnabledSwitch.setEnabled(true);
356
357                     // Update the DOM storage icon.
358                     if (domStorageEnabledSwitch.isChecked()) {  // DOM storage is enabled.
359                         domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_enabled));
360                     } else {  // DOM storage is disabled.
361                         // Set the icon according to the theme.
362                         if (MainWebViewActivity.darkTheme) {
363                             domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_dark));
364                         } else {
365                             domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_light));
366                         }
367                     }
368                 } else {  // JavaScript is disabled.
369                     // Update the JavaScript icon.
370                     javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.privacy_mode));
371
372                     // Disable the DOM storage `Switch`.
373                     domStorageEnabledSwitch.setEnabled(false);
374
375                     // Set the DOM storage icon according to the theme.
376                     if (MainWebViewActivity.darkTheme) {
377                         domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_ghosted_dark));
378                     } else {
379                         domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_ghosted_light));
380                     }
381                 }
382             }
383         });
384
385         // Set the `firstPartyCookiesEnabledSwitch` `OnCheckedChangeListener()`.
386         firstPartyCookiesEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
387             @Override
388             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
389                 if (isChecked) {  // First-party cookies are enabled.
390                     // Update the first-party cookies icon.
391                     firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_enabled));
392
393                     // Enable the third-party cookies `Switch`.
394                     thirdPartyCookiesEnabledSwitch.setEnabled(true);
395
396                     // Update the third-party cookies icon.
397                     if (thirdPartyCookiesEnabledSwitch.isChecked()) {  // Third-party cookies are enabled.
398                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_warning));
399                     } else {  // Third-party cookies are disabled.
400                         // Set the third-party cookies icon according to the theme.
401                         if (MainWebViewActivity.darkTheme) {
402                             thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_dark));
403                         } else {
404                             thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
405                         }
406                     }
407                 } else {  // First-party cookies are disabled.
408                     // Update the first-party cookies icon according to the theme.
409                     if (MainWebViewActivity.darkTheme) {
410                         firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_dark));
411                     } else {
412                         firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
413                     }
414
415                     // Disable the third-party cookies `Switch`.
416                     thirdPartyCookiesEnabledSwitch.setEnabled(false);
417
418                     // Set the third-party cookies icon according to the theme.
419                     if (MainWebViewActivity.darkTheme) {
420                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_ghosted_dark));
421                     } else {
422                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_ghosted_light));
423                     }
424                 }
425             }
426         });
427
428         // Set the `thirdPartyCookiesEnabledSwitch` `OnCheckedChangeListener()`.
429         thirdPartyCookiesEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
430             @Override
431             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
432                 // Update the icon.
433                 if (isChecked) {
434                     thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_warning));
435                 } else {
436                     // Update the third-party cookies icon according to the theme.
437                     if (MainWebViewActivity.darkTheme) {
438                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_dark));
439                     } else {
440                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
441                     }
442                 }
443             }
444         });
445
446         // Set the `domStorageEnabledSwitch` `OnCheckedChangeListener()`.
447         domStorageEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
448             @Override
449             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
450                 // Update the icon.
451                 if (isChecked) {
452                     domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_enabled));
453                 } else {
454                     // Set the icon according to the theme.
455                     if (MainWebViewActivity.darkTheme) {
456                         domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_dark));
457                     } else {
458                         domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_light));
459                     }
460                 }
461             }
462         });
463
464         // Set the `formDataEnabledSwitch` `OnCheckedChangeListener()`.
465         formDataEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
466             @Override
467             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
468                 // Update the icon.
469                 if (isChecked) {
470                     formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_enabled));
471                 } else {
472                     // Set the icon according to the theme.
473                     if (MainWebViewActivity.darkTheme) {
474                         formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_disabled_dark));
475                     } else {
476                         formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_disabled_light));
477                     }
478                 }
479             }
480         });
481
482         // Set the `userAgentSpinner` `onItemClickListener()`.
483         userAgentSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
484             @Override
485             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
486                 // Store the new user agent string.
487                 String newUserAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[position];
488
489                 // Set the new user agent.
490                 switch (newUserAgentString) {
491                     case "Custom user agent":
492                         // Hide `userAgentTextView`.
493                         userAgentTextView.setVisibility(View.GONE);
494
495                         // Show `customUserAgentEditText` and set `userAgentString` as the text.
496                         customUserAgentEditText.setVisibility(View.VISIBLE);
497                         customUserAgentEditText.setText(currentUserAgentString);
498                         break;
499
500                     case "WebView default user agent":
501                         // Show `userAgentTextView` and set the text.
502                         userAgentTextView.setVisibility(View.VISIBLE);
503                         userAgentTextView.setText(webViewDefaultUserAgentString);
504
505                         // Hide `customUserAgentEditText`.
506                         customUserAgentEditText.setVisibility(View.GONE);
507                         break;
508
509                     default:
510                         // Show `userAgentTextView` and set the text.
511                         userAgentTextView.setVisibility(View.VISIBLE);
512                         userAgentTextView.setText(getResources().getStringArray(R.array.user_agent_entry_values)[position]);
513
514                         // Hide `customUserAgentEditText`.
515                         customUserAgentEditText.setVisibility(View.GONE);
516                         break;
517                 }
518             }
519
520             @Override
521             public void onNothingSelected(AdapterView<?> parent) {
522                 // Do nothing.
523             }
524         });
525
526         // Set the `displayImagesSwitch` `onItemClickListener()`.
527         displayWebpageImagesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
528             @Override
529             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
530                 // Update the icon.
531                 switch (position) {
532                     case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_SYSTEM_DEFAULT:
533                         if (MainWebViewActivity.displayWebpageImagesBoolean) {
534                             // Set the icon according to the theme.
535                             if (MainWebViewActivity.darkTheme) {
536                                 displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_dark));
537                             } else {
538                                 displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
539                             }
540                         } else {
541                             // Set the icon according to the theme.
542                             if (MainWebViewActivity.darkTheme) {
543                                 displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_dark));
544                             } else {
545                                 displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_light));
546                             }
547                         }
548                         break;
549
550                     case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_ENABLED:
551                         // Set the icon according to the theme.
552                         if (MainWebViewActivity.darkTheme) {
553                             displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_dark));
554                         } else {
555                             displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
556                         }
557                         break;
558
559                     case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_DISABLED:
560                         // Set the icon according to the theme.
561                         if (MainWebViewActivity.darkTheme) {
562                             displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_dark));
563                         } else {
564                             displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_light));
565                         }
566                         break;
567                 }
568             }
569
570             @Override
571             public void onNothingSelected(AdapterView<?> parent) {
572                 // Do nothing.
573             }
574         });
575
576         return domainSettingsView;
577     }
578 }