diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 82c1b0474d..546810e5ee 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 JSONObject config = new JSONObject(); + private Config config; // A reference to the main activity for the app private final Activity context; @@ -136,7 +136,7 @@ public class Bridge { * @param context * @param webView */ - public Bridge(Activity context, WebView webView, List> initialPlugins, CordovaInterfaceImpl cordovaInterface, PluginManager pluginManager, CordovaPreferences preferences) { + public Bridge(Activity context, WebView webView, List> initialPlugins, CordovaInterfaceImpl cordovaInterface, PluginManager pluginManager, CordovaPreferences preferences, JSONObject config) { this.context = context; this.webView = webView; this.webViewClient = new BridgeWebViewClient(this); @@ -148,7 +148,8 @@ public Bridge(Activity context, WebView webView, List> 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(); @@ -166,8 +167,8 @@ public Bridge(Activity context, WebView webView, List> 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 authorities = new ArrayList(); if (appAllowNavigationConfig != null) { @@ -175,7 +176,7 @@ private void loadWebView() { } 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(); @@ -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); @@ -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() { @@ -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)); @@ -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)); } /** diff --git a/android/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java b/android/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java index 41b2d97fb6..24541b02d1 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java +++ b/android/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java @@ -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; @@ -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; @@ -43,8 +45,11 @@ protected void onCreate(Bundle savedInstanceState) { } protected void init(Bundle savedInstanceState, List> plugins) { + this.init(savedInstanceState, plugins, null); + } + protected void init(Bundle savedInstanceState, List> plugins, JSONObject config) { this.initialPlugins = plugins; - + this.config = config; loadConfig(this.getApplicationContext(),this); getApplication().setTheme(getResources().getIdentifier("AppTheme_NoActionBar", "style", getPackageName())); @@ -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); diff --git a/android/capacitor/src/main/java/com/getcapacitor/BridgeFragment.java b/android/capacitor/src/main/java/com/getcapacitor/BridgeFragment.java index 2234dca6be..750cea8765 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/BridgeFragment.java +++ b/android/capacitor/src/main/java/com/getcapacitor/BridgeFragment.java @@ -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; @@ -54,7 +55,7 @@ public class BridgeFragment extends Fragment { private String lastActivityPlugin; private List> initialPlugins = new ArrayList<>(); - + private JSONObject config = new JSONObject(); public BridgeFragment() { // Required empty public constructor @@ -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); diff --git a/android/capacitor/src/main/java/com/getcapacitor/CapacitorWebView.java b/android/capacitor/src/main/java/com/getcapacitor/CapacitorWebView.java index c9a30b2276..0042cbfe4e 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/CapacitorWebView.java +++ b/android/capacitor/src/main/java/com/getcapacitor/CapacitorWebView.java @@ -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); diff --git a/android/capacitor/src/main/java/com/getcapacitor/Config.java b/android/capacitor/src/main/java/com/getcapacitor/Config.java index 4def24b9e6..d3ce6165d0 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Config.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Config.java @@ -1,6 +1,7 @@ package com.getcapacitor; import android.app.Activity; +import android.content.res.AssetManager; import org.json.JSONException; import org.json.JSONObject; @@ -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(); @@ -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; @@ -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) { @@ -99,20 +95,20 @@ 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 @@ -120,7 +116,7 @@ public static int getInt(String key, int defaultValue) { 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]; @@ -128,14 +124,14 @@ private static String getConfigKey(String key) { 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) { diff --git a/android/capacitor/src/main/java/com/getcapacitor/Logger.java b/android/capacitor/src/main/java/com/getcapacitor/Logger.java index 271c1b1d56..e380deedcd 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Logger.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Logger.java @@ -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) { @@ -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)); } } diff --git a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java index 9ea818fea9..eccc0a2979 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Plugin.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Plugin.java @@ -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; } diff --git a/android/capacitor/src/main/java/com/getcapacitor/Splash.java b/android/capacitor/src/main/java/com/getcapacitor/Splash.java index d3e5e4b1c3..5838a8f155 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Splash.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Splash.java @@ -42,9 +42,9 @@ public interface SplashListener { private static boolean isVisible = false; private static boolean isHiding = false; - private static void buildViews(Context c) { + private static void buildViews(Context c, Config config) { if (splashImage == null) { - String splashResourceName = Config.getString(CONFIG_KEY_PREFIX + "androidSplashResourceName", "splash"); + String splashResourceName = config.getString(CONFIG_KEY_PREFIX + "androidSplashResourceName", "splash"); int splashId = c.getResources().getIdentifier(splashResourceName, "drawable", c.getPackageName()); @@ -65,8 +65,8 @@ private static void buildViews(Context c) { splashImage.setFitsSystemWindows(true); // Enable immersive mode (hides status bar and navbar) during splash screen or hide status bar. - Boolean splashImmersive = Config.getBoolean(CONFIG_KEY_PREFIX + "splashImmersive", DEFAULT_SPLASH_IMMERSIVE); - Boolean splashFullScreen = Config.getBoolean(CONFIG_KEY_PREFIX + "splashFullScreen", DEFAULT_SPLASH_FULL_SCREEN); + Boolean splashImmersive = config.getBoolean(CONFIG_KEY_PREFIX + "splashImmersive", DEFAULT_SPLASH_IMMERSIVE); + Boolean splashFullScreen = config.getBoolean(CONFIG_KEY_PREFIX + "splashFullScreen", DEFAULT_SPLASH_FULL_SCREEN); if (splashImmersive) { final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION @@ -83,7 +83,7 @@ private static void buildViews(Context c) { // https://stackoverflow.com/a/21847579/32140 splashImage.setDrawingCacheEnabled(true); - String backgroundColor = Config.getString(CONFIG_KEY_PREFIX + "backgroundColor"); + String backgroundColor = config.getString(CONFIG_KEY_PREFIX + "backgroundColor"); try { if (backgroundColor != null) { splashImage.setBackgroundColor(Color.parseColor(backgroundColor)); @@ -92,7 +92,7 @@ private static void buildViews(Context c) { Logger.debug("Background color not applied"); } - String scaleTypeName = Config.getString(CONFIG_KEY_PREFIX + "androidScaleType", "FIT_XY"); + String scaleTypeName = config.getString(CONFIG_KEY_PREFIX + "androidScaleType", "FIT_XY"); ImageView.ScaleType scaleType = null; try { scaleType = ImageView.ScaleType.valueOf(scaleTypeName); @@ -105,7 +105,7 @@ private static void buildViews(Context c) { } if (spinnerBar == null) { - String spinnerStyle = Config.getString(CONFIG_KEY_PREFIX + "androidSpinnerStyle"); + String spinnerStyle = config.getString(CONFIG_KEY_PREFIX + "androidSpinnerStyle"); if (spinnerStyle != null) { int spinnerBarStyle = android.R.attr.progressBarStyleLarge; @@ -136,7 +136,7 @@ private static void buildViews(Context c) { } spinnerBar.setIndeterminate(true); - String spinnerColor = Config.getString(CONFIG_KEY_PREFIX + "spinnerColor"); + String spinnerColor = config.getString(CONFIG_KEY_PREFIX + "spinnerColor"); try { if (spinnerColor != null) { int[][] states = new int[][]{ @@ -165,15 +165,15 @@ private static void buildViews(Context c) { * Show the splash screen on launch without fading in * @param a */ - public static void showOnLaunch(final BridgeActivity a) { - Integer duration = Config.getInt(CONFIG_KEY_PREFIX + "launchShowDuration", DEFAULT_LAUNCH_SHOW_DURATION); - Boolean autohide = Config.getBoolean(CONFIG_KEY_PREFIX + "launchAutoHide", DEFAULT_AUTO_HIDE); + public static void showOnLaunch(final BridgeActivity a, Config config) { + Integer duration = config.getInt(CONFIG_KEY_PREFIX + "launchShowDuration", DEFAULT_LAUNCH_SHOW_DURATION); + Boolean autohide = config.getBoolean(CONFIG_KEY_PREFIX + "launchAutoHide", DEFAULT_AUTO_HIDE); if (duration == 0) { return; } - show(a, duration, 0, DEFAULT_FADE_OUT_DURATION, autohide, null, true); + show(a, duration, 0, DEFAULT_FADE_OUT_DURATION, autohide, null, true, config); } /** @@ -181,7 +181,7 @@ public static void showOnLaunch(final BridgeActivity a) { * @param a */ public static void show(final Activity a) { - show(a, DEFAULT_LAUNCH_SHOW_DURATION, DEFAULT_FADE_IN_DURATION, DEFAULT_FADE_OUT_DURATION, DEFAULT_AUTO_HIDE, null); + show(a, DEFAULT_LAUNCH_SHOW_DURATION, DEFAULT_FADE_IN_DURATION, DEFAULT_FADE_OUT_DURATION, DEFAULT_AUTO_HIDE, null, null); } /** @@ -192,8 +192,9 @@ public static void show(final Activity a, final int fadeInDuration, final int fadeOutDuration, final boolean autoHide, - final SplashListener splashListener) { - show(a, showDuration, fadeInDuration, fadeOutDuration, autoHide, splashListener, false); + final SplashListener splashListener, + final Config config) { + show(a, showDuration, fadeInDuration, fadeOutDuration, autoHide, splashListener, false, config); } /** @@ -212,14 +213,15 @@ public static void show(final Activity a, final int fadeOutDuration, final boolean autoHide, final SplashListener splashListener, - final boolean isLaunchSplash) { + final boolean isLaunchSplash, + final Config config) { wm = (WindowManager)a.getSystemService(Context.WINDOW_SERVICE); if (a.isFinishing()) { return; } - buildViews(a); + buildViews(a, config); if (isVisible) { return; @@ -284,7 +286,7 @@ public void run() { splashImage.setVisibility(View.VISIBLE); if (spinnerBar != null) { - Boolean showSpinner = Config.getBoolean(CONFIG_KEY_PREFIX + "showSpinner", false); + Boolean showSpinner = config.getBoolean(CONFIG_KEY_PREFIX + "showSpinner", false); spinnerBar.setVisibility(View.INVISIBLE); diff --git a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java index 285b9c5caa..9e1ff30fc2 100755 --- a/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +++ b/android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java @@ -172,7 +172,7 @@ public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) { return null; } - if (isLocalFile(loadingUrl) || (Config.getString("server.url") == null && !bridge.getAppAllowNavigationMask().matches(loadingUrl.getHost()))) { + if (isLocalFile(loadingUrl) || (bridge.getConfig().getString("server.url") == null && !bridge.getAppAllowNavigationMask().matches(loadingUrl.getHost()))) { Logger.debug("Handling local request: " + request.getUrl().toString()); return handleLocalRequest(request, handler); } else { diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java index e607c57c22..5520712822 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/LocalNotifications.java @@ -39,7 +39,7 @@ public LocalNotifications() { public void load() { super.load(); notificationStorage = new NotificationStorage(getContext()); - manager = new LocalNotificationManager(notificationStorage, getActivity(), getContext()); + manager = new LocalNotificationManager(notificationStorage, getActivity(), getContext(), this.bridge.getConfig()); manager.createNotificationChannel(); notificationChannelManager = new NotificationChannelManager(getActivity()); } diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/SplashScreen.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/SplashScreen.java index 6733f4293b..5a038e305c 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/SplashScreen.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/SplashScreen.java @@ -25,7 +25,7 @@ public void completed() { public void error() { call.error("An error occurred while showing splash"); } - }); + }, bridge.getConfig()); } @PluginMethod() diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java index 08409108a3..7f16e3794c 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/notification/LocalNotification.java @@ -9,6 +9,7 @@ import com.getcapacitor.JSObject; import com.getcapacitor.Logger; import com.getcapacitor.PluginCall; +import com.getcapacitor.plugin.util.AssetUtil; import org.json.JSONException; import org.json.JSONObject; @@ -23,11 +24,6 @@ */ public class LocalNotification { - private static final String CONFIG_KEY_PREFIX = "plugins.LocalNotifications."; - private static final int RESOURCE_ID_ZERO_VALUE = 0; - private static int defaultSmallIconID = RESOURCE_ID_ZERO_VALUE; - private static int defaultSoundID = RESOURCE_ID_ZERO_VALUE; - private String title; private String body; private Integer id; @@ -69,17 +65,17 @@ public void setSchedule(LocalNotificationSchedule schedule) { this.schedule = schedule; } - public String getSound(Context context) { + public String getSound(Context context, int defaultSound) { String soundPath = null; - int resId = RESOURCE_ID_ZERO_VALUE; - String name = getResourceBaseName(sound); + int resId = AssetUtil.RESOURCE_ID_ZERO_VALUE; + String name = AssetUtil.getResourceBaseName(sound); if (name != null) { - resId = getResourceID(context, name, "raw"); + resId = AssetUtil.getResourceID(context, name, "raw"); } - if (resId == RESOURCE_ID_ZERO_VALUE) { - resId = getDefaultSound(context); + if (resId == AssetUtil.RESOURCE_ID_ZERO_VALUE) { + resId = defaultSound; } - if(resId != RESOURCE_ID_ZERO_VALUE){ + if(resId != AssetUtil.RESOURCE_ID_ZERO_VALUE){ soundPath = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + resId; } return soundPath; @@ -89,15 +85,14 @@ public void setSound(String sound) { this.sound = sound; } - public void setSmallIcon(String smallIcon) { this.smallIcon = getResourceBaseName(smallIcon); } + public void setSmallIcon(String smallIcon) { this.smallIcon = AssetUtil.getResourceBaseName(smallIcon); } - public String getIconColor() { + public String getIconColor(String globalColor) { // use the one defined local before trying for a globally defined color if (iconColor != null) { return iconColor; } - - String globalColor = Config.getString(CONFIG_KEY_PREFIX + "iconColor"); + if (globalColor != null) { return globalColor; } @@ -249,61 +244,21 @@ public static JSObject buildLocalNotificationPendingList(List ids) { return result; } - public int getSmallIcon(Context context) { - int resId = RESOURCE_ID_ZERO_VALUE; + public int getSmallIcon(Context context, int defaultIcon) { + int resId = AssetUtil.RESOURCE_ID_ZERO_VALUE; if(smallIcon != null){ - resId = getResourceID(context, smallIcon,"drawable"); - } - - if(resId == RESOURCE_ID_ZERO_VALUE){ - resId = getDefaultSmallIcon(context); - } - - return resId; - } - - private static int getDefaultSmallIcon(Context context){ - if(defaultSmallIconID != RESOURCE_ID_ZERO_VALUE) return defaultSmallIconID; - - int resId = RESOURCE_ID_ZERO_VALUE; - String smallIconConfigResourceName = Config.getString(CONFIG_KEY_PREFIX + "smallIcon"); - smallIconConfigResourceName = getResourceBaseName(smallIconConfigResourceName); - - if(smallIconConfigResourceName != null){ - resId = getResourceID(context, smallIconConfigResourceName, "drawable"); + resId = AssetUtil.getResourceID(context, smallIcon,"drawable"); } - if(resId == RESOURCE_ID_ZERO_VALUE){ - resId = android.R.drawable.ic_dialog_info; + if(resId == AssetUtil.RESOURCE_ID_ZERO_VALUE){ + resId = defaultIcon; } - defaultSmallIconID = resId; return resId; } - private static int getDefaultSound(Context context){ - if(defaultSoundID != RESOURCE_ID_ZERO_VALUE) return defaultSoundID; - - int resId = RESOURCE_ID_ZERO_VALUE; - String soundConfigResourceName = Config.getString(CONFIG_KEY_PREFIX + "sound"); - soundConfigResourceName = getResourceBaseName(soundConfigResourceName); - - if(soundConfigResourceName != null){ - resId = getResourceID(context, soundConfigResourceName, "raw"); - } - - defaultSoundID = resId; - return resId; - } - public static Uri getDefaultSoundUrl(Context context){ - int soundId = LocalNotification.getDefaultSound(context); - if (soundId != RESOURCE_ID_ZERO_VALUE) { - return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + soundId); - } - return null; - } public boolean isScheduled() { return this.schedule != null && @@ -388,21 +343,4 @@ public void setSource(String source) { this.source = source; } - private static int getResourceID(Context context, String resourceName, String dir){ - return context.getResources().getIdentifier(resourceName, dir, context.getPackageName()); - } - - private static String getResourceBaseName (String resPath) { - if (resPath == null) return null; - - if (resPath.contains("/")) { - return resPath.substring(resPath.lastIndexOf('/') + 1); - } - - if (resPath.contains(".")) { - return resPath.substring(0, resPath.lastIndexOf('.')); - } - - return resPath; - } } 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 9536ef05f9..d5e16939be 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 @@ -5,6 +5,7 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.PendingIntent; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.graphics.Color; @@ -19,10 +20,13 @@ import androidx.core.app.NotificationManagerCompat; import androidx.core.app.RemoteInput; +import com.getcapacitor.Config; import com.getcapacitor.JSObject; import com.getcapacitor.Logger; import com.getcapacitor.PluginCall; import com.getcapacitor.android.R; +import com.getcapacitor.plugin.util.AssetUtil; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -36,6 +40,9 @@ */ public class LocalNotificationManager { + private static final String CONFIG_KEY_PREFIX = "plugins.LocalNotifications."; + private static int defaultSoundID = AssetUtil.RESOURCE_ID_ZERO_VALUE; + private static int defaultSmallIconID = AssetUtil.RESOURCE_ID_ZERO_VALUE; // Action constants public static final String NOTIFICATION_INTENT_KEY = "LocalNotificationId"; public static final String NOTIFICATION_OBJ_INTENT_KEY = "LocalNotficationObject"; @@ -49,11 +56,13 @@ public class LocalNotificationManager { private Context context; private Activity activity; private NotificationStorage storage; + private Config config; - public LocalNotificationManager(NotificationStorage notificationStorage, Activity activity, Context context ) { + public LocalNotificationManager(NotificationStorage notificationStorage, Activity activity, Context context, Config config) { storage = notificationStorage; this.activity = activity; this.context = context; + this.config = config; } /** @@ -109,7 +118,7 @@ public void createNotificationChannel() { AudioAttributes audioAttributes = new AudioAttributes.Builder() .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setUsage(AudioAttributes.USAGE_ALARM).build(); - Uri soundUri = LocalNotification.getDefaultSoundUrl(context); + Uri soundUri = this.getDefaultSoundUrl(context); if (soundUri != null) { channel.setSound(soundUri, audioAttributes); } @@ -169,7 +178,7 @@ private void buildNotification(NotificationManagerCompat notificationManager, Lo // support multiline text mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(localNotification.getBody())); - String sound = localNotification.getSound(context); + String sound = localNotification.getSound(context, getDefaultSound(context)); if (sound != null) { Uri soundUri = Uri.parse(sound); // Grant permission to use sound @@ -197,9 +206,9 @@ private void buildNotification(NotificationManagerCompat notificationManager, Lo mBuilder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE); mBuilder.setOnlyAlertOnce(true); - mBuilder.setSmallIcon(localNotification.getSmallIcon(context)); + mBuilder.setSmallIcon(localNotification.getSmallIcon(context, getDefaultSmallIcon(context))); - String iconColor = localNotification.getIconColor(); + String iconColor = localNotification.getIconColor(config.getString(CONFIG_KEY_PREFIX + "iconColor")); if (iconColor != null) { try { mBuilder.setColor(Color.parseColor(iconColor)); @@ -360,4 +369,46 @@ public boolean areNotificationsEnabled(){ NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); return notificationManager.areNotificationsEnabled(); } + + public Uri getDefaultSoundUrl(Context context){ + int soundId = this.getDefaultSound(context); + if (soundId != AssetUtil.RESOURCE_ID_ZERO_VALUE) { + return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + soundId); + } + return null; + } + + private int getDefaultSound(Context context){ + if(defaultSoundID != AssetUtil.RESOURCE_ID_ZERO_VALUE) return defaultSoundID; + + int resId = AssetUtil.RESOURCE_ID_ZERO_VALUE; + String soundConfigResourceName = config.getString(CONFIG_KEY_PREFIX + "sound"); + soundConfigResourceName = AssetUtil.getResourceBaseName(soundConfigResourceName); + + if(soundConfigResourceName != null){ + resId = AssetUtil.getResourceID(context, soundConfigResourceName, "raw"); + } + + defaultSoundID = resId; + return resId; + } + + private int getDefaultSmallIcon(Context context){ + if(defaultSmallIconID != AssetUtil.RESOURCE_ID_ZERO_VALUE) return defaultSmallIconID; + + int resId = AssetUtil.RESOURCE_ID_ZERO_VALUE; + String smallIconConfigResourceName = config.getString(CONFIG_KEY_PREFIX + "smallIcon"); + smallIconConfigResourceName = AssetUtil.getResourceBaseName(smallIconConfigResourceName); + + if(smallIconConfigResourceName != null){ + resId = AssetUtil.getResourceID(context, smallIconConfigResourceName, "drawable"); + } + + if(resId == AssetUtil.RESOURCE_ID_ZERO_VALUE){ + resId = android.R.drawable.ic_dialog_info; + } + + defaultSmallIconID = resId; + return resId; + } } diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/AssetUtil.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/AssetUtil.java index 063329c251..da0ecf928d 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/AssetUtil.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/AssetUtil.java @@ -28,6 +28,7 @@ */ public final class AssetUtil { + public static final int RESOURCE_ID_ZERO_VALUE = 0; // Name of the storage folder private static final String STORAGE_FOLDER = "/capacitorassets"; @@ -345,4 +346,22 @@ private String getPkgName (Resources res) { return res == Resources.getSystem() ? "android" : context.getPackageName(); } + public static int getResourceID(Context context, String resourceName, String dir){ + return context.getResources().getIdentifier(resourceName, dir, context.getPackageName()); + } + + public static String getResourceBaseName (String resPath) { + if (resPath == null) return null; + + if (resPath.contains("/")) { + return resPath.substring(resPath.lastIndexOf('/') + 1); + } + + if (resPath.contains(".")) { + return resPath.substring(0, resPath.lastIndexOf('.')); + } + + return resPath; + } + }