2 * Copyright 2019,2021-2024 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.ParseFilterListHelper
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 PopulateFilterListsCoroutine(context: Context) {
40 // The public interface is used to send information back to the parent activity.
41 interface PopulateFilterListsListener {
42 fun finishedPopulatingFilterLists(combinedFilterLists: ArrayList<ArrayList<List<Array<String>>>>)
45 // Define the class variables.
46 private val context: Context
47 private val populateFilterListsListener: PopulateFilterListsListener
49 // The public constructor.
51 // Get a handle for the populate filter lists listener from the launching activity.
52 populateFilterListsListener = context as PopulateFilterListsListener
55 this.context = context
58 fun populateFilterLists(activity: Activity) {
59 // Use a coroutine to populate the filter lists.
60 CoroutineScope(Dispatchers.Main).launch {
61 // Get handles for the views.
62 val drawerLayout = activity.findViewById<DrawerLayout>(R.id.drawerlayout)
63 val loadingFilterListsRelativeLayout = activity.findViewById<RelativeLayout>(R.id.loading_filterlists_relativelayout)
64 val loadingFilterListTextView = activity.findViewById<TextView>(R.id.loading_filterlist_textview)
66 // Show the loading filter lists screen.
67 loadingFilterListsRelativeLayout.visibility = View.VISIBLE
69 // Instantiate the filter list helper.
70 val parseFilterListHelper = ParseFilterListHelper()
72 // Create a combined array list.
73 val combinedFilterLists = ArrayList<ArrayList<List<Array<String>>>>()
75 // Advertise the loading of EasyList.
76 loadingFilterListTextView.text = context.getString(R.string.loading_easylist)
78 // Populate the filter lists on the IO thread.
79 withContext(Dispatchers.IO) {
81 val easyList = parseFilterListHelper.parseFilterList(context.assets, "filterlists/easylist.txt")
83 // Advertise the loading of EasyPrivacy.
84 withContext(Dispatchers.Main) {
85 loadingFilterListTextView.text = context.getString(R.string.loading_easyprivacy)
88 // Populate EasyPrivacy.
89 val easyPrivacy = parseFilterListHelper.parseFilterList(context.assets, "filterlists/easyprivacy.txt")
91 // Advertise the loading of Fanboy's Annoyance List.
92 withContext(Dispatchers.Main) {
93 loadingFilterListTextView.text = context.getString(R.string.loading_fanboys_annoyance_list)
96 // Populate Fanboy's Annoyance List.
97 val fanboysAnnoyanceList = parseFilterListHelper.parseFilterList(context.assets, "filterlists/fanboy-annoyance.txt")
99 // Advertise the loading of Fanboy's social blocking list.
100 withContext(Dispatchers.Main) {
101 loadingFilterListTextView.text = context.getString(R.string.loading_fanboys_social_blocking_list)
104 // Populate Fanboy's Social Blocking List.
105 val fanboysSocialList = parseFilterListHelper.parseFilterList(context.assets, "filterlists/fanboy-social.txt")
107 // Advertise the loading of UltraList
108 withContext(Dispatchers.Main) {
109 loadingFilterListTextView.text = context.getString(R.string.loading_ultralist)
112 // Populate UltraList.
113 val ultraList = parseFilterListHelper.parseFilterList(context.assets, "filterlists/ultralist.txt")
115 // Advertise the loading of UltraPrivacy.
116 withContext(Dispatchers.Main) {
117 loadingFilterListTextView.text = context.getString(R.string.loading_ultraprivacy)
120 // Populate UltraPrivacy.
121 val ultraPrivacy = parseFilterListHelper.parseFilterList(context.assets, "filterlists/ultraprivacy.txt")
123 // Populate the combined array list.
124 combinedFilterLists.add(easyList)
125 combinedFilterLists.add(easyPrivacy)
126 combinedFilterLists.add(fanboysAnnoyanceList)
127 combinedFilterLists.add(fanboysSocialList)
128 combinedFilterLists.add(ultraList)
129 combinedFilterLists.add(ultraPrivacy)
132 withContext(Dispatchers.Main) {
133 // Show the drawer layout.
134 drawerLayout.visibility = View.VISIBLE
136 // Hide the loading filter lists screen.
137 loadingFilterListsRelativeLayout.visibility = View.GONE
139 // Enable the sliding drawers.
140 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
142 // Continue loading the app.
143 populateFilterListsListener.finishedPopulatingFilterLists(combinedFilterLists)