2 * Copyright 2019,2021-2022 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.BlocklistHelper
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 blocklistHelper = BlocklistHelper()
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 withContext(Dispatchers.IO) {
82 val easyList = blocklistHelper.parseBlocklist(context.assets, "blocklists/easylist.txt")
84 // Advertise the loading of EasyPrivacy.
85 withContext(Dispatchers.Main) {
86 loadingBlocklistTextView.text = context.getString(R.string.loading_easyprivacy)
89 // Populate EasyPrivacy.
90 val easyPrivacy = blocklistHelper.parseBlocklist(context.assets, "blocklists/easyprivacy.txt")
92 // Advertise the loading of Fanboy's Annoyance List.
93 withContext(Dispatchers.Main) {
94 loadingBlocklistTextView.text = context.getString(R.string.loading_fanboys_annoyance_list)
97 // Populate Fanboy's Annoyance List.
98 val fanboysAnnoyanceList = blocklistHelper.parseBlocklist(context.assets, "blocklists/fanboy-annoyance.txt")
100 // Advertise the loading of Fanboy's social blocking list.
101 withContext(Dispatchers.Main) {
102 loadingBlocklistTextView.text = context.getString(R.string.loading_fanboys_social_blocking_list)
105 // Populate Fanboy's Social Blocking List.
106 val fanboysSocialList = blocklistHelper.parseBlocklist(context.assets, "blocklists/fanboy-social.txt")
108 // Advertise the loading of UltraList
109 withContext(Dispatchers.Main) {
110 loadingBlocklistTextView.text = context.getString(R.string.loading_ultralist)
113 // Populate UltraList.
114 val ultraList = blocklistHelper.parseBlocklist(context.assets, "blocklists/ultralist.txt")
116 // Advertise the loading of UltraPrivacy.
117 withContext(Dispatchers.Main) {
118 loadingBlocklistTextView.text = context.getString(R.string.loading_ultraprivacy)
121 // Populate UltraPrivacy.
122 val ultraPrivacy = blocklistHelper.parseBlocklist(context.assets, "blocklists/ultraprivacy.txt")
124 // Populate the combined array list.
125 combinedBlocklists.add(easyList)
126 combinedBlocklists.add(easyPrivacy)
127 combinedBlocklists.add(fanboysAnnoyanceList)
128 combinedBlocklists.add(fanboysSocialList)
129 combinedBlocklists.add(ultraList)
130 combinedBlocklists.add(ultraPrivacy)
133 withContext(Dispatchers.Main) {
134 // Show the drawer layout.
135 drawerLayout.visibility = View.VISIBLE
137 // Hide the loading blocklists screen.
138 loadingBlocklistsRelativeLayout.visibility = View.GONE
140 // Enable the sliding drawers.
141 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
143 // Add the first tab.
144 populateBlocklistsListener.finishedPopulatingBlocklists(combinedBlocklists)