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.database.Cursor;
24 import android.os.Bundle;
25 import android.support.design.widget.FloatingActionButton;
26 import android.support.v7.app.ActionBar;
27 import android.support.v7.app.AppCompatActivity;
28 import android.support.v7.app.AppCompatDialogFragment;
29 import android.support.v7.widget.Toolbar;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.AdapterView;
33 import android.widget.CursorAdapter;
34 import android.widget.EditText;
35 import android.widget.ListView;
36 import android.widget.TextView;
38 import com.stoutner.privacybrowser.R;
39 import com.stoutner.privacybrowser.dialogs.AddDomain;
40 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
42 public class DomainsList extends AppCompatActivity implements AddDomain.AddDomainListener {
43 // `domainsDatabaseHelper` is used in `onCreate()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
44 private static DomainsDatabaseHelper domainsDatabaseHelper;
46 // `domainsRecyclerView` is used in `onCreate()` and `updateDomainsListView()`.
47 private ListView domainsListView;
49 private boolean twoPaneMode;
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
54 setContentView(R.layout.domains_list_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_list_framelayout` is only populated if two panes are present.
70 twoPaneMode = ((findViewById(R.id.domains_list_framelayout)) != 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 // Get the database `Cursor` for this ID and move it to the first row.
82 Cursor domainCursor = domainsDatabaseHelper.getCursorForId(databaseId);
83 domainCursor.moveToFirst();
89 FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
90 addDomainFAB.setOnClickListener(new View.OnClickListener() {
92 public void onClick(View view) {
93 // Show the `AddDomain` `AlertDialog` and name the instance `@string/add_domain`.
94 AppCompatDialogFragment addDomainDialog = new AddDomain();
95 addDomainDialog.show(getSupportFragmentManager(), getResources().getString(R.string.add_domain));
99 // Load the `ListView`.
100 updateDomainsListView();
104 public void onAddDomain(AppCompatDialogFragment dialogFragment) {
105 // Get the `domainNameEditText` from `dialogFragment` and extract the string.
106 EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
107 String domainNameString = domainNameEditText.getText().toString();
109 // Create the domain.
110 domainsDatabaseHelper.addDomain(domainNameString);
112 // Refresh the `ListView`.
113 updateDomainsListView();
116 private void updateDomainsListView() {
117 // Get a `Cursor` with the current contents of the domains database.
118 Cursor domainsCursor = domainsDatabaseHelper.getCursorOrderedByDomain();
120 // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`.
121 CursorAdapter domainsCursorAdapter = new CursorAdapter(this, domainsCursor, false) {
123 public View newView(Context context, Cursor cursor, ViewGroup parent) {
124 // Inflate the individual item layout. `false` does not attach it to the root.
125 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
129 public void bindView(View view, Context context, Cursor cursor) {
130 // Set the domain name.
131 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN));
132 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
133 domainNameTextView.setText(domainNameString);
137 // Update the `RecyclerView`.
138 domainsListView.setAdapter(domainsCursorAdapter);