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.app.Activity;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.support.design.widget.CoordinatorLayout;
26 import android.support.design.widget.Snackbar;
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.widget.Toolbar;
31 import android.view.Menu;
32 import android.view.MenuItem;
33 import android.view.View;
34 import android.widget.EditText;
35 import android.widget.Spinner;
36 import android.widget.Switch;
38 import com.stoutner.privacybrowser.R;
39 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
40 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
42 public class DomainSettingsActivity extends AppCompatActivity {
43 // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
47 protected void onCreate(Bundle savedInstanceState) {
48 // Set the activity theme.
49 if (MainWebViewActivity.darkTheme) {
50 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
52 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
55 // Run the default commands.
56 super.onCreate(savedInstanceState);
58 // Set the content view.
59 setContentView(R.layout.domain_settings_coordinatorlayout);
61 // We ned to use `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
62 Toolbar domainSettingsAppBar = (Toolbar) findViewById(R.id.domain_settings_toolbar);
63 setSupportActionBar(domainSettingsAppBar);
65 // Display the home arrow on `appBar`.
66 final ActionBar appBar = getSupportActionBar();
67 assert appBar != null; // This assert removes the incorrect lint warning in Android Studio on the following line that `appBar` might be `null`.
68 appBar.setDisplayHomeAsUpEnabled(true);
70 // Get the intent that started the activity.
71 final Intent launchingIntent = getIntent();
73 // Extract the `databaseID`. The default value is `1`.
74 databaseId = launchingIntent.getIntExtra(DomainSettingsFragment.DATABASE_ID, 1);
76 // Store `databaseId` in `argumentsBundle`.
77 Bundle argumentsBundle = new Bundle();
78 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
80 // Add `argumentsBundle` to `domainSettingsFragment`.
81 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
82 domainSettingsFragment.setArguments(argumentsBundle);
84 // Display `domainSettingsFragment`.
85 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
89 public boolean onCreateOptionsMenu(Menu menu) {
91 getMenuInflater().inflate(R.menu.domains_options_menu, menu);
98 public boolean onOptionsItemSelected(MenuItem menuItem) {
99 // Get the ID of the `MenuItem` that was selected.
100 int menuItemID = menuItem.getItemId();
102 // Initialize the database handler. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
103 // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
104 final DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getApplicationContext(), null, null, 0);
106 switch (menuItemID) {
107 case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
109 NavUtils.navigateUpFromSameTask(this);
112 case R.id.save_domain:
113 // Get handles for the domain settings.
114 EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
115 Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
116 Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
117 Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
118 Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
119 Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
120 Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
121 EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
122 Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
123 Spinner displayWebpageImagesSpinner = (Spinner) findViewById(R.id.domain_settings_display_webpage_images_spinner);
125 // Extract the data for the domain settings.
126 String domainNameString = domainNameEditText.getText().toString();
127 boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked();
128 boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked();
129 boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked();
130 boolean domStorageEnabledEnabledBoolean = domStorageEnabledSwitch.isChecked();
131 boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked();
132 int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
133 int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
134 int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
136 // Get the data for the `Spinners` from the entry values string arrays.
137 String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPositionInt];
138 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePositionInt]);
140 // Check to see if we are using a custom user agent.
141 if (userAgentString.equals("Custom user agent")) {
142 // Set `userAgentString` to the custom user agent string.
143 userAgentString = customUserAgentEditText.getText().toString();
146 // Save the domain settings.
147 domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean, userAgentString, fontSizeInt,
148 displayWebpageImagesInt);
150 // Navigate to `DomainsActivity`.
151 NavUtils.navigateUpFromSameTask(this);
154 case R.id.delete_domain:
155 // Get a handle for the current activity.
156 final Activity activity = this;
158 // Get a handle for `domain_settings_coordinatorlayout` so we can display a `SnackBar` later.
159 CoordinatorLayout domainSettingsCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.domain_settings_coordinatorlayout);
161 // Detach `domain_settings_scrollview`.
162 getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
164 Snackbar.make(domainSettingsCoordinatorLayout, R.string.domain_deleted, Snackbar.LENGTH_SHORT)
165 .setAction(R.string.undo, new View.OnClickListener() {
167 public void onClick(View v) {
168 // Do nothing because everything will be handled by `onDismiss` below.
171 .addCallback(new Snackbar.Callback() {
173 public void onDismissed(Snackbar snackbar, int event) {
175 // The user pushed the `undo` button.
176 case Snackbar.Callback.DISMISS_EVENT_ACTION:
177 // Store `databaseId` in `argumentsBundle`.
178 Bundle argumentsBundle = new Bundle();
179 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
181 // Add `argumentsBundle` to `domainSettingsFragment`.
182 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
183 domainSettingsFragment.setArguments(argumentsBundle);
185 // Display `domainSettingsFragment`.
186 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
189 // The `Snackbar` was dismissed without the `Undo` button being pushed.
191 // Delete the selected domain.
192 domainsDatabaseHelper.deleteDomain(databaseId);
194 // Navigate to `DomainsActivity`.
195 NavUtils.navigateUpFromSameTask(activity);