]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/commitdiff
Populate and save the domain `Switches` and `Spinners`.
authorSoren Stoutner <soren@stoutner.com>
Tue, 14 Mar 2017 00:54:50 +0000 (17:54 -0700)
committerSoren Stoutner <soren@stoutner.com>
Tue, 14 Mar 2017 00:54:50 +0000 (17:54 -0700)
13 files changed:
.idea/dictionaries/soren.xml
app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java
app/src/main/java/com/stoutner/privacybrowser/activities/DomainSettingsActivity.java
app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java
app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java
app/src/main/java/com/stoutner/privacybrowser/fragments/DomainSettingsFragment.java
app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java
app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java
app/src/main/java/com/stoutner/privacybrowser/helpers/DomainsDatabaseHelper.java
app/src/main/res/layout/domain_settings.xml
app/src/main/res/layout/domain_settings_coordinatorlayout.xml
app/src/main/res/menu/domains_options_menu.xml [new file with mode: 0644]
app/src/main/res/values/strings.xml

index 4833f591d15cf82b005e01c3618fc3c8ebfe952c..7734ed667b4c2df958ac8974ea13e733a96be117 100644 (file)
@@ -28,6 +28,7 @@
       <w>displayorder</w>
       <w>dname</w>
       <w>doesn</w>
+      <w>domainname</w>
       <w>downloadfile</w>
       <w>drawerlayout</w>
       <w>dwallach</w>
index b6f9bc617c1902ae8f069ceb42d587bd7fd8792f..dcb603ff715678d25e6c5511ebe4ad3397804baa 100644 (file)
@@ -505,32 +505,27 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
-        //Inflate the menu.
+        // Inflate the menu.
         getMenuInflater().inflate(R.menu.bookmarks_options_menu, menu);
 
-        return true;
-    }
-
-    @Override
-    public boolean onPrepareOptionsMenu(Menu menu) {
-        super.onPrepareOptionsMenu(menu);
-
+        // Success.
         return true;
     }
 
     @Override
     public boolean onOptionsItemSelected(MenuItem menuItem) {
+        // Get the ID of the `MenuItem` that was selected.
         int menuItemId = menuItem.getItemId();
 
         switch (menuItemId) {
-            case android.R.id.home:
+            case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
                 if (currentFolder.isEmpty()) {  // Exit BookmarksActivity if currently in the home folder.
                     NavUtils.navigateUpFromSameTask(this);
                 } else {  // Navigate up one folder.
                     // Place the former parent folder in `currentFolder`.
                     currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder);
 
-                    // Exit BookmarksActivity if currently in the home folder.
+                    // Update `bookmarksListView`.
                     updateBookmarksListView(currentFolder);
                 }
                 break;
index eba5f321f663cfa8ba514141006068b32d80c499..65546068f58bca1ef3bb02bd901ae1bb309dbf8f 100644 (file)
@@ -21,14 +21,24 @@ package com.stoutner.privacybrowser.activities;
 
 import android.content.Intent;
 import android.os.Bundle;
+import android.support.v4.app.NavUtils;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.widget.EditText;
+import android.widget.Spinner;
+import android.widget.Switch;
 
 import com.stoutner.privacybrowser.R;
 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
+import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
 
 public class DomainSettingsActivity extends AppCompatActivity {
+    // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
+    int databaseId;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -47,7 +57,7 @@ public class DomainSettingsActivity extends AppCompatActivity {
         final Intent launchingIntent = getIntent();
 
         // Extract the `databaseID`.  The default value is `1`.
-        int databaseId = launchingIntent.getIntExtra(DomainSettingsFragment.DATABASE_ID, 1);
+        databaseId = launchingIntent.getIntExtra(DomainSettingsFragment.DATABASE_ID, 1);
 
         // Store `databaseId` in `argumentsBundle`.
         Bundle argumentsBundle = new Bundle();
@@ -58,6 +68,84 @@ public class DomainSettingsActivity extends AppCompatActivity {
         domainSettingsFragment.setArguments(argumentsBundle);
 
         // Display `domainSettingsFragment`.
-        getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_linearlayout, domainSettingsFragment).commit();
+        getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
+    }
+
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        // Inflate the menu.
+        getMenuInflater().inflate(R.menu.domains_options_menu, menu);
+
+        // Show the `MenuItems`.
+        menu.findItem(R.id.save_domain).setVisible(true);
+        menu.findItem(R.id.delete_domain).setVisible(true);
+
+        // Success!
+        return true;
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem menuItem) {
+        // Get the ID of the `MenuItem` that was selected.
+        int menuItemID = menuItem.getItemId();
+
+        // Initialize the database handler.  `this` specifies the context.  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`.
+        DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getApplicationContext(), null, null, 0);
+
+        switch (menuItemID) {
+            case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
+                // Go home.
+                NavUtils.navigateUpFromSameTask(this);
+                break;
+
+            case R.id.save_domain:
+                // Get handles for the domain settings.
+                EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
+                Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
+                Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
+                Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
+                Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
+                Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
+                Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
+                EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
+                Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
+
+                // Extract the data for the domain settings.
+                String domainNameString = domainNameEditText.getText().toString();
+                boolean javaScriptEnabled = javaScriptEnabledSwitch.isChecked();
+                boolean firstPartyCookiesEnabled = firstPartyCookiesEnabledSwitch.isChecked();
+                boolean thirdPartyCookiesEnabled = thirdPartyCookiesEnabledSwitch.isChecked();
+                boolean domStorageEnabledEnabled = domStorageEnabledSwitch.isChecked();
+                boolean formDataEnabled = formDataEnabledSwitch.isChecked();
+                int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
+                int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
+
+                // Get the data for the `Spinners` from the entry values string arrays.
+                String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPosition];
+                int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePosition]);
+
+                // Check to see if we are using a custom user agent.
+                if (userAgentString.equals("Custom user agent")) {
+                    // Set `userAgentString` to the custom user agent string.
+                    userAgentString = customUserAgentEditText.getText().toString();
+                }
+
+                // Save the domain settings.
+                domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
+
+                // Navigate to `DomainsActivity`.
+                NavUtils.navigateUpFromSameTask(this);
+                break;
+
+            case R.id.delete_domain:
+                // Delete the selected domain.
+                domainsDatabaseHelper.deleteDomain(databaseId);
+
+                // Navigate to `DomainsActivity`.
+                NavUtils.navigateUpFromSameTask(this);
+                break;
+        }
+        return true;
     }
 }
