2a2b92dbb1d1de13064ccdfefc7c756bffa88609
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / activities / DomainsActivity.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.activities;
21
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.support.design.widget.FloatingActionButton;
27 import android.support.design.widget.Snackbar;
28 import android.support.v4.app.FragmentManager;
29 import android.support.v4.app.NavUtils;
30 import android.support.v7.app.ActionBar;
31 import android.support.v7.app.AppCompatActivity;
32 import android.support.v7.app.AppCompatDialogFragment;
33 import android.support.v7.widget.Toolbar;
34 import android.view.Menu;
35 import android.view.MenuItem;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.CursorAdapter;
39 import android.widget.EditText;
40 import android.widget.ListView;
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.dialogs.AddDomainDialog;
47 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
48 import com.stoutner.privacybrowser.fragments.DomainsListFragment;
49 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
50
51 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
52     // `twoPanedMode` is public static so it can be accessed from `DomainsListFragment`.  It is also used in `onCreate()`, `onCreateOptionsMenu()`, and `populateDomainsListView()`.
53     public static boolean twoPanedMode;
54
55     // `databaseId` is public static so it can be accessed from `DomainsListFragment`.  It is also used in `onCreateOptionsMenu()`, `saveDomainSettings()` and `populateDomainsListView()`.
56     public static int currentDomainDatabaseId;
57
58     // `deleteMenuItem` is public static so it can be accessed from `DomainsListFragment`.  It is also used in `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`.
59     public static MenuItem deleteMenuItem;
60
61     // `undoDeleteSnackbar` is public static so it can be accessed from `DomainsListFragment`.  It is also used in `onOptionsItemSelected()`.
62     public static Snackbar undoDeleteSnackbar;
63
64     // `dismissingSnackbar` is public static so it can be accessed from `DomainsListFragment`.  It is also used in `onOptionsItemSelected()`.
65     public static boolean dismissingSnackbar;
66
67     // `context` is used in `onCreate()`, `onOptionsItemSelected()`, and `onAddDomain()`.
68     private Context context;
69
70     // `supportFragmentManager` is used in `onCreate()` and `onCreateOptionsMenu()`.
71     private FragmentManager supportFragmentManager;
72
73     // `domainsDatabaseHelper` is used in `onCreate()` and `saveDomainSettings()`.
74     private static DomainsDatabaseHelper domainsDatabaseHelper;
75
76     // `domainsListView` is used in `onCreate()` and `populateDomainsList()`.
77     private ListView domainsListView;
78
79     // `addDomainFAB` is used in `onCreate()`, `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`.
80     private FloatingActionButton addDomainFAB;
81
82     // `deletedDomainPosition` is used in an inner and outer class in `onOptionsItemSelected()`.
83     private int deletedDomainPosition;
84
85     // `restartAfterRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
86     private boolean restartAfterRotate;
87
88     // `domainSettingsDisplayedBeforeRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
89     private boolean domainSettingsDisplayedBeforeRotate;
90
91     // `domainSettingsDatabaseIdBeforeRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
92     private int domainSettingsDatabaseIdBeforeRotate;
93
94     @Override
95     protected void onCreate(Bundle savedInstanceState) {
96         // Set the activity theme.
97         if (MainWebViewActivity.darkTheme) {
98             setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
99         } else {
100             setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
101         }
102
103         // Run the default commands.
104         super.onCreate(savedInstanceState);
105
106         // Extract the values from `savedInstanceState` if it is not `null`.
107         if (savedInstanceState != null) {
108             restartAfterRotate = true;
109             domainSettingsDisplayedBeforeRotate = savedInstanceState.getBoolean("domainSettingsDisplayed");
110             domainSettingsDatabaseIdBeforeRotate = savedInstanceState.getInt("domainSettingsDatabaseId");
111         }
112
113         // Set the content view.
114         setContentView(R.layout.domains_coordinatorlayout);
115
116         // Get a handle for the context.
117         context = this;
118
119         // Get a handle for the fragment manager.
120         supportFragmentManager = getSupportFragmentManager();
121
122         // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
123         final Toolbar domainsAppBar = (Toolbar) findViewById(R.id.domains_toolbar);
124         setSupportActionBar(domainsAppBar);
125
126         // Display the home arrow on `SupportActionBar`.
127         ActionBar appBar = getSupportActionBar();
128         assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null.
129         appBar.setDisplayHomeAsUpEnabled(true);
130
131         // 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`.
132         domainsDatabaseHelper = new DomainsDatabaseHelper(context, null, null, 0);
133
134         // Determine if we are in two pane mode.  `domain_settings_fragment_container` does not exist on devices with a width less than 900dp.
135         twoPanedMode = (findViewById(R.id.domain_settings_fragment_container) != null);
136
137         // Configure `addDomainFAB`.
138         addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
139         addDomainFAB.setOnClickListener(new View.OnClickListener() {
140             @Override
141             public void onClick(View view) {
142                 // Show the `AddDomainDialog` `AlertDialog` and name the instance `@string/add_domain`.
143                 AppCompatDialogFragment addDomainDialog = new AddDomainDialog();
144                 addDomainDialog.show(supportFragmentManager, getResources().getString(R.string.add_domain));
145             }
146         });
147     }
148
149     @Override
150     public boolean onCreateOptionsMenu(Menu menu) {
151         // Inflate the menu.
152         getMenuInflater().inflate(R.menu.domains_options_menu, menu);
153
154         // Store `deleteMenuItem` for future use.
155         deleteMenuItem = menu.findItem(R.id.delete_domain);
156
157         // Only display `deleteMenuItem` (initially) in two-paned mode.
158         deleteMenuItem.setVisible(twoPanedMode);
159
160         // Display the fragments.  This must be done from `onCreateOptionsMenu()` instead of `onCreate()` because `populateDomainsListView()` needs `deleteMenuItem` to be inflated.
161         if (restartAfterRotate && !twoPanedMode && domainSettingsDisplayedBeforeRotate) {  // The device was rotated, the new configuration is in single-paned mode, and domain settings were displayed previously.
162             // Reset `restartAfterRotate`.
163             restartAfterRotate = false;
164
165             // Store `currentDomainDatabaseId`.
166             currentDomainDatabaseId = domainSettingsDatabaseIdBeforeRotate;
167
168             // Add `currentDomainDatabaseId` to `argumentsBundle`.
169             Bundle argumentsBundle = new Bundle();
170             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
171
172             // Add `argumentsBundle` to `domainSettingsFragment`.
173             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
174             domainSettingsFragment.setArguments(argumentsBundle);
175
176             // Show `deleteMenuItem`.
177             deleteMenuItem.setVisible(true);
178
179             // Hide `add_domain_fab`.
180             addDomainFAB.setVisibility(View.GONE);
181
182             // Display `domainSettingsFragment`.
183             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
184         } else if (restartAfterRotate && twoPanedMode && domainSettingsDisplayedBeforeRotate) {  // The device was rotated, the new configuration is in two-paned mode, and domain settings were displayed previously.
185             // Reset `restartAfterRotate`.
186             restartAfterRotate = false;
187
188             // Display `DomainsListFragment`.
189             DomainsListFragment domainsListFragment = new DomainsListFragment();
190             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
191             supportFragmentManager.executePendingTransactions();
192
193             // Populate the list of domains.  `domainSettingsDatabaseId` highlights the domain that was highlighted before the rotation.
194             populateDomainsListView(domainSettingsDatabaseIdBeforeRotate);
195         } else {  // The device was not rotated or, if it was, domain settings were not displayed previously.
196             // Display `DomainsListFragment`.
197             DomainsListFragment domainsListFragment = new DomainsListFragment();
198             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
199             supportFragmentManager.executePendingTransactions();
200
201             // Populate the list of domains.  `-1` highlights the first domain.
202             populateDomainsListView(-1);
203         }
204
205         // Success!
206         return true;
207     }
208
209     @Override
210     public boolean onOptionsItemSelected(MenuItem menuItem) {
211         // Get the ID of the `MenuItem` that was selected.
212         int menuItemID = menuItem.getItemId();
213
214         switch (menuItemID) {
215             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
216                 if (twoPanedMode) {  // The device is in two-paned mode.
217                     // Save the current domain settings if the domain settings fragment is displayed.
218                     if (findViewById(R.id.domain_settings_scrollview) != null) {
219                         saveDomainSettings();
220                     }
221
222                     // Go home.
223                     NavUtils.navigateUpFromSameTask(this);
224                 } else if (findViewById(R.id.domain_settings_scrollview) != null) {  // The device is in single-paned mode and `DomainSettingsFragment` is displayed.
225                     // Save the current domain settings.
226                     saveDomainSettings();
227
228                     // Display `DomainsListFragment`.
229                     DomainsListFragment domainsListFragment = new DomainsListFragment();
230                     supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
231                     supportFragmentManager.executePendingTransactions();
232
233                     // Populate the list of domains.  `-1` highlights the first domain if in two-paned mode.  It has no effect in single-paned mode.
234                     populateDomainsListView(-1);
235
236                     // Display `addDomainFAB`.
237                     addDomainFAB.setVisibility(View.VISIBLE);
238
239                     // Hide `deleteMenuItem`.
240                     deleteMenuItem.setVisible(false);
241                 } else {  // The device is in single-paned mode and `DomainsListFragment` is displayed.
242                     // Go home.
243                     NavUtils.navigateUpFromSameTask(this);
244                 }
245                 break;
246
247             case R.id.delete_domain:
248                 // Store a copy of `currentDomainDatabaseId` because it could change while the `Snackbar` is displayed.
249                 final int databaseIdToDelete = currentDomainDatabaseId;
250
251                 // Update the fragments and menu items.
252                 if (twoPanedMode) {  // Two-paned mode.
253                     // Store the deleted domain position, which is needed if `Undo` is selected in the `Snackbar`.
254                     deletedDomainPosition = domainsListView.getCheckedItemPosition();
255
256                     // Disable the options `MenuItems`.
257                     deleteMenuItem.setEnabled(false);
258                     deleteMenuItem.setIcon(R.drawable.delete_blue);
259
260                     // Remove the domain settings fragment.
261                     supportFragmentManager.beginTransaction().remove(supportFragmentManager.findFragmentById(R.id.domain_settings_fragment_container)).commit();
262                 } else {  // Single-paned mode.
263                     // Display `DomainsListFragment`.
264                     DomainsListFragment domainsListFragment = new DomainsListFragment();
265                     supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
266                     supportFragmentManager.executePendingTransactions();
267
268                     // Display `addDomainFAB`.
269                     addDomainFAB.setVisibility(View.VISIBLE);
270
271                     // Hide `deleteMenuItem`.
272                     deleteMenuItem.setVisible(false);
273                 }
274
275                 // Get a `Cursor` that does not show the domain to be deleted.
276                 Cursor domainsPendingDeleteCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomainExcept(databaseIdToDelete);
277
278                 // Setup `domainsPendingDeleteCursorAdapter` with `this` context.  `false` disables `autoRequery`.
279                 CursorAdapter domainsPendingDeleteCursorAdapter = new CursorAdapter(this, domainsPendingDeleteCursor, false) {
280                     @Override
281                     public View newView(Context context, Cursor cursor, ViewGroup parent) {
282                         // Inflate the individual item layout.  `false` does not attach it to the root.
283                         return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
284                     }
285
286                     @Override
287                     public void bindView(View view, Context context, Cursor cursor) {
288                         // Set the domain name.
289                         String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
290                         TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
291                         domainNameTextView.setText(domainNameString);
292                     }
293                 };
294
295                 // Update the handle for the current `domains_listview`.
296                 domainsListView = (ListView) findViewById(R.id.domains_listview);
297
298                 // Update the `ListView`.
299                 domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
300
301                 // Display a `Snackbar`.
302                 undoDeleteSnackbar = Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
303                         .setAction(R.string.undo, new View.OnClickListener() {
304                             @Override
305                             public void onClick(View v) {
306                                 // Do nothing because everything will be handled by `onDismissed()` below.
307                             }
308                         })
309                         .addCallback(new Snackbar.Callback() {
310                             @Override
311                             public void onDismissed(Snackbar snackbar, int event) {
312                                 switch (event) {
313                                     // The user pushed the `Undo` button.
314                                     case Snackbar.Callback.DISMISS_EVENT_ACTION:
315                                         // Store `databaseId` in `argumentsBundle`.
316                                         Bundle argumentsBundle = new Bundle();
317                                         argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseIdToDelete);
318
319                                         // Add `argumentsBundle` to `domainSettingsFragment`.
320                                         DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
321                                         domainSettingsFragment.setArguments(argumentsBundle);
322
323                                         // Display the correct fragments.
324                                         if (twoPanedMode) {  // The device in in two-paned mode.
325                                             // Get a `Cursor` with the current contents of the domains database.
326                                             Cursor undoDeleteDomainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
327
328                                             // Setup `domainsCursorAdapter` with `this` context.  `false` disables `autoRequery`.
329                                             CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(context, undoDeleteDomainsCursor, false) {
330                                                 @Override
331                                                 public View newView(Context context, Cursor cursor, ViewGroup parent) {
332                                                     // Inflate the individual item layout.  `false` does not attach it to the root.
333                                                     return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
334                                                 }
335
336                                                 @Override
337                                                 public void bindView(View view, Context context, Cursor cursor) {
338                                                     // Set the domain name.
339                                                     String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
340                                                     TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
341                                                     domainNameTextView.setText(domainNameString);
342                                                 }
343                                             };
344
345                                             // Update the `ListView`.
346                                             domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
347                                             // Select the previously deleted domain in `domainsListView`.
348                                             domainsListView.setItemChecked(deletedDomainPosition, true);
349
350                                             // Display `domainSettingsFragment`.
351                                             supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
352
353                                             // Enable the options `MenuItems`.
354                                             deleteMenuItem.setEnabled(true);
355                                             deleteMenuItem.setIcon(R.drawable.delete_light);
356                                         } else {  // The device in in one-paned mode.
357                                             // Display `domainSettingsFragment`.
358                                             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
359
360                                             // Hide `add_domain_fab`.
361                                             FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
362                                             addDomainFAB.setVisibility(View.GONE);
363
364                                             // Show and enable `deleteMenuItem`.
365                                             deleteMenuItem.setVisible(true);
366
367                                             // Display `domainSettingsFragment`.
368                                             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
369                                         }
370                                         break;
371
372                                     // The `Snackbar` was dismissed without the `Undo` button being pushed.
373                                     default:
374                                         // Delete the selected domain.
375                                         domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
376
377                                         // enable `deleteMenuItem` if the system was waiting for a `Snackbar` to be dismissed.
378                                         if (DomainsActivity.dismissingSnackbar) {
379                                             // Create a `Runnable` to enable the delete menu item.
380                                             Runnable enableDeleteMenuItemRunnable = new Runnable() {
381                                                 @Override
382                                                 public void run() {
383                                                     // Enable `deleteMenuItem` according to the display mode.
384                                                     if (twoPanedMode) {  // Two-paned mode.
385                                                         // Enable `deleteMenuItem`.
386                                                         deleteMenuItem.setEnabled(true);
387
388                                                         // Set the delete icon according to the theme.
389                                                         if (MainWebViewActivity.darkTheme) {
390                                                             deleteMenuItem.setIcon(R.drawable.delete_dark);
391                                                         } else {
392                                                             deleteMenuItem.setIcon(R.drawable.delete_light);
393                                                         }
394                                                     } else {  // Single-paned mode.
395                                                         // Show `deleteMenuItem`.
396                                                         deleteMenuItem.setVisible(true);
397                                                     }
398
399                                                     // Reset `dismissingSnackbar`.
400                                                     dismissingSnackbar = false;
401                                                 }
402                                             };
403
404                                             // Run `enableDeleteMenuItemRunnable` after 100 milliseconds to make sure that the previous domain has been deleted from the database.
405                                             Handler handler = new Handler();
406                                             handler.postDelayed(enableDeleteMenuItemRunnable, 100);
407                                         }
408                                         break;
409                                 }
410                             }
411                         });
412                 undoDeleteSnackbar.show();
413                 break;
414         }
415
416         // Consume the event.
417         return true;
418     }
419
420     @Override
421     protected void onSaveInstanceState(Bundle outState) {
422         // Store the current `DomainSettingsFragment` state in `outState`.
423         if (findViewById(R.id.domain_settings_scrollview) != null) {  // `DomainSettingsFragment` is displayed.
424             // Store `DomainSettingsDisplayed`.
425             outState.putBoolean("domainSettingsDisplayed", true);
426             outState.putInt("domainSettingsDatabaseId", DomainSettingsFragment.databaseId);
427         } else {  // `DomainSettingsFragment` is not displayed.
428             outState.putBoolean("domainSettingsDisplayed", false);
429             outState.putInt("domainSettingsDatabaseId", -1);
430         }
431
432         super.onSaveInstanceState(outState);
433     }
434
435     // Control what the navigation bar back button does.
436     @Override
437     public void onBackPressed() {
438         if (twoPanedMode) {  // The device is in two-paned mode.
439             // Save the current domain settings if the domain settings fragment is displayed.
440             if (findViewById(R.id.domain_settings_scrollview) != null) {
441                 saveDomainSettings();
442             }
443
444             // Go home.
445             NavUtils.navigateUpFromSameTask(this);
446         } else if (findViewById(R.id.domain_settings_scrollview) != null) {  // The device is in single-paned mode and `DomainSettingsFragment` is displayed.
447             // Save the current domain settings.
448             saveDomainSettings();
449
450             // Display `DomainsListFragment`.
451             DomainsListFragment domainsListFragment = new DomainsListFragment();
452             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
453             supportFragmentManager.executePendingTransactions();
454
455             // Populate the list of domains.  `-1` highlights the first domain if in two-paned mode.  It has no effect in single-paned mode.
456             populateDomainsListView(-1);
457
458             // Display `addDomainFAB`.
459             addDomainFAB.setVisibility(View.VISIBLE);
460
461             // Hide `deleteMenuItem`.
462             deleteMenuItem.setVisible(false);
463         } else {  // The device is in single-paned mode and `DomainsListFragment` is displayed.
464             // Pass `onBackPressed()` to the system.
465             super.onBackPressed();
466         }
467     }
468
469     @Override
470     public void onAddDomain(AppCompatDialogFragment dialogFragment) {
471         // Dismiss `undoDeleteSnackbar` if it is currently displayed.
472         if ((undoDeleteSnackbar != null) && (undoDeleteSnackbar.isShown())) {
473             undoDeleteSnackbar.dismiss();
474         }
475
476         // Get the `domainNameEditText` from `dialogFragment` and extract the string.
477         EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
478         String domainNameString = domainNameEditText.getText().toString();
479
480         // Create the domain and store the database ID in `currentDomainDatabaseId`.
481         currentDomainDatabaseId = domainsDatabaseHelper.addDomain(domainNameString);
482
483         // Display the newly created domain.
484         if (twoPanedMode) {  // The device in in two-paned mode.
485             populateDomainsListView(currentDomainDatabaseId);
486         } else {  // The device is in single-paned mode.
487             // Hide `add_domain_fab`.
488             addDomainFAB.setVisibility(View.GONE);
489
490             // Show and enable `deleteMenuItem`.
491             DomainsActivity.deleteMenuItem.setVisible(true);
492
493             // Add `currentDomainDatabaseId` to `argumentsBundle`.
494             Bundle argumentsBundle = new Bundle();
495             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
496
497             // Add `argumentsBundle` to `domainSettingsFragment`.
498             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
499             domainSettingsFragment.setArguments(argumentsBundle);
500
501             // Display `domainSettingsFragment`.
502             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
503         }
504     }
505
506     private void saveDomainSettings() {
507         // Get handles for the domain settings.
508         EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
509         Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
510         Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
511         Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
512         Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
513         Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
514         Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
515         EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
516         Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
517         Spinner displayWebpageImagesSpinner = (Spinner) findViewById(R.id.domain_settings_display_webpage_images_spinner);
518
519         // Extract the data for the domain settings.
520         String domainNameString = domainNameEditText.getText().toString();
521         boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked();
522         boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked();
523         boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked();
524         boolean domStorageEnabledEnabledBoolean  = domStorageEnabledSwitch.isChecked();
525         boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked();
526         int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
527         int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
528         int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
529
530         // Get the data for the `Spinners` from the entry values string arrays.
531         String userAgentString = getResources().getStringArray(R.array.domain_settings_user_agent_entry_values)[userAgentPositionInt];
532         int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePositionInt]);
533
534         // Check to see if we are using a custom user agent.
535         if (userAgentString.equals("Custom user agent")) {
536             // Set `userAgentString` to the custom user agent string.
537             userAgentString = customUserAgentEditText.getText().toString();
538         }
539
540         // Save the domain settings.
541         domainsDatabaseHelper.saveDomain(currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean, userAgentString, fontSizeInt,
542                 displayWebpageImagesInt);
543     }
544
545     private void populateDomainsListView(final int highlightedDomainDatabaseId) {
546         // get a handle for the current `domains_listview`.
547         domainsListView = (ListView) findViewById(R.id.domains_listview);
548
549         // Get a `Cursor` with the current contents of the domains database.
550         Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
551
552         // Setup `domainsCursorAdapter` with `this` context.  `false` disables `autoRequery`.
553         CursorAdapter domainsCursorAdapter = new CursorAdapter(context, domainsCursor, false) {
554             @Override
555             public View newView(Context context, Cursor cursor, ViewGroup parent) {
556                 // Inflate the individual item layout.  `false` does not attach it to the root.
557                 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
558             }
559
560             @Override
561             public void bindView(View view, Context context, Cursor cursor) {
562                 // Set the domain name.
563                 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
564                 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
565                 domainNameTextView.setText(domainNameString);
566             }
567         };
568
569         // Update the `ListView`.
570         domainsListView.setAdapter(domainsCursorAdapter);
571
572         // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
573         if (DomainsActivity.twoPanedMode && (domainsCursor.getCount() > 0)) {  // Two-paned mode is enabled and there is at least one domain.
574             // Initialize `highlightedDomainPosition`.
575             int highlightedDomainPosition = 0;
576
577             // Get the cursor position for the highlighted domain.
578             for (int i = 0; i < domainsCursor.getCount(); i++) {
579                 // Move to position `i` in the cursor.
580                 domainsCursor.moveToPosition(i);
581
582                 // Get the database ID for this position.
583                 int currentDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
584
585                 // Set `highlightedDomainPosition` if the database ID for this matches `highlightedDomainDatabaseId`.
586                 if (highlightedDomainDatabaseId == currentDatabaseId) {
587                     highlightedDomainPosition = i;
588                 }
589             }
590
591             // Select the highlighted domain.
592             domainsListView.setItemChecked(highlightedDomainPosition, true);
593
594             // Get the `databaseId` for the highlighted domain.
595             domainsCursor.moveToPosition(highlightedDomainPosition);
596             currentDomainDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
597
598             // Store `databaseId` in `argumentsBundle`.
599             Bundle argumentsBundle = new Bundle();
600             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
601
602             // Add `argumentsBundle` to `domainSettingsFragment`.
603             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
604             domainSettingsFragment.setArguments(argumentsBundle);
605
606             // Display `domainSettingsFragment`.
607             supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
608
609             // Enable the options `MenuItems`.
610             deleteMenuItem.setEnabled(true);
611
612             // Set the delete icon according to the theme.
613             if (MainWebViewActivity.darkTheme) {
614                 deleteMenuItem.setIcon(R.drawable.delete_dark);
615             } else {
616                 deleteMenuItem.setIcon(R.drawable.delete_light);
617             }
618         } else if (twoPanedMode) {  // Two-paned mode is enabled but there are no domains.
619             // Disable the options `MenuItems`.
620             deleteMenuItem.setEnabled(false);
621             deleteMenuItem.setIcon(R.drawable.delete_blue);
622         }
623     }
624 }