]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/activities/DomainsList.java
fb994f41150cdcdf63b6ebda8ef51b77d86b3d17
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / DomainsList.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.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;
37
38 import com.stoutner.privacybrowser.R;
39 import com.stoutner.privacybrowser.dialogs.AddDomain;
40 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
41
42 public class DomainsList extends AppCompatActivity implements AddDomain.AddDomainListener {
43     // `domainsDatabaseHelper` is used in `onCreate()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
44     private static DomainsDatabaseHelper domainsDatabaseHelper;
45
46     // `domainsRecyclerView` is used in `onCreate()` and `updateDomainsListView()`.
47     private ListView domainsListView;
48
49     private boolean twoPaneMode;
50
51     @Override
52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         setContentView(R.layout.domains_list_coordinatorlayout);
55
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);
59
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);
64
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);
68
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);
71
72         // Initialize `domainsListView`.
73         domainsListView = (ListView) findViewById(R.id.domains_listview);
74
75         domainsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
76             @Override
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;
80
81                 // Get the database `Cursor` for this ID and move it to the first row.
82                 Cursor domainCursor = domainsDatabaseHelper.getCursorForId(databaseId);
83                 domainCursor.moveToFirst();
84
85                 // If the
86             }
87         });
88
89         FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
90         addDomainFAB.setOnClickListener(new View.OnClickListener() {
91             @Override
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));
96             }
97         });
98
99         // Load the `ListView`.
100         updateDomainsListView();
101     }
102
103     @Override
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();
108
109         // Create the domain.
110         domainsDatabaseHelper.addDomain(domainNameString);
111
112         // Refresh the `ListView`.
113         updateDomainsListView();
114     }
115
116     private void updateDomainsListView() {
117         // Get a `Cursor` with the current contents of the domains database.
118         Cursor domainsCursor = domainsDatabaseHelper.getCursorOrderedByDomain();
119
120         // Setup `domainsCursorAdapter` with `this` context.  `false` disables `autoRequery`.
121         CursorAdapter domainsCursorAdapter = new CursorAdapter(this, domainsCursor, false) {
122             @Override
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);
126             }
127
128             @Override
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);
134             }
135         };
136
137         // Update the `RecyclerView`.
138         domainsListView.setAdapter(domainsCursorAdapter);
139     }
140 }