index 7cbe8d053b2007279e39259b48f5b2b8fb8816d9..78fbfb53488c908a3ff23677ee51e26b77aeae32 100644 (file)
@@ -24,16 +24,21 @@ import android.content.Intent;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
+import android.support.v4.app.NavUtils;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.app.AppCompatDialogFragment;
 import android.support.v7.widget.Toolbar;
+import android.view.Menu;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.CursorAdapter;
 import android.widget.EditText;
 import android.widget.ListView;
+import android.widget.Spinner;
+import android.widget.Switch;
 import android.widget.TextView;
 
 import com.stoutner.privacybrowser.R;
@@ -42,17 +47,32 @@ import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
 
 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
-    // `domainsDatabaseHelper` is used in `onCreate()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
+    // `context` is used in `onCreate()` and `onOptionsItemSelected()`.
+    Context context;
+
+    // `domainsDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
     private static DomainsDatabaseHelper domainsDatabaseHelper;
 
     // `domainsRecyclerView` is used in `onCreate()` and `updateDomainsListView()`.
     private ListView domainsListView;
 
+    // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
+    private int databaseId;
+
+    // `saveMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
+    private MenuItem saveMenuItem;
+
+    // `deleteMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
+    private MenuItem deleteMenuItem;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.domains_coordinatorlayout);
 
+        // Get a handle for the context.
+        context = this;
+
         // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
         final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.domains_toolbar);
         setSupportActionBar(bookmarksAppBar);
