2 * Copyright © 2018-2019 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.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ArrayAdapter;
27 import android.widget.LinearLayout;
28 import android.widget.TextView;
30 import androidx.annotation.NonNull;
32 import com.stoutner.privacybrowser.R;
33 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
35 import java.util.List;
37 public class RequestsArrayAdapter extends ArrayAdapter<String[]> {
38 public RequestsArrayAdapter(Context context, List<String[]> resourceRequestsList) {
39 // `super` must be called form the base ArrayAdapter. `0` is the `textViewResourceId`, which is unused.
40 super(context, 0, resourceRequestsList);
45 public View getView(int position, View view, @NonNull ViewGroup parent) {
46 // Get a handle for the context.
47 Context context = getContext();
49 // Inflate the view if it is null.
51 view = LayoutInflater.from(context).inflate(R.layout.requests_item_linearlayout, parent, false);
54 // Get handles for the views.
55 LinearLayout linearLayout = view.findViewById(R.id.request_item_linearlayout);
56 TextView dispositionTextView = view.findViewById(R.id.request_item_disposition);
57 TextView urlTextView = view.findViewById(R.id.request_item_url);
59 // Get the string array for this entry.
60 String[] entryStringArray = getItem(position);
62 // Remove the lint warning below that `entryStringArray` might be null.
63 assert entryStringArray != null;
65 // The ID is one greater than the position because it is 0 based.
66 int id = position + 1;
68 // Set the action text and the background color.
69 switch (Integer.valueOf(entryStringArray[0])) {
70 case MainWebViewActivity.REQUEST_DEFAULT:
71 // Create the disposition string.
72 String requestDefault = id + ". " + context.getResources().getString(R.string.allowed);
74 // Set the disposition text.
75 dispositionTextView.setText(requestDefault);
77 // Set the background color.
78 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.transparent));
81 case MainWebViewActivity.REQUEST_ALLOWED:
82 // Create the disposition string.
83 String requestAllowed = id + ". " + context.getResources().getString(R.string.allowed);
85 // Set the disposition text.
86 dispositionTextView.setText(requestAllowed);
88 // Set the background color.
89 if (MainWebViewActivity.darkTheme) {
90 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.blue_700_50));
92 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.blue_100));
96 case MainWebViewActivity.REQUEST_THIRD_PARTY:
97 // Create the disposition string.
98 String requestThirdParty = id + ". " + context.getResources().getString(R.string.blocked);
100 // Set the disposition text.
101 dispositionTextView.setText(requestThirdParty);
103 // Set the background color.
104 if (MainWebViewActivity.darkTheme) {
105 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.yellow_700_50));
107 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.yellow_100));
112 case MainWebViewActivity.REQUEST_BLOCKED:
113 // Create the disposition string.
114 String requestBlocked = id + ". " + context.getResources().getString(R.string.blocked);
116 // Set the disposition text.
117 dispositionTextView.setText(requestBlocked);
119 // Set the background color.
120 if (MainWebViewActivity.darkTheme) {
121 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.red_700_40));
123 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.red_100));
129 urlTextView.setText(entryStringArray[1]);
131 // 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.
132 if (MainWebViewActivity.darkTheme) {
133 dispositionTextView.setTextColor(context.getResources().getColor(R.color.gray_200));
134 urlTextView.setTextColor(context.getResources().getColor(R.color.gray_200));
136 dispositionTextView.setTextColor(context.getResources().getColor(R.color.black));
137 urlTextView.setTextColor(context.getResources().getColor(R.color.black));
140 // Return the modified view.