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