@@ -75,11 +95,15 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-                // Convert the id from long to int to match the format of the domains database.
-                int databaseId = (int) id;
+                // Convert the id from `long` to `int` to match the format of the domains database.
+                databaseId = (int) id;
 
                 // Display the Domain Settings.
                 if (twoPaneMode) {  // Display a fragment in two paned mode.
+                    // Display the options `MenuItems`.
+                    saveMenuItem.setVisible(true);
+                    deleteMenuItem.setVisible(true);
+
                     // Store `databaseId` in `argumentsBundle`.
                     Bundle argumentsBundle = new Bundle();
                     argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
@@ -91,9 +115,6 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                     // Display `domainSettingsFragment`.
                     getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
                 } else { // Load the second activity on smaller screens.
-                    // Get a handle for the context.
-                    Context context = view.getContext();
-
                     // Create `domainSettingsActivityIntent` with the `databaseId`.
                     Intent domainSettingsActivityIntent = new Intent(context, DomainSettingsActivity.class);
                     domainSettingsActivityIntent.putExtra(DomainSettingsFragment.DATABASE_ID, databaseId);
@@ -118,6 +139,84 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         updateDomainsListView();
     }
 
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        // Inflate the menu.
+        getMenuInflater().inflate(R.menu.domains_options_menu, menu);
+
+        // Store the `MenuItems` for future use.
+        saveMenuItem = menu.findItem(R.id.save_domain);
+        deleteMenuItem = menu.findItem(R.id.delete_domain);
+
+        // Success!
+        return true;
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem menuItem) {
+        // Get the ID of the `MenuItem` that was selected.
+        int menuItemID = menuItem.getItemId();
+
+        switch (menuItemID) {
+            case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
+                // Go home.
+                NavUtils.navigateUpFromSameTask(this);
+                break;
+
+            case R.id.save_domain:
+                // Get handles for the domain settings.
+                EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
+                Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
+                Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
+                Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
+                Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
+                Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
+                Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
+                EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
+                Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
+
+                // Extract the data for the domain settings.
+                String domainNameString = domainNameEditText.getText().toString();
+                boolean javaScriptEnabled = javaScriptEnabledSwitch.isChecked();
+                boolean firstPartyCookiesEnabled = firstPartyCookiesEnabledSwitch.isChecked();
+                boolean thirdPartyCookiesEnabled = thirdPartyCookiesEnabledSwitch.isChecked();
+                boolean domStorageEnabledEnabled = domStorageEnabledSwitch.isChecked();
+                boolean formDataEnabled = formDataEnabledSwitch.isChecked();
+                int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
+                int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
+
+                // Get the data for the `Spinners` from the entry values string arrays.
+                String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPosition];
+                int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePosition]);
+
+                // Check to see if we are using a custom user agent.
+                if (userAgentString.equals("Custom user agent")) {
+                    // Set `userAgentString` to the custom user agent string.
+                    userAgentString = customUserAgentEditText.getText().toString();
+                }
+
+                // Save the domain settings.
+                domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
+                break;
+
+            case R.id.delete_domain:
+                // Delete the selected domain.
+                domainsDatabaseHelper.deleteDomain(databaseId);
+
+                // Detach the domain settings fragment.
+                getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
+
+                // Hide the options `MenuItems`.
+                saveMenuItem.setVisible(false);
+                deleteMenuItem.setVisible(false);
+
+                // Update the `ListView`.
+                updateDomainsListView();
+                break;
+        }
+        return true;
+    }
+
     @Override
     public void onAddDomain(AppCompatDialogFragment dialogFragment) {
         // Get the `domainNameEditText` from `dialogFragment` and extract the string.
@@ -146,7 +245,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
             @Override
             public void bindView(View view, Context context, Cursor cursor) {
                 // Set the domain name.
-                String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN));
+                String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
                 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
                 domainNameTextView.setText(domainNameString);
             }
index 2cad4b6d1e79c88f909c69e8d80b72eb127f7349..0457af740b4dc208124484abd9a00311ddaa9492 100644 (file)
@@ -1926,7 +1926,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
 
         // Set the user agent initial status.
         switch (userAgentString) {
-            case "Default user agent":
+            case "WebView default user agent":
                 // Set the user agent to `""`, which uses the default value.
                 mainWebView.getSettings().setUserAgentString("");
                 break;
index a621417414f34674dbe6fda6c842de138c82dc16..2c415dac5fc450e761b682bfc5055b8023dadb5c 100644 (file)
@@ -19,6 +19,7 @@
 
 package com.stoutner.privacybrowser.fragments;
 
+import android.annotation.SuppressLint;
 import android.content.Context;
 import android.database.Cursor;
 import android.os.Bundle;
@@ -27,9 +28,13 @@ import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.webkit.WebView;
+import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.EditText;
 import android.widget.Spinner;
+import android.widget.Switch;
+import android.widget.TextView;
 
 import com.stoutner.privacybrowser.R;
 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
@@ -59,7 +64,14 @@ public class DomainSettingsFragment extends Fragment {
 
         // Get handles for the views in the fragment.
         EditText domainNameEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_name_edittext);
+        Switch javaScriptEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_javascript_switch);
+        Switch firstPartyCookiesEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_first_party_cookies_switch);
+        Switch thirdPartyCookiesEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_third_party_cookies_switch);
+        Switch domStorageEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_dom_storage_switch);
+        Switch formDataEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_form_data_switch);
         Spinner userAgentSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_user_agent_spinner);
+        final TextView userAgentTextView = (TextView) domainSettingsView.findViewById(R.id.domain_settings_user_agent_textview);
+        final EditText customUserAgentEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_custom_user_agent_edittext);
         Spinner fontSizeSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_font_size_spinner);
 
         // Initialize the database handler.  `this` specifies the context.  The two `nulls` do not specify the database name or a `CursorFactory`.
