Compare commits

...
Sign in to create a new pull request.

20 commits

Author SHA1 Message Date
2386b29233 add new branch "native" 2024-10-10 11:56:06 +02:00
947f057649 request manage all files, list files 2024-10-10 02:05:22 +02:00
a2c5d60e17 switch to load html from file (form assets) 2024-10-09 21:36:45 +02:00
dc86fdeb8b add assets folder to app 2024-10-09 21:32:26 +02:00
61c53c1fe4 example webmessage 2024-10-09 20:29:40 +02:00
f6261aebcd test countdowntimer 2024-10-09 18:26:49 +02:00
9a75e30ae0 add apk/result to gitignore 2024-10-09 18:26:16 +02:00
452082de2a fix Makefile $$ issue 2024-10-09 18:24:55 +02:00
f1dcecf971 javascriptinterface in activity itself 2024-10-09 16:23:50 +02:00
51734be9f8 test Thread.sleep 2024-10-09 16:13:30 +02:00
1c4c210091 non-working because JavasBrigde <> Webview thread 2024-10-09 16:03:27 +02:00
8785107ea5 timer , but not working with webview 2024-10-09 15:50:27 +02:00
bf535476ec add to the Webview content, link and javascript test 2024-10-09 09:25:39 +02:00
07ab6c4ab1 fix gitignore 2024-10-09 09:18:45 +02:00
7833f70173 add /example.app.apk to gitignore 2024-10-09 09:17:44 +02:00
6059e20bb5 set default value for YESACCEPT to suppress WARN 2024-10-09 09:17:08 +02:00
b9b9e32b8b improve Makefile and add red warning for missing YESACCEPT=y 2024-10-09 09:16:28 +02:00
703e34f0b4 fix issue with apk being add into itself 2024-10-08 15:44:19 +02:00
033219169b enable javascript in webview 2024-10-08 15:13:06 +02:00
1a1b53aaa6 make a webview with a static local webpage, via "datauri" 2024-10-08 12:00:10 +02:00
9 changed files with 329 additions and 45 deletions

4
.gitignore vendored
View file

