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 // `domainsRecyclerView` is used in `onCreate()` and `updateDomainsListView()`.
57 private ListView domainsListView;
59 // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
60 private int databaseId;
62 // `saveMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
63 private MenuItem saveMenuItem;
65 // `deleteMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
66 private MenuItem deleteMenuItem;
69 protected void onCreate(Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
71 setContentView(R.layout.domains_coordinatorlayout);
73 // Get a handle for the context.
76 // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
77 final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.domains_toolbar);
78 setSupportActionBar(bookmarksAppBar);
80 // Display the home arrow on `SupportActionBar`.
81 ActionBar appBar = getSupportActionBar();
82 assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null.
83 appBar.setDisplayHomeAsUpEnabled(true);
85 // Initialize the database handler. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
86 // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
87 domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
89 // Determine if we are in two pane mode. `domains_settings_linearlayout` is only populated if two panes are present.
90 final boolean twoPaneMode = ((findViewById(R.id.domain_settings_scrollview)) != null);
92 // Initialize `domainsListView`.
93 domainsListView = (ListView) findViewById(R.id.domains_listview);
95 domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
97 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
98 // Convert the id from `long` to `int` to match the format of the domains database.
99 databaseId = (int) id;
101 // Display the Domain Settings.
102 if (twoPaneMode) { // Display a fragment in two paned mode.
103 // Display the options `MenuItems`.
104 saveMenuItem.setVisible(true);
105 deleteMenuItem.setVisible(true);
107 // Store `databaseId` in `argumentsBundle`.
108 Bundle argumentsBundle = new Bundle();
109 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
111 // Add `argumentsBundle` to `domainSettingsFragment`.
112 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
113 domainSettingsFragment.setArguments(argumentsBundle);
115 // Display `domainSettingsFragment`.
116 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
117 } else { // Load the second activity on smaller screens.
118 // Create `domainSettingsActivityIntent` with the `databaseId`.
119 Intent domainSettingsActivityIntent = new Intent(context, DomainSettingsActivity.class);
120 domainSettingsActivityIntent.putExtra(DomainSettingsFragment.DATABASE_ID, databaseId);
122 // Start `DomainSettingsActivity`.
123 context.startActivity(domainSettingsActivityIntent);
128 FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
129 addDomainFAB.setOnClickListener(new View.OnClickListener() {
131 public void onClick(View view) {
132 // Show the `AddDomainDialog` `AlertDialog` and name the instance `@string/add_domain`.
133 AppCompatDialogFragment addDomainDialog = new AddDomainDialog();
134 addDomainDialog.show(getSupportFragmentManager(), getResources().getString(R.string.add_domain));
138 // Load the `ListView`.
139 updateDomainsListView();
143 public boolean onCreateOptionsMenu(Menu menu) {
145 getMenuInflater().inflate(R.menu.domains_options_menu, menu);
147 // Store the `MenuItems` for future use.
148 saveMenuItem = menu.findItem(R.id.save_domain);
149 deleteMenuItem = menu.findItem(R.id.delete_domain);
156 public boolean onOptionsItemSelected(MenuItem menuItem) {
157 // Get the ID of the `MenuItem` that was selected.
158 int menuItemID = menuItem.getItemId();
160 switch (menuItemID) {
161 case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
163 NavUtils.navigateUpFromSameTask(this);
166 case R.id.save_domain:
167 // Get handles for the domain settings.
168 EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
169 Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
170 Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
171 Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
172 Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
173 Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
174 Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
175 EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
176 Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
178 // Extract the data for the domain settings.
179 String domainNameString = domainNameEditText.getText().toString();
180 boolean javaScriptEnabled = javaScriptEnabledSwitch.isChecked();
181 boolean firstPartyCookiesEnabled = firstPartyCookiesEnabledSwitch.isChecked();
182 boolean thirdPartyCookiesEnabled = thirdPartyCookiesEnabledSwitch.isChecked();
183 boolean domStorageEnabledEnabled = domStorageEnabledSwitch.isChecked();
184 boolean formDataEnabled = formDataEnabledSwitch.isChecked();
185 int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
186 int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
188 // Get the data for the `Spinners` from the entry values string arrays.
189 String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPosition];
190 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePosition]);
192 // Check to see if we are using a custom user agent.
193 if (userAgentString.equals("Custom user agent")) {
194 // Set `userAgentString` to the custom user agent string.
195 userAgentString = customUserAgentEditText.getText().toString();
198 // Save the domain settings.
199 domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
202 case R.id.delete_domain:
203 // Delete the selected domain.
204 domainsDatabaseHelper.deleteDomain(databaseId);
206 // Detach the domain settings fragment.
207 getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
209 // Hide the options `MenuItems`.
210 saveMenuItem.setVisible(false);
211 deleteMenuItem.setVisible(false);
213 // Update the `ListView`.
214 updateDomainsListView();
221 public void onAddDomain(AppCompatDialogFragment dialogFragment) {
222 // Get the `domainNameEditText` from `dialogFragment` and extract the string.
223 EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
224 String domainNameString = domainNameEditText.getText().toString();
226 // Create the domain.
227 domainsDatabaseHelper.addDomain(domainNameString);
229 // Refresh the `ListView`.
230 updateDomainsListView();
233 private void updateDomainsListView() {
234 // Get a `Cursor` with the current contents of the domains database.
235 Cursor domainsCursor = domainsDatabaseHelper.getCursorOrderedByDomain();
237 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
238 CursorAdapter domainsCursorAdapter = new CursorAdapter(this, domainsCursor, false) {
240 public View newView(Context context, Cursor cursor, ViewGroup parent) {
241 // Inflate the individual item layout. `false` does not attach it to the root.
242 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
246 public void bindView(View view, Context context, Cursor cursor) {
247 // Set the domain name.
248 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
249 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
250 domainNameTextView.setText(domainNameString);
254 // Update the `RecyclerView`.
255 domainsListView.setAdapter(domainsCursorAdapter);