@@ -71,12 +83,18 @@ public class DomainSettingsFragment extends Fragment {
         domainCursor.moveToFirst();
 
         // Save the `Cursor` entries as variables.
-        String domainNameString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN));
+        String domainNameString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
+        int javaScriptEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT));
+        int firstPartyCookiesEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES));
+        int thirdPartyCookiesEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES));
+        int domStorageEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE));
+        int formDataEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA));
+        final String currentUserAgentString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT));
         int fontSizeInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE));
 
         // Create `ArrayAdapters` for the `Spinners`and their `entry values`.
         ArrayAdapter<CharSequence> userAgentArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entries, android.R.layout.simple_spinner_item);
-        ArrayAdapter<CharSequence> userAgentEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entry_values, android.R.layout.simple_spinner_item);
+        final ArrayAdapter<CharSequence> userAgentEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entry_values, android.R.layout.simple_spinner_item);
         ArrayAdapter<CharSequence> fontSizeArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entries, android.R.layout.simple_spinner_item);
         ArrayAdapter<CharSequence> fontSizeEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entry_values, android.R.layout.simple_spinner_item);
 
@@ -88,16 +106,105 @@ public class DomainSettingsFragment extends Fragment {
         userAgentSpinner.setAdapter(userAgentArrayAdapter);
         fontSizeSpinner.setAdapter(fontSizeArrayAdapter);
 
-        //
-        // int userAgentArrayPosition =
+        // Set the domain name from the the database cursor.
+        domainNameEditText.setText(domainNameString);
+
+        // Set the status of the `Switches` from the database cursor.
+        javaScriptEnabledSwitch.setChecked(javaScriptEnabledInt == 1);
+        firstPartyCookiesEnabledSwitch.setChecked(firstPartyCookiesEnabledInt == 1);
+        thirdPartyCookiesEnabledSwitch.setChecked(thirdPartyCookiesEnabledInt == 1);
+        domStorageEnabledSwitch.setChecked(domStorageEnabledInt == 1);
+        formDataEnabledSwitch.setChecked(formDataEnabledInt == 1);
+
+        // We need to inflated a `WebView` to get the default user agent.
+        // `@SuppressLint("InflateParams")` removes the warning about using `null` as the `ViewGroup`, which in this case makes sense because we don't want to display `bare_webview` on the screen.  `false` does not attach the view to the root.
+        @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
+        WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
+        final String webViewDefaultUserAgentString = bareWebView.getSettings().getUserAgentString();
+
+        // Get the position of the user agent in `userAgentEntryValuesArrayAdapter`.
+        int userAgentArrayPosition = userAgentEntryValuesArrayAdapter.getPosition(currentUserAgentString);
+
+        // Set the user agent.
+        if (userAgentArrayPosition == -1) {  // We are using a custom `userAgentString`.
+            // Set `userAgentSpinner` to `Custom`.
+            userAgentSpinner.setSelection(userAgentEntryValuesArrayAdapter.getPosition("Custom user agent"));
+
+            // Hide `userAgentTextView`.
+            userAgentTextView.setVisibility(View.GONE);
+
+            // Show `customUserAgentEditText` and set `userAgentString` as the text.
+            customUserAgentEditText.setVisibility(View.VISIBLE);
+            customUserAgentEditText.setText(currentUserAgentString);
+        } else if (currentUserAgentString.equals("WebView default user agent")) {  // We are using the `WebView` default user agent.
+            // Set the `userAgentSpinner` selection.
+            userAgentSpinner.setSelection(userAgentArrayPosition);
+
+            // Show `userAgentTextView` and set the text.
+            userAgentTextView.setVisibility(View.VISIBLE);
+            userAgentTextView.setText(webViewDefaultUserAgentString);
+
+            // Hide `customUserAgentEditText`.
+            customUserAgentEditText.setVisibility(View.GONE);
+        } else {  // We are using a standard user agent.
+            // Set the `userAgentSpinner` selection.
+            userAgentSpinner.setSelection(userAgentArrayPosition);
+
+            // Show `userAgentTextView` and set the text.
+            userAgentTextView.setVisibility(View.VISIBLE);
+            userAgentTextView.setText(currentUserAgentString);
+
+            // Hide `customUserAgentEditText`.
+            customUserAgentEditText.setVisibility(View.GONE);
+        }
 
         // Set the selected font size.
         int fontSizeArrayPosition = fontSizeEntryValuesArrayAdapter.getPosition(String.valueOf(fontSizeInt));
         fontSizeSpinner.setSelection(fontSizeArrayPosition);
 
-
-        // Set the text from the database cursor.
-        domainNameEditText.setText(domainNameString);
+        // Set the `userAgentSpinner` `onItemClickListener()`.
+        userAgentSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
+            @Override
+            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
+                // Store the new user agent string.
+                String newUserAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[position];
+
+                // Set the new user agent.
+                switch (newUserAgentString) {
+                    case "Custom user agent":
+                        // Hide `userAgentTextView`.
+                        userAgentTextView.setVisibility(View.GONE);
+
+                        // Show `customUserAgentEditText` and set `userAgentString` as the text.
+                        customUserAgentEditText.setVisibility(View.VISIBLE);
+                        customUserAgentEditText.setText(currentUserAgentString);
+                        break;
+
+                    case "WebView default user agent":
+                        // Show `userAgentTextView` and set the text.
+                        userAgentTextView.setVisibility(View.VISIBLE);
+                        userAgentTextView.setText(webViewDefaultUserAgentString);
+
+                        // Hide `customUserAgentEditText`.
+                        customUserAgentEditText.setVisibility(View.GONE);
+                        break;
+
+                    default:
+                        // Show `userAgentTextView` and set the text.
+                        userAgentTextView.setVisibility(View.VISIBLE);
+                        userAgentTextView.setText(getResources().getStringArray(R.array.user_agent_entry_values)[position]);
+
+                        // Hide `customUserAgentEditText`.
+                        customUserAgentEditText.setVisibility(View.GONE);
+                        break;
+                }
+            }
+
+            @Override
+            public void onNothingSelected(AdapterView<?> parent) {
+                // Do nothing.
+            }
+        });
 
         return domainSettingsView;
     }
