]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/fragments/DomainSettingsFragment.java
Populate and save the domain `Switches` and `Spinners`.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / DomainSettingsFragment.java
index 3e28fc7fbc54124833f07e22476fa2c521ec3784..2c415dac5fc450e761b682bfc5055b8023dadb5c 100644 (file)
@@ -19,6 +19,8 @@
 
 package com.stoutner.privacybrowser.fragments;
 
+import android.annotation.SuppressLint;
+import android.content.Context;
 import android.database.Cursor;
 import android.os.Bundle;
 // We have to use `android.support.v4.app.Fragment` until minimum API >= 23.  Otherwise we cannot call `getContext()`.
@@ -26,7 +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;
@@ -51,6 +59,21 @@ public class DomainSettingsFragment extends Fragment {
         // Inflate `domain_settings`.  `false` does not attach it to the root `container`.
         View domainSettingsView = inflater.inflate(R.layout.domain_settings, container, false);
 
+        // Get a handle for the `Context`.
+        Context context = getContext();
+
+        // 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`.
         // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
         DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
@@ -59,11 +82,129 @@ public class DomainSettingsFragment extends Fragment {
         Cursor domainCursor = domainsDatabaseHelper.getCursorForId(databaseId);
         domainCursor.moveToFirst();
 
-        // Get handles for the `EditTexts`.
-        EditText domainNameEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_name_edittext);
+        // Save the `Cursor` entries as variables.
+        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);
+        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);
+
+        // Set the drop down style for the `ArrayAdapters`.
+        userAgentArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+        fontSizeArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+
+        // Set the `ArrayAdapters` for the `Spinners`.
+        userAgentSpinner.setAdapter(userAgentArrayAdapter);
+        fontSizeSpinner.setAdapter(fontSizeArrayAdapter);
+
+        // 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 `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;
+                }
+            }
 
-        // Set the text from the database cursor.
-        domainNameEditText.setText(domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN)));
+            @Override
+            public void onNothingSelected(AdapterView<?> parent) {
+                // Do nothing.
+            }
+        });
 
         return domainSettingsView;
     }