From 947f0576496e755424483200db5ce3f68a6a1e6c Mon Sep 17 00:00:00 2001 From: Alexander Mahr Date: Thu, 10 Oct 2024 02:05:22 +0200 Subject: [PATCH] request manage all files, list files --- apk/AndroidManifest.xml | 1 + apk/assets/index.html | 109 +++++++++++++++++++++++++--- apk/src/app/example/ExampleApp.java | 87 ++++++++++++++++++++-- 3 files changed, 180 insertions(+), 17 deletions(-) diff --git a/apk/AndroidManifest.xml b/apk/AndroidManifest.xml index d9b61ec..7319c7a 100644 --- a/apk/AndroidManifest.xml +++ b/apk/AndroidManifest.xml @@ -9,6 +9,7 @@ + diff --git a/apk/assets/index.html b/apk/assets/index.html index b38bf97..06c166c 100644 --- a/apk/assets/index.html +++ b/apk/assets/index.html @@ -1,21 +1,106 @@ https://html5test.co/

this is html

this is a h2

+ diff --git a/apk/src/app/example/ExampleApp.java b/apk/src/app/example/ExampleApp.java index 7e41bbe..0d68748 100644 --- a/apk/src/app/example/ExampleApp.java +++ b/apk/src/app/example/ExampleApp.java @@ -1,10 +1,13 @@ package app.example; - +import android.provider.Settings ; +import android.content.Intent; import android.util.Log; import android.util.Base64; +import java.util.Objects; import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; +import android.os.Environment; import android.text.method.ScrollingMovementMethod; import android.view.*; //import android.view.MenuItem; @@ -18,9 +21,50 @@ import org.json.JSONObject; //import android.webkit.WebMessage; //import android.webkit.WebMessagePort; import java.io.InputStream; +import java.io.File; public class ExampleApp extends Activity { + public JSONObject doLs(JSONObject message){ + try{ + JSONObject file; + File directory = new File(Environment.getExternalStorageDirectory().toString() + message.getString("path")); + File[] files = directory.listFiles(); + for (int i = 0; i < files.length; i++) + { + try{ + //Log.d("ALEXINFO","filename"+files[i].getName()); + file = new JSONObject(); + file.put("name",files[i].getName()); + file.put("isFile",files[i].isFile()); + file.put("isDirectory",files[i].isDirectory()); + file.put("size",files[i].length()); + message.accumulate("result",file); + } catch (Exception e) { + Log.d("Exception EX1","ex1"); + } + } + } catch (Exception e) { + Log.d("Exception EX2","ex2"); + } + return message; + } + + public JSONObject doCat(JSONObject message){ + try{ + File directory = new File(Environment.getExternalStorageDirectory().toString() + message.getString("path")); + File[] files = directory.listFiles(); + for (int i = 0; i < files.length; i++) + { + try{ + message.accumulate("result",files[i].getName()); + } catch (Exception e) {} + } + } catch (Exception e) {} + return message; + } + + final static int APP_STORAGE_ACCESS_REQUEST_CODE = 501; // Any value private static final String BASE_URI = "https://alexmahr.de"; private WebMessagePort port; private void initPort(WebView myWebView) { @@ -28,10 +72,35 @@ public class ExampleApp extends Activity { port=channel[0]; port.setWebMessageCallback(new WebMessagePort.WebMessageCallback() { @Override - public void onMessage(WebMessagePort port, WebMessage message) { + public void onMessage(WebMessagePort porte, WebMessage message) { + try{ + JSONObject messageJSON = new JSONObject(message.getData()); + JSONObject reply; + if(Objects.equals(messageJSON.getString("function"),"ls")) + { + reply = doLs(messageJSON); + reply.put("super","man"); + port.postMessage(new WebMessage(reply.toString())); + return; + } + else if(Objects.equals(messageJSON.getString("function"),"cat")) + { + reply = doCat(messageJSON); + reply.put("super","cat"); + port.postMessage(new WebMessage(reply.toString())); + return; + } + else { + reply=messageJSON; + reply.put("super","else"); + reply.accumulate("result",1); + reply.accumulate("result","something"); + port.postMessage(new WebMessage(reply.toString())); + } + } catch( Exception e) { } } }); - myWebView.postWebMessage(new WebMessage("", new WebMessagePort[]{channel[1]}),Uri.parse(BASE_URI)); + myWebView.postWebMessage(new WebMessage("init-from-java", new WebMessagePort[]{channel[1]}),Uri.parse(BASE_URI)); } public String readFileFromAssets(String filename) { @@ -73,7 +142,7 @@ public class ExampleApp extends Activity { // alternatively this could be to load a website //myWebView.loadUrl("https://alexmahr.de/ru"); setContentView(myWebView); - new CountDownTimer(5000, 1000) { + new CountDownTimer(500, 100) { public void onTick(long millisUntilFinished) { try{ JSONObject MyJSONObject = new JSONObject("{\"json\":[1,2,3],\"something\":\"test\"}"); @@ -84,9 +153,17 @@ public class ExampleApp extends Activity { } public void onFinish() { - myWebView.evaluateJavascript("document.body.innerHTML='all is lost';",null); + initPort(myWebView);//myWebView.evaluateJavascript("document.body.innerHTML='all is lost';",null); } }.start(); +// Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, Uri.parse("package: app.example"));// + BuildConfig.APPLICATION_ID)); + Intent intent = new Intent(); + intent.setAction(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION); + //intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION); + Uri uri = Uri.fromParts("package", this.getPackageName(), null); + intent.setData(uri); + startActivity(intent); +// startActivityForResult(intent, APP_STORAGE_ACCESS_REQUEST_CODE); } @JavascriptInterface public String toString() {