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.fragments;
22 import android.annotation.SuppressLint;
23 import android.content.Context;
24 import android.database.Cursor;
25 import android.os.Bundle;
26 // We have to use `android.support.v4.app.Fragment` until minimum API >= 23. Otherwise we cannot call `getContext()`.
27 import android.support.v4.app.Fragment;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.webkit.WebView;
32 import android.widget.AdapterView;
33 import android.widget.ArrayAdapter;
34 import android.widget.EditText;
35 import android.widget.Spinner;
36 import android.widget.Switch;
37 import android.widget.TextView;
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
42 public class DomainSettingsFragment extends Fragment {
43 // `DATABASE_ID` is used by activities calling this fragment.
44 public static final String DATABASE_ID = "database_id";
46 // `databaseId` is used in `onCreate()` and `onCreateView()`.
47 private int databaseId;
50 public void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
53 // Store the database id in `databaseId`.
54 databaseId = getArguments().getInt(DATABASE_ID);
58 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
59 // Inflate `domain_settings`. `false` does not attach it to the root `container`.
60 View domainSettingsView = inflater.inflate(R.layout.domain_settings, container, false);
62 // Get a handle for the `Context`.
63 Context context = getContext();
65 // Get handles for the views in the fragment.
66 EditText domainNameEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_name_edittext);
67 Switch javaScriptEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_javascript_switch);
68 Switch firstPartyCookiesEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_first_party_cookies_switch);
69 Switch thirdPartyCookiesEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_third_party_cookies_switch);
70 Switch domStorageEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_dom_storage_switch);
71 Switch formDataEnabledSwitch = (Switch) domainSettingsView.findViewById(R.id.domain_settings_form_data_switch);
72 Spinner userAgentSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_user_agent_spinner);
73 final TextView userAgentTextView = (TextView) domainSettingsView.findViewById(R.id.domain_settings_user_agent_textview);
74 final EditText customUserAgentEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_custom_user_agent_edittext);
75 Spinner fontSizeSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_font_size_spinner);
77 // Initialize the database handler. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
78 // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
79 DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
81 // Get the database `Cursor` for this ID and move it to the first row.
82 Cursor domainCursor = domainsDatabaseHelper.getCursorForId(databaseId);
83 domainCursor.moveToFirst();
85 // Save the `Cursor` entries as variables.
86 String domainNameString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
87 int javaScriptEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT));
88 int firstPartyCookiesEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES));
89 int thirdPartyCookiesEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES));
90 int domStorageEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE));
91 int formDataEnabledInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA));
92 final String currentUserAgentString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT));
93 int fontSizeInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE));
95 // Create `ArrayAdapters` for the `Spinners`and their `entry values`.
96 ArrayAdapter<CharSequence> userAgentArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entries, android.R.layout.simple_spinner_item);
97 final ArrayAdapter<CharSequence> userAgentEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entry_values, android.R.layout.simple_spinner_item);
98 ArrayAdapter<CharSequence> fontSizeArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entries, android.R.layout.simple_spinner_item);
99 ArrayAdapter<CharSequence> fontSizeEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entry_values, android.R.layout.simple_spinner_item);
101 // Set the drop down style for the `ArrayAdapters`.
102 userAgentArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
103 fontSizeArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
105 // Set the `ArrayAdapters` for the `Spinners`.
106 userAgentSpinner.setAdapter(userAgentArrayAdapter);
107 fontSizeSpinner.setAdapter(fontSizeArrayAdapter);
109 // Set the domain name from the the database cursor.
110 domainNameEditText.setText(domainNameString);
112 // Set the status of the `Switches` from the database cursor.
113 javaScriptEnabledSwitch.setChecked(javaScriptEnabledInt == 1);
114 firstPartyCookiesEnabledSwitch.setChecked(firstPartyCookiesEnabledInt == 1);
115 thirdPartyCookiesEnabledSwitch.setChecked(thirdPartyCookiesEnabledInt == 1);
116 domStorageEnabledSwitch.setChecked(domStorageEnabledInt == 1);
117 formDataEnabledSwitch.setChecked(formDataEnabledInt == 1);
119 // We need to inflated a `WebView` to get the default user agent.
120 // `@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.
121 @SuppressLint("InflateParams") View bareWebViewLayout = inflater.inflate(R.layout.bare_webview, null, false);
122 WebView bareWebView = (WebView) bareWebViewLayout.findViewById(R.id.bare_webview);
123 final String webViewDefaultUserAgentString = bareWebView.getSettings().getUserAgentString();
125 // Get the position of the user agent in `userAgentEntryValuesArrayAdapter`.
126 int userAgentArrayPosition = userAgentEntryValuesArrayAdapter.getPosition(currentUserAgentString);
128 // Set the user agent.
129 if (userAgentArrayPosition == -1) { // We are using a custom `userAgentString`.
130 // Set `userAgentSpinner` to `Custom`.
131 userAgentSpinner.setSelection(userAgentEntryValuesArrayAdapter.getPosition("Custom user agent"));
133 // Hide `userAgentTextView`.
134 userAgentTextView.setVisibility(View.GONE);
136 // Show `customUserAgentEditText` and set `userAgentString` as the text.
137 customUserAgentEditText.setVisibility(View.VISIBLE);
138 customUserAgentEditText.setText(currentUserAgentString);
139 } else if (currentUserAgentString.equals("WebView default user agent")) { // We are using the `WebView` default user agent.
140 // Set the `userAgentSpinner` selection.
141 userAgentSpinner.setSelection(userAgentArrayPosition);
143 // Show `userAgentTextView` and set the text.
144 userAgentTextView.setVisibility(View.VISIBLE);
145 userAgentTextView.setText(webViewDefaultUserAgentString);
147 // Hide `customUserAgentEditText`.
148 customUserAgentEditText.setVisibility(View.GONE);
149 } else { // We are using a standard user agent.
150 // Set the `userAgentSpinner` selection.
151 userAgentSpinner.setSelection(userAgentArrayPosition);
153 // Show `userAgentTextView` and set the text.
154 userAgentTextView.setVisibility(View.VISIBLE);
155 userAgentTextView.setText(currentUserAgentString);
157 // Hide `customUserAgentEditText`.
158 customUserAgentEditText.setVisibility(View.GONE);
161 // Set the selected font size.
162 int fontSizeArrayPosition = fontSizeEntryValuesArrayAdapter.getPosition(String.valueOf(fontSizeInt));
163 fontSizeSpinner.setSelection(fontSizeArrayPosition);
165 // Set the `userAgentSpinner` `onItemClickListener()`.
166 userAgentSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
168 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
169 // Store the new user agent string.
170 String newUserAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[position];
172 // Set the new user agent.
173 switch (newUserAgentString) {
174 case "Custom user agent":
175 // Hide `userAgentTextView`.
176 userAgentTextView.setVisibility(View.GONE);
178 // Show `customUserAgentEditText` and set `userAgentString` as the text.
179 customUserAgentEditText.setVisibility(View.VISIBLE);
180 customUserAgentEditText.setText(currentUserAgentString);
183 case "WebView default user agent":
184 // Show `userAgentTextView` and set the text.
185 userAgentTextView.setVisibility(View.VISIBLE);
186 userAgentTextView.setText(webViewDefaultUserAgentString);
188 // Hide `customUserAgentEditText`.
189 customUserAgentEditText.setVisibility(View.GONE);
193 // Show `userAgentTextView` and set the text.
194 userAgentTextView.setVisibility(View.VISIBLE);
195 userAgentTextView.setText(getResources().getStringArray(R.array.user_agent_entry_values)[position]);
197 // Hide `customUserAgentEditText`.
198 customUserAgentEditText.setVisibility(View.GONE);
204 public void onNothingSelected(AdapterView<?> parent) {
209 return domainSettingsView;