part 1
This commit is contained in:
parent
cd330248b7
commit
d2c1f2e652
@ -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">
|
||||
|
@ -25,7 +25,7 @@ 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 class ConfigActivity 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;
|
||||
|
@ -15,7 +15,7 @@ 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;
|
||||
|
||||
@ -85,8 +85,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_CUSTOM_DNS_SERVER_ID, ConfigActivity.CUSTOM_DNS_SERVER_ID_NONE);
|
||||
if (index != ConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) {
|
||||
CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(index);
|
||||
serverName.setText(server.getName());
|
||||
serverName.setSummary(server.getName());
|
||||
@ -120,7 +120,7 @@ public class DnsServerConfigFragment extends PreferenceFragment implements Toolb
|
||||
break;
|
||||
}
|
||||
|
||||
if (index == DnsServerConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) {
|
||||
if (index == ConfigActivity.CUSTOM_DNS_SERVER_ID_NONE) {
|
||||
Daedalus.configurations.getCustomDnsServers().add(new CustomDnsServer(serverName, serverAddress, Integer.parseInt(serverPort)));
|
||||
} else {
|
||||
CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(index);
|
||||
@ -131,7 +131,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.CUSTOM_DNS_SERVER_ID_NONE) {
|
||||
new AlertDialog.Builder(getActivity())
|
||||
.setTitle(R.string.notice_delete_confirm_prompt)
|
||||
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
||||
|
@ -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,8 @@ 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_CUSTOM_DNS_SERVER_ID,
|
||||
ConfigActivity.CUSTOM_DNS_SERVER_ID_NONE));
|
||||
}
|
||||
});
|
||||
return view;
|
||||
@ -154,8 +154,8 @@ 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_CUSTOM_DNS_SERVER_ID, index));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,242 +2,144 @@ package org.itxtech.daedalus.fragment;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
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 java.io.*;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import org.itxtech.daedalus.util.Rule;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @author PeratX
|
||||
*/
|
||||
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) {
|
||||
if (Daedalus.configurations.getRules().get(((ViewHolder) viewHolder).getIndex()).isUsing()) {
|
||||
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() {
|
||||
@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();
|
||||
}
|
||||
}
|
||||
});
|
||||
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.setIndex(position);
|
||||
holder.textViewName.setText(rule.getName());
|
||||
holder.textViewAddress.setText(rule.getFileName());
|
||||
}
|
||||
|
||||
Snackbar.make(view, R.string.notice_downloaded, Snackbar.LENGTH_LONG)
|
||||
.setAction("Action", null).show();
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return Daedalus.configurations.getCustomDnsServers().size();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_server, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
}
|
||||
|
||||
Snackbar.make(view, R.string.notice_downloaded, Snackbar.LENGTH_LONG)
|
||||
.setAction("Action", null).show();
|
||||
private static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||
private final TextView textViewName;
|
||||
private final TextView textViewAddress;
|
||||
private int index;
|
||||
|
||||
mFragment.updateUserInterface();
|
||||
break;
|
||||
}
|
||||
ViewHolder(View view) {
|
||||
super(view);
|
||||
textViewName = (TextView) view.findViewById(R.id.textView_rule_name);
|
||||
textViewAddress = (TextView) view.findViewById(R.id.textView_rule_detail);
|
||||
view.setOnClickListener(this);
|
||||
}
|
||||
|
||||
void setIndex(int index) {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
int getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
@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));
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,223 @@
|
||||
package org.itxtech.daedalus.fragment;
|
||||
|
||||
import android.app.Fragment;
|
||||
|
||||
/**
|
||||
* 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 RulesFragment0 extends Fragment {
|
||||
/*
|
||||
private Thread mThread = null;
|
||||
private View view = null;
|
||||
private RulesHandler mHandler = 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() {
|
||||
@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();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Button buttonDownloadDnsmasq = (Button) view.findViewById(R.id.button_download_dnsmasq);
|
||||
buttonDownloadDnsmasq.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();
|
||||
}
|
||||
}
|
||||
});
|
||||
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 onResume() {
|
||||
super.onResume();
|
||||
|
||||
updateUserInterface();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
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 RulesFragment0 mFragment = null;
|
||||
|
||||
RulesHandler setView(View view) {
|
||||
this.view = view;
|
||||
return this;
|
||||
}
|
||||
|
||||
RulesHandler setHostsFragment(RulesFragment0 fragment) {
|
||||
mFragment = fragment;
|
||||
return this;
|
||||
}
|
||||
|
||||
void shutdown() {
|
||||
view = null;
|
||||
mFragment = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
@ -28,6 +28,8 @@ public class Configurations {
|
||||
|
||||
private ArrayList<CustomDnsServer> customDnsServers;
|
||||
|
||||
private ArrayList<Rule> rules;
|
||||
|
||||
private int totalId;
|
||||
|
||||
private long activateCounter;
|
||||
@ -54,6 +56,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;
|
||||
|
71
app/src/main/java/org/itxtech/daedalus/util/Rule.java
Normal file
71
app/src/main/java/org/itxtech/daedalus/util/Rule.java
Normal file
@ -0,0 +1,71 @@
|
||||
package org.itxtech.daedalus.util;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
public Rule(String name, String fileName, int type, String downloadUrl) {
|
||||
this.name = name;
|
||||
this.fileName = fileName;
|
||||
this.type = type;
|
||||
this.downloadUrl = downloadUrl;
|
||||
this.using = false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package org.itxtech.daedalus.view;
|
||||
package org.itxtech.daedalus.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
@ -1,4 +1,4 @@
|
||||
package org.itxtech.daedalus.view;
|
||||
package org.itxtech.daedalus.widget;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
@ -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>
|
41
app/src/main/res/layout/card_rule.xml
Normal file
41
app/src/main/res/layout/card_rule.xml
Normal file
@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.v7.widget.CardView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:clickable="true"
|
||||
android:foreground="?attr/selectableItemBackground"
|
||||
app:cardCornerRadius="10dp"
|
||||
app:cardPreventCornerOverlap="true"
|
||||
app:cardUseCompatPadding="true">
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="@dimen/margin_small"
|
||||
android:paddingBottom="@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:paddingTop="@dimen/margin_small"
|
||||
android:paddingBottom="@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"/>
|
||||
|
||||
</RelativeLayout>
|
||||
</android.support.v7.widget.CardView>
|
@ -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"
|
||||
|
@ -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 |
@ -69,11 +69,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"/>
|
||||
|
Loading…
Reference in New Issue
Block a user