2 * Copyright © 2018-2020 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.adapters;
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;
31 import androidx.annotation.NonNull;
33 import com.stoutner.privacybrowser.R;
34 import com.stoutner.privacybrowser.helpers.BlocklistHelper;
36 import java.util.List;
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);
46 public View getView(int position, View view, @NonNull ViewGroup parent) {
47 // Get a handle for the context.
48 Context context = getContext();
50 // Inflate the view if it is null.
52 view = LayoutInflater.from(context).inflate(R.layout.requests_item_linearlayout, parent, false);
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);
60 // Get the string array for this entry.
61 String[] entryStringArray = getItem(position);
63 // Remove the lint warning below that `entryStringArray` might be null.
64 assert entryStringArray != null;
66 // The ID is one greater than the position because it is 0 based.
67 int id = position + 1;
69 // Get the current theme status.
70 int currentThemeStatus = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
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);
78 // Set the disposition text.
79 dispositionTextView.setText(requestDefault);
81 // Set the background color.
82 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.transparent));
85 case BlocklistHelper.REQUEST_ALLOWED:
86 // Create the disposition string.
87 String requestAllowed = id + ". " + context.getResources().getString(R.string.allowed);
89 // Set the disposition text.
90 dispositionTextView.setText(requestAllowed);
92 // Set the background color.
93 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
94 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.blue_100));
96 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.blue_700_50));
100 case BlocklistHelper.REQUEST_THIRD_PARTY:
101 // Create the disposition string.
102 String requestThirdParty = id + ". " + context.getResources().getString(R.string.blocked);
104 // Set the disposition text.
105 dispositionTextView.setText(requestThirdParty);
107 // Set the background color.
108 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
109 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.yellow_100));
111 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.yellow_700_50));
116 case BlocklistHelper.REQUEST_BLOCKED:
117 // Create the disposition string.
118 String requestBlocked = id + ". " + context.getResources().getString(R.string.blocked);
120 // Set the disposition text.
121 dispositionTextView.setText(requestBlocked);
123 // Set the background color.
124 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
125 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.red_100));
127 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.red_700_40));
133 urlTextView.setText(entryStringArray[1]);
135 // Return the modified view.