Skip to content

Commit

Permalink
read from file
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault-bilibili committed Oct 24, 2022
1 parent 2ac3afa commit 81a198b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
Expand All @@ -23,6 +26,7 @@
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.shadowsocks.plugin.ConfigurationActivity;
import com.github.shadowsocks.plugin.PluginOptions;
Expand All @@ -31,13 +35,18 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class ConfigActivity extends ConfigurationActivity {
Expand Down Expand Up @@ -348,6 +357,7 @@ private void addFileEntry(final String fileName, final String fileData, String h
@SuppressLint("InflateParams") final View child = inflater.inflate(R.layout.fileentry, null);

TextView fileNameLabel = child.findViewById(R.id.text_file_name);
Button button_load = child.findViewById(R.id.button_load);
Button button_del_file = child.findViewById(R.id.button_del_file);
EditText fileDataEditText = child.findViewById(R.id.editText_file_data);

Expand All @@ -363,6 +373,13 @@ private void addFileEntry(final String fileName, final String fileData, String h

fileDataMap.put(fileName, fileDataEditText.getText());

button_load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readFromFile(fileName);
}
});

button_del_file.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -373,6 +390,72 @@ public void onClick(View v) {
parent.addView(child);
}

private static final int READ_FROM_FILE = 1;
private String openingFileName = "";
private void readFromFile(String openingFileName) {
if (readFileThread != null && readFileThread.isAlive()) {
showToast(R.string.still_reading);
return;
}
this.openingFileName = openingFileName;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
String mimeType = "*/*";
if (openingFileName.endsWith(".json"))
mimeType = "application/json";
else if (openingFileName.endsWith(".pem"))
mimeType = "application/x-pem-file";
else if (openingFileName.endsWith(".txt"))
mimeType = "text/plain";
intent.setType(mimeType);
startActivityForResult(intent, READ_FROM_FILE);
}

