Added support for dynamic reload rules

This commit is contained in:
PeratX 2017-07-06 09:21:49 +08:00
parent f6cc1219dc
commit 6abe3cd734
10 changed files with 54 additions and 17 deletions

View File

@ -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;
}

View File

@ -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());

View File

@ -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;

View File

@ -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;
}
}

View File

@ -6,4 +6,10 @@
android:alphabeticShortcut="c"
android:numericShortcut="1"
app:showAsAction="always"/>
<item android:id="@+id/action_reload"
android:title="@string/action_reload"
android:alphabeticShortcut="r"
android:numericShortcut="2"
android:icon="@drawable/ic_refresh"
app:showAsAction="always"/>
</menu>

View File

@ -77,4 +77,7 @@
<string name="action_refresh">刷新</string>
<string name="settings_log_size">日志大小限制</string>
<string name="server_cutedns_south_china">CuteDNS 华南</string>
<string name="settings_allow_dynamic_rule_reload">允许动态更新规则</string>
<string name="action_reload">重新加载规则</string>
<string name="notice_check_dynamic_rule_reload">请在设置中勾选“允许动态更新规则”</string>
</resources>

View File

@ -77,4 +77,7 @@
<string name="action_refresh">刷新</string>
<string name="settings_log_size">日誌大小限制</string>
<string name="server_cutedns_south_china">CuteDNS 華南</string>
<string name="settings_allow_dynamic_rule_reload">允许动态更新规则</string>
<string name="action_reload">重新加载规则</string>
<string name="notice_check_dynamic_rule_reload">请在设置中勾选“允许动态更新规则”</string>
</resources>

View File

@ -13,6 +13,7 @@
<string name="notice_fill_in_all">Please fill in all configuration items.</string>
<string name="notice_delete_confirm_prompt">Are you sure you want to remove this configuration?</string>
<string name="notice_after_stop">Please modify activated rules after stopping service.</string>
<string name="notice_check_dynamic_rule_reload">Please enable "Allow dynamic rule reload" in Settings</string>
<string name="action_settings">Settings</string>
<string name="action_about">About</string>
@ -25,6 +26,7 @@
<string name="action_undo">Undo</string>
<string name="action_log">Log</string>
<string name="action_refresh">Refresh</string>
<string name="action_reload">Reload</string>
<string name="delete">Delete</string>
<string name="apply">Apply</string>
@ -56,6 +58,7 @@
<string name="settings_log_size">Log size limit</string>
<string name="settings_manual">Manual</string>
<string name="settings_manual_summary">Visit GitHub wiki page.</string>
<string name="settings_allow_dynamic_rule_reload">Allow dynamic rule reload</string>
<string name="settings_rule_name">Rule Name</string>
<string name="settings_rule_type">Rule Type</string>

View File

@ -61,6 +61,11 @@
android:title="@string/settings_local_rules_resolution"
android:defaultValue="false"
android:enabled="false"/>
<SwitchPreference
android:key="settings_allow_dynamic_rule_reload"
android:title="@string/settings_allow_dynamic_rule_reload"
android:defaultValue="false"
android:enabled="false"/>
<SwitchPreference
android:key="settings_dns_over_tcp"
android:title="@string/settings_dns_over_tcp"