diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 546810e5ee..fcc562331c 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -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; @@ -148,7 +148,8 @@ public Bridge(Activity context, WebView webView, List> 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 @@ -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; } diff --git a/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java b/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java new file mode 100644 index 0000000000..da81081573 --- /dev/null +++ b/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java @@ -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 0) { return parts[parts.length - 1]; @@ -124,14 +148,20 @@ private String getConfigKey(String key) { 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) { @@ -149,4 +179,4 @@ public String[] getArray(String key, String[] defaultValue) { } catch (Exception ex) {} return defaultValue; } -} +} \ No newline at end of file diff --git a/android/capacitor/src/main/java/com/getcapacitor/Logger.java b/android/capacitor/src/main/java/com/getcapacitor/Logger.java index e380deedcd..96d8817a06 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Logger.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Logger.java @@ -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; @@ -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; } diff --git a/android/capacitor/src/main/java/com/getcapacitor/Splash.java b/android/capacitor/src/main/java/com/getcapacitor/Splash.java index 5838a8f155..587e60244b 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Splash.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Splash.java @@ -42,7 +42,7 @@ public interface SplashListener { private static boolean isVisible = false; private static boolean isHiding = false; - private static void buildViews(Context c, Config config) { + private static void buildViews(Context c, CapConfig config) { if (splashImage == null) { String splashResourceName = config.getString(CONFIG_KEY_PREFIX + "androidSplashResourceName", "splash"); @@ -165,7 +165,7 @@ private static void buildViews(Context c, Config config) { * Show the splash screen on launch without fading in * @param a */ - public static void showOnLaunch(final BridgeActivity a, Config config) { + public static void showOnLaunch(final BridgeActivity a, CapConfig config) { Integer duration = config.getInt(CONFIG_KEY_PREFIX + "launchShowDuration", DEFAULT_LAUNCH_SHOW_DURATION); Boolean autohide = config.getBoolean(CONFIG_KEY_PREFIX + "launchAutoHide", DEFAULT_AUTO_HIDE); @@ -193,7 +193,7 @@ public static void show(final Activity a, final int fadeOutDuration, final boolean autoHide, final SplashListener splashListener, - final Config config) { + final CapConfig config) { show(a, showDuration, fadeInDuration, fadeOutDuration, autoHide, splashListener, false, config); } @@ -214,7 +214,7 @@ public static void show(final Activity a, final boolean autoHide, final SplashListener splashListener, final boolean isLaunchSplash, - final Config config) { + final CapConfig config) { wm = (WindowManager)a.getSystemService(Context.WINDOW_SERVICE); if (a.isFinishing()) { diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java index 93224dcc4e..d6cd583c5c 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotificationManager.java @@ -20,6 +20,7 @@ import androidx.core.app.NotificationManagerCompat; import androidx.core.app.RemoteInput; +import com.getcapacitor.CapConfig; import com.getcapacitor.Config; import com.getcapacitor.JSObject; import com.getcapacitor.Logger; @@ -56,9 +57,9 @@ public class LocalNotificationManager { private Context context; private Activity activity; private NotificationStorage storage; - private Config config; + private CapConfig config; - public LocalNotificationManager(NotificationStorage notificationStorage, Activity activity, Context context, Config config) { + public LocalNotificationManager(NotificationStorage notificationStorage, Activity activity, Context context, CapConfig config) { storage = notificationStorage; this.activity = activity; this.context = context;