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