]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/activities/DomainSettingsActivity.java
Add color changes to domain settings icons.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / DomainSettingsActivity.java
1 /*
2  * Copyright 2017 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.activities;
21
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;
33
34 import com.stoutner.privacybrowser.R;
35 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
36 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
37
38 public class DomainSettingsActivity extends AppCompatActivity {
39     // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
40     int databaseId;
41
42     @Override
43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         setContentView(R.layout.domain_settings_coordinatorlayout);
46
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);
50
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);
55
56         // Get the intent that started the activity.
57         final Intent launchingIntent = getIntent();
58
59         // Extract the `databaseID`.  The default value is `1`.
60         databaseId = launchingIntent.getIntExtra(DomainSettingsFragment.DATABASE_ID, 1);
61
62         // Store `databaseId` in `argumentsBundle`.
63         Bundle argumentsBundle = new Bundle();
64         argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
65
66         // Add `argumentsBundle` to `domainSettingsFragment`.
67         DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
68         domainSettingsFragment.setArguments(argumentsBundle);
69
70         // Display `domainSettingsFragment`.
71         getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
72     }
73
74     @Override
75     public boolean onCreateOptionsMenu(Menu menu) {
76         // Inflate the menu.
77         getMenuInflater().inflate(R.menu.domains_options_menu, menu);
78
79         // Success!
80         return true;
81     }
82
83     @Override
84     public boolean onOptionsItemSelected(MenuItem menuItem) {
85         // Get the ID of the `MenuItem` that was selected.
86         int menuItemID = menuItem.getItemId();
87
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);
91
92         switch (menuItemID) {
93             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
94                 // Go home.
95                 NavUtils.navigateUpFromSameTask(this);
96                 break;
97
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);
109
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();
119
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]);
123
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();
128                 }
129
130                 // Save the domain settings.
131                 domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
132
133                 // Navigate to `DomainsActivity`.
134                 NavUtils.navigateUpFromSameTask(this);
135                 break;
136
137             case R.id.delete_domain:
138                 // Delete the selected domain.
139                 domainsDatabaseHelper.deleteDomain(databaseId);
140
141                 // Navigate to `DomainsActivity`.
142                 NavUtils.navigateUpFromSameTask(this);
143                 break;
144         }
145         return true;
146     }
147 }