Fixed crash caused by missing custom DNS server

This commit is contained in:
PeratX 2017-05-01 15:34:57 +08:00
parent 2a4cfbfd79
commit d2ebad1f5a
10 changed files with 162 additions and 104 deletions

View File

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

View File

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

View File

@ -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<String> dnsServers = new ArrayList<String>() {{
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;
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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
* <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, 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<String> 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<String> 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);
}
}

View File

@ -62,4 +62,5 @@
<string name="settings_server_port">服务器端口</string>
<string name="delete">删除</string>
<string name="apply">应用</string>
<string name="notice_fill_in_all">请填写所有配置项。</string>
</resources>

View File

@ -40,7 +40,7 @@
<string name="server_aixyz_south_china">AIXYZ DNS South China</string>
<string name="server_aixyz_east_china">AIXYZ DNS East China</string>
<string name="nav_github">GitHub</string>
<string name="settings_advanced">Advanced system Settings</string>
<string name="settings_advanced">Advanced System Settings</string>
<string name="settings_advanced_on">On</string>
<string name="settings_local_hosts_resolution">Local hosts resolution</string>
<string name="notice_need_restart">Re-activate Daedalus to make the settings take effect.</string>
@ -62,4 +62,5 @@
<string name="settings_server_port">Server Port</string>
<string name="delete">Delete</string>
<string name="apply">Apply</string>
<string name="notice_fill_in_all">Please fill in all configuration items.</string>
</resources>