@ -2,4 +2,8 @@ apk/ToyKey.keystore
apk/obj
apk/obj/*
apk/bin/*
apk/result
apk/result/
apk/result/*
apk/bin/!.gitkeep
/example.app.apk

View file

@ -19,9 +19,11 @@ ENV JAVA_HOME="/usr/lib/jvm/java-11-openjdk/"
#ENTRYPOINT bash -c 'sleep 10000'
ENV LIBRARY_PATH="$LIBRARY_PATH:$BUILD_TOOLS_LATEST/lib"
ARG YESACCEPT=n
RUN test "$YESACCEPT" = "y" || { printf "\033[31;1;4m%s\n%s\033[0m " "FAILED TO BUILD CONTAINER: You did not ACCEPT THE UPSTREAM LICENSE" " -> export YESACCEPT=y" >&2; exit 1; }
RUN echo you selected to accept the licenses/TOS
RUN echo "$YESACCEPT" | sdkmanager --install "build-tools;33.0.2"
RUN echo "$YESACCEPT" | sdkmanager --install "platforms;android-33"
RUN echo "$YESACCEPT" | sdkmanager --install "ndk;28.0.12433566"
#RUN apk add setpriv
COPY entrypoint.sh /entrypoint.sh
RUN chown 0:0 /entrypoint.sh

View file

@ -3,33 +3,25 @@
all: build install
build:
docker-compose run compile
docker-compose run --rm compile
ln -sf apk/result/example.app.apk ./; test -e ./example.app.apk || rm ./example.app.apk
install:
adb install -r ./apk/example.app.apk
adb install -r ./example.app.apk
.PHONY: clean-all
clean-all: clean-docker clean-apk
.PHONY: clean-docker
clean-docker:
rm docker-compose-build.log || true
docker-compose down --remove-orphans --rmi all
.PHONY: clean-apk
clean-apk:
cd apk && $(MAKE) clean
apk: apk/app.apk
true
apk/app.apk: docker-compose-build.log
docker-compose run --rm compile
docker-compose-build.log: Dockerfile docker-compose.yml
docker-compose down --remove-orphans --rmi all
BUILDKIT_PROGRESS=plain docker-compose build | tee docker-compose-build.log

View file

@ -1,11 +1,28 @@
# Repo allowing -in a KISS way- to create a containered way to build an Android App APK
## brach `no-res.xml`
## branchs
### branch `native`
this branch should allow to use the Native Developer Kit to build an app without much java
the content below apk/native are LICENSE MIT
### branch `no-res.xml`
this branch features the exmaple.app to not require the compoletely bogus unneeded requirement
to setup a `./apk/res/layouts/res.xml` file to setup the layout to be used.
Instead a layout is created inline
### branch `webview`
a bra
## changelog
* using https://android.googlesource.com/platform/cts/+/android-7.1.1_r13/tests/tests/webkit/src/android/webkit/cts/PostMessageTest.java derived
webmessage to send data to Javascript/Webview from Java (using a timer)
## tl;dr
@ -27,7 +44,7 @@ This will generate the APK file `./apk/example.app.apk`
this can be installed via `adb`
```
adb install -r ./apk/example.app.apk
adb install -r ./example.app.apk
# or alternative type
make install
```

View file

@ -5,9 +5,25 @@
android:versionName="1.0">
<uses-sdk android:minSdkVersion="30"
android:targetSdkVersion="33"/>
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<!-- android:maxSdkVersion="integer" /> -->
<uses-permission android:name="android.permission.INTERNET"/>
<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"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="app.example.ExampleApp"
android:exported="true"

View file

@ -14,29 +14,34 @@ all: build
.PHONY : deploy
.PHONY : clean
build : ./bin/example.app.apk
ln -sf ./bin/example.app.apk ./example.app.apk
build : ./result/example.app.apk
true
./bin/example.app.apk : ./bin/signed.apk
env:
set > set ; env > env
./result/example.app.apk : ./result/signed.apk
$(ANDROID_HOME)/build-tools/$(ANDROID_VERSION)/zipalign -v -f 4 $< $@
$(ANDROID_HOME)/build-tools/$(ANDROID_VERSION)/apksigner sign --ks ToyKey.keystore --key-pass pass:armena --ks-pass pass:armena $@
./bin/signed.apk : ./bin/unsigned.apk ./ToyKey.keystore
./result/signed.apk : ./result/unsigned.apk ./ToyKey.keystore ./result
jarsigner -verbose -keystore ./ToyKey.keystore -storepass armena -keypass armena -signedjar $@ $< helljniKey
./bin/unsigned.apk : ./bin/classes.dex
./result:
mkdir -p "$@"
./result/unsigned.apk : ./bin/classes.dex ./result
rm -rvf "$@"
$(ANDROID_HOME)/build-tools/$(ANDROID_VERSION)/aapt package -v -u -f -M ./AndroidManifest.xml -S ./res \
-I $(ANDROID_HOME)/platforms/$(PLATFORM)/android.jar -F $@ ./bin
-I $(ANDROID_HOME)/platforms/$(PLATFORM)/android.jar -A ./assets -F $@ ./bin
./bin/classes.dex : ./obj/app/example/ExampleApp.class
$(ANDROID_HOME)/build-tools/$(ANDROID_VERSION)/d8 $(shell find obj -name '*.class') --lib $(ANDROID_HOME)/platforms/$(PLATFORM)/android.jar --output bin
./bin/classes.dex : ./obj/app/example/ExampleApp.class ./obj/app/example/ExampleApp$$1.class
$(ANDROID_HOME)/build-tools/$(ANDROID_VERSION)/d8 ./obj/app/example/*.class --lib $(ANDROID_HOME)/platforms/$(PLATFORM)/android.jar --output bin
./src/app/example/R.java : $(shell find ./res -type f)
$(ANDROID_HOME)/build-tools/$(ANDROID_VERSION)/aapt package -v -f -m -S ./res -J ./src -M ./AndroidManifest.xml \
-I $(ANDROID_HOME)/platforms/$(PLATFORM)/android.jar
./obj/app/example/ExampleApp.class : ./src/app/example/ExampleApp.java ./src/app/example/R.java
./obj/app/example/ExampleApp.class : ./src/app/example/ExampleApp.java ./src/app/example/R.java
javac -d ./obj -classpath $(ANDROID_HOME)/platforms/$(PLATFORM)/android.jar -sourcepath ./src $<
./ToyKey.keystore :
@ -44,4 +49,4 @@ build : ./bin/example.app.apk
-storepass armena -keypass armena -alias helljniKey -keyalg RSA -v
clean:
rm -f ./bin/* ./lib/arm64-v8a/*
rm -f ./bin/* ./lib/arm64-v8a/* ./result/*

106
apk/assets/index.html Normal file
View file

@ -0,0 +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)=>{
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',()=>{
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>

View file

@ -1,33 +1,174 @@
package app.example;
import android.provider.Settings ;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
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.MenuItem;
import android.view.*;
import android.widget.*;
//import android.view.MenuItem;
import android.view.ViewGroup.*;
import android.widget.*;
//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 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) {
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);
// creating LinearLayout
LinearLayout linLayout = new LinearLayout(this);
// specifying vertical orientation
linLayout.setOrientation(LinearLayout.VERTICAL);
// creating LayoutParams
LayoutParams linLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// set LinearLayout as a root element of the screen
setContentView(linLayout, linLayoutParam);
// 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);
// MyJavascriptInterface myJavaScriptInterface = new MyJavascriptInterface(this,myWebView);
// we create a webview (there is also 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);
}
LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setText("SUPERVIEalexW");
tv.setLayoutParams(lpView);
linLayout.addView(tv);
public void onFinish() {
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() {
// this.webview.evaluateJavascript("(setTimeout(()=>{document.body.innerHTML='all gone';},2000)()",null);
return "this is good";
}
}

View file

@ -1,9 +1,10 @@
services:
compile:
hostname: android-app-builder
build:
context: .
args:
YESACCEPT: ${YESACCEPT}
YESACCEPT: ${YESACCEPT:-n}
stop_grace_period: 1s
volumes:
- ./apk:/apk