From d2ebad1f5a9f0dc279b1c36b1e536a875c0a0ab7 Mon Sep 17 00:00:00 2001 From: PeratX <1215714524@qq.com> Date: Mon, 1 May 2017 15:34:57 +0800 Subject: [PATCH] Fixed crash caused by missing custom DNS server --- .../daedalus/activity/MainActivity.java | 4 + .../fragment/DnsServerConfigFragment.java | 27 +++-- .../daedalus/fragment/DnsTestFragment.java | 17 ++- .../daedalus/fragment/MainFragment.java | 6 +- .../daedalus/fragment/SettingsFragment.java | 24 ++-- .../receiver/BootBroadcastReceiver.java | 6 +- .../org/itxtech/daedalus/util/DnsServer.java | 73 ------------ .../daedalus/util/DnsServerHelper.java | 105 ++++++++++++++++++ app/src/main/res/values-zh/strings.xml | 1 + app/src/main/res/values/strings.xml | 3 +- 10 files changed, 162 insertions(+), 104 deletions(-) create mode 100644 app/src/main/java/org/itxtech/daedalus/util/DnsServerHelper.java 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 ef60ad4..8170d2b 100644 --- a/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java +++ b/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java @@ -74,6 +74,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On return instance; } + public int getCurrentFragment() { + return currentFragment; + } + @Override protected void onCreate(Bundle savedInstanceState) { setTheme(R.style.AppTheme_NoActionBar_TransparentStatusBar); 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 541b87d..4976382 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/DnsServerConfigFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/DnsServerConfigFragment.java @@ -5,6 +5,7 @@ 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; @@ -27,6 +28,7 @@ import org.itxtech.daedalus.util.CustomDnsServer; */ public class DnsServerConfigFragment extends PreferenceFragment implements Toolbar.OnMenuItemClickListener { private Intent intent = null; + private View view; private int index; public void setIntent(Intent intent) { @@ -38,6 +40,7 @@ public class DnsServerConfigFragment extends PreferenceFragment implements Toolb super.onDestroy(); intent = null; + view = null; } @Override @@ -48,7 +51,7 @@ public class DnsServerConfigFragment extends PreferenceFragment implements Toolb @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = super.onCreateView(inflater, container, savedInstanceState); + view = super.onCreateView(inflater, container, savedInstanceState); EditTextPreference serverName = (EditTextPreference) findPreference("serverName"); serverName.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @@ -101,17 +104,23 @@ public class DnsServerConfigFragment extends PreferenceFragment implements Toolb switch (id) { case R.id.action_apply: + String serverName = ((EditTextPreference) findPreference("serverName")).getText(); + String serverAddress = ((EditTextPreference) findPreference("serverAddress")).getText(); + String serverPort = ((EditTextPreference) findPreference("serverPort")).getText(); + + if (serverName.equals("") | serverAddress.equals("") | serverPort.equals("")) { + Snackbar.make(view, R.string.notice_fill_in_all, Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); + break; + } + if (index == DnsServerConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) { - Daedalus.configurations.getCustomDnsServers().add(new CustomDnsServer( - ((EditTextPreference) findPreference("serverName")).getText(), - ((EditTextPreference) findPreference("serverAddress")).getText(), - Integer.parseInt(((EditTextPreference) findPreference("serverPort")).getText()) - )); + Daedalus.configurations.getCustomDnsServers().add(new CustomDnsServer(serverName, serverAddress, Integer.parseInt(serverPort))); } else { CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(index); - server.setName(((EditTextPreference) findPreference("serverName")).getText()); - server.setAddress(((EditTextPreference) findPreference("serverAddress")).getText()); - server.setPort(Integer.parseInt(((EditTextPreference) findPreference("serverPort")).getText())); + server.setName(serverName); + server.setAddress(serverAddress); + server.setPort(Integer.parseInt(serverPort)); } getActivity().finish(); break; 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 b501e1c..16962c5 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/DnsTestFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/DnsTestFragment.java @@ -20,7 +20,8 @@ import de.measite.minidns.record.A; import de.measite.minidns.util.InetAddressUtil; import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.R; -import org.itxtech.daedalus.util.DnsServer; +import org.itxtech.daedalus.activity.MainActivity; +import org.itxtech.daedalus.util.DnsServerHelper; import java.net.InetAddress; import java.util.ArrayList; @@ -51,9 +52,9 @@ public class DnsTestFragment extends Fragment { final TextView textViewTestInfo = (TextView) view.findViewById(R.id.textView_test_info); final Spinner spinnerServerChoice = (Spinner) view.findViewById(R.id.spinner_server_choice); - ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, DnsServer.getDnsServerNames(Daedalus.getInstance())); + ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, DnsServerHelper.getDnsServerNames(Daedalus.getInstance())); spinnerServerChoice.setAdapter(spinnerArrayAdapter); - spinnerServerChoice.setSelection(Integer.valueOf(Daedalus.getPrefs().getString("primary_server", "0"))); + spinnerServerChoice.setSelection(Integer.parseInt(DnsServerHelper.getPrimaryDnsServer())); final AutoCompleteTextView textViewTestUrl = (AutoCompleteTextView) view.findViewById(R.id.autoCompleteTextView_test_url); ArrayAdapter autoCompleteArrayAdapter = new ArrayAdapter<>(Daedalus.getInstance(), android.R.layout.simple_list_item_1, Daedalus.DEFAULT_TEST_DOMAINS); @@ -69,7 +70,7 @@ public class DnsTestFragment extends Fragment { } StringBuilder testText = new StringBuilder(); ArrayList dnsServers = new ArrayList() {{ - add(DnsServer.getDnsServerAddressByStringDescription(Daedalus.getInstance(), spinnerServerChoice.getSelectedItem().toString())); + add(DnsServerHelper.getDnsServerAddressByStringDescription(Daedalus.getInstance(), spinnerServerChoice.getSelectedItem().toString())); String servers = Daedalus.getPrefs().getString("dns_test_servers", ""); if (!servers.equals("")) { for (String server : servers.split(",")) { @@ -200,10 +201,14 @@ public class DnsTestFragment extends Fragment { switch (msg.what) { case MSG_DISPLAY_STATUS: - textViewTestInfo.setText((String) msg.obj); + if (MainActivity.getInstance().getCurrentFragment() == MainActivity.FRAGMENT_DNS_TEST) { + textViewTestInfo.setText((String) msg.obj); + } break; case MSG_TEST_DONE: - startTestBtn.setVisibility(View.VISIBLE); + if (MainActivity.getInstance().getCurrentFragment() == MainActivity.FRAGMENT_DNS_TEST) { + startTestBtn.setVisibility(View.VISIBLE); + } stopThread(); break; } 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 1d0d3cc..6bff1f6 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/MainFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/MainFragment.java @@ -16,7 +16,7 @@ import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.R; import org.itxtech.daedalus.activity.MainActivity; import org.itxtech.daedalus.service.DaedalusVpnService; -import org.itxtech.daedalus.util.DnsServer; +import org.itxtech.daedalus.util.DnsServerHelper; /** * Daedalus Project @@ -86,8 +86,8 @@ public class MainFragment extends Fragment { public void onActivityResult(int request, int result, Intent data) { if (result == Activity.RESULT_OK) { - DaedalusVpnService.primaryServer = DnsServer.getDnsServerAddressById(Daedalus.getPrefs().getString("primary_server", "0")); - DaedalusVpnService.secondaryServer = DnsServer.getDnsServerAddressById(Daedalus.getPrefs().getString("secondary_server", "1")); + DaedalusVpnService.primaryServer = DnsServerHelper.getDnsServerAddressById(DnsServerHelper.getPrimaryDnsServer()); + DaedalusVpnService.secondaryServer = DnsServerHelper.getDnsServerAddressById(DnsServerHelper.getSecondaryDnsServer()); Daedalus.getInstance().startService(Daedalus.getInstance().getServiceIntent().setAction(DaedalusVpnService.ACTION_ACTIVATE)); 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 9a5ecc0..8ee8b2e 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/SettingsFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/SettingsFragment.java @@ -11,7 +11,7 @@ import android.view.View; import android.view.ViewGroup; import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.R; -import org.itxtech.daedalus.util.DnsServer; +import org.itxtech.daedalus.util.DnsServerHelper; /** * Daedalus Project @@ -28,16 +28,22 @@ public class SettingsFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + Daedalus.getPrefs().edit() + .putString("primary_server", DnsServerHelper.getPrimaryDnsServer()) + .putString("secondary_server", DnsServerHelper.getSecondaryDnsServer()) + .apply(); + addPreferencesFromResource(R.xml.perf_settings); ListPreference primaryServer = (ListPreference) findPreference("primary_server"); - primaryServer.setEntries(DnsServer.getDnsServerNames(Daedalus.getInstance())); - primaryServer.setEntryValues(DnsServer.getDnsServerIds()); - primaryServer.setSummary(DnsServer.getDnsServerDescription(primaryServer.getValue(), Daedalus.getInstance())); + primaryServer.setEntries(DnsServerHelper.getDnsServerNames(Daedalus.getInstance())); + primaryServer.setEntryValues(DnsServerHelper.getDnsServerIds()); + primaryServer.setSummary(DnsServerHelper.getDnsServerDescription(primaryServer.getValue(), Daedalus.getInstance())); primaryServer.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - preference.setSummary(DnsServer.getDnsServerDescription((String) newValue, Daedalus.getInstance())); + preference.setSummary(DnsServerHelper.getDnsServerDescription((String) newValue, Daedalus.getInstance())); Snackbar.make(view, R.string.notice_need_restart, Snackbar.LENGTH_LONG) .setAction("Action", null).show(); return true; @@ -45,13 +51,13 @@ public class SettingsFragment extends PreferenceFragment { }); ListPreference secondaryServer = (ListPreference) findPreference("secondary_server"); - secondaryServer.setEntries(DnsServer.getDnsServerNames(Daedalus.getInstance())); - secondaryServer.setEntryValues(DnsServer.getDnsServerIds()); - secondaryServer.setSummary(DnsServer.getDnsServerDescription(secondaryServer.getValue(), Daedalus.getInstance())); + secondaryServer.setEntries(DnsServerHelper.getDnsServerNames(Daedalus.getInstance())); + secondaryServer.setEntryValues(DnsServerHelper.getDnsServerIds()); + secondaryServer.setSummary(DnsServerHelper.getDnsServerDescription(secondaryServer.getValue(), Daedalus.getInstance())); secondaryServer.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - preference.setSummary(DnsServer.getDnsServerDescription((String) newValue, Daedalus.getInstance())); + preference.setSummary(DnsServerHelper.getDnsServerDescription((String) newValue, Daedalus.getInstance())); Snackbar.make(view, R.string.notice_need_restart, Snackbar.LENGTH_LONG) .setAction("Action", null).show(); return true; diff --git a/app/src/main/java/org/itxtech/daedalus/receiver/BootBroadcastReceiver.java b/app/src/main/java/org/itxtech/daedalus/receiver/BootBroadcastReceiver.java index 2c49ccf..d26ad64 100644 --- a/app/src/main/java/org/itxtech/daedalus/receiver/BootBroadcastReceiver.java +++ b/app/src/main/java/org/itxtech/daedalus/receiver/BootBroadcastReceiver.java @@ -7,7 +7,7 @@ import android.net.VpnService; import android.util.Log; import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.service.DaedalusVpnService; -import org.itxtech.daedalus.util.DnsServer; +import org.itxtech.daedalus.util.DnsServerHelper; /** * Daedalus Project @@ -28,8 +28,8 @@ public class BootBroadcastReceiver extends BroadcastReceiver { context.startActivity(vIntent); } - DaedalusVpnService.primaryServer = DnsServer.getDnsServerAddressById(Daedalus.getPrefs().getString("primary_server", "0")); - DaedalusVpnService.secondaryServer = DnsServer.getDnsServerAddressById(Daedalus.getPrefs().getString("secondary_server", "1")); + DaedalusVpnService.primaryServer = DnsServerHelper.getDnsServerAddressById(DnsServerHelper.getPrimaryDnsServer()); + DaedalusVpnService.secondaryServer = DnsServerHelper.getDnsServerAddressById(DnsServerHelper.getSecondaryDnsServer()); context.startService((new Intent(context, DaedalusVpnService.class)).setAction(DaedalusVpnService.ACTION_ACTIVATE)); diff --git a/app/src/main/java/org/itxtech/daedalus/util/DnsServer.java b/app/src/main/java/org/itxtech/daedalus/util/DnsServer.java index 8309cf8..d53b464 100644 --- a/app/src/main/java/org/itxtech/daedalus/util/DnsServer.java +++ b/app/src/main/java/org/itxtech/daedalus/util/DnsServer.java @@ -1,9 +1,6 @@ package org.itxtech.daedalus.util; import android.content.Context; -import org.itxtech.daedalus.Daedalus; - -import java.util.ArrayList; /** * Daedalus Project @@ -36,77 +33,7 @@ public class DnsServer { return address; } - public int getDescription() { - return description; - } - public String getStringDescription(Context context) { return context.getResources().getString(description); } - - public static String getDnsServerAddressById(String id) { - for (DnsServer server : Daedalus.DNS_SERVERS) { - if (server.getId().equals(id)) { - return server.getAddress(); - } - } - for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { - if (customDnsServer.getId().equals(id)) { - return customDnsServer.getAddress(); - } - } - return Daedalus.DNS_SERVERS.get(0).getAddress(); - } - - public static String getDnsServerAddressByStringDescription(Context context, String description) { - for (DnsServer server : Daedalus.DNS_SERVERS) { - if (server.getStringDescription(context).equals(description)) { - return server.getAddress(); - } - } - for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { - if (customDnsServer.getName().equals(description)) { - return customDnsServer.getAddress(); - } - } - return Daedalus.DNS_SERVERS.get(0).getAddress(); - } - - public static String[] getDnsServerIds() { - ArrayList servers = new ArrayList<>(Daedalus.DNS_SERVERS.size()); - for (DnsServer server : Daedalus.DNS_SERVERS) { - servers.add(server.getId()); - } - for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { - servers.add(customDnsServer.getId()); - } - String[] stringServers = new String[Daedalus.DNS_SERVERS.size()]; - return servers.toArray(stringServers); - } - - public static String[] getDnsServerNames(Context context) { - ArrayList servers = new ArrayList<>(Daedalus.DNS_SERVERS.size()); - for (DnsServer server : Daedalus.DNS_SERVERS) { - servers.add(server.getStringDescription(context)); - } - for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { - servers.add(customDnsServer.getName()); - } - String[] stringServers = new String[Daedalus.DNS_SERVERS.size()]; - return servers.toArray(stringServers); - } - - public static String getDnsServerDescription(String id, Context context) { - for (DnsServer server : Daedalus.DNS_SERVERS) { - if (server.getId().equals(id)) { - return server.getStringDescription(context); - } - } - for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { - if (customDnsServer.getId().equals(id)) { - return customDnsServer.getName(); - } - } - return Daedalus.DNS_SERVERS.get(0).getStringDescription(context); - } } diff --git a/app/src/main/java/org/itxtech/daedalus/util/DnsServerHelper.java b/app/src/main/java/org/itxtech/daedalus/util/DnsServerHelper.java new file mode 100644 index 0000000..dfaf695 --- /dev/null +++ b/app/src/main/java/org/itxtech/daedalus/util/DnsServerHelper.java @@ -0,0 +1,105 @@ +package org.itxtech.daedalus.util; + +import android.content.Context; +import org.itxtech.daedalus.Daedalus; + +import java.util.ArrayList; + +/** + * Daedalus Project + * + * @author iTXTech + * @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, version 3. + */ +public class DnsServerHelper { + public static String getPrimaryDnsServer() { + return String.valueOf(DnsServerHelper.checkServerId(Integer.parseInt(Daedalus.getPrefs().getString("primary_server", "0")))); + } + + public static String getSecondaryDnsServer() { + return String.valueOf(DnsServerHelper.checkServerId(Integer.parseInt(Daedalus.getPrefs().getString("secondary_server", "1")))); + } + + + private static int checkServerId(int id) { + if (id < (Daedalus.DNS_SERVERS.size() - 1)) { + return id; + } + for (CustomDnsServer server : Daedalus.configurations.getCustomDnsServers()) { + if (server.getId().equals(String.valueOf(id))) { + return id; + } + } + return 0; + } + + public static String getDnsServerAddressById(String id) { + for (DnsServer server : Daedalus.DNS_SERVERS) { + if (server.getId().equals(id)) { + return server.getAddress(); + } + } + for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { + if (customDnsServer.getId().equals(id)) { + return customDnsServer.getAddress(); + } + } + return Daedalus.DNS_SERVERS.get(0).getAddress(); + } + + public static String getDnsServerAddressByStringDescription(Context context, String description) { + for (DnsServer server : Daedalus.DNS_SERVERS) { + if (server.getStringDescription(context).equals(description)) { + return server.getAddress(); + } + } + for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { + if (customDnsServer.getName().equals(description)) { + return customDnsServer.getAddress(); + } + } + return Daedalus.DNS_SERVERS.get(0).getAddress(); + } + + public static String[] getDnsServerIds() { + ArrayList servers = new ArrayList<>(Daedalus.DNS_SERVERS.size()); + for (DnsServer server : Daedalus.DNS_SERVERS) { + servers.add(server.getId()); + } + for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { + servers.add(customDnsServer.getId()); + } + String[] stringServers = new String[Daedalus.DNS_SERVERS.size()]; + return servers.toArray(stringServers); + } + + public static String[] getDnsServerNames(Context context) { + ArrayList servers = new ArrayList<>(Daedalus.DNS_SERVERS.size()); + for (DnsServer server : Daedalus.DNS_SERVERS) { + servers.add(server.getStringDescription(context)); + } + for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { + servers.add(customDnsServer.getName()); + } + String[] stringServers = new String[Daedalus.DNS_SERVERS.size()]; + return servers.toArray(stringServers); + } + + public static String getDnsServerDescription(String id, Context context) { + for (DnsServer server : Daedalus.DNS_SERVERS) { + if (server.getId().equals(id)) { + return server.getStringDescription(context); + } + } + for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { + if (customDnsServer.getId().equals(id)) { + return customDnsServer.getName(); + } + } + return Daedalus.DNS_SERVERS.get(0).getStringDescription(context); + } +} diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 4b28f34..c1bcbf4 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -62,4 +62,5 @@ 服务器端口 删除 应用 + 请填写所有配置项。 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3ecdb8e..00f4da9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -40,7 +40,7 @@ AIXYZ DNS South China AIXYZ DNS East China GitHub - Advanced system Settings + Advanced System Settings On Local hosts resolution Re-activate Daedalus to make the settings take effect. @@ -62,4 +62,5 @@ Server Port Delete Apply + Please fill in all configuration items.