]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/adapters/RequestsArrayAdapter.java
Add an option to block all third-party requests. https://redmine.stoutner.com/issues/209
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / adapters / RequestsArrayAdapter.java
1 /*
2  * Copyright © 2018 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.adapters;
21
22 import android.content.Context;
23 import android.support.annotation.NonNull;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ArrayAdapter;
28 import android.widget.LinearLayout;
29 import android.widget.TextView;
30
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
33
34 import java.util.List;
35
36 public class RequestsArrayAdapter extends ArrayAdapter<String[]> {
37     public RequestsArrayAdapter(Context context, List<String[]> resourceRequestsList) {
38         // `super` must be called form the base ArrayAdapter.  `0` is the `textViewResourceId`, which is unused.
39         super(context, 0, resourceRequestsList);
40     }
41
42     @Override
43     @NonNull
44     public View getView(int position, View view, @NonNull ViewGroup parent) {
45         // Get a handle for the context.
46         Context context = getContext();
47
48         // Inflate the view if it is null.
49         if (view == null) {
50             view = LayoutInflater.from(context).inflate(R.layout.requests_item_linearlayout, parent, false);
51         }
52
53         // Get handles for the views.
54         LinearLayout linearLayout = view.findViewById(R.id.request_item_linearlayout);
55         TextView dispositionTextView = view.findViewById(R.id.request_item_disposition);
56         TextView urlTextView = view.findViewById(R.id.request_item_url);
57
58         // Get the string array for this entry.
59         String[] entryStringArray = getItem(position);
60
61         // Remove the lint warning below that `entryStringArray` might be null.
62         assert entryStringArray != null;
63
64         // The ID is one greater than the position because it is 0 based.
65         int id = position + 1;
66
67         // Set the action text and the background color.
68         switch (Integer.valueOf(entryStringArray[0])) {
69             case MainWebViewActivity.REQUEST_DEFAULT:
70                 // Create the disposition string.
71                 String requestDefault = id + ". " + context.getResources().getString(R.string.allowed);
72
73                 // Set the disposition text.
74                 dispositionTextView.setText(requestDefault);
75
76                 // Set the background color.
77                 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.transparent));
78                 break;
79
80             case MainWebViewActivity.REQUEST_ALLOWED:
81                 // Create the disposition string.
82                 String requestAllowed = id + ". " + context.getResources().getString(R.string.allowed);
83
84                 // Set the disposition text.
85                 dispositionTextView.setText(requestAllowed);
86
87                 // Set the background color.
88                 if (MainWebViewActivity.darkTheme) {
89                     linearLayout.setBackgroundColor(context.getResources().getColor(R.color.blue_700_50));
90                 } else {
91                     linearLayout.setBackgroundColor(context.getResources().getColor(R.color.blue_100));
92                 }
93                 break;
94
95             case MainWebViewActivity.REQUEST_THIRD_PARTY:
96                 // Create the disposition string.
97                 String requestThirdParty = id + ". " + context.getResources().getString(R.string.blocked);
98
99                 // Set the disposition text.
100                 dispositionTextView.setText(requestThirdParty);
101
102                 // Set the background color.
103                 if (MainWebViewActivity.darkTheme) {
104                     linearLayout.setBackgroundColor(context.getResources().getColor(R.color.yellow_700_50));
105                 } else {
106                     linearLayout.setBackgroundColor(context.getResources().getColor(R.color.yellow_100));
107                 }
108                 break;
109
110
111             case MainWebViewActivity.REQUEST_BLOCKED:
112                 // Create the disposition string.
113                 String requestBlocked = id + ". " + context.getResources().getString(R.string.blocked);
114
115                 // Set the disposition text.
116                 dispositionTextView.setText(requestBlocked);
117
118                 // Set the background color.
119                 if (MainWebViewActivity.darkTheme) {
120                     linearLayout.setBackgroundColor(context.getResources().getColor(R.color.red_700_40));
121                 } else {
122                     linearLayout.setBackgroundColor(context.getResources().getColor(R.color.red_100));
123                 }
124                 break;
125         }
126
127         // Set the URL text.
128         urlTextView.setText(entryStringArray[1]);
129
130         // Set the text color.  For some unexplained reason, `android:textColor="?android:textColorPrimary"` doesn't work in the layout file.  Probably some bug relating to array adapters.
131         if (MainWebViewActivity.darkTheme) {
132             dispositionTextView.setTextColor(context.getResources().getColor(R.color.gray_200));
133             urlTextView.setTextColor(context.getResources().getColor(R.color.gray_200));
134         } else {
135             dispositionTextView.setTextColor(context.getResources().getColor(R.color.black));
136             urlTextView.setTextColor(context.getResources().getColor(R.color.black));
137         }
138
139         // Return the modified view.
140         return view;
141     }
142 }