Improved and added download
This commit is contained in:
parent
24c09c65b5
commit
a792265b7d
@ -21,10 +21,7 @@ import com.google.gson.JsonParseException;
|
|||||||
import com.google.gson.stream.JsonReader;
|
import com.google.gson.stream.JsonReader;
|
||||||
import org.itxtech.daedalus.activity.MainActivity;
|
import org.itxtech.daedalus.activity.MainActivity;
|
||||||
import org.itxtech.daedalus.service.DaedalusVpnService;
|
import org.itxtech.daedalus.service.DaedalusVpnService;
|
||||||
import org.itxtech.daedalus.util.Configurations;
|
import org.itxtech.daedalus.util.*;
|
||||||
import org.itxtech.daedalus.util.DnsServer;
|
|
||||||
import org.itxtech.daedalus.util.RulesProvider;
|
|
||||||
import org.itxtech.daedalus.util.RulesResolver;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -78,8 +75,7 @@ public class Daedalus extends Application {
|
|||||||
|
|
||||||
public static Configurations configurations;
|
public static Configurations configurations;
|
||||||
|
|
||||||
public static String hostsPath = null;
|
public static String rulesPath = null;
|
||||||
public static String dnsmasqPath = null;
|
|
||||||
private static String configPath = null;
|
private static String configPath = null;
|
||||||
|
|
||||||
private static Daedalus instance = null;
|
private static Daedalus instance = null;
|
||||||
@ -94,11 +90,10 @@ public class Daedalus extends Application {
|
|||||||
mHostsResolver.start();
|
mHostsResolver.start();
|
||||||
|
|
||||||
if (getExternalFilesDir(null) != null) {
|
if (getExternalFilesDir(null) != null) {
|
||||||
hostsPath = getExternalFilesDir(null).getPath() + "/hosts";
|
rulesPath = getExternalFilesDir(null).getPath() + "/rules/";
|
||||||
dnsmasqPath = getExternalFilesDir(null).getPath() + "/dnsmasq/";
|
|
||||||
configPath = getExternalFilesDir(null).getPath() + "/config.json";
|
configPath = getExternalFilesDir(null).getPath() + "/config.json";
|
||||||
|
|
||||||
File file = new File(dnsmasqPath);
|
File file = new File(rulesPath);
|
||||||
Log.d(TAG, "mkdir result: " + file.mkdirs());
|
Log.d(TAG, "mkdir result: " + file.mkdirs());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,10 +137,23 @@ public class Daedalus extends Application {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Daedalus.getPrefs().getBoolean("settings_use_dnsmasq", false)) {
|
ArrayList<String> pendingLoad = new ArrayList<>();
|
||||||
RulesResolver.startLoadDnsmasq(dnsmasqPath);
|
int type = Rule.TYPE_HOSTS;
|
||||||
} else {
|
for (Rule rule : configurations.getRules()) {
|
||||||
RulesResolver.startLoadHosts(hostsPath);
|
if (rule.isUsing()) {
|
||||||
|
pendingLoad.add(rulesPath + rule.getFileName());
|
||||||
|
type = rule.getType(); //Only one type and they should the same
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String[] arr = new String[pendingLoad.size()];
|
||||||
|
pendingLoad.toArray(arr);
|
||||||
|
switch (type) {
|
||||||
|
case Rule.TYPE_HOSTS:
|
||||||
|
RulesResolver.startLoadHosts(arr);
|
||||||
|
break;
|
||||||
|
case Rule.TYPE_DNAMASQ:
|
||||||
|
RulesResolver.startLoadDnsmasq(arr);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import android.app.FragmentTransaction;
|
|||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.PersistableBundle;
|
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.support.v4.graphics.drawable.DrawableCompat;
|
import android.support.v4.graphics.drawable.DrawableCompat;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
@ -76,8 +75,8 @@ public class ConfigActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
|
public void onPostCreate(Bundle savedInstanceState) {
|
||||||
super.onPostCreate(savedInstanceState, persistentState);
|
super.onPostCreate(savedInstanceState);
|
||||||
|
|
||||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_config);
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_config);
|
||||||
switch (getIntent().getIntExtra(LAUNCH_ACTION_FRAGMENT, LAUNCH_FRAGMENT_DNS_SERVER)) {
|
switch (getIntent().getIntExtra(LAUNCH_ACTION_FRAGMENT, LAUNCH_FRAGMENT_DNS_SERVER)) {
|
||||||
|
@ -4,6 +4,8 @@ import android.app.AlertDialog;
|
|||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Message;
|
||||||
import android.preference.EditTextPreference;
|
import android.preference.EditTextPreference;
|
||||||
import android.preference.ListPreference;
|
import android.preference.ListPreference;
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
@ -16,6 +18,11 @@ import org.itxtech.daedalus.Daedalus;
|
|||||||
import org.itxtech.daedalus.R;
|
import org.itxtech.daedalus.R;
|
||||||
import org.itxtech.daedalus.activity.ConfigActivity;
|
import org.itxtech.daedalus.activity.ConfigActivity;
|
||||||
import org.itxtech.daedalus.util.Rule;
|
import org.itxtech.daedalus.util.Rule;
|
||||||
|
import org.itxtech.daedalus.widget.ClickPreference;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Daedalus Project
|
* Daedalus Project
|
||||||
@ -30,6 +37,8 @@ import org.itxtech.daedalus.util.Rule;
|
|||||||
*/
|
*/
|
||||||
public class RuleConfigFragment extends ConfigFragment {
|
public class RuleConfigFragment extends ConfigFragment {
|
||||||
private Intent intent = null;
|
private Intent intent = null;
|
||||||
|
private Thread mThread = null;
|
||||||
|
private RuleConfigHandler mHandler = null;
|
||||||
private View view;
|
private View view;
|
||||||
private int index;
|
private int index;
|
||||||
|
|
||||||
@ -41,10 +50,21 @@ public class RuleConfigFragment extends ConfigFragment {
|
|||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|
||||||
|
stopThread();
|
||||||
intent = null;
|
intent = null;
|
||||||
|
mHandler.shutdown();
|
||||||
|
mHandler = null;
|
||||||
view = null;
|
view = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void stopThread() {
|
||||||
|
if (mThread != null) {
|
||||||
|
mThread.interrupt();
|
||||||
|
mThread = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@ -55,6 +75,8 @@ public class RuleConfigFragment extends ConfigFragment {
|
|||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
view = super.onCreateView(inflater, container, savedInstanceState);
|
view = super.onCreateView(inflater, container, savedInstanceState);
|
||||||
|
|
||||||
|
mHandler = new RuleConfigHandler().setView(view);
|
||||||
|
|
||||||
EditTextPreference ruleName = (EditTextPreference) findPreference("ruleName");
|
EditTextPreference ruleName = (EditTextPreference) findPreference("ruleName");
|
||||||
ruleName.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
ruleName.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||||
@Override
|
@Override
|
||||||
@ -77,7 +99,7 @@ public class RuleConfigFragment extends ConfigFragment {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
EditTextPreference ruleDownloadUrl = (EditTextPreference) findPreference("ruleDownloadUrl");
|
final EditTextPreference ruleDownloadUrl = (EditTextPreference) findPreference("ruleDownloadUrl");
|
||||||
ruleDownloadUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
ruleDownloadUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||||
@ -86,7 +108,7 @@ public class RuleConfigFragment extends ConfigFragment {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
EditTextPreference ruleFilename = (EditTextPreference) findPreference("ruleFilename");
|
final EditTextPreference ruleFilename = (EditTextPreference) findPreference("ruleFilename");
|
||||||
ruleFilename.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
ruleFilename.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||||
@ -95,6 +117,46 @@ public class RuleConfigFragment extends ConfigFragment {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ClickPreference ruleSync = (ClickPreference) findPreference("ruleSync");
|
||||||
|
ruleSync.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onPreferenceClick(Preference preference) {
|
||||||
|
save();
|
||||||
|
if (mThread == null) {
|
||||||
|
Snackbar.make(view, R.string.notice_start_download, Snackbar.LENGTH_LONG)
|
||||||
|
.setAction("Action", null).show();
|
||||||
|
mThread = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
URLConnection connection = new URL(ruleDownloadUrl.getText()).openConnection();
|
||||||
|
InputStream inputStream = connection.getInputStream();
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
String result;
|
||||||
|
while ((result = reader.readLine()) != null) {
|
||||||
|
builder.append("\n").append(result);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
|
||||||
|
new RuleData(ruleFilename.getText(), builder.toString())).sendToTarget();
|
||||||
|
stopThread();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
stopThread();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
mThread.start();
|
||||||
|
} else {
|
||||||
|
Snackbar.make(view, R.string.notice_now_downloading, Snackbar.LENGTH_LONG)
|
||||||
|
.setAction("Action", null).show();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
index = intent.getIntExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE);
|
index = intent.getIntExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE);
|
||||||
if (index != ConfigActivity.ID_NONE) {
|
if (index != ConfigActivity.ID_NONE) {
|
||||||
@ -121,32 +183,36 @@ public class RuleConfigFragment extends ConfigFragment {
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void save() {
|
||||||
|
String ruleName = ((EditTextPreference) findPreference("ruleName")).getText();
|
||||||
|
String ruleType = ((ListPreference) findPreference("ruleType")).getValue();
|
||||||
|
String ruleFilename = ((EditTextPreference) findPreference("ruleFilename")).getText();
|
||||||
|
String ruleDownloadUrl = ((EditTextPreference) findPreference("ruleDownloadUrl")).getText();
|
||||||
|
|
||||||
|
if (ruleName.equals("") | ruleType.equals("") | ruleFilename.equals("") | ruleDownloadUrl.equals("")) {
|
||||||
|
Snackbar.make(view, R.string.notice_fill_in_all, Snackbar.LENGTH_LONG)
|
||||||
|
.setAction("Action", null).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (index == ConfigActivity.ID_NONE) {
|
||||||
|
Daedalus.configurations.getRules().add(new Rule(ruleName, ruleFilename, Integer.parseInt(ruleType), ruleDownloadUrl));
|
||||||
|
} else {
|
||||||
|
Rule rule = Daedalus.configurations.getRules().get(index);
|
||||||
|
rule.setName(ruleName);
|
||||||
|
rule.setType(Integer.parseInt(ruleType));
|
||||||
|
rule.setFileName(ruleFilename);
|
||||||
|
rule.setDownloadUrl(ruleDownloadUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onMenuItemClick(MenuItem item) {
|
public boolean onMenuItemClick(MenuItem item) {
|
||||||
int id = item.getItemId();
|
int id = item.getItemId();
|
||||||
|
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case R.id.action_apply:
|
case R.id.action_apply:
|
||||||
String ruleName = ((EditTextPreference) findPreference("ruleName")).getText();
|
save();
|
||||||
String ruleType = ((ListPreference) findPreference("ruleType")).getValue();
|
|
||||||
String ruleFilename = ((EditTextPreference) findPreference("ruleFilename")).getText();
|
|
||||||
String ruleDownloadUrl = ((EditTextPreference) findPreference("ruleDownloadUrl")).getText();
|
|
||||||
|
|
||||||
if (ruleName.equals("") | ruleType.equals("") | ruleFilename.equals("") | ruleDownloadUrl.equals("")) {
|
|
||||||
Snackbar.make(view, R.string.notice_fill_in_all, Snackbar.LENGTH_LONG)
|
|
||||||
.setAction("Action", null).show();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index == ConfigActivity.ID_NONE) {
|
|
||||||
Daedalus.configurations.getRules().add(new Rule(ruleName, ruleFilename, Integer.parseInt(ruleType), ruleDownloadUrl));
|
|
||||||
} else {
|
|
||||||
Rule rule = Daedalus.configurations.getRules().get(index);
|
|
||||||
rule.setName(ruleName);
|
|
||||||
rule.setType(Integer.parseInt(ruleType));
|
|
||||||
rule.setFileName(ruleFilename);
|
|
||||||
rule.setDownloadUrl(ruleDownloadUrl);
|
|
||||||
}
|
|
||||||
getActivity().finish();
|
getActivity().finish();
|
||||||
break;
|
break;
|
||||||
case R.id.action_delete:
|
case R.id.action_delete:
|
||||||
@ -171,4 +237,59 @@ public class RuleConfigFragment extends ConfigFragment {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class RuleData {
|
||||||
|
private String data;
|
||||||
|
private String filename;
|
||||||
|
|
||||||
|
RuleData(String filename, String data) {
|
||||||
|
this.data = data;
|
||||||
|
this.filename = filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getFilename() {
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class RuleConfigHandler extends Handler {
|
||||||
|
static final int MSG_RULE_DOWNLOADED = 0;
|
||||||
|
|
||||||
|
private View view = null;
|
||||||
|
|
||||||
|
RuleConfigHandler setView(View view) {
|
||||||
|
this.view = view;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void shutdown() {
|
||||||
|
view = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message msg) {
|
||||||
|
super.handleMessage(msg);
|
||||||
|
|
||||||
|
switch (msg.what) {
|
||||||
|
case MSG_RULE_DOWNLOADED:
|
||||||
|
try {
|
||||||
|
RuleData ruleData = (RuleData) msg.obj;
|
||||||
|
File file = new File(Daedalus.rulesPath + ruleData.getFilename());
|
||||||
|
FileOutputStream stream = new FileOutputStream(file);
|
||||||
|
stream.write(ruleData.getData().getBytes());
|
||||||
|
stream.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
Snackbar.make(view, R.string.notice_downloaded, Snackbar.LENGTH_LONG)
|
||||||
|
.setAction("Action", null).show();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,9 @@ import org.itxtech.daedalus.R;
|
|||||||
import org.itxtech.daedalus.activity.ConfigActivity;
|
import org.itxtech.daedalus.activity.ConfigActivity;
|
||||||
import org.itxtech.daedalus.util.Rule;
|
import org.itxtech.daedalus.util.Rule;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.text.DecimalFormat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Daedalus Project
|
* Daedalus Project
|
||||||
*
|
*
|
||||||
@ -120,6 +123,15 @@ public class RulesFragment extends Fragment {
|
|||||||
holder.setIndex(position);
|
holder.setIndex(position);
|
||||||
holder.textViewName.setText(rule.getName());
|
holder.textViewName.setText(rule.getName());
|
||||||
holder.textViewAddress.setText(rule.getFileName());
|
holder.textViewAddress.setText(rule.getFileName());
|
||||||
|
|
||||||
|
File file = new File(Daedalus.rulesPath + rule.getFileName());
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
if (file.exists()) {
|
||||||
|
builder.append(new DecimalFormat("0.00").format(((float) file.length() / 1024)));
|
||||||
|
} else {
|
||||||
|
builder.append("0");
|
||||||
|
}
|
||||||
|
holder.textViewSize.setText(builder.append(" KB").toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -137,12 +149,16 @@ public class RulesFragment extends Fragment {
|
|||||||
private static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
private static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
||||||
private final TextView textViewName;
|
private final TextView textViewName;
|
||||||
private final TextView textViewAddress;
|
private final TextView textViewAddress;
|
||||||
|
private final TextView textViewSize;
|
||||||
|
private final View view;
|
||||||
private int index;
|
private int index;
|
||||||
|
|
||||||
ViewHolder(View view) {
|
ViewHolder(View view) {
|
||||||
super(view);
|
super(view);
|
||||||
|
this.view = view;
|
||||||
textViewName = (TextView) view.findViewById(R.id.textView_rule_name);
|
textViewName = (TextView) view.findViewById(R.id.textView_rule_name);
|
||||||
textViewAddress = (TextView) view.findViewById(R.id.textView_rule_detail);
|
textViewAddress = (TextView) view.findViewById(R.id.textView_rule_detail);
|
||||||
|
textViewSize = (TextView) view.findViewById(R.id.textView_rule_size);
|
||||||
view.setOnClickListener(this);
|
view.setOnClickListener(this);
|
||||||
view.setOnLongClickListener(this);
|
view.setOnLongClickListener(this);
|
||||||
view.setBackgroundResource(R.drawable.background_selectable);
|
view.setBackgroundResource(R.drawable.background_selectable);
|
||||||
@ -150,6 +166,7 @@ public class RulesFragment extends Fragment {
|
|||||||
|
|
||||||
void setIndex(int index) {
|
void setIndex(int index) {
|
||||||
this.index = index;
|
this.index = index;
|
||||||
|
view.setSelected(Daedalus.configurations.getRules().get(index).isUsing());
|
||||||
}
|
}
|
||||||
|
|
||||||
int getIndex() {
|
int getIndex() {
|
||||||
@ -158,6 +175,7 @@ public class RulesFragment extends Fragment {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
Daedalus.configurations.getRules().get(index).setUsing(!v.isSelected());
|
||||||
v.setSelected(!v.isSelected());
|
v.setSelected(!v.isSelected());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,223 +0,0 @@
|
|||||||
package org.itxtech.daedalus.fragment;
|
|
||||||
|
|
||||||
import android.app.Fragment;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Daedalus Project
|
|
||||||
*
|
|
||||||
* @author iTX Technologies
|
|
||||||
* @link https://itxtech.org
|
|
||||||
* <p>
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*/
|
|
||||||
public class RulesFragment0 extends Fragment {
|
|
||||||
/*
|
|
||||||
private Thread mThread = null;
|
|
||||||
private View view = null;
|
|
||||||
private RulesHandler mHandler = null;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
||||||
view = inflater.inflate(R.layout.fragment_rules, container, false);
|
|
||||||
|
|
||||||
mHandler = new RulesHandler().setView(view).setHostsFragment(this);
|
|
||||||
|
|
||||||
final Spinner spinnerHosts = (Spinner) view.findViewById(R.id.spinner_hosts);
|
|
||||||
ArrayAdapter hostsArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, RulesProvider.getHostsProviderNames());
|
|
||||||
spinnerHosts.setAdapter(hostsArrayAdapter);
|
|
||||||
spinnerHosts.setSelection(0);
|
|
||||||
|
|
||||||
final Spinner spinnerDnsmasq = (Spinner) view.findViewById(R.id.spinner_dnsmasq);
|
|
||||||
ArrayAdapter dnsmasqArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, RulesProvider.getDnsmasqProviderNames());
|
|
||||||
spinnerDnsmasq.setAdapter(dnsmasqArrayAdapter);
|
|
||||||
spinnerDnsmasq.setSelection(0);
|
|
||||||
|
|
||||||
Button buttonDownloadHosts = (Button) view.findViewById(R.id.button_download_hosts);
|
|
||||||
buttonDownloadHosts.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
if (mThread == null) {
|
|
||||||
Snackbar.make(view, R.string.notice_start_download, Snackbar.LENGTH_LONG)
|
|
||||||
.setAction("Action", null).show();
|
|
||||||
mThread = new Thread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
URLConnection connection = new URL(RulesProvider.getDownloadUrlByName(spinnerHosts.getSelectedItem().toString())).openConnection();
|
|
||||||
InputStream inputStream = connection.getInputStream();
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
String result;
|
|
||||||
while ((result = reader.readLine()) != null) {
|
|
||||||
builder.append("\n").append(result);
|
|
||||||
}
|
|
||||||
reader.close();
|
|
||||||
|
|
||||||
mHandler.obtainMessage(RulesHandler.MSG_HOSTS_DOWNLOADED, builder.toString()).sendToTarget();
|
|
||||||
stopThread();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
mThread.start();
|
|
||||||
} else {
|
|
||||||
Snackbar.make(view, R.string.notice_now_downloading, Snackbar.LENGTH_LONG)
|
|
||||||
.setAction("Action", null).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Button buttonDownloadDnsmasq = (Button) view.findViewById(R.id.button_download_dnsmasq);
|
|
||||||
buttonDownloadDnsmasq.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
if (mThread == null) {
|
|
||||||
Snackbar.make(view, R.string.notice_start_download, Snackbar.LENGTH_LONG)
|
|
||||||
.setAction("Action", null).show();
|
|
||||||
mThread = new Thread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
RulesProvider provider = RulesProvider.getProviderByName(spinnerDnsmasq.getSelectedItem().toString());
|
|
||||||
URLConnection connection = new URL(provider.getDownloadURL()).openConnection();
|
|
||||||
InputStream inputStream = connection.getInputStream();
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
String result;
|
|
||||||
while ((result = reader.readLine()) != null) {
|
|
||||||
builder.append("\n").append(result);
|
|
||||||
}
|
|
||||||
reader.close();
|
|
||||||
|
|
||||||
provider.setData(builder.toString());
|
|
||||||
mHandler.obtainMessage(RulesHandler.MSG_DNSMASQ_DOWNLOADED, provider).sendToTarget();
|
|
||||||
stopThread();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
mThread.start();
|
|
||||||
} else {
|
|
||||||
Snackbar.make(view, R.string.notice_now_downloading, Snackbar.LENGTH_LONG)
|
|
||||||
.setAction("Action", null).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return view;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateUserInterface() {
|
|
||||||
File hosts = new File(Daedalus.hostsPath);
|
|
||||||
TextView info = (TextView) view.findViewById(R.id.textView_hosts);
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
builder.append(getString(R.string.hosts_path)).append(" ").append(Daedalus.hostsPath).append("\n");
|
|
||||||
if (!hosts.exists()) {
|
|
||||||
builder.append(getString(R.string.hosts_not_found));
|
|
||||||
} else {
|
|
||||||
builder.append(getString(R.string.hosts_last_modified)).append(" ").append(new Date(hosts.lastModified()).toString()).append("\n")
|
|
||||||
.append(getString(R.string.hosts_size)).append(" ").append(new DecimalFormat("0.00").format(((float) hosts.length() / 1024))).append(" KB");
|
|
||||||
}
|
|
||||||
builder.append("\n");
|
|
||||||
File dnsmasq = new File(Daedalus.dnsmasqPath);
|
|
||||||
builder.append(getString(R.string.dnsmasq_path)).append(" ").append(Daedalus.dnsmasqPath);
|
|
||||||
if (dnsmasq.exists()) {
|
|
||||||
for (File conf : dnsmasq.listFiles()) {
|
|
||||||
builder.append("\n").append(conf.getName()).append(" ")
|
|
||||||
.append(new DecimalFormat("0.00").format(((float) conf.length() / 1024))).append(" KB");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
info.setText(builder.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResume() {
|
|
||||||
super.onResume();
|
|
||||||
|
|
||||||
updateUserInterface();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
|
|
||||||
stopThread();
|
|
||||||
mHandler.shutdown();
|
|
||||||
mHandler = null;
|
|
||||||
view = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void stopThread() {
|
|
||||||
if (mThread != null) {
|
|
||||||
mThread.interrupt();
|
|
||||||
mThread = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class RulesHandler extends Handler {
|
|
||||||
static final int MSG_HOSTS_DOWNLOADED = 0;
|
|
||||||
static final int MSG_DNSMASQ_DOWNLOADED = 1;
|
|
||||||
|
|
||||||
private View view = null;
|
|
||||||
private RulesFragment0 mFragment = null;
|
|
||||||
|
|
||||||
RulesHandler setView(View view) {
|
|
||||||
this.view = view;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
RulesHandler setHostsFragment(RulesFragment0 fragment) {
|
|
||||||
mFragment = fragment;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void shutdown() {
|
|
||||||
view = null;
|
|
||||||
mFragment = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleMessage(Message msg) {
|
|
||||||
super.handleMessage(msg);
|
|
||||||
|
|
||||||
switch (msg.what) {
|
|
||||||
case MSG_HOSTS_DOWNLOADED:
|
|
||||||
try {
|
|
||||||
String result = (String) msg.obj;
|
|
||||||
File file = new File(Daedalus.hostsPath);
|
|
||||||
FileOutputStream stream = new FileOutputStream(file);
|
|
||||||
stream.write(result.getBytes());
|
|
||||||
stream.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
Snackbar.make(view, R.string.notice_downloaded, Snackbar.LENGTH_LONG)
|
|
||||||
.setAction("Action", null).show();
|
|
||||||
|
|
||||||
mFragment.updateUserInterface();
|
|
||||||
break;
|
|
||||||
case MSG_DNSMASQ_DOWNLOADED:
|
|
||||||
try {
|
|
||||||
RulesProvider provider = (RulesProvider) msg.obj;
|
|
||||||
File file = new File(Daedalus.dnsmasqPath + provider.getFileName());
|
|
||||||
FileOutputStream stream = new FileOutputStream(file);
|
|
||||||
stream.write(provider.getData().getBytes());
|
|
||||||
stream.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
Snackbar.make(view, R.string.notice_downloaded, Snackbar.LENGTH_LONG)
|
|
||||||
.setAction("Action", null).show();
|
|
||||||
|
|
||||||
mFragment.updateUserInterface();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
|
@ -32,15 +32,15 @@ public class RulesResolver implements Runnable {
|
|||||||
|
|
||||||
private static int status = STATUS_NOT_LOADED;
|
private static int status = STATUS_NOT_LOADED;
|
||||||
private static int mode = MODE_HOSTS;
|
private static int mode = MODE_HOSTS;
|
||||||
private static String hostsFile;
|
private static String[] hostsFiles;
|
||||||
private static String dnsmasqPath;
|
private static String[] dnsmasqFiles;
|
||||||
private static HashMap<String, String> rules;
|
private static HashMap<String, String> rules;
|
||||||
private static boolean shutdown = false;
|
private static boolean shutdown = false;
|
||||||
|
|
||||||
public RulesResolver() {
|
public RulesResolver() {
|
||||||
status = STATUS_NOT_LOADED;
|
status = STATUS_NOT_LOADED;
|
||||||
hostsFile = "";
|
hostsFiles = new String[0];
|
||||||
dnsmasqPath = "";
|
dnsmasqFiles = new String[0];
|
||||||
shutdown = false;
|
shutdown = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,16 +52,16 @@ public class RulesResolver implements Runnable {
|
|||||||
return status == STATUS_LOADED;
|
return status == STATUS_LOADED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void startLoadHosts(String loadFile) {
|
public static void startLoadHosts(String[] loadFile) {
|
||||||
Log.d(TAG, "Loading hosts file " + loadFile);
|
Log.d(TAG, "Loading hosts file " + loadFile.length);
|
||||||
hostsFile = loadFile;
|
hostsFiles = loadFile;
|
||||||
mode = MODE_HOSTS;
|
mode = MODE_HOSTS;
|
||||||
status = STATUS_PENDING_LOAD;
|
status = STATUS_PENDING_LOAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void startLoadDnsmasq(String loadPath) {
|
public static void startLoadDnsmasq(String[] loadPath) {
|
||||||
Log.d(TAG, "Loading DNSMasq file " + loadPath);
|
Log.d(TAG, "Loading DNSMasq file " + loadPath.length);
|
||||||
dnsmasqPath = loadPath;
|
dnsmasqFiles = loadPath;
|
||||||
mode = MODE_DNSMASQ;
|
mode = MODE_DNSMASQ;
|
||||||
status = STATUS_PENDING_LOAD;
|
status = STATUS_PENDING_LOAD;
|
||||||
}
|
}
|
||||||
@ -101,37 +101,32 @@ public class RulesResolver implements Runnable {
|
|||||||
status = STATUS_LOADING;
|
status = STATUS_LOADING;
|
||||||
rules = new HashMap<>();
|
rules = new HashMap<>();
|
||||||
if (mode == MODE_HOSTS) {
|
if (mode == MODE_HOSTS) {
|
||||||
File file = new File(hostsFile);
|
for (String hostsFile : hostsFiles) {
|
||||||
if (file.exists() && file.canRead()) {
|
File file = new File(hostsFile);
|
||||||
FileInputStream stream = new FileInputStream(file);
|
if (file.canRead()) {
|
||||||
BufferedReader dataIO = new BufferedReader(new InputStreamReader(stream));
|
FileInputStream stream = new FileInputStream(file);
|
||||||
String strLine;
|
BufferedReader dataIO = new BufferedReader(new InputStreamReader(stream));
|
||||||
String[] data;
|
String strLine;
|
||||||
while ((strLine = dataIO.readLine()) != null) {
|
String[] data;
|
||||||
//Log.d(TAG, strLine);
|
while ((strLine = dataIO.readLine()) != null) {
|
||||||
if (!strLine.equals("") && !strLine.startsWith("#")) {
|
//Log.d(TAG, strLine);
|
||||||
data = strLine.split("\\s+");
|
if (!strLine.equals("") && !strLine.startsWith("#")) {
|
||||||
rules.put(data[1], data[0]);
|
data = strLine.split("\\s+");
|
||||||
Log.d(TAG, "Putting " + data[0] + " " + data[1]);
|
rules.put(data[1], data[0]);
|
||||||
|
Log.d(TAG, "Putting " + data[0] + " " + data[1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
dataIO.close();
|
dataIO.close();
|
||||||
stream.close();
|
stream.close();
|
||||||
} else {
|
}
|
||||||
status = STATUS_NOT_LOADED;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
} else if (mode == MODE_DNSMASQ) {
|
} else if (mode == MODE_DNSMASQ) {
|
||||||
File file = new File(dnsmasqPath);
|
for (String dnsmasqFile : dnsmasqFiles) {
|
||||||
if (!file.mkdirs() && !file.exists()) {
|
File file = new File(dnsmasqFile);
|
||||||
status = STATUS_NOT_LOADED;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (File conf : file.listFiles()) {
|
|
||||||
if (file.canRead()) {
|
if (file.canRead()) {
|
||||||
Log.d(TAG, "load: Loading DNSMasq configuration " + conf.toString());
|
Log.d(TAG, "load: Loading DNSMasq configuration " + file.toString());
|
||||||
FileInputStream stream = new FileInputStream(conf);
|
FileInputStream stream = new FileInputStream(file);
|
||||||
BufferedReader dataIO = new BufferedReader(new InputStreamReader(stream));
|
BufferedReader dataIO = new BufferedReader(new InputStreamReader(stream));
|
||||||
String strLine;
|
String strLine;
|
||||||
String[] data;
|
String[] data;
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
android:clickable="true"
|
android:clickable="true"
|
||||||
android:foreground="?attr/selectableItemBackgroundBorderless"
|
android:foreground="?attr/selectableItemBackgroundBorderless"
|
||||||
android:focusable="true"
|
android:focusable="true"
|
||||||
|
app:cardBackgroundColor="@android:color/white"
|
||||||
app:cardCornerRadius="10dp"
|
app:cardCornerRadius="10dp"
|
||||||
app:cardPreventCornerOverlap="true"
|
app:cardPreventCornerOverlap="true"
|
||||||
app:cardUseCompatPadding="true">
|
app:cardUseCompatPadding="true">
|
||||||
@ -37,6 +38,16 @@
|
|||||||
android:layout_alignParentBottom="true"
|
android:layout_alignParentBottom="true"
|
||||||
android:layout_alignParentLeft="true"
|
android:layout_alignParentLeft="true"
|
||||||
android:layout_alignParentStart="true"/>
|
android:layout_alignParentStart="true"/>
|
||||||
|
<TextView android:id="@+id/textView_rule_size"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="bottom"
|
||||||
|
android:paddingBottom="@dimen/margin_small"
|
||||||
|
android:paddingRight="@dimen/margin_small"
|
||||||
|
android:paddingEnd="@dimen/margin_small"
|
||||||
|
android:layout_alignParentEnd="true"
|
||||||
|
android:layout_alignParentRight="true"
|
||||||
|
android:ellipsize="end"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceSmall"/>
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
</android.support.v7.widget.CardView>
|
</android.support.v7.widget.CardView>
|
@ -65,7 +65,6 @@
|
|||||||
<string name="settings_dns_over_tcp">DNS over TCP</string>
|
<string name="settings_dns_over_tcp">DNS over TCP</string>
|
||||||
<string name="action_removed">已移除。</string>
|
<string name="action_removed">已移除。</string>
|
||||||
<string name="action_undo">撤销</string>
|
<string name="action_undo">撤销</string>
|
||||||
<string name="settings_use_dnsmasq">使用 DNSMasq 替代 Hosts</string>
|
|
||||||
<string name="button_text_download_dnsmasq">下载 DNSMasq 配置文件</string>
|
<string name="button_text_download_dnsmasq">下载 DNSMasq 配置文件</string>
|
||||||
<string name="dnsmasq_path">本地 DNSMasq 配置路径:</string>
|
<string name="dnsmasq_path">本地 DNSMasq 配置路径:</string>
|
||||||
<string name="notice_delete_confirm_prompt">您确定要删除此配置项?</string>
|
<string name="notice_delete_confirm_prompt">您确定要删除此配置项?</string>
|
||||||
|
@ -65,7 +65,6 @@
|
|||||||
<string name="settings_dns_over_tcp">DNS over TCP</string>
|
<string name="settings_dns_over_tcp">DNS over TCP</string>
|
||||||
<string name="action_removed">Removed.</string>
|
<string name="action_removed">Removed.</string>
|
||||||
<string name="action_undo">Undo</string>
|
<string name="action_undo">Undo</string>
|
||||||
<string name="settings_use_dnsmasq">Use DNSMasq instead of Hosts</string>
|
|
||||||
<string name="button_text_download_dnsmasq">Download DNSMasq configuration</string>
|
<string name="button_text_download_dnsmasq">Download DNSMasq configuration</string>
|
||||||
<string name="dnsmasq_path">Local DNSMasq configurations path:</string>
|
<string name="dnsmasq_path">Local DNSMasq configurations path:</string>
|
||||||
<string name="notice_delete_confirm_prompt">Are you sure you want to remove this configuration?</string>
|
<string name="notice_delete_confirm_prompt">Are you sure you want to remove this configuration?</string>
|
||||||
|
@ -54,11 +54,6 @@
|
|||||||
android:title="@string/settings_local_rules_resolution"
|
android:title="@string/settings_local_rules_resolution"
|
||||||
android:defaultValue="false"
|
android:defaultValue="false"
|
||||||
android:enabled="false"/>
|
android:enabled="false"/>
|
||||||
<SwitchPreference
|
|
||||||
android:key="settings_use_dnsmasq"
|
|
||||||
android:title="@string/settings_use_dnsmasq"
|
|
||||||
android:defaultValue="false"
|
|
||||||
android:enabled="false"/>
|
|
||||||
<SwitchPreference
|
<SwitchPreference
|
||||||
android:key="settings_dns_over_tcp"
|
android:key="settings_dns_over_tcp"
|
||||||
android:title="@string/settings_dns_over_tcp"
|
android:title="@string/settings_dns_over_tcp"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user