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 com.stoutner.privacybrowser.R;
31 import com.stoutner.privacybrowser.helpers.BlockListHelper;
33 import java.lang.ref.WeakReference;
34 import java.util.ArrayList;
35 import java.util.List;
37 import androidx.appcompat.widget.Toolbar;
39 public class PopulateBlocklists extends AsyncTask<Void, String, ArrayList<ArrayList<List<String[]>>>> {
40 // The public interface is used to send information back to the parent activity.
41 public interface PopulateBlocklistsListener {
42 void finishedPopulatingBlocklists(ArrayList<ArrayList<List<String[]>>> combinedBlocklists);
45 // Declare a populate blocklists listener.
46 private PopulateBlocklistsListener populateBlocklistsListener;
48 // Declare weak references for the activity and context.
49 private WeakReference<Context> contextWeakReference;
50 private WeakReference<Activity> activityWeakReference;
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;
64 protected ArrayList<ArrayList<List<String[]>>> doInBackground(Void... none) {
65 // Get a handle for the context.
66 Context context = contextWeakReference.get();
68 // Instantiate the blocklist helper.
69 BlockListHelper blockListHelper = new BlockListHelper();
71 // Create a combined array list.
72 ArrayList<ArrayList<List<String[]>>> combinedBlocklists = new ArrayList<>();
74 // Load the blocklists if the context still exists.
75 if (context != null) {
76 // Update the progress.
77 publishProgress(context.getString(R.string.loading_easylist));
80 ArrayList<List<String[]>> easyList = blockListHelper.parseBlockList(context.getAssets(), "blocklists/easylist.txt");
83 // Update the progress.
84 publishProgress(context.getString(R.string.loading_easyprivacy));
86 // Populate EasyPrivacy.
87 ArrayList<List<String[]>> easyPrivacy = blockListHelper.parseBlockList(context.getAssets(), "blocklists/easyprivacy.txt");
90 // Update the progress.
91 publishProgress(context.getString(R.string.loading_fanboys_annoyance_list));
93 // Populate Fanboy's Annoyance List.
94 ArrayList<List<String[]>> fanboysAnnoyanceList = blockListHelper.parseBlockList(context.getAssets(), "blocklists/fanboy-annoyance.txt");
97 // Update the progress.
98 publishProgress(context.getString(R.string.loading_fanboys_social_blocking_list));
100 // Populate Fanboy's Social Blocking List.
101 ArrayList<List<String[]>> fanboysSocialList = blockListHelper.parseBlockList(context.getAssets(), "blocklists/fanboy-social.txt");
104 // Update the progress.
105 publishProgress(context.getString(R.string.loading_ultraprivacy));
107 // Populate UltraPrivacy.
108 ArrayList<List<String[]>> ultraPrivacy = blockListHelper.parseBlockList(context.getAssets(), "blocklists/ultraprivacy.txt");
111 // Populate the combined array list.
112 combinedBlocklists.add(easyList);
113 combinedBlocklists.add(easyPrivacy);
114 combinedBlocklists.add(fanboysAnnoyanceList);
115 combinedBlocklists.add(fanboysSocialList);
116 combinedBlocklists.add(ultraPrivacy);
119 // Return the combined array list.
120 return combinedBlocklists;
124 protected void onProgressUpdate(String... loadingBlocklist) {
125 // Get a handle for the activity.
126 Activity activity = activityWeakReference.get();
128 // Abort if the activity is gone.
129 if ((activity == null) || activity.isFinishing()) {
133 // Get a handle for the loading blocklist text view.
134 TextView loadingBlocklistTextView = activity.findViewById(R.id.loading_blocklist_textview);
136 // Update the status.
137 loadingBlocklistTextView.setText(loadingBlocklist[0]);
141 protected void onPostExecute(ArrayList<ArrayList<List<String[]>>> combinedBlocklists) {
142 // Get a handle for the activity.
143 Activity activity = activityWeakReference.get();
145 // Abort if the activity is gone.
146 if ((activity == null) || activity.isFinishing()) {
150 // Get handles for the views.
151 Toolbar toolbar = activity.findViewById(R.id.toolbar);
152 LinearLayout tabsLinearLayout = activity.findViewById(R.id.tabs_linearlayout);
153 RelativeLayout loadingBlocklistsRelativeLayout = activity.findViewById(R.id.loading_blocklists_relativelayout);
155 // Show the toolbar and tabs linear layout.
156 toolbar.setVisibility(View.VISIBLE);
157 tabsLinearLayout.setVisibility(View.VISIBLE);
159 // Hide the loading blocklists screen.
160 loadingBlocklistsRelativeLayout.setVisibility(View.GONE);
162 // Add the first tab.
163 populateBlocklistsListener.finishedPopulatingBlocklists(combinedBlocklists);