From 36a1e71b9523c37d87236051e9e34f719365ba1e Mon Sep 17 00:00:00 2001 From: jcesarmobile <jcesarmobile@gmail.com> Date: Mon, 1 Jul 2019 14:19:49 +0200 Subject: [PATCH] fix(cordova): Read DisableDeploy preference before setting start path --- .../main/java/com/getcapacitor/Bridge.java | 19 ++++++++---- .../java/com/getcapacitor/BridgeActivity.java | 2 +- ios/Capacitor/Capacitor/CAPBridge.swift | 14 ++++----- .../Capacitor/CAPBridgeViewController.swift | 29 ++++++++++++++----- 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index e3afa4031..7cf9717bb 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -45,6 +45,7 @@ import com.getcapacitor.util.HostMask; import org.apache.cordova.CordovaInterfaceImpl; +import org.apache.cordova.CordovaPreferences; import org.apache.cordova.PluginManager; import org.json.JSONException; import org.json.JSONObject; @@ -102,6 +103,7 @@ public class Bridge { // A reference to the main WebView for the app private final WebView webView; public final CordovaInterfaceImpl cordovaInterface; + private CordovaPreferences preferences; // Our MessageHandler for sending and receiving data to the WebView private final MessageHandler msgHandler; @@ -134,11 +136,12 @@ public class Bridge { * @param context * @param webView */ - public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>> initialPlugins, CordovaInterfaceImpl cordovaInterface, PluginManager pluginManager) { + public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>> initialPlugins, CordovaInterfaceImpl cordovaInterface, PluginManager pluginManager, CordovaPreferences preferences) { this.context = context; this.webView = webView; this.initialPlugins = initialPlugins; this.cordovaInterface = cordovaInterface; + this.preferences = preferences; // Start our plugin execution threads and handlers handlerThread.start(); @@ -230,15 +233,21 @@ private boolean launchIntent(Uri url) { } }); - SharedPreferences prefs = getContext().getSharedPreferences(com.getcapacitor.plugin.WebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE); - String path = prefs.getString(com.getcapacitor.plugin.WebView.CAP_SERVER_PATH, null); - if (path != null && !path.isEmpty() && new File(path).exists()) { - setServerBasePath(path); + if (!isDeployDisabled()) { + SharedPreferences prefs = getContext().getSharedPreferences(com.getcapacitor.plugin.WebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE); + String path = prefs.getString(com.getcapacitor.plugin.WebView.CAP_SERVER_PATH, null); + if (path != null && !path.isEmpty() && new File(path).exists()) { + setServerBasePath(path); + } } // Get to work webView.loadUrl(appUrl); } + public boolean isDeployDisabled() { + return preferences.getBoolean("DisableDeploy", false); + } + public void handleAppUrlLoadError(Exception ex) { if (ex instanceof SocketTimeoutException) { diff --git a/android/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java b/android/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java index 2128bbafa..423bcc048 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java +++ b/android/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java @@ -74,7 +74,7 @@ protected void load(Bundle savedInstanceState) { pluginManager = mockWebView.getPluginManager(); cordovaInterface.onCordovaInit(pluginManager); - bridge = new Bridge(this, webView, initialPlugins, cordovaInterface, pluginManager); + bridge = new Bridge(this, webView, initialPlugins, cordovaInterface, pluginManager, preferences); Splash.showOnLaunch(this); diff --git a/ios/Capacitor/Capacitor/CAPBridge.swift b/ios/Capacitor/Capacitor/CAPBridge.swift index e69691c79..278c5b9a4 100644 --- a/ios/Capacitor/Capacitor/CAPBridge.swift +++ b/ios/Capacitor/Capacitor/CAPBridge.swift @@ -307,14 +307,12 @@ enum BridgeError: Error { } func registerCordovaPlugins() { - let cordovaParser = CDVConfigParser.init(); - let configUrl = Bundle.main.url(forResource: "config", withExtension: "xml") - let configParser = XMLParser(contentsOf: configUrl!)!; - configParser.delegate = cordovaParser - configParser.parse() - cordovaPluginManager = CDVPluginManager.init(parser: cordovaParser, viewController: self.viewController, webView: self.getWebView()) - if cordovaParser.startupPluginNames.count > 0 { - for pluginName in cordovaParser.startupPluginNames { + guard let bridgeVC = self.viewController as? CAPBridgeViewController else { + return + } + cordovaPluginManager = CDVPluginManager.init(parser: bridgeVC.cordovaParser, viewController: self.viewController, webView: self.getWebView()) + if bridgeVC.cordovaParser.startupPluginNames.count > 0 { + for pluginName in bridgeVC.cordovaParser.startupPluginNames { _ = cordovaPluginManager?.getCommandInstance(pluginName as? String) } } diff --git a/ios/Capacitor/Capacitor/CAPBridgeViewController.swift b/ios/Capacitor/Capacitor/CAPBridgeViewController.swift index 4bf1ce6bb..fab84d934 100644 --- a/ios/Capacitor/Capacitor/CAPBridgeViewController.swift +++ b/ios/Capacitor/Capacitor/CAPBridgeViewController.swift @@ -5,6 +5,7 @@ import UIKit import WebKit +import Cordova public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScriptMessageHandler, WKUIDelegate, WKNavigationDelegate { @@ -17,7 +18,7 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr public var bridgedViewController: UIViewController? { return self } - + public let cordovaParser = CDVConfigParser.init(); private var hostname: String? private var allowNavigationConfig: [String]? private var basePath: String = "" @@ -34,6 +35,10 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr private var handler: CAPAssetHandler? override public func loadView() { + let configUrl = Bundle.main.url(forResource: "config", withExtension: "xml") + let configParser = XMLParser(contentsOf: configUrl!)!; + configParser.delegate = cordovaParser + configParser.parse() guard let startPath = self.getStartPath() else { return } @@ -84,18 +89,26 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr return nil } - let defaults = UserDefaults.standard - let persistedPath = defaults.string(forKey: "serverBasePath") - if (persistedPath != nil && !persistedPath!.isEmpty) { - let libPath = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0] - let cordovaDataDirectory = (libPath as NSString).appendingPathComponent("NoCloud") - let snapshots = (cordovaDataDirectory as NSString).appendingPathComponent("ionic_built_snapshots") - startPath = (snapshots as NSString).appendingPathComponent((persistedPath! as NSString).lastPathComponent) + if !isDeployDisabled() { + let defaults = UserDefaults.standard + let persistedPath = defaults.string(forKey: "serverBasePath") + if (persistedPath != nil && !persistedPath!.isEmpty) { + let libPath = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0] + let cordovaDataDirectory = (libPath as NSString).appendingPathComponent("NoCloud") + let snapshots = (cordovaDataDirectory as NSString).appendingPathComponent("ionic_built_snapshots") + startPath = (snapshots as NSString).appendingPathComponent((persistedPath! as NSString).lastPathComponent) + } } + self.basePath = startPath return startPath } + func isDeployDisabled() -> Bool { + let val = cordovaParser.settings.object(forKey: "DisableDeploy".lowercased()) as? NSString + return val?.boolValue ?? false + } + override public func viewDidLoad() { super.viewDidLoad() self.becomeFirstResponder()