Merge branch 'profile-redesign'

This commit is contained in:
PeratX 2017-05-20 00:39:42 +08:00
commit 9f54c54205
30 changed files with 1027 additions and 432 deletions

View File

@ -57,7 +57,7 @@
android:resource="@xml/shortcuts"/>
</activity>
<activity
android:name=".activity.DnsServerConfigActivity"
android:name=".activity.ConfigActivity"
android:label=""
android:configChanges="keyboard|keyboardHidden|screenLayout|uiMode|orientation|screenSize|smallestScreenSize"
android:theme="@style/AppTheme.NoActionBar">

View File

@ -23,7 +23,7 @@ import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.Configurations;
import org.itxtech.daedalus.util.DnsServer;
import org.itxtech.daedalus.util.RulesProvider;
import org.itxtech.daedalus.util.Rule;
import org.itxtech.daedalus.util.RulesResolver;
import java.io.File;
@ -57,15 +57,20 @@ public class Daedalus extends Application {
add(new DnsServer("123.206.21.48", R.string.server_aixyz_south_china));
}};
public static final List<RulesProvider> HOSTS_PROVIDERS = new ArrayList<RulesProvider>() {{
add(new RulesProvider("racaljk/hosts", "https://coding.net/u/scaffrey/p/hosts/git/raw/master/hosts"));
add(new RulesProvider("fengixng/google-hosts", "https://raw.githubusercontent.com/fengixng/google-hosts/master/hosts"));
add(new RulesProvider("sy618/hosts", "https://raw.githubusercontent.com/sy618/hosts/master/ADFQ"));
}};
public static final List<Rule> RULES = new ArrayList<Rule>() {{
//Build-in Hosts rule providers
add(new Rule("racaljk/hosts", "racaljk.hosts", Rule.TYPE_HOSTS,
"https://coding.net/u/scaffrey/p/hosts/git/raw/master/hosts", false));
add(new Rule("fengixng/google-hosts", "fengixng.hosts", Rule.TYPE_HOSTS,
"https://raw.githubusercontent.com/fengixng/google-hosts/master/hosts", false));
add(new Rule("sy618/hosts", "sy618.hosts", Rule.TYPE_HOSTS,
"https://raw.githubusercontent.com/sy618/hosts/master/ADFQ", false));
//Build-in DNSMasq rule providers
add(new Rule("sy618/hosts/dnsad", "dnsad.dnsmasq", Rule.TYPE_DNAMASQ,
"https://raw.githubusercontent.com/sy618/hosts/master/dnsmasq/dnsad", false));
add(new Rule("sy618/hosts/dnsfq", "dnsfq.dnsmasq", Rule.TYPE_DNAMASQ,
"https://raw.githubusercontent.com/sy618/hosts/master/dnsmasq/dnsfq", false));
public static final List<RulesProvider> DNSMASQ_PROVIDERS = new ArrayList<RulesProvider>() {{
add(new RulesProvider("sy618/hosts/dnsad", "https://raw.githubusercontent.com/sy618/hosts/master/dnsmasq/dnsad", "dnsad"));
add(new RulesProvider("sy618/hosts/dnsfq", "https://raw.githubusercontent.com/sy618/hosts/master/dnsmasq/dnsfq", "dnsfq"));
}};
public static final String[] DEFAULT_TEST_DOMAINS = new String[]{
@ -78,8 +83,7 @@ public class Daedalus extends Application {
public static Configurations configurations;
public static String hostsPath = null;
public static String dnsmasqPath = null;
public static String rulesPath = null;
private static String configPath = null;
private static Daedalus instance = null;
@ -94,11 +98,10 @@ public class Daedalus extends Application {
mHostsResolver.start();
if (getExternalFilesDir(null) != null) {
hostsPath = getExternalFilesDir(null).getPath() + "/hosts";
dnsmasqPath = getExternalFilesDir(null).getPath() + "/dnsmasq/";
rulesPath = getExternalFilesDir(null).getPath() + "/rules/";
configPath = getExternalFilesDir(null).getPath() + "/config.json";
File file = new File(dnsmasqPath);
File file = new File(rulesPath);
Log.d(TAG, "mkdir result: " + file.mkdirs());
}
@ -142,10 +145,23 @@ public class Daedalus extends Application {
return;
}
}
if (Daedalus.getPrefs().getBoolean("settings_use_dnsmasq", false)) {
RulesResolver.startLoadDnsmasq(dnsmasqPath);
} else {
RulesResolver.startLoadHosts(hostsPath);
ArrayList<String> pendingLoad = new ArrayList<>();
int type = Rule.TYPE_HOSTS;
for (Rule rule : configurations.getRules()) {
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;
}
}
}

View File

@ -12,7 +12,9 @@ import android.support.v7.widget.Toolbar;
import android.view.View;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.fragment.ConfigFragment;
import org.itxtech.daedalus.fragment.DnsServerConfigFragment;
import org.itxtech.daedalus.fragment.RuleConfigFragment;
/**
* Daedalus Project
@ -25,16 +27,31 @@ import org.itxtech.daedalus.fragment.DnsServerConfigFragment;
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
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;
public class ConfigActivity extends AppCompatActivity {
public static final String LAUNCH_ACTION_FRAGMENT = "org.itxtech.daedalus.activity.ConfigActivity.LAUNCH_ACTION_FRAGMENT";
public static final int LAUNCH_FRAGMENT_DNS_SERVER = 0;
public static final int LAUNCH_FRAGMENT_RULE = 1;
public static final String LAUNCH_ACTION_ID = "org.itxtech.daedalus.activity.ConfigActivity.LAUNCH_ACTION_ID";
public static final int ID_NONE = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dns_server_config);
DnsServerConfigFragment fragment = new DnsServerConfigFragment();
ConfigFragment fragment;
switch (getIntent().getIntExtra(LAUNCH_ACTION_FRAGMENT, LAUNCH_FRAGMENT_DNS_SERVER)) {
case LAUNCH_FRAGMENT_DNS_SERVER:
fragment = new DnsServerConfigFragment();
break;
case LAUNCH_FRAGMENT_RULE:
fragment = new RuleConfigFragment();
break;
default://should never reach this
fragment = new DnsServerConfigFragment();
break;
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_config);
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_clear);
@ -57,6 +74,23 @@ public class DnsServerConfigActivity extends AppCompatActivity {
fragmentTransaction.commit();
}
@Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_config);
switch (getIntent().getIntExtra(LAUNCH_ACTION_FRAGMENT, LAUNCH_FRAGMENT_DNS_SERVER)) {
case LAUNCH_FRAGMENT_DNS_SERVER:
toolbar.setTitle(R.string.config_dns_server);
break;
case LAUNCH_FRAGMENT_RULE:
toolbar.setTitle(R.string.config_rule);
break;
default://should never reach this
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();

View File

@ -3,6 +3,7 @@ package org.itxtech.daedalus.activity;
import android.Manifest;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
@ -20,6 +21,7 @@ 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;
import org.itxtech.daedalus.BuildConfig;
import org.itxtech.daedalus.Daedalus;
@ -328,6 +330,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_drawer_layout);
drawer.closeDrawer(GravityCompat.START);
InputMethodManager imm = (InputMethodManager) Daedalus.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(findViewById(R.id.id_content).getWindowToken(), 0);
return true;
}
}

View File

@ -0,0 +1,31 @@
package org.itxtech.daedalus.fragment;
import android.content.Intent;
import android.preference.PreferenceFragment;
import android.support.v7.widget.Toolbar;
/**
* Daedalus Project
*
* @author iTX Technologies
* @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, either version 3 of the License, or
* (at your option) any later version.
*/
abstract public class ConfigFragment extends PreferenceFragment implements Toolbar.OnMenuItemClickListener {
protected Intent intent = null;
public void setIntent(Intent intent) {
this.intent = intent;
}
@Override
public void onDestroy() {
super.onDestroy();
intent = null;
}
}

View File

@ -2,20 +2,17 @@ package org.itxtech.daedalus.fragment;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
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;
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.activity.ConfigActivity;
import org.itxtech.daedalus.util.CustomDnsServer;
import org.itxtech.daedalus.util.DnsServer;
@ -30,20 +27,14 @@ import org.itxtech.daedalus.util.DnsServer;
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
public class DnsServerConfigFragment extends PreferenceFragment implements Toolbar.OnMenuItemClickListener {
private Intent intent = null;
public class DnsServerConfigFragment extends ConfigFragment {
private View view;
private int index;
public void setIntent(Intent intent) {
this.intent = intent;
}
@Override
public void onDestroy() {
super.onDestroy();
intent = null;
view = null;
}
@ -85,8 +76,8 @@ public class DnsServerConfigFragment extends PreferenceFragment implements Toolb
});
index = intent.getIntExtra(DnsServerConfigActivity.LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID, DnsServerConfigActivity.CUSTOM_DNS_SERVER_ID_NONE);
if (index != DnsServerConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) {
index = intent.getIntExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE);
if (index != ConfigActivity.ID_NONE) {
CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(index);
serverName.setText(server.getName());
serverName.setSummary(server.getName());
@ -120,7 +111,7 @@ public class DnsServerConfigFragment extends PreferenceFragment implements Toolb
break;
}
if (index == DnsServerConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) {
if (index == ConfigActivity.ID_NONE) {
Daedalus.configurations.getCustomDnsServers().add(new CustomDnsServer(serverName, serverAddress, Integer.parseInt(serverPort)));
} else {
CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(index);
@ -131,7 +122,7 @@ public class DnsServerConfigFragment extends PreferenceFragment implements Toolb
getActivity().finish();
break;
case R.id.action_delete:
if (index != DnsServerConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) {
if (index != ConfigActivity.ID_NONE) {
new AlertDialog.Builder(getActivity())
.setTitle(R.string.notice_delete_confirm_prompt)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

View File

@ -14,7 +14,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.activity.ConfigActivity;
import org.itxtech.daedalus.util.CustomDnsServer;
import org.itxtech.daedalus.util.DnsServerHelper;
@ -73,8 +73,10 @@ public class DnsServersFragment extends Fragment {
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));
startActivity(new Intent(getActivity(), ConfigActivity.class)
.putExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE)
.putExtra(ConfigActivity.LAUNCH_ACTION_FRAGMENT, ConfigActivity.LAUNCH_FRAGMENT_DNS_SERVER)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
});
return view;
@ -154,8 +156,10 @@ public class DnsServersFragment extends Fragment {
@Override
public void onClick(View v) {
if (!DnsServerHelper.isInUsing(Daedalus.configurations.getCustomDnsServers().get(index))) {
Daedalus.getInstance().startActivity(new Intent(Daedalus.getInstance(), DnsServerConfigActivity.class)
.putExtra(DnsServerConfigActivity.LAUNCH_ACTION_CUSTOM_DNS_SERVER_ID, index));
Daedalus.getInstance().startActivity(new Intent(Daedalus.getInstance(), ConfigActivity.class)
.putExtra(ConfigActivity.LAUNCH_ACTION_ID, index)
.putExtra(ConfigActivity.LAUNCH_ACTION_FRAGMENT, ConfigActivity.LAUNCH_FRAGMENT_DNS_SERVER)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
}

View File

@ -0,0 +1,322 @@
package org.itxtech.daedalus.fragment;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.support.design.widget.Snackbar;
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.ConfigActivity;
import org.itxtech.daedalus.util.Rule;
import org.itxtech.daedalus.widget.ClickPreference;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
/**
* Daedalus Project
*
* @author iTX Technologies
* @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, either version 3 of the License, or
* (at your option) any later version.
*/
public class RuleConfigFragment extends ConfigFragment {
private Intent intent = null;
private Thread mThread = null;
private RuleConfigHandler mHandler = null;
private View view;
private int id;
public void setIntent(Intent intent) {
this.intent = intent;
}
@Override
public void onDestroy() {
super.onDestroy();
stopThread();
intent = null;
mHandler.shutdown();
mHandler = null;
view = null;
}
private void stopThread() {
if (mThread != null) {
mThread.interrupt();
mThread = null;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.perf_rule);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = super.onCreateView(inflater, container, savedInstanceState);
mHandler = new RuleConfigHandler().setView(view);
final EditTextPreference ruleName = (EditTextPreference) findPreference("ruleName");
ruleName.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String) newValue);
return true;
}
});
final ListPreference ruleType = (ListPreference) findPreference("ruleType");
final String[] entries = {"hosts", "DNSMasq"};
String[] values = {"0", "1"};
ruleType.setEntries(entries);
ruleType.setEntryValues(values);
ruleType.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary(entries[Integer.parseInt((String) newValue)]);
return true;
}
});
final EditTextPreference ruleDownloadUrl = (EditTextPreference) findPreference("ruleDownloadUrl");
ruleDownloadUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String) newValue);
return true;
}
});
final EditTextPreference ruleFilename = (EditTextPreference) findPreference("ruleFilename");
ruleFilename.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String) newValue);
return true;
}
});
ClickPreference ruleSync = (ClickPreference) findPreference("ruleSync");
ruleSync.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
save();
if (mThread == null) {
Snackbar.make(view, R.string.notice_start_download, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
mThread = new Thread(new Runnable() {
@Override
public void run() {
try {
URLConnection connection = new URL(ruleDownloadUrl.getText()).openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String result;
while ((result = reader.readLine()) != null) {
builder.append("\n").append(result);
}
reader.close();
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
new RuleData(ruleFilename.getText(), builder.toString())).sendToTarget();
stopThread();
} catch (Exception e) {
e.printStackTrace();
} finally {
stopThread();
}
}
});
mThread.start();
} else {
Snackbar.make(view, R.string.notice_now_downloading, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
return false;
}
});
ListPreference ruleImportBuildIn = (ListPreference) findPreference("ruleImportBuildIn");
ruleImportBuildIn.setEntries(Rule.getBuildInRuleNames());
ruleImportBuildIn.setEntryValues(Rule.getBuildInRuleEntries());
ruleImportBuildIn.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Rule rule = Daedalus.RULES.get(Integer.parseInt((String) newValue));
ruleName.setText(rule.getName());
ruleName.setSummary(rule.getName());
ruleType.setValue(String.valueOf(rule.getType()));
ruleType.setSummary(Rule.getTypeById(rule.getType()));
ruleFilename.setText(rule.getFileName());
ruleFilename.setSummary(rule.getFileName());
ruleDownloadUrl.setText(rule.getDownloadUrl());
ruleDownloadUrl.setSummary(rule.getDownloadUrl());
return true;
}
});
ruleImportBuildIn.setValue("0");
id = intent.getIntExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE);
Rule rule;
if (id != ConfigActivity.ID_NONE && (rule = Rule.getRuleById(String.valueOf(id))) != null) {
Rule.getRuleById(String.valueOf(id));
ruleName.setText(rule.getName());
ruleName.setSummary(rule.getName());
int type = rule.getType();
ruleType.setValue(String.valueOf(type));
ruleType.setSummary(entries[type]);
ruleFilename.setText(rule.getFileName());
ruleFilename.setSummary(rule.getFileName());
ruleDownloadUrl.setText(rule.getDownloadUrl());
ruleDownloadUrl.setSummary(rule.getDownloadUrl());
} else {
ruleName.setText("");
ruleName.setSummary("");
ruleType.setValue("0");
ruleType.setSummary(entries[Rule.TYPE_HOSTS]);
ruleFilename.setText("");
ruleFilename.setSummary("");
ruleDownloadUrl.setText("");
ruleDownloadUrl.setSummary("");
}
return view;
}
private void save() {
String ruleName = ((EditTextPreference) findPreference("ruleName")).getText();
String ruleType = ((ListPreference) findPreference("ruleType")).getValue();
String ruleFilename = ((EditTextPreference) findPreference("ruleFilename")).getText();
String ruleDownloadUrl = ((EditTextPreference) findPreference("ruleDownloadUrl")).getText();
if (ruleName.equals("") | ruleType.equals("") | ruleFilename.equals("") | ruleDownloadUrl.equals("")) {
Snackbar.make(view, R.string.notice_fill_in_all, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
return;
}
if (id == ConfigActivity.ID_NONE) {
Rule rule = new Rule(ruleName, ruleFilename, Integer.parseInt(ruleType), ruleDownloadUrl);
Daedalus.configurations.getRules().add(rule);
id = Integer.parseInt(rule.getId());
} else {
Rule rule = Rule.getRuleById(String.valueOf(id));
if (rule != null) {
rule.setName(ruleName);
rule.setType(Integer.parseInt(ruleType));
rule.setFileName(ruleFilename);
rule.setDownloadUrl(ruleDownloadUrl);
}
}
}
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_apply:
save();
getActivity().finish();
break;
case R.id.action_delete:
if (this.id != ConfigActivity.ID_NONE) {
new AlertDialog.Builder(getActivity())
.setTitle(R.string.notice_delete_confirm_prompt)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Daedalus.configurations.getRules().remove(RuleConfigFragment.this.id);
getActivity().finish();
}
})
.setNegativeButton(R.string.no, null)
.create()
.show();
} else {
getActivity().finish();
}
break;
}
return true;
}
private class RuleData {
private String data;
private String filename;
RuleData(String filename, String data) {
this.data = data;
this.filename = filename;
}
String getData() {
return data;
}
String getFilename() {
return filename;
}
}
private static class RuleConfigHandler extends Handler {
static final int MSG_RULE_DOWNLOADED = 0;
private View view = null;
RuleConfigHandler setView(View view) {
this.view = view;
return this;
}
void shutdown() {
view = null;
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG_RULE_DOWNLOADED:
try {
RuleData ruleData = (RuleData) msg.obj;
File file = new File(Daedalus.rulesPath + ruleData.getFilename());
FileOutputStream stream = new FileOutputStream(file);
stream.write(ruleData.getData().getBytes());
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
Snackbar.make(view, R.string.notice_downloaded, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
break;
}
}
}
}

