Skip to content

Commit 710bfb4

Browse files
committed
Fixed rule download on Android O
1 parent 478271a commit 710bfb4

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Open Source Licenses
7878

7979
Credits
8080
------------
81-
* __[Jetbrains](https://www.jetbrains.com/)__ - For providing free license for [IntelliJ IDEA](https://www.jetbrains.com/idea/)
82-
* __[SNC IDC](http://www.sncidc.com/)__ - For supporting us with love
81+
* __[JetBrains](https://www.jetbrains.com/)__ - For providing free license for [IntelliJ IDEA](https://www.jetbrains.com/idea/)
82+
* __[ShenniaoTech](https://www.sncidc.com/)__ - For supporting us with love
8383

8484
License
8585
------------

app/src/main/java/org/itxtech/daedalus/fragment/RuleConfigFragment.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import android.view.MenuItem;
1717
import android.view.View;
1818
import android.view.ViewGroup;
19+
import okhttp3.OkHttpClient;
20+
import okhttp3.Request;
21+
import okhttp3.Response;
1922
import org.itxtech.daedalus.Daedalus;
2023
import org.itxtech.daedalus.R;
2124
import org.itxtech.daedalus.activity.ConfigActivity;
@@ -24,8 +27,7 @@
2427
import org.itxtech.daedalus.widget.ClickPreference;
2528

2629
import java.io.*;
27-
import java.net.URL;
28-
import java.net.URLConnection;
30+
import java.util.concurrent.TimeUnit;
2931

3032
/**
3133
* Daedalus Project
@@ -39,6 +41,12 @@
3941
* (at your option) any later version.
4042
*/
4143
public class RuleConfigFragment extends ConfigFragment {
44+
private static final OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder()
45+
.connectTimeout(5, TimeUnit.SECONDS)
46+
.readTimeout(10, TimeUnit.SECONDS)
47+
.writeTimeout(10, TimeUnit.SECONDS)
48+
.build();
49+
4250
private static final int READ_REQUEST_CODE = 1;
4351
private Intent intent = null;
4452
private Thread mThread = null;
@@ -127,7 +135,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
127135
buffer.flush();
128136
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
129137
new RuleData(ruleFilename.getText(), buffer.toByteArray())).sendToTarget();
130-
stopThread();
131138
} catch (Exception e) {
132139
Logger.logException(e);
133140
} finally {
@@ -137,21 +144,20 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
137144
} else {
138145
mThread = new Thread(() -> {
139146
try {
140-
URLConnection connection = new URL(ruleDownloadUrl.getText()).openConnection();
141-
InputStream inputStream = connection.getInputStream();
142-
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
143-
StringBuilder builder = new StringBuilder();
144-
String result;
145-
while ((result = reader.readLine()) != null) {
146-
builder.append("\n").append(result);
147+
Request request = new Request.Builder()
148+
.url(ruleDownloadUrl.getText()).get().build();
149+
Response response = HTTP_CLIENT.newCall(request).execute();
150+
Logger.info("Downloaded " + ruleDownloadUrl.getText());
151+
if (response.isSuccessful() && mHandler != null) {
152+
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
153+
new RuleData(ruleFilename.getText(), response.body().bytes())).sendToTarget();
147154
}
148-
reader.close();
149-
150-
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
151-
new RuleData(ruleFilename.getText(), builder.toString().getBytes())).sendToTarget();
152-
stopThread();
153155
} catch (Exception e) {
154156
Logger.logException(e);
157+
if (mHandler != null) {
158+
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
159+
new RuleData(ruleFilename.getText(), new byte[0])).sendToTarget();
160+
}
155161
} finally {
156162
stopThread();
157163
}
@@ -366,8 +372,14 @@ public void handleMessage(Message msg) {
366372

367373
switch (msg.what) {
368374
case MSG_RULE_DOWNLOADED:
375+
RuleData ruleData = (RuleData) msg.obj;
376+
if (ruleData.data.length == 0) {
377+
if (view != null) {
378+
Snackbar.make(view, R.string.notice_download_failed, Snackbar.LENGTH_SHORT).show();
379+
}
380+
break;
381+
}
369382
try {
370-
RuleData ruleData = (RuleData) msg.obj;
371383
File file = new File(Daedalus.rulePath + ruleData.getFilename());
372384
FileOutputStream stream = new FileOutputStream(file);
373385
stream.write(ruleData.getData());

app/src/main/res/values-zh-rCN/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<string name="notice_need_restart">重新启用 Daedalus 以应用设置。</string>
1212
<string name="notice_start_download">正在下载规则,请稍等 ……</string>
1313
<string name="notice_downloaded">已下载规则。</string>
14+
<string name="notice_download_failed">规则下载失败。</string>
1415
<string name="notice_now_downloading">Daedalus 当前正在下载规则,请稍等。</string>
1516
<string name="notice_fill_in_all">请填写所有配置项。</string>
1617
<string name="notice_delete_confirm_prompt">您确定要删除此配置项?</string>

app/src/main/res/values-zh-rTW/strings.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
<string name="notice_need_restart">重新啟動 Daedalus 以套用設定。</string>
1212
<string name="notice_start_download">正在下載規則,請稍等……</string>
1313
<string name="notice_downloaded">已下載規則。</string>
14+
<string name="notice_download_failed">規則下載失敗。</string>
1415
<string name="notice_now_downloading">Daedalus 目前正在下載規則,請稍等。</string>
1516
<string name="notice_fill_in_all">請填寫所有設定項。</string>
1617
<string name="notice_delete_confirm_prompt">您確定要刪除此設定項?</string>
1718
<string name="notice_after_stop">請在服務停止後變更啟動的規則。</string>
1819
<string name="notice_check_dynamic_rule_reload">請在設定中開啟「允許動態規則重新載入」</string>
19-
<string name="notice_export_complete">导出完成。 TODO</string>
20-
<string name="notice_importing_rule">正在导入规则,请稍等。 TODO</string>
20+
<string name="notice_export_complete">導出完成。</string>
21+
<string name="notice_importing_rule">正在導入規則,請稍等。</string>
2122

2223
<string name="action_settings">設定</string>
2324
<string name="action_about">關於</string>
@@ -34,7 +35,7 @@
3435

3536
<string name="delete">刪除</string>
3637
<string name="apply">套用</string>
37-
<string name="export">导出 TODO</string>
38+
<string name="export">導出</string>
3839

3940
<string name="navigation_drawer_close">關閉隱藏式瀏覽選單</string>
4041
<string name="navigation_drawer_open">開啟隱藏式瀏覽選單</string>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<string name="notice_need_restart">Re-activate Daedalus to make the settings take effect.</string>
1111
<string name="notice_start_download">Downloading rule, please wait …</string>
1212
<string name="notice_downloaded">Rule has been downloaded.</string>
13+
<string name="notice_download_failed">Download failed.</string>
1314
<string name="notice_now_downloading">Daedalus is currently downloading rule, please wait.</string>
1415
<string name="notice_fill_in_all">Please fill in all configuration items.</string>
1516
<string name="notice_delete_confirm_prompt">Are you sure you want to remove this configuration?</string>

0 commit comments

Comments
 (0)