Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert static Config class #3126

Merged
merged 2 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 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 Config config;
private CapConfig config;

// A reference to the main activity for the app
private final Activity context;
Expand Down Expand Up @@ -148,7 +148,8 @@ public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>> i
handlerThread.start();
taskHandler = new Handler(handlerThread.getLooper());

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

// Initialize web view and message handler for it
Expand Down Expand Up @@ -335,7 +336,7 @@ public String getScheme() {
return this.config.getString("server.androidScheme", CAPACITOR_HTTP_SCHEME);
}

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

Expand Down
151 changes: 151 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/CapConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.getcapacitor;

import android.content.res.AssetManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* Management interface for accessing values in capacitor.config.json
*/
public class CapConfig {

private JSONObject config = new JSONObject();

public CapConfig(AssetManager assetManager, JSONObject config) {
if (config != null) {
this.config = config;
} else {
// Load our capacitor.config.json
this.loadConfig(assetManager);
}
}

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

// do reading, usually loop until end of file reading
StringBuilder b = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
//process line
b.append(line);
}

String jsonString = b.toString();
this.config = new JSONObject(jsonString);
} catch (IOException ex) {
Logger.error("Unable to load capacitor.config.json. Run npx cap copy first", ex);
} catch (JSONException ex) {
Logger.error("Unable to parse capacitor.config.json. Make sure it's valid json", ex);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}

public JSONObject getObject(String key) {
try {
return this.config.getJSONObject(key);
} catch (Exception ex) {
}
return null;
}

private JSONObject getConfigObjectDeepest(String key) throws JSONException {
// Split on periods
String[] parts = key.split("\\.");

JSONObject o = this.config;
// Search until the second to last part of the key
for (int i = 0; i < parts.length-1; i++) {
String k = parts[i];
o = o.getJSONObject(k);
}
return o;
}

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

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

String value = o.getString(k);
if (value == null) {
return defaultValue;
}
return value;
} catch (Exception ex) {}
return defaultValue;
}

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

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

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

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

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

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

JSONArray a = o.getJSONArray(k);
if (a == null) {
return defaultValue;
}

int l = a.length();
String[] value = new String[l];

for(int i=0; i<l; i++) {
value[i] = (String) a.get(i);
}

return value;
} catch (Exception ex) {}
return defaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public CapacitorWebView(Context context, AttributeSet attrs) {

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

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

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -12,25 +11,32 @@
import java.io.InputStreamReader;

/**
* Management interface for accessing values in capacitor.config.json
* @deprecated use getBridge().getConfig() instead of the static Config
*/
public class Config {

private JSONObject config = new JSONObject();

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

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

/**
* @deprecated
*/
public static void load(Activity activity) {
Config.getInstance().loadConfig(activity);
}

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

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

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

public String getString(String key) {
/**
* @deprecated
*/
public static String getString(String key) {
return getString(key, null);
}

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

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

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

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

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

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

public String[] getArray(String key) {
/**
* @deprecated
*/
public static String[] getArray(String key) {
return getArray(key, null);
}

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

JSONArray a = o.getJSONArray(k);
if (a == null) {
Expand All @@ -149,4 +179,4 @@ public String[] getArray(String key, String[] defaultValue) {
} catch (Exception ex) {}
return defaultValue;
}
}
}
6 changes: 3 additions & 3 deletions android/capacitor/src/main/java/com/getcapacitor/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

private static Logger instance;

Expand All @@ -16,11 +16,11 @@ private static Logger getInstance() {
return instance;
}

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

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

Expand Down
Loading