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.asynctasks;
22 import android.app.Activity;
23 import android.content.Context;
24 import android.os.AsyncTask;
25 import android.view.View;
26 import android.widget.RelativeLayout;
27 import android.widget.TextView;
29 import androidx.drawerlayout.widget.DrawerLayout;
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.helpers.BlocklistHelper;
34 import java.lang.ref.WeakReference;
35 import java.util.ArrayList;
36 import java.util.List;
38 public class PopulateBlocklists extends AsyncTask<Void, String, ArrayList<ArrayList<List<String[]>>>> {
39 // The public interface is used to send information back to the parent activity.
40 public interface PopulateBlocklistsListener {
41 void finishedPopulatingBlocklists(ArrayList<ArrayList<List<String[]>>> combinedBlocklists);
44 // Define a populate blocklists listener.
45 private final PopulateBlocklistsListener populateBlocklistsListener;
47 // Define weak references for the activity and context.
48 private final WeakReference<Context> contextWeakReference;
49 private final WeakReference<Activity> activityWeakReference;
51 // The public constructor.
52 public PopulateBlocklists(Context context, Activity activity) {
53 // Populate the weak reference to the context.
54 contextWeakReference = new WeakReference<>(context);
56 // Populate the weak reference to the activity.
57 activityWeakReference = new WeakReference<>(activity);
59 // Get a handle for the populate blocklists listener from the launching activity.
60 populateBlocklistsListener = (PopulateBlocklistsListener) context;
63 // `onPreExecute()` operates on the UI thread.
65 protected void onPreExecute() {
66 // Get a handle for the activity.
67 Activity activity = activityWeakReference.get();
69 // Abort if the activity is gone.
70 if ((activity == null) || activity.isFinishing()) {
74 // Get handles for the views.
75 RelativeLayout loadingBlocklistsRelativeLayout = activity.findViewById(R.id.loading_blocklists_relativelayout);
77 // Show the loading blocklists screen.
78 loadingBlocklistsRelativeLayout.setVisibility(View.VISIBLE);
82 protected ArrayList<ArrayList<List<String[]>>> doInBackground(Void... none) {
83 // Exit the AsyncTask if the app has been restarted.
88 // Get a handle for the context.
89 Context context = contextWeakReference.get();
91 // Instantiate the blocklist helper.
92 BlocklistHelper blocklistHelper = new BlocklistHelper();
94 // Create a combined array list.
95 ArrayList<ArrayList<List<String[]>>> combinedBlocklists = new ArrayList<>();
97 // Load the blocklists if the context still exists.
98 if (context != null) {
99 // Update the progress.
100 publishProgress(context.getString(R.string.loading_easylist));
102 // Populate EasyList.
103 ArrayList<List<String[]>> easyList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easylist.txt");
105 // Exit the AsyncTask if the app has been restarted.
111 // Update the progress.
112 publishProgress(context.getString(R.string.loading_easyprivacy));
114 // Populate EasyPrivacy.
115 ArrayList<List<String[]>> easyPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easyprivacy.txt");
117 // Exit the AsyncTask if the app has been restarted.
124 // Update the progress.
125 publishProgress(context.getString(R.string.loading_fanboys_annoyance_list));
127 // Populate Fanboy's Annoyance List.
128 ArrayList<List<String[]>> fanboysAnnoyanceList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-annoyance.txt");
130 // Exit the AsyncTask if the app has been restarted.
137 // Update the progress.
138 publishProgress(context.getString(R.string.loading_fanboys_social_blocking_list));
140 // Populate Fanboy's Social Blocking List.
141 ArrayList<List<String[]>> fanboysSocialList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-social.txt");
143 // Exit the AsyncTask if the app has been restarted.
150 // Update the progress.
151 publishProgress(context.getString(R.string.loading_ultralist));
153 // Populate UltraList.
154 ArrayList<List<String[]>> ultraList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/ultralist.txt");
156 // Exit the AsyncTask if the app has been restarted.
163 // Update the progress.
164 publishProgress(context.getString(R.string.loading_ultraprivacy));
166 // Populate UltraPrivacy.
167 ArrayList<List<String[]>> ultraPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/ultraprivacy.txt");
169 // Exit the AsyncTask if the app has been restarted.
176 // Populate the combined array list.
177 combinedBlocklists.add(easyList);
178 combinedBlocklists.add(easyPrivacy);
179 combinedBlocklists.add(fanboysAnnoyanceList);
180 combinedBlocklists.add(fanboysSocialList);
181 combinedBlocklists.add(ultraList);
182 combinedBlocklists.add(ultraPrivacy);
185 // Return the combined array list.
186 return combinedBlocklists;
190 protected void onProgressUpdate(String... loadingBlocklist) {
191 // Get a handle for the activity.
192 Activity activity = activityWeakReference.get();
194 // Abort if the activity is gone.
195 if ((activity == null) || activity.isFinishing()) {
199 // Get a handle for the loading blocklist text view.
200 TextView loadingBlocklistTextView = activity.findViewById(R.id.loading_blocklist_textview);
202 // Update the status.
203 loadingBlocklistTextView.setText(loadingBlocklist[0]);
207 protected void onPostExecute(ArrayList<ArrayList<List<String[]>>> combinedBlocklists) {
208 // Get a handle for the activity.
209 Activity activity = activityWeakReference.get();
211 // Abort if the activity is gone.
212 if ((activity == null) || activity.isFinishing()) {
216 // Get handles for the views.
217 DrawerLayout drawerLayout = activity.findViewById(R.id.drawerlayout);
218 RelativeLayout loadingBlocklistsRelativeLayout = activity.findViewById(R.id.loading_blocklists_relativelayout);
220 // Show the drawer layout.
221 drawerLayout.setVisibility(View.VISIBLE);
223 // Hide the loading blocklists screen.
224 loadingBlocklistsRelativeLayout.setVisibility(View.GONE);
226 // Enable the sliding drawers.
227 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
229 // Add the first tab.
230 populateBlocklistsListener.finishedPopulatingBlocklists(combinedBlocklists);