Add SpaccWebView for Android

This commit is contained in:
2024-11-01 15:43:40 +01:00
parent 1374d68bd7
commit fd0151b999
21 changed files with 533 additions and 0 deletions

1
SpaccDotWeb.Android/app/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,28 @@
plugins {
id 'com.android.application'
}
android {
// Adjust the very following based on the application to ship
namespace 'com.example.spaccwebviewapplication'
defaultConfig {
applicationId 'com.example.spaccwebviewapplication'
versionCode 1
versionName '1.0'
minSdk 1
targetSdk 34
}
buildTypes {
debug {
applicationIdSuffix '.dbg'
}
release {
minifyEnabled true
signingConfig signingConfigs.debug
}
}
compileSdk 34
}

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="auto">
<!-- Lets the app access the Internet — not needed for fully offline apps -->
<!-- <uses-permission android:name="android.permission.INTERNET" /> -->
<!-- Lets the webview load and display files from the device's shared storage -->
<!-- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> -->
<!-- Additional suggested attributes:
android:appCategory=["accessibility" | "audio" | "game" | "image" | "maps" | "news" | "productivity" | "social" | "video"]
android:isGame=["true" | "false"]
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
-->
<application
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault.NoActionBar"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_rules"
android:dataExtractionRules="@xml/data_extraction_rules"
android:hasFragileUserData="true"
android:supportsRtl="true"
android:hasCode="true"
tools:targetApi="34">
<activity
android:name=".MainActivity"
android:exported="true"
android:configChanges="orientation|keyboardHidden|screenSize|screenLayout|layoutDirection"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

View File

@@ -0,0 +1,3 @@
<!DOCTYPE html>
<h1>SpaccWebView Application</h1>
<p><a href="https://gitlab.com/SpaccInc/SpaccDotWeb">https://gitlab.com/SpaccInc/SpaccDotWeb</a></p>

View File

@@ -0,0 +1,28 @@
package com.example.spaccwebviewapplication;
import android.app.Activity;
import android.os.Bundle;
import org.eu.spacc.spaccdotweb.android.*;
public class MainActivity extends Activity {
private SpaccWebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.loadAppIndex();
}
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
}

View File

@@ -0,0 +1,12 @@
package org.eu.spacc.spaccdotweb.android;
import android.os.Build;
public class ApiUtils {
public static void apiRun(int apiLevel, Runnable action) {
if (Build.VERSION.SDK_INT >= apiLevel) {
action.run();
}
}
}

View File

@@ -0,0 +1,11 @@
package org.eu.spacc.spaccdotweb.android;
import org.eu.spacc.spaccdotweb.android.Constants.*;
public class Config {
public static final Boolean ALLOW_JAVASCRIPT = true;
public static final Boolean ALLOW_STORAGE = true;
public static final AppIndex APP_INDEX = AppIndex.LOCAL;
public static final String LOCAL_INDEX = "index.html";
public static final String REMOTE_INDEX = "https://example.com";
}

View File

@@ -0,0 +1,5 @@
package org.eu.spacc.spaccdotweb.android;
public class Constants {
public static enum AppIndex { LOCAL, REMOTE }
}

View File

@@ -0,0 +1,42 @@
package org.eu.spacc.spaccdotweb.android;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class SpaccWebView extends WebView {
@SuppressLint("SetJavaScriptEnabled")
public SpaccWebView(Context context, AttributeSet attrs) {
super(context, attrs);
WebSettings webSettings = getSettings();
webSettings.setJavaScriptEnabled(Config.ALLOW_JAVASCRIPT);
ApiUtils.apiRun(7, () -> webSettings.setDomStorageEnabled(Config.ALLOW_STORAGE));
ApiUtils.apiRun(5, () -> webSettings.setDatabaseEnabled(Config.ALLOW_STORAGE));
if (Config.ALLOW_STORAGE) {
ApiUtils.apiRun(5, () -> webSettings.setDatabasePath(context.getFilesDir().getParent() + "/databases"));
}
ApiUtils.apiRun(3, () -> webSettings.setAllowFileAccess(true));
}
public void loadAppIndex() {
String url = null;
switch (Config.APP_INDEX) {
case LOCAL:
url = ("file:///android_asset/" + Config.LOCAL_INDEX);
break;
case REMOTE:
url = Config.REMOTE_INDEX;
break;
}
loadUrl(url);
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<org.eu.spacc.spaccdotweb.android.SpaccWebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
</android.widget.LinearLayout>

View File

@@ -0,0 +1,3 @@
<resources>
<string name="app_name">SpaccWebView Application</string>
</resources>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<include domain="sharedpref" path="."/>
</full-backup-content>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<data-extraction-rules>
<cloud-backup disableIfNoEncryptionCapabilities="true">
<include domain="sharedpref" path="."/>
</cloud-backup>
<device-transfer>
<include domain="sharedpref" path="."/>
</device-transfer>
</data-extraction-rules>