Adguard: download list

pull/214/head
M66B 7 months ago
parent b78edd89a9
commit f45159b13c

File diff suppressed because it is too large Load Diff

@ -25,19 +25,34 @@ import android.text.TextUtils;
import androidx.annotation.Nullable;
import org.json.JSONException;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
public class Adguard {
// https://github.com/AdguardTeam/AdguardFilters
// https://github.com/AdguardTeam/FiltersRegistry/blob/master/filters/filter_17_TrackParam/filter.txt
// https://github.com/AdguardTeam/TestCases/tree/master/public/Filters/removeparam-rules
private final static int FETCH_TIMEOUT = 20 * 1000; // milliseconds
private final static String LIST = "https://raw.githubusercontent.com/AdguardTeam/FiltersRegistry/master/filters/filter_17_TrackParam/filter.txt";
private static final List<String> ALL_CONTENT = Collections.unmodifiableList(Arrays.asList(
"document",
"subdocument",
@ -63,12 +78,15 @@ public class Adguard {
if (TextUtils.isEmpty(host))
return null;
File file = getFile(context);
if (!file.exists())
return null;
List<String> removes = new ArrayList<>();
List<String> importants = new ArrayList<>();
List<String> excepts = new ArrayList<>();
try (BufferedReader br = new BufferedReader(
new InputStreamReader(context.getAssets().open("adguard_filter.txt")))) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
String line;
while ((line = br.readLine()) != null) {
// https://adguard.com/kb/general/ad-filtering/create-own-filters/#comments
@ -364,4 +382,33 @@ public class Adguard {
return false;
}
static void download(Context context) throws IOException, JSONException {
File file = getFile(context);
URL url = new URL(LIST);
Log.i("GET " + url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(FETCH_TIMEOUT);
connection.setConnectTimeout(FETCH_TIMEOUT);
ConnectionHelper.setUserAgent(context, connection);
connection.connect();
try {
int status = connection.getResponseCode();
if (status != HttpsURLConnection.HTTP_OK)
throw new FileNotFoundException("Error " + status + ": " + connection.getResponseMessage());
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
Helper.copy(connection.getInputStream(), os);
}
} finally {
connection.disconnect();
}
}
private static File getFile(Context context) {
return new File(context.getFilesDir(), "adguard.txt");
}
}

@ -226,7 +226,33 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
btnAdguard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prefs.edit().putLong("adguard_last", new Date().getTime()).apply();
new SimpleTask<Void>() {
@Override
protected void onPreExecute(Bundle args) {
btnAdguard.setEnabled(false);
}
@Override
protected void onPostExecute(Bundle args) {
btnAdguard.setEnabled(true);
}
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
Adguard.download(context);
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
prefs.edit().putLong("adguard_last", new Date().getTime()).apply();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex, !(ex instanceof IOException));
}
}.execute(FragmentOptionsPrivacy.this, new Bundle(), "adguard");
}
});
@ -486,7 +512,6 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
@Override
protected void onExecuted(Bundle args, Void data) {
prefs.edit().putLong("disconnect_last", new Date().getTime()).apply();
setOptions();
}
@Override

Loading…
Cancel
Save