]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java
Add color changes to domain settings icons.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / DomainsActivity.java
index 7cbe8d053b2007279e39259b48f5b2b8fb8816d9..158fd16a6384a794f8d5e0b325c83a7061b81116 100644 (file)
@@ -24,16 +24,21 @@ import android.content.Intent;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
+import android.support.v4.app.NavUtils;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.app.AppCompatDialogFragment;
 import android.support.v7.widget.Toolbar;
+import android.view.Menu;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.CursorAdapter;
 import android.widget.EditText;
 import android.widget.ListView;
+import android.widget.Spinner;
+import android.widget.Switch;
 import android.widget.TextView;
 
 import com.stoutner.privacybrowser.R;
@@ -42,17 +47,35 @@ import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
 
 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
-    // `domainsDatabaseHelper` is used in `onCreate()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
+    // `context` is used in `onCreate()` and `onOptionsItemSelected()`.
+    Context context;
+
+    // `domainsDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
     private static DomainsDatabaseHelper domainsDatabaseHelper;
 
+    // `twoPaneMode` is used in `onCreate()` and `updateDomainsListView()`.
+    private boolean twoPaneMode;
+
     // `domainsRecyclerView` is used in `onCreate()` and `updateDomainsListView()`.
     private ListView domainsListView;
 
+    // `databaseId` is used in `onCreate()` and `onOptionsItemSelected()`.
+    private int databaseId;
+
+    // `saveMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
+    private MenuItem saveMenuItem;
+
+    // `deleteMenuItem` is used in `onCreate()`, `onOptionsItemSelected()`, and `onCreateOptionsMenu()`.
+    private MenuItem deleteMenuItem;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.domains_coordinatorlayout);
 
+        // Get a handle for the context.
+        context = this;
+
         // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
         final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.domains_toolbar);
         setSupportActionBar(bookmarksAppBar);
@@ -67,7 +90,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
 
         // Determine if we are in two pane mode.  `domains_settings_linearlayout` is only populated if two panes are present.
-        final boolean twoPaneMode = ((findViewById(R.id.domain_settings_scrollview)) != null);
+        twoPaneMode = ((findViewById(R.id.domain_settings_scrollview)) != null);
 
         // Initialize `domainsListView`.
         domainsListView = (ListView) findViewById(R.id.domains_listview);
@@ -75,11 +98,15 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
         domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-                // Convert the id from long to int to match the format of the domains database.
-                int databaseId = (int) id;
+                // Convert the id from `long` to `int` to match the format of the domains database.
+                databaseId = (int) id;
 
                 // Display the Domain Settings.
                 if (twoPaneMode) {  // Display a fragment in two paned mode.
+                    // Enable the options `MenuItems`.
+                    saveMenuItem.setEnabled(true);
+                    deleteMenuItem.setEnabled(true);
+
                     // Store `databaseId` in `argumentsBundle`.
                     Bundle argumentsBundle = new Bundle();
                     argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
@@ -91,9 +118,6 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                     // Display `domainSettingsFragment`.
                     getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
                 } else { // Load the second activity on smaller screens.
-                    // Get a handle for the context.
-                    Context context = view.getContext();
-
                     // Create `domainSettingsActivityIntent` with the `databaseId`.
                     Intent domainSettingsActivityIntent = new Intent(context, DomainSettingsActivity.class);
                     domainSettingsActivityIntent.putExtra(DomainSettingsFragment.DATABASE_ID, databaseId);
@@ -113,9 +137,87 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
                 addDomainDialog.show(getSupportFragmentManager(), getResources().getString(R.string.add_domain));
             }
         });
+    }
 
-        // Load the `ListView`.
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        // Inflate the menu.
+        getMenuInflater().inflate(R.menu.domains_options_menu, menu);
+
+        // Store the `MenuItems` for future use.
+        deleteMenuItem = menu.findItem(R.id.delete_domain);
+        saveMenuItem = menu.findItem(R.id.save_domain);
+
+        // Only display the options `MenuItems` in two pane mode.
+        deleteMenuItem.setVisible(twoPaneMode);
+        saveMenuItem.setVisible(twoPaneMode);
+
+        // Load the `ListView`.  We have to do this from `onCreateOptionsMenu()` instead of `onCreate()` because `updateDomainsListView()` needs the `MenuItems` to be inflated.
         updateDomainsListView();
