Hosts file download option

This commit is contained in:
M66B 2016-02-03 11:54:17 +01:00
parent d6e5acd091
commit 191fa26cf3
6 changed files with 226 additions and 12 deletions

View File

@ -74,6 +74,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@ -114,7 +116,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
PreferenceScreen screen = getPreferenceScreen();
final PreferenceScreen screen = getPreferenceScreen();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// Handle auto enable
@ -197,19 +199,25 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
}
});
// Handle hosts import
// https://github.com/Free-Software-for-Android/AdAway/wiki/HostsSources
// Hosts file settings
Preference pref_hosts = screen.findPreference("hosts");
Preference pref_block_domains = screen.findPreference("use_hosts");
pref_block_domains.setEnabled(new File(getFilesDir(), "hosts.txt").exists());
EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
Preference pref_hosts_download = screen.findPreference("hosts_download");
if (Util.isPlayStoreInstall(this)) {
PreferenceCategory pref_backup = (PreferenceCategory) screen.findPreference("category_backup");
pref_backup.removePreference(pref_hosts);
PreferenceCategory pref_category = (PreferenceCategory) screen.findPreference("category_advanced_options");
pref_category.removePreference(pref_block_domains);
PreferenceCategory pref_backup = (PreferenceCategory) screen.findPreference("category_backup");
pref_backup.removePreference(pref_hosts);
pref_backup.removePreference(pref_hosts_url);
pref_backup.removePreference(pref_hosts_download);
} else {
pref_block_domains.setEnabled(new File(getFilesDir(), "hosts.txt").exists());
// Handle hosts import
// https://github.com/Free-Software-for-Android/AdAway/wiki/HostsSources
pref_hosts.setEnabled(getIntentOpenHosts().resolveActivity(getPackageManager()) != null);
pref_hosts.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
@ -218,10 +226,47 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
return true;
}
});
// Handle hosts file download
pref_hosts_url.setSummary(pref_hosts_url.getText());
pref_hosts_download.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
final File hosts = new File(getFilesDir(), "hosts.txt");
EditTextPreference pref_hosts_url = (EditTextPreference) screen.findPreference("hosts_url");
try {
new DownloadTask(ActivitySettings.this, new URL(pref_hosts_url.getText()), hosts, new DownloadTask.Listener() {
@Override
public void onCompleted() {
Toast.makeText(ActivitySettings.this, R.string.msg_downloaded, Toast.LENGTH_LONG).show();
SinkholeService.reload(null, "hosts file download", ActivitySettings.this);
}
@Override
public void onCancelled() {
if (hosts.exists())
hosts.delete();
SinkholeService.reload(null, "hosts file download", ActivitySettings.this);
}
@Override
public void onException(Throwable ex) {
if (hosts.exists())
hosts.delete();
SinkholeService.reload(null, "hosts file download", ActivitySettings.this);
Toast.makeText(ActivitySettings.this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}).execute();
} catch (MalformedURLException ex) {
Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
}
return true;
}
});
}
// Development
if (!(Util.isDebuggable(this) || Util.getSelfVersionName(this).contains("beta"))) {
// Development
screen.removePreference(screen.findPreference("category_development"));
prefs.edit().remove("loglevel").apply();
}
@ -508,19 +553,22 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
}
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_dns, prefs.getString("dns", Util.getDefaultDNS(this))));
} else if ("show_stats".equals(name)) {
} else if ("show_stats".equals(name))
SinkholeService.reloadStats("changed " + name, this);
} else if ("stats_base".equals(name)) {
else if ("stats_base".equals(name))
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_stats_base, prefs.getString(name, "5")));
} else if ("stats_frequency".equals(name)) {
else if ("stats_frequency".equals(name))
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_stats_frequency, prefs.getString(name, "1000")));
} else if ("stats_samples".equals(name)) {
else if ("stats_samples".equals(name))
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_stats_samples, prefs.getString(name, "90")));
} else if ("loglevel".equals(name))
else if ("hosts_url".equals(name))
getPreferenceScreen().findPreference(name).setSummary(prefs.getString(name, "http://www.netguard.me/hosts"));
else if ("loglevel".equals(name))
SinkholeService.reload(null, "changed " + name, this);
}

View File

