]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/coroutines/PopulateBlocklistsCoroutine.kt
4311ef5ea50a2434543497fbd63b23c38b758c5c
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / coroutines / PopulateBlocklistsCoroutine.kt
1 /*
2  * Copyright 2019,2021-2022 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.BlocklistHelper
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 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>>>>)
43     }
44
45     // Define a populate blocklists listener.
46     private val populateBlocklistsListener: PopulateBlocklistsListener
47
48     // Define the class variables.
49     private val context: Context
50
51     // The public constructor.
52     init {
53         // Get a handle for the populate blocklists listener from the launching activity.
54         populateBlocklistsListener = context as PopulateBlocklistsListener
55
56         // Store the context.
57         this.context = context
58     }
59
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)
67
68             // Show the loading blocklists screen.
69             loadingBlocklistsRelativeLayout.visibility = View.VISIBLE
70
71             // Instantiate the blocklist helper.
72             val blocklistHelper = BlocklistHelper()
73
74             // Create a combined array list.
75             val combinedBlocklists = ArrayList<ArrayList<List<Array<String>>>>()
76
77             // Advertise the loading of EasyList.
78             loadingBlocklistTextView.text = context.getString(R.string.loading_easylist)
79
80             withContext(Dispatchers.IO) {
81                 // Populate EasyList.
82                 val easyList = blocklistHelper.parseBlocklist(context.assets, "blocklists/easylist.txt")
83
84                 // Advertise the loading of EasyPrivacy.
85                 withContext(Dispatchers.Main) {
86                     loadingBlocklistTextView.text = context.getString(R.string.loading_easyprivacy)
87                 }
88
89                 // Populate EasyPrivacy.
90                 val easyPrivacy = blocklistHelper.parseBlocklist(context.assets, "blocklists/easyprivacy.txt")
91
92                 // Advertise the loading of Fanboy's Annoyance List.
93                 withContext(Dispatchers.Main) {
94                     loadingBlocklistTextView.text = context.getString(R.string.loading_fanboys_annoyance_list)
95                 }
96
97                 // Populate Fanboy's Annoyance List.
98                 val fanboysAnnoyanceList = blocklistHelper.parseBlocklist(context.assets, "blocklists/fanboy-annoyance.txt")
99
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)
103                 }
104
105                 // Populate Fanboy's Social Blocking List.
106                 val fanboysSocialList = blocklistHelper.parseBlocklist(context.assets, "blocklists/fanboy-social.txt")
107
108                 // Advertise the loading of UltraList
109                 withContext(Dispatchers.Main) {
110                     loadingBlocklistTextView.text = context.getString(R.string.loading_ultralist)
111                 }
112
113                 // Populate UltraList.
114                 val ultraList = blocklistHelper.parseBlocklist(context.assets, "blocklists/ultralist.txt")
115
116                 // Advertise the loading of UltraPrivacy.
117                 withContext(Dispatchers.Main) {
118                     loadingBlocklistTextView.text = context.getString(R.string.loading_ultraprivacy)
119                 }
120
121                 // Populate UltraPrivacy.
122                 val ultraPrivacy = blocklistHelper.parseBlocklist(context.assets, "blocklists/ultraprivacy.txt")
123
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)
131
132                 // Update the UI.
133                 withContext(Dispatchers.Main) {
134                     // Show the drawer layout.
135                     drawerLayout.visibility = View.VISIBLE
136
137                     // Hide the loading blocklists screen.
138                     loadingBlocklistsRelativeLayout.visibility = View.GONE
139
140                     // Enable the sliding drawers.
141                     drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
142
143                     // Add the first tab.
144                     populateBlocklistsListener.finishedPopulatingBlocklists(combinedBlocklists)
145                 }
146             }
147         }
148     }
149 }