2 * Copyright 2018-2023 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
6 * Privacy Browser Android 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 Android 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 Android. 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 com.stoutner.privacybrowser.R
31 import com.stoutner.privacybrowser.helpers.BlocklistHelper
33 // `0` is the `textViewResourceId`, which is unused in this implementation.
34 class RequestsArrayAdapter(context: Context, resourceRequestsList: List<Array<String>>) : ArrayAdapter<Array<String>>(context, 0, resourceRequestsList) {
35 override fun getView(position: Int, view: View?, parent: ViewGroup): View {
36 // Copy the input view to a new view..
39 // Inflate the new view if it is null.
40 if (newView == null) {
41 newView = LayoutInflater.from(context).inflate(R.layout.requests_item_linearlayout, parent, false)
44 // Get handles for the views.
45 val linearLayout = newView!!.findViewById<LinearLayout>(R.id.request_item_linearlayout)
46 val dispositionTextView = newView.findViewById<TextView>(R.id.request_item_disposition)
47 val urlTextView = newView.findViewById<TextView>(R.id.request_item_url)
49 // Get the string array for this entry.
50 val entryStringArray = getItem(position)!!
52 // The ID is one greater than the position because it is 0 based.
55 // Set the action text and the background color.
56 when (entryStringArray[0]) {
57 BlocklistHelper.REQUEST_DEFAULT -> {
58 // Set the disposition text.
59 dispositionTextView.text = context.resources.getString(R.string.request_allowed, id)
61 // Set the background color.
62 linearLayout.setBackgroundColor(context.getColor(R.color.transparent))
65 BlocklistHelper.REQUEST_ALLOWED -> {
66 // Set the disposition text.
67 dispositionTextView.text = context.resources.getString(R.string.request_allowed, id)
69 // Set the background color.
70 linearLayout.setBackgroundColor(context.getColor(R.color.blue_background))
73 BlocklistHelper.REQUEST_THIRD_PARTY -> {
74 // Set the disposition text.
75 dispositionTextView.text = context.resources.getString(R.string.request_blocked, id)
77 // Set the background color.
78 linearLayout.setBackgroundColor(context.getColor(R.color.yellow_background))
81 BlocklistHelper.REQUEST_BLOCKED -> {
82 // Set the disposition text.
83 dispositionTextView.text = context.resources.getString(R.string.request_blocked, id)
85 // Set the background color.
86 linearLayout.setBackgroundColor(context.getColor(R.color.red_background))
91 urlTextView.text = entryStringArray[1]
93 // Return the modified view.