Implemented Custom DNS server management activity

This commit is contained in:
PeratX 2017-05-01 11:02:05 +08:00
parent abdee64724
commit 0a71e3949d
30 changed files with 358 additions and 26 deletions

View File

@ -31,7 +31,6 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</receiver>
<receiver
android:name=".receiver.StatusBarBroadcastReceiver"
android:exported="false">
@ -43,9 +42,9 @@
<activity
android:name=".activity.MainActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|uiMode|orientation|screenSize|smallestScreenSize"
android:label="@string/app_name"
android:launchMode="singleTask"
android:configChanges="keyboard|keyboardHidden|screenLayout|uiMode|orientation|screenSize|smallestScreenSize"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
@ -57,6 +56,12 @@
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts"/>
</activity>
<activity
android:name=".activity.DnsServerConfigActivity"
android:label=""
android:configChanges="keyboard|keyboardHidden|screenLayout|uiMode|orientation|screenSize|smallestScreenSize"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>

View File

@ -0,0 +1,50 @@
package org.itxtech.daedalus.activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.fragment.DnsServerConfigFragment;
/**
* 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 DnsServerConfigActivity extends AppCompatActivity {
public static final String LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID = "org.itxtech.daedalus.activity.DnsServerConfigActivity.LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID";
public static final int CUSTOM_DNS_SERVER_ID_NONE = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dns_server_config);
DnsServerConfigFragment fragment = new DnsServerConfigFragment();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_config);
toolbar.setNavigationIcon(R.drawable.ic_clear);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
toolbar.setOnMenuItemClickListener(fragment);
toolbar.inflateMenu(R.menu.custom_dns_server_menu);
FragmentManager manager = getFragmentManager();
fragment.setIntent(getIntent());
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.id_config, fragment);
fragmentTransaction.commit();
}
}

View File

@ -0,0 +1,129 @@
package org.itxtech.daedalus.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.DnsServerConfigActivity;
import org.itxtech.daedalus.util.CustomDnsServer;
/**
* 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 DnsServerConfigFragment extends PreferenceFragment implements Toolbar.OnMenuItemClickListener {
private Intent intent = null;
private int index;
public void setIntent(Intent intent) {
this.intent = intent;
}
@Override
public void onDestroy() {
super.onDestroy();
intent = null;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.perf_server);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
EditTextPreference serverName = (EditTextPreference) findPreference("serverName");
serverName.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String) newValue);
return true;
}
});
EditTextPreference serverAddress = (EditTextPreference) findPreference("serverAddress");
serverAddress.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String) newValue);
return true;
}
});
EditTextPreference serverPort = (EditTextPreference) findPreference("serverPort");
serverPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String) newValue);
return true;
}
});
index = intent.getIntExtra(DnsServerConfigActivity.LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID, DnsServerConfigActivity.CUSTOM_DNS_SERVER_ID_NONE);
if (index != DnsServerConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) {
CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(index);
serverName.setText(server.getName());
serverName.setSummary(server.getName());
serverAddress.setText(server.getAddress());
serverAddress.setSummary(server.getAddress());
serverPort.setText(String.valueOf(server.getPort()));
serverPort.setSummary(String.valueOf(server.getPort()));
} else {
serverName.setText("");
serverAddress.setText("");
serverPort.setText("");
}
return view;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_apply:
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())
));
} else {
Daedalus.configurations.getCustomDnsServers().set(index, new CustomDnsServer(
((EditTextPreference) findPreference("serverName")).getText(),
((EditTextPreference) findPreference("serverAddress")).getText(),
Integer.parseInt(((EditTextPreference) findPreference("serverPort")).getText())
));
}
getActivity().finish();
break;
case R.id.action_delete:
if (index != DnsServerConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) {
Daedalus.configurations.getCustomDnsServers().remove(index);
}
getActivity().finish();
break;
}
return true;
}
}

View File

@ -1,6 +1,7 @@
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.v7.widget.LinearLayoutManager;
@ -11,6 +12,7 @@ import android.view.ViewGroup;
import android.widget.TextView;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.DnsServerConfigActivity;
import org.itxtech.daedalus.util.CustomDnsServer;
/**
@ -24,16 +26,25 @@ import org.itxtech.daedalus.util.CustomDnsServer;
* the Free Software Foundation, version 3.
*/
public class DnsServersFragment extends Fragment {
private DnsServersFragment.DnsServerAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dns_servers, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_dns_servers);
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(manager);
DnsServerAdapter adapter = new DnsServerAdapter();
adapter = new DnsServerAdapter();
recyclerView.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab_add_server);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), DnsServerConfigActivity.class).putExtra(DnsServerConfigActivity.LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID,
DnsServerConfigActivity.CUSTOM_DNS_SERVER_ID_NONE));
}
});
return view;
}
@ -42,12 +53,21 @@ public class DnsServersFragment extends Fragment {
super.onDestroyView();
Daedalus.configurations.save();
adapter = null;
}
@Override
public void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
private class DnsServerAdapter extends RecyclerView.Adapter<ViewHolder> {
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(position);
holder.setIndex(position);
holder.textViewName.setText(server.getName());
holder.textViewAddress.setText(server.getAddress() + ":" + server.getPort());
}
@ -64,14 +84,26 @@ public class DnsServersFragment extends Fragment {
}
}
private static class ViewHolder extends RecyclerView.ViewHolder {
private static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private final TextView textViewName;
private final TextView textViewAddress;
private int index;
ViewHolder(View view) {
super(view);
textViewName = (TextView) view.findViewById(R.id.textView_custom_dns_name);
textViewAddress = (TextView) view.findViewById(R.id.textView_custom_dns_address);
view.setOnClickListener(this);
}
void setIndex(int index) {
this.index = index;
}
@Override
public void onClick(View v) {
Daedalus.getInstance().startActivity(new Intent(Daedalus.getInstance(), DnsServerConfigActivity.class)
.putExtra(DnsServerConfigActivity.LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID, index));
}
}
}

