add java code missed in previous commit
This commit is contained in:
parent
aa1419e8bf
commit
59c9f2ca2d
2 changed files with 169 additions and 0 deletions
168
app/src/app/persistence/AppActivity.java
Normal file
168
app/src/app/persistence/AppActivity.java
Normal file
|
@ -0,0 +1,168 @@
|
||||||
|
package app.persistence;
|
||||||
|
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;
|
||||||
|
import android.view.ViewGroup.*;
|
||||||
|
import android.widget.*;
|
||||||
|
//import android.widget.Toast;
|
||||||
|
//import android.widget.TextView;
|
||||||
|
import android.webkit.*;
|
||||||
|
import android.net.Uri;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
//import android.webkit.WebView;
|
||||||
|
//import android.webkit.WebMessage;
|
||||||
|
//import android.webkit.WebMessagePort;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class AppActivity 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) {
|
||||||
|
final WebMessagePort[] channel=myWebView.createWebMessageChannel();
|
||||||
|
port=channel[0];
|
||||||
|
port.setWebMessageCallback(new WebMessagePort.WebMessageCallback() {
|
||||||
|
@Override
|
||||||
|
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("init-from-java", new WebMessagePort[]{channel[1]}),Uri.parse(BASE_URI));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String readFileFromAssets(String filename) {
|
||||||
|
String filecontents = "";
|
||||||
|
try {
|
||||||
|
InputStream stream = getAssets().open(filename);
|
||||||
|
int filesize = stream.available();
|
||||||
|
byte[] filebuffer = new byte[filesize];
|
||||||
|
stream.read(filebuffer);
|
||||||
|
stream.close();
|
||||||
|
filecontents = new String(filebuffer);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// I <3 java exceptions
|
||||||
|
}
|
||||||
|
return filecontents;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
// this removes the title bar (a ~1cm big strip at the top of the app showing its name
|
||||||
|
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
|
// we create the webview (at least on the android 14 that is a webview that features avif + websockets etc....)
|
||||||
|
WebView myWebView = new WebView(this);//activityContext);
|
||||||
|
// enables webview debugging
|
||||||
|
myWebView.setWebContentsDebuggingEnabled(true);
|
||||||
|
// we create a webviewclient (setwebviewclient-vs-setwebchromeclient)
|
||||||
|
WebViewClient myWebViewClient= new WebViewClient();
|
||||||
|
myWebView.setWebViewClient(myWebViewClient);
|
||||||
|
// to setup settings
|
||||||
|
WebSettings myWebSettings = myWebView.getSettings();
|
||||||
|
myWebSettings.setBuiltInZoomControls(true);
|
||||||
|
myWebSettings.setDisplayZoomControls(false);
|
||||||
|
myWebSettings.setJavaScriptEnabled(true);
|
||||||
|
myWebView.addJavascriptInterface(this, "myJavaScriptInterface");
|
||||||
|
// load the html from assets file
|
||||||
|
String html = readFileFromAssets("index.html");
|
||||||
|
myWebView.loadDataWithBaseURL(BASE_URI,html, "text/html", "UTF-8",null);
|
||||||
|
//myWebView.loadData(encodedHtml, "text/html", "base64");
|
||||||
|
// alternatively this could be to load a website
|
||||||
|
//myWebView.loadUrl("https://alexmahr.de/ru");
|
||||||
|
setContentView(myWebView);
|
||||||
|
new CountDownTimer(500, 100) {
|
||||||
|
public void onTick(long millisUntilFinished) {
|
||||||
|
try{
|
||||||
|
JSONObject MyJSONObject = new JSONObject("{\"json\":[1,2,3],\"something\":\"test\"}");
|
||||||
|
myWebView.postWebMessage(new WebMessage("this is the message"+millisUntilFinished+ " " + MyJSONObject.get("something")),Uri.parse(BASE_URI));
|
||||||
|
} catch( Exception e) { }
|
||||||
|
// mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
|
||||||
|
// myWebView.evaluateJavascript("document.body.innerHTML='SUP "+millisUntilFinished+" all is lost';",null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onFinish() {
|
||||||
|
initPort(myWebView);//myWebView.evaluateJavascript("document.body.innerHTML='all is lost';",null);
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
|
@JavascriptInterface
|
||||||
|
public String toString() {
|
||||||
|
// this.webview.evaluateJavascript("(setTimeout(()=>{document.body.innerHTML='all gone';},2000)()",null);
|
||||||
|
return "this is good";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
1
app/src/package
Symbolic link
1
app/src/package
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
app/persistence
|
Loading…
Add table
Reference in a new issue