diff --git a/SpaccDotWeb.Android/app/src/main/java/com/example/spaccwebviewapplication/MainActivity.java b/SpaccDotWeb.Android/app/src/main/java/com/example/spaccwebviewapplication/MainActivity.java index 311755c..9949ef4 100644 --- a/SpaccDotWeb.Android/app/src/main/java/com/example/spaccwebviewapplication/MainActivity.java +++ b/SpaccDotWeb.Android/app/src/main/java/com/example/spaccwebviewapplication/MainActivity.java @@ -1,7 +1,6 @@ package com.example.spaccwebviewapplication; import android.os.Bundle; - import org.eu.spacc.spaccdotweb.android.helpers.DataMoveHelper; import org.eu.spacc.spaccdotweb.android.SpaccWebViewActivity; @@ -15,6 +14,7 @@ public class MainActivity extends SpaccWebViewActivity { DataMoveHelper.run(this, R.string.exit, R.string.move_app_data, R.string.move_app_data_info); this.webView = findViewById(R.id.webview); + this.webView.loadConfig(this, R.xml.app_config); this.webView.loadAppIndex(); } } diff --git a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/Config.java b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/Config.java index 6471e27..6a6dd74 100644 --- a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/Config.java +++ b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/Config.java @@ -1,11 +1,54 @@ package org.eu.spacc.spaccdotweb.android; import org.eu.spacc.spaccdotweb.android.Constants.*; +import org.eu.spacc.spaccdotweb.android.helpers.ConfigReader; -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"; +public class Config extends Defaults { + private ConfigReader configReader; + + public Config() {} + + public Config(ConfigReader configReader) { + this.configReader = configReader; + } + + public Boolean getAllowJavascript() { + Boolean value = getBoolean("allow_javascript"); + return (value != null ? value : Defaults.ALLOW_JAVASCRIPT); + } + + public Boolean getAllowStorage() { + Boolean value = getBoolean("allow_storage"); + return (value != null ? value : Defaults.ALLOW_STORAGE); + } + + public AppIndex getAppIndex() { + AppIndex value = (AppIndex)get("app_index"); + return (value != null ? value : Defaults.APP_INDEX); + } + + public String getLocalIndex() { + String value = getString("local_index"); + return (value != null ? value : Defaults.LOCAL_INDEX); + } + + public String getRemoteIndex() { + return getString("remote_index"); + } + + private Object get(String key) { + if (configReader != null) { + return configReader.get(key); + } else { + return null; + } + } + + private Boolean getBoolean(String key) { + return (Boolean)get(key); + } + + private String getString(String key) { + return (String)get(key); + } } diff --git a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/Defaults.java b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/Defaults.java new file mode 100644 index 0000000..b9ef235 --- /dev/null +++ b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/Defaults.java @@ -0,0 +1,10 @@ +package org.eu.spacc.spaccdotweb.android; + +import org.eu.spacc.spaccdotweb.android.Constants.*; + +public class Defaults { + 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"; +} diff --git a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/helpers/ConfigReader.java b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/helpers/ConfigReader.java new file mode 100644 index 0000000..adfdd88 --- /dev/null +++ b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/helpers/ConfigReader.java @@ -0,0 +1,59 @@ +package org.eu.spacc.spaccdotweb.android.helpers; + +import android.content.Context; +import android.content.res.XmlResourceParser; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import org.eu.spacc.spaccdotweb.android.Constants.*; +import org.xmlpull.v1.XmlPullParserException; + +public class ConfigReader { + private final Map configData = new HashMap<>(); + + public ConfigReader(Context context, int configResource) { + try { + parseConfigData(context, configResource); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public Object get(String key) { + return configData.get(key); + } + + private void parseConfigData(Context context, int configResource) throws IOException, XmlPullParserException { + XmlResourceParser parser = context.getResources().getXml(configResource); + int eventType = parser.getEventType(); + while (eventType != XmlResourceParser.END_DOCUMENT) { + if (eventType == XmlResourceParser.START_TAG) { + String type = parser.getName(); + if (!type.equals("config")) { + String name = parser.getAttributeValue(null, "name"); + String value = parser.nextText(); + configData.put(name, parseValue(type, value)); + } + } + eventType = parser.next(); + } + } + + private Object parseValue(String type, String value) { + switch (type) { + case "boolean": + return Boolean.parseBoolean(value); + case "string": + return value; + default: + value = value.toUpperCase(); + try { + switch (type) { + case "AppIndex": + return AppIndex.valueOf(value); + } + } catch (IllegalArgumentException ignored) {} + } + return null; + } +} diff --git a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/helpers/DataMoveHelper.java b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/helpers/DataMoveHelper.java index f3a1a48..2780f55 100644 --- a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/helpers/DataMoveHelper.java +++ b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/helpers/DataMoveHelper.java @@ -6,11 +6,9 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Build; - import org.eu.spacc.spaccdotweb.android.Constants; import org.eu.spacc.spaccdotweb.android.utils.StorageUtils; import org.eu.spacc.spaccdotweb.android.utils.FileUtils; - import java.io.IOException; public class DataMoveHelper { diff --git a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/utils/FileUtils.java b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/utils/FileUtils.java index 3b8c7fe..e89508e 100644 --- a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/utils/FileUtils.java +++ b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/utils/FileUtils.java @@ -3,7 +3,6 @@ package org.eu.spacc.spaccdotweb.android.utils; import static android.content.Context.DOWNLOAD_SERVICE; import android.app.DownloadManager; import android.content.Context; -import android.content.Intent; import android.net.Uri; import android.os.Build; import android.webkit.CookieManager; diff --git a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/webview/SpaccWebView.java b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/webview/SpaccWebView.java index c5cfa25..7e9b5ca 100644 --- a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/webview/SpaccWebView.java +++ b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/webview/SpaccWebView.java @@ -3,14 +3,14 @@ package org.eu.spacc.spaccdotweb.android.webview; import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; -import android.view.ContextMenu; import android.webkit.WebSettings; import android.webkit.WebView; - -import org.eu.spacc.spaccdotweb.android.utils.ApiUtils; import org.eu.spacc.spaccdotweb.android.Config; +import org.eu.spacc.spaccdotweb.android.helpers.ConfigReader; +import org.eu.spacc.spaccdotweb.android.utils.ApiUtils; public class SpaccWebView extends WebView { + private Config config; @SuppressLint("SetJavaScriptEnabled") public SpaccWebView(Context context, AttributeSet attrs) { @@ -18,18 +18,8 @@ public class SpaccWebView extends WebView { this.setWebViewClient(new WebViewClient(context)); this.setWebChromeClient(new WebChromeClient(context)); this.setDownloadListener(new DownloadListener(context)); - - WebSettings webSettings = this.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.getDir("databases", 0).getAbsolutePath())); - } - - ApiUtils.apiRun(3, () -> webSettings.setAllowFileAccess(false)); + this.config = new Config(); + this.applyConfig(context); } // TODO: Implement context menu (long-press on links, images, etc...) @@ -38,16 +28,36 @@ public class SpaccWebView extends WebView { // super.onCreateContextMenu(menu); // } + private void applyConfig(Context context) { + WebSettings webSettings = this.getSettings(); + + webSettings.setJavaScriptEnabled(config.getAllowJavascript()); + + boolean allowStorage = config.getAllowStorage(); + ApiUtils.apiRun(7, () -> webSettings.setDomStorageEnabled(allowStorage)); + ApiUtils.apiRun(5, () -> webSettings.setDatabaseEnabled(allowStorage)); + if (allowStorage) { + ApiUtils.apiRun(5, () -> webSettings.setDatabasePath(context.getDir("databases", 0).getAbsolutePath())); + } + + ApiUtils.apiRun(3, () -> webSettings.setAllowFileAccess(false)); + } + + public void loadConfig(Context context, int configResource) { + this.config = new Config(new ConfigReader(context, configResource)); + this.applyConfig(context); + } + public void loadAppIndex() { String url = null; - switch (Config.APP_INDEX) { + switch (config.getAppIndex()) { case LOCAL: - url = ("file:///android_asset/" + Config.LOCAL_INDEX); + url = ("file:///android_asset/" + config.getLocalIndex()); break; case REMOTE: - url = Config.REMOTE_INDEX; + url = config.getRemoteIndex(); break; } - loadUrl(url); + this.loadUrl(url); } } diff --git a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/webview/WebViewClient.java b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/webview/WebViewClient.java index 10cb877..9603e90 100644 --- a/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/webview/WebViewClient.java +++ b/SpaccDotWeb.Android/app/src/main/java/org/eu/spacc/spaccdotweb/android/webview/WebViewClient.java @@ -1,13 +1,9 @@ package org.eu.spacc.spaccdotweb.android.webview; -import android.content.ActivityNotFoundException; import android.content.Context; -import android.content.Intent; import android.net.Uri; import android.webkit.WebView; - import org.eu.spacc.spaccdotweb.android.utils.ApiUtils; - import java.util.Arrays; import java.util.List; diff --git a/SpaccDotWeb.Android/app/src/main/res/xml/app_config.xml b/SpaccDotWeb.Android/app/src/main/res/xml/app_config.xml new file mode 100644 index 0000000..4d9068f --- /dev/null +++ b/SpaccDotWeb.Android/app/src/main/res/xml/app_config.xml @@ -0,0 +1,9 @@ + + + true + true + + local + index.html + https://example.com + \ No newline at end of file