Added option to append to hosts file

This commit is contained in:
M66B 2019-01-13 13:22:17 +00:00
parent 5b23f1c4f7
commit e8c1e17975
5 changed files with 26 additions and 5 deletions

View File

@ -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

View File

@ -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<Object, Object, Throwable>() {
@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;

View File

@ -129,6 +129,7 @@
<string name="setting_export">Export settings</string>
<string name="setting_import">Import settings</string>
<string name="setting_hosts">Import hosts file</string>
<string name="setting_hosts_append">Import hosts file (append)</string>
<string name="setting_hosts_url">Hosts file download URL</string>
<string name="setting_hosts_download">Download hosts file</string>

View File

@ -314,6 +314,9 @@
<Preference
android:key="hosts_import"
android:title="@string/setting_hosts" />
<Preference
android:key="hosts_import_append"
android:title="@string/setting_hosts_append" />
<EditTextPreference
android:defaultValue="https://www.netguard.me/hosts"
android:hint="https://www.netguard.me/hosts"

View File

@ -314,6 +314,9 @@
<Preference
android:key="hosts_import"
android:title="@string/setting_hosts" />
<Preference
android:key="hosts_import_append"
android:title="@string/setting_hosts_append" />
<EditTextPreference
android:defaultValue="https://www.netguard.me/hosts"
android:hint="https://www.netguard.me/hosts"