]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/asynctasks/PopulateBlocklists.java
Update Download with External App summary. https://redmine.stoutner.com/issues/921
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / asynctasks / PopulateBlocklists.java
1 /*
2  * Copyright © 2019,2021-2022 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.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.RelativeLayout;
27 import android.widget.TextView;
28
29 import androidx.drawerlayout.widget.DrawerLayout;
30
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.helpers.BlocklistHelper;
33
34 import java.lang.ref.WeakReference;
35 import java.util.ArrayList;
36 import java.util.List;
37
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);
42     }
43
44     // Define a populate blocklists listener.
45     private final PopulateBlocklistsListener populateBlocklistsListener;
46
47     // Define weak references for the activity and context.
48     private final WeakReference<Context> contextWeakReference;
49     private final WeakReference<Activity> activityWeakReference;
50
51     // The public constructor.
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     // `onPreExecute()` operates on the UI thread.
64     @Override
65     protected void onPreExecute() {
66         // Get a handle for the activity.
67         Activity activity = activityWeakReference.get();
68
69         // Abort if the activity is gone.
70         if ((activity == null) || activity.isFinishing()) {
71             return;
72         }
73
74         // Get handles for the views.
75         RelativeLayout loadingBlocklistsRelativeLayout = activity.findViewById(R.id.loading_blocklists_relativelayout);
76
77         // Show the loading blocklists screen.
78         loadingBlocklistsRelativeLayout.setVisibility(View.VISIBLE);
79     }
80
81     @Override
82     protected ArrayList<ArrayList<List<String[]>>> doInBackground(Void... none) {
83         // Exit the AsyncTask if the app has been restarted.
84         if (isCancelled()) {
85             return null;
86         }
87
88         // Get a handle for the context.
89         Context context = contextWeakReference.get();
90
91         // Instantiate the blocklist helper.
92         BlocklistHelper blocklistHelper = new BlocklistHelper();
93
94         // Create a combined array list.
95         ArrayList<ArrayList<List<String[]>>> combinedBlocklists = new ArrayList<>();
96
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));
101
102             // Populate EasyList.
103             ArrayList<List<String[]>> easyList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easylist.txt");
104
105             // Exit the AsyncTask if the app has been restarted.
106             if (isCancelled()) {
107                 return null;
108             }
109
110
111             // Update the progress.
112             publishProgress(context.getString(R.string.loading_easyprivacy));
113
114             // Populate EasyPrivacy.
115             ArrayList<List<String[]>> easyPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easyprivacy.txt");
116
117             // Exit the AsyncTask if the app has been restarted.
118             if (isCancelled()) {
119                 return null;
120             }
121
122
123
124             // Update the progress.
125             publishProgress(context.getString(R.string.loading_fanboys_annoyance_list));
126
127             // Populate Fanboy's Annoyance List.
128             ArrayList<List<String[]>> fanboysAnnoyanceList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-annoyance.txt");
129
130             // Exit the AsyncTask if the app has been restarted.
131             if (isCancelled()) {
132                 return null;
133             }
134
135
136
137             // Update the progress.
138             publishProgress(context.getString(R.string.loading_fanboys_social_blocking_list));
139
140             // Populate Fanboy's Social Blocking List.
141             ArrayList<List<String[]>> fanboysSocialList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-social.txt");
142
143             // Exit the AsyncTask if the app has been restarted.
144             if (isCancelled()) {
145                 return null;
146             }
147
148
149
150             // Update the progress.
151             publishProgress(context.getString(R.string.loading_ultralist));
152
153             // Populate UltraList.
154             ArrayList<List<String[]>> ultraList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/ultralist.txt");
155
156             // Exit the AsyncTask if the app has been restarted.
157             if (isCancelled()) {
158                 return null;
159             }
160
161
162
163             // Update the progress.
164             publishProgress(context.getString(R.string.loading_ultraprivacy));
165
166             // Populate UltraPrivacy.
167             ArrayList<List<String[]>> ultraPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/ultraprivacy.txt");
168
169             // Exit the AsyncTask if the app has been restarted.
170             if (isCancelled()) {
171                 return null;
172             }
173
174
175
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);
183         }
184
185         // Return the combined array list.
186         return combinedBlocklists;
187     }
188
189     @Override
190     protected void onProgressUpdate(String... loadingBlocklist) {
191         // Get a handle for the activity.
192         Activity activity = activityWeakReference.get();
193
194         // Abort if the activity is gone.
195         if ((activity == null) || activity.isFinishing()) {
196             return;
197         }
198
199         // Get a handle for the loading blocklist text view.
200         TextView loadingBlocklistTextView = activity.findViewById(R.id.loading_blocklist_textview);
201
202         // Update the status.
203         loadingBlocklistTextView.setText(loadingBlocklist[0]);
204     }
205
206     @Override
207     protected void onPostExecute(ArrayList<ArrayList<List<String[]>>> combinedBlocklists) {
208         // Get a handle for the activity.
209         Activity activity = activityWeakReference.get();
210
211         // Abort if the activity is gone.
212         if ((activity == null) || activity.isFinishing()) {
213             return;
214         }
215
216         // Get handles for the views.
217         DrawerLayout drawerLayout = activity.findViewById(R.id.drawerlayout);
218         RelativeLayout loadingBlocklistsRelativeLayout = activity.findViewById(R.id.loading_blocklists_relativelayout);
219
220         // Show the drawer layout.
221         drawerLayout.setVisibility(View.VISIBLE);
222
223         // Hide the loading blocklists screen.
224         loadingBlocklistsRelativeLayout.setVisibility(View.GONE);
225
226         // Enable the sliding drawers.
227         drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
228
229         // Add the first tab.
230         populateBlocklistsListener.finishedPopulatingBlocklists(combinedBlocklists);
231     }
232 }