2 * Copyright © 2018 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.database.MatrixCursor;
25 import android.os.Bundle;
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.WindowManager;
32 import android.widget.AdapterView;
33 import android.widget.ArrayAdapter;
34 import android.widget.ListView;
35 import android.widget.ResourceCursorAdapter;
36 import android.widget.Spinner;
37 import android.widget.TextView;
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.adapters.RequestsArrayAdapter;
41 import com.stoutner.privacybrowser.dialogs.ViewRequestDialog;
43 import java.util.ArrayList;
44 import java.util.List;
46 public class RequestsActivity extends AppCompatActivity implements ViewRequestDialog.ViewRequestListener {
47 // The list view is used in `onCreate()` and `launchViewRequestDialog()`.
48 private ListView requestsListView;
51 public void onCreate(Bundle savedInstanceState) {
52 // Disable screenshots if not allowed.
53 if (!MainWebViewActivity.allowScreenshots) {
54 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
57 // Set the activity theme.
58 if (MainWebViewActivity.darkTheme) {
59 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
61 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
64 // Run the default commands.
65 super.onCreate(savedInstanceState);
67 // Set the content view.
68 setContentView(R.layout.requests_coordinatorlayout);
70 // Use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
71 Toolbar requestsAppBar = findViewById(R.id.requests_toolbar);
72 setSupportActionBar(requestsAppBar);
74 // Get a handle for the app bar and the list view.
75 ActionBar appBar = getSupportActionBar();
76 requestsListView = findViewById(R.id.requests_listview);
78 // Remove the incorrect lint warning that `appBar` might be null.
79 assert appBar != null;
81 // Display the spinner and the back arrow in the app bar.
82 appBar.setCustomView(R.layout.requests_spinner);
83 appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
85 // 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.
86 List<String[]> allResourceRequests = new ArrayList<>();
87 List<String[]> defaultResourceRequests = new ArrayList<>();
88 List<String[]> allowedResourceRequests = new ArrayList<>();
89 List<String[]> thirdPartyResourceRequests = new ArrayList<>();
90 List<String[]> blockedResourceRequests = new ArrayList<>();
92 // Populate the resource array lists.
93 for (String[] request : MainWebViewActivity.resourceRequests) {
94 switch (Integer.valueOf(request[MainWebViewActivity.REQUEST_DISPOSITION])) {
95 case MainWebViewActivity.REQUEST_DEFAULT:
96 // Add the request to the list of all requests.
97 allResourceRequests.add(request);
99 // Add the request to the list of default requests.
100 defaultResourceRequests.add(request);
103 case MainWebViewActivity.REQUEST_ALLOWED:
104 // Add the request to the list of all requests.
105 allResourceRequests.add(request);
107 // Add the request to the list of allowed requests.
108 allowedResourceRequests.add(request);
111 case MainWebViewActivity.REQUEST_THIRD_PARTY:
112 // Add the request to the list of all requests.
113 allResourceRequests.add(request);
115 // Add the request to the list of third-party requests.
116 thirdPartyResourceRequests.add(request);
119 case MainWebViewActivity.REQUEST_BLOCKED:
120 // Add the request to the list of all requests.
121 allResourceRequests.add(request);
123 // Add the request to the list of blocked requests.
124 blockedResourceRequests.add(request);
129 // Setup a matrix cursor for the resource lists.
130 MatrixCursor spinnerCursor = new MatrixCursor(new String[]{"_id", "Requests"});
131 spinnerCursor.addRow(new Object[]{0, getString(R.string.all) + " - " + allResourceRequests.size()});
132 spinnerCursor.addRow(new Object[]{1, getString(R.string.default_label) + " - " + defaultResourceRequests.size()});
133 spinnerCursor.addRow(new Object[]{2, getString(R.string.allowed_plural) + " - " + allowedResourceRequests.size()});
134 if (MainWebViewActivity.blockAllThirdPartyRequests) {
135 spinnerCursor.addRow(new Object[]{3, getString(R.string.third_party_plural) + " - " + thirdPartyResourceRequests.size()});
137 spinnerCursor.addRow(new Object[]{4, getString(R.string.blocked_plural) + " - " + blockedResourceRequests.size()});
139 // Create a resource cursor adapter for the spinner.
140 ResourceCursorAdapter spinnerCursorAdapter = new ResourceCursorAdapter(this, R.layout.requests_spinner_item, spinnerCursor, 0) {
142 public void bindView(View view, Context context, Cursor cursor) {
143 // Get a handle for the spinner item text view.
144 TextView spinnerItemTextView = view.findViewById(R.id.spinner_item_textview);
146 // Set the text view to display the resource list.
147 spinnerItemTextView.setText(cursor.getString(1));
151 // Set the resource cursor adapter drop down view resource.
152 spinnerCursorAdapter.setDropDownViewResource(R.layout.requests_spinner_dropdown_item);
154 // Get a handle for the app bar spinner and set the adapter.
155 Spinner appBarSpinner = findViewById(R.id.requests_spinner);
156 appBarSpinner.setAdapter(spinnerCursorAdapter);
158 // Handle clicks on the spinner dropdown.
159 appBarSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
161 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
163 case 0: // All requests.
164 // Get an adapter for all the request.
165 ArrayAdapter<String[]> allResourceRequestsArrayAdapter = new RequestsArrayAdapter(getApplicationContext(), allResourceRequests);
167 // Display the adapter in the list view.
168 requestsListView.setAdapter(allResourceRequestsArrayAdapter);
171 case 1: // Default requests.
172 // Get an adapter for the default requests.
173 ArrayAdapter<String[]> defaultResourceRequestsArrayAdapter = new RequestsArrayAdapter(getApplicationContext(), defaultResourceRequests);
175 // Display the adapter in the list view.
176 requestsListView.setAdapter(defaultResourceRequestsArrayAdapter);
179 case 2: // Allowed requests.
180 // Get an adapter for the allowed requests.
181 ArrayAdapter<String[]> allowedResourceRequestsArrayAdapter = new RequestsArrayAdapter(getApplicationContext(), allowedResourceRequests);
183 // Display the adapter in the list view.
184 requestsListView.setAdapter(allowedResourceRequestsArrayAdapter);
187 case 3: // Third-party requests.
188 // Get an adapter for the third-party requests.
189 ArrayAdapter<String[]> thirdPartyResourceRequestsArrayAdapter = new RequestsArrayAdapter(getApplicationContext(), thirdPartyResourceRequests);
191 //Display the adapter in the list view.
192 requestsListView.setAdapter(thirdPartyResourceRequestsArrayAdapter);
195 case 4: // Blocked requests.
196 // Get an adapter fo the blocked requests.
197 ArrayAdapter<String[]> blockedResourceRequestsArrayAdapter = new RequestsArrayAdapter(getApplicationContext(), blockedResourceRequests);
199 // Display the adapter in the list view.
200 requestsListView.setAdapter(blockedResourceRequestsArrayAdapter);
206 public void onNothingSelected(AdapterView<?> parent) {
211 // Create an array adapter with the list of the resource requests.
212 ArrayAdapter<String[]> resourceRequestsArrayAdapter = new RequestsArrayAdapter(getApplicationContext(), allResourceRequests);
214 // Populate the list view with the resource requests adapter.
215 requestsListView.setAdapter(resourceRequestsArrayAdapter);
217 // Listen for taps on entries in the list view.
218 requestsListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
219 // Display the view request dialog. The list view is 0 based, so the position must be incremented by 1.
220 launchViewRequestDialog(position + 1);
225 public void onPrevious(int id) {
226 // Show the previous dialog.
227 launchViewRequestDialog(id -1);
231 public void onNext(int id) {
232 // Show the next dialog.
233 launchViewRequestDialog(id + 1);
236 private void launchViewRequestDialog(int id) {
237 // Determine if this is the last request in the list.
238 boolean isLastRequest = (id == requestsListView.getCount());
240 // Get the string array for the selected resource request. The resource requests list view is zero based.
241 String[] selectedRequestStringArray = (String[]) requestsListView.getItemAtPosition(id - 1);
243 // Remove the warning that `selectedRequest` might be null.
244 assert selectedRequestStringArray != null;
246 // Show the request detail dialog.
247 AppCompatDialogFragment viewRequestDialogFragment = ViewRequestDialog.request(id, isLastRequest, selectedRequestStringArray);
248 viewRequestDialogFragment.show(getSupportFragmentManager(), getString(R.string.request_details));