106c8d9c919a8ddbad8e16fbe92c1fd7c0bcd0a2
[PrivacyBrowser.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.ProgressBar;
28 import android.widget.RelativeLayout;
29 import android.widget.TextView;
30
31 import androidx.appcompat.widget.Toolbar;
32 import androidx.drawerlayout.widget.DrawerLayout;
33
34 import com.stoutner.privacybrowser.R;
35 import com.stoutner.privacybrowser.helpers.BlocklistHelper;
36
37 import java.lang.ref.WeakReference;
38 import java.util.ArrayList;
39 import java.util.List;
40
41 public class PopulateBlocklists extends AsyncTask<Void, String, ArrayList<ArrayList<List<String[]>>>> {
42     // The public interface is used to send information back to the parent activity.
43     public interface PopulateBlocklistsListener {
44         void finishedPopulatingBlocklists(ArrayList<ArrayList<List<String[]>>> combinedBlocklists);
45     }
46
47     // Declare a populate blocklists listener.
48     private PopulateBlocklistsListener populateBlocklistsListener;
49
50     // Declare weak references for the activity and context.
51     private WeakReference<Context> contextWeakReference;
52     private WeakReference<Activity> activityWeakReference;
53
54     public PopulateBlocklists(Context context, Activity activity) {
55         // Populate the weak reference to the context.
56         contextWeakReference = new WeakReference<>(context);
57
58         // Populate the weak reference to the activity.
59         activityWeakReference = new WeakReference<>(activity);
60
61         // Get a handle for the populate blocklists listener from the launching activity.
62         populateBlocklistsListener = (PopulateBlocklistsListener) context;
63     }
64
65     // `onPreExecute()` operates on the UI thread.
66     @Override
67     protected void onPreExecute() {
68         // Get a handle for the activity.
69         Activity activity = activityWeakReference.get();
70
71         // Abort if the activity is gone.
72         if ((activity == null) || activity.isFinishing()) {
73             return;
74         }
75
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);
80
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);
84
85         // Show the loading blocklists screen.
86         loadingBlocklistsRelativeLayout.setVisibility(View.VISIBLE);
87     }
88
89     @Override
90     protected ArrayList<ArrayList<List<String[]>>> doInBackground(Void... none) {
91         // Get a handle for the context.
92         Context context = contextWeakReference.get();
93
94         // Instantiate the blocklist helper.
95         BlocklistHelper blocklistHelper = new BlocklistHelper();
96
97         // Create a combined array list.
98         ArrayList<ArrayList<List<String[]>>> combinedBlocklists = new ArrayList<>();
99
100         // Load the blocklists if the context still exists.
101         if (context != null) {
102             // Update the progress.
103             publishProgress(context.getString(R.string.loading_easylist));
104
105             // Populate EasyList.
106             ArrayList<List<String[]>> easyList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easylist.txt");
107
108
109             // Update the progress.
110             publishProgress(context.getString(R.string.loading_easyprivacy));
111
112             // Populate EasyPrivacy.
113             ArrayList<List<String[]>> easyPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easyprivacy.txt");
114
115
116             // Update the progress.
117             publishProgress(context.getString(R.string.loading_fanboys_annoyance_list));
118
119             // Populate Fanboy's Annoyance List.
120             ArrayList<List<String[]>> fanboysAnnoyanceList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-annoyance.txt");
121
122
123             // Update the progress.
124             publishProgress(context.getString(R.string.loading_fanboys_social_blocking_list));
125
126             // Populate Fanboy's Social Blocking List.
127             ArrayList<List<String[]>> fanboysSocialList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-social.txt");
128
129
130             // Update the progress.
131             publishProgress(context.getString(R.string.loading_ultraprivacy));
132
133             // Populate UltraPrivacy.
134             ArrayList<List<String[]>> ultraPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/ultraprivacy.txt");
135
136
137             // Populate the combined array list.
138             combinedBlocklists.add(easyList);
139             combinedBlocklists.add(easyPrivacy);
140             combinedBlocklists.add(fanboysAnnoyanceList);
141             combinedBlocklists.add(fanboysSocialList);
142             combinedBlocklists.add(ultraPrivacy);
143         }
144
145         // Return the combined array list.
146         return combinedBlocklists;
147     }
148
149     @Override
150     protected void onProgressUpdate(String... loadingBlocklist) {
151         // Get a handle for the activity.
152         Activity activity = activityWeakReference.get();
153
154         // Abort if the activity is gone.
155         if ((activity == null) || activity.isFinishing()) {
156             return;
157         }
158
159         // Get a handle for the loading blocklist text view.
160         TextView loadingBlocklistTextView = activity.findViewById(R.id.loading_blocklist_textview);
161
162         // Update the status.
163         loadingBlocklistTextView.setText(loadingBlocklist[0]);
164     }
165
166     @Override
167     protected void onPostExecute(ArrayList<ArrayList<List<String[]>>> combinedBlocklists) {
168         // Get a handle for the activity.
169         Activity activity = activityWeakReference.get();
170
171         // Abort if the activity is gone.
172         if ((activity == null) || activity.isFinishing()) {
173             return;
174         }
175
176         // Get handles for the views.
177         Toolbar toolbar = activity.findViewById(R.id.toolbar);
178         DrawerLayout drawerLayout = activity.findViewById(R.id.drawerlayout);
179         LinearLayout tabsLinearLayout = activity.findViewById(R.id.tabs_linearlayout);
180         RelativeLayout loadingBlocklistsRelativeLayout = activity.findViewById(R.id.loading_blocklists_relativelayout);
181
182         // Show the toolbar and tabs linear layout.
183         toolbar.setVisibility(View.VISIBLE);
184         tabsLinearLayout.setVisibility(View.VISIBLE);
185
186         // Hide the loading blocklists screen.
187         loadingBlocklistsRelativeLayout.setVisibility(View.GONE);
188
189         // Enable the sliding drawers.
190         drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
191
192         // Add the first tab.
193         populateBlocklistsListener.finishedPopulatingBlocklists(combinedBlocklists);
194     }
195 }