From 81a198b410af2e4393683a9211e3c821df53d4ee Mon Sep 17 00:00:00 2001 From: segfault-bilibili Date: Mon, 24 Oct 2022 18:07:47 +0800 Subject: [PATCH] read from file --- .../plugin/gost/ConfigActivity.java | 83 +++++++++++++++++++ app/src/main/res/layout/fileentry.xml | 33 ++++++-- app/src/main/res/values-zh/strings.xml | 3 + app/src/main/res/values/strings.xml | 3 + 4 files changed, 113 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/github/shadowsocks/plugin/gost/ConfigActivity.java b/app/src/main/java/com/github/shadowsocks/plugin/gost/ConfigActivity.java index f63be05..271d327 100644 --- a/app/src/main/java/com/github/shadowsocks/plugin/gost/ConfigActivity.java +++ b/app/src/main/java/com/github/shadowsocks/plugin/gost/ConfigActivity.java @@ -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; @@ -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; @@ -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 { @@ -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); @@ -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) { @@ -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", diff --git a/app/src/main/res/layout/fileentry.xml b/app/src/main/res/layout/fileentry.xml index 6309bdf..598d11b 100644 --- a/app/src/main/res/layout/fileentry.xml +++ b/app/src/main/res/layout/fileentry.xml @@ -15,16 +15,31 @@ android:textSize="24sp" android:textStyle="bold" /> -