]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/activities/RequestsActivity.java
0b9be790da9b05abe8e66b06877988cc8811eb8d
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / RequestsActivity.java
1 /*
2  * Copyright © 2018-2020 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.content.SharedPreferences;
25 import android.database.Cursor;
26 import android.database.MatrixCursor;
27 import android.os.Bundle;
28 import android.preference.PreferenceManager;
29 import android.view.View;
30 import android.view.WindowManager;
31 import android.widget.AdapterView;
32 import android.widget.ArrayAdapter;
33 import android.widget.ListView;
34 import android.widget.ResourceCursorAdapter;
35 import android.widget.Spinner;
36 import android.widget.TextView;
37
38 import androidx.annotation.NonNull;
39 import androidx.appcompat.app.ActionBar;
40 import androidx.appcompat.app.AppCompatActivity;
41 import androidx.appcompat.widget.Toolbar;
42 import androidx.fragment.app.DialogFragment;
43
44 import com.stoutner.privacybrowser.R;
45 import com.stoutner.privacybrowser.adapters.RequestsArrayAdapter;
46 import com.stoutner.privacybrowser.dialogs.ViewRequestDialog;
47 import com.stoutner.privacybrowser.helpers.BlocklistHelper;
48
49 import java.util.ArrayList;
50 import java.util.List;
51
52 public class RequestsActivity extends AppCompatActivity implements ViewRequestDialog.ViewRequestListener {
53     // The resource requests are populated by `MainWebViewActivity` before `RequestsActivity` is launched.
54     public static List<String[]> resourceRequests;
55
56     // Initialize the class constants.
57     private final String LISTVIEW_POSITION = "listview_position";
58
59     // Define the class views.
60     private ListView requestsListView;
61
62     @Override
63     public void onCreate(Bundle savedInstanceState) {
64         // Get a handle for the shared preferences.
65         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
66
67         // Get the screenshot preference.
68         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
69
70         // Disable screenshots if not allowed.
71         if (!allowScreenshots) {
72             getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
73         }
74
75         // Set the theme.
76         setTheme(R.style.PrivacyBrowser);
77
78         // Run the default commands.
79         super.onCreate(savedInstanceState);
80
81         // Get the launching intent
82         Intent intent = getIntent();
83
84         // Get the status of the third-party blocklist.
85         boolean blockAllThirdPartyRequests = intent.getBooleanExtra("block_all_third_party_requests", false);
86
87         // Set the content view.
88         setContentView(R.layout.requests_coordinatorlayout);
89
90         // Use the AndroidX toolbar until the minimum API is >= 21.
91         Toolbar toolbar = findViewById(R.id.requests_toolbar);
92         setSupportActionBar(toolbar);
93
94         // Get a handle for the app bar and the list view.
95         ActionBar appBar = getSupportActionBar();
96         requestsListView = findViewById(R.id.requests_listview);
97
98         // Remove the incorrect lint warning that `appBar` might be null.
99         assert appBar != null;
100
101         // Display the spinner and the back arrow in the app bar.
102         appBar.setCustomView(R.layout.spinner);
103         appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
104
105         // Initialize the resource array lists.  A list is needed for all the resource requests, or the activity can crash if `MainWebViewActivity.resourceRequests` is modified after the activity loads.
106         List<String[]> allResourceRequests = new ArrayList<>();
107         List<String[]> defaultResourceRequests = new ArrayList<>();
108         List<String[]> allowedResourceRequests = new ArrayList<>();
109         List<String[]> thirdPartyResourceRequests = new ArrayList<>();
110         List<String[]> blockedResourceRequests = new ArrayList<>();
111
112         // Populate the resource array lists.
113         for (String[] request : resourceRequests) {
114             switch (request[BlocklistHelper.REQUEST_DISPOSITION]) {
115                 case BlocklistHelper.REQUEST_DEFAULT:
116                     // Add the request to the list of all requests.
117                     allResourceRequests.add(request);
118
119                     // Add the request to the list of default requests.
120                     defaultResourceRequests.add(request);
121                     break;
122
123                 case BlocklistHelper.REQUEST_ALLOWED:
124                     // Add the request to the list of all requests.
125                     allResourceRequests.add(request);
126
127                     // Add the request to the list of allowed requests.
128                     allowedResourceRequests.add(request);
129                     break;
130
131                 case BlocklistHelper.REQUEST_THIRD_PARTY:
132                     // Add the request to the list of all requests.
133                     allResourceRequests.add(request);
134
135                     // Add the request to the list of third-party requests.
136                     thirdPartyResourceRequests.add(request);
137                     break;
138
139                 case BlocklistHelper.REQUEST_BLOCKED:
140                     // Add the request to the list of all requests.
141                     allResourceRequests.add(request);
142
143                     // Add the request to the list of blocked requests.
144                     blockedResourceRequests.add(request);
145                     break;
146             }
147         }
148
149         // Setup a matrix cursor for the resource lists.
150         MatrixCursor spinnerCursor = new MatrixCursor(new String[]{"_id", "Requests"});
151         spinnerCursor.addRow(new Object[]{0, getString(R.string.all) + " - " + allResourceRequests.size()});
152         spinnerCursor.addRow(new Object[]{1, getString(R.string.default_label) + " - " + defaultResourceRequests.size()});
153         spinnerCursor.addRow(new Object[]{2, getString(R.string.allowed_plural) + " - " + allowedResourceRequests.size()});
154         if (blockAllThirdPartyRequests) {
155             spinnerCursor.addRow(new Object[]{3, getString(R.string.third_party_plural) + " - " + thirdPartyResourceRequests.size()});
156         }
157         spinnerCursor.addRow(new Object[]{4, getString(R.string.blocked_plural) + " - " + blockedResourceRequests.size()});
158
159         // Create a resource cursor adapter for the spinner.
160         ResourceCursorAdapter spinnerCursorAdapter = new ResourceCursorAdapter(this, R.layout.requests_appbar_spinner_item, spinnerCursor, 0) {
161             @Override
162             public void bindView(View view, Context context, Cursor cursor) {
163                 // Get a handle for the spinner item text view.
164                 TextView spinnerItemTextView = view.findViewById(R.id.spinner_item_textview);
165
166                 // Set the text view to display the resource list.
167                 spinnerItemTextView.setText(cursor.getString(1));
168             }
169         };
170
171         // Set the resource cursor adapter drop down view resource.
172         spinnerCursorAdapter.setDropDownViewResource(R.layout.requests_appbar_spinner_dropdown_item);
173
174         // Get a handle for the app bar spinner and set the adapter.
175         Spinner appBarSpinner = findViewById(R.id.spinner);
176         appBarSpinner.setAdapter(spinnerCursorAdapter);
177
178         // Get a handle for the context.
179         Context context = this;
180
181         // Handle clicks on the spinner dropdown.
182         appBarSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
183             @Override
184             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
185                 switch ((int) id) {
186                     case 0:  // All requests.
187                         // Get an adapter for all the request.
188                         ArrayAdapter<String[]> allResourceRequestsArrayAdapter = new RequestsArrayAdapter(context, allResourceRequests);
189
190                         // Display the adapter in the list view.
191                         requestsListView.setAdapter(allResourceRequestsArrayAdapter);
192                         break;
193
194                     case 1:  // Default requests.
195                         // Get an adapter for the default requests.
196                         ArrayAdapter<String[]> defaultResourceRequestsArrayAdapter = new RequestsArrayAdapter(context, defaultResourceRequests);
197
198                         // Display the adapter in the list view.
199                         requestsListView.setAdapter(defaultResourceRequestsArrayAdapter);
200                         break;
201
202                     case 2:  // Allowed requests.
203                         // Get an adapter for the allowed requests.
204                         ArrayAdapter<String[]> allowedResourceRequestsArrayAdapter = new RequestsArrayAdapter(context, allowedResourceRequests);
205
206                         // Display the adapter in the list view.
207                         requestsListView.setAdapter(allowedResourceRequestsArrayAdapter);
208                         break;
209
210                     case 3:  // Third-party requests.
211                         // Get an adapter for the third-party requests.
212                         ArrayAdapter<String[]> thirdPartyResourceRequestsArrayAdapter = new RequestsArrayAdapter(context, thirdPartyResourceRequests);
213
214                         //Display the adapter in the list view.
215                         requestsListView.setAdapter(thirdPartyResourceRequestsArrayAdapter);
216                         break;
217
218                     case 4:  // Blocked requests.
219                         // Get an adapter fo the blocked requests.
220                         ArrayAdapter<String[]> blockedResourceRequestsArrayAdapter = new RequestsArrayAdapter(context, blockedResourceRequests);
221
222                         // Display the adapter in the list view.
223                         requestsListView.setAdapter(blockedResourceRequestsArrayAdapter);
224                         break;
225                 }
226             }
227
228             @Override
229             public void onNothingSelected(AdapterView<?> parent) {
230                 // Do nothing.
231             }
232         });
233
234         // Create an array adapter with the list of the resource requests.
235         ArrayAdapter<String[]> resourceRequestsArrayAdapter = new RequestsArrayAdapter(context, allResourceRequests);
236
237         // Populate the list view with the resource requests adapter.
238         requestsListView.setAdapter(resourceRequestsArrayAdapter);
239
240         // Listen for taps on entries in the list view.
241         requestsListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
242             // Display the view request dialog.  The list view is 0 based, so the position must be incremented by 1.
243             launchViewRequestDialog(position + 1);
244         });
245
246         // Check to see if the activity has been restarted.
247         if (savedInstanceState != null) {
248             // Scroll to the saved position.
249             requestsListView.post(() -> requestsListView.setSelection(savedInstanceState.getInt(LISTVIEW_POSITION)));
250         }
251     }
252
253     @Override
254     public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
255         // Run the default commands.
256         super.onSaveInstanceState(savedInstanceState);
257
258         // Get the listview position.
259         int listViewPosition = requestsListView.getFirstVisiblePosition();
260
261         // Store the listview position in the bundle.
262         savedInstanceState.putInt(LISTVIEW_POSITION, listViewPosition);
263     }
264
265     @Override
266     public void onPrevious(int id) {
267         // Show the previous dialog.
268         launchViewRequestDialog(id -1);
269     }
270
271     @Override
272     public void onNext(int id) {
273         // Show the next dialog.
274         launchViewRequestDialog(id + 1);
275     }
276
277     private void launchViewRequestDialog(int id) {
278         // Determine if this is the last request in the list.
279         boolean isLastRequest = (id == requestsListView.getCount());
280
281         // Get the string array for the selected resource request.  The resource requests list view is zero based.
282         String[] selectedRequestStringArray = (String[]) requestsListView.getItemAtPosition(id - 1);
283
284         // Remove the warning that `selectedRequest` might be null.
285         assert selectedRequestStringArray != null;
286
287         // Show the request detail dialog.
288         DialogFragment viewRequestDialogFragment = ViewRequestDialog.request(id, isLastRequest, selectedRequestStringArray);
289         viewRequestDialogFragment.show(getSupportFragmentManager(), getString(R.string.request_details));
290     }
291 }