@@ -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"));
0 commit comments