From 6abe3cd734ed1296e414a51b9ea2a690283aa569 Mon Sep 17 00:00:00 2001 From: PeratX <1215714524@qq.com> Date: Thu, 6 Jul 2017 09:21:49 +0800 Subject: [PATCH] Added support for dynamic reload rules --- .../java/org/itxtech/daedalus/Daedalus.java | 7 ++++ .../daedalus/activity/ConfigActivity.java | 2 +- .../fragment/DNSServerConfigFragment.java | 2 + .../daedalus/fragment/RulesFragment.java | 40 +++++++++++-------- ..._dns_server_menu.xml => custom_config.xml} | 0 app/src/main/res/menu/rules.xml | 6 +++ app/src/main/res/values-zh-rCN/strings.xml | 3 ++ app/src/main/res/values-zh-rTW/strings.xml | 3 ++ app/src/main/res/values/strings.xml | 3 ++ app/src/main/res/xml/perf_settings.xml | 5 +++ 10 files changed, 54 insertions(+), 17 deletions(-) rename app/src/main/res/menu/{custom_dns_server_menu.xml => custom_config.xml} (100%) diff --git a/app/src/main/java/org/itxtech/daedalus/Daedalus.java b/app/src/main/java/org/itxtech/daedalus/Daedalus.java index 4371dfc..927a42f 100644 --- a/app/src/main/java/org/itxtech/daedalus/Daedalus.java +++ b/app/src/main/java/org/itxtech/daedalus/Daedalus.java @@ -167,6 +167,13 @@ public class Daedalus extends Application { } } + public static void setRulesChanged() { + if (instance.isServiceActivated() && + getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false)) { + initHostsResolver(); + } + } + public static SharedPreferences getPrefs() { return getInstance().prefs; } diff --git a/app/src/main/java/org/itxtech/daedalus/activity/ConfigActivity.java b/app/src/main/java/org/itxtech/daedalus/activity/ConfigActivity.java index e1c7e32..6215f34 100644 --- a/app/src/main/java/org/itxtech/daedalus/activity/ConfigActivity.java +++ b/app/src/main/java/org/itxtech/daedalus/activity/ConfigActivity.java @@ -65,7 +65,7 @@ public class ConfigActivity extends AppCompatActivity { } }); toolbar.setOnMenuItemClickListener(fragment); - toolbar.inflateMenu(R.menu.custom_dns_server_menu); + toolbar.inflateMenu(R.menu.custom_config); FragmentManager manager = getFragmentManager(); fragment.setIntent(getIntent()); diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/DNSServerConfigFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/DNSServerConfigFragment.java index b56ae1e..f05e844 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/DNSServerConfigFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/DNSServerConfigFragment.java @@ -111,6 +111,7 @@ public class DNSServerConfigFragment extends ConfigFragment { server.setAddress(serverAddress); server.setPort(Integer.parseInt(serverPort)); } + Daedalus.setRulesChanged(); getActivity().finish(); break; case R.id.action_delete: @@ -128,6 +129,7 @@ public class DNSServerConfigFragment extends ConfigFragment { .create() .show(); } else { + Daedalus.setRulesChanged(); getActivity().finish(); } break; diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/RulesFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/RulesFragment.java index 27320e0..565ef26 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/RulesFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/RulesFragment.java @@ -59,12 +59,12 @@ public class RulesFragment extends ToolbarFragment implements Toolbar.OnMenuItem ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() { @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { - if (viewHolder instanceof RulesFragment.ViewHolder) { - Rule rule = Rule.getRuleById(((ViewHolder) viewHolder).getId()); - if (rule != null && rule.isServiceAndUsing()) { - /*Snackbar.make(getView(), R.string.notice_after_stop, Snackbar.LENGTH_LONG) - .setAction("Action", null).show();*/ - return 0; + if (!Daedalus.getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false)) { + if (viewHolder instanceof RulesFragment.ViewHolder) { + Rule rule = Rule.getRuleById(((ViewHolder) viewHolder).getId()); + if (rule != null && rule.isServiceAndUsing()) { + return 0; + } } } return makeMovementFlags(0, ItemTouchHelper.START | ItemTouchHelper.END); @@ -122,6 +122,15 @@ public class RulesFragment extends ToolbarFragment implements Toolbar.OnMenuItem toolbar.getMenu().findItem(R.id.action_change_type).setTitle(Rule.getTypeById(currentType)); adapter.notifyDataSetChanged(); } + + if (id == R.id.action_reload) { + if (!Daedalus.getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false)) { + Snackbar.make(getView(), R.string.notice_check_dynamic_rule_reload, Snackbar.LENGTH_SHORT) + .setAction("Action", null).show(); + } else { + Daedalus.setRulesChanged(); + } + } return true; } @@ -223,30 +232,29 @@ public class RulesFragment extends ToolbarFragment implements Toolbar.OnMenuItem @Override public void onClick(View v) { - if (!Daedalus.getInstance().isServiceActivated()) { + if ((!Daedalus.getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false) && + !Daedalus.getInstance().isServiceActivated()) || + Daedalus.getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false)) { Rule rule = Rule.getRuleById(id); if (rule != null) { rule.setUsing(!v.isSelected()); v.setSelected(!v.isSelected()); + Daedalus.setRulesChanged(); } - }/* else { - Snackbar.make(view, R.string.notice_after_stop, Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - }*/ + } } @Override public boolean onLongClick(View v) { Rule rule = Rule.getRuleById(id); - if (rule != null && !rule.isServiceAndUsing()) { + if (rule != null && + (Daedalus.getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false) || + !rule.isServiceAndUsing())) { Daedalus.getInstance().startActivity(new Intent(Daedalus.getInstance(), ConfigActivity.class) .putExtra(ConfigActivity.LAUNCH_ACTION_ID, Integer.parseInt(id)) .putExtra(ConfigActivity.LAUNCH_ACTION_FRAGMENT, ConfigActivity.LAUNCH_FRAGMENT_RULE) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); - }/* else { - Snackbar.make(view, R.string.notice_after_stop, Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - }*/ + } return true; } } diff --git a/app/src/main/res/menu/custom_dns_server_menu.xml b/app/src/main/res/menu/custom_config.xml similarity index 100% rename from app/src/main/res/menu/custom_dns_server_menu.xml rename to app/src/main/res/menu/custom_config.xml diff --git a/app/src/main/res/menu/rules.xml b/app/src/main/res/menu/rules.xml index 98881cd..fed1964 100644 --- a/app/src/main/res/menu/rules.xml +++ b/app/src/main/res/menu/rules.xml @@ -6,4 +6,10 @@ android:alphabeticShortcut="c" android:numericShortcut="1" app:showAsAction="always"/> + diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 00409d3..f2b724f 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -77,4 +77,7 @@ 刷新 日志大小限制 CuteDNS 华南 + 允许动态更新规则 + 重新加载规则 + 请在设置中勾选“允许动态更新规则” \ No newline at end of file diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index a52793e..3d45ba0 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -77,4 +77,7 @@ 刷新 日誌大小限制 CuteDNS 華南 + 允许动态更新规则 + 重新加载规则 + 请在设置中勾选“允许动态更新规则” diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 31b4473..9ccec69 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,6 +13,7 @@ Please fill in all configuration items. Are you sure you want to remove this configuration? Please modify activated rules after stopping service. + Please enable "Allow dynamic rule reload" in Settings Settings About @@ -25,6 +26,7 @@ Undo Log Refresh + Reload Delete Apply @@ -56,6 +58,7 @@ Log size limit Manual Visit GitHub wiki page. + Allow dynamic rule reload Rule Name Rule Type diff --git a/app/src/main/res/xml/perf_settings.xml b/app/src/main/res/xml/perf_settings.xml index bbee037..e31b29a 100644 --- a/app/src/main/res/xml/perf_settings.xml +++ b/app/src/main/res/xml/perf_settings.xml @@ -61,6 +61,11 @@ android:title="@string/settings_local_rules_resolution" android:defaultValue="false" android:enabled="false"/> +