View File

@ -1,26 +1,24 @@
package org.itxtech.daedalus.fragment;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.util.RulesProvider;
import org.itxtech.daedalus.activity.ConfigActivity;
import org.itxtech.daedalus.util.Rule;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.io.File;
import java.text.DecimalFormat;
import java.util.Date;
/**
* Daedalus Project
@ -35,209 +33,180 @@ import java.util.Date;
*/
public class RulesFragment extends Fragment {
private Thread mThread = null;
private View view = null;
private RulesHandler mHandler = null;
private RuleAdapter adapter;
private Rule rule = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_rules, container, false);
mHandler = new RulesHandler().setView(view).setHostsFragment(this);
final Spinner spinnerHosts = (Spinner) view.findViewById(R.id.spinner_hosts);
ArrayAdapter hostsArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, RulesProvider.getHostsProviderNames());
spinnerHosts.setAdapter(hostsArrayAdapter);
spinnerHosts.setSelection(0);
final Spinner spinnerDnsmasq = (Spinner) view.findViewById(R.id.spinner_dnsmasq);
ArrayAdapter dnsmasqArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, RulesProvider.getDnsmasqProviderNames());
spinnerDnsmasq.setAdapter(dnsmasqArrayAdapter);
spinnerDnsmasq.setSelection(0);
Button buttonDownloadHosts = (Button) view.findViewById(R.id.button_download_hosts);
buttonDownloadHosts.setOnClickListener(new View.OnClickListener() {
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_rules);
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(manager);
adapter = new RuleAdapter();
recyclerView.setAdapter(adapter);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
@Override
public void onClick(View v) {
if (mThread == null) {
Snackbar.make(view, R.string.notice_start_download, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
mThread = new Thread(new Runnable() {
@Override
public void run() {
try {
URLConnection connection = new URL(RulesProvider.getDownloadUrlByName(spinnerHosts.getSelectedItem().toString())).openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String result;
while ((result = reader.readLine()) != null) {
builder.append("\n").append(result);
}
reader.close();
mHandler.obtainMessage(RulesHandler.MSG_HOSTS_DOWNLOADED, builder.toString()).sendToTarget();
stopThread();
} catch (Exception e) {
e.printStackTrace();
}
}
});
mThread.start();
} else {
Snackbar.make(view, R.string.notice_now_downloading, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof RulesFragment.ViewHolder) {
Rule rule = Rule.getRuleById(((ViewHolder) viewHolder).getId());
if (rule != null && rule.isServiceAndUsing()) {
return 0;
}
}
return makeMovementFlags(0, ItemTouchHelper.START | ItemTouchHelper.END);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
rule = Daedalus.configurations.getRules().get(position);
Daedalus.configurations.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);
}
});
itemTouchHelper.attachToRecyclerView(recyclerView);
Button buttonDownloadDnsmasq = (Button) view.findViewById(R.id.button_download_dnsmasq);
buttonDownloadDnsmasq.setOnClickListener(new View.OnClickListener() {
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab_add_rule);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mThread == null) {
Snackbar.make(view, R.string.notice_start_download, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
mThread = new Thread(new Runnable() {
@Override
public void run() {
try {
RulesProvider provider = RulesProvider.getProviderByName(spinnerDnsmasq.getSelectedItem().toString());
URLConnection connection = new URL(provider.getDownloadURL()).openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String result;
while ((result = reader.readLine()) != null) {
builder.append("\n").append(result);
}
reader.close();
provider.setData(builder.toString());
mHandler.obtainMessage(RulesHandler.MSG_DNSMASQ_DOWNLOADED, provider).sendToTarget();
stopThread();
} catch (Exception e) {
e.printStackTrace();
}
}
});
mThread.start();
} else {
Snackbar.make(view, R.string.notice_now_downloading, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
startActivity(new Intent(getActivity(), ConfigActivity.class)
.putExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE)
.putExtra(ConfigActivity.LAUNCH_ACTION_FRAGMENT, ConfigActivity.LAUNCH_FRAGMENT_RULE)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
});
return view;
}
private void updateUserInterface() {
File hosts = new File(Daedalus.hostsPath);
TextView info = (TextView) view.findViewById(R.id.textView_hosts);
StringBuilder builder = new StringBuilder();
builder.append(getString(R.string.hosts_path)).append(" ").append(Daedalus.hostsPath).append("\n");
if (!hosts.exists()) {
builder.append(getString(R.string.hosts_not_found));
} else {
builder.append(getString(R.string.hosts_last_modified)).append(" ").append(new Date(hosts.lastModified()).toString()).append("\n")
.append(getString(R.string.hosts_size)).append(" ").append(new DecimalFormat("0.00").format(((float) hosts.length() / 1024))).append(" KB");
}
builder.append("\n");
File dnsmasq = new File(Daedalus.dnsmasqPath);
builder.append(getString(R.string.dnsmasq_path)).append(" ").append(Daedalus.dnsmasqPath);
if (dnsmasq.exists()) {
for (File conf : dnsmasq.listFiles()) {
builder.append("\n").append(conf.getName()).append(" ")
.append(new DecimalFormat("0.00").format(((float) conf.length() / 1024))).append(" KB");
}
}
info.setText(builder.toString());
@Override
public void onDestroyView() {
super.onDestroyView();
Daedalus.configurations.save();
adapter = null;
rule = null;
}
@Override
public void onResume() {
super.onResume();
updateUserInterface();
adapter.notifyDataSetChanged();
}
@Override
public void onDestroy() {
super.onDestroy();
private class SnackbarClickListener implements View.OnClickListener {
private final int position;
stopThread();
mHandler.shutdown();
mHandler = null;
view = null;
}
private void stopThread() {
if (mThread != null) {
mThread.interrupt();
mThread = null;
}
}
private static class RulesHandler extends Handler {
static final int MSG_HOSTS_DOWNLOADED = 0;
static final int MSG_DNSMASQ_DOWNLOADED = 1;
private View view = null;
private RulesFragment mFragment = null;
RulesHandler setView(View view) {
this.view = view;
return this;
}
RulesHandler setHostsFragment(RulesFragment fragment) {
mFragment = fragment;
return this;
}
void shutdown() {
view = null;
mFragment = null;
private SnackbarClickListener(int position) {
this.position = position;
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
public void onClick(View v) {
Daedalus.configurations.getRules().add(position, rule);
adapter.notifyItemInserted(position);
}
}
switch (msg.what) {
case MSG_HOSTS_DOWNLOADED:
try {
String result = (String) msg.obj;
File file = new File(Daedalus.hostsPath);
FileOutputStream stream = new FileOutputStream(file);
stream.write(result.getBytes());
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
private class RuleAdapter extends RecyclerView.Adapter<ViewHolder> {
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Rule rule = Daedalus.configurations.getRules().get(position);
holder.setId(rule.getId());
holder.textViewName.setText(rule.getName());
holder.textViewAddress.setText(rule.getFileName());
Snackbar.make(view, R.string.notice_downloaded, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
mFragment.updateUserInterface();
break;
case MSG_DNSMASQ_DOWNLOADED:
try {
RulesProvider provider = (RulesProvider) msg.obj;
File file = new File(Daedalus.dnsmasqPath + provider.getFileName());
FileOutputStream stream = new FileOutputStream(file);
stream.write(provider.getData().getBytes());
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
Snackbar.make(view, R.string.notice_downloaded, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
mFragment.updateUserInterface();
break;
File file = new File(Daedalus.rulesPath + rule.getFileName());
StringBuilder builder = new StringBuilder();
if (file.exists()) {
builder.append(new DecimalFormat("0.00").format(((float) file.length() / 1024)));
} else {
builder.append("0");
}
holder.textViewSize.setText(builder.append(" KB").toString());
}
@Override
public int getItemCount() {
return Daedalus.configurations.getRules().size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_rule, parent, false);
return new ViewHolder(view);
}
void checkType(Rule rule) {
for (Rule check : Daedalus.configurations.getRules()) {
if (check.getType() != rule.getType()) {
check.setUsing(false);
}
}
notifyDataSetChanged();
}
}
private class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private final TextView textViewName;
private final TextView textViewAddress;
private final TextView textViewSize;
private final View view;
private String id;
ViewHolder(View view) {
super(view);
this.view = view;
textViewName = (TextView) view.findViewById(R.id.textView_rule_name);
textViewAddress = (TextView) view.findViewById(R.id.textView_rule_detail);
textViewSize = (TextView) view.findViewById(R.id.textView_rule_size);
view.setOnClickListener(this);
view.setOnLongClickListener(this);
view.findViewById(R.id.cardView_indicator).setBackgroundResource(R.drawable.background_selectable);
}
void setId(String id) {
this.id = id;
Rule rule = Rule.getRuleById(id);
if (rule != null) {
view.setSelected(rule.isUsing());
}
}
String getId() {
return id;
}
@Override
public void onClick(View v) {
Rule rule = Rule.getRuleById(id);
if (rule != null) {
rule.setUsing(!v.isSelected());
v.setSelected(!v.isSelected());
//adapter.checkType(rule);
}
}
@Override
public boolean onLongClick(View v) {
Rule rule = Rule.getRuleById(id);
if (rule != null && !rule.isServiceAndUsing()) {
Daedalus.getInstance().startActivity(new Intent(Daedalus.getInstance(), ConfigActivity.class)
.putExtra(ConfigActivity.LAUNCH_ACTION_ID, Integer.parseInt(id))
.putExtra(ConfigActivity.LAUNCH_ACTION_FRAGMENT, ConfigActivity.LAUNCH_FRAGMENT_RULE)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
return true;
}
}
}

View File

@ -22,21 +22,31 @@ import java.util.ArrayList;
* (at your option) any later version.
*/
public class Configurations {
private static final int CUSTOM_DNS_ID_START = 32;
private static final int CUSTOM_ID_START = 32;
private static File file;
private ArrayList<CustomDnsServer> customDnsServers;
private int totalId;
private ArrayList<Rule> rules;
private int totalDnsId;
private int totalRuleId;
private long activateCounter;
int getNextId() {
if (totalId < CUSTOM_DNS_ID_START) {
totalId = CUSTOM_DNS_ID_START;
int getNextDnsId() {
if (totalDnsId < CUSTOM_ID_START) {
totalDnsId = CUSTOM_ID_START;
}
return totalId++;
return totalDnsId++;
}
int getNextRuleId() {
if (totalRuleId < 0) {
totalRuleId = 0;
}
return totalRuleId++;
}
public long getActivateCounter() {
@ -54,6 +64,13 @@ public class Configurations {
return customDnsServers;
}
public ArrayList<Rule> getRules() {
if (rules == null) {
rules = new ArrayList<>();
}
return rules;
}
public static Configurations load(File file) {
Configurations.file = file;
Configurations config = null;

View File

@ -23,7 +23,7 @@ public class CustomDnsServer {
this.name = name;
this.address = address;
this.port = port;
this.id = String.valueOf(Daedalus.configurations.getNextId());
this.id = String.valueOf(Daedalus.configurations.getNextDnsId());
}
public String getId() {

View File

@ -0,0 +1,135 @@
package org.itxtech.daedalus.util;
import org.itxtech.daedalus.Daedalus;
import java.util.ArrayList;
/**
* Daedalus Project
*
* @author iTX Technologies
* @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, either version 3 of the License, or
* (at your option) any later version.
*/
public class Rule {
public static final int TYPE_HOSTS = 0;
public static final int TYPE_DNAMASQ = 1;
private String name;
private String fileName;
private int type;
private String downloadUrl;
private boolean using;
private String id;
public Rule(String name, String fileName, int type, String downloadUrl, boolean withId) {
this.name = name;
this.fileName = fileName;
this.type = type;
this.downloadUrl = downloadUrl;
this.using = false;
if (withId) {
this.id = String.valueOf(Daedalus.configurations.getNextRuleId());
}
}
public Rule(String name, String fileName, int type, String downloadUrl) {
this(name, fileName, type, downloadUrl, true);
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setUsing(boolean using) {
this.using = using;
}
public boolean isUsing() {
return using;
}
public String getName() {
return name;
}
public String getFileName() {
return fileName;
}
public int getType() {
return type;
}
public String getDownloadUrl() {
return downloadUrl;
}
public void setName(String name) {
this.name = name;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setType(int type) {
this.type = type;
}
public void setDownloadUrl(String downloadUrl) {
this.downloadUrl = downloadUrl;
}
public boolean isServiceAndUsing() {
return Daedalus.getInstance().isServiceActivated() && isUsing();
}
//STATIC METHODS
public static String[] getBuildInRuleNames() {
ArrayList<String> names = new ArrayList<>(Daedalus.RULES.size());
for (Rule rule : Daedalus.RULES) {
names.add(rule.getName() + " - " + getTypeById(rule.getType()));
}
String[] strings = new String[names.size()];
return names.toArray(strings);
}
public static String[] getBuildInRuleEntries() {
ArrayList<String> entries = new ArrayList<>(Daedalus.RULES.size());
for (int i = 0; i < Daedalus.RULES.size(); i++) {
entries.add(String.valueOf(i));
}
String[] strings = new String[entries.size()];
return entries.toArray(strings);
}
public static Rule getRuleById(String id) {
for (Rule rule : Daedalus.configurations.getRules()) {
if (rule.getId().equals(id)) {
return rule;
}
}
return null;
}
public static String getTypeById(int id) {
switch (id) {
case TYPE_HOSTS:
return "Hosts";
case TYPE_DNAMASQ:
return "DNSMasq";
default:
return "Unknown";
}
}
}

View File

@ -1,91 +0,0 @@
package org.itxtech.daedalus.util;
import org.itxtech.daedalus.Daedalus;
import java.util.ArrayList;
/**
* Daedalus Project
*
* @author iTX Technologies
* @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, either version 3 of the License, or
* (at your option) any later version.
*/
public class RulesProvider {
private String name;
private String downloadURL;
private String fileName;
private String data;
public RulesProvider(String name, String downloadURL) {
this(name, downloadURL, "Unknown");
}
public RulesProvider(String name, String downloadURL, String fileName) {
this.name = name;
this.downloadURL = downloadURL;
this.fileName = fileName;
}
public String getName() {
return name;
}
public String getDownloadURL() {
return downloadURL;
}
public String getFileName() {
return fileName;
}
public void setData(String data) {
this.data = data;
}
public String getData() {
String temp = data;
data = null;
return temp;
}
public static String[] getHostsProviderNames() {
ArrayList<String> servers = new ArrayList<>(Daedalus.HOSTS_PROVIDERS.size());
for (RulesProvider provider : Daedalus.HOSTS_PROVIDERS) {
servers.add(provider.getName());
}
String[] stringServers = new String[Daedalus.HOSTS_PROVIDERS.size()];
return servers.toArray(stringServers);
}
public static String[] getDnsmasqProviderNames() {
ArrayList<String> servers = new ArrayList<>(Daedalus.DNSMASQ_PROVIDERS.size());
for (RulesProvider provider : Daedalus.DNSMASQ_PROVIDERS) {
servers.add(provider.getName());
}
String[] stringServers = new String[Daedalus.DNSMASQ_PROVIDERS.size()];
return servers.toArray(stringServers);
}
public static String getDownloadUrlByName(String name) {
for (RulesProvider provider : Daedalus.HOSTS_PROVIDERS) {
if (provider.getName().equals(name)) {
return provider.getDownloadURL();
}
}
return null;
}
public static RulesProvider getProviderByName(String name) {
for (RulesProvider provider : Daedalus.DNSMASQ_PROVIDERS) {
if (provider.getName().equals(name)) {
return provider;
}
}
return null;
}
}

View File

@ -32,15 +32,15 @@ public class RulesResolver implements Runnable {
private static int status = STATUS_NOT_LOADED;
private static int mode = MODE_HOSTS;
private static String hostsFile;
private static String dnsmasqPath;
private static String[] hostsFiles;
private static String[] dnsmasqFiles;
private static HashMap<String, String> rules;
private static boolean shutdown = false;
public RulesResolver() {
status = STATUS_NOT_LOADED;
hostsFile = "";
dnsmasqPath = "";
hostsFiles = new String[0];
dnsmasqFiles = new String[0];
shutdown = false;
}
@ -52,16 +52,16 @@ public class RulesResolver implements Runnable {
return status == STATUS_LOADED;
}
public static void startLoadHosts(String loadFile) {
Log.d(TAG, "Loading hosts file " + loadFile);
hostsFile = loadFile;
public static void startLoadHosts(String[] loadFile) {
Log.d(TAG, "Loading hosts file " + loadFile.length);
hostsFiles = loadFile;
mode = MODE_HOSTS;
status = STATUS_PENDING_LOAD;
}
public static void startLoadDnsmasq(String loadPath) {
Log.d(TAG, "Loading DNSMasq file " + loadPath);
dnsmasqPath = loadPath;
public static void startLoadDnsmasq(String[] loadPath) {
Log.d(TAG, "Loading DNSMasq file " + loadPath.length);
dnsmasqFiles = loadPath;
mode = MODE_DNSMASQ;
status = STATUS_PENDING_LOAD;
}
@ -101,37 +101,32 @@ public class RulesResolver implements Runnable {
status = STATUS_LOADING;
rules = new HashMap<>();
if (mode == MODE_HOSTS) {
File file = new File(hostsFile);
if (file.exists() && file.canRead()) {
FileInputStream stream = new FileInputStream(file);
BufferedReader dataIO = new BufferedReader(new InputStreamReader(stream));
String strLine;
String[] data;
while ((strLine = dataIO.readLine()) != null) {
//Log.d(TAG, strLine);
if (!strLine.equals("") && !strLine.startsWith("#")) {
data = strLine.split("\\s+");
rules.put(data[1], data[0]);
Log.d(TAG, "Putting " + data[0] + " " + data[1]);
for (String hostsFile : hostsFiles) {
File file = new File(hostsFile);
if (file.canRead()) {
FileInputStream stream = new FileInputStream(file);
BufferedReader dataIO = new BufferedReader(new InputStreamReader(stream));
String strLine;
String[] data;
while ((strLine = dataIO.readLine()) != null) {
//Log.d(TAG, strLine);
if (!strLine.equals("") && !strLine.startsWith("#")) {
data = strLine.split("\\s+");
rules.put(data[1], data[0]);
Log.d(TAG, "Putting " + data[0] + " " + data[1]);
}
}
}
dataIO.close();
stream.close();
} else {
status = STATUS_NOT_LOADED;
return;
dataIO.close();
stream.close();
}
}
} else if (mode == MODE_DNSMASQ) {
File file = new File(dnsmasqPath);
if (!file.mkdirs() && !file.exists()) {
status = STATUS_NOT_LOADED;
return;
}
for (File conf : file.listFiles()) {
for (String dnsmasqFile : dnsmasqFiles) {
File file = new File(dnsmasqFile);
if (file.canRead()) {
Log.d(TAG, "load: Loading DNSMasq configuration " + conf.toString());
FileInputStream stream = new FileInputStream(conf);
Log.d(TAG, "load: Loading DNSMasq configuration " + file.toString());
FileInputStream stream = new FileInputStream(file);
BufferedReader dataIO = new BufferedReader(new InputStreamReader(stream));
String strLine;
String[] data;
@ -143,7 +138,7 @@ public class RulesResolver implements Runnable {
data[1] = data[1].substring(1, data[1].length());
}
rules.put(data[1], data[2]);
Log.d(TAG, "Putting " + data[1] + " " + data[2]);
//Log.d(TAG, "Putting " + data[1] + " " + data[2]);
}
}
}

View File

@ -0,0 +1,54 @@
package org.itxtech.daedalus.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import org.itxtech.daedalus.R;
/**
* Daedalus Project
*
* @author iTX Technologies & MrFuFuFu
* @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, either version 3 of the License, or
* (at your option) any later version.
*/
public class BoundedCardView extends CardView {
public BoundedCardView(Context context) {
super(context);
}
public BoundedCardView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(context, attrs);
}
public BoundedCardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private int boundedWidth;
private int boundedHeight;
private void initAttrs(Context context, AttributeSet attrs) {
TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.BoundedView);
boundedWidth = arr.getDimensionPixelSize(R.styleable.BoundedView_bounded_width, 0);
boundedHeight = arr.getDimensionPixelSize(R.styleable.BoundedView_bounded_height, 0);
arr.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (boundedWidth > 0 && boundedWidth < MeasureSpec.getSize(widthMeasureSpec)) {
MeasureSpec.makeMeasureSpec(boundedWidth, MeasureSpec.getMode(widthMeasureSpec));
}
if (boundedHeight > 0 && boundedHeight < MeasureSpec.getSize(heightMeasureSpec)) {
MeasureSpec.makeMeasureSpec(boundedHeight, MeasureSpec.getMode(heightMeasureSpec));
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

View File

@ -1,4 +1,4 @@
package org.itxtech.daedalus.view;
package org.itxtech.daedalus.widget;
import android.content.Context;
import android.graphics.drawable.Drawable;

View File

@ -1,4 +1,4 @@
package org.itxtech.daedalus.view;
package org.itxtech.daedalus.widget;
import android.annotation.TargetApi;
import android.content.Context;

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#90A4AE"/>
<corners android:radius="4dp"/>
</shape>
</item>
<item>
<selector android:enterFadeDuration="@android:integer/config_mediumAnimTime"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="#388E3C"/>
<corners android:radius="4dp"/>
</shape>
</item>
</selector>
</item>
<item android:left="8dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:bottomRightRadius="4dp" android:topRightRadius="4dp"/>
</shape>
</item>
</layer-list>

View File

@ -27,7 +27,7 @@
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">
tools:context="org.itxtech.daedalus.activity.ConfigActivity">
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/margin_small"
android:layout_marginStart="@dimen/margin_small"
android:layout_marginEnd="@dimen/margin_small"
app:cardCornerRadius="10dp"
app:cardPreventCornerOverlap="true"
android:id="@+id/cardView_indicator">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:orientation="vertical"
android:background="?android:attr/selectableItemBackground">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_small"
android:layout_marginLeft="@dimen/margin_small">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/margin_small"
android:id="@+id/textView_rule_name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView_rule_name"
android:padding="@dimen/margin_small"
android:id="@+id/textView_rule_detail"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView android:id="@+id/textView_rule_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="@dimen/margin_small"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</FrameLayout>

View File

@ -18,8 +18,8 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/margin_small"
android:paddingBottom="@dimen/margin_small"
android:id="@+id/textView_custom_dns_name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentTop="true"
@ -29,8 +29,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView_custom_dns_name"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/margin_small"
android:paddingBottom="@dimen/margin_small"
android:id="@+id/textView_custom_dns_address"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_alignParentBottom="true"

View File

@ -37,7 +37,7 @@
android:layout_marginTop="10dp"
android:id="@+id/textView_test_info"
android:textSize="18sp"/>
<org.itxtech.daedalus.view.ClearAutoCompleteTextView
<org.itxtech.daedalus.widget.ClearAutoCompleteTextView
android:completionThreshold="1"
android:singleLine="true"
android:hint="@string/test_test_domain"

View File

@ -1,59 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView_about"
android:id="@+id/fragment_rules"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/fragment_rules"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_rules"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add_rule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_hosts"
android:layout_alignParentTop="true"/>
<Button
android:text="@string/button_text_download_hosts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner_hosts"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:id="@+id/button_download_hosts"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_dnsmasq"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_below="@+id/button_download_hosts"/>
<Button
android:text="@string/button_text_download_dnsmasq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner_dnsmasq"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:id="@+id/button_download_dnsmasq"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView_hosts"
android:layout_below="@+id/button_download_dnsmasq"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</ScrollView>
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_note_add"
android:tint="#FFFFFF"/>
</android.support.design.widget.CoordinatorLayout>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -48,7 +48,7 @@
<string name="notice_need_storage_perm">Daedalus 需要访问外部储存以实现本地规则解析。</string>
<string name="action_rules">规则</string>
<string name="button_text_download_hosts">下载 hosts</string>
<string name="notice_start_download">正在下载 rules,请稍等 ……</string>
<string name="notice_start_download">正在下载规则,请稍等 ……</string>
<string name="notice_downloaded">已下载规则。</string>
<string name="notice_now_downloading">Daedalus 当前正在下载规则,请稍等。</string>
<string name="hosts_not_found">找不到本地 hosts 文件。</string>
@ -65,10 +65,19 @@
<string name="settings_dns_over_tcp">DNS over TCP</string>
<string name="action_removed">已移除。</string>
<string name="action_undo">撤销</string>
<string name="settings_use_dnsmasq">使用 DNSMasq 替代 Hosts</string>
<string name="button_text_download_dnsmasq">下载 DNSMasq 配置文件</string>
<string name="dnsmasq_path">本地 DNSMasq 配置路径:</string>
<string name="notice_delete_confirm_prompt">您确定要删除此 DNS 服务器</string>
<string name="notice_delete_confirm_prompt">您确定要删除此配置项</string>
<string name="yes"></string>
<string name="no"></string>
<string name="config_dns_server">配置 DNS 服务器</string>
<string name="config_rule">配置规则</string>
<string name="settings_rule_name">规则名称</string>
<string name="settings_rule_type">规则类型</string>
<string name="settings_rule">规则设置</string>
<string name="settings_rule_download_url">规则下载地址</string>
<string name="settings_rule_sync">同步规则</string>
<string name="settings_rule_filename">规则文件名</string>
<string name="settings_rule_import">导入</string>
<string name="settings_rule_import_build_in">从内置规则导入</string>
</resources>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="BoundedView">
<attr name="bounded_width" format="dimension"/>
<attr name="bounded_height" format="dimension"/>
</declare-styleable>
</resources>

View File

@ -3,8 +3,11 @@
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fragment_vertical_margin">16dp</dimen>
<dimen name="margin_small">8dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="nav_header_vertical_spacing">16dp</dimen>
<dimen name="nav_header_height">180dp</dimen>
<dimen name="config_item_max_width">480dp</dimen>
</resources>

View File

@ -65,10 +65,19 @@
<string name="settings_dns_over_tcp">DNS over TCP</string>
<string name="action_removed">Removed.</string>
<string name="action_undo">Undo</string>
<string name="settings_use_dnsmasq">Use DNSMasq instead of Hosts</string>
<string name="button_text_download_dnsmasq">Download DNSMasq configuration</string>
<string name="dnsmasq_path">Local DNSMasq configurations path:</string>
<string name="notice_delete_confirm_prompt">Are you sure you want to remove this DNS server?</string>
<string name="notice_delete_confirm_prompt">Are you sure you want to remove this configuration?</string>
<string name="yes">Yes</string>
<string name="no">No</string>
<string name="config_dns_server">DNS server config</string>
<string name="config_rule">Rule config</string>
<string name="settings_rule_name">Rule Name</string>
<string name="settings_rule_type">Rule Type</string>
<string name="settings_rule">Rule Settings</string>
<string name="settings_rule_download_url">Rule Download Url</string>
<string name="settings_rule_sync">Sync Rule</string>
<string name="settings_rule_filename">Rule Filename</string>
<string name="settings_rule_import">Import</string>
<string name="settings_rule_import_build_in">Import from build in rules</string>
</resources>

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="ruleName"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_rule_name"/>
<PreferenceCategory
android:key="rulSettings"
android:title="@string/settings_rule">
<ListPreference
android:key="ruleType"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_rule_type"/>
<EditTextPreference
android:key="ruleFilename"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_rule_filename"/>
<EditTextPreference
android:key="ruleDownloadUrl"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_rule_download_url"/>
<org.itxtech.daedalus.widget.ClickPreference
android:key="ruleSync"
android:title="@string/settings_rule_sync"/>
</PreferenceCategory>
<PreferenceCategory
android:key="Import"
android:title="@string/settings_rule_import">
<ListPreference
android:key="ruleImportBuildIn"
android:title="@string/settings_rule_import_build_in"/>
</PreferenceCategory>
</PreferenceScreen>

View File

@ -54,11 +54,6 @@
android:title="@string/settings_local_rules_resolution"
android:defaultValue="false"
android:enabled="false"/>
<SwitchPreference
android:key="settings_use_dnsmasq"
android:title="@string/settings_use_dnsmasq"
android:defaultValue="false"
android:enabled="false"/>
<SwitchPreference
android:key="settings_dns_over_tcp"
android:title="@string/settings_dns_over_tcp"
@ -69,11 +64,11 @@
<PreferenceCategory
android:key="settings_help"
android:title="@string/settings_help_and_support">
<org.itxtech.daedalus.view.ClickPreference
<org.itxtech.daedalus.widget.ClickPreference
android:key="settings_check_update"
android:title="@string/settings_check_update"
android:summary="@string/settings_update_summary"/>
<org.itxtech.daedalus.view.ClickPreference
<org.itxtech.daedalus.widget.ClickPreference
android:key="settings_issue_tracker"
android:title="@string/settings_bug_report"
android:summary="@string/settings_issue_summary"/>