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