Skip to content

Commit

Permalink
feat(android): expose CapConfig.loadDefault(), deprecate v2 construct…
Browse files Browse the repository at this point in the history
…or (#3964)
  • Loading branch information
carlpoole authored Dec 17, 2020
1 parent 1a1b702 commit 94ae977
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
45 changes: 41 additions & 4 deletions android/capacitor/src/main/java/com/getcapacitor/CapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -324,6 +346,8 @@ private static Map<String, PluginConfig> deserializePluginsConfig(JSONObject plu
*/
public static class Builder {

private Context context;

// Server Config Values
private boolean html5mode = true;
private String serverUrl;
Expand All @@ -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<String, PluginConfig> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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")
Expand Down

0 comments on commit 94ae977

Please sign in to comment.