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.Context;
23 import android.content.Intent;
24 import android.database.Cursor;
25 import android.os.Bundle;
26 import android.support.design.widget.FloatingActionButton;
27 import android.support.v7.app.ActionBar;
28 import android.support.v7.app.AppCompatActivity;
29 import android.support.v7.app.AppCompatDialogFragment;
30 import android.support.v7.widget.Toolbar;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.AdapterView;
34 import android.widget.CursorAdapter;
35 import android.widget.EditText;
36 import android.widget.ListView;
37 import android.widget.TextView;
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.dialogs.AddDomainDialog;
41 import com.stoutner.privacybrowser.fragments.DomainSettingsFragment;
42 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
44 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
45 // `domainsDatabaseHelper` is used in `onCreate()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
46 private static DomainsDatabaseHelper domainsDatabaseHelper;
48 // `domainsRecyclerView` is used in `onCreate()` and `updateDomainsListView()`.
49 private ListView domainsListView;
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54 setContentView(R.layout.domains_coordinatorlayout);
56 // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
57 final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.domains_toolbar);
58 setSupportActionBar(bookmarksAppBar);
60 // Display the home arrow on `SupportActionBar`.
61 ActionBar appBar = getSupportActionBar();
62 assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null.
63 appBar.setDisplayHomeAsUpEnabled(true);
65 // Initialize the database handler. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
66 // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
67 domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
69 // Determine if we are in two pane mode. `domains_settings_linearlayout` is only populated if two panes are present.
70 final boolean twoPaneMode = ((findViewById(R.id.domain_settings_linearlayout)) != null);
72 // Initialize `domainsListView`.
73 domainsListView = (ListView) findViewById(R.id.domains_listview);
75 domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
77 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
78 // Convert the id from long to int to match the format of the domains database.
79 int databaseId = (int) id;
81 // Display the Domain Settings.
82 if (twoPaneMode) { // Display a fragment in two paned mode.
83 // Highlight the selected domain.
84 domainsListView.setItemChecked(position, true);
86 // Store `databaseId` in `argumentsBundle`.
87 Bundle argumentsBundle = new Bundle();
88 argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
90 // Add `argumentsBundle` to `domainSettingsFragment`.
91 DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
92 domainSettingsFragment.setArguments(argumentsBundle);
94 // Display `domainSettingsFragment`.
95 getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_linearlayout, domainSettingsFragment).commit();
96 } else { // Load the second activity on smaller screens.
97 // Get a handle for the context.
98 Context context = view.getContext();
100 // Create `domainSettingsActivityIntent` with the `databaseId`.
101 Intent domainSettingsActivityIntent = new Intent(context, DomainSettingsActivity.class);
102 domainSettingsActivityIntent.putExtra(DomainSettingsFragment.DATABASE_ID, databaseId);
104 // Start `DomainSettingsActivity`.
105 context.startActivity(domainSettingsActivityIntent);
110 FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
111 addDomainFAB.setOnClickListener(new View.OnClickListener() {
113 public void onClick(View view) {
114 // Show the `AddDomainDialog` `AlertDialog` and name the instance `@string/add_domain`.
115 AppCompatDialogFragment addDomainDialog = new AddDomainDialog();
116 addDomainDialog.show(getSupportFragmentManager(), getResources().getString(R.string.add_domain));
120 // Load the `ListView`.
121 updateDomainsListView();
125 public void onAddDomain(AppCompatDialogFragment dialogFragment) {
126 // Get the `domainNameEditText` from `dialogFragment` and extract the string.
127 EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
128 String domainNameString = domainNameEditText.getText().toString();
130 // Create the domain.
131 domainsDatabaseHelper.addDomain(domainNameString);
133 // Refresh the `ListView`.
134 updateDomainsListView();
137 private void updateDomainsListView() {
138 // Get a `Cursor` with the current contents of the domains database.
139 Cursor domainsCursor = domainsDatabaseHelper.getCursorOrderedByDomain();
141 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
142 CursorAdapter domainsCursorAdapter = new CursorAdapter(this, domainsCursor, false) {
144 public View newView(Context context, Cursor cursor, ViewGroup parent) {
145 // Inflate the individual item layout. `false` does not attach it to the root.
146 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
150 public void bindView(View view, Context context, Cursor cursor) {
151 // Set the domain name.
152 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN));
153 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
154 domainNameTextView.setText(domainNameString);
158 // Update the `RecyclerView`.
159 domainsListView.setAdapter(domainsCursorAdapter);