Populate and save the domain `Switches` and `Spinners`.
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / activities / DomainSettingsActivity.java
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;
     }
 }