diff --git a/app/src/main/java/org/itxtech/daedalus/Daedalus.java b/app/src/main/java/org/itxtech/daedalus/Daedalus.java index fad12cb..7c0c8a7 100644 --- a/app/src/main/java/org/itxtech/daedalus/Daedalus.java +++ b/app/src/main/java/org/itxtech/daedalus/Daedalus.java @@ -129,22 +129,23 @@ public class Daedalus extends Application { public static void initHostsResolver() { if (Daedalus.getPrefs().getBoolean("settings_local_rules_resolution", false)) { ArrayList pendingLoad = new ArrayList<>(); - int type = Rule.TYPE_HOSTS; - for (Rule rule : configurations.getRules()) { + ArrayList usingRules = configurations.getUsingRules(); + for (Rule rule : usingRules) { 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; + if (pendingLoad.size() > 0) { + String[] arr = new String[pendingLoad.size()]; + pendingLoad.toArray(arr); + switch (usingRules.get(0).getType()) { + case Rule.TYPE_HOSTS: + RulesResolver.startLoadHosts(arr); + break; + case Rule.TYPE_DNAMASQ: + RulesResolver.startLoadDnsmasq(arr); + break; + } } } } diff --git a/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java b/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java index 7a09d35..3ca4309 100644 --- a/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java +++ b/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java @@ -14,7 +14,6 @@ import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; -import android.view.Menu; import android.view.MenuItem; import android.view.inputmethod.InputMethodManager; import android.widget.TextView; @@ -58,7 +57,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On private DnsTestFragment mDnsTest; private SettingsFragment mSettings; private AboutFragment mAbout; - private RulesFragment mHosts; + private RulesFragment mRules; private DnsServersFragment mDnsServers; private int currentFragment = FRAGMENT_NONE; @@ -105,68 +104,36 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On public void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); - updateTitle(); - updateNavigationMenu(); - } - - private void updateNavigationMenu() { - Menu menu = ((NavigationView) findViewById(R.id.nav_view)).getMenu(); switch (currentFragment) { case FRAGMENT_MAIN: - menu.findItem(R.id.nav_home).setChecked(true); + mMain.checkStatus(); break; case FRAGMENT_DNS_TEST: - menu.findItem(R.id.nav_dns_test).setChecked(true); + mDnsTest.checkStatus(); break; case FRAGMENT_SETTINGS: - menu.findItem(R.id.nav_settings).setChecked(true); + mSettings.checkStatus(); break; case FRAGMENT_ABOUT: - menu.findItem(R.id.nav_about).setChecked(true); + mAbout.checkStatus(); break; case FRAGMENT_RULES: - menu.findItem(R.id.nav_rules).setChecked(true); + mRules.checkStatus(); break; case FRAGMENT_DNS_SERVERS: - menu.findItem(R.id.nav_dns_server).setChecked(true); - } - } - - private void updateTitle() { - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); - switch (currentFragment) { - case FRAGMENT_MAIN: - toolbar.setTitle(R.string.action_home); - break; - case FRAGMENT_DNS_TEST: - toolbar.setTitle(R.string.action_dns_test); - break; - case FRAGMENT_SETTINGS: - toolbar.setTitle(R.string.action_settings); - break; - case FRAGMENT_ABOUT: - toolbar.setTitle(R.string.action_about); - break; - case FRAGMENT_RULES: - toolbar.setTitle(R.string.action_rules); - break; - case FRAGMENT_DNS_SERVERS: - toolbar.setTitle(R.string.action_dns_servers); - break; + mDnsServers.checkStatus(); } } private void switchFragment(int fragment) { FragmentManager fm = getFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); switch (fragment) { case FRAGMENT_MAIN: if (mMain == null) { mMain = new MainFragment(); } transaction.replace(R.id.id_content, mMain); - toolbar.setTitle(R.string.action_home); currentFragment = FRAGMENT_MAIN; break; case FRAGMENT_DNS_TEST: @@ -174,7 +141,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On mDnsTest = new DnsTestFragment(); } transaction.replace(R.id.id_content, mDnsTest); - toolbar.setTitle(R.string.action_dns_test); currentFragment = FRAGMENT_DNS_TEST; break; case FRAGMENT_SETTINGS: @@ -182,7 +148,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On mSettings = new SettingsFragment(); } transaction.replace(R.id.id_content, mSettings); - toolbar.setTitle(R.string.action_settings); currentFragment = FRAGMENT_SETTINGS; break; case FRAGMENT_ABOUT: @@ -190,15 +155,13 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On mAbout = new AboutFragment(); } transaction.replace(R.id.id_content, mAbout); - toolbar.setTitle(R.string.action_about); currentFragment = FRAGMENT_ABOUT; break; case FRAGMENT_RULES: - if (mHosts == null) { - mHosts = new RulesFragment(); + if (mRules == null) { + mRules = new RulesFragment(); } - transaction.replace(R.id.id_content, mHosts); - toolbar.setTitle(R.string.action_rules); + transaction.replace(R.id.id_content, mRules); currentFragment = FRAGMENT_RULES; break; case FRAGMENT_DNS_SERVERS: @@ -206,7 +169,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On mDnsServers = new DnsServersFragment(); } transaction.replace(R.id.id_content, mDnsServers); - toolbar.setTitle(R.string.action_dns_servers); currentFragment = FRAGMENT_DNS_SERVERS; break; } @@ -220,7 +182,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On drawer.closeDrawer(GravityCompat.START); } else if (currentFragment != FRAGMENT_MAIN) { switchFragment(FRAGMENT_MAIN); - updateNavigationMenu(); } else { super.onBackPressed(); } @@ -235,7 +196,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On mDnsTest = null; mSettings = null; mAbout = null; - mHosts = null; + mRules = null; instance = null; } @@ -263,7 +224,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On int fragment = intent.getIntExtra(LAUNCH_FRAGMENT, FRAGMENT_NONE); if (fragment != FRAGMENT_NONE) { switchFragment(fragment); - updateNavigationMenu(); } } diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/AboutFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/AboutFragment.java index 7bb6bd8..0e00dfc 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/AboutFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/AboutFragment.java @@ -1,7 +1,6 @@ package org.itxtech.daedalus.fragment; import android.annotation.SuppressLint; -import android.app.Fragment; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -28,7 +27,7 @@ import java.util.Locale; * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. */ -public class AboutFragment extends Fragment { +public class AboutFragment extends ToolbarFragment { private WebView mWebView = null; @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled", "addJavascriptInterface"}) @@ -76,6 +75,12 @@ public class AboutFragment extends Fragment { return view; } + @Override + public void checkStatus() { + menu.findItem(R.id.nav_about).setChecked(true); + toolbar.setTitle(R.string.action_about); + } + @Override public void onDestroyView() { super.onDestroyView(); diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/DnsServersFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/DnsServersFragment.java index 6a86d6a..3863fe5 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/DnsServersFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/DnsServersFragment.java @@ -1,6 +1,5 @@ package org.itxtech.daedalus.fragment; -import android.app.Fragment; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; @@ -29,7 +28,7 @@ import org.itxtech.daedalus.util.DnsServerHelper; * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. */ -public class DnsServersFragment extends Fragment { +public class DnsServersFragment extends ToolbarFragment { private DnsServersFragment.DnsServerAdapter adapter; private CustomDnsServer server = null; @@ -82,6 +81,12 @@ public class DnsServersFragment extends Fragment { return view; } + @Override + public void checkStatus() { + menu.findItem(R.id.nav_dns_server).setChecked(true); + toolbar.setTitle(R.string.action_dns_servers); + } + private class SnackbarClickListener implements View.OnClickListener { private final int position; diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/DnsTestFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/DnsTestFragment.java index 406af67..0182c1c 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/DnsTestFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/DnsTestFragment.java @@ -1,6 +1,5 @@ package org.itxtech.daedalus.fragment; -import android.app.Fragment; import android.content.Context; import android.os.Bundle; import android.os.Handler; @@ -41,7 +40,7 @@ import java.util.Set; * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. */ -public class DnsTestFragment extends Fragment { +public class DnsTestFragment extends ToolbarFragment { private static final String TAG = "DServerTest"; private static Thread mThread = null; @@ -156,6 +155,12 @@ public class DnsTestFragment extends Fragment { return view; } + @Override + public void checkStatus() { + menu.findItem(R.id.nav_dns_test).setChecked(true); + toolbar.setTitle(R.string.action_dns_test); + } + @Override public void onDestroy() { super.onDestroy(); diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/GlobalConfigFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/GlobalConfigFragment.java new file mode 100644 index 0000000..f6f3523 --- /dev/null +++ b/app/src/main/java/org/itxtech/daedalus/fragment/GlobalConfigFragment.java @@ -0,0 +1,160 @@ +package org.itxtech.daedalus.fragment; + +import android.content.Intent; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.preference.*; +import android.support.design.widget.Snackbar; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import org.itxtech.daedalus.Daedalus; +import org.itxtech.daedalus.R; +import org.itxtech.daedalus.util.DnsServerHelper; + +/** + * Daedalus Project + * + * @author iTX Technologies + * @link https://itxtech.org + *

+ * 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 GlobalConfigFragment extends PreferenceFragment { + private View view = null; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + Daedalus.getPrefs().edit() + .putString("primary_server", DnsServerHelper.getPrimary()) + .putString("secondary_server", DnsServerHelper.getSecondary()) + .apply(); + + addPreferencesFromResource(R.xml.perf_settings); + + ListPreference primaryServer = (ListPreference) findPreference("primary_server"); + primaryServer.setEntries(DnsServerHelper.getNames(Daedalus.getInstance())); + primaryServer.setEntryValues(DnsServerHelper.getIds()); + primaryServer.setSummary(DnsServerHelper.getDescription(primaryServer.getValue(), Daedalus.getInstance())); + primaryServer.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + preference.setSummary(DnsServerHelper.getDescription((String) newValue, Daedalus.getInstance())); + Snackbar.make(view, R.string.notice_need_restart, Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); + return true; + } + }); + + ListPreference secondaryServer = (ListPreference) findPreference("secondary_server"); + secondaryServer.setEntries(DnsServerHelper.getNames(Daedalus.getInstance())); + secondaryServer.setEntryValues(DnsServerHelper.getIds()); + secondaryServer.setSummary(DnsServerHelper.getDescription(secondaryServer.getValue(), Daedalus.getInstance())); + secondaryServer.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + preference.setSummary(DnsServerHelper.getDescription((String) newValue, Daedalus.getInstance())); + Snackbar.make(view, R.string.notice_need_restart, Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); + return true; + } + }); + + EditTextPreference testDNSServers = (EditTextPreference) findPreference("dns_test_servers"); + testDNSServers.setSummary(testDNSServers.getText()); + testDNSServers.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + preference.setSummary((String) newValue); + return true; + } + }); + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + SwitchPreference advanced = (SwitchPreference) findPreference("settings_advanced_switch"); + advanced.setEnabled(false); + advanced.setChecked(false); + SwitchPreference boot = (SwitchPreference) findPreference("settings_boot"); + boot.setEnabled(false); + boot.setChecked(false); + } + + SwitchPreference advanced = (SwitchPreference) findPreference("settings_advanced_switch"); + advanced.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + updateAdvancedOptions((boolean) newValue); + return true; + } + }); + + findPreference("settings_check_update").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus/releases"))); + return false; + } + }); + + findPreference("settings_issue_tracker").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus/issues"))); + return false; + } + }); + + findPreference("settings_manual").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus/wiki"))); + return false; + } + }); + + updateAdvancedOptions(advanced.isChecked()); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + view = super.onCreateView(inflater, container, savedInstanceState); + return view; + } + + private void updateAdvancedOptions(boolean checked) { + PreferenceCategory category = (PreferenceCategory) findPreference("settings_advanced"); + for (int i = 1; i < category.getPreferenceCount(); i++) { + Preference preference = category.getPreference(i); + if (checked) { + preference.setEnabled(true); + } else { + preference.setEnabled(false); + if (preference instanceof SwitchPreference) { + ((SwitchPreference) preference).setChecked(false); + } + } + } + } + + @Override + public void onStart() { + super.onStart(); + if (view != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + Snackbar.make(view, R.string.notice_legacy_api, Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); + } + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + + view = null; + } +} diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/MainFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/MainFragment.java index bb87b20..8076c7f 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/MainFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/MainFragment.java @@ -1,7 +1,6 @@ package org.itxtech.daedalus.fragment; import android.app.Activity; -import android.app.Fragment; import android.content.DialogInterface; import android.content.Intent; import android.net.VpnService; @@ -30,7 +29,7 @@ import org.itxtech.daedalus.util.DnsServerHelper; * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. */ -public class MainFragment extends Fragment { +public class MainFragment extends ToolbarFragment { private View view = null; public static MainFragmentHandler mHandler = null; @@ -61,6 +60,12 @@ public class MainFragment extends Fragment { return view; } + @Override + public void checkStatus() { + menu.findItem(R.id.nav_home).setChecked(true); + toolbar.setTitle(R.string.action_home); + } + @Override public void onDetach() { super.onDetach(); diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/RuleConfigFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/RuleConfigFragment.java index 564fc47..5b797f9 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/RuleConfigFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/RuleConfigFragment.java @@ -220,7 +220,7 @@ public class RuleConfigFragment extends ConfigFragment { if (id == ConfigActivity.ID_NONE) { Rule rule = new Rule(ruleName, ruleFilename, Integer.parseInt(ruleType), ruleDownloadUrl); - Daedalus.configurations.getRules().add(rule); + rule.addToConfig(); id = Integer.parseInt(rule.getId()); } else { Rule rule = Rule.getRuleById(String.valueOf(id)); @@ -251,7 +251,10 @@ public class RuleConfigFragment extends ConfigFragment { .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - Daedalus.configurations.getRules().remove(RuleConfigFragment.this.id); + Rule rule = Rule.getRuleById(String.valueOf(RuleConfigFragment.this.id)); + if (rule != null) { + rule.removeFromConfig(); + } getActivity().finish(); } }) 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 34e4d6d..751d141 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/RulesFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/RulesFragment.java @@ -1,14 +1,15 @@ 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; +import android.support.v7.widget.Toolbar; import android.support.v7.widget.helper.ItemTouchHelper; import android.view.LayoutInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @@ -19,7 +20,7 @@ import org.itxtech.daedalus.util.Rule; import java.io.File; import java.text.DecimalFormat; -import java.util.concurrent.CopyOnWriteArrayList; +import java.util.ArrayList; /** * Daedalus Project @@ -32,15 +33,24 @@ import java.util.concurrent.CopyOnWriteArrayList; * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. */ -public class RulesFragment extends Fragment { - +public class RulesFragment extends ToolbarFragment implements Toolbar.OnMenuItemClickListener { private View view = null; private RuleAdapter adapter; private Rule rule = null; + private int currentType; + + private ArrayList getRules() { + if (currentType == Rule.TYPE_HOSTS) { + return Daedalus.configurations.getHostsRules(); + } else { + return Daedalus.configurations.getDnsmasqRules(); + } + } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_rules, container, false); + currentType = Rule.TYPE_HOSTS; RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_rules); LinearLayoutManager manager = new LinearLayoutManager(getActivity()); @@ -69,8 +79,8 @@ public class RulesFragment extends Fragment { @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { int position = viewHolder.getAdapterPosition(); - rule = Daedalus.configurations.getRules().get(position); - Daedalus.configurations.getRules().remove(position); + rule = getRules().get(position); + getRules().remove(position); Snackbar.make(view, R.string.action_removed, Snackbar.LENGTH_LONG) .setAction(R.string.action_undo, new SnackbarClickListener(position)).show(); adapter.notifyItemRemoved(position); @@ -91,6 +101,31 @@ public class RulesFragment extends Fragment { return view; } + @Override + public void checkStatus() { + menu.findItem(R.id.nav_rules).setChecked(true); + toolbar.inflateMenu(R.menu.rules); + toolbar.setTitle(R.string.action_rules); + toolbar.setOnMenuItemClickListener(this); + } + + @Override + public boolean onMenuItemClick(MenuItem item) { + int id = item.getItemId(); + + if (id == R.id.action_change_type) { + if (currentType == Rule.TYPE_HOSTS) { + currentType = Rule.TYPE_DNAMASQ; + item.setTitle("DNSMasq"); + } else if (currentType == Rule.TYPE_DNAMASQ) { + currentType = Rule.TYPE_HOSTS; + item.setTitle("Hosts"); + } + adapter.notifyDataSetChanged(); + } + return true; + } + @Override public void onDestroyView() { super.onDestroyView(); @@ -117,17 +152,21 @@ public class RulesFragment extends Fragment { @Override public void onClick(View v) { - Daedalus.configurations.getRules().add(position, rule); - adapter.notifyItemInserted(position); + if (rule.getType() == Rule.TYPE_HOSTS) { + Daedalus.configurations.getHostsRules().add(position, rule); + } else if (rule.getType() == Rule.TYPE_DNAMASQ) { + Daedalus.configurations.getDnsmasqRules().add(position, rule); + } + if (currentType == rule.getType()) { + adapter.notifyItemInserted(position); + } } } private class RuleAdapter extends RecyclerView.Adapter { - private CopyOnWriteArrayList selectedItems = new CopyOnWriteArrayList<>(); - @Override public void onBindViewHolder(ViewHolder holder, int position) { - Rule rule = Daedalus.configurations.getRules().get(position); + Rule rule = getRules().get(position); holder.setId(rule.getId()); holder.textViewName.setText(rule.getName()); holder.textViewAddress.setText(rule.getFileName()); @@ -144,7 +183,7 @@ public class RulesFragment extends Fragment { @Override public int getItemCount() { - return Daedalus.configurations.getRules().size(); + return getRules().size(); } @Override @@ -152,19 +191,6 @@ public class RulesFragment extends Fragment { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_rule, parent, false); return new ViewHolder(view); } - - void checkType(Rule rule, ViewHolder holder) { - for (ViewHolder viewHolder : selectedItems) { - Rule check = Rule.getRuleById(viewHolder.getId()); - if (check != null && check.getType() != rule.getType()) { - viewHolder.view.setSelected(false); - check.setUsing(false); - selectedItems.remove(viewHolder); - notifyItemChanged(viewHolder.getLayoutPosition()); - } - } - selectedItems.add(holder); - } } private class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { @@ -190,9 +216,6 @@ public class RulesFragment extends Fragment { Rule rule = Rule.getRuleById(id); if (rule != null) { view.setSelected(rule.isUsing()); - if (view.isSelected()) { - adapter.selectedItems.add(this); - } } } @@ -207,9 +230,6 @@ public class RulesFragment extends Fragment { if (rule != null) { rule.setUsing(!v.isSelected()); v.setSelected(!v.isSelected()); - if (v.isSelected()) { - adapter.checkType(rule, this); - } } } else { Snackbar.make(view, R.string.notice_after_stop, Snackbar.LENGTH_LONG) diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/SettingsFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/SettingsFragment.java index f1af20e..13d0aa1 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/SettingsFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/SettingsFragment.java @@ -1,17 +1,11 @@ package org.itxtech.daedalus.fragment; -import android.content.Intent; -import android.net.Uri; -import android.os.Build; +import android.app.FragmentManager; import android.os.Bundle; -import android.preference.*; -import android.support.design.widget.Snackbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.R; -import org.itxtech.daedalus.util.DnsServerHelper; /** * Daedalus Project @@ -24,136 +18,22 @@ import org.itxtech.daedalus.util.DnsServerHelper; * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. */ -public class SettingsFragment extends PreferenceFragment { - private View view = null; - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - Daedalus.getPrefs().edit() - .putString("primary_server", DnsServerHelper.getPrimary()) - .putString("secondary_server", DnsServerHelper.getSecondary()) - .apply(); - - addPreferencesFromResource(R.xml.perf_settings); - - ListPreference primaryServer = (ListPreference) findPreference("primary_server"); - primaryServer.setEntries(DnsServerHelper.getNames(Daedalus.getInstance())); - primaryServer.setEntryValues(DnsServerHelper.getIds()); - primaryServer.setSummary(DnsServerHelper.getDescription(primaryServer.getValue(), Daedalus.getInstance())); - primaryServer.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - preference.setSummary(DnsServerHelper.getDescription((String) newValue, Daedalus.getInstance())); - Snackbar.make(view, R.string.notice_need_restart, Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - return true; - } - }); - - ListPreference secondaryServer = (ListPreference) findPreference("secondary_server"); - secondaryServer.setEntries(DnsServerHelper.getNames(Daedalus.getInstance())); - secondaryServer.setEntryValues(DnsServerHelper.getIds()); - secondaryServer.setSummary(DnsServerHelper.getDescription(secondaryServer.getValue(), Daedalus.getInstance())); - secondaryServer.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - preference.setSummary(DnsServerHelper.getDescription((String) newValue, Daedalus.getInstance())); - Snackbar.make(view, R.string.notice_need_restart, Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - return true; - } - }); - - EditTextPreference testDNSServers = (EditTextPreference) findPreference("dns_test_servers"); - testDNSServers.setSummary(testDNSServers.getText()); - testDNSServers.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - preference.setSummary((String) newValue); - return true; - } - }); - - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - SwitchPreference advanced = (SwitchPreference) findPreference("settings_advanced_switch"); - advanced.setEnabled(false); - advanced.setChecked(false); - SwitchPreference boot = (SwitchPreference) findPreference("settings_boot"); - boot.setEnabled(false); - boot.setChecked(false); - } - - SwitchPreference advanced = (SwitchPreference) findPreference("settings_advanced_switch"); - advanced.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - updateAdvancedOptions((boolean) newValue); - return true; - } - }); - - findPreference("settings_check_update").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus/releases"))); - return false; - } - }); - - findPreference("settings_issue_tracker").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus/issues"))); - return false; - } - }); - - findPreference("settings_manual").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus/wiki"))); - return false; - } - }); - - updateAdvancedOptions(advanced.isChecked()); - } - +public class SettingsFragment extends ToolbarFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - view = super.onCreateView(inflater, container, savedInstanceState); - return view; - } - - private void updateAdvancedOptions(boolean checked) { - PreferenceCategory category = (PreferenceCategory) findPreference("settings_advanced"); - for (int i = 1; i < category.getPreferenceCount(); i++) { - Preference preference = category.getPreference(i); - if (checked) { - preference.setEnabled(true); - } else { - preference.setEnabled(false); - if (preference instanceof SwitchPreference) { - ((SwitchPreference) preference).setChecked(false); - } - } - } + return inflater.inflate(R.layout.fragment_settings, container, false); } @Override - public void onStart() { - super.onStart(); - if (view != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - Snackbar.make(view, R.string.notice_legacy_api, Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - } + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + FragmentManager fm = getFragmentManager(); + fm.beginTransaction().replace(R.id.settings_content, new GlobalConfigFragment()).commit(); } @Override - public void onDestroyView() { - super.onDestroyView(); - - view = null; + public void checkStatus() { + menu.findItem(R.id.nav_settings).setChecked(true); + toolbar.setTitle(R.string.action_settings); } } diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/ToolbarFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/ToolbarFragment.java new file mode 100644 index 0000000..73fe87d --- /dev/null +++ b/app/src/main/java/org/itxtech/daedalus/fragment/ToolbarFragment.java @@ -0,0 +1,44 @@ +package org.itxtech.daedalus.fragment; + +import android.app.Fragment; +import android.os.Bundle; +import android.support.design.widget.NavigationView; +import android.support.v7.widget.Toolbar; +import android.view.Menu; +import android.view.View; +import org.itxtech.daedalus.R; + +/** + * Daedalus Project + * + * @author iTX Technologies + * @link https://itxtech.org + *

+ * 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 ToolbarFragment extends Fragment { + protected Toolbar toolbar; + protected Menu menu; + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + menu = ((NavigationView) getActivity().findViewById(R.id.nav_view)).getMenu(); + toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar); + toolbar.getMenu().clear(); + checkStatus(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + + toolbar = null; + menu = null; + } + + public abstract void checkStatus(); +} diff --git a/app/src/main/java/org/itxtech/daedalus/util/Configurations.java b/app/src/main/java/org/itxtech/daedalus/util/Configurations.java index fa2f089..c3da8e2 100644 --- a/app/src/main/java/org/itxtech/daedalus/util/Configurations.java +++ b/app/src/main/java/org/itxtech/daedalus/util/Configurations.java @@ -28,7 +28,8 @@ public class Configurations { private ArrayList customDnsServers; - private ArrayList rules; + private ArrayList hostsRules; + private ArrayList dnsmasqRules; private int totalDnsId; private int totalRuleId; @@ -64,11 +65,36 @@ public class Configurations { return customDnsServers; } - public ArrayList getRules() { - if (rules == null) { - rules = new ArrayList<>(); + public ArrayList getHostsRules() { + if (hostsRules == null) { + hostsRules = new ArrayList<>(); } - return rules; + return hostsRules; + } + + public ArrayList getDnsmasqRules() { + if (dnsmasqRules == null) { + dnsmasqRules = new ArrayList<>(); + } + return dnsmasqRules; + } + + public ArrayList getUsingRules() { + if (hostsRules != null && hostsRules.size() > 0) { + for (Rule rule : hostsRules) { + if (rule.isUsing()) { + return hostsRules; + } + } + } + if (dnsmasqRules != null && dnsmasqRules.size() > 0) { + for (Rule rule : dnsmasqRules) { + if (rule.isUsing()) { + return dnsmasqRules; + } + } + } + return hostsRules; } public static Configurations load(File file) { diff --git a/app/src/main/java/org/itxtech/daedalus/util/Rule.java b/app/src/main/java/org/itxtech/daedalus/util/Rule.java index 383cc17..eee900d 100644 --- a/app/src/main/java/org/itxtech/daedalus/util/Rule.java +++ b/app/src/main/java/org/itxtech/daedalus/util/Rule.java @@ -51,6 +51,17 @@ public class Rule { public void setUsing(boolean using) { this.using = using; + if (using) { + if (type == TYPE_HOSTS) { + for (Rule rule : Daedalus.configurations.getDnsmasqRules()) { + rule.setUsing(false); + } + } else if (type == TYPE_DNAMASQ) { + for (Rule rule : Daedalus.configurations.getHostsRules()) { + rule.setUsing(false); + } + } + } } public boolean isUsing() { @@ -82,7 +93,9 @@ public class Rule { } public void setType(int type) { + this.removeFromConfig(); this.type = type; + this.addToConfig(); } public void setDownloadUrl(String downloadUrl) { @@ -93,6 +106,22 @@ public class Rule { return Daedalus.getInstance().isServiceActivated() && isUsing(); } + public void addToConfig() { + if (getType() == Rule.TYPE_HOSTS) { + Daedalus.configurations.getHostsRules().add(this); + } else if (getType() == Rule.TYPE_DNAMASQ) { + Daedalus.configurations.getDnsmasqRules().add(this); + } + } + + public void removeFromConfig() { + if (getType() == Rule.TYPE_HOSTS) { + Daedalus.configurations.getHostsRules().remove(this); + } else if (getType() == Rule.TYPE_DNAMASQ) { + Daedalus.configurations.getDnsmasqRules().remove(this); + } + } + //STATIC METHODS public static String[] getBuildInRuleNames() { @@ -114,7 +143,12 @@ public class Rule { } public static Rule getRuleById(String id) { - for (Rule rule : Daedalus.configurations.getRules()) { + for (Rule rule : Daedalus.configurations.getHostsRules()) { + if (rule.getId().equals(id)) { + return rule; + } + } + for (Rule rule : Daedalus.configurations.getDnsmasqRules()) { if (rule.getId().equals(id)) { return rule; } diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml new file mode 100644 index 0000000..bbe9ac2 --- /dev/null +++ b/app/src/main/res/layout/fragment_settings.xml @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/app/src/main/res/menu/rules.xml b/app/src/main/res/menu/rules.xml new file mode 100644 index 0000000..98881cd --- /dev/null +++ b/app/src/main/res/menu/rules.xml @@ -0,0 +1,9 @@ + +

+ +