2 * Copyright 2019,2021-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.coroutines
22 import android.app.Activity
23 import android.content.Context
24 import android.view.View
25 import android.widget.RelativeLayout
26 import android.widget.TextView
27 import androidx.drawerlayout.widget.DrawerLayout
29 import com.stoutner.privacybrowser.R
30 import com.stoutner.privacybrowser.helpers.ParseBlocklistHelper
32 import kotlinx.coroutines.CoroutineScope
33 import kotlinx.coroutines.Dispatchers
34 import kotlinx.coroutines.launch
35 import kotlinx.coroutines.withContext
37 import java.util.ArrayList
39 class PopulateBlocklistsCoroutine(context: Context) {
40 // The public interface is used to send information back to the parent activity.
41 interface PopulateBlocklistsListener {
42 fun finishedPopulatingBlocklists(combinedBlocklists: ArrayList<ArrayList<List<Array<String>>>>)
45 // Define a populate blocklists listener.
46 private val populateBlocklistsListener: PopulateBlocklistsListener
48 // Define the class variables.
49 private val context: Context
51 // The public constructor.
53 // Get a handle for the populate blocklists listener from the launching activity.
54 populateBlocklistsListener = context as PopulateBlocklistsListener
57 this.context = context
60 fun populateBlocklists(activity: Activity) {
61 // Use a coroutine to populate the blocklists.
62 CoroutineScope(Dispatchers.Main).launch {
63 // Get handles for the views.
64 val drawerLayout = activity.findViewById<DrawerLayout>(R.id.drawerlayout)
65 val loadingBlocklistsRelativeLayout = activity.findViewById<RelativeLayout>(R.id.loading_blocklists_relativelayout)
66 val loadingBlocklistTextView = activity.findViewById<TextView>(R.id.loading_blocklist_textview)
68 // Show the loading blocklists screen.
69 loadingBlocklistsRelativeLayout.visibility = View.VISIBLE
71 // Instantiate the blocklist helper.
72 val parseBlocklistHelper = ParseBlocklistHelper()
74 // Create a combined array list.
75 val combinedBlocklists = ArrayList<ArrayList<List<Array<String>>>>()
77 // Advertise the loading of EasyList.
78 loadingBlocklistTextView.text = context.getString(R.string.loading_easylist)
80 // Populate the blocklists on the IO thread.
81 withContext(Dispatchers.IO) {
83 val easyList = parseBlocklistHelper.parseBlocklist(context.assets, "blocklists/easylist.txt")
85 // Advertise the loading of EasyPrivacy.
86 withContext(Dispatchers.Main) {
87 loadingBlocklistTextView.text = context.getString(R.string.loading_easyprivacy)
90 // Populate EasyPrivacy.
91 val easyPrivacy = parseBlocklistHelper.parseBlocklist(context.assets, "blocklists/easyprivacy.txt")
93 // Advertise the loading of Fanboy's Annoyance List.
94 withContext(Dispatchers.Main) {
95 loadingBlocklistTextView.text = context.getString(R.string.loading_fanboys_annoyance_list)
98 // Populate Fanboy's Annoyance List.
99 val fanboysAnnoyanceList = parseBlocklistHelper.parseBlocklist(context.assets, "blocklists/fanboy-annoyance.txt")
101 // Advertise the loading of Fanboy's social blocking list.
102 withContext(Dispatchers.Main) {
103 loadingBlocklistTextView.text = context.getString(R.string.loading_fanboys_social_blocking_list)
106 // Populate Fanboy's Social Blocking List.
107 val fanboysSocialList = parseBlocklistHelper.parseBlocklist(context.assets, "blocklists/fanboy-social.txt")
109 // Advertise the loading of UltraList
110 withContext(Dispatchers.Main) {
111 loadingBlocklistTextView.text = context.getString(R.string.loading_ultralist)
114 // Populate UltraList.
115 val ultraList = parseBlocklistHelper.parseBlocklist(context.assets, "blocklists/ultralist.txt")
117 // Advertise the loading of UltraPrivacy.
118 withContext(Dispatchers.Main) {
119 loadingBlocklistTextView.text = context.getString(R.string.loading_ultraprivacy)
122 // Populate UltraPrivacy.
123 val ultraPrivacy = parseBlocklistHelper.parseBlocklist(context.assets, "blocklists/ultraprivacy.txt")
125 // Populate the combined array list.
126 combinedBlocklists.add(easyList)
127 combinedBlocklists.add(easyPrivacy)
128 combinedBlocklists.add(fanboysAnnoyanceList)
129 combinedBlocklists.add(fanboysSocialList)
130 combinedBlocklists.add(ultraList)
131 combinedBlocklists.add(ultraPrivacy)
134 withContext(Dispatchers.Main) {
135 // Show the drawer layout.
136 drawerLayout.visibility = View.VISIBLE
138 // Hide the loading blocklists screen.
139 loadingBlocklistsRelativeLayout.visibility = View.GONE
141 // Enable the sliding drawers.
142 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
144 // Add the first tab.
145 populateBlocklistsListener.finishedPopulatingBlocklists(combinedBlocklists)