Skip to content

Commit

Permalink
Fixed rule download on Android O
Browse files Browse the repository at this point in the history
  • Loading branch information
PeratX committed Jun 23, 2018
1 parent 478271a commit 710bfb4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
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;
Expand All @@ -24,8 +27,7 @@
import org.itxtech.daedalus.widget.ClickPreference;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;

/**
* Daedalus Project
Expand All @@ -39,6 +41,12 @@
* (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;
Expand Down Expand Up @@ -127,7 +135,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
buffer.flush();
mHandler.obtainMessage(RuleConfigHandler.MSG_RULE_DOWNLOADED,
new RuleData(ruleFilename.getText(), buffer.toByteArray())).sendToTarget();
stopThread();
} catch (Exception e) {
Logger.logException(e);
} finally {
Expand All @@ -137,21 +144,20 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
} 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();
}
Expand Down Expand Up @@ -366,8 +372,14 @@ public void handleMessage(Message msg) {

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());
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 710bfb4

Please sign in to comment.