]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/coroutines/PopulateBlocklistsCoroutine.kt
Migrate five classes to Kotlin. https://redmine.stoutner.com/issues/950
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / coroutines / PopulateBlocklistsCoroutine.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.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             // Populate the blocklists on the IO thread.
81             withContext(Dispatchers.IO) {
82                 // Populate EasyList.
83                 val easyList = blocklistHelper.parseBlocklist(context.assets, "blocklists/easylist.txt")
84
85                 // Advertise the loading of EasyPrivacy.
86                 withContext(Dispatchers.Main) {
87                     loadingBlocklistTextView.text = context.getString(R.string.loading_easyprivacy)
88                 }
89
90                 // Populate EasyPrivacy.
91                 val easyPrivacy = blocklistHelper.parseBlocklist(context.assets, "blocklists/easyprivacy.txt")
92
93                 // Advertise the loading of Fanboy's Annoyance List.
94                 withContext(Dispatchers.Main) {
95                     loadingBlocklistTextView.text = context.getString(R.string.loading_fanboys_annoyance_list)
96                 }
97
98                 // Populate Fanboy's Annoyance List.
99                 val fanboysAnnoyanceList = blocklistHelper.parseBlocklist(context.assets, "blocklists/fanboy-annoyance.txt")
100
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)
104                 }
105
106                 // Populate Fanboy's Social Blocking List.
107                 val fanboysSocialList = blocklistHelper.parseBlocklist(context.assets, "blocklists/fanboy-social.txt")
108
109                 // Advertise the loading of UltraList
110                 withContext(Dispatchers.Main) {
111                     loadingBlocklistTextView.text = context.getString(R.string.loading_ultralist)
112                 }
113
114                 // Populate UltraList.
115                 val ultraList = blocklistHelper.parseBlocklist(context.assets, "blocklists/ultralist.txt")
116
117                 // Advertise the loading of UltraPrivacy.
118                 withContext(Dispatchers.Main) {
119                     loadingBlocklistTextView.text = context.getString(R.string.loading_ultraprivacy)
120                 }
121
122                 // Populate UltraPrivacy.
123                 val ultraPrivacy = blocklistHelper.parseBlocklist(context.assets, "blocklists/ultraprivacy.txt")
124
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)
132
133                 // Update the UI.
134                 withContext(Dispatchers.Main) {
135                     // Show the drawer layout.
136                     drawerLayout.visibility = View.VISIBLE
137
138                     // Hide the loading blocklists screen.
139                     loadingBlocklistsRelativeLayout.visibility = View.GONE
140
141                     // Enable the sliding drawers.
142                     drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
143
144                     // Add the first tab.
145                     populateBlocklistsListener.finishedPopulatingBlocklists(combinedBlocklists)
146                 }
147             }
148         }
149     }
150 }