private Thread readFileThread;
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == READ_FROM_FILE) {
if (data != null) {
final Uri uri = data.getData();
final ContentResolver resolver = this.getContentResolver();
if (readFileThread == null || !readFileThread.isAlive()) {
readFileThread = new Thread() {
@Override
public void run() {
try {
StringBuilder stringBuilder = new StringBuilder();
InputStream inputStream = resolver.openInputStream(uri);
BufferedReader reader = new BufferedReader(new InputStreamReader(Objects.requireNonNull(inputStream)));
char[] buf = new char[4096];
for (int r, t = 0; (r = reader.read(buf)) != -1; t += r) {
if (t > 1024 * 1024) {
showToast(R.string.err_file_too_large);
return;
}
stringBuilder.append(buf, 0, r);
}
final String result = stringBuilder.toString();
runOnUiThread(new Runnable() {
@Override
public void run() {
Editable editable = fileDataMap.get(openingFileName);
if (editable != null) {
editable.clear();
editable.append(result);
}
}
});
} catch (IOException ignored) {}
}
};
readFileThread.start();
} else Log.e("ConfigActivity", "readFileThread is unexpectedly alive");
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}

private final String[] fileNameList = {
"config.json",
"cacert.pem",
Expand Down
33 changes: 24 additions & 9 deletions app/src/main/res/layout/fileentry.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,31 @@
android:textSize="24sp"
android:textStyle="bold" />

<Button
android:id="@+id/button_del_file"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#303030"
android:backgroundTint="#303030"
android:text="@string/string_delete"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold" />
android:orientation="horizontal">
<Button
android:id="@+id/button_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#303030"
android:backgroundTint="#303030"
android:text="@string/string_load"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_del_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#303030"
android:backgroundTint="#303030"
android:text="@string/string_delete"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>

<EditText
android:id="@+id/editText_file_data"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<string name="string_delete">删除</string>
<string name="confirm_del_file_title">删除单个文件</string>
<string name="confirm_del_file_msg">你确定要删除这个文件吗?\n文件名: </string>
<string name="string_load">加载</string>
<string name="still_reading">仍在读取之前的文件,请稍后重试</string>
<string name="err_file_too_large">错误!文件过大(>1M字符)</string>
<string name="example_cfgjson">/* 示例配置,不会被保存生效 */\n{\n\t\t\"Debug\": true,\n\t\t\"Retries\": 0,\n\t\t\"ServeNodes\": [\n\t\t\t\t\":8080\",\n\t\t\t\t\"ss://chacha20:12345678@:8338\"\n\t\t],\n\t\t\"ChainNodes\": [\n\t\t\t\t\"http://192.168.1.1:8080\",\n\t\t\t\t\"https://10.0.2.1:443\"\n\t\t],\n\t\t\"Routes\": [\n\t\t\t\t{\n\t\t\t\t\t\t\"Retries\": 1,\n\t\t\t\t\t\t\"ServeNodes\": [\n\t\t\t\t\t\t\t\t\"ws://:1443\"\n\t\t\t\t\t\t],\n\t\t\t\t\t\t\"ChainNodes\": [\n\t\t\t\t\t\t\t\t\"socks://:192.168.1.1:1080\"\n\t\t\t\t\t\t]\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\t\"Retries\": 3,\n\t\t\t\t\t\t\"ServeNodes\": [\n\t\t\t\t\t\t\t\t\"quic://:443\"\n\t\t\t\t\t\t]\n\t\t\t\t}\n\t\t]\n}\n</string>
<string name="example_cacert">-----BEGIN CERTIFICATE-----\n(你自己的CA证书,Base64 PEM格式)\n-----END CERTIFICATE-----</string>
<string name="example_clientcert">-----BEGIN CERTIFICATE-----\n(你自己的客户端证书,Base64 PEM格式)\n-----END CERTIFICATE-----</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<string name="string_delete">Delete</string>
<string name="confirm_del_file_title">Delete a file</string>
<string name="confirm_del_file_msg">Are you sure to delete this file?\nFile name: </string>
<string name="string_load">Load</string>
<string name="still_reading">Still reading the previous file, please retry later</string>
<string name="err_file_too_large">Error! File is too large (>1M characters)</string>
<string name="example_cfgjson">/* sample config, won\'t be saved &amp; applied */\n{\n\t\t\"Debug\": true,\n\t\t\"Retries\": 0,\n\t\t\"ServeNodes\": [\n\t\t\t\t\":8080\",\n\t\t\t\t\"ss://chacha20:12345678@:8338\"\n\t\t],\n\t\t\"ChainNodes\": [\n\t\t\t\t\"http://192.168.1.1:8080\",\n\t\t\t\t\"https://10.0.2.1:443\"\n\t\t],\n\t\t\"Routes\": [\n\t\t\t\t{\n\t\t\t\t\t\t\"Retries\": 1,\n\t\t\t\t\t\t\"ServeNodes\": [\n\t\t\t\t\t\t\t\t\"ws://:1443\"\n\t\t\t\t\t\t],\n\t\t\t\t\t\t\"ChainNodes\": [\n\t\t\t\t\t\t\t\t\"socks://:192.168.1.1:1080\"\n\t\t\t\t\t\t]\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t\t\"Retries\": 3,\n\t\t\t\t\t\t\"ServeNodes\": [\n\t\t\t\t\t\t\t\t\"quic://:443\"\n\t\t\t\t\t\t]\n\t\t\t\t}\n\t\t]\n}</string>
<string name="example_cacert">-----BEGIN CERTIFICATE-----\n(Your own CA certificate in Base64 PEM format)\n-----END CERTIFICATE-----</string>
<string name="example_clientcert">-----BEGIN CERTIFICATE-----\n(Your own client certificate in Base64 PEM format)\n-----END CERTIFICATE-----</string>
Expand Down

0 comments on commit 81a198b

Please sign in to comment.