Check if hosts file exists

This commit is contained in:
M66B 2016-01-30 17:01:53 +01:00
parent 07c74cf51f
commit be61bbb782
2 changed files with 9 additions and 5 deletions

View File

@ -98,6 +98,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/objectFiles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/proguard-rules" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />

View File

@ -823,10 +823,12 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
private void prepareHostsBlocked() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
boolean use_hosts = prefs.getBoolean("use_hosts", false);
File hosts = new File(getFilesDir(), "hosts.txt");
mapHostsBlocked.clear();
if (use_hosts) {
File hosts = new File(getFilesDir(), "hosts.txt");
if (use_hosts && hosts.exists() && hosts.canRead()) {
int count = 0;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(hosts));
@ -838,13 +840,14 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
line = line.trim();
if (line.length() > 0) {
String[] words = line.split("\\s+");
if (words.length == 2)
if (words.length == 2) {
count++;
mapHostsBlocked.put(words[1], true);
else
} else
Log.i(TAG, "Invalid hosts file line: " + line);
}
}
br.close();
Log.i(TAG, count + " hosts read");
} catch (IOException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
} finally {