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.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;
32 import androidx.annotation.NonNull;
34 import com.stoutner.privacybrowser.R;
35 import com.stoutner.privacybrowser.helpers.BlockListHelper;
37 import java.util.List;
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);
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());
51 // Get the theme preferences.
52 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
54 // Get a handle for the context.
55 Context context = getContext();
57 // Inflate the view if it is null.
59 view = LayoutInflater.from(context).inflate(R.layout.requests_item_linearlayout, parent, false);
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);
67 // Get the string array for this entry.
68 String[] entryStringArray = getItem(position);
70 // Remove the lint warning below that `entryStringArray` might be null.
71 assert entryStringArray != null;
73 // The ID is one greater than the position because it is 0 based.
74 int id = position + 1;
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);
82 // Set the disposition text.
83 dispositionTextView.setText(requestDefault);
85 // Set the background color.
86 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.transparent));
89 case BlockListHelper.REQUEST_ALLOWED:
90 // Create the disposition string.
91 String requestAllowed = id + ". " + context.getResources().getString(R.string.allowed);
93 // Set the disposition text.
94 dispositionTextView.setText(requestAllowed);
96 // Set the background color.
98 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.blue_700_50));
100 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.blue_100));
104 case BlockListHelper.REQUEST_THIRD_PARTY:
105 // Create the disposition string.
106 String requestThirdParty = id + ". " + context.getResources().getString(R.string.blocked);
108 // Set the disposition text.
109 dispositionTextView.setText(requestThirdParty);
111 // Set the background color.
113 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.yellow_700_50));
115 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.yellow_100));
120 case BlockListHelper.REQUEST_BLOCKED:
121 // Create the disposition string.
122 String requestBlocked = id + ". " + context.getResources().getString(R.string.blocked);
124 // Set the disposition text.
125 dispositionTextView.setText(requestBlocked);
127 // Set the background color.
129 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.red_700_40));
131 linearLayout.setBackgroundColor(context.getResources().getColor(R.color.red_100));
137 urlTextView.setText(entryStringArray[1]);
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.
141 dispositionTextView.setTextColor(context.getResources().getColor(R.color.gray_200));
142 urlTextView.setTextColor(context.getResources().getColor(R.color.gray_200));
144 dispositionTextView.setTextColor(context.getResources().getColor(R.color.black));
145 urlTextView.setTextColor(context.getResources().getColor(R.color.black));
148 // Return the modified view.