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 super.onCreate(savedInstanceState);
49 setContentView(R.layout.domain_settings_coordinatorlayout);
51 // We ned to use `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
52 Toolbar domainSettingsAppBar = (Toolbar) findViewById(R.id.domain_settings_toolbar);
53 setSupportActionBar(domainSettingsAppBar);
55 // Display the home arrow on `appBar`.
56 final ActionBar appBar = getSupportActionBar();
57 assert appBar != null; // This assert removes the incorrect lint warning in Android Studio on the following line that `appBar` might be `null`.
58 appBar.setDisplayHomeAsUpEnabled(true);
60 // Get the intent that started the activity.
61 final Intent launchingIntent = getIntent();
63 // Extract the `databaseID`. The default value is `1`.
64 databaseId = launchingIntent.getIntExtra(DomainSettingsFragment.DATABASE_ID, 1);
66 // Store `databaseId` in `argumentsBundle`.
67 Bundle argumentsBundle = new Bundle();
68 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
70 // Add `argumentsBundle` to `domainSettingsFragment`.
71 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
72 domainSettingsFragment.setArguments(argumentsBundle);
74 // Display `domainSettingsFragment`.
75 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
79 public boolean onCreateOptionsMenu(Menu menu) {
81 getMenuInflater().inflate(R.menu.domains_options_menu, menu);
88 public boolean onOptionsItemSelected(MenuItem menuItem) {
89 // Get the ID of the `MenuItem` that was selected.
90 int menuItemID = menuItem.getItemId();
92 // Initialize the database handler. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
93 // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
94 final DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getApplicationContext(), null, null, 0);
97 case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
99 NavUtils.navigateUpFromSameTask(this);
102 case R.id.save_domain:
103 // Get handles for the domain settings.
104 EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
105 Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
106 Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
107 Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
108 Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
109 Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
110 Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
111 EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
112 Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
113 Spinner displayWebpageImagesSpinner = (Spinner) findViewById(R.id.domain_settings_display_webpage_images_spinner);
115 // Extract the data for the domain settings.
116 String domainNameString = domainNameEditText.getText().toString();
117 boolean javaScriptEnabledBoolean = javaScriptEnabledSwitch.isChecked();
118 boolean firstPartyCookiesEnabledBoolean = firstPartyCookiesEnabledSwitch.isChecked();
119 boolean thirdPartyCookiesEnabledBoolean = thirdPartyCookiesEnabledSwitch.isChecked();
120 boolean domStorageEnabledEnabledBoolean = domStorageEnabledSwitch.isChecked();
121 boolean formDataEnabledBoolean = formDataEnabledSwitch.isChecked();
122 int userAgentPositionInt = userAgentSpinner.getSelectedItemPosition();
123 int fontSizePositionInt = fontSizeSpinner.getSelectedItemPosition();
124 int displayWebpageImagesInt = displayWebpageImagesSpinner.getSelectedItemPosition();
126 // Get the data for the `Spinners` from the entry values string arrays.
127 String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPositionInt];
128 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePositionInt]);
130 // Check to see if we are using a custom user agent.
131 if (userAgentString.equals("Custom user agent")) {
132 // Set `userAgentString` to the custom user agent string.
133 userAgentString = customUserAgentEditText.getText().toString();
136 // Save the domain settings.
137 domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabledBoolean, firstPartyCookiesEnabledBoolean, thirdPartyCookiesEnabledBoolean, domStorageEnabledEnabledBoolean, formDataEnabledBoolean, userAgentString, fontSizeInt,
138 displayWebpageImagesInt);
140 // Navigate to `DomainsActivity`.
141 NavUtils.navigateUpFromSameTask(this);
144 case R.id.delete_domain:
145 // Get a handle for the current activity.
146 final Activity activity = this;
148 // Get a handle for `domain_settings_coordinatorlayout` so we can display a `SnackBar` later.
149 CoordinatorLayout domainSettingsCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.domain_settings_coordinatorlayout);
151 // Detach `domain_settings_scrollview`.
152 getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
154 Snackbar.make(domainSettingsCoordinatorLayout, R.string.domain_deleted, Snackbar.LENGTH_SHORT)
155 .setAction(R.string.undo, new View.OnClickListener() {
157 public void onClick(View v) {
158 // Do nothing because everything will be handled by `onDismiss` below.
161 .addCallback(new Snackbar.Callback() {
163 public void onDismissed(Snackbar snackbar, int event) {
165 // The user pushed the `undo` button.
166 case Snackbar.Callback.DISMISS_EVENT_ACTION:
167 // Store `databaseId` in `argumentsBundle`.
168 Bundle argumentsBundle = new Bundle();
169 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
171 // Add `argumentsBundle` to `domainSettingsFragment`.
172 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
173 domainSettingsFragment.setArguments(argumentsBundle);
175 // Display `domainSettingsFragment`.
176 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
179 // The `Snackbar` was dismissed without the `Undo` button being pushed.
181 // Delete the selected domain.
182 domainsDatabaseHelper.deleteDomain(databaseId);
184 // Navigate to `DomainsActivity`.
185 NavUtils.navigateUpFromSameTask(activity);