View File

@ -45,7 +45,7 @@ public class MainFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_main, container, false);
final Button but = (Button) view.findViewById(R.id.button_activate);
Button but = (Button) view.findViewById(R.id.button_activate);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

View File

@ -100,15 +100,15 @@ public class DaedalusVpnService extends VpnService implements Runnable {
builder.setWhen(0)
.setContentTitle(getResources().getString(R.string.notification_activated))
.setDefaults(NotificationCompat.DEFAULT_LIGHTS)
.setSmallIcon(R.drawable.ic_security_black_24dp)
.setSmallIcon(R.drawable.ic_security)
.setAutoCancel(false)
.setOngoing(true)
.setTicker(getResources().getString(R.string.notification_activated))
.setContentIntent(pIntent)
.addAction(R.drawable.ic_security_black_24dp, getResources().getString(R.string.button_text_deactivate),
.addAction(R.drawable.ic_security, getResources().getString(R.string.button_text_deactivate),
PendingIntent.getBroadcast(this, 0,
new Intent(StatusBarBroadcastReceiver.STATUS_BAR_BTN_DEACTIVATE_CLICK_ACTION), 0))
.addAction(R.drawable.ic_security_black_24dp, getResources().getString(R.string.action_settings),
.addAction(R.drawable.ic_security, getResources().getString(R.string.action_settings),
PendingIntent.getBroadcast(this, 0,
new Intent(StatusBarBroadcastReceiver.STATUS_BAR_BTN_SETTINGS_CLICK_ACTION), 0));

View File

@ -14,11 +14,21 @@ public class CustomDnsServer {
private String name;
private String address;
private int port;
private String id;
public CustomDnsServer(String name, String address, int port) {
this.name = name;
this.address = address;
this.port = port;
this.id = String.valueOf(DnsServer.totalId++);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {

View File

@ -16,7 +16,7 @@ import java.util.ArrayList;
* the Free Software Foundation, version 3.
*/
public class DnsServer {
private static int totalId = 0;
public static int totalId = 0;
private String id;
private String address;
@ -51,7 +51,7 @@ public class DnsServer {
}
}
for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) {
if (customDnsServer.getName().equals(id)) {
if (customDnsServer.getId().equals(id)) {
return customDnsServer.getAddress();
}
}
@ -78,7 +78,7 @@ public class DnsServer {
servers.add(server.getId());
}
for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) {
servers.add(customDnsServer.getName());
servers.add(customDnsServer.getId());
}
String[] stringServers = new String[Daedalus.DNS_SERVERS.size()];
return servers.toArray(stringServers);
@ -103,7 +103,7 @@ public class DnsServer {
}
}
for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) {
if (customDnsServer.getName().equals(id)) {
if (customDnsServer.getId().equals(id)) {
return customDnsServer.getName();
}
}

View File

@ -61,6 +61,9 @@ public class HostsResolver implements Runnable {
}
public static boolean canResolve(String hostname) {
if (hosts == null) {
return false;
}
if (hosts.containsKey(hostname)) {
return true;
}
@ -80,6 +83,9 @@ public class HostsResolver implements Runnable {
}
public static String resolve(String hostname) {
if (hosts == null) {
return "";
}
if (hosts.containsKey(hostname)) {
return hosts.get(hostname);
}

View File

@ -44,7 +44,7 @@ public class ClearAutoCompleteTextView extends AutoCompleteTextView implements V
}
private void init(final Context context) {
final Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_clear_black_24dp);
final Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_clear);
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable); //Wrap the drawable so that it can be tinted pre Lollipop
DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
mClearTextIcon = wrappedDrawable;

View File

@ -4,6 +4,6 @@
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:fillColor="#FFFFFFFF"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/>
</vector>

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_config"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/id_config"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="org.itxtech.daedalus.activity.DnsServerConfigActivity">
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

View File

@ -11,7 +11,7 @@
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_note_add_black_24dp"
android:src="@drawable/ic_note_add"
android:tint="#FFFFFF"/>
<android.support.v7.widget.RecyclerView

View File

@ -8,37 +8,37 @@
<item android:id="@+id/nav_home"
android:title="@string/action_home"
android:checked="true"
android:icon="@drawable/ic_home_black_24dp"/>
android:icon="@drawable/ic_home"/>
<item android:id="@+id/nav_dns_test"
android:title="@string/action_dns_test"
android:checked="false"
android:icon="@drawable/ic_verified_user_black_24dp"/>
android:icon="@drawable/ic_verified_user"/>
</group>
<group android:checkableBehavior="single"
android:id="@+id/nav_group_profiles">
<item android:id="@+id/nav_dns_server"
android:title="@string/action_dns_servers"
android:icon="@drawable/ic_device_hub_black_24dp"/>
android:icon="@drawable/ic_device_hub"/>
<item android:id="@+id/nav_hosts"
android:title="@string/action_hosts"
android:checked="false"
android:icon="@drawable/ic_description_black_24dp"/>
android:icon="@drawable/ic_description"/>
</group>
<group android:checkableBehavior="single"
android:id="@+id/nav_group_more">
<item android:id="@+id/nav_settings"
android:title="@string/action_settings"
android:icon="@drawable/ic_settings_black_24dp"/>
android:icon="@drawable/ic_settings"/>
<item android:id="@+id/nav_about"
android:title="@string/action_about"
android:icon="@drawable/ic_info_black_24dp"/>
android:icon="@drawable/ic_info"/>
</group>
<group android:id="@+id/nav_group_help_and_support">
<item android:id="@+id/nav_github"
android:title="@string/nav_github"
android:icon="@drawable/ic_github_black_24dp"/>
android:icon="@drawable/ic_github"/>
</group>
</menu>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_delete"
android:title="@string/delete"
android:icon="@drawable/ic_delete"
android:alphabeticShortcut="d"
android:numericShortcut="2"
app:showAsAction="always"/>
<item android:id="@+id/action_apply"
android:title="@string/apply"
android:icon="@drawable/ic_done"
android:alphabeticShortcut="a"
android:numericShortcut="3"
app:showAsAction="always"/>
</menu>

View File

@ -57,4 +57,9 @@
<string name="hosts_size">大小:</string>
<string name="settings_hosts_pan_domain_resolution">Hosts 域名泛解析</string>
<string name="action_dns_servers">服务器</string>
<string name="settings_server_name">服务器名称</string>
<string name="settings_server_address">服务器地址</string>
<string name="settings_server_port">服务器端口</string>
<string name="delete">删除</string>
<string name="apply">应用</string>
</resources>

View File

@ -6,9 +6,9 @@
<string name="button_text_deactivate">Deactivate</string>
<string name="action_about">About</string>
<string name="notification_activated">iTXTech Daedalus is activated.</string>
<string name="settings_system">System settings</string>
<string name="settings_system">System Settings</string>
<string name="settings_boot">Auto activate on boot</string>
<string name="settings_server">Server settings</string>
<string name="settings_server">Server Settings</string>
<string name="primary_server">Primary DNS server</string>
<string name="secondary_server">Secondary DNS server</string>
<string name="notice_start_test">Testing specified DNS server …</string>
@ -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>
@ -57,4 +57,9 @@
<string name="hosts_size">Size:</string>
<string name="settings_hosts_pan_domain_resolution">Hosts pan domain name resolution</string>
<string name="action_dns_servers">Servers</string>
<string name="settings_server_name">Server Name</string>
<string name="settings_server_address">Server Address</string>
<string name="settings_server_port">Server Port</string>
<string name="delete">Delete</string>
<string name="apply">Apply</string>
</resources>

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="serverName"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_server_name"/>
<PreferenceCategory
android:key="serverSettings"
android:title="@string/settings_server">
<EditTextPreference
android:key="serverAddress"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_server_address"/>
<EditTextPreference
android:key="serverPort"
android:selectAllOnFocus="false"
android:singleLine="true"
android:numeric="integer"
android:title="@string/settings_server_port"/>
</PreferenceCategory>
</PreferenceScreen>

View File

@ -2,7 +2,7 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="settings_server"
android:key="settingsServer"
android:title="@string/settings_server">
<ListPreference