Implemented new rule manager

This commit is contained in:
PeratX 2017-05-16 21:15:59 +08:00
parent d2c1f2e652
commit 24c09c65b5
11 changed files with 348 additions and 32 deletions

View File

@ -5,6 +5,7 @@ import android.app.FragmentTransaction;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
@ -12,7 +13,9 @@ import android.support.v7.widget.Toolbar;
import android.view.View;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.fragment.ConfigFragment;
import org.itxtech.daedalus.fragment.DnsServerConfigFragment;
import org.itxtech.daedalus.fragment.RuleConfigFragment;
/**
* Daedalus Project
@ -26,15 +29,30 @@ import org.itxtech.daedalus.fragment.DnsServerConfigFragment;
* (at your option) any later version.
*/
public class ConfigActivity extends AppCompatActivity {
public static final String LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID = "org.itxtech.daedalus.activity.DnsServerConfigActivity.LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID";
public static final int CUSTOM_DNS_SERVER_ID_NONE = -1;
public static final String LAUNCH_ACTION_FRAGMENT = "org.itxtech.daedalus.activity.ConfigActivity.LAUNCH_ACTION_FRAGMENT";
public static final int LAUNCH_FRAGMENT_DNS_SERVER = 0;
public static final int LAUNCH_FRAGMENT_RULE = 1;
public static final String LAUNCH_ACTION_ID = "org.itxtech.daedalus.activity.ConfigActivity.LAUNCH_ACTION_ID";
public static final int ID_NONE = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dns_server_config);
DnsServerConfigFragment fragment = new DnsServerConfigFragment();
ConfigFragment fragment;
switch (getIntent().getIntExtra(LAUNCH_ACTION_FRAGMENT, LAUNCH_FRAGMENT_DNS_SERVER)) {
case LAUNCH_FRAGMENT_DNS_SERVER:
fragment = new DnsServerConfigFragment();
break;
case LAUNCH_FRAGMENT_RULE:
fragment = new RuleConfigFragment();
break;
default://should never reach this
fragment = new DnsServerConfigFragment();
break;
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_config);
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_clear);
@ -57,6 +75,23 @@ public class ConfigActivity extends AppCompatActivity {
fragmentTransaction.commit();
}
@Override
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_config);
switch (getIntent().getIntExtra(LAUNCH_ACTION_FRAGMENT, LAUNCH_FRAGMENT_DNS_SERVER)) {
case LAUNCH_FRAGMENT_DNS_SERVER:
toolbar.setTitle(R.string.config_dns_server);
break;
case LAUNCH_FRAGMENT_RULE:
toolbar.setTitle(R.string.config_rule);
break;
default://should never reach this
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();

View File

@ -0,0 +1,31 @@
package org.itxtech.daedalus.fragment;
import android.content.Intent;
import android.preference.PreferenceFragment;
import android.support.v7.widget.Toolbar;
/**
* 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.
*/
abstract public class ConfigFragment extends PreferenceFragment implements Toolbar.OnMenuItemClickListener {
protected Intent intent = null;
public void setIntent(Intent intent) {
this.intent = intent;
}
@Override
public void onDestroy() {
super.onDestroy();
intent = null;
}
}

View File

@ -2,13 +2,10 @@ package org.itxtech.daedalus.fragment;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
@ -30,20 +27,14 @@ import org.itxtech.daedalus.util.DnsServer;
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
public class DnsServerConfigFragment extends PreferenceFragment implements Toolbar.OnMenuItemClickListener {
private Intent intent = null;
public class DnsServerConfigFragment extends ConfigFragment {
private View view;
private int index;
public void setIntent(Intent intent) {
this.intent = intent;
}
@Override
public void onDestroy() {
super.onDestroy();
intent = null;
view = null;
}
@ -85,8 +76,8 @@ public class DnsServerConfigFragment extends PreferenceFragment implements Toolb
});
index = intent.getIntExtra(ConfigActivity.LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID, ConfigActivity.CUSTOM_DNS_SERVER_ID_NONE);
if (index != ConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) {
index = intent.getIntExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE);
if (index != ConfigActivity.ID_NONE) {
CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(index);
serverName.setText(server.getName());
serverName.setSummary(server.getName());
@ -120,7 +111,7 @@ public class DnsServerConfigFragment extends PreferenceFragment implements Toolb
break;
}
if (index == ConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) {
if (index == ConfigActivity.ID_NONE) {
Daedalus.configurations.getCustomDnsServers().add(new CustomDnsServer(serverName, serverAddress, Integer.parseInt(serverPort)));
} else {
CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(index);
@ -131,7 +122,7 @@ public class DnsServerConfigFragment extends PreferenceFragment implements Toolb
getActivity().finish();
break;
case R.id.action_delete:
if (index != ConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) {
if (index != ConfigActivity.ID_NONE) {
new AlertDialog.Builder(getActivity())
.setTitle(R.string.notice_delete_confirm_prompt)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

View File

@ -73,8 +73,9 @@ public class DnsServersFragment extends Fragment {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), ConfigActivity.class).putExtra(ConfigActivity.LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID,
ConfigActivity.CUSTOM_DNS_SERVER_ID_NONE));
startActivity(new Intent(getActivity(), ConfigActivity.class)
.putExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE)
.putExtra(ConfigActivity.LAUNCH_ACTION_FRAGMENT, ConfigActivity.LAUNCH_FRAGMENT_DNS_SERVER));
}
});
return view;
@ -155,7 +156,7 @@ public class DnsServersFragment extends Fragment {
public void onClick(View v) {
if (!DnsServerHelper.isInUsing(Daedalus.configurations.getCustomDnsServers().get(index))) {
Daedalus.getInstance().startActivity(new Intent(Daedalus.getInstance(), ConfigActivity.class)
.putExtra(ConfigActivity.LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID, index));
.putExtra(ConfigActivity.LAUNCH_ACTION_ID, index));
}
}
}

