From 94ae9774d2467fa7ba0336e7183f6d28cae45908 Mon Sep 17 00:00:00 2001 From: Carl Poole Date: Wed, 16 Dec 2020 18:44:11 -0600 Subject: [PATCH] feat(android): expose CapConfig.loadDefault(), deprecate v2 constructor (#3964) --- .../main/java/com/getcapacitor/CapConfig.java | 45 +++++++++++++++++-- .../com/getcapacitor/ConfigBuildingTest.java | 6 ++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java b/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java index 862ff9ae5..1c786cdcd 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java +++ b/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java @@ -6,6 +6,7 @@ import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.res.AssetManager; +import androidx.annotation.Nullable; import com.getcapacitor.util.JSONUtils; import java.io.IOException; import java.util.HashMap; @@ -46,13 +47,34 @@ public class CapConfig { */ private CapConfig() {} + /** + * Get an instance of the Config file object. + * @deprecated use {@link #loadDefault(Context)} to load an instance of the Config object + * from the capacitor.config.json file, or use the {@link CapConfig.Builder} to construct + * a CapConfig for embedded use. + * + * @param assetManager The AssetManager used to load the config file + * @param config JSON describing a configuration to use + */ + @Deprecated + public CapConfig(AssetManager assetManager, JSONObject config) { + if (config != null) { + this.configJSON = config; + } else { + // Load the capacitor.config.json + loadConfig(assetManager); + } + + deserializeConfig(null); + } + /** * Constructs a Capacitor Configuration from config.json file. * * @param context The context. * @return A loaded config file, if successful. */ - static CapConfig loadDefault(Context context) { + public static CapConfig loadDefault(Context context) { CapConfig config = new CapConfig(); if (context == null) { @@ -108,7 +130,7 @@ private void loadConfig(AssetManager assetManager) { /** * Deserializes the config from JSON into a Capacitor Configuration object. */ - private void deserializeConfig(Context context) { + private void deserializeConfig(@Nullable Context context) { // Server html5mode = JSONUtils.getBoolean(configJSON, "server.html5mode", html5mode); serverUrl = JSONUtils.getString(configJSON, "server.url", null); @@ -131,7 +153,7 @@ private void deserializeConfig(Context context) { ); captureInput = JSONUtils.getBoolean(configJSON, "android.captureInput", captureInput); hideLogs = JSONUtils.getBoolean(configJSON, "android.hideLogs", JSONUtils.getBoolean(configJSON, "hideLogs", hideLogs)); - webContentsDebuggingEnabled = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; + webContentsDebuggingEnabled = context != null && (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; webContentsDebuggingEnabled = JSONUtils.getBoolean(configJSON, "android.webContentsDebuggingEnabled", webContentsDebuggingEnabled); // Plugins @@ -324,6 +346,8 @@ private static Map deserializePluginsConfig(JSONObject plu */ public static class Builder { + private Context context; + // Server Config Values private boolean html5mode = true; private String serverUrl; @@ -337,18 +361,31 @@ public static class Builder { private String backgroundColor; private boolean allowMixedContent = false; private boolean captureInput = false; - private boolean webContentsDebuggingEnabled = false; + private Boolean webContentsDebuggingEnabled = null; private boolean hideLogs = false; // Plugins Config Object private Map pluginsConfiguration = new HashMap<>(); + /** + * Constructs a new CapConfig Builder. + * + * @param context The context + */ + public Builder(Context context) { + this.context = context; + } + /** * Builds a Capacitor Config from the builder. * * @return A new Capacitor Config */ public CapConfig create() { + if (webContentsDebuggingEnabled == null) { + webContentsDebuggingEnabled = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; + } + return new CapConfig(this); } diff --git a/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java b/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java index e6579549e..d465c3619 100644 --- a/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java +++ b/android/capacitor/src/test/java/com/getcapacitor/ConfigBuildingTest.java @@ -2,15 +2,19 @@ import static org.junit.Assert.*; +import android.app.Activity; import org.json.JSONArray; import org.json.JSONObject; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; public class ConfigBuildingTest { final String TEST_PLUGIN_NAME = "TestPlugin"; + Activity context = Mockito.mock(Activity.class); + JSONObject pluginConfig = new JSONObject(); JSONObject testPluginObject = new JSONObject(); JSONObject testPluginNestedObject = new JSONObject(); @@ -37,7 +41,7 @@ public void setup() { pluginConfig.put(TEST_PLUGIN_NAME, testPluginObject); config = - new CapConfig.Builder() + new CapConfig.Builder(context) .setAllowMixedContent(true) .setAllowNavigation(new String[] { "http://www.google.com" }) .setAndroidScheme("test")