Updated Turkisk translation.
[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     // `onPreExecute()` operates on the UI thread.
65     @Override
66     protected void onPreExecute() {
67         // Get a handle for the activity.
68         Activity activity = activityWeakReference.get();
69
70         // Abort if the activity is gone.
71         if ((activity == null) || activity.isFinishing()) {
72             return;
73         }
74
75         // Get handles for the views.
76         Toolbar toolbar = activity.findViewById(R.id.toolbar);
77         LinearLayout tabsLinearLayout = activity.findViewById(R.id.tabs_linearlayout);
78         RelativeLayout loadingBlocklistsRelativeLayout = activity.findViewById(R.id.loading_blocklists_relativelayout);
79
80         // 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.
81         toolbar.setVisibility(View.GONE);
82         tabsLinearLayout.setVisibility(View.GONE);
83
84         // Show the loading blocklists screen.
85         loadingBlocklistsRelativeLayout.setVisibility(View.VISIBLE);
86     }
87
88     @Override
89     protected ArrayList<ArrayList<List<String[]>>> doInBackground(Void... none) {
90         // Get a handle for the context.
91         Context context = contextWeakReference.get();
92
93         // Instantiate the blocklist helper.
94         BlocklistHelper blocklistHelper = new BlocklistHelper();
95
96         // Create a combined array list.
97         ArrayList<ArrayList<List<String[]>>> combinedBlocklists = new ArrayList<>();
98
99         // Load the blocklists if the context still exists.
100         if (context != null) {
101             // Update the progress.
102             publishProgress(context.getString(R.string.loading_easylist));
103
104             // Populate EasyList.
105             ArrayList<List<String[]>> easyList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easylist.txt");
106
107
108             // Update the progress.
109             publishProgress(context.getString(R.string.loading_easyprivacy));
110
111             // Populate EasyPrivacy.
112             ArrayList<List<String[]>> easyPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easyprivacy.txt");
113
114
115             // Update the progress.
116             publishProgress(context.getString(R.string.loading_fanboys_annoyance_list));
117
118             // Populate Fanboy's Annoyance List.
119             ArrayList<List<String[]>> fanboysAnnoyanceList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-annoyance.txt");
120
121
122             // Update the progress.
123             publishProgress(context.getString(R.string.loading_fanboys_social_blocking_list));
124
125             // Populate Fanboy's Social Blocking List.
126             ArrayList<List<String[]>> fanboysSocialList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-social.txt");
127
128
129             // Update the progress.
130             publishProgress(context.getString(R.string.loading_ultraprivacy));
131
132             // Populate UltraPrivacy.
133             ArrayList<List<String[]>> ultraPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/ultraprivacy.txt");
134
135
136             // Populate the combined array list.
137             combinedBlocklists.add(easyList);
138             combinedBlocklists.add(easyPrivacy);
139             combinedBlocklists.add(fanboysAnnoyanceList);
140             combinedBlocklists.add(fanboysSocialList);
141             combinedBlocklists.add(ultraPrivacy);
142         }
143
144         // Return the combined array list.
145         return combinedBlocklists;
146     }
147
148     @Override
149     protected void onProgressUpdate(String... loadingBlocklist) {
150         // Get a handle for the activity.
151         Activity activity = activityWeakReference.get();
152
153         // Abort if the activity is gone.
154         if ((activity == null) || activity.isFinishing()) {
155             return;
156         }
157
158         // Get a handle for the loading blocklist text view.
159         TextView loadingBlocklistTextView = activity.findViewById(R.id.loading_blocklist_textview);
160
161         // Update the status.
162         loadingBlocklistTextView.setText(loadingBlocklist[0]);
163     }
164
165     @Override
166     protected void onPostExecute(ArrayList<ArrayList<List<String[]>>> combinedBlocklists) {
167         // Get a handle for the activity.
168         Activity activity = activityWeakReference.get();
169
170         // Abort if the activity is gone.
171         if ((activity == null) || activity.isFinishing()) {
172             return;
173         }
174
175         // Get handles for the views.
176         Toolbar toolbar = activity.findViewById(R.id.toolbar);
177         DrawerLayout drawerLayout = activity.findViewById(R.id.drawerlayout);
178         LinearLayout tabsLinearLayout = activity.findViewById(R.id.tabs_linearlayout);
179         RelativeLayout loadingBlocklistsRelativeLayout = activity.findViewById(R.id.loading_blocklists_relativelayout);
180
181         // Show the toolbar and tabs linear layout.
182         toolbar.setVisibility(View.VISIBLE);
183         tabsLinearLayout.setVisibility(View.VISIBLE);
184
185         // Hide the loading blocklists screen.
186         loadingBlocklistsRelativeLayout.setVisibility(View.GONE);
187
188         // Enable the sliding drawers.
189         drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
190
191         // Add the first tab.
192         populateBlocklistsListener.finishedPopulatingBlocklists(combinedBlocklists);
193     }
194 }