View File

@ -0,0 +1,174 @@
package org.itxtech.daedalus.fragment;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.ConfigActivity;
import org.itxtech.daedalus.util.Rule;
/**
* 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 RuleConfigFragment extends ConfigFragment {
private Intent intent = null;
private View view;
private int index;
public void setIntent(Intent intent) {
this.intent = intent;
}
@Override
public void onDestroy() {
super.onDestroy();
intent = null;
view = null;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.perf_rule);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = super.onCreateView(inflater, container, savedInstanceState);
EditTextPreference ruleName = (EditTextPreference) findPreference("ruleName");
ruleName.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String) newValue);
return true;
}
});
ListPreference ruleType = (ListPreference) findPreference("ruleType");
final String[] entries = {"hosts", "DNSMasq"};
String[] values = {"0", "1"};
ruleType.setEntries(entries);
ruleType.setEntryValues(values);
ruleType.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary(entries[Integer.parseInt((String) newValue)]);
return true;
}
});
EditTextPreference ruleDownloadUrl = (EditTextPreference) findPreference("ruleDownloadUrl");
ruleDownloadUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String) newValue);
return true;
}
});
EditTextPreference ruleFilename = (EditTextPreference) findPreference("ruleFilename");
ruleFilename.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String) newValue);
return true;
}
});
index = intent.getIntExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE);
if (index != ConfigActivity.ID_NONE) {
Rule rule = Daedalus.configurations.getRules().get(index);
ruleName.setText(rule.getName());
ruleName.setSummary(rule.getName());
int type = rule.getType();
ruleType.setValue(String.valueOf(type));
ruleType.setSummary(entries[type]);
ruleFilename.setText(rule.getFileName());
ruleFilename.setSummary(rule.getFileName());
ruleDownloadUrl.setText(rule.getDownloadUrl());
ruleDownloadUrl.setSummary(rule.getDownloadUrl());
} else {
ruleName.setText("");
ruleName.setSummary("");
ruleType.setValue("0");
ruleType.setSummary(entries[Rule.TYPE_HOSTS]);
ruleFilename.setText("");
ruleFilename.setSummary("");
ruleDownloadUrl.setText("");
ruleDownloadUrl.setSummary("");
}
return view;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_apply:
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();
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();
break;
case R.id.action_delete:
if (index != ConfigActivity.ID_NONE) {
new AlertDialog.Builder(getActivity())
.setTitle(R.string.notice_delete_confirm_prompt)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Daedalus.configurations.getRules().remove(index);
getActivity().finish();
}
})
.setNegativeButton(R.string.no, null)
.create()
.show();
} else {
getActivity().finish();
}
break;
}
return true;
}
}

View File

@ -1,7 +1,9 @@
package org.itxtech.daedalus.fragment;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
@ -12,10 +14,19 @@ import android.view.ViewGroup;
import android.widget.TextView;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.ConfigActivity;
import org.itxtech.daedalus.util.Rule;
/**
* @author PeratX
* 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 RulesFragment extends Fragment {
@ -60,6 +71,15 @@ public class RulesFragment extends Fragment {
});
itemTouchHelper.attachToRecyclerView(recyclerView);
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab_add_rule);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), ConfigActivity.class)
.putExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE)
.putExtra(ConfigActivity.LAUNCH_ACTION_FRAGMENT, ConfigActivity.LAUNCH_FRAGMENT_RULE));
}
});
return view;
}
@ -104,17 +124,17 @@ public class RulesFragment extends Fragment {
@Override
public int getItemCount() {
return Daedalus.configurations.getCustomDnsServers().size();
return Daedalus.configurations.getRules().size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_server, parent, false);
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_rule, parent, false);
return new ViewHolder(view);
}
}
private static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private final TextView textViewName;
private final TextView textViewAddress;
private int index;
@ -124,6 +144,8 @@ public class RulesFragment extends Fragment {
textViewName = (TextView) view.findViewById(R.id.textView_rule_name);
textViewAddress = (TextView) view.findViewById(R.id.textView_rule_detail);
view.setOnClickListener(this);
view.setOnLongClickListener(this);
view.setBackgroundResource(R.drawable.background_selectable);
}
void setIndex(int index) {
@ -136,10 +158,17 @@ public class RulesFragment extends Fragment {
@Override
public void onClick(View v) {
/*if (!DnsServerHelper.isInUsing(Daedalus.configurations.getCustomDnsServers().get(index))) {
Daedalus.getInstance().startActivity(new Intent(Daedalus.getInstance(), DnsServerConfigActivity.class)
.putExtra(DnsServerConfigActivity.LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID, index));
}*/
v.setSelected(!v.isSelected());
}
@Override
public boolean onLongClick(View v) {
if (!Daedalus.configurations.getRules().get(index).isUsing()) {
Daedalus.getInstance().startActivity(new Intent(Daedalus.getInstance(), ConfigActivity.class)
.putExtra(ConfigActivity.LAUNCH_ACTION_ID, index)
.putExtra(ConfigActivity.LAUNCH_ACTION_FRAGMENT, ConfigActivity.LAUNCH_FRAGMENT_RULE));
}
return true;
}
}
}

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="@android:integer/config_mediumAnimTime"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:state_selected="true">
<color android:color="@color/colorPrimaryDark"/>
</item>
</selector>

