]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/coroutines/PopulateFilterListsCoroutine.kt
Replace `Blocklists` with `Filter Lists`. https://redmine.stoutner.com/issues/705
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / coroutines / PopulateFilterListsCoroutine.kt
1 /*
2  * Copyright 2019,2021-2023 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.coroutines
21
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
28
29 import com.stoutner.privacybrowser.R
30 import com.stoutner.privacybrowser.helpers.ParseFilterListHelper
31
32 import kotlinx.coroutines.CoroutineScope
33 import kotlinx.coroutines.Dispatchers
34 import kotlinx.coroutines.launch
35 import kotlinx.coroutines.withContext
36
37 import java.util.ArrayList
38
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>>>>)
43     }
44
45     // Define the class variables.
46     private val context: Context
47     private val populateFilterListsListener: PopulateFilterListsListener
48
49     // The public constructor.
50     init {
51         // Get a handle for the populate filter lists listener from the launching activity.
52         populateFilterListsListener = context as PopulateFilterListsListener
53
54         // Store the context.
55         this.context = context
56     }
57
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)
65
66             // Show the loading filter lists screen.
67             loadingFilterListsRelativeLayout.visibility = View.VISIBLE
68
69             // Instantiate the filter list helper.
70             val parseFilterListHelper = ParseFilterListHelper()
71
72             // Create a combined array list.
73             val combinedFilterLists = ArrayList<ArrayList<List<Array<String>>>>()
74
75             // Advertise the loading of EasyList.
76             loadingFilterListTextView.text = context.getString(R.string.loading_easylist)
77
78             // Populate the filter lists on the IO thread.
79             withContext(Dispatchers.IO) {
80                 // Populate EasyList.
81                 val easyList = parseFilterListHelper.parseFilterList(context.assets, "filterlists/easylist.txt")
82
83                 // Advertise the loading of EasyPrivacy.
84                 withContext(Dispatchers.Main) {
85                     loadingFilterListTextView.text = context.getString(R.string.loading_easyprivacy)
86                 }
87
88                 // Populate EasyPrivacy.
89                 val easyPrivacy = parseFilterListHelper.parseFilterList(context.assets, "filterlists/easyprivacy.txt")
90
91                 // Advertise the loading of Fanboy's Annoyance List.
92                 withContext(Dispatchers.Main) {
93                     loadingFilterListTextView.text = context.getString(R.string.loading_fanboys_annoyance_list)
94                 }
95
96                 // Populate Fanboy's Annoyance List.
97                 val fanboysAnnoyanceList = parseFilterListHelper.parseFilterList(context.assets, "filterlists/fanboy-annoyance.txt")
98
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)
102                 }
103
104                 // Populate Fanboy's Social Blocking List.
105                 val fanboysSocialList = parseFilterListHelper.parseFilterList(context.assets, "filterlists/fanboy-social.txt")
106
107                 // Advertise the loading of UltraList
108                 withContext(Dispatchers.Main) {
109                     loadingFilterListTextView.text = context.getString(R.string.loading_ultralist)
110                 }
111
112                 // Populate UltraList.
113                 val ultraList = parseFilterListHelper.parseFilterList(context.assets, "filterlists/ultralist.txt")
114
115                 // Advertise the loading of UltraPrivacy.
116                 withContext(Dispatchers.Main) {
117                     loadingFilterListTextView.text = context.getString(R.string.loading_ultraprivacy)
118                 }
119
120                 // Populate UltraPrivacy.
121                 val ultraPrivacy = parseFilterListHelper.parseFilterList(context.assets, "filterlists/ultraprivacy.txt")
122
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)
130
131                 // Update the UI.
132                 withContext(Dispatchers.Main) {
133                     // Show the drawer layout.
134                     drawerLayout.visibility = View.VISIBLE
135
136                     // Hide the loading filter lists screen.
137                     loadingFilterListsRelativeLayout.visibility = View.GONE
138
139                     // Enable the sliding drawers.
140                     drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
141
142                     // Add the first tab.
143                     populateFilterListsListener.finishedPopulatingFilterLists(combinedFilterLists)
144                 }
145             }
146         }
147     }
148 }