Permanent favicon blacklisting

pull/183/head
M66B 5 years ago
parent 2c8137d31d
commit 19fc6eb827

@ -50,10 +50,8 @@ import java.net.HttpURLConnection;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -74,7 +72,6 @@ public class ContactInfo {
private static Map<String, Lookup> emailLookup = new ConcurrentHashMap<>(); private static Map<String, Lookup> emailLookup = new ConcurrentHashMap<>();
private static final Map<String, ContactInfo> emailContactInfo = new HashMap<>(); private static final Map<String, ContactInfo> emailContactInfo = new HashMap<>();
private static final Map<String, Avatar> emailGravatar = new HashMap<>(); private static final Map<String, Avatar> emailGravatar = new HashMap<>();
private static final List<String> emailFaviconBlacklist = new ArrayList<>();
private static final ExecutorService executor = private static final ExecutorService executor =
Helper.getBackgroundExecutor(1, "contact"); Helper.getBackgroundExecutor(1, "contact");
@ -119,10 +116,26 @@ public class ContactInfo {
return (new Date().getTime() - time > CACHE_CONTACT_DURATION); return (new Date().getTime() - time > CACHE_CONTACT_DURATION);
} }
static void clearCache() { static void clearCache(Context context) {
synchronized (emailContactInfo) { synchronized (emailContactInfo) {
emailContactInfo.clear(); emailContactInfo.clear();
} }
final File dir = new File(context.getCacheDir(), "favicons");
executor.submit(new Runnable() {
@Override
public void run() {
try {
File[] favicons = dir.listFiles();
if (favicons != null)
for (File favicon : favicons)
favicon.delete();
} catch (Throwable ex) {
Log.w(ex);
}
}
});
} }
@NonNull @NonNull
@ -265,25 +278,22 @@ public class ContactInfo {
int at = (info.email == null ? -1 : info.email.indexOf('@')); int at = (info.email == null ? -1 : info.email.indexOf('@'));
String domain = (at < 0 ? null : info.email.substring(at + 1).toLowerCase(Locale.ROOT)); String domain = (at < 0 ? null : info.email.substring(at + 1).toLowerCase(Locale.ROOT));
synchronized (emailFaviconBlacklist) {
if (emailFaviconBlacklist.contains(domain)) {
Log.i("Favicon blacklisted domain=" + domain);
domain = null;
}
}
if (domain != null) { if (domain != null) {
if ("gmail.com".equals(domain)) if ("gmail.com".equals(domain))
domain = "google.com"; domain = "google.com";
File dir = new File(context.getCacheDir(), "favicons");
if (!dir.exists())
dir.mkdir();
File file = new File(dir, domain);
try { try {
// check cache // check cache
File dir = new File(context.getCacheDir(), "favicons");
if (!dir.exists())
dir.mkdir();
File file = new File(dir, domain);
if (file.exists()) if (file.exists())
info.bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); if (file.length() == 0)
Log.i("Favicon blacklisted domain=" + domain);
else
info.bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
else { else {
info.bitmap = getFavicon(new URL("https://" + domain)); info.bitmap = getFavicon(new URL("https://" + domain));
if (info.bitmap == null) if (info.bitmap == null)
@ -299,8 +309,10 @@ public class ContactInfo {
Log.w(ex); Log.w(ex);
} finally { } finally {
if (info.bitmap == null) if (info.bitmap == null)
synchronized (emailFaviconBlacklist) { try {
emailFaviconBlacklist.add(domain); file.createNewFile();
} catch (IOException ex) {
Log.e(ex);
} }
} }
} }

@ -334,7 +334,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("avatars", checked).apply(); prefs.edit().putBoolean("avatars", checked).apply();
ContactInfo.clearCache(); ContactInfo.clearCache(getContext());
} }
}); });
@ -342,7 +342,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("gravatars", checked).apply(); prefs.edit().putBoolean("gravatars", checked).apply();
ContactInfo.clearCache(); ContactInfo.clearCache(getContext());
} }
}); });
@ -350,7 +350,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("favicons", checked).apply(); prefs.edit().putBoolean("favicons", checked).apply();
ContactInfo.clearCache(); ContactInfo.clearCache(getContext());
} }
}); });
@ -370,7 +370,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
sbSaturation.setEnabled(checked); sbSaturation.setEnabled(checked);
sbBrightness.setEnabled(checked); sbBrightness.setEnabled(checked);
sbThreshold.setEnabled(checked); sbThreshold.setEnabled(checked);
ContactInfo.clearCache(); ContactInfo.clearCache(getContext());
} }
}); });
@ -378,7 +378,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("identicons", checked).apply(); prefs.edit().putBoolean("identicons", checked).apply();
ContactInfo.clearCache(); ContactInfo.clearCache(getContext());
} }
}); });
@ -387,7 +387,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("circular", checked).apply(); prefs.edit().putBoolean("circular", checked).apply();
updateColor(); updateColor();
ContactInfo.clearCache(); ContactInfo.clearCache(getContext());
} }
}); });
@ -396,7 +396,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
prefs.edit().putInt("saturation", progress).apply(); prefs.edit().putInt("saturation", progress).apply();
updateColor(); updateColor();
ContactInfo.clearCache(); ContactInfo.clearCache(getContext());
} }
@Override @Override
@ -415,7 +415,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
prefs.edit().putInt("brightness", progress).apply(); prefs.edit().putInt("brightness", progress).apply();
updateColor(); updateColor();
ContactInfo.clearCache(); ContactInfo.clearCache(getContext());
} }
@Override @Override
@ -434,7 +434,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
prefs.edit().putInt("threshold", progress).apply(); prefs.edit().putInt("threshold", progress).apply();
updateColor(); updateColor();
ContactInfo.clearCache(); ContactInfo.clearCache(getContext());
} }
@Override @Override
@ -1023,7 +1023,7 @@ public class FragmentOptionsDisplay extends FragmentBase implements SharedPrefer
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
getActivity().getIntent().putExtra("tab", "display"); getActivity().getIntent().putExtra("tab", "display");
ContactInfo.clearCache(); ContactInfo.clearCache(getContext());
boolean reverse = (swReverse.isEnabled() && swReverse.isChecked()); boolean reverse = (swReverse.isEnabled() && swReverse.isChecked());
boolean dark = (swDark.isEnabled() && swDark.isChecked()); boolean dark = (swDark.isEnabled() && swDark.isChecked());

@ -124,7 +124,7 @@ public class WorkerCleanup extends Worker {
ServiceSynchronize.reschedule(context); ServiceSynchronize.reschedule(context);
// Contact info cache // Contact info cache
ContactInfo.clearCache(); ContactInfo.clearCache(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.i("Checking notification channels"); Log.i("Checking notification channels");

Loading…
Cancel
Save