Cached disconnect list

pull/184/head
M66B 5 years ago
parent 846c38f9c9
commit 0dad43cfca

@ -5957,36 +5957,11 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
uriTitle.getHost().equalsIgnoreCase(uri.getHost())
? View.GONE : View.VISIBLE);
Bundle args = new Bundle();
args.putParcelable("uri", uri);
new SimpleTask<List<String>>() {
@Override
protected void onPreExecute(Bundle args) {
tvDisconnect.setVisibility(View.GONE);
tvDisconnectCategories.setVisibility(View.GONE);
}
@Override
protected List<String> onExecute(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri");
return DisconnectBlacklist.getCategories(uri.getHost(), context);
}
@Override
protected void onExecuted(Bundle args, List<String> data) {
if (data != null) {
tvDisconnectCategories.setText(TextUtils.join(", ", data));
tvDisconnect.setVisibility(View.VISIBLE);
tvDisconnectCategories.setVisibility(View.VISIBLE);
}
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(getContext(), getViewLifecycleOwner(), args, "disconnect");
List<String> categories = DisconnectBlacklist.getCategories(uri.getHost());
if (categories != null)
tvDisconnectCategories.setText(TextUtils.join(", ", categories));
tvDisconnect.setVisibility(categories == null ? View.GONE : View.VISIBLE);
tvDisconnectCategories.setVisibility(categories == null ? View.GONE : View.VISIBLE);
final Context context = getContext();

@ -114,6 +114,8 @@ public class ApplicationEx extends Application {
MessageHelper.setSystemProperties(this);
ContactInfo.init(this);
DisconnectBlacklist.init(this);
WorkerWatchdog.init(this);
WorkerCleanup.queue(this);

@ -21,6 +21,7 @@ package eu.faircode.email;
import android.content.Context;
import android.net.Uri;
import android.os.SystemClock;
import org.json.JSONArray;
import org.json.JSONException;
@ -31,43 +32,42 @@ import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import javax.net.ssl.HttpsURLConnection;
public class DisconnectBlacklist {
private static final Map<String, List<String>> map = new HashMap<>();
private static final ExecutorService executor = Helper.getBackgroundExecutor(1, "disconnect");
private final static int FETCH_TIMEOUT = 20 * 1000; // milliseconds
private final static String LIST = "https://raw.githubusercontent.com/disconnectme/disconnect-tracking-protection/master/services.json";
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);
connection.connect();
static void init(Context context) {
final File file = getFile(context);
executor.submit(new Runnable() {
@Override
public void run() {
try {
String response = Helper.readStream(connection.getInputStream(), StandardCharsets.UTF_8.name());
Helper.writeText(file, response);
} finally {
connection.disconnect();
init(file);
} catch (Throwable ex) {
Log.e(ex);
}
}
});
}
static List<String> getCategories(String domain, Context context) throws IOException, JSONException {
if (domain == null)
return null;
File file = getFile(context);
if (!file.exists())
return null;
private static void init(File file) throws IOException, JSONException {
synchronized (map) {
long start = SystemClock.elapsedRealtime();
List<String> result = new ArrayList<>();
map.clear();
String json = Helper.readText(file);
JSONObject jdisconnect = new JSONObject(json);
@ -89,21 +89,58 @@ public class DisconnectBlacklist {
String site = sites.next();
String host = Uri.parse(site).getHost();
if (host != null)
domains.add(host);
domains.add(host.toLowerCase(Locale.ROOT));
JSONArray jdomains = jsites.getJSONArray(site);
for (int d = 0; d < jdomains.length(); d++)
domains.add(jdomains.getString(d));
domains.add(jdomains.getString(d).toLowerCase(Locale.ROOT));
for (String domain : domains) {
if (!map.containsKey(domain))
map.put(domain, new ArrayList<>());
List<String> list = map.get(domain);
if (!list.contains(category))
list.add(category);
}
}
}
}
}
for (String d : domains)
if (domain.equalsIgnoreCase(d) && !result.contains(category))
result.add(category);
long elapsed = SystemClock.elapsedRealtime() - start;
Log.i("Disconnect domains=" + map.size() + " elapsed=" + elapsed + " ms");
}
}
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);
connection.connect();
try {
String response = Helper.readStream(connection.getInputStream(), StandardCharsets.UTF_8.name());
Helper.writeText(file, response);
} finally {
connection.disconnect();
}
init(file);
}
return (result.size() == 0 ? null : result);
static List<String> getCategories(String domain) {
if (domain == null)
return null;
synchronized (map) {
List<String> result = map.get(domain.toLowerCase(Locale.ROOT));
return (result == null || result.size() == 0 ? null : result);
}
}
private static File getFile(Context context) {

Loading…
Cancel
Save