Wait to enable the delete icon until the previous domain has been deleted. Fixes...
[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.content.SharedPreferences;
25 import android.database.Cursor;
26 import android.os.Build;
27 import android.os.Bundle;
28 // We have to use `android.support.v4.app.Fragment` until minimum API >= 23.  Otherwise we cannot call `getContext()`.
29 import android.preference.PreferenceManager;
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_fragment`.  `false` does not attach it to the root `container`.
69         View domainSettingsView = inflater.inflate(R.layout.domain_settings_fragment, 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.  The two `nulls` do not specify the database name or a `CursorFactory`.  The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
95         DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(context, null, null, 0);
96
97         // Get the database `Cursor` for this ID and move it to the first row.
98         Cursor domainCursor = domainsDatabaseHelper.getCursorForId(databaseId);
99         domainCursor.moveToFirst();
100
101         // Save the `Cursor` entries as variables.
102         String domainNameString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
103         int javaScriptEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT));
104         int firstPartyCookiesEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES));
105         int thirdPartyCookiesEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES));
106         int domStorageEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE));
107         int formDataEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA));
108         final String currentUserAgentString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT));
109         int fontSizeInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE));
110         int displayImagesInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.DISPLAY_IMAGES));
111
112         // Create `ArrayAdapters` for the `Spinners`and their `entry values`.
113         ArrayAdapter<CharSequence> userAgentArrayAdapter = ArrayAdapter.createFromResource(context, R.array.domain_settings_user_agent_entries, R.layout.spinner_item);
114         final ArrayAdapter<CharSequence> userAgentEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.domain_settings_user_agent_entry_values, R.layout.spinner_item);
115         ArrayAdapter<CharSequence> fontSizeArrayAdapter = ArrayAdapter.createFromResource(context, R.array.domain_settings_font_size_entries, R.layout.spinner_item);
116         ArrayAdapter<CharSequence> fontSizeEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.domain_settings_font_size_entry_values, R.layout.spinner_item);
117         final ArrayAdapter<CharSequence> displayImagesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.display_website_images_array, R.layout.spinner_item);
118
119         // Set the `DropDownViewResource` on the `Spinners`.
120         userAgentArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
121         fontSizeArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
122         displayImagesArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
123
124         // Set the `ArrayAdapters` for the `Spinners`.
125         userAgentSpinner.setAdapter(userAgentArrayAdapter);
126         fontSizeSpinner.setAdapter(fontSizeArrayAdapter);
127         displayWebpageImagesSpinner.setAdapter(displayImagesArrayAdapter);
128
129         // Set the domain name from the the database cursor.
130         domainNameEditText.setText(domainNameString);
131
132         // Set the JavaScript status.
133         if (javaScriptEnabledInt == 1) {  // JavaScript is enabled.
134             javaScriptEnabledSwitch.setChecked(true);
135             javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.javascript_enabled));
136         } else {  // JavaScript is disabled.
137             javaScriptEnabledSwitch.setChecked(false);
138             javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.privacy_mode));
139         }
140
141         // Set the first-party cookies status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
142         if (firstPartyCookiesEnabledInt == 1) {  // First-party cookies are enabled.
143             firstPartyCookiesEnabledSwitch.setChecked(true);
144             firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_enabled));
145         } else {  // First-party cookies are disabled.
146             firstPartyCookiesEnabledSwitch.setChecked(false);
147
148             // Set the icon according to the theme.
149             if (MainWebViewActivity.darkTheme) {
150                 firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_dark));
151             } else {
152                 firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
153             }
154         }
155
156         // Only display third-party cookies if SDK_INT >= 21.
157         if (Build.VERSION.SDK_INT >= 21) {  // Third-party cookies can be configured for API >= 21.
158             // Only enable third-party-cookies if first-party cookies are enabled.
159             if (firstPartyCookiesEnabledInt == 1) {  // First-party cookies are enabled.
160                 // Set the third-party cookies status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
161                 if (thirdPartyCookiesEnabledInt == 1) {  // Both first-party and third-party cookies are enabled.
162                     thirdPartyCookiesEnabledSwitch.setChecked(true);
163                     thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_warning));
164                 } else {  // First party cookies are enabled but third-party cookies are disabled.
165                     thirdPartyCookiesEnabledSwitch.setChecked(false);
166
167                     // Set the icon according to the theme.
168                     if (MainWebViewActivity.darkTheme) {
169                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_dark));
170                     } else {
171                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
172                     }
173                 }
174             } else {  // First-party cookies are disabled.
175                 // Set the status of third-party cookies.
176                 if (thirdPartyCookiesEnabledInt == 1) {
177                     thirdPartyCookiesEnabledSwitch.setChecked(true);
178                 } else {
179                     thirdPartyCookiesEnabledSwitch.setChecked(false);
180                 }
181
182                 // Disable the third-party cookies switch.
183                 thirdPartyCookiesEnabledSwitch.setEnabled(false);
184
185                 // Set the icon according to the theme.
186                 if (MainWebViewActivity.darkTheme) {
187                     thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_ghosted_dark));
188                 } else {
189                     thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_ghosted_light));
190                 }
191             }
192         } else {  // Third-party cookies cannot be configured for API <= 21.
193             // Hide the `LinearLayout` for third-party cookies.
194             thirdPartyCookiesLinearLayout.setVisibility(View.GONE);
195         }
196
197         // Only enable DOM storage if JavaScript is enabled.
198         if (javaScriptEnabledInt == 1) {  // JavaScript is enabled.
199             // Set the DOM storage status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
200             if (domStorageEnabledInt == 1) {  // Both JavaScript and DOM storage are enabled.
201                 domStorageEnabledSwitch.setChecked(true);
202                 domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_enabled));
203             } else {  // JavaScript is enabled but DOM storage is disabled.
204                 // Set the DOM storage switch to off.
205                 domStorageEnabledSwitch.setChecked(false);
206
207                 // Set the icon according to the theme.
208                 if (MainWebViewActivity.darkTheme) {
209                     domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_dark));
210                 } else {
211                     domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_light));
212                 }
213             }
214         } else {  // JavaScript is disabled.
215             // Set the checked status of DOM storage.
216             if (domStorageEnabledInt == 1) {  // DOM storage is enabled but JavaScript is disabled.
217                 domStorageEnabledSwitch.setChecked(true);
218             } else {  // Both JavaScript and DOM storage are disabled.
219                 domStorageEnabledSwitch.setChecked(false);
220             }
221
222             // Disable `domStorageEnabledSwitch`.
223             domStorageEnabledSwitch.setEnabled(false);
224
225             // Set the icon according to the theme.
226             if (MainWebViewActivity.darkTheme) {
227                 domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_ghosted_dark));
228             } else {
229                 domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_ghosted_light));
230             }
231         }
232
233         // Set the form data status.  Once minimum API >= 21 we can use a selector as the tint mode instead of specifying different icons.
234         if (formDataEnabledInt == 1) {  // Form data is enabled.
235             formDataEnabledSwitch.setChecked(true);
236             formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_enabled));
237         } else {  // Form data is disabled.
238             // Set the form data switch to off.
239             formDataEnabledSwitch.setChecked(false);
240
241             // Set the icon according to the theme.
242             if (MainWebViewActivity.darkTheme) {
243                 formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_disabled_dark));
244             } else {
245                 formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_disabled_light));
246             }
247         }
248
249         // We need to inflated a `WebView` to get the default user agent.
250         // `@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.
251         @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
252         WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
253         final String webViewDefaultUserAgentString = bareWebView.getSettings().getUserAgentString();
254
255         // Get a handle for the shared preference.
256         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
257
258         // Store the default user agent string values.
259         final String defaultUserAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
260         final String defaultCustomUserAgentString = sharedPreferences.getString("custom_user_agent", "PrivacyBrowser/1.0");
261
262         // Get the position of the user agent in `userAgentEntryValuesArrayAdapter`.
263         int userAgentArrayPosition = userAgentEntryValuesArrayAdapter.getPosition(currentUserAgentString);
264
265         // Set the user agent.
266         if (userAgentArrayPosition == -1) {  // We are using a custom `userAgentString`.
267             // Set `userAgentSpinner` to `Custom`.
268             userAgentSpinner.setSelection(userAgentEntryValuesArrayAdapter.getPosition("Custom user agent"));
269
270             // Hide `userAgentTextView`.
271             userAgentTextView.setVisibility(View.GONE);
272
273             // Show `customUserAgentEditText` and set `userAgentString` as the text.
274             customUserAgentEditText.setVisibility(View.VISIBLE);
275             customUserAgentEditText.setText(currentUserAgentString);
276         } else{  // We are using one of the preset user agents.
277             // Set the `userAgentSpinner` selection.
278             userAgentSpinner.setSelection(userAgentArrayPosition);
279
280             // Show `userAgentTextView`.
281             userAgentTextView.setVisibility(View.VISIBLE);
282
283             // Hide `customUserAgentEditText`.
284             customUserAgentEditText.setVisibility(View.GONE);
285
286             // Set the user agent text.
287             switch (currentUserAgentString) {
288                 case "System default user agent":
289                     // Display the user agent text string.
290                     switch (defaultUserAgentString) {
291                         case "WebView default user agent":
292                             // Display the `WebView` default user agent.
293                             userAgentTextView.setText(webViewDefaultUserAgentString);
294                             break;
295
296                         case "Custom user agent":
297                             // Display the custom user agent.
298                             userAgentTextView.setText(defaultCustomUserAgentString);
299                             break;
300
301                         default:
302                             // Display the text from `defaultUserAgentString`.
303                             userAgentTextView.setText(defaultUserAgentString);
304                     }
305                     break;
306
307                 case "WebView default user agent":
308                     // Display the `WebView` default user agent.
309                     userAgentTextView.setText(webViewDefaultUserAgentString);
310                     break;
311
312                 default:
313                     // Display the text from `currentUserAgentString`.
314                     userAgentTextView.setText(currentUserAgentString);
315             }
316         }
317
318         // Set the selected font size.
319         int fontSizeArrayPosition = fontSizeEntryValuesArrayAdapter.getPosition(String.valueOf(fontSizeInt));
320         fontSizeSpinner.setSelection(fontSizeArrayPosition);
321
322         // Set the selected display website images mode.
323         displayWebpageImagesSpinner.setSelection(displayImagesInt);
324
325         // Set the display website images icon.
326         switch (displayImagesInt) {
327             case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_SYSTEM_DEFAULT:
328                 if (MainWebViewActivity.displayWebpageImagesBoolean) {
329                     // Set the icon according to the theme.
330                     if (MainWebViewActivity.darkTheme) {
331                         displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_dark));
332                     } else {
333                         displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
334                     }
335                 } else {
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                 }
343                 break;
344
345             case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_ENABLED:
346                 // Set the icon according to the theme.
347                 if (MainWebViewActivity.darkTheme) {
348                     displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_dark));
349                 } else {
350                     displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
351                 }
352                 break;
353
354             case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_DISABLED:
355                 // Set the icon according to the theme.
356                 if (MainWebViewActivity.darkTheme) {
357                     displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_dark));
358                 } else {
359                     displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_light));
360                 }
361                 break;
362         }
363
364
365         // Set the `javaScriptEnabledSwitch` `OnCheckedChangeListener()`.
366         javaScriptEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
367             @Override
368             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
369                 if (isChecked) {  // JavaScript is enabled.
370                     // Update the JavaScript icon.
371                     javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.javascript_enabled));
372
373                     // Enable the DOM storage `Switch`.
374                     domStorageEnabledSwitch.setEnabled(true);
375
376                     // Update the DOM storage icon.
377                     if (domStorageEnabledSwitch.isChecked()) {  // DOM storage is enabled.
378                         domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_enabled));
379                     } else {  // DOM storage is disabled.
380                         // Set the icon according to the theme.
381                         if (MainWebViewActivity.darkTheme) {
382                             domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_dark));
383                         } else {
384                             domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_light));
385                         }
386                     }
387                 } else {  // JavaScript is disabled.
388                     // Update the JavaScript icon.
389                     javaScriptImageView.setImageDrawable(getResources().getDrawable(R.drawable.privacy_mode));
390
391                     // Disable the DOM storage `Switch`.
392                     domStorageEnabledSwitch.setEnabled(false);
393
394                     // Set the DOM storage icon according to the theme.
395                     if (MainWebViewActivity.darkTheme) {
396                         domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_ghosted_dark));
397                     } else {
398                         domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_ghosted_light));
399                     }
400                 }
401             }
402         });
403
404         // Set the `firstPartyCookiesEnabledSwitch` `OnCheckedChangeListener()`.
405         firstPartyCookiesEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
406             @Override
407             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
408                 if (isChecked) {  // First-party cookies are enabled.
409                     // Update the first-party cookies icon.
410                     firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_enabled));
411
412                     // Enable the third-party cookies `Switch`.
413                     thirdPartyCookiesEnabledSwitch.setEnabled(true);
414
415                     // Update the third-party cookies icon.
416                     if (thirdPartyCookiesEnabledSwitch.isChecked()) {  // Third-party cookies are enabled.
417                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_warning));
418                     } else {  // Third-party cookies are disabled.
419                         // Set the third-party cookies icon according to the theme.
420                         if (MainWebViewActivity.darkTheme) {
421                             thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_dark));
422                         } else {
423                             thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
424                         }
425                     }
426                 } else {  // First-party cookies are disabled.
427                     // Update the first-party cookies icon according to the theme.
428                     if (MainWebViewActivity.darkTheme) {
429                         firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_dark));
430                     } else {
431                         firstPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
432                     }
433
434                     // Disable the third-party cookies `Switch`.
435                     thirdPartyCookiesEnabledSwitch.setEnabled(false);
436
437                     // Set the third-party cookies icon according to the theme.
438                     if (MainWebViewActivity.darkTheme) {
439                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_ghosted_dark));
440                     } else {
441                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_ghosted_light));
442                     }
443                 }
444             }
445         });
446
447         // Set the `thirdPartyCookiesEnabledSwitch` `OnCheckedChangeListener()`.
448         thirdPartyCookiesEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
449             @Override
450             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
451                 // Update the icon.
452                 if (isChecked) {
453                     thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_warning));
454                 } else {
455                     // Update the third-party cookies icon according to the theme.
456                     if (MainWebViewActivity.darkTheme) {
457                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_dark));
458                     } else {
459                         thirdPartyCookiesImageView.setImageDrawable(getResources().getDrawable(R.drawable.cookies_disabled_light));
460                     }
461                 }
462             }
463         });
464
465         // Set the `domStorageEnabledSwitch` `OnCheckedChangeListener()`.
466         domStorageEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
467             @Override
468             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
469                 // Update the icon.
470                 if (isChecked) {
471                     domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_enabled));
472                 } else {
473                     // Set the icon according to the theme.
474                     if (MainWebViewActivity.darkTheme) {
475                         domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_dark));
476                     } else {
477                         domStorageImageView.setImageDrawable(getResources().getDrawable(R.drawable.dom_storage_disabled_light));
478                     }
479                 }
480             }
481         });
482
483         // Set the `formDataEnabledSwitch` `OnCheckedChangeListener()`.
484         formDataEnabledSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
485             @Override
486             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
487                 // Update the icon.
488                 if (isChecked) {
489                     formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_enabled));
490                 } else {
491                     // Set the icon according to the theme.
492                     if (MainWebViewActivity.darkTheme) {
493                         formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_disabled_dark));
494                     } else {
495                         formDataImageView.setImageDrawable(getResources().getDrawable(R.drawable.form_data_disabled_light));
496                     }
497                 }
498             }
499         });
500
501         // Set the `userAgentSpinner` `onItemClickListener()`.
502         userAgentSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
503             @Override
504             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
505                 // Store the new user agent string.
506                 String newUserAgentString = getResources().getStringArray(R.array.domain_settings_user_agent_entry_values)[position];
507
508                 // Set the new user agent.
509                 switch (newUserAgentString) {
510                     case "System default user agent":
511                         // Show `userAgentTextView`.
512                         userAgentTextView.setVisibility(View.VISIBLE);
513
514                         // Hide `customUserAgentEditText`.
515                         customUserAgentEditText.setVisibility(View.GONE);
516
517                         // Set the user text.
518                         switch (defaultUserAgentString) {
519                             case "WebView default user agent":
520                                 // Display the `WebView` default user agent.
521                                 userAgentTextView.setText(webViewDefaultUserAgentString);
522                                 break;
523
524                             case "Custom user agent":
525                                 // Display the custom user agent.
526                                 userAgentTextView.setText(defaultCustomUserAgentString);
527                                 break;
528
529                             default:
530                                 // Display the text from `defaultUserAgentString`.
531                                 userAgentTextView.setText(defaultUserAgentString);
532                         }
533                         break;
534
535                     case "WebView default user agent":
536                         // Show `userAgentTextView` and set the text.
537                         userAgentTextView.setVisibility(View.VISIBLE);
538                         userAgentTextView.setText(webViewDefaultUserAgentString);
539
540                         // Hide `customUserAgentEditText`.
541                         customUserAgentEditText.setVisibility(View.GONE);
542                         break;
543
544                     case "Custom user agent":
545                         // Hide `userAgentTextView`.
546                         userAgentTextView.setVisibility(View.GONE);
547
548                         // Show `customUserAgentEditText` and set `userAgentString` as the text.
549                         customUserAgentEditText.setVisibility(View.VISIBLE);
550                         customUserAgentEditText.setText(currentUserAgentString);
551                         break;
552
553                     default:
554                         // Show `userAgentTextView` and set the text.
555                         userAgentTextView.setVisibility(View.VISIBLE);
556                         userAgentTextView.setText(newUserAgentString);
557
558                         // Hide `customUserAgentEditText`.
559                         customUserAgentEditText.setVisibility(View.GONE);
560                 }
561             }
562
563             @Override
564             public void onNothingSelected(AdapterView<?> parent) {
565                 // Do nothing.
566             }
567         });
568
569         // Set the `displayImagesSwitch` `onItemClickListener()`.
570         displayWebpageImagesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
571             @Override
572             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
573                 // Update the icon.
574                 switch (position) {
575                     case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_SYSTEM_DEFAULT:
576                         if (MainWebViewActivity.displayWebpageImagesBoolean) {
577                             // Set the icon according to the theme.
578                             if (MainWebViewActivity.darkTheme) {
579                                 displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_dark));
580                             } else {
581                                 displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
582                             }
583                         } else {
584                             // Set the icon according to the theme.
585                             if (MainWebViewActivity.darkTheme) {
586                                 displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_dark));
587                             } else {
588                                 displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_light));
589                             }
590                         }
591                         break;
592
593                     case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_ENABLED:
594                         // Set the icon according to the theme.
595                         if (MainWebViewActivity.darkTheme) {
596                             displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_dark));
597                         } else {
598                             displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_enabled_light));
599                         }
600                         break;
601
602                     case DomainsDatabaseHelper.DISPLAY_WEBPAGE_IMAGES_DISABLED:
603                         // Set the icon according to the theme.
604                         if (MainWebViewActivity.darkTheme) {
605                             displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_dark));
606                         } else {
607                             displayWebpageImagesImageView.setImageDrawable(getResources().getDrawable(R.drawable.images_disabled_light));
608                         }
609                         break;
610                 }
611             }
612
613             @Override
614             public void onNothingSelected(AdapterView<?> parent) {
615                 // Do nothing.
616             }
617         });
618
619         return domainSettingsView;
620     }
621 }