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