Fixed rule download on Android O

This commit is contained in:
PeratX 2018-06-23 11:15:29 +08:00
parent 478271a95b
commit 710bfb48de
5 changed files with 36 additions and 21 deletions

View File

@ -78,8 +78,8 @@ Open Source Licenses
Credits
------------
* __[Jetbrains](https://www.jetbrains.com/)__ - For providing free license for [IntelliJ IDEA](https://www.jetbrains.com/idea/)
* __[SNC IDC](http://www.sncidc.com/)__ - For supporting us with love
* __[JetBrains](https://www.jetbrains.com/)__ - For providing free license for [IntelliJ IDEA](https://www.jetbrains.com/idea/)
* __[ShenniaoTech](https://www.sncidc.com/)__ - For supporting us with love
License
------------

View File

@ -16,6 +16,9 @@ import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.ConfigActivity;
@ -24,8 +27,7 @@ import org.itxtech.daedalus.util.Rule;
import org.itxtech.daedalus.widget.ClickPreference;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;
/**
* Daedalus Project
@ -39,6 +41,12 @@ import java.net.URLConnection;
* (at your option) any later version.
*/
public class RuleConfigFragment extends ConfigFragment {
private static final OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
private static final int READ_REQUEST_CODE = 1;
private Intent intent = null;
private Thread mThread = null;
@ -127,7 +135,6 @@ public class RuleConfigFragment extends ConfigFragment {
buffer.flush();
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
new RuleData(ruleFilename.getText(), buffer.toByteArray())).sendToTarget();
stopThread();
} catch (Exception e) {
Logger.logException(e);
} finally {
@ -137,21 +144,20 @@ public class RuleConfigFragment extends ConfigFragment {
} else {
mThread = new Thread(() -> {
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);
Request request = new Request.Builder()
.url(ruleDownloadUrl.getText()).get().build();
Response response = HTTP_CLIENT.newCall(request).execute();
Logger.info("Downloaded " + ruleDownloadUrl.getText());
if (response.isSuccessful() && mHandler != null) {
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
new RuleData(ruleFilename.getText(), response.body().bytes())).sendToTarget();
}
reader.close();
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
new RuleData(ruleFilename.getText(), builder.toString().getBytes())).sendToTarget();
stopThread();
} catch (Exception e) {
Logger.logException(e);
if (mHandler != null) {
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
new RuleData(ruleFilename.getText(), new byte[0])).sendToTarget();
}
} finally {
stopThread();
}
@ -366,8 +372,14 @@ public class RuleConfigFragment extends ConfigFragment {
switch (msg.what) {
case MSG_RULE_DOWNLOADED:
RuleData ruleData = (RuleData) msg.obj;
if (ruleData.data.length == 0) {
if (view != null) {
Snackbar.make(view, R.string.notice_download_failed, Snackbar.LENGTH_SHORT).show();
}
break;
}
try {
RuleData ruleData = (RuleData) msg.obj;
File file = new File(Daedalus.rulePath + ruleData.getFilename());
FileOutputStream stream = new FileOutputStream(file);
stream.write(ruleData.getData());

View File

@ -11,6 +11,7 @@
<string name="notice_need_restart">重新启用 Daedalus 以应用设置。</string>
<string name="notice_start_download">正在下载规则,请稍等 ……</string>
<string name="notice_downloaded">已下载规则。</string>
<string name="notice_download_failed">规则下载失败。</string>
<string name="notice_now_downloading">Daedalus 当前正在下载规则,请稍等。</string>
<string name="notice_fill_in_all">请填写所有配置项。</string>
<string name="notice_delete_confirm_prompt">您确定要删除此配置项?</string>

View File

@ -11,13 +11,14 @@
<string name="notice_need_restart">重新啟動 Daedalus 以套用設定。</string>
<string name="notice_start_download">正在下載規則,請稍等……</string>
<string name="notice_downloaded">已下載規則。</string>
<string name="notice_download_failed">規則下載失敗。</string>
<string name="notice_now_downloading">Daedalus 目前正在下載規則,請稍等。</string>
<string name="notice_fill_in_all">請填寫所有設定項。</string>
<string name="notice_delete_confirm_prompt">您確定要刪除此設定項?</string>
<string name="notice_after_stop">請在服務停止後變更啟動的規則。</string>
<string name="notice_check_dynamic_rule_reload">請在設定中開啟「允許動態規則重新載入」</string>
<string name="notice_export_complete">导出完成。 TODO</string>
<string name="notice_importing_rule">正在导入规则,请稍等。 TODO</string>
<string name="notice_export_complete">導出完成。</string>
<string name="notice_importing_rule">正在導入規則,請稍等。</string>
<string name="action_settings">設定</string>
<string name="action_about">關於</string>
@ -34,7 +35,7 @@
<string name="delete">刪除</string>
<string name="apply">套用</string>
<string name="export">导出 TODO</string>
<string name="export">導出</string>
<string name="navigation_drawer_close">關閉隱藏式瀏覽選單</string>
<string name="navigation_drawer_open">開啟隱藏式瀏覽選單</string>

View File

@ -10,6 +10,7 @@
<string name="notice_need_restart">Re-activate Daedalus to make the settings take effect.</string>
<string name="notice_start_download">Downloading rule, please wait …</string>
<string name="notice_downloaded">Rule has been downloaded.</string>
<string name="notice_download_failed">Download failed.</string>
<string name="notice_now_downloading">Daedalus is currently downloading rule, please wait.</string>
<string name="notice_fill_in_all">Please fill in all configuration items.</string>
<string name="notice_delete_confirm_prompt">Are you sure you want to remove this configuration?</string>