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.content.Intent;
24 import android.database.Cursor;
25 import android.os.Bundle;
26 import android.support.design.widget.FloatingActionButton;
27 import android.support.v4.app.NavUtils;
28 import android.support.v7.app.ActionBar;
29 import android.support.v7.app.AppCompatActivity;
30 import android.support.v7.app.AppCompatDialogFragment;
31 import android.support.v7.widget.Toolbar;
32 import android.view.Menu;
33 import android.view.MenuItem;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.AdapterView;
37 import android.widget.CursorAdapter;
38 import android.widget.EditText;
39 import android.widget.ListView;
40 import android.widget.Spinner;
41 import android.widget.Switch;
42 import android.widget.TextView;
44 import com.stoutner.privacybrowser.R;
45 import com.stoutner.privacybrowser.dialogs.AddDomainDialog;
46 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
47 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
49 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
50 // `context` is used in `onCreate()` and `onOptionsItemSelected()`.
53 // `domainsDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
54 private static DomainsDatabaseHelper domainsDatabaseHelper;
56 // `twoPaneMode` is used in `onCreate()` and `updateDomainsListView()`.
57 private boolean twoPaneMode;
59 // `domainsRecyclerView` is used in `onCreate()` and `updateDomainsListView()`.
60 private ListView domainsListView;
62 // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
63 private int databaseId;
65 // `saveMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
66 private MenuItem saveMenuItem;
68 // `deleteMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
69 private MenuItem deleteMenuItem;
72 protected void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
74 setContentView(R.layout.domains_coordinatorlayout);
76 // Get a handle for the context.
79 // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
80 final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.domains_toolbar);
81 setSupportActionBar(bookmarksAppBar);
83 // Display the home arrow on `SupportActionBar`.
84 ActionBar appBar = getSupportActionBar();
85 assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null.
86 appBar.setDisplayHomeAsUpEnabled(true);
88 // Initialize the database handler. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
89 // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
90 domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
92 // Determine if we are in two pane mode. `domains_settings_linearlayout` is only populated if two panes are present.
93 twoPaneMode = ((findViewById(R.id.domain_settings_scrollview)) != null);
95 // Initialize `domainsListView`.
96 domainsListView = (ListView) findViewById(R.id.domains_listview);
98 domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
100 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
101 // Convert the id from `long` to `int` to match the format of the domains database.
102 databaseId = (int) id;
104 // Display the Domain Settings.
105 if (twoPaneMode) { // Display a fragment in two paned mode.
106 // Enable the options `MenuItems`.
107 saveMenuItem.setEnabled(true);
108 deleteMenuItem.setEnabled(true);
110 // Store `databaseId` in `argumentsBundle`.
111 Bundle argumentsBundle = new Bundle();
112 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
114 // Add `argumentsBundle` to `domainSettingsFragment`.
115 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
116 domainSettingsFragment.setArguments(argumentsBundle);
118 // Display `domainSettingsFragment`.
119 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
120 } else { // Load the second activity on smaller screens.
121 // Create `domainSettingsActivityIntent` with the `databaseId`.
122 Intent domainSettingsActivityIntent = new Intent(context, DomainSettingsActivity.class);
123 domainSettingsActivityIntent.putExtra(DomainSettingsFragment.DATABASE_ID, databaseId);
125 // Start `DomainSettingsActivity`.
126 context.startActivity(domainSettingsActivityIntent);
131 FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
132 addDomainFAB.setOnClickListener(new View.OnClickListener() {
134 public void onClick(View view) {
135 // Show the `AddDomainDialog` `AlertDialog` and name the instance `@string/add_domain`.
136 AppCompatDialogFragment addDomainDialog = new AddDomainDialog();
137 addDomainDialog.show(getSupportFragmentManager(), getResources().getString(R.string.add_domain));
143 public boolean onCreateOptionsMenu(Menu menu) {
145 getMenuInflater().inflate(R.menu.domains_options_menu, menu);
147 // Store the `MenuItems` for future use.
148 deleteMenuItem = menu.findItem(R.id.delete_domain);
149 saveMenuItem = menu.findItem(R.id.save_domain);
151 // Only display the options `MenuItems` in two pane mode.
152 deleteMenuItem.setVisible(twoPaneMode);
153 saveMenuItem.setVisible(twoPaneMode);
155 // Load the `ListView`. We have to do this from `onCreateOptionsMenu()` instead of `onCreate()` because `updateDomainsListView()` needs the `MenuItems` to be inflated.
156 updateDomainsListView();
163 public boolean onOptionsItemSelected(MenuItem menuItem) {
164 // Get the ID of the `MenuItem` that was selected.
165 int menuItemID = menuItem.getItemId();
167 switch (menuItemID) {
168 case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
170 NavUtils.navigateUpFromSameTask(this);
173 case R.id.save_domain:
174 // Get handles for the domain settings.
175 EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
176 Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
177 Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
178 Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
179 Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
180 Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
181 Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
182 EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
183 Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
185 // Extract the data for the domain settings.
186 String domainNameString = domainNameEditText.getText().toString();
187 boolean javaScriptEnabled = javaScriptEnabledSwitch.isChecked();
188 boolean firstPartyCookiesEnabled = firstPartyCookiesEnabledSwitch.isChecked();
189 boolean thirdPartyCookiesEnabled = thirdPartyCookiesEnabledSwitch.isChecked();
190 boolean domStorageEnabledEnabled = domStorageEnabledSwitch.isChecked();
191 boolean formDataEnabled = formDataEnabledSwitch.isChecked();
192 int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
193 int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
195 // Get the data for the `Spinners` from the entry values string arrays.
196 String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPosition];
197 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePosition]);
199 // Check to see if we are using a custom user agent.
200 if (userAgentString.equals("Custom user agent")) {
201 // Set `userAgentString` to the custom user agent string.
202 userAgentString = customUserAgentEditText.getText().toString();
205 // Save the domain settings.
206 domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
209 case R.id.delete_domain:
210 // Delete the selected domain.
211 domainsDatabaseHelper.deleteDomain(databaseId);
213 // Detach the domain settings fragment.
214 getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
216 // Update the `ListView`.
217 updateDomainsListView();
224 public void onAddDomain(AppCompatDialogFragment dialogFragment) {
225 // Get the `domainNameEditText` from `dialogFragment` and extract the string.
226 EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
227 String domainNameString = domainNameEditText.getText().toString();
229 // Create the domain.
230 domainsDatabaseHelper.addDomain(domainNameString);
232 // Refresh the `ListView`.
233 updateDomainsListView();
236 private void updateDomainsListView() {
237 // Get a `Cursor` with the current contents of the domains database.
238 Cursor domainsCursor = domainsDatabaseHelper.getCursorOrderedByDomain();
240 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
241 CursorAdapter domainsCursorAdapter = new CursorAdapter(this, domainsCursor, false) {
243 public View newView(Context context, Cursor cursor, ViewGroup parent) {
244 // Inflate the individual item layout. `false` does not attach it to the root.
245 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
249 public void bindView(View view, Context context, Cursor cursor) {
250 // Set the domain name.
251 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
252 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
253 domainNameTextView.setText(domainNameString);
257 // Update the `ListView`.
258 domainsListView.setAdapter(domainsCursorAdapter);
260 // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
261 if (twoPaneMode && (domainsCursor.getCount() > 0)) {
262 // Select the first domain.
263 domainsListView.setItemChecked(0, true);
265 // Get the `databaseId` of the first item.
266 domainsCursor.moveToFirst();
267 databaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
269 // Store `databaseId` in `argumentsBundle`.
270 Bundle argumentsBundle = new Bundle();
271 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
273 // Add `argumentsBundle` to `domainSettingsFragment`.
274 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
275 domainSettingsFragment.setArguments(argumentsBundle);
277 // Display `domainSettingsFragment`.
278 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
280 // Enable the options `MenuItems`.
281 deleteMenuItem.setEnabled(true);
282 deleteMenuItem.setIcon(R.drawable.delete);
283 saveMenuItem.setEnabled(true);
285 // Disable the options `MenuItems`.
286 deleteMenuItem.setEnabled(false);
287 deleteMenuItem.setIcon(R.drawable.delete_blue);
288 saveMenuItem.setEnabled(false);