/*
- * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2019,2021-2022 Soren Stoutner <soren@stoutner.com>.
*
- * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
+ * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
*
- * Privacy Browser is free software: you can redistribute it and/or modify
+ * Privacy Browser Android is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
- * Privacy Browser is distributed in the hope that it will be useful,
+ * Privacy Browser Android is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
+ * along with Privacy Browser Android. If not, see <http://www.gnu.org/licenses/>.
*/
package com.stoutner.privacybrowser.asynctasks;
void finishedPopulatingBlocklists(ArrayList<ArrayList<List<String[]>>> combinedBlocklists);
}
- // Declare a populate blocklists listener.
- private PopulateBlocklistsListener populateBlocklistsListener;
+ // Define a populate blocklists listener.
+ private final PopulateBlocklistsListener populateBlocklistsListener;
- // Declare weak references for the activity and context.
- private WeakReference<Context> contextWeakReference;
- private WeakReference<Activity> activityWeakReference;
+ // Define weak references for the activity and context.
+ private final WeakReference<Context> contextWeakReference;
+ private final WeakReference<Activity> activityWeakReference;
+ // The public constructor.
public PopulateBlocklists(Context context, Activity activity) {
// Populate the weak reference to the context.
contextWeakReference = new WeakReference<>(context);
populateBlocklistsListener = (PopulateBlocklistsListener) context;
}
+ // `onPreExecute()` operates on the UI thread.
+ @Override
+ protected void onPreExecute() {
+ // Get a handle for the activity.
+ Activity activity = activityWeakReference.get();
+
+ // Abort if the activity is gone.
+ if ((activity == null) || activity.isFinishing()) {
+ return;
+ }
+
+ // Get handles for the views.
+ Toolbar toolbar = activity.findViewById(R.id.toolbar);
+ LinearLayout tabsLinearLayout = activity.findViewById(R.id.tabs_linearlayout);
+ RelativeLayout loadingBlocklistsRelativeLayout = activity.findViewById(R.id.loading_blocklists_relativelayout);
+
+ // 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.
+ toolbar.setVisibility(View.GONE);
+ tabsLinearLayout.setVisibility(View.GONE);
+
+ // Show the loading blocklists screen.
+ loadingBlocklistsRelativeLayout.setVisibility(View.VISIBLE);
+ }
+
@Override
protected ArrayList<ArrayList<List<String[]>>> doInBackground(Void... none) {
+ // Exit the AsyncTask if the app has been restarted.
+ if (isCancelled()) {
+ return null;
+ }
+
// Get a handle for the context.
Context context = contextWeakReference.get();
// Populate EasyList.
ArrayList<List<String[]>> easyList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easylist.txt");
+ // Exit the AsyncTask if the app has been restarted.
+ if (isCancelled()) {
+ return null;
+ }
+
// Update the progress.
publishProgress(context.getString(R.string.loading_easyprivacy));
// Populate EasyPrivacy.
ArrayList<List<String[]>> easyPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/easyprivacy.txt");
+ // Exit the AsyncTask if the app has been restarted.
+ if (isCancelled()) {
+ return null;
+ }
+
+
// Update the progress.
publishProgress(context.getString(R.string.loading_fanboys_annoyance_list));
// Populate Fanboy's Annoyance List.
ArrayList<List<String[]>> fanboysAnnoyanceList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-annoyance.txt");
+ // Exit the AsyncTask if the app has been restarted.
+ if (isCancelled()) {
+ return null;
+ }
+
+
// Update the progress.
publishProgress(context.getString(R.string.loading_fanboys_social_blocking_list));
// Populate Fanboy's Social Blocking List.
ArrayList<List<String[]>> fanboysSocialList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/fanboy-social.txt");
+ // Exit the AsyncTask if the app has been restarted.
+ if (isCancelled()) {
+ return null;
+ }
+
+
+
+ // Update the progress.
+ publishProgress(context.getString(R.string.loading_ultralist));
+
+ // Populate UltraList.
+ ArrayList<List<String[]>> ultraList = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/ultralist.txt");
+
+ // Exit the AsyncTask if the app has been restarted.
+ if (isCancelled()) {
+ return null;
+ }
+
+
// Update the progress.
publishProgress(context.getString(R.string.loading_ultraprivacy));
// Populate UltraPrivacy.
ArrayList<List<String[]>> ultraPrivacy = blocklistHelper.parseBlocklist(context.getAssets(), "blocklists/ultraprivacy.txt");
+ // Exit the AsyncTask if the app has been restarted.
+ if (isCancelled()) {
+ return null;
+ }
+
+
// Populate the combined array list.
combinedBlocklists.add(easyList);
combinedBlocklists.add(easyPrivacy);
combinedBlocklists.add(fanboysAnnoyanceList);
combinedBlocklists.add(fanboysSocialList);
+ combinedBlocklists.add(ultraList);
combinedBlocklists.add(ultraPrivacy);
}