2 * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser 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 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. 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.LinearLayout;
27 import android.widget.RelativeLayout;
28 import android.widget.TextView;
30 import androidx.appcompat.widget.Toolbar;
31 import androidx.drawerlayout.widget.DrawerLayout;
33 import com.stoutner.privacybrowser.R;
34 import com.stoutner.privacybrowser.helpers.BlocklistHelper;
36 import java.lang.ref.WeakReference;
37 import java.util.ArrayList;
38 import java.util.List;
40 public class PopulateBlocklists extends AsyncTask<Void, String, ArrayList<ArrayList<List<String[]>>>> {
41 // The public interface is used to send information back to the parent activity.
42 public interface PopulateBlocklistsListener {
43 void finishedPopulatingBlocklists(ArrayList<ArrayList<List<String[]>>> combinedBlocklists);
46 // Define a populate blocklists listener.
47 private PopulateBlocklistsListener populateBlocklistsListener;
49 // Define weak references for the activity and context.
50 private WeakReference<Context> contextWeakReference;
51 private WeakReference<Activity> activityWeakReference;
53 // The public constructor.
54 public PopulateBlocklists(Context context, Activity activity) {
55 // Populate the weak reference to the context.
56 contextWeakReference = new WeakReference<>(context);
58 // Populate the weak reference to the activity.
59 activityWeakReference = new WeakReference<>(activity);
61 // Get a handle for the populate blocklists listener from the launching activity.
62 populateBlocklistsListener = (PopulateBlocklistsListener) context;
65 // `onPreExecute()` operates on the UI thread.
67 protected void onPreExecute() {
68 // Get a handle for the activity.
69 Activity activity = activityWeakReference.get();
71 // Abort if the activity is gone.
72 if ((activity == null) || activity.isFinishing()) {
76 // Get handles for the views.
77 Toolbar toolbar = activity.findViewById(R.id.toolbar);
78 LinearLayout tabsLinearLayout = activity.findViewById(R.id.tabs_linearlayout);
79 RelativeLayout loadingBlocklistsRelativeLayout = activity.findViewById(R.id.loading_blocklists_relativelayout);
81 // Hide the toolbar and tabs linear layout, which will be visible if this is being run after the app process has been killed in the background.
82 toolbar.setVisibility(View.GONE);
83 tabsLinearLayout.setVisibility(View.GONE);
85 // Show the loading blocklists screen.
86 loadingBlocklistsRelativeLayout.setVisibility(View.VISIBLE);
90 protected ArrayList<ArrayList<List<String[]>>> doInBackground(Void... none) {
91 // Exit the AsyncTask if the app has been restarted.
96 // Get a handle for the context.
97 Context context = contextWeakReference.get();
99 // Instantiate the blocklist helper.
100 BlocklistHelper blocklistHelper = new BlocklistHelper();
102 // Create a combined array list.
103 ArrayList<ArrayList<List<String[]>>> combinedBlocklists = new ArrayList<>();
105 // Load the blocklists if the context still exists.
106 if (context != null) {
107 // Update the progress.
108 publishProgress(context.getString(R.string.loading_easylist));
110 // Populate EasyList.
111 ArrayList<List<String[]>> easyList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easylist.txt");
113 // Exit the AsyncTask if the app has been restarted.
119 // Update the progress.
120 publishProgress(context.getString(R.string.loading_easyprivacy));
122 // Populate EasyPrivacy.
123 ArrayList<List<String[]>> easyPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easyprivacy.txt");
125 // Exit the AsyncTask if the app has been restarted.
132 // Update the progress.
133 publishProgress(context.getString(R.string.loading_fanboys_annoyance_list));
135 // Populate Fanboy's Annoyance List.
136 ArrayList<List<String[]>> fanboysAnnoyanceList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-annoyance.txt");
138 // Exit the AsyncTask if the app has been restarted.
145 // Update the progress.
146 publishProgress(context.getString(R.string.loading_fanboys_social_blocking_list));
148 // Populate Fanboy's Social Blocking List.
149 ArrayList<List<String[]>> fanboysSocialList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-social.txt");
151 // Exit the AsyncTask if the app has been restarted.
158 // Update the progress.
159 publishProgress(context.getString(R.string.loading_ultralist));
161 // Populate UltraList.
162 ArrayList<List<String[]>> ultraList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/ultralist.txt");
164 // Exit the AsyncTask if the app has been restarted.
171 // Update the progress.
172 publishProgress(context.getString(R.string.loading_ultraprivacy));
174 // Populate UltraPrivacy.
175 ArrayList<List<String[]>> ultraPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/ultraprivacy.txt");
177 // Exit the AsyncTask if the app has been restarted.
184 // Populate the combined array list.
185 combinedBlocklists.add(easyList);
186 combinedBlocklists.add(easyPrivacy);
187 combinedBlocklists.add(fanboysAnnoyanceList);
188 combinedBlocklists.add(fanboysSocialList);
189 combinedBlocklists.add(ultraList);
190 combinedBlocklists.add(ultraPrivacy);
193 // Return the combined array list.
194 return combinedBlocklists;
198 protected void onProgressUpdate(String... loadingBlocklist) {
199 // Get a handle for the activity.
200 Activity activity = activityWeakReference.get();
202 // Abort if the activity is gone.
203 if ((activity == null) || activity.isFinishing()) {
207 // Get a handle for the loading blocklist text view.
208 TextView loadingBlocklistTextView = activity.findViewById(R.id.loading_blocklist_textview);
210 // Update the status.
211 loadingBlocklistTextView.setText(loadingBlocklist[0]);
215 protected void onPostExecute(ArrayList<ArrayList<List<String[]>>> combinedBlocklists) {
216 // Get a handle for the activity.
217 Activity activity = activityWeakReference.get();
219 // Abort if the activity is gone.
220 if ((activity == null) || activity.isFinishing()) {
224 // Get handles for the views.
225 Toolbar toolbar = activity.findViewById(R.id.toolbar);
226 DrawerLayout drawerLayout = activity.findViewById(R.id.drawerlayout);
227 LinearLayout tabsLinearLayout = activity.findViewById(R.id.tabs_linearlayout);
228 RelativeLayout loadingBlocklistsRelativeLayout = activity.findViewById(R.id.loading_blocklists_relativelayout);
230 // Show the toolbar and tabs linear layout.
231 toolbar.setVisibility(View.VISIBLE);
232 tabsLinearLayout.setVisibility(View.VISIBLE);
234 // Hide the loading blocklists screen.
235 loadingBlocklistsRelativeLayout.setVisibility(View.GONE);
237 // Enable the sliding drawers.
238 drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
240 // Add the first tab.
241 populateBlocklistsListener.finishedPopulatingBlocklists(combinedBlocklists);