2 * Copyright © 2017 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.activities;
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;
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;
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;
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;
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;
61 // `undoDeleteSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()`.
62 public static Snackbar undoDeleteSnackbar;
64 // `dismissingSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()`.
65 public static boolean dismissingSnackbar;
67 // `context` is used in `onCreate()`, `onOptionsItemSelected()`, and `onAddDomain()`.
68 private Context context;
70 // `supportFragmentManager` is used in `onCreate()` and `onCreateOptionsMenu()`.
71 private FragmentManager supportFragmentManager;
73 // `domainsDatabaseHelper` is used in `onCreate()` and `saveDomainSettings()`.
74 private static DomainsDatabaseHelper domainsDatabaseHelper;
76 // `domainsListView` is used in `onCreate()` and `populateDomainsList()`.
77 private ListView domainsListView;
79 // `addDomainFAB` is used in `onCreate()`, `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`.
80 private FloatingActionButton addDomainFAB;
82 // `deletedDomainPosition` is used in an inner and outer class in `onOptionsItemSelected()`.
83 private int deletedDomainPosition;
85 // `restartAfterRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
86 private boolean restartAfterRotate;
88 // `domainSettingsDisplayedBeforeRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
89 private boolean domainSettingsDisplayedBeforeRotate;
91 // `domainSettingsDatabaseIdBeforeRotate` is used in `onCreate()` and `onCreateOptionsMenu()`.
92 private int domainSettingsDatabaseIdBeforeRotate;
95 protected void onCreate(Bundle savedInstanceState) {
96 // Set the activity theme.
97 if (MainWebViewActivity.darkTheme) {
98 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
100 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
103 // Run the default commands.
104 super.onCreate(savedInstanceState);
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");
113 // Set the content view.
114 setContentView(R.layout.domains_coordinatorlayout);
116 // Get a handle for the context.
119 // Get a handle for the fragment manager.
120 supportFragmentManager = getSupportFragmentManager();
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);
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);
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);
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);
137 // Configure `addDomainFAB`.
138 addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
139 addDomainFAB.setOnClickListener(new View.OnClickListener() {
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));
150 public boolean onCreateOptionsMenu(Menu menu) {
152 getMenuInflater().inflate(R.menu.domains_options_menu, menu);
154 // Store `deleteMenuItem` for future use.
155 deleteMenuItem = menu.findItem(R.id.delete_domain);
157 // Only display `deleteMenuItem` (initially) in two-paned mode.
158 deleteMenuItem.setVisible(twoPanedMode);
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;
165 // Store `currentDomainDatabaseId`.
166 currentDomainDatabaseId = domainSettingsDatabaseIdBeforeRotate;
168 // Add `currentDomainDatabaseId` to `argumentsBundle`.
169 Bundle argumentsBundle = new Bundle();
170 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
172 // Add `argumentsBundle` to `domainSettingsFragment`.
173 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
174 domainSettingsFragment.setArguments(argumentsBundle);
176 // Show `deleteMenuItem`.
177 deleteMenuItem.setVisible(true);
179 // Hide `add_domain_fab`.
180 addDomainFAB.setVisibility(View.GONE);
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;
188 // Display `DomainsListFragment`.
189 DomainsListFragment domainsListFragment = new DomainsListFragment();
190 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
191 supportFragmentManager.executePendingTransactions();
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();
201 // Populate the list of domains. `-1` highlights the first domain.
202 populateDomainsListView(-1);
210 public boolean onOptionsItemSelected(MenuItem menuItem) {
211 // Get the ID of the `MenuItem` that was selected.
212 int menuItemID = menuItem.getItemId();
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();
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();
228 // Display `DomainsListFragment`.
229 DomainsListFragment domainsListFragment = new DomainsListFragment();
230 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
231 supportFragmentManager.executePendingTransactions();
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);
236 // Display `addDomainFAB`.
237 addDomainFAB.setVisibility(View.VISIBLE);
239 // Hide `deleteMenuItem`.
240 deleteMenuItem.setVisible(false);
241 } else { // The device is in single-paned mode and `DomainsListFragment` is displayed.
243 NavUtils.navigateUpFromSameTask(this);
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;
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();
256 // Disable the options `MenuItems`.
257 deleteMenuItem.setEnabled(false);
258 deleteMenuItem.setIcon(R.drawable.delete_blue);
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();
268 // Display `addDomainFAB`.
269 addDomainFAB.setVisibility(View.VISIBLE);
271 // Hide `deleteMenuItem`.
272 deleteMenuItem.setVisible(false);
275 // Get a `Cursor` that does not show the domain to be deleted.
276 Cursor domainsPendingDeleteCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomainExcept(databaseIdToDelete);
278 // Setup `domainsPendingDeleteCursorAdapter` with `this` context. `false` disables `autoRequery`.
279 CursorAdapter domainsPendingDeleteCursorAdapter = new CursorAdapter(this, domainsPendingDeleteCursor, false) {
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);
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);
295 // Update the handle for the current `domains_listview`.
296 domainsListView = (ListView) findViewById(R.id.domains_listview);
298 // Update the `ListView`.
299 domainsListView.setAdapter(domainsPendingDeleteCursorAdapter);
301 // Display a `Snackbar`.
302 undoDeleteSnackbar = Snackbar.make(domainsListView, R.string.domain_deleted, Snackbar.LENGTH_LONG)
303 .setAction(R.string.undo, new View.OnClickListener() {
305 public void onClick(View v) {
306 // Do nothing because everything will be handled by `onDismissed()` below.
309 .addCallback(new Snackbar.Callback() {
311 public void onDismissed(Snackbar snackbar, int 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);
319 // Add `argumentsBundle` to `domainSettingsFragment`.
320 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
321 domainSettingsFragment.setArguments(argumentsBundle);
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();
328 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
329 CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(context, undoDeleteDomainsCursor, false) {
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);
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);
345 // Update the `ListView`.
346 domainsListView.setAdapter(undoDeleteDomainsCursorAdapter);
347 // Select the previously deleted domain in `domainsListView`.
348 domainsListView.setItemChecked(deletedDomainPosition, true);
350 // Display `domainSettingsFragment`.
351 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
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();
360 // Hide `add_domain_fab`.
361 FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
362 addDomainFAB.setVisibility(View.GONE);
364 // Show and enable `deleteMenuItem`.
365 deleteMenuItem.setVisible(true);
367 // Display `domainSettingsFragment`.
368 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
372 // The `Snackbar` was dismissed without the `Undo` button being pushed.
374 // Delete the selected domain.
375 domainsDatabaseHelper.deleteDomain(databaseIdToDelete);
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() {
383 // Enable `deleteMenuItem` according to the display mode.
384 if (twoPanedMode) { // Two-paned mode.
385 // Enable `deleteMenuItem`.
386 deleteMenuItem.setEnabled(true);
388 // Set the delete icon according to the theme.
389 if (MainWebViewActivity.darkTheme) {
390 deleteMenuItem.setIcon(R.drawable.delete_dark);
392 deleteMenuItem.setIcon(R.drawable.delete_light);
394 } else { // Single-paned mode.
395 // Show `deleteMenuItem`.
396 deleteMenuItem.setVisible(true);
399 // Reset `dismissingSnackbar`.
400 dismissingSnackbar = false;
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);
412 undoDeleteSnackbar.show();
416 // Consume the event.
421 protected void onSaveInstanceState(Bundle outState) {
422 // Store the current `DomainSettingsFragment` state in `outState`.
423 if (findViewById(R.id.domain_settings_scrollview) != null) { // `DomainSettingsFragment` is displayed.
424 // Store `DomainSettingsDisplayed`.
425 outState.putBoolean("domainSettingsDisplayed", true);
426 outState.putInt("domainSettingsDatabaseId", DomainSettingsFragment.databaseId);
427 } else { // `DomainSettingsFragment` is not displayed.
428 outState.putBoolean("domainSettingsDisplayed", false);
429 outState.putInt("domainSettingsDatabaseId", -1);
432 super.onSaveInstanceState(outState);
435 // Control what the navigation bar back button does.
437 public void onBackPressed() {
438 if (twoPanedMode) { // The device is in two-paned mode.
439 // Save the current domain settings if the domain settings fragment is displayed.
440 if (findViewById(R.id.domain_settings_scrollview) != null) {
441 saveDomainSettings();
445 NavUtils.navigateUpFromSameTask(this);
446 } else if (findViewById(R.id.domain_settings_scrollview) != null) { // The device is in single-paned mode and `DomainSettingsFragment` is displayed.
447 // Save the current domain settings.
448 saveDomainSettings();
450 // Display `DomainsListFragment`.
451 DomainsListFragment domainsListFragment = new DomainsListFragment();
452 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainsListFragment).commit();
453 supportFragmentManager.executePendingTransactions();
455 // Populate the list of domains. `-1` highlights the first domain if in two-paned mode. It has no effect in single-paned mode.
456 populateDomainsListView(-1);
458 // Display `addDomainFAB`.
459 addDomainFAB.setVisibility(View.VISIBLE);
461 // Hide `deleteMenuItem`.
462 deleteMenuItem.setVisible(false);
463 } else { // The device is in single-paned mode and `DomainsListFragment` is displayed.
464 // Pass `onBackPressed()` to the system.
465 super.onBackPressed();
470 public void onAddDomain(AppCompatDialogFragment dialogFragment) {
471 // Dismiss `undoDeleteSnackbar` if it is currently displayed.
472 if ((undoDeleteSnackbar != null) && (undoDeleteSnackbar.isShown())) {
473 undoDeleteSnackbar.dismiss();
476 // Get the `domainNameEditText` from `dialogFragment` and extract the string.
477 EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
478 String domainNameString = domainNameEditText.getText().toString();
480 // Create the domain and store the database ID in `currentDomainDatabaseId`.
481 currentDomainDatabaseId = domainsDatabaseHelper.addDomain(domainNameString);
483 // Display the newly created domain.
484 if (twoPanedMode) { // The device in in two-paned mode.
485 populateDomainsListView(currentDomainDatabaseId);
486 } else { // The device is in single-paned mode.
487 // Hide `add_domain_fab`.
488 addDomainFAB.setVisibility(View.GONE);
490 // Show and enable `deleteMenuItem`.
491 DomainsActivity.deleteMenuItem.setVisible(true);
493 // Add `currentDomainDatabaseId` to `argumentsBundle`.
494 Bundle argumentsBundle = new Bundle();
495 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
497 // Add `argumentsBundle` to `domainSettingsFragment`.
498 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
499 domainSettingsFragment.setArguments(argumentsBundle);
501 // Display `domainSettingsFragment`.
502 supportFragmentManager.beginTransaction().replace(R.id.domains_listview_fragment_container, domainSettingsFragment).commit();
506 private void saveDomainSettings() {
507 // Get handles for the domain settings.
508 EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
509 Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
510 Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
511 Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
512 Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
513 Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
514 Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
515 EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
516 Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
517 Spinner displayWebpageImagesSpinner = (Spinner) findViewById(R.id.domain_settings_display_webpage_images_spinner);
519 // Extract the data for the domain settings.
520 String domainNameString = domainNameEditText.getText().toString();
521 boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked();
522 boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked();
523 boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked();
524 boolean domStorageEnabledEnabledBoolean = domStorageEnabledSwitch.isChecked();
525 boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked();
526 int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
527 int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
528 int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
530 // Get the data for the `Spinners` from the entry values string arrays.
531 String userAgentString = getResources().getStringArray(R.array.domain_settings_user_agent_entry_values)[userAgentPositionInt];
532 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.domain_settings_font_size_entry_values)[fontSizePositionInt]);
534 // Check to see if we are using a custom user agent.
535 if (userAgentString.equals("Custom user agent")) {
536 // Set `userAgentString` to the custom user agent string.
537 userAgentString = customUserAgentEditText.getText().toString();
540 // Save the domain settings.
541 domainsDatabaseHelper.saveDomain(currentDomainDatabaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean, userAgentString, fontSizeInt,
542 displayWebpageImagesInt);
545 private void populateDomainsListView(final int highlightedDomainDatabaseId) {
546 // get a handle for the current `domains_listview`.
547 domainsListView = (ListView) findViewById(R.id.domains_listview);
549 // Get a `Cursor` with the current contents of the domains database.
550 Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain();
552 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
553 CursorAdapter domainsCursorAdapter = new CursorAdapter(context, domainsCursor, false) {
555 public View newView(Context context, Cursor cursor, ViewGroup parent) {
556 // Inflate the individual item layout. `false` does not attach it to the root.
557 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
561 public void bindView(View view, Context context, Cursor cursor) {
562 // Set the domain name.
563 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
564 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
565 domainNameTextView.setText(domainNameString);
569 // Update the `ListView`.
570 domainsListView.setAdapter(domainsCursorAdapter);
572 // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
573 if (DomainsActivity.twoPanedMode && (domainsCursor.getCount() > 0)) { // Two-paned mode is enabled and there is at least one domain.
574 // Initialize `highlightedDomainPosition`.
575 int highlightedDomainPosition = 0;
577 // Get the cursor position for the highlighted domain.
578 for (int i = 0; i < domainsCursor.getCount(); i++) {
579 // Move to position `i` in the cursor.
580 domainsCursor.moveToPosition(i);
582 // Get the database ID for this position.
583 int currentDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
585 // Set `highlightedDomainPosition` if the database ID for this matches `highlightedDomainDatabaseId`.
586 if (highlightedDomainDatabaseId == currentDatabaseId) {
587 highlightedDomainPosition = i;
591 // Select the highlighted domain.
592 domainsListView.setItemChecked(highlightedDomainPosition, true);
594 // Get the `databaseId` for the highlighted domain.
595 domainsCursor.moveToPosition(highlightedDomainPosition);
596 currentDomainDatabaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
598 // Store `databaseId` in `argumentsBundle`.
599 Bundle argumentsBundle = new Bundle();
600 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, currentDomainDatabaseId);
602 // Add `argumentsBundle` to `domainSettingsFragment`.
603 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
604 domainSettingsFragment.setArguments(argumentsBundle);
606 // Display `domainSettingsFragment`.
607 supportFragmentManager.beginTransaction().replace(R.id.domain_settings_fragment_container, domainSettingsFragment).commit();
609 // Enable the options `MenuItems`.
610 deleteMenuItem.setEnabled(true);
612 // Set the delete icon according to the theme.
613 if (MainWebViewActivity.darkTheme) {
614 deleteMenuItem.setIcon(R.drawable.delete_dark);
616 deleteMenuItem.setIcon(R.drawable.delete_light);
618 } else if (twoPanedMode) { // Two-paned mode is enabled but there are no domains.
619 // Disable the options `MenuItems`.
620 deleteMenuItem.setEnabled(false);
621 deleteMenuItem.setIcon(R.drawable.delete_blue);