index 0f7330bb83010555acafd6be440d24d2d47b3e2d..bda6dc82ead9210ca3843b3cb5a882c7f61b3964 100644 (file)
@@ -79,16 +79,15 @@ public class SettingsFragment extends PreferenceFragment {
         thirdPartyCookiesEnabled.setEnabled(savedPreferences.getBoolean("first_party_cookies_enabled", false));
 
 
-        // We need an inflated `WebView` to get the default user agent.
+        // We need to inflated a `WebView` to get the default user agent.
         LayoutInflater inflater = getActivity().getLayoutInflater();
-        // `@SuppressLint("InflateParams")` removes the warning about using `null` as the `ViewGroup`, which in this case makes sense because we don't want to display `bare_webview` on the screen.
-        // `false` does not attach the view to the root.
+        // `@SuppressLint("InflateParams")` removes the warning about using `null` as the `ViewGroup`, which in this case makes sense because we don't want to display `bare_webview` on the screen.  `false` does not attach the view to the root.
         @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
         final WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
 
         // Set the current user-agent as the summary text for the "user_agent" preference when the preference screen is loaded.
         switch (savedPreferences.getString("user_agent", "PrivacyBrowser/1.0")) {
-            case "Default user agent":
+            case "WebView default user agent":
                 // Get the user agent text from the webview (which changes based on the version of Android and WebView installed).
                 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
                 break;
@@ -219,7 +218,7 @@ public class SettingsFragment extends PreferenceFragment {
                         String userAgentString = sharedPreferences.getString("user_agent", "PrivacyBrowser/1.0");
 
                         switch (userAgentString) {
-                            case "Default user agent":
+                            case "WebView default user agent":
                                 // Display the user agent as the summary text for `userAgentPreference`, and disable `customUserAgent`.
                                 userAgentPreference.setSummary(bareWebView.getSettings().getUserAgentString());
                                 customUserAgent.setEnabled(false);
index ccd420e1115c9c06c21bffb8493bb8a424fe2839..0daf7f6a9befc6edd66c308dfdda89056011f47f 100644 (file)
@@ -428,7 +428,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder);
         bookmarkContentValues.put(PARENT_FOLDER, newFolder);
 
-        // Update the database.  The last argument is 'null' because there are no 'whereArgs'.
+        // Update the database.  The last argument is `null` because there are no `whereArgs`.
         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, _ID + " = " + databaseId, null);
 
         // Close the database handle.
@@ -439,7 +439,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
         // Get a writable database handle.
         SQLiteDatabase bookmarksDatabase = this.getWritableDatabase();
 
-        // Deletes the row with the given databaseId.  The last argument is null because we don't need additional parameters.
+        // Deletes the row with the given `databaseId`.  The last argument is `null` because we don't need additional parameters.
         bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null);
 
         // Close the database handle.
index 5e7926781ba9aecac3b19e455ce4b1a38c862f3d..d30839fac6fdbece4c8e0ad40650808c1909d1d6 100644 (file)
@@ -31,18 +31,15 @@ public class DomainsDatabaseHelper extends SQLiteOpenHelper {
     private static final String DOMAINS_TABLE = "domains";
     private static final String _ID = "_id";
 
-    public static final String DOMAIN = "domain";
+    public static final String DOMAIN_NAME = "domainname";
+    public static final String ENABLE_JAVASCRIPT = "enablejavascript";
+    public static final String ENABLE_FIRST_PARTY_COOKIES = "enablefirstpartycookies";
+    public static final String ENABLE_THIRD_PARTY_COOKIES = "enablethirdpartycookies";
+    public static final String ENABLE_DOM_STORAGE = "enabledomstorage";
+    public static final String ENABLE_FORM_DATA = "enableformdata";
+    public static final String USER_AGENT = "useragent";
     public static final String FONT_SIZE = "fontsize";
 
-    private static final String ENABLE_JAVASCRIPT = "enablejavascript";
-    private static final String ENABLE_FIRST_PARTY_COOKIES = "enablefirstpartycookies";
-    private static final String ENABLE_THIRD_PARTY_COOKIES = "enablethirdpartycookies";
-    private static final String ENABLE_DOM_STORAGE = "enabledomstorage";
-    private static final String ENABLE_FORM_DATA = "enableformdata";
-    private static final String USER_AGENT_NAME = "useragentname";
-    private static final String USER_AGENT_STRING = "useragentstring";
-    private static final String CUSTOM_USER_AGENT_STRING = "customuseragent";
-
     // Initialize the database.  The lint warnings for the unused parameters are suppressed.
     public DomainsDatabaseHelper(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory cursorFactory, @SuppressWarnings("UnusedParameters") int version) {
         super(context, DOMAINS_DATABASE, cursorFactory, SCHEMA_VERSION);
@@ -53,15 +50,13 @@ public class DomainsDatabaseHelper extends SQLiteOpenHelper {
         // Setup the SQL string to create the `domains` table.
         final String CREATE_DOMAINS_TABLE = "CREATE TABLE " + DOMAINS_TABLE + " (" +
                 _ID + " integer primary key, " +
-                DOMAIN + " text, " +
+                DOMAIN_NAME + " text, " +
                 ENABLE_JAVASCRIPT + " boolean, " +
                 ENABLE_FIRST_PARTY_COOKIES + " boolean, " +
                 ENABLE_THIRD_PARTY_COOKIES + " boolean, " +
                 ENABLE_DOM_STORAGE + " boolean, " +
                 ENABLE_FORM_DATA + " boolean, " +
-                USER_AGENT_NAME + " text, " +
-                USER_AGENT_STRING + " text, " +
-                CUSTOM_USER_AGENT_STRING + " text, " +
+                USER_AGENT + " text, " +
                 FONT_SIZE + " integer);";
 
         // Create the `domains` table if it doesn't exist.
@@ -77,9 +72,9 @@ public class DomainsDatabaseHelper extends SQLiteOpenHelper {
         // Get a readable database handle.
         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
 
-        // Get everything in `DOMAINS_TABLE` ordered by `DOMAIN`.
+        // Get everything in `DOMAINS_TABLE` ordered by `DOMAIN_NAME`.
         final String GET_CURSOR_SORTED_BY_DOMAIN = "Select * FROM " + DOMAINS_TABLE +
-                " ORDER BY " + DOMAIN + " ASC";
+                " ORDER BY " + DOMAIN_NAME + " ASC";
 
         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.  We can't close the `Cursor` because we need to use it in the parent activity.
         return domainsDatabase.rawQuery(GET_CURSOR_SORTED_BY_DOMAIN, null);
@@ -98,19 +93,17 @@ public class DomainsDatabaseHelper extends SQLiteOpenHelper {
     }
 
     public void addDomain(String domainName) {
-        // We need to store the domain data in a `ContentValues`.
+        // Store the domain data in a `ContentValues`.
         ContentValues domainContentValues = new ContentValues();
 
         // Create entries for each field in the database.  The ID is created automatically.
-        domainContentValues.put(DOMAIN, domainName);
+        domainContentValues.put(DOMAIN_NAME, domainName);
         domainContentValues.put(ENABLE_JAVASCRIPT, false);
         domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, false);
         domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, false);
         domainContentValues.put(ENABLE_DOM_STORAGE, false);
         domainContentValues.put(ENABLE_FORM_DATA, false);
-        domainContentValues.put(USER_AGENT_NAME, "Privacy Browser 1.0");
-        domainContentValues.put(USER_AGENT_STRING, "PrivacyBrowser/1.0");
-        domainContentValues.put(CUSTOM_USER_AGENT_STRING, "PrivacyBrowser/1.0");
+        domainContentValues.put(USER_AGENT, "PrivacyBrowser/1.0");
         domainContentValues.put(FONT_SIZE, "100");
 
         // Get a writable database handle.
@@ -122,4 +115,39 @@ public class DomainsDatabaseHelper extends SQLiteOpenHelper {
         // Close the database handle.
         domainsDatabase.close();
     }
+
+    public void saveDomain(int databaseId, String domainName, boolean javaScriptEnabled, boolean firstPartyCookiesEnabled, boolean thirdPartyCookiesEnabled, boolean domStorageEnabled, boolean formDataEnabled, String userAgent, int fontSize) {
+        // Store the domain data in a `ContentValues`.
+        ContentValues domainContentValues = new ContentValues();
+
+        // Add entries for each field in the database.
+        domainContentValues.put(DOMAIN_NAME, domainName);
+        domainContentValues.put(ENABLE_JAVASCRIPT, javaScriptEnabled);
+        domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, firstPartyCookiesEnabled);
+        domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, thirdPartyCookiesEnabled);
+        domainContentValues.put(ENABLE_DOM_STORAGE, domStorageEnabled);
+        domainContentValues.put(ENABLE_FORM_DATA, formDataEnabled);
+        domainContentValues.put(USER_AGENT, userAgent);
+        domainContentValues.put(FONT_SIZE, fontSize);
+
+        // Get a writable database handle.
+        SQLiteDatabase domainsDatabase = this.getWritableDatabase();
+
+        // Update the row for `databaseId`.  The last argument is `null` because there are no `whereArgs`.
+        domainsDatabase.update(DOMAINS_TABLE, domainContentValues, _ID + " = " + databaseId, null);
+
+        // Close the database handle.
+        domainsDatabase.close();
+    }
+
+    public void deleteDomain(int databaseId) {
+        // Get a writable database handle.
+        SQLiteDatabase domainsDatabase = this.getWritableDatabase();
+
+        // Delete the row for `databaseId`.  The last argument is `null` because we don't need additional parameters.
+        domainsDatabase.delete(DOMAINS_TABLE, _ID + " = " + databaseId, null);
+
+        // Close the database handle.
+        domainsDatabase.close();
+    }
 }
index 725041ea1ab702a6299b659dbaa5aab6b91e65e7..d550386ee5f48d3c2703e6398f3fb053335b5c2f 100644 (file)
@@ -46,6 +46,7 @@
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:layout_marginBottom="26dp"
+                android:layout_marginEnd="10dp"
                 android:layout_gravity="bottom"
                 android:src="@drawable/domains"
                 android:tint="@color/blue_800"
@@ -77,6 +78,7 @@
             <ImageView
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
+                android:layout_marginEnd="10dp"
                 android:layout_gravity="center_vertical"
                 android:src="@drawable/privacy_mode"
                 tools:ignore="contentDescription" />
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:layout_marginTop="1dp"
+                android:layout_marginEnd="10dp"
                 android:layout_gravity="center_vertical"
                 android:src="@drawable/cookies_enabled"
                 android:tint="@color/blue_800"
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:layout_marginTop="1dp"
+                android:layout_marginEnd="10dp"
                 android:layout_gravity="center_vertical"
                 android:src="@drawable/cookies_enabled"
                 android:tint="@color/blue_800"
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:layout_marginTop="1dp"
+                android:layout_marginEnd="10dp"
                 android:layout_gravity="center_vertical"
                 android:src="@drawable/dom_storage_enabled"
                 android:tint="@color/blue_800"
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:layout_marginTop="1dp"
+                android:layout_marginEnd="10dp"
                 android:layout_gravity="center_vertical"
                 android:src="@drawable/form_data_enabled"
                 android:tint="@color/blue_800"
         <LinearLayout
             android:layout_height="wrap_content"
             android:layout_width="match_parent"
-            android:orientation="horizontal">
+            android:orientation="vertical"
+            android:layout_marginTop="14dp"
+            android:layout_marginBottom="14dp" >
 
-            <ImageView
+            <LinearLayout
                 android:layout_height="wrap_content"
-                android:layout_width="wrap_content"
-                android:layout_marginTop="1dp"
-                android:layout_gravity="center_vertical"
-                android:src="@drawable/user_agent"
-                android:tint="@color/blue_800"
-                android:contentDescription="@string/user_agent" />
+                android:layout_width="match_parent"
+                android:orientation="horizontal">
 
-            <Spinner
-                android:id="@+id/domain_settings_user_agent_spinner"
+                <ImageView
+                    android:layout_height="wrap_content"
+                    android:layout_width="wrap_content"
+                    android:layout_marginTop="1dp"
+                    android:layout_marginEnd="10dp"
+                    android:layout_gravity="center_vertical"
+                    android:src="@drawable/user_agent"
+                    android:tint="@color/blue_800"
+                    android:contentDescription="@string/user_agent" />
+
+                <Spinner
+                    android:id="@+id/domain_settings_user_agent_spinner"
+                    android:layout_height="wrap_content"
+                    android:layout_width="match_parent"
+                    android:labelFor="@+id/domain_settings_custom_user_agent_edittext"/>
+            </LinearLayout>
+
+            <TextView
+                android:id="@+id/domain_settings_user_agent_textview"
+                android:layout_height="match_parent"
+                android:layout_width="match_parent"
+                android:layout_marginStart="43dp"
+                android:layout_marginEnd="36dp"
+                android:textSize="13sp"/>
+
+            <EditText
+                android:id="@id/domain_settings_custom_user_agent_edittext"
                 android:layout_height="wrap_content"
                 android:layout_width="match_parent"
-                android:layout_marginTop="14dp"
-                android:layout_marginBottom="14dp" />
+                android:layout_marginStart="38dp"
+                android:layout_marginEnd="60dp"
+                android:inputType="textUri" />
         </LinearLayout>
 
         <!-- Font Size. -->
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:layout_marginTop="1dp"
+                android:layout_marginEnd="10dp"
                 android:layout_gravity="center_vertical"
                 android:src="@drawable/font_size"
                 android:tint="@color/blue_800"
index 7c47eba91d65639a81beaa3b20105179525bf99c..ceb1beacb7e2770b3953e1718d651b5e4fe0613e 100644 (file)
                 app:popupTheme="@style/LightPopupOverlay" />
         </android.support.design.widget.AppBarLayout>
 
-        <LinearLayout
-            android:id="@+id/domain_settings_linearlayout"
+        <ScrollView
+            android:id="@+id/domain_settings_scrollview"
             android:layout_height="match_parent"
-            android:layout_width="match_parent"
-            android:orientation="horizontal" />
+            android:layout_width="match_parent" />
     </LinearLayout>
 </android.support.design.widget.CoordinatorLayout>
\ No newline at end of file
diff --git a/app/src/main/res/menu/domains_options_menu.xml b/app/src/main/res/menu/domains_options_menu.xml
new file mode 100644 (file)
index 0000000..641dfd8
--- /dev/null
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+  Copyright 2017 Soren Stoutner <soren@stoutner.com>.
+
+  This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
+
+  Privacy Browser is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  Privacy Browser is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>. -->
+
+<menu
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto">
+
+    <item
+        android:id="@+id/save_domain"
+        android:title="@string/save"
+        android:orderInCategory="10"
+        android:visible="false"
+        app:showAsAction="ifRoom" />
+
+    <item
+        android:id="@+id/delete_domain"
+        android:title="@string/delete"
+        android:orderInCategory="10"
+        android:icon="@drawable/delete"
+        android:visible="false"
+        app:showAsAction="ifRoom" />
+</menu>
\ No newline at end of file
index 9dbfc3bf1c91118475f87857f9513ee1f80d1b81..cca1c042501d85e43bc14cfb896853411cb8f28f 100644 (file)
         </string-array>
         <string-array name="user_agent_entry_values" translatable="false">  <!-- None of the items in this `string-array` should be translated. -->
             <item>PrivacyBrowser/1.0</item>
-            <item>Default user agent</item>  <!-- This item must not be translated into other languages because it is referenced in code.  It is never displayed on the screen. -->
+            <item>WebView default user agent</item>  <!-- This item must not be translated into other languages because it is referenced in code.  It is never displayed on the screen. -->
             <item>Mozilla/5.0 (Android 7.1.1; Mobile; rv:50.0) Gecko/50.0 Firefox/50.0</item>
             <item>Mozilla/5.0 (Linux; Android 7.1.1; Nexus 6P Build/N4F26I) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36</item>
             <item>Mozilla/5.0 (iPhone; CPU iPhone OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 (KHTML, like Gecko) Version/10.0 Mobile/14D27 Safari/602.1</item>