+
+        // Success!
+        return true;
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem menuItem) {
+        // Get the ID of the `MenuItem` that was selected.
+        int menuItemID = menuItem.getItemId();
+
+        switch (menuItemID) {
+            case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
+                // Go home.
+                NavUtils.navigateUpFromSameTask(this);
+                break;
+
+            case R.id.save_domain:
+                // Get handles for the domain settings.
+                EditText domainNameEditText = (EditText) findViewById(R.id.domain_settings_name_edittext);
+                Switch javaScriptEnabledSwitch = (Switch) findViewById(R.id.domain_settings_javascript_switch);
+                Switch firstPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_first_party_cookies_switch);
+                Switch thirdPartyCookiesEnabledSwitch = (Switch) findViewById(R.id.domain_settings_third_party_cookies_switch);
+                Switch domStorageEnabledSwitch = (Switch) findViewById(R.id.domain_settings_dom_storage_switch);
+                Switch formDataEnabledSwitch = (Switch) findViewById(R.id.domain_settings_form_data_switch);
+                Spinner userAgentSpinner = (Spinner) findViewById(R.id.domain_settings_user_agent_spinner);
+                EditText customUserAgentEditText = (EditText) findViewById(R.id.domain_settings_custom_user_agent_edittext);
+                Spinner fontSizeSpinner = (Spinner) findViewById(R.id.domain_settings_font_size_spinner);
+
+                // Extract the data for the domain settings.
+                String domainNameString = domainNameEditText.getText().toString();
+                boolean javaScriptEnabled = javaScriptEnabledSwitch.isChecked();
+                boolean firstPartyCookiesEnabled = firstPartyCookiesEnabledSwitch.isChecked();
+                boolean thirdPartyCookiesEnabled = thirdPartyCookiesEnabledSwitch.isChecked();
+                boolean domStorageEnabledEnabled = domStorageEnabledSwitch.isChecked();
+                boolean formDataEnabled = formDataEnabledSwitch.isChecked();
+                int userAgentPosition = userAgentSpinner.getSelectedItemPosition();
+                int fontSizePosition = fontSizeSpinner.getSelectedItemPosition();
+
+                // Get the data for the `Spinners` from the entry values string arrays.
+                String userAgentString = getResources().getStringArray(R.array.user_agent_entry_values)[userAgentPosition];
+                int fontSizeInt = Integer.parseInt(getResources().getStringArray(R.array.default_font_size_entry_values)[fontSizePosition]);
+
+                // Check to see if we are using a custom user agent.
+                if (userAgentString.equals("Custom user agent")) {
+                    // Set `userAgentString` to the custom user agent string.
+                    userAgentString = customUserAgentEditText.getText().toString();
+                }
+
+                // Save the domain settings.
+                domainsDatabaseHelper.saveDomain(databaseId, domainNameString, javaScriptEnabled, firstPartyCookiesEnabled, thirdPartyCookiesEnabled, domStorageEnabledEnabled, formDataEnabled, userAgentString, fontSizeInt);
+                break;
+
+            case R.id.delete_domain:
+                // Delete the selected domain.
+                domainsDatabaseHelper.deleteDomain(databaseId);
+
+                // Detach the domain settings fragment.
+                getSupportFragmentManager().beginTransaction().detach(getSupportFragmentManager().findFragmentById(R.id.domain_settings_scrollview)).commit();
+
+                // Update the `ListView`.
+                updateDomainsListView();
+                break;
+        }
+        return true;
     }
 
     @Override
@@ -146,13 +248,44 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo
             @Override
             public void bindView(View view, Context context, Cursor cursor) {
                 // Set the domain name.
-                String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN));
+                String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME));
                 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
                 domainNameTextView.setText(domainNameString);
             }
         };
 
-        // Update the `RecyclerView`.
+        // Update the `ListView`.
         domainsListView.setAdapter(domainsCursorAdapter);
+
+        // Display the domain settings in the second pane if operating in two pane mode and the database contains at least one domain.
+        if (twoPaneMode && (domainsCursor.getCount() > 0)) {
+            // Select the first domain.
+            domainsListView.setItemChecked(0, true);
+
+            // Get the `databaseId` of the first item.
+            domainsCursor.moveToFirst();
+            databaseId = domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper._ID));
+
+            // Store `databaseId` in `argumentsBundle`.
+            Bundle argumentsBundle = new Bundle();
+            argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
+
+            // Add `argumentsBundle` to `domainSettingsFragment`.
+            DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
+            domainSettingsFragment.setArguments(argumentsBundle);
+
+            // Display `domainSettingsFragment`.
+            getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
+
+            // Enable the options `MenuItems`.
+            deleteMenuItem.setEnabled(true);
+            deleteMenuItem.setIcon(R.drawable.delete);
+            saveMenuItem.setEnabled(true);
+        } else {
+            // Disable the options `MenuItems`.
+            deleteMenuItem.setEnabled(false);
+            deleteMenuItem.setIcon(R.drawable.delete_blue);
+            saveMenuItem.setEnabled(false);
+        }
     }
 }
\ No newline at end of file