Improved rules management

This commit is contained in:
PeratX 2017-05-09 21:51:52 +08:00
parent d63451c155
commit c4ec99f139
6 changed files with 163 additions and 31 deletions

View File

@ -63,7 +63,8 @@ public class Daedalus extends Application {
}};
public static final List<RulesProvider> DNSMASQ_PROVIDERS = new ArrayList<RulesProvider>() {{
add(new RulesProvider("sy618/hosts/dnsad", "https://raw.githubusercontent.com/sy618/hosts/master/dnsmasq/dnsad", "dnsad"));
add(new RulesProvider("sy618/hosts/dnsfq", "https://raw.githubusercontent.com/sy618/hosts/master/dnsmasq/dnsfq", "dnsfq"));
}};
public static final String[] DEFAULT_TEST_DOMAINS = new String[]{

View File

@ -37,21 +37,26 @@ public class RulesFragment extends Fragment {
private Thread mThread = null;
private View view = null;
private HostsHandler mHandler = null;
private RulesHandler mHandler = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_hosts, container, false);
view = inflater.inflate(R.layout.fragment_rules, container, false);
mHandler = new HostsHandler().setView(view).setHostsFragment(this);
mHandler = new RulesHandler().setView(view).setHostsFragment(this);
final Spinner spinnerHosts = (Spinner) view.findViewById(R.id.spinner_hosts);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, RulesProvider.getHostsProviderNames());
spinnerHosts.setAdapter(spinnerArrayAdapter);
ArrayAdapter hostsArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, RulesProvider.getHostsProviderNames());
spinnerHosts.setAdapter(hostsArrayAdapter);
spinnerHosts.setSelection(0);
Button buttonDownload = (Button) view.findViewById(R.id.button_download_hosts);
buttonDownload.setOnClickListener(new View.OnClickListener() {
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) {
@ -71,7 +76,45 @@ public class RulesFragment extends Fragment {
}
reader.close();
mHandler.obtainMessage(HostsHandler.MSG_DOWNLOADED, builder.toString()).sendToTarget();
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();
@ -89,15 +132,24 @@ public class RulesFragment extends Fragment {
}
private void updateUserInterface() {
File file = new File(Daedalus.hostsPath);
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\n");
if (!file.exists()) {
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(file.lastModified()).toString()).append("\n\n")
.append(getString(R.string.hosts_size)).append(" ").append(new DecimalFormat("0.00").format(((float) file.length() / 1024))).append(" KB");
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());
}
@ -126,18 +178,19 @@ public class RulesFragment extends Fragment {
}
}
private static class HostsHandler extends Handler {
static final int MSG_DOWNLOADED = 0;
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 RulesFragment mFragment = null;
HostsHandler setView(View view) {
RulesHandler setView(View view) {
this.view = view;
return this;
}
HostsHandler setHostsFragment(RulesFragment fragment) {
RulesHandler setHostsFragment(RulesFragment fragment) {
mFragment = fragment;
return this;
}
@ -152,7 +205,7 @@ public class RulesFragment extends Fragment {
super.handleMessage(msg);
switch (msg.what) {
case MSG_DOWNLOADED:
case MSG_HOSTS_DOWNLOADED:
try {
String result = (String) msg.obj;
File file = new File(Daedalus.hostsPath);
@ -163,6 +216,22 @@ public class RulesFragment extends Fragment {
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();

View File

@ -18,10 +18,17 @@ import java.util.ArrayList;
public class RulesProvider {
private String name;
private String downloadURL;
private String fileName;
private String data;
public RulesProvider(String name, String downloadURL) {
this(name, downloadURL, "Unknown");
}
public RulesProvider(String name, String downloadURL, String fileName) {
this.name = name;
this.downloadURL = downloadURL;
this.fileName = fileName;
}
public String getName() {
@ -32,19 +39,51 @@ public class RulesProvider {
return downloadURL;
}
public String getFileName() {
return fileName;
}
public void setData(String data) {
this.data = data;
}
public String getData() {
String temp = data;
data = null;
return temp;
}
public static String[] getHostsProviderNames() {
ArrayList<String> servers = new ArrayList<>(Daedalus.HOSTS_PROVIDERS.size());
for (RulesProvider hostsProvider : Daedalus.HOSTS_PROVIDERS) {
servers.add(hostsProvider.getName());
for (RulesProvider provider : Daedalus.HOSTS_PROVIDERS) {
servers.add(provider.getName());
}
String[] stringServers = new String[Daedalus.HOSTS_PROVIDERS.size()];
return servers.toArray(stringServers);
}
public static String[] getDnsmasqProviderNames() {
ArrayList<String> servers = new ArrayList<>(Daedalus.DNSMASQ_PROVIDERS.size());
for (RulesProvider provider : Daedalus.DNSMASQ_PROVIDERS) {
servers.add(provider.getName());
}
String[] stringServers = new String[Daedalus.DNSMASQ_PROVIDERS.size()];
return servers.toArray(stringServers);
}
public static String getDownloadUrlByName(String name) {
for (RulesProvider hostsProvider : Daedalus.HOSTS_PROVIDERS) {
if (hostsProvider.getName().equals(name)) {
return hostsProvider.getDownloadURL();
for (RulesProvider provider : Daedalus.HOSTS_PROVIDERS) {
if (provider.getName().equals(name)) {
return provider.getDownloadURL();
}
}
return null;
}
public static RulesProvider getProviderByName(String name) {
for (RulesProvider provider : Daedalus.DNSMASQ_PROVIDERS) {
if (provider.getName().equals(name)) {
return provider;
}
}
return null;

View File

@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/fragment_hosts"
android:id="@+id/fragment_rules"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
@ -27,13 +27,32 @@
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_dnsmasq"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_below="@+id/button_download_hosts"/>
<Button
android:text="@string/button_text_download_dnsmasq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner_dnsmasq"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:id="@+id/button_download_dnsmasq"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView_hosts"
android:layout_below="@+id/button_download_hosts"
android:layout_below="@+id/button_download_dnsmasq"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:textSize="14sp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>

View File

@ -49,7 +49,7 @@
<string name="action_rules">规则</string>
<string name="button_text_download_hosts">下载 hosts</string>
<string name="notice_start_download">正在下载 rules请稍等 ……</string>
<string name="notice_downloaded">已下载 hosts </string>
<string name="notice_downloaded">已下载规则</string>
<string name="notice_now_downloading">Daedalus 当前正在下载规则,请稍等。</string>
<string name="hosts_not_found">找不到本地 hosts 文件。</string>
<string name="hosts_path">本地 hosts 路径:</string>
@ -66,4 +66,6 @@
<string name="action_removed">已移除。</string>
<string name="action_undo">撤销</string>
<string name="settings_use_dnsmasq">使用 DNSMasq 替代 Hosts</string>
<string name="button_text_download_dnsmasq">下载 DNSMasq 配置文件</string>
<string name="dnsmasq_path">本地 DNSMasq 配置路径:</string>
</resources>

View File

@ -48,9 +48,9 @@
</string>
<string name="action_rules">Rules</string>
<string name="button_text_download_hosts">Download hosts</string>
<string name="notice_start_download">Downloading rules, please wait …</string>
<string name="notice_downloaded">Hosts has been downloaded.</string>
<string name="notice_now_downloading">Daedalus is currently downloading rules, please wait.</string>
<string name="notice_start_download">Downloading rule, please wait …</string>
<string name="notice_downloaded">Rule has been downloaded.</string>
<string name="notice_now_downloading">Daedalus is currently downloading rule, please wait.</string>
<string name="hosts_not_found">Local hosts not found.</string>
<string name="hosts_path">Local hosts path:</string>
<string name="hosts_last_modified">Last modified:</string>
@ -66,4 +66,6 @@
<string name="action_removed">Removed.</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="dnsmasq_path">Local DNSMasq configurations path:</string>
</resources>