@ -0,0 +1,143 @@
package eu.faircode.netguard;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadTask extends AsyncTask<Object, Integer, Object> {
private static final String TAG = "NetGuard.Download";
private Context context;
private URL url;
private File file;
private Listener listener;
ProgressDialog progressDialog;
private PowerManager.WakeLock wakeLock;
public interface Listener {
void onCompleted();
void onCancelled();
void onException(Throwable ex);
}
public DownloadTask(Context context, URL url, File file, Listener listener) {
this.context = context;
this.url = url;
this.file = file;
this.listener = listener;
}
@Override
protected void onPreExecute() {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
wakeLock.acquire();
progressDialog = new ProgressDialog(context);
progressDialog.setIcon(R.mipmap.ic_launcher);
progressDialog.setTitle(R.string.app_name);
progressDialog.setMessage(context.getString(R.string.msg_downloading, url.toString()));
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
DownloadTask.this.cancel(true);
}
});
progressDialog.show();
}
@Override
protected Object doInBackground(Object... args) {
Log.i(TAG, "Downloading " + url + " into " + file);
InputStream in = null;
OutputStream out = null;
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
throw new IOException(connection.getResponseCode() + " " + connection.getResponseMessage());
int contentLength = connection.getContentLength();
Log.i(TAG, "Content length=" + contentLength);
in = connection.getInputStream();
out = new FileOutputStream(file);
long size = 0;
byte buffer[] = new byte[4096];
int bytes;
while (!isCancelled() && (bytes = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
size += bytes;
if (contentLength > 0)
publishProgress((int) (size * 100 / contentLength));
}
Log.i(TAG, "Downloaded size=" + size);
return null;
} catch (Throwable ex) {
return ex;
} finally {
try {
if (out != null)
out.close();
} catch (IOException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
try {
if (in != null)
in.close();
} catch (IOException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
if (connection != null)
connection.disconnect();
}
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setProgress(progress[0]);
}
@Override
protected void onCancelled() {
super.onCancelled();
Log.i(TAG, "Cancelled");
listener.onCancelled();
}
@Override
protected void onPostExecute(Object result) {
wakeLock.release();
progressDialog.dismiss();
if (result instanceof Throwable) {
Log.e(TAG, result.toString() + "\n" + Log.getStackTraceString((Throwable) result));
listener.onException((Throwable) result);
} else
listener.onCompleted();
}
}

View File

@ -72,6 +72,8 @@ however it is impossible to guarantee NetGuard will work correctly on every devi
<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_url">Hosts file download URL</string>
<string name="setting_hosts_download">Download hosts file</string>
<string name="setting_technical">Technical information</string>
<string name="setting_technical_general">General</string>
@ -124,6 +126,8 @@ Your internet traffic is not being sent to a remote VPN server.</string>
<string name="msg_log_disabled">Traffic logging is disabled, use the switch above to enable logging. Traffic logging might result in extra battery usage.</string>
<string name="msg_clear_rules">This will reset the rules and conditions to their default values</string>
<string name="msg_reset_access">This will delete access attempt log lines without allow/block rules</string>
<string name="msg_downloading">Downloading\n%1s</string>
<string name="msg_downloaded">Hosts file downloaded</string>
<string name="title_conditions">Conditions</string>
<string name="title_screen_wifi">Allow Wi-Fi when screen is on</string>

View File

@ -176,6 +176,14 @@
android:key="hosts"
android:summary="@string/summary_hosts"
android:title="@string/setting_hosts" />
<EditTextPreference
android:defaultValue="http://www.netguard.me/hosts"
android:key="hosts_url"
android:title="@string/setting_hosts_url" />
<Preference
android:dependency="hosts_url"
android:key="hosts_download"
android:title="@string/setting_hosts_download" />
</PreferenceCategory>
<PreferenceCategory

View File

@ -176,6 +176,14 @@
android:key="hosts"
android:summary="@string/summary_hosts"
android:title="@string/setting_hosts" />
<EditTextPreference
android:defaultValue="http://www.netguard.me/hosts"
android:key="hosts_url"
android:title="@string/setting_hosts_url" />
<Preference
android:dependency="hosts_url"
android:key="hosts_download"
android:title="@string/setting_hosts_download" />
</PreferenceCategory>
<PreferenceCategory

3
checkprefs.sh Normal file
View File

@ -0,0 +1,3 @@
#!/bin/sh
#apt-get install meld
meld app/src/main/res/xml-v14/preferences.xml app/src/main/res/xml-v21/preferences.xml &