2 * Copyright © 2020 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.viewmodels;
22 import android.text.SpannableStringBuilder;
24 import androidx.annotation.Nullable;
25 import androidx.lifecycle.LiveData;
26 import androidx.lifecycle.MutableLiveData;
27 import androidx.lifecycle.ViewModel;
29 import com.stoutner.privacybrowser.backgroundtasks.GetSourceBackgroundTask;
31 import java.net.Proxy;
32 import java.util.concurrent.ExecutorService;
34 public class WebViewSource extends ViewModel {
35 // Initialize the mutable live data variables.
36 private final MutableLiveData<SpannableStringBuilder[]> mutableLiveDataSourceStringArray = new MutableLiveData<>();
37 private final MutableLiveData<String> mutableLiveDataErrorString = new MutableLiveData<>();
39 // Define the class variables.
40 private final String userAgent;
41 private final boolean doNotTrack;
42 private final String localeString;
43 private final Proxy proxy;
44 private final ExecutorService executorService;
46 // The public constructor.
47 public WebViewSource(@Nullable String urlString, String userAgent, boolean doNotTrack, String localeString, Proxy proxy, ExecutorService executorService) {
48 // Store the class variables.
49 this.userAgent = userAgent;
50 this.doNotTrack = doNotTrack;
51 this.localeString = localeString;
53 this.executorService = executorService;
56 updateSource(urlString);
59 // The source observer.
60 public LiveData<SpannableStringBuilder[]> observeSource() {
61 // Return the source to the activity.
62 return mutableLiveDataSourceStringArray;
65 // The error observer.
66 public LiveData<String> observeErrors() {
67 // Return any errors to the activity.
68 return mutableLiveDataErrorString;
71 // The interface for returning the error from the background task
72 public void returnError(String errorString) {
73 // Update the mutable live data error string.
74 mutableLiveDataErrorString.postValue(errorString);
77 // The workhorse that gets the source.
78 public void updateSource(String urlString) {
79 // Reset the mutable live data error string. This prevents the snackbar it from displaying a later if the activity restarts.
80 mutableLiveDataErrorString.postValue("");
82 // Instantiate the get source background task class.
83 GetSourceBackgroundTask getSourceBackgroundTask = new GetSourceBackgroundTask();
86 executorService.execute(() -> mutableLiveDataSourceStringArray.postValue(getSourceBackgroundTask.acquire(urlString, userAgent, doNotTrack, localeString, proxy, this)));