From e8c1e1797537630d5506a0563debd1a3db5b6f6b Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 13 Jan 2019 13:22:17 +0000 Subject: [PATCH] Added option to append to hosts file --- ADBLOCKING.md | 1 - .../faircode/netguard/ActivitySettings.java | 23 +++++++++++++++---- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml-v14/preferences.xml | 3 +++ app/src/main/res/xml-v21/preferences.xml | 3 +++ 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/ADBLOCKING.md b/ADBLOCKING.md index 204e0236..10efb5c1 100644 --- a/ADBLOCKING.md +++ b/ADBLOCKING.md @@ -30,7 +30,6 @@ Note that: * applications, browsers mostly, that have a *"data saver"*-like feature that proxies requests through their servers (eg. Opera w/ Turbo, Opera Max, Puffin, Chrome w/ data saver, UC Browser, Yandex w/ Turbo, Apus Browser, KK Browser, Onavo Extend, Maxthon) will not have ads blocked as NetGuard cannot see those domain requests * YouTube ads are not domain-based, and thus cannot be blocked with NetGuard * NetGuard ignores the IP addresses in the hosts file, because it does not route blocked domains to localhost -* NetGuard does not concatenate hosts files, so you will have to use a source which does this for you or do it yourself * When NetGuard imports the hosts file, it automatically discards any duplicates entries, so duplicate entries are not a problem and have no performance impact after the file is imported * you can check the number of hosts (domains) imported by pulling the NetGuard notification down using two fingers if your version of Android supports that functionality * wildcards are not supported due to performance and battery usage reasons diff --git a/app/src/main/java/eu/faircode/netguard/ActivitySettings.java b/app/src/main/java/eu/faircode/netguard/ActivitySettings.java index 638b0266..bd533793 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivitySettings.java +++ b/app/src/main/java/eu/faircode/netguard/ActivitySettings.java @@ -100,7 +100,8 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere private static final int REQUEST_EXPORT = 1; private static final int REQUEST_IMPORT = 2; private static final int REQUEST_HOSTS = 3; - private static final int REQUEST_CALL = 4; + private static final int REQUEST_HOSTS_APPEND = 4; + private static final int REQUEST_CALL = 5; private AlertDialog dialogFilter = null; @@ -300,6 +301,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere Preference pref_block_domains = screen.findPreference("use_hosts"); EditTextPreference pref_rcode = (EditTextPreference) screen.findPreference("rcode"); Preference pref_hosts_import = screen.findPreference("hosts_import"); + Preference pref_hosts_import_append = screen.findPreference("hosts_import_append"); EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url"); final Preference pref_hosts_download = screen.findPreference("hosts_download"); @@ -312,6 +314,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere cat_advanced.removePreference(pref_rcode); cat_advanced.removePreference(pref_forwarding); cat_backup.removePreference(pref_hosts_import); + cat_backup.removePreference(pref_hosts_import_append); cat_backup.removePreference(pref_hosts_url); cat_backup.removePreference(pref_hosts_download); @@ -333,6 +336,14 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere return true; } }); + pref_hosts_import_append.setEnabled(pref_hosts_import.isEnabled()); + pref_hosts_import_append.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + startActivityForResult(getIntentOpenHosts(), ActivitySettings.REQUEST_HOSTS_APPEND); + return true; + } + }); // Handle hosts file download pref_hosts_url.setSummary(pref_hosts_url.getText()); @@ -827,7 +838,11 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere } else if (requestCode == REQUEST_HOSTS) { if (resultCode == RESULT_OK && data != null) - handleHosts(data); + handleHosts(data, false); + + } else if (requestCode == REQUEST_HOSTS_APPEND) { + if (resultCode == RESULT_OK && data != null) + handleHosts(data, true); } else { Log.w(TAG, "Unknown activity result request=" + requestCode); @@ -913,7 +928,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } - private void handleHosts(final Intent data) { + private void handleHosts(final Intent data, final boolean append) { new AsyncTask() { @Override protected Throwable doInBackground(Object... objects) { @@ -928,7 +943,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere String streamType = (streamTypes == null || streamTypes.length == 0 ? "*/*" : streamTypes[0]); AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(data.getData(), streamType, null); in = descriptor.createInputStream(); - out = new FileOutputStream(hosts); + out = new FileOutputStream(hosts, append); int len; long total = 0; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7050405c..06f07fce 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -129,6 +129,7 @@ Export settings Import settings Import hosts file + Import hosts file (append) Hosts file download URL Download hosts file diff --git a/app/src/main/res/xml-v14/preferences.xml b/app/src/main/res/xml-v14/preferences.xml index 596ba8dd..7914e28e 100644 --- a/app/src/main/res/xml-v14/preferences.xml +++ b/app/src/main/res/xml-v14/preferences.xml @@ -314,6 +314,9 @@ + +