mirror of https://github.com/M66B/NetGuard.git
Added intent to download hosts file
This commit is contained in:
parent
8873ccbabe
commit
99c44f4d36
|
@ -149,6 +149,14 @@
|
|||
</intent-filter>
|
||||
</service>
|
||||
|
||||
<service
|
||||
android:name=".ServiceExternal"
|
||||
android:label="@string/app_name">
|
||||
<intent-filter>
|
||||
<action android:name="eu.faircode.netguard.DOWNLOAD_HOSTS_FILE" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
<service
|
||||
android:name=".ServiceTileMain"
|
||||
android:icon="@drawable/ic_security_white_24dp"
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
package eu.faircode.netguard;
|
||||
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
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;
|
||||
import java.net.URLConnection;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class ServiceExternal extends IntentService {
|
||||
private static final String TAG = "NetGuard.External";
|
||||
private static final String ACTION_DOWNLOAD_HOSTS_FILE = "eu.faircode.netguard.DOWNLOAD_HOSTS_FILE";
|
||||
|
||||
// am startservice -a eu.faircode.netguard.DOWNLOAD_HOSTS_FILE
|
||||
|
||||
public ServiceExternal() {
|
||||
super(TAG);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
Log.i(TAG, "Received " + intent);
|
||||
Util.logExtras(intent);
|
||||
|
||||
if (ACTION_DOWNLOAD_HOSTS_FILE.equals(intent.getAction())) {
|
||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
String hosts_url = prefs.getString("hosts_url", null);
|
||||
File tmp = new File(getFilesDir(), "hosts.tmp");
|
||||
File hosts = new File(getFilesDir(), "hosts.txt");
|
||||
|
||||
InputStream in = null;
|
||||
OutputStream out = null;
|
||||
URLConnection connection = null;
|
||||
try {
|
||||
URL url = new URL(hosts_url);
|
||||
connection = url.openConnection();
|
||||
connection.connect();
|
||||
|
||||
if (connection instanceof HttpURLConnection) {
|
||||
HttpURLConnection httpConnection = (HttpURLConnection) connection;
|
||||
if (httpConnection.getResponseCode() != HttpURLConnection.HTTP_OK)
|
||||
throw new IOException(httpConnection.getResponseCode() + " " + httpConnection.getResponseMessage());
|
||||
}
|
||||
|
||||
int contentLength = connection.getContentLength();
|
||||
Log.i(TAG, "Content length=" + contentLength);
|
||||
in = connection.getInputStream();
|
||||
out = new FileOutputStream(tmp);
|
||||
|
||||
long size = 0;
|
||||
byte buffer[] = new byte[4096];
|
||||
int bytes;
|
||||
while ((bytes = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytes);
|
||||
size += bytes;
|
||||
}
|
||||
|
||||
Log.i(TAG, "Downloaded size=" + size);
|
||||
|
||||
if (hosts.exists())
|
||||
hosts.delete();
|
||||
tmp.renameTo(hosts);
|
||||
|
||||
String last = SimpleDateFormat.getDateTimeInstance().format(new Date().getTime());
|
||||
prefs.edit().putString("hosts_last_download", last).apply();
|
||||
|
||||
ServiceSinkhole.reload("hosts file download", this);
|
||||
|
||||
} catch (Throwable ex) {
|
||||
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
||||
|
||||
if (tmp.exists())
|
||||
tmp.delete();
|
||||
} 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 instanceof HttpURLConnection)
|
||||
((HttpURLConnection) connection).disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue