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.Intent;
23 import android.os.Bundle;
24 import android.support.v4.app.NavUtils;
25 import android.support.v7.app.ActionBar;
26 import android.support.v7.app.AppCompatActivity;
27 import android.support.v7.widget.Toolbar;
28 import android.view.Menu;
29 import android.view.MenuItem;
30 import android.widget.EditText;
31 import android.widget.Spinner;
32 import android.widget.Switch;
34 import com.stoutner.privacybrowser.R;
35 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
36 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
38 public class DomainSettingsActivity extends AppCompatActivity {
39 // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
43 protected void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45 setContentView(R.layout.domain_settings_coordinatorlayout);
47 // We ned to use `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
48 Toolbar domainSettingsAppBar = (Toolbar) findViewById(R.id.domain_settings_toolbar);
49 setSupportActionBar(domainSettingsAppBar);
51 // Display the home arrow on `appBar`.
52 final ActionBar appBar = getSupportActionBar();
53 assert appBar != null; // This assert removes the incorrect lint warning in Android Studio on the following line that `appBar` might be `null`.
54 appBar.setDisplayHomeAsUpEnabled(true);
56 // Get the intent that started the activity.
57 final Intent launchingIntent = getIntent();
59 // Extract the `databaseID`. The default value is `1`.
60 databaseId = launchingIntent.getIntExtra(DomainSettingsFragment.DATABASE_ID, 1);
62 // Store `databaseId` in `argumentsBundle`.
63 Bundle argumentsBundle = new Bundle();
64 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
66 // Add `argumentsBundle` to `domainSettingsFragment`.
67 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
68 domainSettingsFragment.setArguments(argumentsBundle);
70 // Display `domainSettingsFragment`.
71 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
75 public boolean onCreateOptionsMenu(Menu menu) {
77 getMenuInflater().inflate(R.menu.domains_options_menu, menu);
84 public boolean onOptionsItemSelected(MenuItem menuItem) {
85 // Get the ID of the `MenuItem` that was selected.
86 int menuItemID = menuItem.getItemId();
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 domainsDatabaseHelper = new DomainsDatabaseHelper(getApplicationContext(), null, null, 0);
93 case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
95 NavUtils.navigateUpFromSameTask(this);
98 case R.id.save_domain:
99 // Get handles for the domain settings.
100 EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
101 Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
102 Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
103 Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
104 Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
105 Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
106 Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
107 EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
108 Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
110 // Extract the data for the domain settings.
111 String domainNameString = domainNameEditText.getText().toString();
112 boolean javaScriptEnabled = javaScriptEnabledSwitch.isChecked();
113 boolean firstPartyCookiesEnabled = firstPartyCookiesEnabledSwitch.isChecked();
114 boolean thirdPartyCookiesEnabled = thirdPartyCookiesEnabledSwitch.isChecked();
115 boolean domStorageEnabledEnabled = domStorageEnabledSwitch.isChecked();
116 boolean formDataEnabled = formDataEnabledSwitch.isChecked();
117 int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
118 int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
120 // Get the data for the `Spinners` from the entry values string arrays.
121 String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPosition];
122 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePosition]);
124 // Check to see if we are using a custom user agent.
125 if (userAgentString.equals("Custom user agent")) {
126 // Set `userAgentString` to the custom user agent string.
127 userAgentString = customUserAgentEditText.getText().toString();
130 // Save the domain settings.
131 domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
133 // Navigate to `DomainsActivity`.
134 NavUtils.navigateUpFromSameTask(this);
137 case R.id.delete_domain:
138 // Delete the selected domain.
139 domainsDatabaseHelper.deleteDomain(databaseId);
141 // Navigate to `DomainsActivity`.
142 NavUtils.navigateUpFromSameTask(this);