]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/asynctasks/PopulateBlocklists.java
Fix changes to Download with External App not applying to existing tabs. https:...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / asynctasks / PopulateBlocklists.java
1 /*
2  * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.asynctasks;
21
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;
29
30 import com.stoutner.privacybrowser.R;
31 import com.stoutner.privacybrowser.helpers.BlockListHelper;
32
33 import java.lang.ref.WeakReference;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import androidx.appcompat.widget.Toolbar;
38
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);
43     }
44
45     // Declare a populate blocklists listener.
46     private PopulateBlocklistsListener populateBlocklistsListener;
47
48     // Declare weak references for the activity and context.
49     private WeakReference<Context> contextWeakReference;
50     private WeakReference<Activity> activityWeakReference;
51
52     public PopulateBlocklists(Context context, Activity activity) {
53         // Populate the weak reference to the context.
54         contextWeakReference = new WeakReference<>(context);
55
56         // Populate the weak reference to the activity.
57         activityWeakReference = new WeakReference<>(activity);
58
59         // Get a handle for the populate blocklists listener from the launching activity.
60         populateBlocklistsListener = (PopulateBlocklistsListener) context;
61     }
62
63     @Override
64     protected ArrayList<ArrayList<List<String[]>>> doInBackground(Void... none) {
65         // Get a handle for the context.
66         Context context = contextWeakReference.get();
67
68         // Instantiate the blocklist helper.
69         BlockListHelper blockListHelper = new BlockListHelper();
70
71         // Create a combined array list.
72         ArrayList<ArrayList<List<String[]>>> combinedBlocklists = new ArrayList<>();
73
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));
78
79             // Populate EasyList.
80             ArrayList<List<String[]>> easyList = blockListHelper.parseBlockList(context.getAssets(), "blocklists/easylist.txt");
81
82
83             // Update the progress.
84             publishProgress(context.getString(R.string.loading_easyprivacy));
85
86             // Populate EasyPrivacy.
87             ArrayList<List<String[]>> easyPrivacy = blockListHelper.parseBlockList(context.getAssets(), "blocklists/easyprivacy.txt");
88
89
90             // Update the progress.
91             publishProgress(context.getString(R.string.loading_fanboys_annoyance_list));
92
93             // Populate Fanboy's Annoyance List.
94             ArrayList<List<String[]>> fanboysAnnoyanceList = blockListHelper.parseBlockList(context.getAssets(), "blocklists/fanboy-annoyance.txt");
95
96
97             // Update the progress.
98             publishProgress(context.getString(R.string.loading_fanboys_social_blocking_list));
99
100             // Populate Fanboy's Social Blocking List.
101             ArrayList<List<String[]>> fanboysSocialList = blockListHelper.parseBlockList(context.getAssets(), "blocklists/fanboy-social.txt");
102
103
104             // Update the progress.
105             publishProgress(context.getString(R.string.loading_ultraprivacy));
106
107             // Populate UltraPrivacy.
108             ArrayList<List<String[]>> ultraPrivacy = blockListHelper.parseBlockList(context.getAssets(), "blocklists/ultraprivacy.txt");
109
110
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);
117         }
118
119         // Return the combined array list.
120         return combinedBlocklists;
121     }
122
123     @Override
124     protected void onProgressUpdate(String... loadingBlocklist) {
125         // Get a handle for the activity.
126         Activity activity = activityWeakReference.get();
127
128         // Abort if the activity is gone.
129         if ((activity == null) || activity.isFinishing()) {
130             return;
131         }
132
133         // Get a handle for the loading blocklist text view.
134         TextView loadingBlocklistTextView = activity.findViewById(R.id.loading_blocklist_textview);
135
136         // Update the status.
137         loadingBlocklistTextView.setText(loadingBlocklist[0]);
138     }
139
140     @Override
141     protected void onPostExecute(ArrayList<ArrayList<List<String[]>>> combinedBlocklists) {
142         // Get a handle for the activity.
143         Activity activity = activityWeakReference.get();
144
145         // Abort if the activity is gone.
146         if ((activity == null) || activity.isFinishing()) {
147             return;
148         }
149
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);
154
155         // Show the toolbar and tabs linear layout.
156         toolbar.setVisibility(View.VISIBLE);
157         tabsLinearLayout.setVisibility(View.VISIBLE);
158
159         // Hide the loading blocklists screen.
160         loadingBlocklistsRelativeLayout.setVisibility(View.GONE);
161
162         // Add the first tab.
163         populateBlocklistsListener.finishedPopulatingBlocklists(combinedBlocklists);
164     }
165 }