]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java
Implement a night mode option. https://redmine.stoutner.com/issues/145.
[PrivacyBrowserAndroid.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         Spinner nightModeSpinner = (Spinner) findViewById(R.id.domain_settings_night_mode_spinner);
524         Switch pinnedSslCertificateSwitch = (Switch) findViewById(R.id.domain_settings_pinned_ssl_certificate_switch);
525         RadioButton savedSslCertificateRadioButton = (RadioButton) findViewById(R.id.saved_ssl_certificate_radiobutton);
526         RadioButton currentWebsiteCertificateRadioButton = (RadioButton) findViewById(R.id.current_website_certificate_radiobutton);
527
528         // Extract the data for the domain settings.
529         String domainNameString = domainNameEditText.getText().toString();
530         boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked();
531         boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked();
532         boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked();
533         boolean domStorageEnabledEnabledBoolean  = domStorageEnabledSwitch.isChecked();
534         boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked();
535         int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
536         int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
537         int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
538         int nightModeInt = nightModeSpinner.getSelectedItemPosition();
539         boolean pinnedSslCertificate = pinnedSslCertificateSwitch.isChecked();
540
541         // Get the data for the `Spinners` from the entry values string arrays.
542         String userAgentString = getResources().getStringArray(R.array.domain_settings_user_agent_entry_values)[userAgentPositionInt];
543         int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePositionInt]);
544
545         // Check to see if we are using a custom user agent.
546         if (userAgentString.equals("Custom user agent")) {
547             // Set `userAgentString` to the custom user agent string.
548             userAgentString = customUserAgentEditText.getText().toString();
549         }
550
551         // Save the domain settings.
552         if (savedSslCertificateRadioButton.isChecked()) {  // The current certificate is being used.
553             // Update the database except for the certificate.
554             domainsDatabaseHelper.updateDomainExceptCertificate(DomainsActivity.currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean,
555                     formDataEnabledBoolean, userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt, pinnedSslCertificate);
556         } else if (currentWebsiteCertificateRadioButton.isChecked()) {  // The certificate is being updated with the current website certificate.
557             // Get the current website SSL certificate.
558             SslCertificate currentWebsiteSslCertificate = MainWebViewActivity.sslCertificate;
559
560             // Store the values from the SSL certificate.
561             String issuedToCommonName = currentWebsiteSslCertificate.getIssuedTo().getCName();
562             String issuedToOrganization = currentWebsiteSslCertificate.getIssuedTo().getOName();
563             String issuedToOrganizationalUnit = currentWebsiteSslCertificate.getIssuedTo().getUName();
564             String issuedByCommonName = currentWebsiteSslCertificate.getIssuedBy().getCName();
565             String issuedByOrganization = currentWebsiteSslCertificate.getIssuedBy().getOName();
566             String issuedByOrganizationalUnit = currentWebsiteSslCertificate.getIssuedBy().getUName();
567             long startDateLong = currentWebsiteSslCertificate.getValidNotBeforeDate().getTime();
568             long endDateLong = currentWebsiteSslCertificate.getValidNotAfterDate().getTime();
569
570             // Update the database.
571             domainsDatabaseHelper.updateDomainWithCertificate(currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean,
572                     userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt, pinnedSslCertificate, issuedToCommonName, issuedToOrganization, issuedToOrganizationalUnit, issuedByCommonName, issuedByOrganization, issuedByOrganizationalUnit,
573                     startDateLong, endDateLong);
574         } else {  // No certificate is selected.
575             // Update the database, with PINNED_SSL_CERTIFICATE set to false.
576             domainsDatabaseHelper.updateDomainExceptCertificate(currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean,
577                     userAgentString, fontSizeInt, displayWebpageImagesInt, nightModeInt, false);
578         }
579     }
580
581     private void populateDomainsListView(final int highlightedDomainDatabaseId) {
582         // get a handle for the current `domains_listview`.
583         domainsListView = (ListView) findViewById(R.id.domains_listview);
584
585         // Get a `Cursor` with the current contents of the domains database.
586         Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
587
588         // Setup `domainsCursorAdapter` with `this` context.  `false` disables `autoRequery`.
589         CursorAdapter domainsCursorAdapter = new CursorAdapter(context, domainsCursor, false) {
590             @Override
591             public View newView(Context context, Cursor cursor, ViewGroup parent) {
592                 // Inflate the individual item layout.  `false` does not attach it to the root.
593                 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
594             }
595
596             @Override
597             public void bindView(View view, Context context, Cursor cursor) {
598                 // Set the domain name.
599                 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
600                 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
601                 domainNameTextView.setText(domainNameString);
602             }
603         };
604
605         // Update the `ListView`.
606         domainsListView.setAdapter(domainsCursorAdapter);
607
608         // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
609         if (DomainsActivity.twoPanedMode && (domainsCursor.getCount() > 0)) {  // Two-paned mode is enabled and there is at least one domain.
610             // Initialize `highlightedDomainPosition`.
611             int highlightedDomainPosition = 0;
612
613             // Get the cursor position for the highlighted domain.
614             for (int i = 0; i < domainsCursor.getCount(); i++) {
615                 // Move to position `i` in the cursor.
616                 domainsCursor.moveToPosition(i);
617
618                 // Get the database ID for this position.
619                 int currentDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
620
621                 // Set `highlightedDomainPosition` if the database ID for this matches `highlightedDomainDatabaseId`.
622                 if (highlightedDomainDatabaseId == currentDatabaseId) {
623                     highlightedDomainPosition = i;
624                 }
625             }
626
627             // Select the highlighted domain.
628             domainsListView.setItemChecked(highlightedDomainPosition, true);
629
630             // Get the `databaseId` for the highlighted domain.
631             domainsCursor.moveToPosition(highlightedDomainPosition);
632             currentDomainDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
633
634             // Store `databaseId` in `argumentsBundle`.
635             Bundle argumentsBundle = new Bundle();
636             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
637
638             // Add `argumentsBundle` to `domainSettingsFragment`.
639             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
640             domainSettingsFragment.setArguments(argumentsBundle);
641
642             // Display `domainSettingsFragment`.
643             supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
644
645             // Enable the options `MenuItems`.
646             deleteMenuItem.setEnabled(true);
647
648             // Set the delete icon according to the theme.
649             if (MainWebViewActivity.darkTheme) {
650                 deleteMenuItem.setIcon(R.drawable.delete_dark);
651             } else {
652                 deleteMenuItem.setIcon(R.drawable.delete_light);
653             }
654         } else if (twoPanedMode) {  // Two-paned mode is enabled but there are no domains.
655             // Disable the options `MenuItems`.
656             deleteMenuItem.setEnabled(false);
657             deleteMenuItem.setIcon(R.drawable.delete_blue);
658         }
659     }
660 }