Implemented import external rule
This commit is contained in:
parent
e9c7415e10
commit
97625045b2
@ -100,7 +100,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
FragmentManager fm = getFragmentManager();
|
||||
fm.beginTransaction().replace(R.id.id_content, fragment).commit();
|
||||
currentFragment = fragment;
|
||||
} catch (Throwable e){
|
||||
} catch (Exception e) {
|
||||
Logger.logException(e);
|
||||
}
|
||||
}
|
||||
|
@ -146,8 +146,7 @@ public class GlobalConfigFragment extends PreferenceFragment {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
||||
Snackbar.make(view, R.string.notice_legacy_api, Snackbar.LENGTH_LONG)
|
||||
.setAction("Action", null).show();
|
||||
Snackbar.make(view, R.string.notice_legacy_api, Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ public class LogFragment extends ToolbarFragment implements Toolbar.OnMenuItemCl
|
||||
fileWriter.close();
|
||||
Snackbar.make(getView(), getString(R.string.notice_export_complete) + file, Snackbar.LENGTH_SHORT)
|
||||
.setAction("Action", null).show();
|
||||
} catch (Throwable e){
|
||||
} catch (Exception e) {
|
||||
Logger.logException(e);
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,12 @@
|
||||
package org.itxtech.daedalus.fragment;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
@ -37,6 +41,7 @@ import java.net.URLConnection;
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
public class RuleConfigFragment extends ConfigFragment {
|
||||
private static final int READ_REQUEST_CODE = 1;
|
||||
private Intent intent = null;
|
||||
private Thread mThread = null;
|
||||
private RuleConfigHandler mHandler = null;
|
||||
@ -122,36 +127,60 @@ public class RuleConfigFragment extends ConfigFragment {
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
save();
|
||||
if (mThread == null) {
|
||||
Snackbar.make(getView(), 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);
|
||||
Snackbar.make(getView(), R.string.notice_start_download, Snackbar.LENGTH_SHORT).show();
|
||||
if (ruleDownloadUrl.getText().startsWith("content:/")) {
|
||||
mThread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
InputStream inputStream = getActivity().getContentResolver().openInputStream(Uri.parse(ruleDownloadUrl.getText()));
|
||||
int readLen;
|
||||
byte[] data = new byte[1024];
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
while ((readLen = inputStream.read(data)) != -1) {
|
||||
buffer.write(data, 0, readLen);
|
||||
}
|
||||
inputStream.close();
|
||||
buffer.flush();
|
||||
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
|
||||
new RuleData(ruleFilename.getText(), buffer.toByteArray())).sendToTarget();
|
||||
stopThread();
|
||||
} catch (Exception e) {
|
||||
Logger.logException(e);
|
||||
} finally {
|
||||
stopThread();
|
||||
}
|
||||
reader.close();
|
||||
|
||||
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
|
||||
new RuleData(ruleFilename.getText(), builder.toString())).sendToTarget();
|
||||
stopThread();
|
||||
} catch (Exception e) {
|
||||
Logger.logException(e);
|
||||
} finally {
|
||||
stopThread();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
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().getBytes())).sendToTarget();
|
||||
stopThread();
|
||||
} catch (Exception e) {
|
||||
Logger.logException(e);
|
||||
} finally {
|
||||
stopThread();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
mThread.start();
|
||||
} else {
|
||||
Snackbar.make(getView(), R.string.notice_now_downloading, Snackbar.LENGTH_LONG)
|
||||
.setAction("Action", null).show();
|
||||
Snackbar.make(getView(), R.string.notice_now_downloading, Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -176,6 +205,19 @@ public class RuleConfigFragment extends ConfigFragment {
|
||||
}
|
||||
});
|
||||
|
||||
ClickPreference ruleImportExternal = (ClickPreference) findPreference("ruleImportExternal");
|
||||
ruleImportExternal.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
performFileSearch();
|
||||
} else {
|
||||
Snackbar.make(getView(), R.string.notice_legacy_api, Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
ruleImportBuildIn.setValue("0");
|
||||
if (intent != null) {
|
||||
@ -209,6 +251,50 @@ public class RuleConfigFragment extends ConfigFragment {
|
||||
return view;
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.KITKAT)
|
||||
private void performFileSearch() {
|
||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
intent.setType("*/*");
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
|
||||
|
||||
startActivityForResult(intent, READ_REQUEST_CODE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@TargetApi(Build.VERSION_CODES.KITKAT)
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
|
||||
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
|
||||
if (resultData != null) {
|
||||
Uri uri = resultData.getData();
|
||||
try {
|
||||
getActivity().getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
|
||||
String file = System.currentTimeMillis() + ".dr";//Daedalus Rule
|
||||
InputStream inputStream = getActivity().getContentResolver().openInputStream(uri);
|
||||
OutputStream outputStream = new FileOutputStream(Daedalus.rulePath + file);
|
||||
byte[] b = new byte[1024];
|
||||
while ((inputStream.read(b)) != -1) {
|
||||
outputStream.write(b);
|
||||
}
|
||||
inputStream.close();
|
||||
outputStream.close();
|
||||
|
||||
((EditTextPreference) findPreference("ruleFilename")).setText(file);
|
||||
findPreference("ruleFilename").setSummary(file);
|
||||
((EditTextPreference) findPreference("ruleDownloadUrl")).setText(uri.toString());
|
||||
findPreference("ruleDownloadUrl").setSummary(uri.toString());
|
||||
|
||||
Snackbar.make(getView(), R.string.notice_importing_rule, Snackbar.LENGTH_LONG).show();
|
||||
} catch (Exception e) {
|
||||
Logger.logException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean save() {
|
||||
String ruleName = ((EditTextPreference) findPreference("ruleName")).getText();
|
||||
String ruleType = ((ListPreference) findPreference("ruleType")).getValue();
|
||||
@ -274,15 +360,15 @@ public class RuleConfigFragment extends ConfigFragment {
|
||||
}
|
||||
|
||||
private class RuleData {
|
||||
private String data;
|
||||
private byte[] data;
|
||||
private String filename;
|
||||
|
||||
RuleData(String filename, String data) {
|
||||
RuleData(String filename, byte[] data) {
|
||||
this.data = data;
|
||||
this.filename = filename;
|
||||
}
|
||||
|
||||
String getData() {
|
||||
byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@ -315,14 +401,13 @@ public class RuleConfigFragment extends ConfigFragment {
|
||||
RuleData ruleData = (RuleData) msg.obj;
|
||||
File file = new File(Daedalus.rulePath + ruleData.getFilename());
|
||||
FileOutputStream stream = new FileOutputStream(file);
|
||||
stream.write(ruleData.getData().getBytes());
|
||||
stream.write(ruleData.getData());
|
||||
stream.close();
|
||||
} catch (Exception e) {
|
||||
Logger.logException(e);
|
||||
}
|
||||
|
||||
Snackbar.make(view, R.string.notice_downloaded, Snackbar.LENGTH_SHORT)
|
||||
.setAction("Action", null).show();
|
||||
Snackbar.make(view, R.string.notice_downloaded, Snackbar.LENGTH_SHORT).show();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -122,14 +122,14 @@ public class Configurations {
|
||||
if (file.exists()) {
|
||||
try {
|
||||
config = Daedalus.parseJson(Configurations.class, new JsonReader(new FileReader(file)));
|
||||
Logger.info("Loading configuration successfully from " + file);
|
||||
Logger.info("Load configuration successfully from " + file);
|
||||
} catch (Exception e) {
|
||||
Logger.logException(e);
|
||||
}
|
||||
}
|
||||
|
||||
if (config == null) {
|
||||
Logger.info("Loading configuration failed. Generating default configurations.");
|
||||
Logger.info("Load configuration failed. Generating default configurations.");
|
||||
config = new Configurations();
|
||||
}
|
||||
|
||||
|
@ -58,7 +58,7 @@ public class Logger {
|
||||
error(getExceptionMessage(e));
|
||||
}
|
||||
|
||||
private static String getExceptionMessage(Throwable e) {
|
||||
public static String getExceptionMessage(Throwable e) {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
PrintWriter printWriter = new PrintWriter(stringWriter);
|
||||
e.printStackTrace(printWriter);
|
||||
|
@ -2,6 +2,7 @@ package org.itxtech.daedalus.util;
|
||||
|
||||
import org.itxtech.daedalus.Daedalus;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@ -120,6 +121,8 @@ public class Rule {
|
||||
} else if (getType() == Rule.TYPE_DNAMASQ) {
|
||||
Daedalus.configurations.getDnsmasqRules().remove(this);
|
||||
}
|
||||
File file = new File(getFileName());
|
||||
Logger.info("Delete rule " + getName() + " result: " + String.valueOf(file.delete()));
|
||||
}
|
||||
|
||||
|
||||
|
@ -71,7 +71,8 @@
|
||||
<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>
|
||||
<string name="settings_rule_import_build_in">内置</string>
|
||||
<string name="settings_rule_import_external">外部</string>
|
||||
<string name="notice_after_stop">请在服务停止后更改启用的规则。</string>
|
||||
<string name="settings_manual">使用手册</string>
|
||||
<string name="settings_manual_summary">访问 GitHub wiki 页面。</string>
|
||||
@ -83,4 +84,5 @@
|
||||
<string name="action_reload">重新加载规则</string>
|
||||
<string name="notice_check_dynamic_rule_reload">请在设置中勾选“允许动态更新规则”</string>
|
||||
<string name="notice_export_complete">导出完成。</string>
|
||||
<string name="notice_importing_rule">正在导入规则,请稍等。</string>
|
||||
</resources>
|
@ -56,7 +56,7 @@
|
||||
<string name="settings_server_port">伺服器埠</string>
|
||||
<string name="delete">刪除</string>
|
||||
<string name="apply">套用</string>
|
||||
<string name="export">导出</string>
|
||||
<string name="export">导出 TODO</string>
|
||||
<string name="notice_fill_in_all">請填寫所有設定項。</string>
|
||||
<string name="settings_dns_over_tcp">DNS over TCP</string>
|
||||
<string name="action_removed">已移除。</string>
|
||||
@ -71,7 +71,8 @@
|
||||
<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>
|
||||
<string name="settings_rule_import_build_in">内置</string>
|
||||
<string name="settings_rule_import_external">外部</string>
|
||||
<string name="notice_after_stop">請在服務停止後變更啟動的規則。</string>
|
||||
<string name="settings_manual">使用手冊</string>
|
||||
<string name="settings_manual_summary">造訪 GitHub wiki 頁面。</string>
|
||||
@ -82,5 +83,6 @@
|
||||
<string name="settings_allow_dynamic_rule_reload">允許動態規則重新載入</string>
|
||||
<string name="action_reload">重新載入</string>
|
||||
<string name="notice_check_dynamic_rule_reload">請在設定中開啟「允許動態規則重新載入」</string>
|
||||
<string name="notice_export_complete">导出完成。</string>
|
||||
<string name="notice_export_complete">导出完成。 TODO</string>
|
||||
<string name="notice_importing_rule">正在导入规则,请稍等。 TODO</string>
|
||||
</resources>
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
<string name="notice_main">See the world outside.</string>
|
||||
<string name="notice_activated">iTXTech Daedalus is activated.</string>
|
||||
<string name="notice_start_test">Testing specified DNS server …</string>
|
||||
<string name="notice_start_test">Testing specified DNS server…</string>
|
||||
<string name="notice_queries">DNS query times:</string>
|
||||
<string name="notice_legacy_api">The current Android API can not support all the features.</string>
|
||||
<string name="notice_need_restart">Re-activate Daedalus to make the settings take effect.</string>
|
||||
@ -16,6 +16,7 @@
|
||||
<string name="notice_after_stop">Please modify activated rules after stopping service.</string>
|
||||
<string name="notice_check_dynamic_rule_reload">Please enable "Allow dynamic rule reload" in Settings</string>
|
||||
<string name="notice_export_complete">Export completed.</string>
|
||||
<string name="notice_importing_rule">Importing rule… Please wait until blanks are filled.</string>
|
||||
|
||||
<string name="action_settings">Settings</string>
|
||||
<string name="action_about">About</string>
|
||||
@ -70,7 +71,8 @@
|
||||
<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>
|
||||
<string name="settings_rule_import_build_in">Build-in</string>
|
||||
<string name="settings_rule_import_external">External</string>
|
||||
|
||||
<string name="settings_server_name">Server Name</string>
|
||||
<string name="settings_server_address">Server Address</string>
|
||||
|
@ -33,5 +33,8 @@
|
||||
<ListPreference
|
||||
android:key="ruleImportBuildIn"
|
||||
android:title="@string/settings_rule_import_build_in"/>
|
||||
<org.itxtech.daedalus.widget.ClickPreference
|
||||
android:key="ruleImportExternal"
|
||||
android:title="@string/settings_rule_import_external"/>
|
||||
</PreferenceCategory>
|
||||
</PreferenceScreen>
|
Loading…
Reference in New Issue
Block a user