View File

@ -5,7 +5,8 @@
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:focusable="true"
app:cardCornerRadius="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">

View File

@ -48,7 +48,7 @@
<string name="notice_need_storage_perm">Daedalus 需要访问外部储存以实现本地规则解析。</string>
<string name="action_rules">规则</string>
<string name="button_text_download_hosts">下载 hosts</string>
<string name="notice_start_download">正在下载 rules,请稍等 ……</string>
<string name="notice_start_download">正在下载规则,请稍等 ……</string>
<string name="notice_downloaded">已下载规则。</string>
<string name="notice_now_downloading">Daedalus 当前正在下载规则,请稍等。</string>
<string name="hosts_not_found">找不到本地 hosts 文件。</string>
@ -68,7 +68,15 @@
<string name="settings_use_dnsmasq">使用 DNSMasq 替代 Hosts</string>
<string name="button_text_download_dnsmasq">下载 DNSMasq 配置文件</string>
<string name="dnsmasq_path">本地 DNSMasq 配置路径:</string>
<string name="notice_delete_confirm_prompt">您确定要删除此 DNS 服务器</string>
<string name="notice_delete_confirm_prompt">您确定要删除此配置项</string>
<string name="yes"></string>
<string name="no"></string>
<string name="config_dns_server">配置 DNS 服务器</string>
<string name="config_rule">配置规则</string>
<string name="settings_rule_name">规则名称</string>
<string name="settings_rule_type">规则类型</string>
<string name="settings_rule">规则设置</string>
<string name="settings_rule_download_url">规则下载地址</string>
<string name="settings_rule_sync">同步规则</string>
<string name="settings_rule_filename" translatable="false">规则文件名</string>
</resources>

View File

@ -68,7 +68,15 @@
<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>
<string name="notice_delete_confirm_prompt">Are you sure you want to remove this DNS server?</string>
<string name="notice_delete_confirm_prompt">Are you sure you want to remove this configuration?</string>
<string name="yes">Yes</string>
<string name="no">No</string>
<string name="config_dns_server">DNS server config</string>
<string name="config_rule">Rule config</string>
<string name="settings_rule_name">Rule Name</string>
<string name="settings_rule_type">Rule Type</string>
<string name="settings_rule">Rule Settings</string>
<string name="settings_rule_download_url">Rule Download Url</string>
<string name="settings_rule_sync">Sync Rule</string>
<string name="settings_rule_filename">Rule File Name</string>
</resources>

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="ruleName"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_rule_name"/>
<PreferenceCategory
android:key="Rule Settings"
android:title="@string/settings_rule">
<ListPreference
android:key="ruleType"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_rule_type"/>
<EditTextPreference
android:key="ruleFilename"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_rule_filename"/>
<EditTextPreference
android:key="ruleDownloadUrl"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_rule_download_url"/>
<org.itxtech.daedalus.widget.ClickPreference
android:key="ruleSync"
android:title="@string/settings_rule_sync"/>
</PreferenceCategory>
</PreferenceScreen>