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