Skip to content

Commit

Permalink
feat(android): move Config to be per-instance rather than a singleton (
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Jun 8, 2020
1 parent 7adde78 commit b4815a5
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 151 deletions.
31 changes: 18 additions & 13 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class Bridge {
public static final String CAPACITOR_CONTENT_START = "/_capacitor_content_";

// Loaded Capacitor config
private JSONObject config = new JSONObject();
private Config config;

// A reference to the main activity for the app
private final Activity context;
Expand Down Expand Up @@ -136,7 +136,7 @@ public class Bridge {
* @param context
* @param webView
*/
public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>> initialPlugins, CordovaInterfaceImpl cordovaInterface, PluginManager pluginManager, CordovaPreferences preferences) {
public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>> initialPlugins, CordovaInterfaceImpl cordovaInterface, PluginManager pluginManager, CordovaPreferences preferences, JSONObject config) {
this.context = context;
this.webView = webView;
this.webViewClient = new BridgeWebViewClient(this);
Expand All @@ -148,7 +148,8 @@ public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>> i
handlerThread.start();
taskHandler = new Handler(handlerThread.getLooper());

Config.load(getActivity());
this.config = new Config(getActivity().getAssets(), config);
Logger.init(this.config);

// Initialize web view and message handler for it
this.initWebView();
Expand All @@ -166,16 +167,16 @@ public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>> i
}

private void loadWebView() {
appUrlConfig = Config.getString("server.url");
String[] appAllowNavigationConfig = Config.getArray("server.allowNavigation");
appUrlConfig = this.config.getString("server.url");
String[] appAllowNavigationConfig = this.config.getArray("server.allowNavigation");

ArrayList<String> authorities = new ArrayList<String>();
if (appAllowNavigationConfig != null) {
authorities.addAll(Arrays.asList(appAllowNavigationConfig));
}
this.appAllowNavigationMask = HostMask.Parser.parse(appAllowNavigationConfig);

String authority = Config.getString("server.hostname", "localhost");
String authority = this.config.getString("server.hostname", "localhost");
authorities.add(authority);

String scheme = this.getScheme();
Expand All @@ -201,7 +202,7 @@ private void loadWebView() {
}
}

final boolean html5mode = Config.getBoolean("server.html5mode", true);
final boolean html5mode = this.config.getBoolean("server.html5mode", true);

// Start the local web server
localServer = new WebViewLocalServer(context, this, getJSInjector(), authorities, html5mode);
Expand Down Expand Up @@ -331,7 +332,11 @@ public Uri getIntentUri() {
* @return
*/
public String getScheme() {
return Config.getString("server.androidScheme", CAPACITOR_HTTP_SCHEME);
return this.config.getString("server.androidScheme", CAPACITOR_HTTP_SCHEME);
}

public Config getConfig() {
return this.config;
}

public void reset() {
Expand All @@ -351,21 +356,21 @@ private void initWebView() {
settings.setAppCacheEnabled(true);
settings.setMediaPlaybackRequiresUserGesture(false);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
if (Config.getBoolean("android.allowMixedContent", false)) {
if (this.config.getBoolean("android.allowMixedContent", false)) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}

String appendUserAgent = Config.getString("android.appendUserAgent" , Config.getString("appendUserAgent", null));
String appendUserAgent = this.config.getString("android.appendUserAgent" , this.config.getString("appendUserAgent", null));
if (appendUserAgent != null) {
String defaultUserAgent = settings.getUserAgentString();
settings.setUserAgentString(defaultUserAgent + " " + appendUserAgent);
}
String overrideUserAgent = Config.getString("android.overrideUserAgent" , Config.getString("overrideUserAgent", null));
String overrideUserAgent = this.config.getString("android.overrideUserAgent" , this.config.getString("overrideUserAgent", null));
if (overrideUserAgent != null) {
settings.setUserAgentString(overrideUserAgent);
}

String backgroundColor = Config.getString("android.backgroundColor" , Config.getString("backgroundColor", null));
String backgroundColor = this.config.getString("android.backgroundColor" , this.config.getString("backgroundColor", null));
try {
if (backgroundColor != null) {
webView.setBackgroundColor(Color.parseColor(backgroundColor));
Expand All @@ -378,7 +383,7 @@ private void initWebView() {
defaultDebuggable = true;
}
webView.requestFocusFromTouch();
WebView.setWebContentsDebuggingEnabled(Config.getBoolean("android.webContentsDebuggingEnabled", defaultDebuggable));
WebView.setWebContentsDebuggingEnabled(this.config.getBoolean("android.webContentsDebuggingEnabled", defaultDebuggable));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.cordova.CordovaPreferences;
import org.apache.cordova.PluginEntry;
import org.apache.cordova.PluginManager;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -30,6 +31,7 @@ public class BridgeActivity extends AppCompatActivity {
private PluginManager pluginManager;
private CordovaPreferences preferences;
private MockCordovaWebViewImpl mockWebView;
private JSONObject config;

private int activityDepth = 0;

Expand All @@ -43,8 +45,11 @@ protected void onCreate(Bundle savedInstanceState) {
}

protected void init(Bundle savedInstanceState, List<Class<? extends Plugin>> plugins) {
this.init(savedInstanceState, plugins, null);
}
protected void init(Bundle savedInstanceState, List<Class<? extends Plugin>> plugins, JSONObject config) {
this.initialPlugins = plugins;

this.config = config;
loadConfig(this.getApplicationContext(),this);

getApplication().setTheme(getResources().getIdentifier("AppTheme_NoActionBar", "style", getPackageName()));
Expand Down Expand Up @@ -75,9 +80,9 @@ protected void load(Bundle savedInstanceState) {

pluginManager = mockWebView.getPluginManager();
cordovaInterface.onCordovaInit(pluginManager);
bridge = new Bridge(this, webView, initialPlugins, cordovaInterface, pluginManager, preferences);
bridge = new Bridge(this, webView, initialPlugins, cordovaInterface, pluginManager, preferences, this.config);

Splash.showOnLaunch(this);
Splash.showOnLaunch(this, bridge.getConfig());

if (savedInstanceState != null) {
bridge.restoreInstanceState(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.cordova.CordovaPreferences;
import org.apache.cordova.PluginEntry;
import org.apache.cordova.PluginManager;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class BridgeFragment extends Fragment {
private String lastActivityPlugin;

private List<Class<? extends Plugin>> initialPlugins = new ArrayList<>();

private JSONObject config = new JSONObject();

public BridgeFragment() {
// Required empty public constructor
Expand Down Expand Up @@ -108,7 +109,7 @@ protected void load(Bundle savedInstanceState) {
preferences = new CordovaPreferences();
}

bridge = new Bridge(this.getActivity(), webView, initialPlugins, cordovaInterface, pluginManager, preferences);
bridge = new Bridge(this.getActivity(), webView, initialPlugins, cordovaInterface, pluginManager, preferences, config);

if (startDir != null) {
bridge.setServerAssetPath(startDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public CapacitorWebView(Context context, AttributeSet attrs) {

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
boolean captureInput = Config.getBoolean("android.captureInput", false);
Config config = new Config(getContext().getAssets(), null);
boolean captureInput = config.getBoolean("android.captureInput", false);
if (captureInput) {
if (capInputConnection == null) {
capInputConnection = new BaseInputConnection(this, false);
Expand Down
48 changes: 22 additions & 26 deletions android/capacitor/src/main/java/com/getcapacitor/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.getcapacitor;

import android.app.Activity;
import android.content.res.AssetManager;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -17,24 +18,19 @@ public class Config {

private JSONObject config = new JSONObject();

private static Config instance;

private static Config getInstance() {
if (instance == null) {
instance = new Config();
public Config(AssetManager assetManager, JSONObject config) {
if (config != null) {
this.config = config;
} else {
// Load our capacitor.config.json
this.loadConfig(assetManager);
}
return instance;
}

// Load our capacitor.config.json
public static void load(Activity activity) {
Config.getInstance().loadConfig(activity);
}

private void loadConfig(Activity activity) {
private void loadConfig(AssetManager assetManager) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(activity.getAssets().open("capacitor.config.json")));
reader = new BufferedReader(new InputStreamReader(assetManager.open("capacitor.config.json")));

// do reading, usually loop until end of file reading
StringBuilder b = new StringBuilder();
Expand All @@ -60,9 +56,9 @@ private void loadConfig(Activity activity) {
}
}

public static JSONObject getObject(String key) {
public JSONObject getObject(String key) {
try {
return getInstance().config.getJSONObject(key);
return this.config.getJSONObject(key);
} catch (Exception ex) {
}
return null;
Expand All @@ -81,14 +77,14 @@ private JSONObject getConfigObjectDeepest(String key) throws JSONException {
return o;
}

public static String getString(String key) {
public String getString(String key) {
return getString(key, null);
}

public static String getString(String key, String defaultValue) {
public String getString(String key, String defaultValue) {
String k = getConfigKey(key);
try {
JSONObject o = getInstance().getConfigObjectDeepest(key);
JSONObject o = this.getConfigObjectDeepest(key);

String value = o.getString(k);
if (value == null) {
Expand All @@ -99,43 +95,43 @@ public static String getString(String key, String defaultValue) {
return defaultValue;
}

public static boolean getBoolean(String key, boolean defaultValue) {
public boolean getBoolean(String key, boolean defaultValue) {
String k = getConfigKey(key);
try {
JSONObject o = getInstance().getConfigObjectDeepest(key);
JSONObject o = this.getConfigObjectDeepest(key);

return o.getBoolean(k);
} catch (Exception ex) {}
return defaultValue;
}

public static int getInt(String key, int defaultValue) {
public int getInt(String key, int defaultValue) {
String k = getConfigKey(key);
try {
JSONObject o = getInstance().getConfigObjectDeepest(key);
JSONObject o = this.getConfigObjectDeepest(key);
return o.getInt(k);
} catch (Exception ignore) {
// value was not found
}
return defaultValue;
}

private static String getConfigKey(String key) {
private String getConfigKey(String key) {
String[] parts = key.split("\\.");
if (parts.length > 0) {
return parts[parts.length - 1];
}
return null;
}

public static String[] getArray(String key) {
public String[] getArray(String key) {
return getArray(key, null);
}

public static String[] getArray(String key, String[] defaultValue) {
public String[] getArray(String key, String[] defaultValue) {
String k = getConfigKey(key);
try {
JSONObject o = getInstance().getConfigObjectDeepest(key);
JSONObject o = this.getConfigObjectDeepest(key);

JSONArray a = o.getJSONArray(k);
if (a == null) {
Expand Down
20 changes: 19 additions & 1 deletion android/capacitor/src/main/java/com/getcapacitor/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

public class Logger {
public static final String LOG_TAG_CORE = "Capacitor";
public static Config config;

private static Logger instance;

private static Logger getInstance() {
if (instance == null) {
instance = new Logger();
}
return instance;
}

public static void init(Config config) {
Logger.getInstance().loadConfig(config);
}

private void loadConfig(Config config) {
this.config = config;
}

public static String tags(String... subtags) {
if (subtags != null && subtags.length > 0) {
Expand Down Expand Up @@ -79,6 +97,6 @@ public static void error(String tag, String message, Throwable e) {
}

protected static boolean shouldLog() {
return !Config.getBoolean("android.hideLogs", Config.getBoolean("hideLogs", false));
return config == null || !config.getBoolean("android.hideLogs", config.getBoolean("hideLogs", false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public PluginCall getSavedCall() {

public Object getConfigValue(String key) {
try {
JSONObject plugins = Config.getObject("plugins");
JSONObject plugins = bridge.getConfig().getObject("plugins");
if (plugins == null) {
return null;
}
Expand Down
Loading

0 comments on commit b4815a5

Please sign in to comment.