]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java
Add icons to `DomainSettingsFragment`.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / DomainsActivity.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.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;
38
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;
43
44 public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener {
45     // `domainsDatabaseHelper` is used in `onCreate()`, `onAddDomain()`, and `updateDomainsRecyclerView()`.
46     private static DomainsDatabaseHelper domainsDatabaseHelper;
47
48     // `domainsRecyclerView` is used in `onCreate()` and `updateDomainsListView()`.
49     private ListView domainsListView;
50
51     @Override
52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         setContentView(R.layout.domains_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_settings_linearlayout` is only populated if two panes are present.
70         final boolean twoPaneMode = ((findViewById(R.id.domain_settings_scrollview)) != 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                 // Display the Domain Settings.
82                 if (twoPaneMode) {  // Display a fragment in two paned mode.
83                     // Store `databaseId` in `argumentsBundle`.
84                     Bundle argumentsBundle = new Bundle();
85                     argumentsBundle.putInt(DomainSettingsFragment.DATABASE_ID, databaseId);
86
87                     // Add `argumentsBundle` to `domainSettingsFragment`.
88                     DomainSettingsFragment domainSettingsFragment = new DomainSettingsFragment();
89                     domainSettingsFragment.setArguments(argumentsBundle);
90
91                     // Display `domainSettingsFragment`.
92                     getSupportFragmentManager().beginTransaction().replace(R.id.domain_settings_scrollview, domainSettingsFragment).commit();
93                 } else { // Load the second activity on smaller screens.
94                     // Get a handle for the context.
95                     Context context = view.getContext();
96
97                     // Create `domainSettingsActivityIntent` with the `databaseId`.
98                     Intent domainSettingsActivityIntent = new Intent(context, DomainSettingsActivity.class);
99                     domainSettingsActivityIntent.putExtra(DomainSettingsFragment.DATABASE_ID, databaseId);
100
101                     // Start `DomainSettingsActivity`.
102                     context.startActivity(domainSettingsActivityIntent);
103                 }
104             }
105         });
106
107         FloatingActionButton addDomainFAB = (FloatingActionButton) findViewById(R.id.add_domain_fab);
108         addDomainFAB.setOnClickListener(new View.OnClickListener() {
109             @Override
110             public void onClick(View view) {
111                 // Show the `AddDomainDialog` `AlertDialog` and name the instance `@string/add_domain`.
112                 AppCompatDialogFragment addDomainDialog = new AddDomainDialog();
113                 addDomainDialog.show(getSupportFragmentManager(), getResources().getString(R.string.add_domain));
114             }
115         });
116
117         // Load the `ListView`.
118         updateDomainsListView();
119     }
120
121     @Override
122     public void onAddDomain(AppCompatDialogFragment dialogFragment) {
123         // Get the `domainNameEditText` from `dialogFragment` and extract the string.
124         EditText domainNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
125         String domainNameString = domainNameEditText.getText().toString();
126
127         // Create the domain.
128         domainsDatabaseHelper.addDomain(domainNameString);
129
130         // Refresh the `ListView`.
131         updateDomainsListView();
132     }
133
134     private void updateDomainsListView() {
135         // Get a `Cursor` with the current contents of the domains database.
136         Cursor domainsCursor = domainsDatabaseHelper.getCursorOrderedByDomain();
137
138         // Setup `domainsCursorAdapter` with `this` context.  `false` disables `autoRequery`.
139         CursorAdapter domainsCursorAdapter = new CursorAdapter(this, domainsCursor, false) {
140             @Override
141             public View newView(Context context, Cursor cursor, ViewGroup parent) {
142                 // Inflate the individual item layout.  `false` does not attach it to the root.
143                 return getLayoutInflater().inflate(R.layout.domain_name_linearlayout, parent, false);
144             }
145
146             @Override
147             public void bindView(View view, Context context, Cursor cursor) {
148                 // Set the domain name.
149                 String domainNameString = cursor.getString(cursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN));
150                 TextView domainNameTextView = (TextView) view.findViewById(R.id.domain_name_textview);
151                 domainNameTextView.setText(domainNameString);
152             }
153         };
154
155         // Update the `RecyclerView`.
156         domainsListView.setAdapter(domainsCursorAdapter);
157     }
158 }