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);
79 // Show the `MenuItems`.
80 menu.findItem(R.id.save_domain).setVisible(true);
81 menu.findItem(R.id.delete_domain).setVisible(true);
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 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);
114 // Extract the data for the domain settings.
115 String domainNameString = domainNameEditText.getText().toString();
116 boolean javaScriptEnabled = javaScriptEnabledSwitch.isChecked();
117 boolean firstPartyCookiesEnabled = firstPartyCookiesEnabledSwitch.isChecked();
118 boolean thirdPartyCookiesEnabled = thirdPartyCookiesEnabledSwitch.isChecked();
119 boolean domStorageEnabledEnabled = domStorageEnabledSwitch.isChecked();
120 boolean formDataEnabled = formDataEnabledSwitch.isChecked();
121 int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
122 int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
124 // Get the data for the `Spinners` from the entry values string arrays.
125 String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPosition];
126 int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePosition]);
128 // Check to see if we are using a custom user agent.
129 if (userAgentString.equals("Custom user agent")) {
130 // Set `userAgentString` to the custom user agent string.
131 userAgentString = customUserAgentEditText.getText().toString();
134 // Save the domain settings.
135 domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
137 // Navigate to `DomainsActivity`.
138 NavUtils.navigateUpFromSameTask(this);
141 case R.id.delete_domain:
142 // Delete the selected domain.
143 domainsDatabaseHelper.deleteDomain(databaseId);
145 // Navigate to `DomainsActivity`.
146 NavUtils.navigateUpFromSameTask(this);