AdGuard v3.6.45版本的 MD5 值为:ce30a13dcef92cf942973fffcc6d20fb
以下内容为反编译后的 k.java 源代码,内容仅作参考
package com.adguard.android.service;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.adguard.android.R;
import com.adguard.android.model.enums.Id;
import com.adguard.android.service.ao;
import com.adguard.android.service.ap;
import com.adguard.android.service.k;
import com.adguard.android.ui.dialog.Dialog;
import com.adguard.android.ui.dialog.DialogFactory;
import com.adguard.android.ui.dialog.impl.c;
import com.adguard.android.ui.other.EditableItem;
import com.adguard.android.ui.utils.h;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
public class k implements j {
private static final org.slf4j.c f309a = org.slf4j.d.a(k.class);
private final v b;
private final PreferencesService c;
private final com.adguard.android.db.h d;
private final com.adguard.android.db.d e;
private final f f;
private final Context g;
public class a {
String f310a;
int b;
int c;
boolean d;
a(String str, int i, int i2, boolean z) {
this.f310a = str;
this.b = i;
this.c = i2;
this.d = z;
}
}
public class b extends AsyncTask<Void, Void, a> {
private final WeakReference<Activity> b;
private final ProgressDialog c;
private final String d;
b(Activity activity, ProgressDialog progressDialog, String str) {
this.b = new WeakReference<>(activity);
this.c = progressDialog;
this.d = str;
}
private a a() {
Activity activity = this.b.get();
if (activity == null) {
return null;
}
k.f309a.info("Downloading DNS filter from {}", this.d);
try {
String b = com.adguard.android.api.b.a().b(activity, this.d);
return new a(b, k.this.q(), com.adguard.commons.c.a.a((CharSequence) b), a(b));
} catch (IOException e) {
k.f309a.error("Error downloading DNS filter from {}\n", this.d, e);
return null;
}
}
public void a(Activity activity, final Dialog.a aVar) {
com.adguard.android.ui.utils.h.a(activity, R.l.warningNotificationTitle, R.l.dns_filter_validation_warning, R.l.add, R.l.cancel, new h.b() {
@Override
public final void a() {
aVar.a();
}
});
}
public void a(EditableItem editableItem, a aVar, DialogInterface dialogInterface, int i) {
Editable text = editableItem.getText();
if (text != null && !StringUtils.isBlank(text.toString())) {
k.this.a(new com.adguard.android.model.filters.b(aVar.b, aVar.c, true, text.toString(), this.d, new Date()), aVar.f310a);
dialogInterface.dismiss();
return;
}
editableItem.setErrorText(R.l.custom_filter_dialog_title_error);
}
private static boolean a(String str) {
BufferedReader bufferedReader = null;
try {
try {
BufferedReader bufferedReader2 = new BufferedReader(new StringReader(str));
int i = 0;
while (true) {
bufferedReader = 80;
if (i >= 80) {
IOUtils.closeQuietly((Reader) bufferedReader2);
break;
}
try {
String readLine = bufferedReader2.readLine();
if (!StringUtils.startsWithAny(readLine, "<html>", "<header>", "<!DOCTYPE")) {
if (StringUtils.startsWithAny(readLine, "||", "@@||", "0.0.0.0", "127.0.0.1", "::") || com.adguard.commons.e.e.d(readLine)) {
break;
}
i++;
} else {
IOUtils.closeQuietly((Reader) bufferedReader2);
return false;
}
} catch (IOException e) {
e = e;
bufferedReader = bufferedReader2;
k.f309a.warn("An error occurred while DNS filter validation:", (Throwable) e);
IOUtils.closeQuietly((Reader) bufferedReader);
bufferedReader = bufferedReader;
return false;
} catch (Throwable th) {
th = th;
bufferedReader = bufferedReader2;
IOUtils.closeQuietly((Reader) bufferedReader);
throw th;
}
}
} catch (Throwable th2) {
th = th2;
}
} catch (IOException e2) {
e = e2;
}
}
@Override
protected final a doInBackground(Void[] voidArr) {
return a();
}
@Override
protected final void onPostExecute(a aVar) {
final a aVar2 = aVar;
com.adguard.android.ui.utils.q.a(this.c);
final Activity activity = this.b.get();
if (aVar2 != null && activity != null) {
if (com.adguard.android.a.a.a()) {
k.f309a.warn("Running in InstrumentedTest environment");
k.this.a(new com.adguard.android.model.filters.b(aVar2.b, aVar2.c, true, "TestFilter", this.d, new Date()), aVar2.f310a);
return;
}
View inflate = LayoutInflater.from(activity).inflate(R.g.dns_filter_add_dialog, (ViewGroup) null);
final EditableItem editableItem = (EditableItem) inflate.findViewById(R.f.filter_title);
editableItem.setHint(R.l.dns_filter_add_dialog_title);
DialogFactory.a.C0040a c0040a = (DialogFactory.a.C0040a) new DialogFactory.a.C0040a(this.b.get()).a(R.l.dns_filter_dialog_title);
((c.a) c0040a).f583a = inflate;
final Dialog.a c = ((DialogFactory.a.C0040a) c0040a.a(R.l.custom_filter_dialog_add, new DialogInterface.OnClickListener() {
@Override
public final void onClick(DialogInterface dialogInterface, int i) {
k.b.this.a(editableItem, aVar2, dialogInterface, i);
}
})).c();
boolean z = aVar2.f310a.length() > 5242880;
if (aVar2.d && !z) {
c.a();
return;
} else if (!z) {
a(activity, c);
return;
} else {
final boolean z2 = aVar2.d;
com.adguard.android.ui.utils.h.a(activity, R.l.dns_filter_size_validation_warning_title, R.l.dns_filter_size_validation_warning, R.l.add, R.l.cancel, new h.b() {
@Override
public final void a() {
if (z2) {
c.a();
} else {
b.this.a(activity, c);
}
}
});
return;
}
}
com.adguard.android.b.a(k.this.g).t.b(R.l.add_dns_filter_error);
}
}
public k(Context context, PreferencesService preferencesService, v vVar, f fVar) {
f309a.info("Creating DnsFilterService instance for {}", context);
this.d = new com.adguard.android.db.j(context, "dns_filters");
this.c = preferencesService;
this.b = vVar;
com.adguard.android.db.e eVar = new com.adguard.android.db.e(preferencesService);
this.e = eVar;
this.f = fVar;
this.g = context;
if (CollectionUtils.isEmpty(eVar.a())) {
o();
}
}
public void a(String str, String str2, boolean z) {
if (StringUtils.isEmpty(str)) {
f309a.error("Error downloading DNS user rules from {}", str2);
this.b.b(R.l.importDnsUserRulesErrorResultMessage);
return;
}
String[] split = StringUtils.split(str, "\n");
f309a.info("{} DNS user rules downloaded from {}", Integer.valueOf(split.length), str2);
ArrayList arrayList = new ArrayList(split.length);
for (String str3 : split) {
String trim = str3.trim();
if (trim.length() < 1000) {
arrayList.add(trim);
}
}
if (arrayList.size() > 0 && !((String) arrayList.get(0)).contains("\u0000")) {
String join = StringUtils.join(arrayList, "\n");
if (!z) {
join = this.c.i() + "\n" + join;
}
a(join);
if (z) {
this.c.c(new HashSet());
}
f309a.info("DNS user rules added successfully.");
this.b.a(arrayList, R.l.importUserRulesSuccessResultMessage);
com.adguard.android.events.b.a().b();
return;
}
f309a.error("Invalid DNS user rules from {}", str2);
this.b.b(R.l.importDnsUserRulesErrorResultMessage);
}
public static boolean a(com.adguard.android.model.filters.b bVar, com.adguard.android.model.filters.b bVar2) {
return bVar2.getFilterId() == bVar.getFilterId();
}
private boolean b(com.adguard.android.model.filters.b bVar) {
try {
return c(bVar);
} catch (IOException e) {
f309a.warn("Error while downloading DNS filter updates\n", (Throwable) e);
com.adguard.android.b.a(this.g).t.c(this.g.getString(R.l.update_dns_filter_error, bVar.getName()));
return false;
}
}
private boolean c(final int i) {
if (this.d.c(i)) {
return false;
}
com.adguard.commons.concurrent.c.a("dns_filter-update-".concat(String.valueOf(i)), 10L, TimeUnit.SECONDS, new Runnable() {
@Override
public final void run() {
k.this.d(i);
}
});
return true;
}
private boolean c(com.adguard.android.model.filters.b bVar) {
if (com.adguard.android.model.filters.a.isValidSubscriptionUrl(bVar.getSubscriptionUrl())) {
int filterId = bVar.getFilterId();
String subscriptionUrl = bVar.getSubscriptionUrl();
String b2 = com.adguard.android.api.b.a().b(this.g, subscriptionUrl);
if (StringUtils.isBlank(b2)) {
throw new IOException(String.format("Cannot download filter rules for filter %s with id %d with %s", bVar.getName(), Integer.valueOf(filterId), subscriptionUrl));
}
if (StringUtils.equals(b2, this.d.b(filterId))) {
f309a.info("DNS filter {} content doesn't changed. Doing nothing", Integer.valueOf(filterId));
return false;
}
f309a.info("Updating rules for the DNS filter {} with {}", Integer.valueOf(filterId), subscriptionUrl);
bVar.setRulesCount(com.adguard.commons.c.a.a((CharSequence) b2));
bVar.setLastTimeDownloaded(new Date());
a(bVar, b2);
return true;
}
return false;
}
public void d(int i) {
if (b(a(i))) {
com.adguard.android.events.b.a().b();
}
}
private void o() {
f309a.info("Initializing SDN filter from the app assets");
this.e.a(Collections.singletonList(new com.adguard.android.model.filters.b(15, com.adguard.commons.c.a.a((CharSequence) this.d.b(15)), true, this.g.getString(R.l.adguard_sdn_filter_name), "https://filters.adtidy.org/android/filters/15_optimized.txt", new Date())));
f309a.info("Finished initializing filters from the app assets");
}
private Set<Integer> p() {
HashSet hashSet = new HashSet();
List<com.adguard.android.model.filters.b> a2 = this.e.a();
if (CollectionUtils.isNotEmpty(a2)) {
for (com.adguard.android.model.filters.b bVar : a2) {
if (bVar.isEnabled()) {
hashSet.add(Integer.valueOf(bVar.getFilterId()));
}
}
}
return hashSet;
}
public synchronized int q() {
int i;
i = com.adguard.android.model.dns.f.AUTOMATION_SERVER_ID;
try {
for (com.adguard.android.model.filters.b bVar : b()) {
if (bVar.getFilterId() >= i) {
i = bVar.getFilterId() + 1;
}
}
} catch (Throwable th) {
throw th;
}
return i;
}
@Override
public final com.adguard.android.filtering.dns.a a() {
HashMap hashMap = new HashMap();
for (Integer num : p()) {
if (!c(num.intValue())) {
hashMap.put(num, this.d.a(num.intValue()));
}
}
if (e()) {
this.d.a(-3, m());
hashMap.put(-3, this.d.a(-3));
}
return new com.adguard.android.filtering.dns.a(hashMap);
}
@Override
public final com.adguard.android.model.filters.b a(int i) {
return this.e.a(i);
}
@Override
public final com.adguard.android.model.k a(boolean z, boolean z2) {
com.adguard.android.model.k kVar = new com.adguard.android.model.k(Id.DNS_FILTERS);
if (z || this.f.i()) {
f309a.info("Start DNS filters update");
List<com.adguard.android.model.filters.b> a2 = this.e.a();
int i = 0;
int i2 = 0;
for (com.adguard.android.model.filters.b bVar : a2) {
try {
if (bVar.isEnabled() && c(bVar)) {
i2++;
kVar.a(new com.adguard.android.model.j(bVar.getName(), true));
}
} catch (IOException e) {
i++;
f309a.warn("Error while downloading DNS filter updates\n", (Throwable) e);
kVar.a(new com.adguard.android.model.j(bVar.getName(), false));
}
}
if (i == a2.size() && i > 0) {
f309a.error("Failed to update all DNS filters");
}
if (i2 > 0) {
f309a.info("{} DNS filters updated", Integer.valueOf(i2));
if (z2) {
com.adguard.android.events.b.a().b();
}
}
f309a.info("Finish DNS filters update");
return kVar.a();
}
return kVar;
}
@Override
public final void a(Activity activity, ProgressDialog progressDialog, Uri uri) {
f309a.info("Start export user rules to {}", uri);
com.adguard.commons.concurrent.b.b(new ap.b(activity, new ap.a() {
@Override
public final String getUserRules() {
return k.this.f();
}
}, progressDialog, uri, R.l.exportDnsUserRulesSuccessResultMessage, R.l.exportDnsUserRulesErrorResultMessage));
f309a.info("Submitted import DNS user rules task");
}
@Override
public final void a(Activity activity, ProgressDialog progressDialog, String str) {
f309a.info("Start import DNS filter from {}", str);
new b(activity, progressDialog, str).execute(new Void[0]);
f309a.info("Submitted import DNS filter task");
}
@Override
public final void a(ProgressDialog progressDialog, String str, boolean z) {
f309a.info("Start import DNS user rules from {}", str);
com.adguard.commons.concurrent.b.b(new ap.d(this.g, new ap.c() {
@Override
public final void importRules(String str2, String str3, boolean z2) {
k.this.a(str2, str3, z2);
}
}, progressDialog, str, z, R.l.importDnsUserRulesErrorResultMessage));
f309a.info("Submitted import DNS user rules task");
}
@Override
public final void a(com.adguard.android.model.filters.b bVar) {
this.e.a(bVar);
com.adguard.android.events.b.a().b();
}
public final synchronized void a(final com.adguard.android.model.filters.b bVar, String str) {
try {
List<com.adguard.android.model.filters.b> a2 = this.e.a();
com.adguard.android.model.filters.b bVar2 = (com.adguard.android.model.filters.b) IterableUtils.find(a2, new Predicate() {
@Override
public final boolean evaluate(Object obj) {
boolean a3;
a3 = k.a(com.adguard.android.model.filters.b.this, (com.adguard.android.model.filters.b) obj);
return a3;
}
});
if (bVar2 != null) {
a2.remove(bVar2);
}
a2.add(bVar);
this.e.a(a2);
this.d.a(bVar.getFilterId(), str);
com.adguard.android.events.b.a().b();
} catch (Throwable th) {
throw th;
}
}
@Override
public final void a(com.adguard.android.model.filters.b bVar, boolean z) {
if (bVar.isEnabled() == z) {
return;
}
bVar.setEnabled(z);
int filterId = bVar.getFilterId();
this.e.a(filterId, z);
if (!c(bVar.getFilterId())) {
com.adguard.android.events.b.a().a(filterId, z);
}
}
@Override
public void a(String str) {
this.c.c(str);
this.c.g(com.adguard.commons.c.a.a(str, "\n", true).size());
com.adguard.android.events.b.a().b();
}
@Override
public final void a(boolean z) {
this.c.w(z);
}
@Override
public final boolean a(String str, String str2) {
a aVar;
try {
String b2 = com.adguard.android.api.b.a().b(this.g, str);
aVar = new a(b2, q(), com.adguard.commons.c.a.a((CharSequence) b2), true);
} catch (IOException e) {
f309a.error("Error downloading DNS filter from {}\n", str, e);
aVar = null;
}
if (aVar == null) {
com.adguard.android.b.a(this.g).t.b(R.l.add_dns_filter_error);
return false;
}
a(new com.adguard.android.model.filters.b(aVar.b, aVar.c, true, str2, str, new Date()), aVar.f310a);
return true;
}
@Override
public final String b(int i) {
if (i == -3) {
return this.g.getString(R.l.dns_user_filter);
}
for (com.adguard.android.model.filters.b bVar : this.e.a()) {
if (bVar.getFilterId() == i) {
return bVar.getName();
}
}
return null;
}
@Override
public final List<com.adguard.android.model.filters.b> b() {
return this.e.a();
}
@Override
public final void b(String str) {
List<String> g = g();
if (g.remove(str)) {
a(StringUtils.join(g, "\n"));
}
}
@Override
public final int c() {
return p().size() + (e() ? 1 : 0);
}
@Override
public final void c(String str) {
Set<String> k = this.c.k();
if (k.add(str)) {
this.c.c(k);
}
}
@Override
public final void d() {
o();
com.adguard.android.events.b.a().b();
}
@Override
public final void d(String str) {
Set<String> k = this.c.k();
if (k.remove(str)) {
this.c.c(k);
}
}
@Override
public final boolean e() {
return this.c.am();
}
@Override
public String f() {
return this.c.i();
}
@Override
public final List<String> g() {
return com.adguard.commons.c.a.a(f(), "\n", false);
}
@Override
public final int h() {
return this.c.af();
}
@Override
public final void i() {
a("");
this.c.c(new HashSet());
}
@Override
public final Set<String> j() {
return this.c.k();
}
@Override
public final void k() {
this.c.c(new HashSet());
}
@Override
public void k(String str) {
ao.CC.$default$k(this, str);
}
@Override
public final void l() {
for (String str : com.adguard.commons.c.a.a(m(), "\r\n", false)) {
c(str);
}
}
@Override
public final String m() {
List<String> g = g();
f309a.info("Got {} rules from DNS user filter", Integer.valueOf(g.size()));
Set<String> j = j();
f309a.info("Got {} disabled DNS user rules", Integer.valueOf(j.size()));
ListIterator<String> listIterator = g.listIterator();
while (listIterator.hasNext()) {
if (j.contains(listIterator.next())) {
listIterator.remove();
}
}
return StringUtils.join(g, "\n");
}
}