Skip to content

Commit 5db25a4

Browse files
committed
fix crashes
1 parent bcbd537 commit 5db25a4

2 files changed

Lines changed: 66 additions & 50 deletions

File tree

app/src/main/java/eu/faircode/netguard/ActivitySettings.java

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -317,61 +317,68 @@ public boolean onPreferenceClick(Preference preference) {
317317
}
318318

319319
String last_download = prefs.getString("hosts_last_download", null);
320-
if (last_download != null)
321-
pref_hosts_download.setSummary(getString(R.string.msg_update_last, last_download));
322-
323-
pref_hosts_download.setOnPreferenceClickListener(preference -> {
324-
Constraints constraints = new Constraints.Builder()
325-
.setRequiredNetworkType(NetworkType.CONNECTED)
326-
.build();
327-
328-
OneTimeWorkRequest downloadWork = new OneTimeWorkRequest.Builder(HostsDownloadWorker.class)
329-
.setConstraints(constraints)
330-
.addTag("ManualHostsDownload")
331-
.build();
332-
WorkManager.getInstance(ActivitySettings.this).enqueue(downloadWork);
333-
return true;
334-
});
335-
336-
WorkManager.getInstance(this).getWorkInfosByTagLiveData("ManualHostsDownload").observe(this, workInfos -> {
337-
if (workInfos != null && !workInfos.isEmpty()) {
338-
androidx.work.WorkInfo workInfo = workInfos.get(0);
339-
if (workInfo.getState() == androidx.work.WorkInfo.State.SUCCEEDED) {
340-
String last = SimpleDateFormat.getDateTimeInstance().format(new Date().getTime());
341-
pref_hosts_download.setSummary(getString(R.string.msg_update_last, last));
320+
if (pref_hosts_download != null) {
321+
if (last_download != null)
322+
pref_hosts_download.setSummary(getString(R.string.msg_update_last, last_download));
323+
324+
pref_hosts_download.setOnPreferenceClickListener(preference -> {
325+
Constraints constraints = new Constraints.Builder()
326+
.setRequiredNetworkType(NetworkType.CONNECTED)
327+
.build();
328+
329+
OneTimeWorkRequest downloadWork = new OneTimeWorkRequest.Builder(HostsDownloadWorker.class)
330+
.setConstraints(constraints)
331+
.addTag("ManualHostsDownload")
332+
.build();
333+
WorkManager.getInstance(ActivitySettings.this).enqueue(downloadWork);
334+
return true;
335+
});
336+
337+
WorkManager.getInstance(this).getWorkInfosByTagLiveData("ManualHostsDownload").observe(this, workInfos -> {
338+
if (workInfos != null && !workInfos.isEmpty()) {
339+
androidx.work.WorkInfo workInfo = workInfos.get(0);
340+
if (workInfo.getState() == androidx.work.WorkInfo.State.SUCCEEDED) {
341+
String last = SimpleDateFormat.getDateTimeInstance().format(new Date().getTime());
342+
pref_hosts_download.setSummary(getString(R.string.msg_update_last, last));
343+
}
342344
}
343-
}
344-
});
345+
});
346+
}
345347

346348
eu.faircode.netguard.SwitchPreference pref_hosts_auto_update = (eu.faircode.netguard.SwitchPreference) screen
347349
.findPreference("hosts_auto_update");
348-
pref_hosts_auto_update.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
349-
@Override
350-
public boolean onPreferenceChange(Preference preference, Object newValue) {
351-
boolean enabled = (Boolean) newValue;
352-
if (enabled) {
353-
Constraints constraints = new Constraints.Builder()
354-
.setRequiredNetworkType(NetworkType.CONNECTED)
355-
.build();
356-
357-
PeriodicWorkRequest saveRequest = new PeriodicWorkRequest.Builder(HostsDownloadWorker.class, 24,
358-
TimeUnit.HOURS)
359-
.setConstraints(constraints)
360-
.build();
361-
WorkManager.getInstance(ActivitySettings.this).enqueueUniquePeriodicWork(
362-
"HostsUpdate",
363-
ExistingPeriodicWorkPolicy.UPDATE,
364-
saveRequest);
365-
} else {
366-
WorkManager.getInstance(ActivitySettings.this).cancelUniqueWork("HostsUpdate");
350+
if (pref_hosts_auto_update != null) {
351+
pref_hosts_auto_update.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
352+
@Override
353+
public boolean onPreferenceChange(Preference preference, Object newValue) {
354+
boolean enabled = (Boolean) newValue;
355+
if (enabled) {
356+
Constraints constraints = new Constraints.Builder()
357+
.setRequiredNetworkType(NetworkType.CONNECTED)
358+
.build();
359+
360+
PeriodicWorkRequest saveRequest = new PeriodicWorkRequest.Builder(HostsDownloadWorker.class, 24,
361+
TimeUnit.HOURS)
362+
.setConstraints(constraints)
363+
.build();
364+
WorkManager.getInstance(ActivitySettings.this).enqueueUniquePeriodicWork(
365+
"HostsUpdate",
366+
ExistingPeriodicWorkPolicy.UPDATE,
367+
saveRequest);
368+
} else {
369+
WorkManager.getInstance(ActivitySettings.this).cancelUniqueWork("HostsUpdate");
370+
}
371+
return true;
367372
}
368-
return true;
369-
}
370-
});
373+
});
374+
}
371375

372376
// Development
373-
if (!Util.isDebuggable(this))
374-
screen.removePreference(screen.findPreference("screen_development"));
377+
if (!Util.isDebuggable(this)) {
378+
Preference screenDevelopment = screen.findPreference("screen_development");
379+
if (screenDevelopment != null)
380+
screen.removePreference(screenDevelopment);
381+
}
375382

376383
/*
377384
* cat_network.removePreference(screen.findPreference("use_metered"));

app/src/main/java/net/kollnig/missioncontrol/dns/DnsOverHttpsClient.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.io.IOException;
2424
import java.util.Objects;
25+
import java.util.concurrent.ExecutorService;
26+
import java.util.concurrent.Executors;
2527
import java.util.concurrent.TimeUnit;
2628

2729
import okhttp3.ConnectionPool;
@@ -43,6 +45,11 @@ public class DnsOverHttpsClient {
4345
private static final int CONNECT_TIMEOUT_MS = 5000;
4446
private static final int READ_TIMEOUT_MS = 5000;
4547
private static final int WRITE_TIMEOUT_MS = 5000;
48+
private static final ExecutorService SHUTDOWN_EXECUTOR = Executors.newSingleThreadExecutor(r -> {
49+
Thread thread = new Thread(r, "DoH-shutdown");
50+
thread.setDaemon(true);
51+
return thread;
52+
});
4653
private static DnsOverHttpsClient instance;
4754
private final OkHttpClient client;
4855
private final String endpoint;
@@ -95,8 +102,10 @@ public static synchronized void resetInstance() {
95102
*/
96103
public void shutdown() {
97104
Log.i(TAG, "Shutting down DoH client");
98-
client.dispatcher().cancelAll();
99-
new Thread(() -> client.connectionPool().evictAll(), "DoH-shutdown").start();
105+
SHUTDOWN_EXECUTOR.execute(() -> {
106+
client.dispatcher().cancelAll();
107+
client.connectionPool().evictAll();
108+
});
100109
}
101110

102111
/**

0 commit comments

Comments
 (0)