request manage all files, list files
This commit is contained in:
parent
a2c5d60e17
commit
947f057649
3 changed files with 180 additions and 17 deletions
|
@ -9,6 +9,7 @@
|
|||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
|
||||
<uses-permission android:name="android.permission.MANAGE_MEDIA"/>
|
||||
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
||||
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>
|
||||
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
|
||||
|
|
|
@ -1,21 +1,106 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<script>
|
||||
|
||||
function sleepit(ms) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(1);
|
||||
}, ms);
|
||||
});
|
||||
}
|
||||
function divMessage(html){
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML=html;
|
||||
document.body.appendChild(div);
|
||||
div.scrollIntoView({ behavior: 'smooth'});
|
||||
}
|
||||
|
||||
(async function webviewprogram(){
|
||||
var port;
|
||||
var resolves = [];
|
||||
function setupMessage(){
|
||||
return new Promise((resolve) => {
|
||||
window.addEventListener('message',(e)=>{
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML='message'+e.data;
|
||||
document.body.appendChild(div);
|
||||
div.scrollIntoView({ behavior: 'smooth'});
|
||||
if(e.data=="init-from-java" && ! port){
|
||||
divMessage('INIT');
|
||||
port = e.ports[0];
|
||||
port.onmessage = function (ee) {
|
||||
divMessage('AAport.onmessage='+ee.data);
|
||||
var response = JSON.parse(ee.data);
|
||||
divMessage('response.resolveIndex'+response.resolveIndex);//AAport.onmessage='+ee.data);
|
||||
var localresolve = resolves[response.resolveIndex];
|
||||
divMessage(typeof localresolve)
|
||||
|
||||
localresolve(response);
|
||||
}
|
||||
resolve()
|
||||
}
|
||||
divMessage('message='+e.data);
|
||||
},true);
|
||||
});
|
||||
}
|
||||
function doLs(path){
|
||||
divMessage('doLs');
|
||||
return new Promise((resolve) => {
|
||||
divMessage('resolves.length'+resolves.length);
|
||||
resolves.push(resolve);
|
||||
divMessage('after push resolves.length'+resolves.length);
|
||||
port.postMessage('{"function":"ls","path":"'+path+'","resolveIndex":"'+(resolves.length-1)+'"}');
|
||||
});
|
||||
}
|
||||
function doCat(file){
|
||||
return new Promise((resolve) => {
|
||||
resolves.push(resolve);
|
||||
port.postMessage('{"function":"cat","resolveIndex":"'+resolves.length+'"}');
|
||||
});
|
||||
}
|
||||
window.addEventListener('load',()=>{
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML='JAVASCRIPT works'
|
||||
document.body.appendChild(div);
|
||||
div.scrollIntoView({ behavior: 'smooth'});
|
||||
divMessage('JAVASCRIPT WORKS');
|
||||
},false);
|
||||
// await sleepit(2000);
|
||||
// divMessage('awaited 2000');
|
||||
// await sleepit(2000);
|
||||
// divMessage('awaited again 2000');
|
||||
await setupMessage();
|
||||
divMessage('setup');
|
||||
async function mapDoLs(path){
|
||||
var reply = await doLs(path);
|
||||
divMessage("reply.result.length"+reply.result.length);
|
||||
reply.result.forEach((file)=>{
|
||||
divMessage(file.name);//reply.result.length"+reply.result.length);
|
||||
//var button
|
||||
button = document.createElement("button");
|
||||
button.addEventListener("click",()=>{
|
||||
if(file.isDirectory)
|
||||
mapDoLs(path+"/"+file.name);
|
||||
},false);
|
||||
//if(file.isDirectory){
|
||||
//{
|
||||
// button = document.createElement("button");
|
||||
// button.addEventListener("click",()=>{
|
||||
// mapDoLs(path+"/"+file.name);
|
||||
// },false);
|
||||
//} else {
|
||||
// button = document.createElement("div");
|
||||
//}
|
||||
////a.href="#";
|
||||
//////a.textContent="asdasdada"
|
||||
button.textContent=file.name+ " " + file.size;
|
||||
document.body.appendChild(button);
|
||||
});
|
||||
}
|
||||
await mapDoLs("/DCIM");
|
||||
divMessage("done");
|
||||
// for(let file of DCIM.result){
|
||||
// divMessage("filename");
|
||||
|
||||
//});
|
||||
})();
|
||||
</script>
|
||||
<a href='https://html5test.co/'>https://html5test.co/</a>
|
||||
<h1> this is html <h1>
|
||||
<h2> this is a h2</h2>
|
||||
<img src='https://wald.alexmahr.de/images/bear.avif'>
|
||||
<img src='https://wald.alexmahr.de/images/delphin.avif'>
|
||||
</html>
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Add table
Reference in a new issue