]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/viewmodels/WebViewSource.java
Release 3.6.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / viewmodels / WebViewSource.java
1 /*
2  * Copyright © 2020 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.viewmodels;
21
22 import android.text.SpannableStringBuilder;
23
24 import androidx.annotation.Nullable;
25 import androidx.lifecycle.LiveData;
26 import androidx.lifecycle.MutableLiveData;
27 import androidx.lifecycle.ViewModel;
28
29 import com.stoutner.privacybrowser.backgroundtasks.GetSourceBackgroundTask;
30
31 import java.net.Proxy;
32 import java.util.concurrent.ExecutorService;
33
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<>();
38
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;
45
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;
52         this.proxy = proxy;
53         this.executorService = executorService;
54
55         // Get the source.
56         updateSource(urlString);
57     }
58
59     // The source observer.
60     public LiveData<SpannableStringBuilder[]> observeSource() {
61         // Return the source to the activity.
62         return mutableLiveDataSourceStringArray;
63     }
64
65     // The error observer.
66     public LiveData<String> observeErrors() {
67         // Return any errors to the activity.
68         return mutableLiveDataErrorString;
69     }
70
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);
75     }
76
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("");
81
82         // Instantiate the get source background task class.
83         GetSourceBackgroundTask getSourceBackgroundTask = new GetSourceBackgroundTask();
84
85         // Get the source.
86         executorService.execute(() -> mutableLiveDataSourceStringArray.postValue(getSourceBackgroundTask.acquire(urlString, userAgent, doNotTrack, localeString, proxy, this)));
87     }
88 }