Don't reset the `WebView` title `onPageStarted()`, because `onPageStarted()` is run...
[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             // Save any changes that have been made to the domain settings.
425             saveDomainSettings();
426
427             // Store `DomainSettingsDisplayed`.
428             outState.putBoolean("domainSettingsDisplayed", true);
429             outState.putInt("domainSettingsDatabaseId", DomainSettingsFragment.databaseId);
430         } else {  // `DomainSettingsFragment` is not displayed.
431             outState.putBoolean("domainSettingsDisplayed", false);
432             outState.putInt("domainSettingsDatabaseId", -1);
433         }
434
435         super.onSaveInstanceState(outState);
436     }
437
438     // Control what the navigation bar back button does.
439     @Override
440     public void onBackPressed() {
441         if (twoPanedMode) {  // The device is in two-paned mode.
442             // Save the current domain settings if the domain settings fragment is displayed.
443             if (findViewById(R.id.domain_settings_scrollview) != null) {
444                 saveDomainSettings();
445             }
446
447             // Go home.
448             NavUtils.navigateUpFromSameTask(this);
449         } else if (findViewById(R.id.domain_settings_scrollview) != null) {  // The device is in single-paned mode and `DomainSettingsFragment` is displayed.
450             // Save the current domain settings.
451             saveDomainSettings();
452
453             // Display `DomainsListFragment`.
454             DomainsListFragment domainsListFragment = new DomainsListFragment();
455             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
456             supportFragmentManager.executePendingTransactions();
457
458             // Populate the list of domains.  `-1` highlights the first domain if in two-paned mode.  It has no effect in single-paned mode.
459             populateDomainsListView(-1);
460
461             // Display `addDomainFAB`.
462             addDomainFAB.setVisibility(View.VISIBLE);
463
464             // Hide `deleteMenuItem`.
465             deleteMenuItem.setVisible(false);
466         } else {  // The device is in single-paned mode and `DomainsListFragment` is displayed.
467             // Pass `onBackPressed()` to the system.
468             super.onBackPressed();
469         }
470     }
471
472     @Override
473     public void onAddDomain(AppCompatDialogFragment dialogFragment) {
474         // Dismiss `undoDeleteSnackbar` if it is currently displayed.
475         if ((undoDeleteSnackbar != null) && (undoDeleteSnackbar.isShown())) {
476             undoDeleteSnackbar.dismiss();
477         }
478
479         // Get the `domainNameEditText` from `dialogFragment` and extract the string.
480         EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
481         String domainNameString = domainNameEditText.getText().toString();
482
483         // Create the domain and store the database ID in `currentDomainDatabaseId`.
484         currentDomainDatabaseId = domainsDatabaseHelper.addDomain(domainNameString);
485
486         // Display the newly created domain.
487         if (twoPanedMode) {  // The device in in two-paned mode.
488             populateDomainsListView(currentDomainDatabaseId);
489         } else {  // The device is in single-paned mode.
490             // Hide `add_domain_fab`.
491             addDomainFAB.setVisibility(View.GONE);
492
493             // Show and enable `deleteMenuItem`.
494             DomainsActivity.deleteMenuItem.setVisible(true);
495
496             // Add `currentDomainDatabaseId` to `argumentsBundle`.
497             Bundle argumentsBundle = new Bundle();
498             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
499
500             // Add `argumentsBundle` to `domainSettingsFragment`.
501             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
502             domainSettingsFragment.setArguments(argumentsBundle);
503
504             // Display `domainSettingsFragment`.
505             supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
506         }
507     }
508
509     private void saveDomainSettings() {
510         // Get handles for the domain settings.
511         EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
512         Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
513         Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
514         Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
515         Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
516         Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
517         Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
518         EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
519         Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
520         Spinner displayWebpageImagesSpinner = (Spinner) findViewById(R.id.domain_settings_display_webpage_images_spinner);
521
522         // Extract the data for the domain settings.
523         String domainNameString = domainNameEditText.getText().toString();
524         boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked();
525         boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked();
526         boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked();
527         boolean domStorageEnabledEnabledBoolean  = domStorageEnabledSwitch.isChecked();
528         boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked();
529         int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
530         int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
531         int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
532
533         // Get the data for the `Spinners` from the entry values string arrays.
534         String userAgentString = getResources().getStringArray(R.array.domain_settings_user_agent_entry_values)[userAgentPositionInt];
535         int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePositionInt]);
536
537         // Check to see if we are using a custom user agent.
538         if (userAgentString.equals("Custom user agent")) {
539             // Set `userAgentString` to the custom user agent string.
540             userAgentString = customUserAgentEditText.getText().toString();
541         }
542
543         // Save the domain settings.
544         domainsDatabaseHelper.saveDomain(currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean, userAgentString, fontSizeInt,
545                 displayWebpageImagesInt);
546     }
547
548     private void populateDomainsListView(final int highlightedDomainDatabaseId) {
549         // get a handle for the current `domains_listview`.
550         domainsListView = (ListView) findViewById(R.id.domains_listview);
551
552         // Get a `Cursor` with the current contents of the domains database.
553         Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
554
555         // Setup `domainsCursorAdapter` with `this` context.  `false` disables `autoRequery`.
556         CursorAdapter domainsCursorAdapter = new CursorAdapter(context, domainsCursor, false) {
557             @Override
558             public View newView(Context context, Cursor cursor, ViewGroup parent) {
559                 // Inflate the individual item layout.  `false` does not attach it to the root.
560                 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
561             }
562
563             @Override
564             public void bindView(View view, Context context, Cursor cursor) {
565                 // Set the domain name.
566                 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
567                 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
568                 domainNameTextView.setText(domainNameString);
569             }
570         };
571
572         // Update the `ListView`.
573         domainsListView.setAdapter(domainsCursorAdapter);
574
575         // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
576         if (DomainsActivity.twoPanedMode && (domainsCursor.getCount() > 0)) {  // Two-paned mode is enabled and there is at least one domain.
577             // Initialize `highlightedDomainPosition`.
578             int highlightedDomainPosition = 0;
579
580             // Get the cursor position for the highlighted domain.
581             for (int i = 0; i < domainsCursor.getCount(); i++) {
582                 // Move to position `i` in the cursor.
583                 domainsCursor.moveToPosition(i);
584
585                 // Get the database ID for this position.
586                 int currentDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
587
588                 // Set `highlightedDomainPosition` if the database ID for this matches `highlightedDomainDatabaseId`.
589                 if (highlightedDomainDatabaseId == currentDatabaseId) {
590                     highlightedDomainPosition = i;
591                 }
592             }
593
594             // Select the highlighted domain.
595             domainsListView.setItemChecked(highlightedDomainPosition, true);
596
597             // Get the `databaseId` for the highlighted domain.
598             domainsCursor.moveToPosition(highlightedDomainPosition);
599             currentDomainDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
600
601             // Store `databaseId` in `argumentsBundle`.
602             Bundle argumentsBundle = new Bundle();
603             argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
604
605             // Add `argumentsBundle` to `domainSettingsFragment`.
606             DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
607             domainSettingsFragment.setArguments(argumentsBundle);
608
609             // Display `domainSettingsFragment`.
610             supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
611
612             // Enable the options `MenuItems`.
613             deleteMenuItem.setEnabled(true);
614
615             // Set the delete icon according to the theme.
616             if (MainWebViewActivity.darkTheme) {
617                 deleteMenuItem.setIcon(R.drawable.delete_dark);
618             } else {
619                 deleteMenuItem.setIcon(R.drawable.delete_light);
620             }
621         } else if (twoPanedMode) {  // Two-paned mode is enabled but there are no domains.
622             // Disable the options `MenuItems`.
623             deleteMenuItem.setEnabled(false);
624             deleteMenuItem.setIcon(R.drawable.delete_blue);
625         }
626     }
627 }